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

Co-authored-by: shivam <[email protected]>
This commit is contained in:
Aditya Garudandshivam authored and GitHub committed 2026-09-18 13:03:35 +05:30
1 parent c5bbf3d149
commit 56a9bf2bd7
2 files changed
+55 -1

No files matched your search

@@ -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", () => {
+2 -1
View File
@@ -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",
});