Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Read staged pages and commit named paths when git exports GIT_LITERAL_PATHSPECS

garrytan6636289726fd
diff --git a/src/commands/frontmatter.ts b/src/commands/frontmatter.ts
index fcfa9c7e60..c7624cff86 100644
--- a/src/commands/frontmatter.ts
+++ b/src/commands/frontmatter.ts
@@ -43,7 +43,7 @@ import {
type AuditFix,
} from '../core/brain-writer.ts';
import { collectGitVisibleFiles } from '../core/git-visible-files.ts';
-import { literalPathspecs } from '../core/git-env.ts';
+import { GIT_PATHSPEC_ENV_VARS, literalPathspecs } from '../core/git-env.ts';
import { isMarkdownFilePath, pruneDir, slugifyPath } from '../core/sync.ts';
import { isPathContained } from '../core/path-confine.ts';
@@ -282,8 +282,10 @@ function findBrainRoot(start: string): string {
* comes back with `content: null`.
*/
function readStagedBlobs(paths: string[], cwd = process.cwd()): { root: string; files: Array<{ rel: string; content: string | null }> } {
+ const env = { ...process.env };
+ for (const name of GIT_PATHSPEC_ENV_VARS) delete env[name];
const git = (args: string[], input?: string): Buffer =>
- execFileSync('git', args, { cwd, input, maxBuffer: 1 << 30, env: process.env, stdio: ['pipe', 'pipe', 'pipe'] });
+ execFileSync('git', args, { cwd, input, maxBuffer: 1 << 30, env, stdio: ['pipe', 'pipe', 'pipe'] });
const root = git(['rev-parse', '--show-toplevel']).toString('utf8').trim();
const listed = paths.length > 0
? git(['ls-files', '--stage', '--full-name', '-z', '--', ...literalPathspecs(paths)])
diff --git a/src/core/brain-repo-durability.ts b/src/core/brain-repo-durability.ts
index 1573b34851..b1a0abe158 100644
--- a/src/core/brain-repo-durability.ts
+++ b/src/core/brain-repo-durability.ts
@@ -49,7 +49,7 @@ import { readFix } from './ops/op-fix.ts';
// Bundled into the --compile binary as the fallback taxonomy for repos that
// don't ship their own — see resolveFilingRules().
import filingRulesDoc from '../../skills/_brain-filing-rules.json';
-import { gitChildEnv } from './git-env.ts';
+import { GIT_PATHSPEC_ENV_VARS, gitChildEnv } from './git-env.ts';
import { commitAndPush, commitPathsLocked, readIntegrateConflict, SOURCE_ROOT_CONFIG } from './git/commit-push.ts';
export { execFileBounded, type BoundedExecOptions } from './bounded-child-exec.ts';
@@ -236,6 +236,7 @@ ${PROTOCOL_LINE}
# commits for secrets first), exiting 4. It never runs pull --rebase, stash or reset. Guide: docs/guides/shared-checkout-git.md
set -euo pipefail
unset $(git rev-parse --local-env-vars) 2>/dev/null || true
+unset ${GIT_PATHSPEC_ENV_VARS.join(' ')}
_managed_git="$(git rev-parse --git-dir 2>/dev/null || echo .git)"
if [ -e "$_managed_git/gbrain-managed.json" ] || [ -e .gbrain-managed ]; then
diff --git a/test/frontmatter-install-hook.test.ts b/test/frontmatter-install-hook.test.ts
index 27fa110754..fcf4fdabd5 100644
--- a/test/frontmatter-install-hook.test.ts
+++ b/test/frontmatter-install-hook.test.ts
@@ -545,4 +545,21 @@ describe('frontmatter install-hook (B13)', () => {
rmSync(bin, { recursive: true, force: true });
}
}, 60_000);
+
+ test('#7793: with the real CLI, a valid staged page passes when git runs the hook with --literal-pathspecs (GIT_LITERAL_PATHSPECS=1)', async () => {
+ await install(tmp);
+ const bin = mkdtempSync(join(tmpdir(), 'fm-hook-literal-'));
+ writeFileSync(join(bin, 'gbrain'), `#!/bin/sh\nexec '${process.execPath}' '${join(import.meta.dir, '..', 'src', 'cli.ts')}' "$@"\n`, { mode: 0o755 });
+ const env = { ...process.env, PATH: `${bin}:${process.env.PATH ?? ''}`, GBRAIN_HOME: join(bin, 'home'), DATABASE_URL: '', GBRAIN_DATABASE_URL: '', GBRAIN_SKIP_STARTUP_HOOKS: '1', GIT_LITERAL_PATHSPECS: '1' };
+ try {
+ writeFileSync(join(tmp, 'note.md'), '---\ntitle: "a: b"\ntype: note\n---\n\nbody\n');
+ execFileSync('git', ['-C', tmp, 'add', 'note.md']);
+ let run = { status: 0, stderr: '' };
+ try { execFileSync('sh', [join(tmp, '.githooks', 'pre-commit')], { cwd: tmp, env, stdio: 'pipe' }); }
+ catch (e) { run = { status: (e as { status: number }).status, stderr: String((e as { stderr: Buffer }).stderr) }; }
+ expect(run).toEqual({ status: 0, stderr: '' });
+ } finally {
+ rmSync(bin, { recursive: true, force: true });
+ }
+ }, 60_000);
});
diff --git a/test/git-commit-push-skipped-paths-gst2116.test.ts b/test/git-commit-push-skipped-paths-gst2116.test.ts
index 849ffdece6..e260aaba47 100644
--- a/test/git-commit-push-skipped-paths-gst2116.test.ts
+++ b/test/git-commit-push-skipped-paths-gst2116.test.ts
@@ -193,6 +193,16 @@ function helper(f: Fixture, cli: boolean, ...args: string[]) {
env: { PATH: '/usr/local/bin:/usr/bin:/bin', HOME: f.home, GBRAIN_HOME: f.home, GBRAIN_SKIP_STARTUP_HOOKS: '1', ...identity } });
}
+test('#7793: helper without gbrain commits a named page when git exported GIT_LITERAL_PATHSPECS=1 to it', () => {
+ const f = fixture('helper-literal', 'remoteless');
+ helper(f, false);
+ writeFileSync(join(f.brain, 'people', 'bob-example.md'), page('Bob', 'Second version.'));
+ const r = spawnSync('bash', [join(f.brain, 'scripts', 'brain-commit-push.sh'), 'write pages', 'people/bob-example.md'], { cwd: f.brain, encoding: 'utf8', timeout: 60_000,
+ env: { PATH: '/usr/local/bin:/usr/bin:/bin', HOME: f.home, GBRAIN_HOME: f.home, GBRAIN_SKIP_STARTUP_HOOKS: '1', GIT_LITERAL_PATHSPECS: '1', ...identity } });
+ expect(r.stderr).not.toContain('did not match');
+ expect(headBlob(f, 'people/bob-example.md')).toContain('Second version.');
+}, 60_000);
+
test('helper without gbrain: a skipped path fails loudly instead of printing nothing to commit and exiting 0', () => {
const f = fixture('helper-fallback', 'classic');
editHidden(f, 'people/alice-example.md');