Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a git older than 2.45 holds a file whose .gitattributes a partial clone lacks

garrytan699883d2d80c
diff --git a/src/core/persistence/sync-blobs.ts b/src/core/persistence/sync-blobs.ts
index cfd198ea92..9f99b46812 100644
--- a/src/core/persistence/sync-blobs.ts
+++ b/src/core/persistence/sync-blobs.ts
@@ -79,6 +79,14 @@ export function readBlobBytes(gitRoot: string, blobs: TreeBlob[]): Map<string, B
}
let checkAttrSource: boolean | undefined;
+let noLazyFetch = true;
+
+/** Test seam: `false` leaves out `GIT_NO_LAZY_FETCH`, as a git older than 2.45 ignores it; returns the restore. */
+export function __setNoLazyFetchForTests(value: boolean): () => void {
+ const previous = noLazyFetch;
+ noLazyFetch = value;
+ return () => { noLazyFetch = previous; };
+}
/** Test seam: `false` takes the path of a git older than 2.40; returns the restore. */
export function __setCheckAttrSourceForTests(value: boolean | undefined): () => void {
@@ -98,12 +106,13 @@ export interface MissingAttributes { attributes: string; oid: string }
* `check-attr --source=<target>`; an older git reads `target` into a temporary index (`read-tree`) and asks
* `check-attr --cached` there. Every call takes `HARDENED_GIT_ARGS` and `GIT_NO_LAZY_FETCH=1`, so no repository
* program runs and a partial clone fetches nothing. Git then reads a `.gitattributes` the repository lacks as empty, so
- * the ones that apply are checked first, and a path under a missing one maps to it instead of to a driver.
+ * the ones that apply are checked first (a git older than 2.45 fails the batch on a promisor object instead, so each is then checked alone), and a path
+ * under a missing one maps to it instead of to a driver and is not passed to `check-attr`.
*/
export function readFilterDrivers(gitRoot: string, target: string, gitPaths: string[]): Map<string, string | null | MissingAttributes> {
const out = new Map<string, string | null | MissingAttributes>();
if (gitPaths.length === 0) return out;
- const read = (args: string[], input?: string, env: NodeJS.ProcessEnv = {}) => git(gitRoot, [...HARDENED_GIT_ARGS, ...args], input, undefined, { GIT_NO_LAZY_FETCH: '1', ...env });
+ const read = (args: string[], input?: string, env: NodeJS.ProcessEnv = {}) => git(gitRoot, [...HARDENED_GIT_ARGS, ...args], input, undefined, { ...(noLazyFetch ? { GIT_NO_LAZY_FETCH: '1' } : {}), ...env });
if (checkAttrSource === undefined) {
const [major, minor] = (/git version (\d+)\.(\d+)/.exec(read(['version']).toString('utf8')) ?? ['', '0', '0']).slice(1).map(Number);
checkAttrSource = major! > 2 || major === 2 && minor! >= 40;
@@ -118,19 +127,29 @@ export function readFilterDrivers(gitRoot: string, target: string, gitPaths: str
}
}
const missing = new Map<string, MissingAttributes>();
- const checked = present.length ? read(['cat-file', '--batch-check'], present.map(file => file.oid).join('\n') + '\n').toString('utf8').split('\n') : [];
+ let checked: string[] = [];
+ try {
+ if (present.length) checked = read(['cat-file', '--batch-check'], present.map(file => file.oid).join('\n') + '\n').toString('utf8').split('\n');
+ } catch (error) {
+ if (!String((error as { stderr?: unknown }).stderr ?? '').includes('from promisor remote')) throw error;
+ checked = present.map(file => {
+ try { read(['cat-file', '-e', file.oid]); return ''; } catch { return `${file.oid} missing`; }
+ });
+ }
present.forEach((file, at) => { if (checked[at]?.endsWith(' missing')) missing.set(file.attributes.slice(0, -'.gitattributes'.length).replace(/\/$/, ''), file); });
+ const screened = gitPaths.filter(path => {
+ const lost = dirsOf(path).map(dir => missing.get(dir)).find(Boolean);
+ if (lost) out.set(path, lost);
+ return !lost;
+ });
const scratch = checkAttrSource ? null : mkdtempSync(join(tmpdir(), 'gbrain-attributes-'));
const env = scratch ? { GIT_INDEX_FILE: join(scratch, 'index') } : {};
try {
if (scratch) read(['read-tree', target], undefined, env);
- for (let i = 0; i < gitPaths.length; i += 2000) {
+ for (let i = 0; i < screened.length; i += 2000) {
const fields = read(['check-attr', ...(scratch ? ['--cached'] : [`--source=${target}`]), '-z', '--stdin', 'filter'],
- gitPaths.slice(i, i + 2000).join('\0') + '\0', env).toString('utf8').split('\0');
- for (let at = 0; at + 2 < fields.length; at += 3) {
- const lost = dirsOf(fields[at]!).map(dir => missing.get(dir)).find(Boolean);
- out.set(fields[at]!, lost ?? (['unspecified', 'unset', 'set'].includes(fields[at + 2]!) ? null : fields[at + 2]!));
- }
+ screened.slice(i, i + 2000).join('\0') + '\0', env).toString('utf8').split('\0');
+ for (let at = 0; at + 2 < fields.length; at += 3) out.set(fields[at]!, ['unspecified', 'unset', 'set'].includes(fields[at + 2]!) ? null : fields[at + 2]!);
}
} finally {
if (scratch) rmSync(scratch, { recursive: true, force: true });
diff --git a/test/cli-flag-equals.test.ts b/test/cli-flag-equals.test.ts
index 7503061a5a..631618af1d 100644
--- a/test/cli-flag-equals.test.ts
+++ b/test/cli-flag-equals.test.ts
@@ -435,6 +435,7 @@ describe('#7619 split rules', () => {
'src/core/git/commit-guard.ts': 'git child argv',
'src/commands/doctor/checks/foreign-writers.ts': 'git child argv',
'src/core/persistence/pending-import.ts': 'git child argv',
+ 'src/core/persistence/sync-blobs.ts': 'git child argv',
};
const READER = [/['"`]--[a-z0-9-]+=/, /(?:startsWith|indexOf|===)\(?\s*`\$\{\w+\}=`/, /startsWith\(\w+ \+ '='\)/];
const found = [...new Glob('src/**/*.ts').scanSync('.')].filter(p => !p.endsWith('.generated.ts') && READER.some(r => r.test(readFileSync(p, 'utf8'))));
diff --git a/test/sync-filter-owned-9024.test.ts b/test/sync-filter-owned-9024.test.ts
index 69871229b4..e14f11890e 100644
--- a/test/sync-filter-owned-9024.test.ts
+++ b/test/sync-filter-owned-9024.test.ts
@@ -3,7 +3,7 @@ import { execFileSync } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
-import { __setCheckAttrSourceForTests, __setPinnedWindowForTests } from '../src/core/persistence/sync-blobs.ts';
+import { __setCheckAttrSourceForTests, __setNoLazyFetchForTests, __setPinnedWindowForTests } from '../src/core/persistence/sync-blobs.ts';
import { retryHeld } from '../src/commands/sources-retry-held.ts';
import { filterOwnedRefusal } from '../src/core/persistence/sync-entry-fault.ts';
import { readGitSourceHolds } from '../src/core/persistence/sync-holds.ts';
@@ -139,9 +139,9 @@ for (const backend of testBackends()) describe('[' + backend + '] a committed fi
expect(filterOwnedRefusal('a.md', 'text', 'secret', 'default', true)!.message).toContain('gbrain sources retry-held default');
});
- for (const source of [true, false]) {
- test('a partial clone missing the commit\'s .gitattributes holds the file git_object_missing and fetches nothing (' + (source ? 'check-attr --source' : 'git before 2.40: a temporary index') + ')', () => brain(async ({ engine, root }) => {
- const restore = __setCheckAttrSourceForTests(source);
+ for (const source of [true, false]) for (const lazyOff of [true, false]) {
+ test('a partial clone missing the commit\'s .gitattributes holds the file git_object_missing and fetches nothing (' + (source ? 'check-attr --source' : 'git before 2.40: a temporary index') + (lazyOff ? '' : ', git before 2.45: no GIT_NO_LAZY_FETCH') + ')', () => brain(async ({ engine, root }) => {
+ const restore = __setCheckAttrSourceForTests(source), restoreLazy = __setNoLazyFetchForTests(lazyOff);
try {
expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
const marker = filtered(root, false);
@@ -151,7 +151,12 @@ for (const backend of testBackends()) describe('[' + backend + '] a committed fi
writeFileSync(uploadPack, '#!/bin/sh\ntouch ' + marker + '.upload-pack\nexec git-upload-pack "$@"\n'); chmodSync(uploadPack, 0o755);
for (const [key, value] of [['core.repositoryformatversion', '1'], ['extensions.partialClone', 'origin'], ['remote.origin.url', 'file://' + server],
['remote.origin.promisor', 'true'], ['remote.origin.partialclonefilter', 'blob:none'], ['remote.origin.uploadpack', uploadPack]]) git(root, 'config', key!, value!);
+ // Every object but the .gitattributes blob goes into a promisor pack, as in a real partial clone that never fetched it.
const oid = git(root, 'rev-parse', 'HEAD:.gitattributes');
+ const objects = git(root, 'rev-list', '--objects', '--all').split('\n').map(line => line.split(' ')[0]).filter(object => object !== oid);
+ const pack = execFileSync('git', ['-C', root, 'pack-objects', '-q', join(root, '.git', 'objects', 'pack', 'pack')], { input: objects.join('\n') + '\n', encoding: 'utf8' }).trim();
+ writeFileSync(join(root, '.git', 'objects', 'pack', 'pack-' + pack + '.promisor'), '');
+ git(root, 'prune-packed');
rmSync(join(root, '.git', 'objects', oid.slice(0, 2), oid.slice(2)));
await performManagedSync(engine, OPTS);
expect(existsSync(marker + '.upload-pack')).toBe(false);
@@ -163,7 +168,7 @@ for (const backend of testBackends()) describe('[' + backend + '] a committed fi
expect(await live(engine)).toEqual(['seed']);
expect(existsSync(marker)).toBe(false);
expect(existsSync(marker + '.fsmonitor')).toBe(false);
- } finally { restore(); }
+ } finally { restore(); restoreLazy(); }
}), 240_000);
}