Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Effect index restore: read HEAD literally, and never re-track a file another writer untracked
garrytand2e6c111c011
diff --git a/src/core/persistence/effect-git.ts b/src/core/persistence/effect-git.tsindex a6b879ddc4..d646798f0f 100644--- a/src/core/persistence/effect-git.ts+++ b/src/core/persistence/effect-git.ts@@ -446,8 +446,8 @@ async function restoreIndex(session: GitSession, prefix: string, paths: string[]session = { ...session, signal: undefined };const now = await indexSnapshot(session, paths);const local = (path: string) => path.startsWith(prefix) ? path.slice(prefix.length) : path;- // ls-tree reads its paths literally and relative to the root, like update-index.- const tree = await git(session, ['ls-tree', '-r', '--full-name', '-z', 'HEAD', '--', ...paths]);+ // ls-tree takes no wildcards but does read pathspec magic (a leading ':'), so each path goes literal.+ const tree = await git(session, ['ls-tree', '-r', '--full-name', '-z', 'HEAD', '--', ...literalPathspecs(paths)]);const head = new Map(tree.code !== 0 ? [] : tree.stdout.split('\0').filter(Boolean).map(record => {const [mode, , oid] = record.slice(0, record.indexOf('\t')).split(' ');return [record.slice(record.indexOf('\t') + 1), `${mode} ${oid} 0`] as const;@@ -455,7 +455,10 @@ async function restoreIndex(session: GitSession, prefix: string, paths: string[]const staged = (path: string) => now.entries.get(path) !== head.get(path);const wanted = new Set(paths);const added = [...now.entries.keys()].filter(path => !before.entries.has(path) && staged(path));- const changed = [...before.entries].filter(([path, entry]) => wanted.has(local(path)) && now.entries.get(path) !== entry && staged(path));+ // An entry gone from the index comes back only for a file gone from disk, as the group's own removals are: a file+ // still on disk was untracked by another writer (git rm --cached), and putting it back would track it again.+ const changed = [...before.entries].filter(([path, entry]) => wanted.has(local(path)) && now.entries.get(path) !== entry && staged(path)+ && (now.entries.has(path) || !existsSync(join(session.root, local(path)))));if (added.length) await git(session, ['update-index', '--force-remove', '--', ...added.map(local)]);const cacheinfo = changed.filter(([, entry]) => entry.endsWith(' 0')).flatMap(([path, entry]) => ['--cacheinfo', `${entry.split(' ')[0]},${entry.split(' ')[1]},${path}`]);if (cacheinfo.length) await git(session, ['update-index', '--add', ...cacheinfo]);diff --git a/test/effect-git-failed-commit-unstages-gst2128.test.ts b/test/effect-git-failed-commit-unstages-gst2128.test.tsindex eb7de7f45d..7c1bab12f9 100644--- a/test/effect-git-failed-commit-unstages-gst2128.test.ts+++ b/test/effect-git-failed-commit-unstages-gst2128.test.ts@@ -172,3 +172,33 @@ test('an untracked target that an ignore rule covers by the time the group addsexpect(git(top, 'ls-tree', '-r', '--name-only', 'HEAD', '--', 'brain/private')).toBe('');expect(git(top, 'diff', '--cached', '--name-only')).toBe('');});++test('a landed commit is not reversed for a file whose name starts with a colon, edited or new', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, ':colon-example.md'), 'Colon, first version.\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'colon page');+ writeFileSync(join(root, ':colon-example.md'), 'Colon, second version.\n');+ writeFileSync(join(root, ':new-example.md'), 'New.\n');+ const path = failingGit(has(' update-ref '));+ await withEnv({ PATH: path }, () => commitGitTargets(root, [':colon-example.md', ':new-example.md']));+ expect(git(top, 'show', 'HEAD:brain/:colon-example.md')).toBe('Colon, second version.');+ expect(git(top, 'show', 'HEAD:brain/:new-example.md')).toBe('New.');+ expect(git(top, 'status', '--porcelain')).toBe('');+});++test('putting the index back never tracks again a target another writer untracked meanwhile', async () => {+ const { top, root } = checkout();+ mkdirSync(join(root, 'archive'));+ writeFileSync(join(root, 'archive', 'carol-example.md'), 'Carol, first version.\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'archive');+ writeFileSync(join(root, '.gitignore'), 'archive/\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'ignore the archive folder');+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ // While the effect commits, another writer runs git rm --cached on the ignored file; the commit then fails.+ const path = failingGit('false');+ const realGit = execFileSync('sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim();+ writeFileSync(join(path.split(':')[0]!, 'git'), `#!/bin/sh\n${has(' update-ref ')} && { ${realGit} -C '${top}' rm -q --cached -- brain/archive/carol-example.md; exit 1; }\nexec ${realGit} "$@"\n`, { mode: 0o755 });+ await withEnv({ PATH: path }, () => commitGitTargets(root, ['people/alice-example.md', 'archive/carol-example.md']));+ expect(git(top, 'ls-files', '--', 'brain/archive')).toBe('');+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('brain/archive/carol-example.md');+});