Skip to content

Commit 0627489

Browse files
fix(server): selected-file commits during a cherry-pick or revert are refused (pingdotgg#35120)
1 parent e89c8ce commit 0627489

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,6 +6492,45 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
64926492
}),
64936493
);
64946494

6495+
it.effect.each([
6496+
{ operation: "cherry-pick", head: "CHERRY_PICK_HEAD" },
6497+
{ operation: "revert", head: "REVERT_HEAD" },
6498+
])("rejects selected-file commits during a $operation", ({ operation, head }) =>
6499+
Effect.gen(function* () {
6500+
const cwd = yield* makeTmpDir();
6501+
const { initialBranch } = yield* initRepoWithCommit(cwd);
6502+
const driver = yield* GitVcsDriver.GitVcsDriver;
6503+
yield* git(cwd, ["checkout", "-b", "other"]);
6504+
yield* writeTextFile(cwd, "README.md", "other version\n");
6505+
yield* writeTextFile(cwd, "picked.txt", "picked\n");
6506+
yield* git(cwd, ["add", "."]);
6507+
yield* git(cwd, ["commit", "-m", "Other"]);
6508+
if (operation === "cherry-pick") yield* git(cwd, ["checkout", initialBranch]);
6509+
yield* writeTextFile(cwd, "README.md", "main version\n");
6510+
yield* git(cwd, ["commit", "-am", "Main"]);
6511+
const originalHead = yield* git(cwd, ["rev-parse", "HEAD"]);
6512+
const result = yield* driver.execute({
6513+
operation: `test.${operation}`,
6514+
cwd,
6515+
args: [operation, operation === "cherry-pick" ? "other" : "HEAD~1"],
6516+
allowNonZeroExit: true,
6517+
});
6518+
assert.equal(result.exitCode, 1);
6519+
yield* writeTextFile(cwd, "README.md", "resolved version\n");
6520+
yield* git(cwd, ["add", "README.md"]);
6521+
const gitDir = yield* git(cwd, ["rev-parse", "--absolute-git-dir"]);
6522+
const selected = operation === "cherry-pick" ? "picked.txt" : "README.md";
6523+
6524+
const commit = yield* driver
6525+
.commit(cwd, "Partial", "", { stage: { filePaths: [selected] } })
6526+
.pipe(Effect.result);
6527+
6528+
assert.isTrue(Result.isFailure(commit), `Selected-file ${operation} commit succeeded`);
6529+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), originalHead);
6530+
assert.isTrue(NodeFS.existsSync(`${gitDir}/${head}`));
6531+
}),
6532+
);
6533+
64956534
it.effect("treats selected file paths literally", () =>
64966535
Effect.gen(function* () {
64976536
const cwd = yield* makeTmpDir();

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,31 +2495,38 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24952495
{ env },
24962496
);
24972497
} else {
2498-
const mergeHeadPath = yield* runGitStdout("GitVcsDriver.commit.mergeHead", cwd, [
2498+
const inProgressHeads = [
2499+
{ head: "MERGE_HEAD", operation: "merge" },
2500+
{ head: "CHERRY_PICK_HEAD", operation: "cherry-pick" },
2501+
{ head: "REVERT_HEAD", operation: "revert" },
2502+
] as const;
2503+
const headPaths = yield* runGitStdout("GitVcsDriver.commit.mergeHead", cwd, [
24992504
"rev-parse",
2500-
"--git-path",
2501-
"MERGE_HEAD",
2505+
...inProgressHeads.flatMap(({ head }) => ["--git-path", head]),
25022506
]);
2503-
const isMerging = yield* fileSystem.exists(path.resolve(cwd, mergeHeadPath.trim())).pipe(
2507+
const resolvedHeadPaths = headPaths.trim().split("\n");
2508+
const inProgress = yield* Effect.findFirst(inProgressHeads, (_, index) =>
2509+
fileSystem.exists(path.resolve(cwd, resolvedHeadPaths[index] ?? "")),
2510+
).pipe(
25042511
Effect.mapError(
25052512
(cause) =>
25062513
new GitCommandError({
25072514
operation: "GitVcsDriver.commit.mergeHead",
25082515
command: "git commit",
25092516
cwd,
2510-
detail: "Could not check whether a merge is in progress.",
2517+
detail: "Could not check whether a merge, cherry-pick or revert is in progress.",
25112518
cause,
25122519
}),
25132520
),
25142521
);
2515-
// Like git commit --only, reject partial merges before reset can discard their ancestry.
2516-
if (isMerging) {
2522+
// Like git commit --only, reject partial commits before reset can discard their state.
2523+
if (Option.isSome(inProgress)) {
2524+
const { operation } = inProgress.value;
25172525
return yield* new GitCommandError({
25182526
operation: "GitVcsDriver.commit",
25192527
command: "git commit",
25202528
cwd,
2521-
detail:
2522-
"Cannot commit selected files during a merge. Commit all files to finish the merge.",
2529+
detail: `Cannot commit selected files during a ${operation}. Commit all files to finish the ${operation}.`,
25232530
});
25242531
}
25252532
yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset", "--quiet"]);

0 commit comments

Comments
 (0)