Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,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", () => {
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3504,7 +3504,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",
});
Expand Down
Loading