Skip to content

Commit ca54136

Browse files
committed
Extract normalizeRepoKey helper for .git suffix stripping
1 parent 6b43ee5 commit ca54136

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

apps/code/src/main/services/folders/service.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ describe("FoldersService", () => {
296296
]);
297297
});
298298

299-
it("strips .git suffix from remote repo name in display name", async () => {
299+
it("strips .git suffix from remote repo name in display name (defensive against legacy data)", async () => {
300300
const repos = [
301301
{
302302
id: "folder-1",
303-
path: "/home/user/MURZINI",
303+
path: "/home/user/my-billing-fork",
304304
remoteUrl: "PostHog/billing.git",
305305
lastAccessedAt: "2024-01-01T00:00:00.000Z",
306306
createdAt: "2024-01-01T00:00:00.000Z",
@@ -312,7 +312,7 @@ describe("FoldersService", () => {
312312

313313
const result = await service.getFolders();
314314

315-
expect(result[0].name).toBe("MURZINI (billing)");
315+
expect(result[0].name).toBe("my-billing-fork (billing)");
316316
});
317317

318318
it("uses remote repo name in display name when it differs from local dir", async () => {

apps/code/src/main/services/folders/service.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import path from "node:path";
33
import { getRemoteUrl, isGitRepository } from "@posthog/git/queries";
44
import { InitRepositorySaga } from "@posthog/git/sagas/init";
55

6+
import { normalizeRepoKey } from "@shared/utils/repo";
7+
68
function extractRepoKey(url: string): string | null {
79
const httpsMatch = url.match(/github\.com\/([^/]+\/[^/]+)/);
8-
if (httpsMatch) return httpsMatch[1].replace(/\.git$/, "");
10+
if (httpsMatch) return normalizeRepoKey(httpsMatch[1]);
911

1012
const sshMatch = url.match(/github\.com:([^/]+\/[^/]+)/);
11-
if (sshMatch) return sshMatch[1].replace(/\.git$/, "");
13+
if (sshMatch) return normalizeRepoKey(sshMatch[1]);
1214

1315
return null;
1416
}
@@ -86,12 +88,7 @@ export class FoldersService {
8688
): string {
8789
const localName = path.basename(repoPath);
8890
if (remoteUrl) {
89-
const repoName = remoteUrl
90-
.trim()
91-
.split("/")
92-
.pop()
93-
?.replace(/\.git$/, "")
94-
?.trim();
91+
const repoName = normalizeRepoKey(remoteUrl).split("/").pop();
9592
if (repoName && repoName.toLowerCase() !== localName.toLowerCase()) {
9693
return `${localName} (${repoName})`;
9794
}

apps/code/src/renderer/features/sidebar/components/TaskListView.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@phosphor-icons/react";
1616
import { Box, Flex, Popover, Text } from "@radix-ui/themes";
1717
import { useWorkspace } from "@renderer/features/workspace/hooks/useWorkspace";
18+
import { normalizeRepoKey } from "@shared/utils/repo";
1819
import { useNavigationStore } from "@stores/navigationStore";
1920
import { useCallback, useEffect } from "react";
2021
import type { TaskData, TaskGroup } from "../hooks/useSidebarData";
@@ -378,10 +379,9 @@ export function TaskListView({
378379
const isExpanded = !collapsedSections.has(group.id);
379380
const folder = folders.find(
380381
(f) =>
381-
f.remoteUrl
382-
?.trim()
383-
.replace(/\.git$/, "")
384-
.toLowerCase() === group.id.trim().toLowerCase() ||
382+
(f.remoteUrl &&
383+
normalizeRepoKey(f.remoteUrl).toLowerCase() ===
384+
normalizeRepoKey(group.id).toLowerCase()) ||
385385
f.path === group.id,
386386
);
387387
const groupFolderId =

apps/code/src/shared/utils/repo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function normalizeRepoKey(key: string): string {
2+
return key.trim().replace(/\.git$/, "");
3+
}

0 commit comments

Comments
 (0)