The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 18:30 UTC
Handle branch recency lookup failures in branch listing
- move branch recency parsing into `readBranchRecency` - fall back to an empty recency map if lookup fails - add test to ensure branches still list when recency read times out Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Feb 12, 2026 at 23:28 UTC
6b916d3a6fbe2145ac53776c73ba7b3d0aa969de
2 files changed
+52
-25
apps/server/src/git.test.ts
+17
-1
@@ -2,7 +2,7 @@ import { existsSync } from "node:fs";
2
import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
import { tmpdir } from "node:os";
4
import path from "node:path";
5
-import { describe, expect, it } from "vitest";
5
+import { describe, expect, it, vi } from "vitest";
6
7
import {
8
GitCoreService,
@@ -603,5 +603,21 @@ describe("git integration", () => {
603
const skipped = await core.pushCurrentBranch(tmp.path, null);
604
expect(skipped.status).toBe("skipped_up_to_date");
605
});
606
+
607
+ it("lists branches when recency lookup fails", async () => {
608
+ await using tmp = await makeTmpDir();
609
+ await initRepoWithCommit(tmp.path);
610
+ const core = new GitCoreService();
611
+ const recencySpy = vi
612
+ .spyOn(core as any, "readBranchRecency")
613
+ .mockRejectedValueOnce(new Error("timeout"));
614
+
615
+ const result = await core.listBranches({ cwd: tmp.path });
616
+
617
+ expect(result.isRepo).toBe(true);
618
+ expect(result.branches.length).toBeGreaterThan(0);
619
+ expect(result.branches[0]?.current).toBe(true);
620
+ recencySpy.mockRestore();
621
+ });
622
});
623
});
apps/server/src/git.ts
+35
-24
@@ -312,7 +312,40 @@ export class GitCoreService {
312
return value.length > 0 ? value : null;
313
}
314
315
+ private async readBranchRecency(cwd: string): Promise<Map<string, number>> {
316
+ const branchRecency = await executeGit(
317
+ cwd,
318
+ ["for-each-ref", "--format=%(refname:short)%09%(committerdate:unix)", "refs/heads"],
319
+ {
320
+ timeoutMs: 15_000,
321
+ allowNonZeroExit: true,
322
+ },
323
+ );
324
+
325
+ const branchLastCommit = new Map<string, number>();
326
+ if (branchRecency.code !== 0) {
327
+ return branchLastCommit;
328
+ }
329
+
330
+ for (const line of branchRecency.stdout.split("\n")) {
331
+ if (line.length === 0) {
332
+ continue;
333
+ }
334
+ const [name, lastCommitRaw] = line.split("\t");
335
+ if (!name) {
336
+ continue;
337
+ }
338
+ const lastCommit = Number.parseInt(lastCommitRaw ?? "0", 10);
339
+ branchLastCommit.set(name, Number.isFinite(lastCommit) ? lastCommit : 0);
340
+ }
341
+
342
+ return branchLastCommit;
343
+ }
344
+
345
async listBranches(input: GitListBranchesInput): Promise<GitListBranchesResult> {
346
+ const branchRecencyPromise = this.readBranchRecency(input.cwd).catch(
347
+ () => new Map<string, number>(),
348
+ );
349
const result = await executeGit(input.cwd, ["branch", "--no-color"], {
350
timeoutMs: 10_000,
351
allowNonZeroExit: true,
@@ -326,7 +359,7 @@ export class GitCoreService {
359
throw new Error(stderr || "git branch failed");
360
}
361
329
- const [defaultRef, worktreeList, branchRecency] = await Promise.all([
362
+ const [defaultRef, worktreeList, branchLastCommit] = await Promise.all([
363
executeGit(input.cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"], {
364
timeoutMs: 5_000,
365
allowNonZeroExit: true,
@@ -335,14 +368,7 @@ export class GitCoreService {
368
timeoutMs: 5_000,
369
allowNonZeroExit: true,
370
}),
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
- ),
371
+ branchRecencyPromise,
372
]);
373
const defaultBranch =
374
defaultRef.code === 0
@@ -364,21 +390,6 @@ export class GitCoreService {
390
}
391
}
392
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
-
393
const branches = result.stdout
394
.split("\n")
395
.map((line) => line.trim())