@devries-cameron20 / t3code Mirror · read-only / commits / 1af43dbf90
Fork 0

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

Handle remote checkout conflicts without duplicate local branch

- fall back to plain checkout (detached HEAD) when `--track` would clash with an existing local branch - add GitCore coverage for the conflict fallback path - simplify branch-name derivation in BranchToolbar to consistently strip the first remote segment

Julius Marminge committed Feb 28, 2026 at 21:47 UTC 1af43dbf90add958b02ac5935b103f0eba6dc60a
5 files changed +57 -15
apps/server/src/git/Layers/GitCore.test.ts
+27
@@ -734,6 +734,33 @@ it.layer(TestLayer)("git integration", (it) => {
734 }),
735 );
736
737 + it.effect(
738 + "falls back to detached checkout when --track would conflict with an existing local branch",
739 + () =>
740 + Effect.gen(function* () {
741 + const remote = yield* makeTmpDir();
742 + const source = yield* makeTmpDir();
743 + yield* git(remote, ["init", "--bare"]);
744 +
745 + yield* initRepoWithCommit(source);
746 + const defaultBranch = (yield* listGitBranches({ cwd: source })).branches.find(
747 + (branch) => branch.current,
748 + )!.name;
749 + yield* git(source, ["remote", "add", "origin", remote]);
750 + yield* git(source, ["push", "-u", "origin", defaultBranch]);
751 +
752 + // Keep local branch but remove tracking so `--track origin/<branch>`
753 + // would attempt to create an already-existing local branch.
754 + yield* git(source, ["branch", "--unset-upstream"]);
755 +
756 + yield* checkoutGitBranch({ cwd: source, branch: `origin/${defaultBranch}` });
757 +
758 + const core = yield* GitCore;
759 + const status = yield* core.statusDetails(source);
760 + expect(status.branch).toBeNull();
761 + }),
762 + );
763 +
764 it.effect("throws when checkout would overwrite uncommitted changes", () =>
765 Effect.gen(function* () {
766 const tmp = yield* makeTmpDir();
apps/server/src/git/Layers/GitCore.ts
+25
@@ -148,6 +148,15 @@ function parseTrackingBranchByUpstreamRef(
148 return null;
149 }
150
151 +function deriveLocalBranchNameFromRemoteRef(branchName: string): string | null {
152 + const separatorIndex = branchName.indexOf("/");
153 + if (separatorIndex <= 0 || separatorIndex === branchName.length - 1) {
154 + return null;
155 + }
156 + const localBranch = branchName.slice(separatorIndex + 1).trim();
157 + return localBranch.length > 0 ? localBranch : null;
158 +}
159 +
160 function commandLabel(args: readonly string[]): string {
161 return `git ${args.join(" ")}`;
162 }
@@ -921,8 +930,24 @@ const makeGitCore = Effect.gen(function* () {
930 )
931 : null;
932
933 + const localTrackedBranchCandidate = deriveLocalBranchNameFromRemoteRef(input.branch);
934 + const localTrackedBranchTargetExists =
935 + remoteExists && localTrackedBranchCandidate
936 + ? yield* executeGit(
937 + "GitCore.checkoutBranch.localTrackedBranchTargetExists",
938 + input.cwd,
939 + ["show-ref", "--verify", "--quiet", `refs/heads/${localTrackedBranchCandidate}`],
940 + {
941 + timeoutMs: 5_000,
942 + allowNonZeroExit: true,
943 + },
944 + ).pipe(Effect.map((result) => result.code === 0))
945 + : false;
946 +
947 const checkoutArgs =
948 localInputExists
949 + ? ["checkout", input.branch]
950 + : remoteExists && !localTrackingBranch && localTrackedBranchTargetExists
951 ? ["checkout", input.branch]
952 : remoteExists && !localTrackingBranch
953 ? ["checkout", "--track", input.branch]
apps/web/src/components/BranchToolbar.logic.test.ts
+3 -3
@@ -68,9 +68,9 @@ describe("deriveLocalBranchNameFromRemoteRef", () => {
68 });
69
70 it("supports remote names that contain slashes", () => {
71 - expect(
72 - deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo", "my-org/upstream"),
73 - ).toBe("feature/demo");
71 + expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
72 + "upstream/feature/demo",
73 + );
74 });
75
76 it("returns the original name when ref is malformed", () => {
apps/web/src/components/BranchToolbar.logic.ts
+1 -11
@@ -8,17 +8,7 @@ interface DeriveSyncedLocalBranchInput {
8 queryBranches: ReadonlyArray<GitBranch> | undefined;
9 }
10
11 -export function deriveLocalBranchNameFromRemoteRef(
12 - branchName: string,
13 - remoteName?: string,
14 -): string {
15 - if (remoteName) {
16 - const remotePrefix = `${remoteName}/`;
17 - if (branchName.startsWith(remotePrefix) && branchName.length > remotePrefix.length) {
18 - return branchName.slice(remotePrefix.length);
19 - }
20 - }
21 -
11 +export function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
12 const firstSeparatorIndex = branchName.indexOf("/");
13 if (firstSeparatorIndex <= 0 || firstSeparatorIndex === branchName.length - 1) {
14 return branchName;
apps/web/src/components/BranchToolbar.tsx
+1 -1
@@ -179,7 +179,7 @@ export default function BranchToolbar({
179 }
180
181 const selectedBranchName = branch.isRemote
182 - ? deriveLocalBranchNameFromRemoteRef(branch.name, branch.remoteName)
182 + ? deriveLocalBranchNameFromRemoteRef(branch.name)
183 : branch.name;
184
185 checkoutMutation.mutate(branch.name, {