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
52 changes: 52 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4939,6 +4939,58 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);

it.effect.each(["untracked", "other-remote"] as const)(
"refuses a remote switch that collides with a local branch (%s)",
(tracking) =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const { initialBranch } = yield* initRepoWithCommit(cwd);
const remote = yield* makeTmpDir("git-remote-");
yield* git(remote, ["init", "--bare"]);
yield* git(cwd, ["remote", "add", "upstream", remote]);
yield* git(cwd, ["push", "upstream", "HEAD:refs/heads/feature"]);
yield* git(cwd, ["fetch", "upstream"]);
yield* git(cwd, ["checkout", "--no-track", "-b", "feature"]);
yield* git(cwd, ["commit", "--allow-empty", "-m", "Local work"]);
if (tracking === "other-remote") {
yield* git(cwd, ["remote", "add", "origin", remote]);
yield* git(cwd, ["fetch", "origin"]);
yield* git(cwd, ["branch", "--set-upstream-to=origin/feature", "feature"]);
}
const featureHead = yield* git(cwd, ["rev-parse", "feature"]);
const upstream = yield* git(cwd, [
"for-each-ref",
"--format=%(upstream)",
"refs/heads/feature",
]);
yield* git(cwd, ["checkout", initialBranch]);
const head = yield* git(cwd, ["rev-parse", "HEAD"]);
yield* writeTextFile(cwd, "README.md", "Uncommitted work\n");
const driver = yield* GitVcsDriver.GitVcsDriver;

const result = yield* driver
.switchRef({ cwd, refName: "upstream/feature" })
.pipe(Effect.result);
assert.isTrue(Result.isFailure(result));
assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), head);
assert.equal(yield* git(cwd, ["rev-parse", "feature"]), featureHead);
assert.equal(
yield* git(cwd, ["for-each-ref", "--format=%(upstream)", "refs/heads/feature"]),
upstream,
);
assert.equal(
yield* (yield* FileSystem.FileSystem).readFileString(
(yield* Path.Path).join(cwd, "README.md"),
),
"Uncommitted work\n",
);
// Selecting the existing local branch remains an explicit, attached checkout.
assert.equal((yield* driver.switchRef({ cwd, refName: "feature" })).refName, "feature");
assert.equal(yield* git(cwd, ["symbolic-ref", "HEAD"]), "refs/heads/feature");
}),
);

it.effect("rejects a missing branch without restoring a matching dirty file", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
Expand Down
16 changes: 7 additions & 9 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5309,15 +5309,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*

const checkoutArgs = localInputExists
? ["checkout", input.refName]
: remoteExists && !localTrackingBranch && localTrackedBranchTargetExists
? ["checkout", input.refName]
: newTrackingBranch
? ["checkout", newTrackingBranch]
: remoteExists && !localTrackingBranch
? ["checkout", "--track", input.refName]
: remoteExists && localTrackingBranch
? ["checkout", localTrackingBranch]
: ["checkout", input.refName];
: newTrackingBranch
? ["checkout", newTrackingBranch]
: remoteExists && !localTrackingBranch
? ["checkout", "--track", input.refName]
: remoteExists && localTrackingBranch
? ["checkout", localTrackingBranch]
: ["checkout", input.refName];

yield* refuseOptionLikeRefName("GitVcsDriver.switchRef", input.cwd, checkoutArgs.at(-1));

Expand Down
Loading