Skip to content

Commit 996da60

Browse files
committed
fix(server): preserve merge ancestry on selected-file commits
1 parent a0e0f8a commit 996da60

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,93 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
38473847
}),
38483848
);
38493849

3850+
it.effect.each([
3851+
{ location: "root", conflict: false },
3852+
{ location: "nested", conflict: false },
3853+
{ location: "worktree", conflict: false },
3854+
{ location: "root", conflict: true },
3855+
{ location: "nested", conflict: true },
3856+
{ location: "worktree", conflict: true },
3857+
])(
3858+
"rejects selected-file merge commits without mutation ($location, conflict: $conflict)",
3859+
({ location, conflict }) =>
3860+
Effect.gen(function* () {
3861+
let cwd = yield* makeTmpDir();
3862+
const { initialBranch } = yield* initRepoWithCommit(cwd);
3863+
const driver = yield* GitVcsDriver.GitVcsDriver;
3864+
const fs = yield* FileSystem.FileSystem;
3865+
const path = yield* Path.Path;
3866+
yield* git(cwd, ["checkout", "-b", "feature"]);
3867+
yield* writeTextFile(cwd, "feature.txt", "feature\n");
3868+
if (conflict) yield* writeTextFile(cwd, "README.md", "feature version\n");
3869+
yield* git(cwd, ["add", "."]);
3870+
yield* git(cwd, ["commit", "-m", "Feature"]);
3871+
const featureHead = yield* git(cwd, ["rev-parse", "HEAD"]);
3872+
yield* git(cwd, ["checkout", initialBranch]);
3873+
yield* writeTextFile(cwd, "main.txt", "main\n");
3874+
if (conflict) yield* writeTextFile(cwd, "README.md", "main version\n");
3875+
yield* git(cwd, ["add", "."]);
3876+
yield* git(cwd, ["commit", "-m", "Main"]);
3877+
const mainHead = yield* git(cwd, ["rev-parse", "HEAD"]);
3878+
if (location === "worktree") {
3879+
const worktree = path.join(yield* makeTmpDir(), "linked");
3880+
yield* git(cwd, ["worktree", "add", "-b", "linked", worktree]);
3881+
cwd = worktree;
3882+
}
3883+
const merge = yield* driver.execute({
3884+
operation: "test.merge",
3885+
cwd,
3886+
args: ["merge", "--no-commit", "--no-ff", "feature"],
3887+
allowNonZeroExit: true,
3888+
});
3889+
assert.equal(merge.exitCode, conflict ? 1 : 0);
3890+
yield* writeTextFile(cwd, "unrelated.txt", "unrelated\n");
3891+
const commitCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
3892+
yield* fs.makeDirectory(commitCwd, { recursive: true });
3893+
const gitDir = yield* git(cwd, ["rev-parse", "--absolute-git-dir"]);
3894+
const names = ["index", "MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "ORIG_HEAD"];
3895+
const before = names.map((name) => NodeFS.readFileSync(path.join(gitDir, name)));
3896+
const files = ["README.md", "feature.txt", "main.txt", "unrelated.txt"];
3897+
const contents = files.map((name) => NodeFS.readFileSync(path.join(cwd, name)));
3898+
const unmerged = yield* git(cwd, ["ls-files", "--unmerged"]);
3899+
3900+
const result = yield* driver
3901+
.commit(commitCwd, "Partial merge", "", { stage: { filePaths: ["feature.txt"] } })
3902+
.pipe(Effect.result);
3903+
if (Result.isSuccess(result)) {
3904+
const parents = yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]);
3905+
assert.fail(
3906+
`Selected merge commit succeeded with parents ${parents}; MERGE_HEAD exists: ${NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD"))}`,
3907+
);
3908+
} else {
3909+
assert.include(result.failure.detail, "Cannot commit selected files during a merge");
3910+
assert.include(result.failure.detail, "Commit all files");
3911+
}
3912+
assert.deepEqual(
3913+
names.map((name) => NodeFS.readFileSync(path.join(gitDir, name))),
3914+
before,
3915+
);
3916+
assert.deepEqual(
3917+
files.map((name) => NodeFS.readFileSync(path.join(cwd, name))),
3918+
contents,
3919+
);
3920+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
3921+
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
3922+
3923+
if (conflict) yield* writeTextFile(cwd, "README.md", "resolved version\n");
3924+
yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} });
3925+
assert.equal(
3926+
yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]),
3927+
`${mainHead} ${featureHead}`,
3928+
);
3929+
assert.isFalse(NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD")));
3930+
assert.equal(yield* git(cwd, ["show", "HEAD:feature.txt"]), "feature");
3931+
assert.equal(yield* git(cwd, ["show", "HEAD:unrelated.txt"]), "unrelated");
3932+
if (conflict)
3933+
assert.equal(yield* git(cwd, ["show", "HEAD:README.md"]), "resolved version");
3934+
}),
3935+
);
3936+
38503937
it.effect("treats selected file paths literally", () =>
38513938
Effect.gen(function* () {
38523939
const cwd = yield* makeTmpDir();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,22 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
23322332
{ env },
23332333
);
23342334
} else {
2335+
const mergeHead = yield* executeGit(
2336+
"GitVcsDriver.commit.mergeHead",
2337+
cwd,
2338+
["rev-parse", "--verify", "--quiet", "MERGE_HEAD"],
2339+
{ allowNonZeroExit: true },
2340+
);
2341+
// Like git commit --only, reject partial merges before reset can discard their ancestry.
2342+
if (mergeHead.exitCode === 0) {
2343+
return yield* new GitCommandError({
2344+
operation: "GitVcsDriver.commit",
2345+
command: "git commit",
2346+
cwd,
2347+
detail:
2348+
"Cannot commit selected files during a merge. Commit all files to finish the merge.",
2349+
});
2350+
}
23352351
yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset"]).pipe(
23362352
Effect.catchTags({ GitCommandError: () => Effect.void }),
23372353
);

0 commit comments

Comments
 (0)