Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
commit-push keeps skip-worktree and assume-unchanged flags when a commit is refused
garrytane087d57678f4
diff --git a/src/core/git/commit-push.ts b/src/core/git/commit-push.tsindex 28fa476adc..db29b6da11 100644--- a/src/core/git/commit-push.ts+++ b/src/core/git/commit-push.ts@@ -421,14 +421,21 @@ function indexEntries(root: string, rel: string[]): string[] {/*** Put the index under `rel` back to `before` after a commit that did not land:* `git add` already staged the paths, and an entry left staged in the shared- * index is what the next writer's commit without paths sweeps up.+ * index is what the next writer's commit without paths sweeps up. `--index-info`+ * writes fresh entries, so the skip-worktree and assume-unchanged flags in+ * `flagged` (`ls-files -v` records) are set again: a dropped flag lets the+ * next commit publish the edit it hid.*/-function restoreIndex(root: string, rel: string[], before: string[]): void {+function restoreIndex(root: string, rel: string[], before: string[], flagged: string[]): void {const pathOf = (line: string) => line.slice(line.indexOf('\t') + 1);const kept = new Set(before.map(pathOf));const zero = '0'.repeat(git(root, ['rev-parse', 'HEAD']).stdout.trim().length || 40);const records = [...indexEntries(root, rel).map(pathOf).filter(p => !kept.has(p)).map(p => `0 ${zero}\t${p}`), ...before];if (records.length) withIndexRetry(root, ['update-index', '-z', '--index-info'], 'index restore', undefined, records.map(l => `${l}\0`).join(''));+ for (const [option, pattern] of [['--skip-worktree', /^[sS] /], ['--assume-unchanged', /^[a-z] /]] as const) {+ const paths = flagged.filter(entry => pattern.test(entry)).map(entry => `${entry.slice(2)}\0`);+ if (paths.length) withIndexRetry(root, ['update-index', option, '-z', '--stdin'], 'index restore', undefined, paths.join(''));+ }}interface CommitScope { roots: string[]; lockPath: string; markHeld: boolean; bases?: ReadonlyMap<string, string> }@@ -596,15 +603,16 @@ function commitPaths(root: string, rel: string[], message: string, scope: Commitdocs: 'docs/guides/shared-checkout-git.md#outcomes' } });}const before = indexEntries(root, rel);+ const flagged = gitOk(root, ['ls-files', '-v', '-z', '--', ...literalPathspecs(rel)], 'ls-files -v').split('\0').filter(entry => /^[a-zS] /.test(entry));+ const skipWorktree = new Set(flagged.filter(entry => /^[sS] /.test(entry)).map(entry => entry.slice(2)));// GST-2118: git add refuses a named skip-worktree file that is on disk; it is left to the skipped-path check, so the other paths still commit.- const sparse = new Set(gitOk(root, ['ls-files', '-v', '-z', '--', ...literalPathspecs(rel)], 'ls-files -v').split('\0')- .filter(entry => /^[sS] /.test(entry) && rel.includes(entry.slice(2)) && existsSync(join(root, entry.slice(2)))).map(entry => entry.slice(2)));+ const sparse = new Set([...skipWorktree].filter(p => rel.includes(p) && existsSync(join(root, p))));const live = rel.filter(p => !sparse.has(p));// #7984: a tracked path stages with `add -u`, because `add -A` refuses a deletion whose only match left is an ignored// path; visible untracked files stage with `add -A`; a path already staged as deleted is staged. Any other path keeps// git's own refusal (missing, or only an ignored untracked file).const listed = (args: string[]) => live.length ? git(root, [...args, '-z', '--', ...literalPathspecs(live)]).stdout.split('\0').filter(Boolean) : [];- const tracked = before.map(line => line.slice(line.indexOf('\t') + 1));+ const tracked = before.map(line => line.slice(line.indexOf('\t') + 1)).filter(p => !skipWorktree.has(p));const untracked = listed(['ls-files', '--others', '--exclude-standard']);const deleted = listed(['diff', '--cached', '--no-renames', '--name-only', '--diff-filter=D']);const matching = (paths: string[]) => live.filter(spec => paths.some(p => covers(spec, p)));@@ -639,7 +647,7 @@ function commitPaths(root: string, rel: string[], message: string, scope: Commitlanded = { committed: true, paths, listed };}} catch (e) {- try { restoreIndex(root, rel, before); } catch { /* the index lock outlasted the retries; the error below still stands */ }+ try { restoreIndex(root, rel, before, flagged); } catch { /* the index lock outlasted the retries; the error below still stands */ }throw e;}// GST-2116: after the try, so a failure from here on never restores the index under a commit that landed.diff --git a/test/commit-push-index-flags.test.ts b/test/commit-push-index-flags.test.tsnew file mode 100644index 0000000000..b0d83f5bad--- /dev/null+++ b/test/commit-push-index-flags.test.ts@@ -0,0 +1,98 @@+/**+ * Index flags around a refused or partial commit-push (persistence train 13 review, GST-2118 with #7984).+ *+ * Protects: a skip-worktree or assume-unchanged entry keeps its flag when commit-push refuses (a missing path, a+ * refusing pre-commit hook), so a later commit-push of the same directory never publishes a skip-worktree edit; and a+ * named directory whose tracked files are all skip-worktree still commits its new untracked files, reporting the flagged one+ * as skipped.+ * Fails when: the index restore rewrites entries from ls-files -s records (dropping their flags), or a directory whose+ * only tracked entries are skip-worktree reaches git add -u, which refuses it (commit_paths_invalid).+ * Seam: real Git against a local bare remote.+ */+import { afterEach, beforeEach, describe, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { commitAndPush } from '../src/core/git/commit-push.ts';+import { withEnv } from './helpers/with-env.ts';++let root: string;+const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-C', cwd, '-c', 'protocol.file.allow=always', ...args],+ { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();+const publish = (repo: string, paths: string[]) => withEnv({ GBRAIN_GIT_ALLOW_FILE_TRANSPORT: '1' },+ () => commitAndPush({ repo, paths, message: 'publish', cwd: repo }));+const flag = (repo: string, path: string) => git(repo, 'ls-files', '-v', '--', path).slice(0, 1);++beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'gbrain-flags-')); });+afterEach(() => rmSync(root, { recursive: true, force: true }));++/** A pushed checkout with notes/a.md, priv/local.env and priv/b.md. */+function checkout(): { bare: string; repo: string } {+ const bare = join(root, 'remote.git'), repo = join(root, 'repo');+ execFileSync('git', ['init', '-q', '--bare', '-b', 'main', bare]);+ execFileSync('git', ['-c', 'protocol.file.allow=always', 'clone', '-q', bare, repo], { stdio: 'ignore' });+ git(repo, 'config', 'user.email', '[email protected]'); git(repo, 'config', 'user.name', 'Example');+ mkdirSync(join(repo, 'notes')); mkdirSync(join(repo, 'priv'));+ writeFileSync(join(repo, 'notes', 'a.md'), 'a\n');+ writeFileSync(join(repo, 'priv', 'local.env'), 'TOKEN=placeholder\n');+ writeFileSync(join(repo, 'priv', 'b.md'), 'b\n');+ git(repo, 'add', '-A'); git(repo, 'commit', '-qm', 'seed'); git(repo, 'push', '-q', '-u', 'origin', 'main');+ return { bare, repo };+}+const hide = (repo: string, mode: '--skip-worktree' | '--assume-unchanged') => {+ git(repo, 'update-index', mode, '--', 'priv/local.env');+ writeFileSync(join(repo, 'priv', 'local.env'), 'TOKEN=local-only-edit\n');+};+const remoteEnv = (bare: string) => git(bare, 'show', 'main:priv/local.env');++describe('commit-push keeps index flags', () => {+ for (const [mode, letter] of [['--skip-worktree', 'S'], ['--assume-unchanged', 'h']] as const) {+ test('a refused commit keeps ' + mode + ' on a named file and on a file under a named directory', async () => {+ const { bare, repo } = checkout();+ hide(repo, mode);+ writeFileSync(join(repo, 'priv', 'b.md'), 'b2\n');+ for (const paths of [['notes/a.md', 'priv/local.env', 'notes/missing.md'], ['priv', 'notes/missing.md']]) {+ await expect(publish(repo, paths)).rejects.toMatchObject({ code: 'commit_paths_invalid' });+ expect(flag(repo, 'priv/local.env')).toBe(letter);+ }+ expect(remoteEnv(bare)).toBe('TOKEN=placeholder');+ });+ }++ test('after a refusal, a retry of the named directory does not publish the skip-worktree edit', async () => {+ const { bare, repo } = checkout();+ hide(repo, '--skip-worktree');+ writeFileSync(join(repo, 'priv', 'b.md'), 'b2\n');+ await expect(publish(repo, ['priv', 'notes/missing.md'])).rejects.toMatchObject({ code: 'commit_paths_invalid' });+ await expect(publish(repo, ['priv'])).rejects.toMatchObject({ code: 'commit_paths_skipped' });+ expect(git(bare, 'show', 'main:priv/b.md')).toBe('b2');+ expect(remoteEnv(bare)).toBe('TOKEN=placeholder');+ expect(flag(repo, 'priv/local.env')).toBe('S');+ });++ test('a pre-commit hook refusal keeps skip-worktree, and a retry does not publish the hidden edit', async () => {+ const { bare, repo } = checkout();+ hide(repo, '--skip-worktree');+ writeFileSync(join(repo, 'priv', 'b.md'), 'b2\n');+ const hook = join(repo, '.git', 'hooks', 'pre-commit');+ writeFileSync(hook, '#!/bin/sh\nexit 1\n'); chmodSync(hook, 0o755);+ await expect(publish(repo, ['priv'])).rejects.toMatchObject({ code: 'git_unavailable' });+ expect(flag(repo, 'priv/local.env')).toBe('S');+ rmSync(hook);+ await expect(publish(repo, ['priv'])).rejects.toMatchObject({ code: 'commit_paths_skipped' });+ expect(git(bare, 'show', 'main:priv/b.md')).toBe('b2');+ expect(remoteEnv(bare)).toBe('TOKEN=placeholder');+ });++ test('a named directory whose tracked files are all skip-worktree commits its new files and reports the flagged one skipped', async () => {+ const { bare, repo } = checkout();+ git(repo, 'update-index', '--skip-worktree', '--', 'priv/b.md');+ hide(repo, '--skip-worktree');+ writeFileSync(join(repo, 'priv', 'new.md'), 'new\n');+ await expect(publish(repo, ['priv'])).rejects.toMatchObject({ code: 'commit_paths_skipped' });+ expect(git(bare, 'show', 'main:priv/new.md')).toBe('new');+ expect(remoteEnv(bare)).toBe('TOKEN=placeholder');+ expect(flag(repo, 'priv/local.env')).toBe('S');+ });+});