From 3e5787d742e35c848218a1c390de4bb44967a860 Mon Sep 17 00:00:00 2001 From: vihaan Date: Tue, 3 Nov 2026 14:57:41 -0800 Subject: [PATCH] refactor(shared): simplify remote branch filtering --- .../components/BranchToolbar.logic.test.ts | 23 +++++++++---- packages/shared/src/git.ts | 33 ++----------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.ts index 903a20066b39e..8b2222c4f74a6 100644 --- a/apps/web/src/components/BranchToolbar.logic.test.ts +++ b/apps/web/src/components/BranchToolbar.logic.test.ts @@ -101,16 +101,23 @@ describe("deriveLocalBranchNameFromRemoteRef", () => { }); describe("dedupeRemoteBranchesWithLocalMatches", () => { - it("hides remote refs when the matching local ref exists", () => { + it.each([ + ["feature/demo", "origin/feature/demo", true], + ["feature/demo", "feature/demo", false], + ["demo", "demo", true], + ["origin/", "origin/", true], + ["/demo", "/demo", true], + ["", "", false], + ])("filters origin ref %s / %s (hidden: %s)", (localName, remoteName, hidden) => { const input: VcsRef[] = [ { - name: "feature/demo", + name: localName, current: false, isDefault: false, worktreePath: null, }, { - name: "origin/feature/demo", + name: remoteName, isRemote: true, remoteName: "origin", current: false, @@ -127,10 +134,12 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => { }, ]; - expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([ - "feature/demo", - "origin/feature/remote-only", - ]); + const result = dedupeRemoteBranchesWithLocalMatches(input); + const expected = hidden ? [input[0], input[2]] : input; + expect(result).toEqual(expected); + for (const [index, ref] of result.entries()) { + expect(ref).toBe(expected[index]); + } }); it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => { diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts index 4ac89394c213f..d4562ed7a9d59 100644 --- a/packages/shared/src/git.ts +++ b/packages/shared/src/git.ts @@ -242,26 +242,6 @@ export function parseOriginUrlFromGitConfig(configText: string): string | null { return originUrl ?? firstRemoteUrl; } -function deriveLocalBranchNameCandidatesFromRemoteRef( - branchName: string, - remoteName?: string, -): ReadonlyArray { - const candidates = new Set(); - const firstSlashCandidate = deriveLocalBranchNameFromRemoteRef(branchName); - if (firstSlashCandidate.length > 0) { - candidates.add(firstSlashCandidate); - } - - if (remoteName) { - const remotePrefix = `${remoteName}/`; - if (branchName.startsWith(remotePrefix) && branchName.length > remotePrefix.length) { - candidates.add(branchName.slice(remotePrefix.length)); - } - } - - return [...candidates]; -} - // Replace only Git-rejected whitespace; nonbreaking spaces are valid ref characters. export function sanitizeNewRefName(rawName: string): string { return rawName.trim().replace(/[ \t\n\r\f\v]+/g, "-"); @@ -277,19 +257,12 @@ export function dedupeRemoteBranchesWithLocalMatches( ); return refs.filter((refName) => { - if (!refName.isRemote) { - return true; - } - - if (refName.remoteName !== "origin") { + if (!refName.isRemote || refName.remoteName !== "origin") { return true; } - const localBranchCandidates = deriveLocalBranchNameCandidatesFromRemoteRef( - refName.name, - refName.remoteName, - ); - return !localBranchCandidates.some((candidate) => localBranchNames.has(candidate)); + const localBranchName = deriveLocalBranchNameFromRemoteRef(refName.name); + return localBranchName.length === 0 || !localBranchNames.has(localBranchName); }); }