Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions apps/web/src/components/BranchToolbar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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", () => {
Expand Down
33 changes: 3 additions & 30 deletions packages/shared/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,6 @@ export function parseOriginUrlFromGitConfig(configText: string): string | null {
return originUrl ?? firstRemoteUrl;
}

function deriveLocalBranchNameCandidatesFromRemoteRef(
branchName: string,
remoteName?: string,
): ReadonlyArray<string> {
const candidates = new Set<string>();
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, "-");
Expand All @@ -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);
});
}

Expand Down
Loading