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, 16:00 UTC
Support remote branch listing and tracking checkout flow
- Include remote refs in branch listings, ordered after local branches - Auto-track remote branches on checkout when no local branch exists - Update branch picker to label remote branches and store local branch name after checkout
Julius Marminge committed
Feb 28, 2026 at 16:24 UTC
3c66cdf2fe2541b96077a4c6cc48bf60617baf0b
5 files changed
+205
-25
apps/server/src/git/Layers/GitCore.test.ts
+44
@@ -370,6 +370,50 @@ it.layer(TestLayer)("git integration", (it) => {
370
expect(result.branches.every((b) => b.isDefault === false)).toBe(true);
371
}),
372
);
373
+
374
+ it.effect("lists local branches first and remote branches last", () =>
375
+ Effect.gen(function* () {
376
+ const remote = yield* makeTmpDir();
377
+ const tmp = yield* makeTmpDir();
378
+
379
+ yield* git(remote, ["init", "--bare"]);
380
+ yield* initRepoWithCommit(tmp);
381
+ const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
382
+ (branch) => branch.current,
383
+ )!.name;
384
+
385
+ yield* git(tmp, ["remote", "add", "origin", remote]);
386
+ yield* git(tmp, ["push", "-u", "origin", defaultBranch]);
387
+
388
+ yield* createGitBranch({ cwd: tmp, branch: "feature/local-only" });
389
+
390
+ const remoteOnlyBranch = "feature/remote-only";
391
+ yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch });
392
+ yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]);
393
+ yield* git(tmp, ["push", "-u", "origin", remoteOnlyBranch]);
394
+ yield* git(tmp, ["checkout", defaultBranch]);
395
+ yield* git(tmp, ["branch", "-D", remoteOnlyBranch]);
396
+
397
+ const result = yield* listGitBranches({ cwd: tmp });
398
+ const firstRemoteIndex = result.branches.findIndex((branch) => branch.isRemote);
399
+
400
+ expect(firstRemoteIndex).toBeGreaterThan(0);
401
+ expect(result.branches.slice(0, firstRemoteIndex).every((branch) => !branch.isRemote)).toBe(
402
+ true,
403
+ );
404
+ expect(result.branches.slice(firstRemoteIndex).every((branch) => branch.isRemote)).toBe(
405
+ true,
406
+ );
407
+ expect(
408
+ result.branches.some((branch) => branch.name === "feature/local-only" && !branch.isRemote),
409
+ ).toBe(true);
410
+ expect(
411
+ result.branches.some(
412
+ (branch) => branch.name === "origin/feature/remote-only" && branch.isRemote,
413
+ ),
414
+ ).toBe(true);
415
+ }),
416
+ );
417
});
418
419
// ── checkoutGitBranch ──
apps/server/src/git/Layers/GitCore.ts
+136
-18
@@ -76,6 +76,33 @@ function parsePorcelainPath(line: string): string | null {
76
return filePath.length > 0 ? filePath : null;
77
}
78
79
+function parseBranchLine(line: string): { name: string; current: boolean } | null {
80
+ const trimmed = line.trim();
81
+ if (trimmed.length === 0) return null;
82
+
83
+ const name = trimmed.replace(/^[*+]\s+/, "");
84
+ // Exclude symbolic refs like: "origin/HEAD -> origin/main".
85
+ if (name.includes(" -> ")) return null;
86
+
87
+ return {
88
+ name,
89
+ current: trimmed.startsWith("* "),
90
+ };
91
+}
92
+
93
+function parseRemoteTrackingCandidate(
94
+ branchName: string,
95
+): { remoteRef: string; localBranch: string } | null {
96
+ const separatorIndex = branchName.indexOf("/");
97
+ if (separatorIndex <= 0 || separatorIndex === branchName.length - 1) return null;
98
+ const localBranch = branchName.slice(separatorIndex + 1).trim();
99
+ if (localBranch.length === 0) return null;
100
+ return {
101
+ remoteRef: branchName.trim(),
102
+ localBranch,
103
+ };
104
+}
105
+
106
function commandLabel(args: readonly string[]): string {
107
return `git ${args.join(" ")}`;
108
}
@@ -264,7 +291,12 @@ const makeGitCore = Effect.gen(function* () {
291
const branchRecency = yield* executeGit(
292
"GitCore.readBranchRecency",
293
cwd,
267
- ["for-each-ref", "--format=%(refname:short)%09%(committerdate:unix)", "refs/heads"],
294
+ [
295
+ "for-each-ref",
296
+ "--format=%(refname:short)%09%(committerdate:unix)",
297
+ "refs/heads",
298
+ "refs/remotes",
299
+ ],
300
{
301
timeoutMs: 15_000,
302
allowNonZeroExit: true,
@@ -564,7 +596,7 @@ const makeGitCore = Effect.gen(function* () {
596
const branchRecencyPromise = readBranchRecency(input.cwd).pipe(
597
Effect.catch(() => Effect.succeed(new Map<string, number>())),
598
);
567
- const result = yield* executeGit(
599
+ const localBranchResult = yield* executeGit(
600
"GitCore.listBranches.branchNoColor",
601
input.cwd,
602
["branch", "--no-color"],
@@ -574,8 +606,8 @@ const makeGitCore = Effect.gen(function* () {
606
},
607
);
608
577
- if (result.code !== 0) {
578
- const stderr = result.stderr.trim();
609
+ if (localBranchResult.code !== 0) {
610
+ const stderr = localBranchResult.stderr.trim();
611
if (stderr.toLowerCase().includes("not a git repository")) {
612
return { branches: [], isRepo: false };
613
}
@@ -587,7 +619,7 @@ const makeGitCore = Effect.gen(function* () {
619
);
620
}
621
590
- const [defaultRef, worktreeList, branchLastCommit] = yield* Effect.all(
622
+ const [defaultRef, worktreeList, remoteBranchResult, branchLastCommit] = yield* Effect.all(
623
[
624
executeGit(
625
"GitCore.listBranches.defaultRef",
@@ -607,6 +639,15 @@ const makeGitCore = Effect.gen(function* () {
639
allowNonZeroExit: true,
640
},
641
),
642
+ executeGit(
643
+ "GitCore.listBranches.remoteBranches",
644
+ input.cwd,
645
+ ["branch", "--no-color", "--remotes"],
646
+ {
647
+ timeoutMs: 10_000,
648
+ allowNonZeroExit: true,
649
+ },
650
+ ),
651
branchRecencyPromise,
652
],
653
{ concurrency: "unbounded" },
@@ -636,19 +677,17 @@ const makeGitCore = Effect.gen(function* () {
677
}
678
}
679
639
- const branches = result.stdout
680
+ const localBranches = localBranchResult.stdout
681
.split("\n")
641
- .map((line) => line.trim())
642
- .filter((line) => line.length > 0)
643
- .map((line) => {
644
- const name = line.replace(/^[*+]\s+/, "");
645
- return {
646
- name,
647
- current: line.startsWith("* "),
648
- isDefault: name === defaultBranch,
649
- worktreePath: worktreeMap.get(name) ?? null,
650
- };
651
- })
682
+ .map(parseBranchLine)
683
+ .filter((branch): branch is { name: string; current: boolean } => branch !== null)
684
+ .map((branch) => ({
685
+ name: branch.name,
686
+ current: branch.current,
687
+ isRemote: false,
688
+ isDefault: branch.name === defaultBranch,
689
+ worktreePath: worktreeMap.get(branch.name) ?? null,
690
+ }))
691
.toSorted((a, b) => {
692
const aPriority = a.current ? 0 : a.isDefault ? 1 : 2;
693
const bPriority = b.current ? 0 : b.isDefault ? 1 : 2;
@@ -660,6 +699,29 @@ const makeGitCore = Effect.gen(function* () {
699
return a.name.localeCompare(b.name);
700
});
701
702
+ const remoteBranches =
703
+ remoteBranchResult.code === 0
704
+ ? remoteBranchResult.stdout
705
+ .split("\n")
706
+ .map(parseBranchLine)
707
+ .filter((branch): branch is { name: string; current: boolean } => branch !== null)
708
+ .map((branch) => ({
709
+ name: branch.name,
710
+ current: false,
711
+ isRemote: true,
712
+ isDefault: false,
713
+ worktreePath: null,
714
+ }))
715
+ .toSorted((a, b) => {
716
+ const aLastCommit = branchLastCommit.get(a.name) ?? 0;
717
+ const bLastCommit = branchLastCommit.get(b.name) ?? 0;
718
+ if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit;
719
+ return a.name.localeCompare(b.name);
720
+ })
721
+ : [];
722
+
723
+ const branches = [...localBranches, ...remoteBranches];
724
+
725
return { branches, isRepo: true };
726
});
727
@@ -719,7 +781,63 @@ const makeGitCore = Effect.gen(function* () {
781
782
const checkoutBranch: GitCoreShape["checkoutBranch"] = (input) =>
783
Effect.gen(function* () {
722
- yield* executeGit("GitCore.checkoutBranch.checkout", input.cwd, ["checkout", input.branch], {
784
+ const trackingCandidate = parseRemoteTrackingCandidate(input.branch);
785
+ if (!trackingCandidate) {
786
+ yield* executeGit("GitCore.checkoutBranch.checkout", input.cwd, ["checkout", input.branch], {
787
+ timeoutMs: 10_000,
788
+ fallbackErrorMessage: "git checkout failed",
789
+ });
790
+
791
+ // Refresh upstream refs in the background so checkout remains responsive.
792
+ yield* Effect.sync(() => {
793
+ void Effect.runPromise(
794
+ refreshCheckedOutBranchUpstream(input.cwd).pipe(Effect.catch(() => Effect.void)),
795
+ );
796
+ });
797
+ return;
798
+ }
799
+
800
+ const [localInputExists, remoteExists, localTrackingExists] = yield* Effect.all(
801
+ [
802
+ executeGit(
803
+ "GitCore.checkoutBranch.localInputExists",
804
+ input.cwd,
805
+ ["show-ref", "--verify", "--quiet", `refs/heads/${input.branch}`],
806
+ {
807
+ timeoutMs: 5_000,
808
+ allowNonZeroExit: true,
809
+ },
810
+ ).pipe(Effect.map((result) => result.code === 0)),
811
+ executeGit(
812
+ "GitCore.checkoutBranch.remoteExists",
813
+ input.cwd,
814
+ ["show-ref", "--verify", "--quiet", `refs/remotes/${trackingCandidate.remoteRef}`],
815
+ {
816
+ timeoutMs: 5_000,
817
+ allowNonZeroExit: true,
818
+ },
819
+ ).pipe(Effect.map((result) => result.code === 0)),
820
+ executeGit(
821
+ "GitCore.checkoutBranch.localTrackingExists",
822
+ input.cwd,
823
+ ["show-ref", "--verify", "--quiet", `refs/heads/${trackingCandidate.localBranch}`],
824
+ {
825
+ timeoutMs: 5_000,
826
+ allowNonZeroExit: true,
827
+ },
828
+ ).pipe(Effect.map((result) => result.code === 0)),
829
+ ],
830
+ { concurrency: "unbounded" },
831
+ );
832
+
833
+ const checkoutArgs =
834
+ localInputExists
835
+ ? ["checkout", input.branch]
836
+ : remoteExists && !localTrackingExists
837
+ ? ["checkout", "--track", trackingCandidate.remoteRef]
838
+ : ["checkout", localTrackingExists ? trackingCandidate.localBranch : input.branch];
839
+
840
+ yield* executeGit("GitCore.checkoutBranch.checkout", input.cwd, checkoutArgs, {
841
timeoutMs: 10_000,
842
fallbackErrorMessage: "git checkout failed",
843
});
apps/server/src/git/Services/GitCore.ts
+1
-1
@@ -101,7 +101,7 @@ export interface GitCoreShape {
101
) => Effect.Effect<string | null, GitCommandError>;
102
103
/**
104
- * List local branches and branch metadata.
104
+ * List local + remote branches and branch metadata.
105
*/
106
readonly listBranches: (
107
input: GitListBranchesInput,
apps/web/src/components/BranchToolbar.tsx
+23
-6
@@ -31,6 +31,14 @@ interface BranchToolbarProps {
31
onComposerFocusRequest?: () => void;
32
}
33
34
+function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
35
+ const separatorIndex = branchName.indexOf("/");
36
+ if (separatorIndex <= 0 || separatorIndex === branchName.length - 1) {
37
+ return branchName;
38
+ }
39
+ return branchName.slice(separatorIndex + 1);
40
+}
41
+
42
export default function BranchToolbar({
43
threadId,
44
envMode,
@@ -174,10 +182,14 @@ export default function BranchToolbar({
182
return;
183
}
184
185
+ const selectedBranchName = branch.isRemote
186
+ ? deriveLocalBranchNameFromRemoteRef(branch.name)
187
+ : branch.name;
188
+
189
checkoutMutation.mutate(branch.name, {
190
onSuccess: () => {
191
setThreadError(null);
180
- setThreadBranch(branch.name, activeWorktreePath);
192
+ setThreadBranch(selectedBranchName, activeWorktreePath);
193
setIsBranchMenuOpen(false);
194
onComposerFocusRequest?.();
195
},
@@ -283,6 +295,15 @@ export default function BranchToolbar({
295
296
const hasSecondaryWorktree =
297
branch.worktreePath && branch.worktreePath !== activeProject.cwd;
298
+ const badge = branch.current
299
+ ? "current"
300
+ : hasSecondaryWorktree
301
+ ? "worktree"
302
+ : branch.isRemote
303
+ ? "remote"
304
+ : branch.isDefault
305
+ ? "default"
306
+ : null;
307
return (
308
<ComboboxItem
309
hideIndicator
@@ -295,11 +316,7 @@ export default function BranchToolbar({
316
>
317
<div className="flex w-full items-center justify-between gap-2">
318
<span className="truncate">{itemValue}</span>
298
- {(branch.current || branch.isDefault || hasSecondaryWorktree) && (
299
- <span className="shrink-0 text-[10px] text-muted-foreground/45">
300
- {branch.current ? "current" : hasSecondaryWorktree ? "worktree" : "default"}
301
- </span>
302
- )}
319
+ {badge && <span className="shrink-0 text-[10px] text-muted-foreground/45">{badge}</span>}
320
</div>
321
</ComboboxItem>
322
);
packages/contracts/src/git.ts
+1
@@ -23,6 +23,7 @@ export type GitStatusPrState = typeof GitStatusPrState.Type;
23
24
export const GitBranch = Schema.Struct({
25
name: TrimmedNonEmptyStringSchema,
26
+ isRemote: Schema.optional(Schema.Boolean),
27
current: Schema.Boolean,
28
isDefault: Schema.Boolean,
29
worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),