Skip to content

Commit 448f779

Browse files
fix(server): failed branch switch no longer deletes the branch it switched to
Drop the rollback of the created tracking branch: Git can switch HEAD and then fail in a post-checkout hook, so deleting the branch left HEAD dangling. AI-assisted; verified with the tests and tools listed above.
1 parent 66f8224 commit 448f779

2 files changed

Lines changed: 13 additions & 108 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,66 +2030,6 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
20302030
}),
20312031
);
20322032

2033-
it.effect.each([
2034-
{ label: "removes the tracking branch it created", movedMeanwhile: false },
2035-
{ label: "keeps a tracking branch another client moved", movedMeanwhile: true },
2036-
])("$label when the checkout fails", ({ movedMeanwhile }) =>
2037-
Effect.gen(function* () {
2038-
const cwd = yield* makeTmpDir();
2039-
const { initialBranch } = yield* initRepoWithCommit(cwd);
2040-
const layerDriver = yield* GitVcsDriver.GitVcsDriver;
2041-
const remote = yield* makeTmpDir("git-remote-");
2042-
yield* git(remote, ["init", "--bare"]);
2043-
yield* git(cwd, ["remote", "add", "origin", remote]);
2044-
yield* git(cwd, ["checkout", "-b", "feature"]);
2045-
yield* writeTextFile(cwd, "README.md", "feature\n");
2046-
yield* git(cwd, ["commit", "-am", "feature"]);
2047-
yield* git(cwd, ["push", "-u", "origin", "feature"]);
2048-
yield* writeTextFile(cwd, "README.md", "moved\n");
2049-
yield* git(cwd, ["commit", "-am", "moved"]);
2050-
const movedCommit = yield* git(cwd, ["rev-parse", "HEAD"]);
2051-
yield* git(cwd, ["checkout", initialBranch]);
2052-
yield* git(cwd, ["branch", "-D", "feature"]);
2053-
yield* writeTextFile(cwd, "README.md", "uncommitted work\n");
2054-
const branches = yield* layerDriver.listLocalBranchNames(cwd);
2055-
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2056-
const spawner = ChildProcessSpawner.make((command) =>
2057-
movedMeanwhile &&
2058-
ChildProcess.isStandardCommand(command) &&
2059-
command.args[0] === "checkout"
2060-
? layerDriver
2061-
.execute({
2062-
operation: "GitVcsDriver.test.moveBranch",
2063-
cwd,
2064-
args: ["update-ref", "refs/heads/feature", movedCommit],
2065-
timeoutMs: 10_000,
2066-
})
2067-
.pipe(Effect.orDie, Effect.andThen(delegate.spawn(command)))
2068-
: delegate.spawn(command),
2069-
);
2070-
const driver = yield* makeGitVcsDriverCore().pipe(
2071-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2072-
Effect.provide(layerServerConfig),
2073-
);
2074-
2075-
yield* driver.switchRef({ cwd, refName: "origin/feature" }).pipe(Effect.flip);
2076-
2077-
assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
2078-
if (movedMeanwhile) {
2079-
assert.equal(yield* git(cwd, ["rev-parse", "refs/heads/feature"]), movedCommit);
2080-
assert.equal(yield* git(cwd, ["config", "branch.feature.remote"]), "origin");
2081-
} else {
2082-
assert.deepEqual(yield* driver.listLocalBranchNames(cwd), branches);
2083-
assert.equal(
2084-
yield* git(cwd, ["config", "--get-regexp", "^branch\\.feature\\."]).pipe(
2085-
Effect.orElseSucceed(() => ""),
2086-
),
2087-
"",
2088-
);
2089-
}
2090-
}),
2091-
);
2092-
20932033
it.effect.each([
20942034
// Git cannot tell a missing revision from a missing file in these.
20952035
{ label: "an argument that may be a path", args: ["log", "-1", "missing"] },

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5002,27 +5002,19 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
50025002
yield* refuseOptionLikeRefName("GitVcsDriver.switchRef", input.cwd, checkoutArgs.at(-1));
50035003

50045004
// Created before the checkout so the shared-config lock is not held while files are written.
5005-
const createdTrackingBranch = newTrackingBranch
5006-
? yield* executeGit(
5007-
"GitVcsDriver.switchRef.createTrackingBranch",
5008-
input.cwd,
5009-
["branch", "--track", "--", newTrackingBranch, input.refName],
5010-
{
5011-
timeoutMs: 10_000,
5012-
fallbackErrorDetail: "git branch create failed",
5013-
writesSharedConfig: true,
5014-
},
5015-
).pipe(
5016-
Effect.andThen(
5017-
runGitStdout("GitVcsDriver.switchRef.createdTrackingBranchCommit", input.cwd, [
5018-
"rev-parse",
5019-
"--verify",
5020-
`refs/heads/${newTrackingBranch}`,
5021-
]),
5022-
),
5023-
Effect.map((stdout) => ({ name: newTrackingBranch, commit: stdout.trim() })),
5024-
)
5025-
: null;
5005+
// Not rolled back on failure: Git may already have switched HEAD to it (post-checkout hooks).
5006+
if (newTrackingBranch) {
5007+
yield* executeGit(
5008+
"GitVcsDriver.switchRef.createTrackingBranch",
5009+
input.cwd,
5010+
["branch", "--track", "--", newTrackingBranch, input.refName],
5011+
{
5012+
timeoutMs: 10_000,
5013+
fallbackErrorDetail: "git branch create failed",
5014+
writesSharedConfig: true,
5015+
},
5016+
);
5017+
}
50265018

50275019
// A stale ref must not turn into a path checkout that discards local edits.
50285020
yield* executeGitWithStableDiagnostics(
@@ -5036,33 +5028,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
50365028
// Creating a branch writes its upstream; Git also guesses a remote branch for a missing name.
50375029
writesSharedConfig: checkoutArgs[1] === "--track" || (!localInputExists && !remoteExists),
50385030
},
5039-
).pipe(
5040-
Effect.tapError(() =>
5041-
createdTrackingBranch
5042-
? // Deletes only if nobody moved the branch since; its upstream goes only with it.
5043-
executeGit(
5044-
"GitVcsDriver.switchRef.removeTrackingBranch",
5045-
input.cwd,
5046-
[
5047-
"update-ref",
5048-
"-d",
5049-
`refs/heads/${createdTrackingBranch.name}`,
5050-
createdTrackingBranch.commit,
5051-
],
5052-
{ timeoutMs: 10_000 },
5053-
).pipe(
5054-
Effect.andThen(
5055-
executeGit(
5056-
"GitVcsDriver.switchRef.removeTrackingBranchConfig",
5057-
input.cwd,
5058-
["config", "--remove-section", `branch.${createdTrackingBranch.name}`],
5059-
{ timeoutMs: 10_000, writesSharedConfig: true },
5060-
),
5061-
),
5062-
Effect.ignore,
5063-
)
5064-
: Effect.void,
5065-
),
50665031
);
50675032

50685033
const refName = yield* runGitStdout("GitVcsDriver.switchRef.currentBranch", input.cwd, [

0 commit comments

Comments
 (0)