Skip to content

Commit 20fe1fd

Browse files
only21milclaude
andcommitted
fix(server): commits no longer include files that still have conflict markers
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 564b2f9 commit 20fe1fd

2 files changed

Lines changed: 220 additions & 13 deletions

File tree

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5919,7 +5919,18 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
59195919
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
59205920
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
59215921

5922-
if (conflict) yield* writeTextFile(cwd, "README.md", "resolved version\n");
5922+
if (conflict) {
5923+
const markers = yield* driver
5924+
.commit(commitCwd, "Finish merge", "", { stage: {} })
5925+
.pipe(Effect.flip);
5926+
assert.equal(
5927+
markers.detail,
5928+
"Cannot commit files that still contain conflict markers: README.md. Resolve the conflicts, then commit again.",
5929+
);
5930+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
5931+
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
5932+
yield* writeTextFile(cwd, "README.md", "resolved version\n");
5933+
}
59235934
yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} });
59245935
assert.equal(
59255936
yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]),
@@ -5960,6 +5971,70 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
59605971
}),
59615972
);
59625973

5974+
it.effect.each([
5975+
{ markerSize: "3", attributes: "untracked" },
5976+
{ markerSize: "3", attributes: "staged" },
5977+
{ markerSize: "9", attributes: "incoming" },
5978+
{ markerSize: "3", attributes: "changed" },
5979+
{ markerSize: "5.0", attributes: "committed" },
5980+
])(
5981+
"rejects files that still contain conflict markers outside a merge (size $markerSize, $attributes)",
5982+
({ markerSize, attributes }) =>
5983+
Effect.gen(function* () {
5984+
const cwd = yield* makeTmpDir();
5985+
yield* initRepoWithCommit(cwd);
5986+
const driver = yield* GitVcsDriver.GitVcsDriver;
5987+
// Git still writes markers into -diff files (often lockfiles), at the configured size. Each row
5988+
// leaves that size in one place only: the worktree, the index, or HEAD (the stash or the user
5989+
// changes it afterwards).
5990+
const setSize = (size: string) =>
5991+
writeTextFile(cwd, ".gitattributes", `/README.md -diff conflict-marker-size=${size}\n`);
5992+
if (attributes === "untracked") yield* setSize(markerSize);
5993+
if (attributes === "incoming" || attributes === "changed" || attributes === "committed") {
5994+
yield* setSize(markerSize);
5995+
yield* git(cwd, ["add", ".gitattributes"]);
5996+
yield* git(cwd, ["commit", "-m", "Attributes"]);
5997+
}
5998+
if (attributes === "incoming") yield* setSize("3");
5999+
yield* writeTextFile(cwd, "README.md", "stashed version\n");
6000+
yield* git(cwd, ["stash"]);
6001+
yield* writeTextFile(cwd, "README.md", "committed version\n");
6002+
yield* git(cwd, ["commit", "-am", "Change README"]);
6003+
if (attributes === "staged") {
6004+
yield* setSize(markerSize);
6005+
yield* git(cwd, ["add", ".gitattributes"]);
6006+
}
6007+
const pop = yield* driver.execute({
6008+
operation: "test.stashPop",
6009+
cwd,
6010+
args: ["stash", "pop"],
6011+
allowNonZeroExit: true,
6012+
});
6013+
assert.equal(pop.exitCode, 1);
6014+
if (attributes === "staged" || attributes === "changed") yield* setSize("7");
6015+
if (attributes === "changed") yield* git(cwd, ["add", ".gitattributes"]);
6016+
yield* writeTextFile(cwd, "other.txt", "other\n");
6017+
const head = yield* git(cwd, ["rev-parse", "HEAD"]);
6018+
const unmerged = yield* git(cwd, ["ls-files", "--unmerged"]);
6019+
const nested = (yield* Path.Path).join(cwd, "nested");
6020+
yield* (yield* FileSystem.FileSystem).makeDirectory(nested);
6021+
const detail =
6022+
"Cannot commit files that still contain conflict markers: README.md. Resolve the conflicts, then commit again.";
6023+
6024+
const selected = yield* driver
6025+
.commit(cwd, "Pick", "", { stage: { filePaths: ["README.md", "other.txt"] } })
6026+
.pipe(Effect.flip);
6027+
const all = yield* driver.prepareCommitContext(nested).pipe(Effect.flip);
6028+
6029+
assert.equal(selected.detail, detail);
6030+
assert.equal(all.detail, detail);
6031+
assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), head);
6032+
assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
6033+
yield* driver.commit(cwd, "Add other", "", { stage: { filePaths: ["other.txt"] } });
6034+
assert.equal(yield* git(cwd, ["show", "--pretty=", "--name-only", "HEAD"]), "other.txt");
6035+
}),
6036+
);
6037+
59636038
const commitWithHooks = (hooks: Record<string, string>) =>
59646039
Effect.gen(function* () {
59656040
const cwd = yield* makeTmpDir();

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

Lines changed: 144 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ const RANGE_DIFF_SUMMARY_MAX_OUTPUT_BYTES = 19_000;
7373
const RANGE_DIFF_PATCH_MAX_OUTPUT_BYTES = 59_000;
7474
const REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES = 120_000;
7575
const REVIEW_METADATA_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
76+
const GIT_DEFAULT_CONFLICT_MARKER_SIZE = 7;
77+
78+
// Git falls back to the default for any value that is not a positive integer.
79+
function parseConflictMarkerSize(value = "") {
80+
const size = /^\+?\d+$/.test(value) ? Number.parseInt(value, 10) : 0;
81+
return size > 0 ? size : GIT_DEFAULT_CONFLICT_MARKER_SIZE;
82+
}
83+
7684
// Temporary-index git add scales poorly (50k files took ~30 s); report incomplete totals above this cap.
7785
const REVIEW_UNTRACKED_MAX_FILES = 5_000;
7886
const REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024;
@@ -2432,9 +2440,139 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24322440
filePaths?: readonly string[],
24332441
env?: NodeJS.ProcessEnv,
24342442
) {
2435-
const config = env
2436-
? ["-c", "core.splitIndex=false", "-c", "splitIndex.sharedIndexExpire=never"]
2437-
: [];
2443+
const isolatedIndexConfig = [
2444+
"-c",
2445+
"core.splitIndex=false",
2446+
"-c",
2447+
"splitIndex.sharedIndexExpire=never",
2448+
];
2449+
const config = env ? isolatedIndexConfig : [];
2450+
const envOptions = env ? { env } : {};
2451+
// Status paths are repository-relative, while cwd can be a subdirectory.
2452+
const toPathspec = (file: string) => `:(top,literal)${file}`;
2453+
const pathspecs = (filePaths ?? []).map(toPathspec);
2454+
const unmerged = yield* executeGit(
2455+
"GitVcsDriver.commit.unmerged",
2456+
cwd,
2457+
[
2458+
"ls-files",
2459+
"--unmerged",
2460+
"-z",
2461+
"--full-name",
2462+
"--",
2463+
...(pathspecs.length > 0 ? pathspecs : [":(top)"]),
2464+
],
2465+
{ ...envOptions, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES },
2466+
);
2467+
// Each record is "<mode> <object> <stage>\t<path>", once per stage.
2468+
const unmergedPaths = new Set(
2469+
splitNullSeparatedGitStdoutPaths(unmerged).map((record) =>
2470+
record.slice(record.indexOf("\t") + 1),
2471+
),
2472+
);
2473+
if (unmergedPaths.size > 0) {
2474+
// add -A marks unmerged paths resolved, so check the files it would stage for leftover markers.
2475+
const files = Array.from(unmergedPaths);
2476+
const operation = "GitVcsDriver.commit.conflictMarkers";
2477+
const checkFailed = "Could not check conflicted files for conflict markers.";
2478+
const root = yield* readWorktreeRoot(cwd);
2479+
if (root === null) {
2480+
return yield* new GitCommandError({ operation, command: "git", cwd, detail: checkFailed });
2481+
}
2482+
// check-attr takes plain repository paths, not pathspecs, and prints one
2483+
// "<path> <attribute> <value>" triple per path in input order.
2484+
const readMarkerSizes = (source: readonly string[], options: { env?: NodeJS.ProcessEnv }) =>
2485+
executeGit(
2486+
"GitVcsDriver.commit.conflictMarkerSize",
2487+
root,
2488+
["check-attr", ...source, "-z", "conflict-marker-size", "--", ...files],
2489+
{ ...options, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES },
2490+
).pipe(Effect.map((result) => result.stdout.split("\0")));
2491+
// HEAD's attributes come from a throwaway index, since check-attr --source needs Git 2.40.
2492+
const readHeadMarkerSizes = Effect.gen(function* () {
2493+
const head = yield* executeGit(
2494+
"GitVcsDriver.commit.head",
2495+
cwd,
2496+
["rev-parse", "--verify", "--quiet", "HEAD"],
2497+
{ allowNonZeroExit: true },
2498+
);
2499+
if (head.exitCode !== 0) return [];
2500+
const directory = yield* fileSystem.makeTempDirectoryScoped({
2501+
prefix: "t3code-attributes-index-",
2502+
});
2503+
const headIndex = { GIT_INDEX_FILE: path.join(directory, "index") };
2504+
yield* runGit(
2505+
"GitVcsDriver.commit.headIndex",
2506+
cwd,
2507+
[...isolatedIndexConfig, "read-tree", "HEAD"],
2508+
{ env: headIndex },
2509+
);
2510+
return yield* readMarkerSizes(["--cached"], { env: headIndex });
2511+
}).pipe(
2512+
Effect.scoped,
2513+
Effect.catchTags({
2514+
PlatformError: (cause) =>
2515+
new GitCommandError({ operation, command: "git", cwd, detail: checkFailed, cause }),
2516+
}),
2517+
);
2518+
// Markers use the size from before the conflict; afterwards it may survive only in HEAD, the
2519+
// index or the worktree. An unborn HEAD leaves the default.
2520+
const lookups = yield* Effect.all(
2521+
[
2522+
readMarkerSizes([], envOptions),
2523+
readMarkerSizes(["--cached"], envOptions),
2524+
readHeadMarkerSizes,
2525+
],
2526+
{ concurrency: 3 },
2527+
);
2528+
const filesBySize = new Map<number, Set<string>>();
2529+
files.forEach((file, index) => {
2530+
for (const values of lookups) {
2531+
const size = parseConflictMarkerSize(values[index * 3 + 2]);
2532+
filesBySize.set(size, (filesBySize.get(size) ?? new Set()).add(file));
2533+
}
2534+
});
2535+
const markedPaths = new Set<string>();
2536+
for (const [size, group] of filesBySize) {
2537+
// Git writes exactly `size` marker characters, then whitespace and a label.
2538+
const args = [
2539+
"grep",
2540+
"-l",
2541+
"-z",
2542+
"--full-name",
2543+
"-E",
2544+
"-e",
2545+
`^(<{${size}}|>{${size}})([[:space:]]|$)`,
2546+
"--",
2547+
...Array.from(group, toPathspec),
2548+
];
2549+
const markers = yield* executeGit(operation, cwd, args, {
2550+
...envOptions,
2551+
allowNonZeroExit: true,
2552+
maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES,
2553+
});
2554+
if (markers.exitCode !== 0 && markers.exitCode !== 1) {
2555+
return yield* new GitCommandError({
2556+
...gitCommandContext({ operation, cwd, args }),
2557+
detail: checkFailed,
2558+
...(markers.exitCode === null ? {} : { exitCode: markers.exitCode }),
2559+
stdoutLength: markers.stdout.length,
2560+
stderrLength: markers.stderr.length,
2561+
});
2562+
}
2563+
for (const file of splitNullSeparatedGitStdoutPaths(markers)) markedPaths.add(file);
2564+
}
2565+
if (markedPaths.size > 0) {
2566+
const named = Array.from(markedPaths).slice(0, 5).join(", ");
2567+
const more = markedPaths.size > 5 ? ` and ${markedPaths.size - 5} more` : "";
2568+
return yield* new GitCommandError({
2569+
operation: "GitVcsDriver.commit",
2570+
command: "git commit",
2571+
cwd,
2572+
detail: `Cannot commit files that still contain conflict markers: ${named}${more}. Resolve the conflicts, then commit again.`,
2573+
});
2574+
}
2575+
}
24382576
if (filePaths && filePaths.length > 0) {
24392577
if (env) {
24402578
const head = yield* executeGit(
@@ -2483,17 +2621,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24832621
yield* runGit(
24842622
"GitVcsDriver.commit.addSelected",
24852623
cwd,
2486-
// Status paths are repository-relative, while cwd can be a subdirectory.
2487-
[...config, "add", "-A", "--", ...filePaths.map((file) => `:(top,literal)${file}`)],
2488-
env ? { env } : {},
2624+
[...config, "add", "-A", "--", ...pathspecs],
2625+
envOptions,
24892626
);
24902627
} else {
2491-
yield* runGit(
2492-
"GitVcsDriver.commit.addAll",
2493-
cwd,
2494-
[...config, "add", "-A"],
2495-
env ? { env } : {},
2496-
);
2628+
yield* runGit("GitVcsDriver.commit.addAll", cwd, [...config, "add", "-A"], envOptions);
24972629
}
24982630
});
24992631

0 commit comments

Comments
 (0)