From 21580dc1b0860e5cc711ab2cc494b52e64103d92 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Feb 2026 23:24:37 -0800 Subject: [PATCH] Prioritize current and default branches in branch list ordering - Keep the current branch first - Place the default branch second - Sort remaining branches by most recent commit and update tests for this behavior Co-authored-by: codex --- apps/server/src/git.test.ts | 49 ++++++++++++++++++++++++++++++++++--- apps/server/src/git.ts | 6 +++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/apps/server/src/git.test.ts b/apps/server/src/git.test.ts index e0612f09f3e57ce1f95bfcb458373f1409965401..9155cb04e2301f9e1078c90a90a1f5f4cd39582d 100644 --- a/apps/server/src/git.test.ts +++ b/apps/server/src/git.test.ts @@ -125,7 +125,7 @@ describe("git integration", () => { expect(current!.current).toBe(true); }); - it("sorts branches by most recent commit first", async () => { + it("keeps current branch first and sorts the remaining branches by recency", async () => { await using tmp = await makeTmpDir(); await initRepoWithCommit(tmp.path); const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find( @@ -153,11 +153,54 @@ describe("git integration", () => { "newer branch change", ); - // Switch away to show sort order is recency-based, not "current"-based. + // Switch away to show current branch is pinned, then remaining branches are recency-sorted. await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" }); const result = await listGitBranches({ cwd: tmp.path }); - expect(result.branches[0]!.name).toBe("newer-branch"); + expect(result.branches[0]!.name).toBe("older-branch"); + expect(result.branches[1]!.name).toBe("newer-branch"); + }); + + it("keeps default branch right after current branch", async () => { + await using tmp = await makeTmpDir(); + await using remote = await makeTmpDir(); + await initRepoWithCommit(tmp.path); + const defaultBranch = (await listGitBranches({ cwd: tmp.path })).branches.find( + (branch) => branch.current, + )!.name; + + await git(remote.path, "init --bare"); + await git(tmp.path, `remote add origin ${JSON.stringify(remote.path)}`); + await git(tmp.path, `push -u origin ${defaultBranch}`); + await git(tmp.path, `remote set-head origin ${defaultBranch}`); + + await createGitBranch({ cwd: tmp.path, branch: "current-branch" }); + await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" }); + await commitWithDate( + tmp.path, + "current.txt", + "current change\n", + "Thu, 1 Jan 2037 00:00:00 +0000", + "current change", + ); + + await checkoutGitBranch({ cwd: tmp.path, branch: defaultBranch }); + await createGitBranch({ cwd: tmp.path, branch: "newer-branch" }); + await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" }); + await commitWithDate( + tmp.path, + "newer.txt", + "newer change\n", + "Fri, 1 Jan 2038 00:00:00 +0000", + "newer change", + ); + + await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" }); + + const result = await listGitBranches({ cwd: tmp.path }); + expect(result.branches[0]!.name).toBe("current-branch"); + expect(result.branches[1]!.name).toBe(defaultBranch); + expect(result.branches[2]!.name).toBe("newer-branch"); }); it("lists multiple branches after creating them", async () => { diff --git a/apps/server/src/git.ts b/apps/server/src/git.ts index 734ee3038b062684720b14458e36c6daf282d16e..39e4e1f93d9ad01bcbd201018fa49fff1d4f349a 100644 --- a/apps/server/src/git.ts +++ b/apps/server/src/git.ts @@ -393,11 +393,13 @@ export class GitCoreService { }; }) .toSorted((a, b) => { + const aPriority = a.current ? 0 : a.isDefault ? 1 : 2; + const bPriority = b.current ? 0 : b.isDefault ? 1 : 2; + if (aPriority !== bPriority) return aPriority - bPriority; + const aLastCommit = branchLastCommit.get(a.name) ?? 0; const bLastCommit = branchLastCommit.get(b.name) ?? 0; if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; - if (a.current !== b.current) return a.current ? -1 : 1; - if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1; return a.name.localeCompare(b.name); }); -- 2.54.0