Skip to content

Commit 9e58138

Browse files
committed
fix(git): prevent stale branch selections from restoring files
1 parent 0d34579 commit 9e58138

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,59 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
14241424
assert.equal(result.branch, current);
14251425
}),
14261426
);
1427+
1428+
it.effect("rejects a missing branch without restoring a matching dirty file", () =>
1429+
Effect.gen(function* () {
1430+
const cwd = yield* makeTmpDir();
1431+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1432+
const driver = yield* GitVcsDriver.GitVcsDriver;
1433+
yield* writeTextFile(cwd, "obsolete-branch", "original\n");
1434+
yield* git(cwd, ["add", "obsolete-branch"]);
1435+
yield* git(cwd, ["commit", "-m", "tracked file"]);
1436+
yield* git(cwd, ["branch", "obsolete-branch"]);
1437+
yield* git(cwd, ["branch", "-D", "obsolete-branch"]);
1438+
yield* writeTextFile(cwd, "obsolete-branch", "uncommitted work\n");
1439+
1440+
const result = yield* driver
1441+
.switchRef({ cwd, refName: "obsolete-branch" })
1442+
.pipe(Effect.result);
1443+
1444+
assert.equal(result._tag, "Failure");
1445+
const fileSystem = yield* FileSystem.FileSystem;
1446+
const path = yield* Path.Path;
1447+
assert.equal(
1448+
yield* fileSystem.readFileString(path.join(cwd, "obsolete-branch")),
1449+
"uncommitted work\n",
1450+
);
1451+
assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
1452+
}),
1453+
);
1454+
1455+
it.effect("still creates and reuses remote tracking branches and allows detached refs", () =>
1456+
Effect.gen(function* () {
1457+
const cwd = yield* makeTmpDir();
1458+
const remote = yield* makeTmpDir("git-remote-");
1459+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1460+
const driver = yield* GitVcsDriver.GitVcsDriver;
1461+
yield* git(remote, ["init", "--bare"]);
1462+
yield* git(cwd, ["remote", "add", "origin", remote]);
1463+
yield* git(cwd, ["push", "origin", "HEAD:refs/heads/remote-only"]);
1464+
1465+
for (let attempt = 0; attempt < 2; attempt += 1) {
1466+
const result = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
1467+
assert.equal(result.refName, "remote-only");
1468+
assert.equal(
1469+
yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
1470+
"origin/remote-only",
1471+
);
1472+
yield* driver.switchRef({ cwd, refName: initialBranch });
1473+
}
1474+
const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
1475+
const detached = yield* driver.switchRef({ cwd, refName: commit });
1476+
assert.equal(detached.refName, null);
1477+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), commit);
1478+
}),
1479+
);
14271480
});
14281481

14291482
describe("worktree operations", () => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
32573257
? ["checkout", localTrackingBranch]
32583258
: ["checkout", input.refName];
32593259

3260-
yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, checkoutArgs, {
3260+
// A stale ref must not turn into a path checkout that discards local edits.
3261+
yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, [...checkoutArgs, "--"], {
32613262
timeoutMs: 10_000,
32623263
fallbackErrorDetail: "git checkout failed",
32633264
});

0 commit comments

Comments
 (0)