authorgravatar for julius0216@outlook.comJulius Marminge <julius0216@outlook.com> 2026-02-12 23:24:37-08:00
committergravatar for jmarminge@gmail.comJulius Marminge <jmarminge@gmail.com> 2026-02-12 23:29:44-08:00
log21580dc1b0860e5cc711ab2cc494b52e64103d92
tree5751453d4f6acdf4558a83abed383dc82101ccf4
parentba9585966c948d761054d54ccaaa71a1420bd394

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 <codex@users.noreply.github.com>

2 files changed, 50 insertions(+), 5 deletions(-)

apps/server/src/git.test.ts+46-3
......@@ -125,7 +125,7 @@ describe("git integration", () => {
125125 expect(current!.current).toBe(true);
126126 });
127127
128 it("sorts branches by most recent commit first", async () => {
128 it("keeps current branch first and sorts the remaining branches by recency", async () => {
129129 await using tmp = await makeTmpDir();
130130 await initRepoWithCommit(tmp.path);
131131 const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
......@@ -153,11 +153,54 @@ describe("git integration", () => {
153153 "newer branch change",
154154 );
155155
156 // Switch away to show sort order is recency-based, not "current"-based.
156 // Switch away to show current branch is pinned, then remaining branches are recency-sorted.
157157 await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" });
158158
159159 const result = await listGitBranches({ cwd: tmp.path });
160 expect(result.branches[0]!.name).toBe("newer-branch");
160 expect(result.branches[0]!.name).toBe("older-branch");
161 expect(result.branches[1]!.name).toBe("newer-branch");
162 });
163
164 it("keeps default branch right after current branch", async () => {
165 await using tmp = await makeTmpDir();
166 await using remote = await makeTmpDir();
167 await initRepoWithCommit(tmp.path);
168 const defaultBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
169 (branch) => branch.current,
170 )!.name;
171
172 await git(remote.path, "init --bare");
173 await git(tmp.path, `remote add origin ${JSON.stringify(remote.path)}`);
174 await git(tmp.path, `push -u origin ${defaultBranch}`);
175 await git(tmp.path, `remote set-head origin ${defaultBranch}`);
176
177 await createGitBranch({ cwd: tmp.path, branch: "current-branch" });
178 await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" });
179 await commitWithDate(
180 tmp.path,
181 "current.txt",
182 "current change\n",
183 "Thu, 1 Jan 2037 00:00:00 +0000",
184 "current change",
185 );
186
187 await checkoutGitBranch({ cwd: tmp.path, branch: defaultBranch });
188 await createGitBranch({ cwd: tmp.path, branch: "newer-branch" });
189 await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" });
190 await commitWithDate(
191 tmp.path,
192 "newer.txt",
193 "newer change\n",
194 "Fri, 1 Jan 2038 00:00:00 +0000",
195 "newer change",
196 );
197
198 await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" });
199
200 const result = await listGitBranches({ cwd: tmp.path });
201 expect(result.branches[0]!.name).toBe("current-branch");
202 expect(result.branches[1]!.name).toBe(defaultBranch);
203 expect(result.branches[2]!.name).toBe("newer-branch");
161204 });
162205
163206 it("lists multiple branches after creating them", async () => {
apps/server/src/git.ts+4-2
......@@ -393,11 +393,13 @@ export class GitCoreService {
393393 };
394394 })
395395 .toSorted((a, b) => {
396 const aPriority = a.current ? 0 : a.isDefault ? 1 : 2;
397 const bPriority = b.current ? 0 : b.isDefault ? 1 : 2;
398 if (aPriority !== bPriority) return aPriority - bPriority;
399
396400 const aLastCommit = branchLastCommit.get(a.name) ?? 0;
397401 const bLastCommit = branchLastCommit.get(b.name) ?? 0;
398402 if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit;
399 if (a.current !== b.current) return a.current ? -1 : 1;
400 if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1;
401403 return a.name.localeCompare(b.name);
402404 });
403405