@devries-cameron20 / t3code Mirror · read-only / commits / f4cf69f522
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 21, 19:45 UTC

fix(server): selecting a remote branch lands on its local branch (#35400)

Julius Marminge committed Nov 12, 2026 at 09:34 UTC f4cf69f522a9c7acc29a519debafad37db11f030
2 files changed +123 -40
apps/server/src/vcs/GitVcsDriverCore.test.ts
+99 -31
@@ -2184,6 +2184,31 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
2184 }),
2185 );
2186
2187 + it.effect(
2188 + "switches a remote ref to its tracking branch when a tag shares the branch name",
2189 + () =>
2190 + Effect.gen(function* () {
2191 + const cwd = yield* makeTmpDir();
2192 + const { initialBranch } = yield* initRepoWithCommit(cwd);
2193 + const remote = yield* makeTmpDir("git-remote-");
2194 + yield* git(remote, ["init", "--bare"]);
2195 + yield* git(cwd, ["remote", "add", "origin", remote]);
2196 + yield* git(cwd, ["push", "origin", "HEAD:refs/heads/v1.2"]);
2197 + yield* git(cwd, ["fetch", "origin"]);
2198 + yield* git(cwd, ["branch", "--track", "v1.2", "origin/v1.2"]);
2199 + yield* git(cwd, ["tag", "v1.2"]);
2200 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
2201 +
2202 + const result = yield* (yield* GitVcsDriver.GitVcsDriver).switchRef({
2203 + cwd,
2204 + refName: "origin/v1.2",
2205 + });
2206 +
2207 + assert.equal(result.refName, "v1.2");
2208 + assert.equal(yield* git(cwd, ["symbolic-ref", "HEAD"]), "refs/heads/v1.2");
2209 + }),
2210 + );
2211 +
2212 it.effect.each([
2213 // Git cannot tell a missing revision from a missing file in these.
2214 { label: "an argument that may be a path", args: ["log", "-1", "missing"] },
@@ -6070,39 +6095,82 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
6095 }),
6096 );
6097
6073 - it.effect("tracks a remote start point once another process releases the config lock", () =>
6074 - Effect.gen(function* () {
6075 - const cwd = yield* makeTmpDir();
6076 - const { initialBranch } = yield* initRepoWithCommit(cwd);
6077 - const remote = yield* makeTmpDir("git-remote-");
6078 - yield* git(remote, ["init", "--bare"]);
6079 - yield* git(cwd, ["remote", "add", "origin", remote]);
6080 - yield* git(cwd, ["push", "origin", initialBranch]);
6081 - const fileSystem = yield* FileSystem.FileSystem;
6082 - const path = yield* Path.Path;
6083 - const lockPath = path.join(cwd, ".git", "config.lock");
6084 - yield* fileSystem.writeFileString(lockPath, "");
6085 - const { driver, failures } = yield* makeDriverReleasingLock(
6086 - (args) => args.some((arg) => arg.startsWith("--set-upstream-to")),
6087 - lockPath,
6088 - );
6098 + it.effect.each(["createWorktree", "switchRef"] as const)(
6099 + "tracks a remote start point once another process releases the config lock: %s",
6100 + (operation) =>
6101 + Effect.gen(function* () {
6102 + const cwd = yield* makeTmpDir();
6103 + const { initialBranch } = yield* initRepoWithCommit(cwd);
6104 + const remote = yield* makeTmpDir("git-remote-");
6105 + yield* git(remote, ["init", "--bare"]);
6106 + yield* git(cwd, ["remote", "add", "origin", remote]);
6107 + yield* git(cwd, ["push", "origin", `${initialBranch}:refs/heads/feature/tracked`]);
6108 + yield* git(cwd, ["fetch", "origin"]);
6109 + const fileSystem = yield* FileSystem.FileSystem;
6110 + const path = yield* Path.Path;
6111 + const lockPath = path.join(cwd, ".git", "config.lock");
6112 + yield* fileSystem.writeFileString(lockPath, "");
6113 + const { driver, failures } = yield* makeDriverReleasingLock(
6114 + (args) => args.some((arg) => arg.startsWith("--set-upstream-to")),
6115 + lockPath,
6116 + );
6117
6090 - // The upstream retry waits on the real clock; the lock is already gone by then.
6091 - yield* TestClock.withLive(
6092 - driver.createWorktree({
6093 - cwd,
6094 - path: path.join(yield* makeTmpDir("git-worktrees-"), "tracked"),
6095 - refName: `origin/${initialBranch}`,
6096 - newRefName: "feature/tracked",
6097 - }),
6098 - );
6118 + // The upstream retry waits on the real clock; the lock is already gone by then.
6119 + if (operation === "createWorktree") {
6120 + yield* TestClock.withLive(
6121 + driver.createWorktree({
6122 + cwd,
6123 + path: path.join(yield* makeTmpDir("git-worktrees-"), "tracked"),
6124 + refName: "origin/feature/tracked",
6125 + newRefName: "feature/tracked",
6126 + }),
6127 + );
6128 + } else {
6129 + const switched = yield* TestClock.withLive(
6130 + driver.switchRef({ cwd, refName: "origin/feature/tracked" }),
6131 + );
6132 + assert.equal(switched.refName, "feature/tracked");
6133 + }
6134
6100 - assert.equal(yield* Ref.get(failures), 1);
6101 - assert.equal(
6102 - yield* git(cwd, ["rev-parse", "--abbrev-ref", "feature/tracked@{upstream}"]),
6103 - `origin/${initialBranch}`,
6104 - );
6105 - }),
6135 + assert.equal(yield* Ref.get(failures), 1);
6136 + assert.equal(
6137 + yield* git(cwd, ["rev-parse", "--abbrev-ref", "feature/tracked@{upstream}"]),
6138 + "origin/feature/tracked",
6139 + );
6140 + }),
6141 + );
6142 +
6143 + it.effect(
6144 + "leaves no untracked branch behind when the config stays locked during a switch",
6145 + () =>
6146 + Effect.gen(function* () {
6147 + const cwd = yield* makeTmpDir();
6148 + const { initialBranch } = yield* initRepoWithCommit(cwd);
6149 + const remote = yield* makeTmpDir("git-remote-");
6150 + yield* git(remote, ["init", "--bare"]);
6151 + yield* git(cwd, ["remote", "add", "upstream", remote]);
6152 + yield* git(cwd, ["push", "upstream", `${initialBranch}:refs/heads/feature`]);
6153 + yield* git(cwd, ["fetch", "upstream"]);
6154 + const fileSystem = yield* FileSystem.FileSystem;
6155 + const lockPath = (yield* Path.Path).join(cwd, ".git", "config.lock");
6156 + yield* fileSystem.writeFileString(lockPath, "");
6157 + const driver = yield* GitVcsDriver.GitVcsDriver;
6158 +
6159 + // The upstream retry waits on the real clock before giving up.
6160 + yield* TestClock.withLive(driver.switchRef({ cwd, refName: "upstream/feature" })).pipe(
6161 + Effect.flip,
6162 + );
6163 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
6164 + assert.equal(yield* git(cwd, ["branch", "--list", "feature"]), "");
6165 +
6166 + yield* fileSystem.remove(lockPath);
6167 + const switched = yield* driver.switchRef({ cwd, refName: "upstream/feature" });
6168 + assert.equal(switched.refName, "feature");
6169 + assert.equal(
6170 + yield* git(cwd, ["rev-parse", "--abbrev-ref", "feature@{upstream}"]),
6171 + "upstream/feature",
6172 + );
6173 + }),
6174 );
6175
6176 it.effect("fetches a remote branch into a tracking branch past another process's lock", () =>
apps/server/src/vcs/GitVcsDriverCore.ts
+24 -9
@@ -5257,7 +5257,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5257 ? yield* executeGit(
5258 "GitVcsDriver.switchRef.localTrackingBranch",
5259 input.cwd,
5260 - ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"],
5260 + // `:short` prefixes a name a tag shares with `heads/`, so names are unambiguous here.
5261 + ["for-each-ref", "--format=%(refname:lstrip=2)\t%(upstream)", "refs/heads"],
5262 {
5263 timeoutMs: 5_000,
5264 allowNonZeroExit: true,
@@ -5265,7 +5266,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5266 ).pipe(
5267 Effect.map((result) =>
5268 result.exitCode === 0
5268 - ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
5269 + ? parseTrackingBranchByUpstreamRef(result.stdout, `refs/remotes/${input.refName}`)
5270 : null,
5271 ),
5272 )
@@ -5307,15 +5308,29 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5308 // Created before the checkout so the shared-config lock is not held while files are written.
5309 // Not rolled back on failure: Git may already have switched HEAD to it (post-checkout hooks).
5310 if (newTrackingBranch) {
5310 - yield* executeGit(
5311 + const startRef = `refs/remotes/${input.refName}`;
5312 + yield* executeGitWithStableDiagnostics(
5313 "GitVcsDriver.switchRef.createTrackingBranch",
5314 input.cwd,
5313 - ["branch", "--track", "--", newTrackingBranch, input.refName],
5314 - {
5315 - timeoutMs: 10_000,
5316 - fallbackErrorDetail: "git branch create failed",
5317 - writesSharedConfig: true,
5318 - },
5315 + ["branch", "--no-track", "--", newTrackingBranch, startRef],
5316 + { fallbackErrorDetail: "git branch create failed" },
5317 + );
5318 + // An untracked branch of this name would make every later switch to the remote detach.
5319 + yield* executeGitWithStableDiagnostics(
5320 + "GitVcsDriver.switchRef.trackRemote",
5321 + input.cwd,
5322 + ["branch", `--set-upstream-to=${startRef}`, "--", newTrackingBranch],
5323 + { writesSharedConfig: true },
5324 + ).pipe(
5325 + retryWhileConfigLocked,
5326 + Effect.tapError(() =>
5327 + executeGit(
5328 + "GitVcsDriver.switchRef.removeUntrackedBranch",
5329 + input.cwd,
5330 + ["update-ref", "-d", `refs/heads/${newTrackingBranch}`, startRef],
5331 + { allowNonZeroExit: true },
5332 + ),
5333 + ),
5334 );
5335 }
5336