The best way to control Claude Code, Codex and more from any computer or phone
Sort git branches by latest commit timestamp
- Order `listGitBranches` by each branch's most recent commit date - Keep current/default/name tiebreakers for stable ordering - Add integration test coverage with dated commits to verify recency sort
Julius Marminge committed
Feb 12, 2026 at 23:18 UTC
ba9585966c948d761054d54ccaaa71a1420bd394
2 files changed
+75
-5
apps/server/src/git.test.ts
+48
-4
@@ -51,6 +51,25 @@ async function initRepoWithCommit(cwd: string): Promise<void> {
51
await git(cwd, "commit -m 'initial commit'");
52
}
53
54
+async function commitWithDate(
55
+ cwd: string,
56
+ fileName: string,
57
+ fileContents: string,
58
+ dateIsoString: string,
59
+ message: string,
60
+): Promise<void> {
61
+ await writeFile(path.join(cwd, fileName), fileContents);
62
+ await git(cwd, `add ${fileName}`);
63
+ const commitResult = await runTerminalCommand({
64
+ command: `GIT_AUTHOR_DATE="${dateIsoString}" GIT_COMMITTER_DATE="${dateIsoString}" git commit -m "${message}"`,
65
+ cwd,
66
+ timeoutMs: 10_000,
67
+ });
68
+ if (commitResult.code !== 0) {
69
+ throw new Error(`git dated commit failed: ${commitResult.stderr}`);
70
+ }
71
+}
72
+
73
// ── Tests ──
74
75
describe("git integration", () => {
@@ -106,14 +125,39 @@ describe("git integration", () => {
125
expect(current!.current).toBe(true);
126
});
127
109
- it("sorts current branch first", async () => {
128
+ it("sorts branches by most recent commit first", async () => {
129
await using tmp = await makeTmpDir();
130
await initRepoWithCommit(tmp.path);
112
- await createGitBranch({ cwd: tmp.path, branch: "aaa-first-alpha" });
113
- await createGitBranch({ cwd: tmp.path, branch: "zzz-last" });
131
+ const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
132
+ (branch) => branch.current,
133
+ )!.name;
134
+
135
+ await createGitBranch({ cwd: tmp.path, branch: "older-branch" });
136
+ await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" });
137
+ await commitWithDate(
138
+ tmp.path,
139
+ "older.txt",
140
+ "older branch change\n",
141
+ "Thu, 1 Jan 2037 00:00:00 +0000",
142
+ "older branch change",
143
+ );
144
+
145
+ await checkoutGitBranch({ cwd: tmp.path, branch: initialBranch });
146
+ await createGitBranch({ cwd: tmp.path, branch: "newer-branch" });
147
+ await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" });
148
+ await commitWithDate(
149
+ tmp.path,
150
+ "newer.txt",
151
+ "newer branch change\n",
152
+ "Fri, 1 Jan 2038 00:00:00 +0000",
153
+ "newer branch change",
154
+ );
155
+
156
+ // Switch away to show sort order is recency-based, not "current"-based.
157
+ await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" });
158
159
const result = await listGitBranches({ cwd: tmp.path });
116
- expect(result.branches[0]!.current).toBe(true);
160
+ expect(result.branches[0]!.name).toBe("newer-branch");
161
});
162
163
it("lists multiple branches after creating them", async () => {
apps/server/src/git.ts
+27
-1
@@ -326,7 +326,7 @@ export class GitCoreService {
326
throw new Error(stderr || "git branch failed");
327
}
328
329
- const [defaultRef, worktreeList] = await Promise.all([
329
+ const [defaultRef, worktreeList, branchRecency] = await Promise.all([
330
executeGit(input.cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"], {
331
timeoutMs: 5_000,
332
allowNonZeroExit: true,
@@ -335,6 +335,14 @@ export class GitCoreService {
335
timeoutMs: 5_000,
336
allowNonZeroExit: true,
337
}),
338
+ executeGit(
339
+ input.cwd,
340
+ ["for-each-ref", "--format=%(refname:short)%09%(committerdate:unix)", "refs/heads"],
341
+ {
342
+ timeoutMs: 5_000,
343
+ allowNonZeroExit: true,
344
+ },
345
+ ),
346
]);
347
const defaultBranch =
348
defaultRef.code === 0
@@ -356,6 +364,21 @@ export class GitCoreService {
364
}
365
}
366
367
+ const branchLastCommit = new Map<string, number>();
368
+ if (branchRecency.code === 0) {
369
+ for (const line of branchRecency.stdout.split("\n")) {
370
+ if (line.length === 0) {
371
+ continue;
372
+ }
373
+ const [name, lastCommitRaw] = line.split("\t");
374
+ if (!name) {
375
+ continue;
376
+ }
377
+ const lastCommit = Number.parseInt(lastCommitRaw ?? "0", 10);
378
+ branchLastCommit.set(name, Number.isFinite(lastCommit) ? lastCommit : 0);
379
+ }
380
+ }
381
+
382
const branches = result.stdout
383
.split("\n")
384
.map((line) => line.trim())
@@ -370,6 +393,9 @@ export class GitCoreService {
393
};
394
})
395
.toSorted((a, b) => {
396
+ const aLastCommit = branchLastCommit.get(a.name) ?? 0;
397
+ const bLastCommit = branchLastCommit.get(b.name) ?? 0;
398
+ 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;
401
return a.name.localeCompare(b.name);