Skip to content
TypeScript

fix(git): prevent stale branch selections from restoring files (#10574)

Aditya Garud56a9bf2bd7d3
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 5cf3b7c4b4b..c09f36ed315 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -1982,6 +1982,59 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
assert.equal(result.branch, current);
}),
);
+
+ it.effect("rejects a missing branch without restoring a matching dirty file", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ yield* writeTextFile(cwd, "obsolete-branch", "original\n");
+ yield* git(cwd, ["add", "obsolete-branch"]);
+ yield* git(cwd, ["commit", "-m", "tracked file"]);
+ yield* git(cwd, ["branch", "obsolete-branch"]);
+ yield* git(cwd, ["branch", "-D", "obsolete-branch"]);
+ yield* writeTextFile(cwd, "obsolete-branch", "uncommitted work\n");
+
+ const result = yield* driver
+ .switchRef({ cwd, refName: "obsolete-branch" })
+ .pipe(Effect.result);
+
+ assert.equal(result._tag, "Failure");
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ assert.equal(
+ yield* fileSystem.readFileString(path.join(cwd, "obsolete-branch")),
+ "uncommitted work\n",
+ );
+ assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
+ }),
+ );
+
+ it.effect("still creates and reuses remote tracking branches and allows detached refs", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ const remote = yield* makeTmpDir("git-remote-");
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ yield* git(remote, ["init", "--bare"]);
+ yield* git(cwd, ["remote", "add", "origin", remote]);
+ yield* git(cwd, ["push", "origin", "HEAD:refs/heads/remote-only"]);
+
+ for (let attempt = 0; attempt < 2; attempt += 1) {
+ const result = yield* driver.switchRef({ cwd, refName: "origin/remote-only" });
+ assert.equal(result.refName, "remote-only");
+ assert.equal(
+ yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
+ "origin/remote-only",
+ );
+ yield* driver.switchRef({ cwd, refName: initialBranch });
+ }
+ const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
+ const detached = yield* driver.switchRef({ cwd, refName: commit });
+ assert.equal(detached.refName, null);
+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), commit);
+ }),
+ );
});
describe("worktree operations", () => {
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 9367d027a21..c1136bb9260 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -3469,7 +3469,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
? ["checkout", localTrackingBranch]
: ["checkout", input.refName];
- yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, checkoutArgs, {
+ // A stale ref must not turn into a path checkout that discards local edits.
+ yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, [...checkoutArgs, "--"], {
timeoutMs: 10_000,
fallbackErrorDetail: "git checkout failed",
});