Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(hooks): removing a conflicted foreign file resolves a merge [H16]

Corey Olson8f60ceeee1e6
diff --git a/src/hooks.js b/src/hooks.js
index b9e23dda..2182b763 100644
--- a/src/hooks.js
+++ b/src/hooks.js
@@ -497,10 +497,15 @@ function unchangedMergePath(root, path, mergeHeads) {
return staged !== null && (mergeHeads.some((mergeHead) => staged === committedObject(root, mergeHead, path)) || staged === committedObject(root, 'HEAD', path));
}
-/** Allow a new foreign object only when Git records both exact parents in the resolved conflict index. */
+/**
+ * Allow a new foreign object, or the file's removal, only when Git records both exact parents in the
+ * resolved conflict index. Removing the file is a resolution like any other (#860): `git rm` after a
+ * real conflict leaves no staged entry, so a missing side compares as absent rather than refusing.
+ */
function resolvedMergeConflictPath(root, path, mergeHeads) {
const staged = stagedObject(root, path);
- if (!staged) return false;
+ if (staged === null) return false;
+ const removed = staged === '';
const records = git(root, ['ls-files', '--resolve-undo', '--stage', '-z', '--', ':(literal)' + path])
.split('\0').filter(Boolean);
const stages = new Map();
@@ -509,7 +514,7 @@ function resolvedMergeConflictPath(root, path, mergeHeads) {
if (separator === -1 || record.slice(separator + 1) !== path) continue;
const fields = /^(\d{6}) ([0-9a-f]{40,64}) ([0123])$/iu.exec(record.slice(0, separator));
if (fields?.[3] === '0') {
- if (fields[1] + ' ' + fields[2] !== staged) return false;
+ if (removed || fields[1] + ' ' + fields[2] !== staged) return false;
continue;
}
if (!fields || stages.has(fields[3])) return false;
@@ -524,12 +529,12 @@ function resolvedMergeConflictPath(root, path, mergeHeads) {
const hasBaseProvenance = baseStage
? bases.some((base) => baseStage === committedObject(root, base, path))
: bases.every((base) => committedObject(root, base, path) === '');
- const stagedMode = staged.split(' ', 1)[0];
const parentModes = [stages.get('2'), stages.get('3')].filter(Boolean).map((entry) => entry.split(' ', 1)[0]);
- return hasBaseProvenance
- && stages.get('2') === committedObject(root, 'HEAD', path)
- && mergeHeads.some((mergeHead) => stages.get('3') === committedObject(root, mergeHead, path))
- && parentModes.includes(stagedMode);
+ // A side the conflict lacks, as in modify/delete, is the parent's absent path, compared as ''.
+ return (stages.has('2') || stages.has('3')) && hasBaseProvenance
+ && (stages.get('2') ?? '') === committedObject(root, 'HEAD', path)
+ && mergeHeads.some((mergeHead) => (stages.get('3') ?? '') === committedObject(root, mergeHead, path))
+ && (removed || parentModes.includes(staged.split(' ', 1)[0]));
}
/**
diff --git a/test/merge-hooks.test.js b/test/merge-hooks.test.js
index fa2c16e6..dfdfa1a4 100644
--- a/test/merge-hooks.test.js
+++ b/test/merge-hooks.test.js
@@ -409,3 +409,38 @@ test('an inherited main candidate cannot authorize an ordinary foreign commit [H
assert.notEqual(result.status, 0, 'the ordinary pre-commit hook ignores GITHEAD candidates');
assert.match(result.stderr, /outside the core lane: view\/file/u);
});
+
+test('removing a conflicted foreign file resolves a main merge and names the coordinator [H16,B26]', (t) => {
+ const box = nativeFixture(t);
+ let result = box.git(box.main, 'switch', '-c', 'historical-view');
+ assert.equal(result.status, 0, result.stderr);
+ writeFileSync(join(box.main, 'view', 'file'), 'historical view\n');
+ assert.equal(box.git(box.main, 'add', 'view/file').status, 0);
+ result = nativeCommit(box, box.main, 'chore: retain historical view file');
+ assert.equal(result.status, 0, result.stderr);
+ result = box.git(box.lane, 'reset', '--hard', 'historical-view');
+ assert.equal(result.status, 0, result.stderr);
+ assert.equal(box.git(box.main, 'switch', 'main').status, 0);
+ writeFileSync(join(box.main, 'view', 'file'), 'current view\n');
+ assert.equal(box.git(box.main, 'add', 'view/file').status, 0);
+ result = nativeCommit(box, box.main, 'chore: update the view file on main');
+ assert.equal(result.status, 0, result.stderr);
+ const mainHead = box.git(box.main, 'rev-parse', 'HEAD').stdout.trim();
+
+ result = box.git(box.lane, 'merge', '--no-edit', 'main');
+ assert.equal(result.status, 1, 'the fixture must create a real conflict in a foreign file');
+ assert.match(box.git(box.lane, 'ls-files', '--unmerged').stdout, /view\/file/u);
+ assert.equal(box.git(box.lane, 'rm', '-q', 'view/file').status, 0);
+ result = nativeCommit(box, box.lane, 'chore: resolve the view conflict by removing it');
+ assert.equal(result.status, 0, `removing the conflicted file is a resolution, not a foreign edit: ${result.stderr}`);
+ assert.match(result.stderr, /the coordinator owns view\/file/u, 'the resolution names the coordinator as the owner');
+ assert.equal(box.git(box.lane, 'rev-parse', 'HEAD^2').stdout.trim(), mainHead, 'the merge completed with main as its second parent');
+
+ // Outside a conflict, writing the foreign file again is still the lane touching it.
+ mkdirSync(join(box.lane, 'view'), { recursive: true });
+ writeFileSync(join(box.lane, 'view', 'file'), 'lane rewrite\n');
+ assert.equal(box.git(box.lane, 'add', 'view/file').status, 0);
+ result = nativeCommit(box, box.lane, 'chore: rewrite a foreign file');
+ assert.notEqual(result.status, 0, 'a foreign edit with no conflict stays refused');
+ assert.match(result.stderr, /outside the core lane: view\/file/u);
+});