Skip to content

Commit 8ef649f

Browse files
fix(server): Commit refuses while a merge still has conflict markers (pingdotgg#35132)
1 parent e6119fd commit 8ef649f

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6507,7 +6507,17 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
65076507
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
65086508
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
65096509

6510-
if (conflict) yield* writeTextFile(cwd, "README.md", "resolved version\n");
6510+
if (conflict) {
6511+
const unresolved = yield* driver
6512+
.commit(commitCwd, "Unresolved merge", "", { stage: {} })
6513+
.pipe(Effect.result);
6514+
assert.isTrue(Result.isFailure(unresolved), "committed an unresolved conflict");
6515+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
6516+
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
6517+
// A lone "=======" is a heading underline, not a conflict boundary.
6518+
yield* writeTextFile(cwd, "README.md", "Heading\n=======\n\nresolved\n");
6519+
assert.isNotNull(yield* driver.prepareCommitContext(commitCwd));
6520+
}
65116521
yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} });
65126522
assert.equal(
65136523
yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]),
@@ -6517,7 +6527,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
65176527
assert.equal(yield* git(cwd, ["show", "HEAD:feature.txt"]), "feature");
65186528
assert.equal(yield* git(cwd, ["show", "HEAD:unrelated.txt"]), "unrelated");
65196529
if (conflict)
6520-
assert.equal(yield* git(cwd, ["show", "HEAD:README.md"]), "resolved version");
6530+
assert.equal(
6531+
yield* git(cwd, ["show", "HEAD:README.md"]),
6532+
"Heading\n=======\n\nresolved",
6533+
);
65216534
}),
65226535
);
65236536

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,78 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
25432543
},
25442544
);
25452545
} else {
2546+
// add -A marks unmerged paths resolved, so refuse while one still has conflict markers.
2547+
const unmergedPaths = (yield* runGitStdoutWithOptions(
2548+
"GitVcsDriver.commit.unmergedPaths",
2549+
cwd,
2550+
["-c", "diff.relative=false", "diff", "--name-only", "--diff-filter=U", "-z"],
2551+
env ? { env } : {},
2552+
))
2553+
.split("\0")
2554+
.filter((file) => file.length > 0);
2555+
if (unmergedPaths.length > 0) {
2556+
const check = yield* executeGit(
2557+
"GitVcsDriver.commit.conflictMarkers",
2558+
cwd,
2559+
[
2560+
"-c",
2561+
"diff.relative=false",
2562+
"diff",
2563+
"--check",
2564+
"--no-color",
2565+
"--",
2566+
...unmergedPaths.map((file) => `:(top,literal)${file}`),
2567+
],
2568+
{
2569+
...(env ? { env } : {}),
2570+
allowNonZeroExit: true,
2571+
maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES,
2572+
appendTruncationMarker: true,
2573+
},
2574+
);
2575+
const reports = check.stdout.split("\n");
2576+
const conflicted: string[] = [];
2577+
let topLevel: string | undefined;
2578+
for (const file of unmergedPaths) {
2579+
const markerLines = new Set(
2580+
reports.flatMap((line) => {
2581+
const match = line.startsWith(`${file}:`)
2582+
? /^(\d+): leftover conflict marker$/.exec(line.slice(file.length + 1))
2583+
: null;
2584+
return match ? [Number(match[1]) - 1] : [];
2585+
}),
2586+
);
2587+
if (markerLines.size === 0) continue;
2588+
topLevel ??= (yield* runGitStdout("GitVcsDriver.commit.topLevel", cwd, [
2589+
"rev-parse",
2590+
"--show-toplevel",
2591+
])).trim();
2592+
const lines = (yield* fileSystem.readFileString(path.join(topLevel, file)).pipe(
2593+
Effect.mapError(
2594+
(cause) =>
2595+
new GitCommandError({
2596+
operation: "GitVcsDriver.commit.conflictMarkers",
2597+
command: "git commit",
2598+
cwd,
2599+
detail: `Could not check ${file} for conflict markers.`,
2600+
cause,
2601+
}),
2602+
),
2603+
)).split("\n");
2604+
// Git also reports a lone "=======", which is valid content such as a heading underline.
2605+
const reported = (marker: string) =>
2606+
lines.some((line, index) => markerLines.has(index) && line.startsWith(marker));
2607+
if (reported("<") && reported(">")) conflicted.push(file);
2608+
}
2609+
if (conflicted.length > 0) {
2610+
return yield* new GitCommandError({
2611+
operation: "GitVcsDriver.commit",
2612+
command: "git commit",
2613+
cwd,
2614+
detail: `Cannot commit with unresolved merge conflicts in ${conflicted.join(", ")}. Resolve them, then commit.`,
2615+
});
2616+
}
2617+
}
25462618
yield* runGit(
25472619
"GitVcsDriver.commit.addAll",
25482620
cwd,

0 commit comments

Comments
 (0)