Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: find in-progress merge and rebase markers through git's absolute git dir, so a symlinked checkout path can't hide them (#8755)

garrytan4450d2017a36
diff --git a/src/core/persistence/pending-import.ts b/src/core/persistence/pending-import.ts
index f1e9547989..02e93397af 100644
--- a/src/core/persistence/pending-import.ts
+++ b/src/core/persistence/pending-import.ts
@@ -17,7 +17,7 @@
* is resubmitted by its caller after that sync.
*/
import { spawn } from '../spawn.ts';
-import { relative, resolve, sep } from 'node:path';
+import { join, relative, sep } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import type { Action } from '../agent-output.ts';
import { gitChildEnv } from '../git-env.ts';
@@ -129,12 +129,13 @@ export async function unmergedPath(root: string, path: string, signal?: AbortSig
/**
* #8755: the absolute paths of the markers a merge, cherry-pick, revert or rebase in progress leaves in `root`'s git dir
* (MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD, rebase-merge, rebase-apply); null when Git cannot name them. The caller
- * checks which exist.
+ * checks which exist. All five live in the per-worktree git dir, which Git prints absolute and symlink-resolved, so a root
+ * reached through a symlink still names the real markers.
*/
export async function operationMarkerPaths(root: string, signal?: AbortSignal): Promise<string[] | null> {
const markers = ['MERGE_HEAD', 'CHERRY_PICK_HEAD', 'REVERT_HEAD', 'rebase-merge', 'rebase-apply'];
- return gitRead(root, ['rev-parse', ...markers.flatMap(marker => ['--git-path', marker])], signal)
- .then(out => { const paths = out.split('\n').filter(Boolean).map(path => resolve(root, path)); return paths.length === markers.length ? paths : null; },
+ return gitRead(root, ['rev-parse', '--absolute-git-dir'], signal)
+ .then(out => { const [gitDir] = out.split('\n'); return gitDir ? markers.map(marker => join(gitDir, marker)) : null; },
error => { if (signal?.aborted) throw error; return null; });
}
diff --git a/test/operation-marker-paths.test.ts b/test/operation-marker-paths.test.ts
new file mode 100644
index 0000000000..aaba1593e3
--- /dev/null
+++ b/test/operation-marker-paths.test.ts
@@ -0,0 +1,81 @@
+/**
+ * #8755: operationMarkerPaths names the markers a merge, cherry-pick, revert or rebase in progress leaves in the
+ * checkout's own git dir, so local-edit adoption refuses mid-operation even when the checkout root is reached
+ * through a symlink, or is a linked worktree.
+ */
+import { afterAll, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { operationMarkerPaths } from '../src/core/persistence/pending-import.ts';
+
+const base = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-op-markers-')));
+afterAll(() => rmSync(base, { recursive: true, force: true }));
+
+const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')));
+function git(cwd: string, ...args: string[]): string {
+ return execFileSync('git', ['-c', 'user.name=example', '-c', '[email protected]', '-c', 'commit.gpgsign=false', '-c', 'init.defaultBranch=main', ...args],
+ { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] }).toString();
+}
+function repo(name: string): string {
+ const root = join(base, name);
+ mkdirSync(join(root, 'notes', 'deep'), { recursive: true });
+ git(root, 'init', '-q');
+ writeFileSync(join(root, 'notes', 'deep', 'page.md'), 'one\n');
+ git(root, 'add', '.');
+ git(root, 'commit', '-qm', 'one');
+ return root;
+}
+async function present(root: string): Promise<string[]> {
+ const paths = await operationMarkerPaths(root);
+ expect(paths).not.toBeNull();
+ return paths!.filter(path => existsSync(path));
+}
+
+test('a root reached through a symlink below the top level at another depth still sees a merge in progress', async () => {
+ const root = repo('symlinked');
+ writeFileSync(join(root, '.git', 'MERGE_HEAD'), `${git(root, 'rev-parse', 'HEAD').trim()}\n`);
+ mkdirSync(join(base, 'elsewhere', 'x'), { recursive: true });
+ symlinkSync(join(root, 'notes'), join(base, 'elsewhere', 'x', 'sublink'));
+ symlinkSync(join(root, 'notes', 'deep'), join(base, 'elsewhere', 'deeplink'));
+ for (const link of [join(base, 'elsewhere', 'x', 'sublink'), join(base, 'elsewhere', 'deeplink'), join(base, 'elsewhere', 'x', 'sublink', 'deep')]) {
+ expect(await present(link)).toEqual([join(root, '.git', 'MERGE_HEAD')]);
+ }
+ rmSync(join(root, '.git', 'MERGE_HEAD'));
+ expect(await present(join(base, 'elsewhere', 'x', 'sublink'))).toEqual([]);
+});
+
+test('a linked worktree mid-merge reads its own git dir, and the main checkout sees no merge', async () => {
+ const main = repo('worktree-main');
+ git(main, 'checkout', '-qb', 'side');
+ writeFileSync(join(main, 'notes', 'side.md'), 'side\n');
+ git(main, 'add', '.');
+ git(main, 'commit', '-qm', 'side');
+ git(main, 'checkout', '-q', 'main');
+ const wt = join(base, 'worktree-linked');
+ git(main, 'worktree', 'add', '-qb', 'wt', wt);
+ git(wt, 'merge', '--no-ff', '--no-commit', 'side');
+ const gitDir = git(wt, 'rev-parse', '--absolute-git-dir').trim();
+ expect(gitDir).not.toBe(join(main, '.git'));
+ expect(await present(wt)).toEqual([join(gitDir, 'MERGE_HEAD')]);
+ expect(await present(join(wt, 'notes'))).toEqual([join(gitDir, 'MERGE_HEAD')]);
+ expect(await present(main)).toEqual([]);
+ git(wt, 'merge', '--abort');
+ expect(await present(wt)).toEqual([]);
+});
+
+test('a plain checkout mid-rebase sees rebase-merge', async () => {
+ const root = repo('rebasing');
+ git(root, 'checkout', '-qb', 'topic');
+ writeFileSync(join(root, 'notes', 'deep', 'page.md'), 'topic\n');
+ git(root, 'commit', '-qam', 'topic');
+ git(root, 'checkout', '-q', 'main');
+ writeFileSync(join(root, 'notes', 'deep', 'page.md'), 'main\n');
+ git(root, 'commit', '-qam', 'main');
+ git(root, 'checkout', '-q', 'topic');
+ expect(() => git(root, 'rebase', '--merge', 'main')).toThrow();
+ expect(await present(join(root, 'notes', 'deep'))).toEqual([join(root, '.git', 'rebase-merge')]);
+ git(root, 'rebase', '--abort');
+ expect(await present(root)).toEqual([]);
+});