Repository navigation
Expand file tree
/
Copy pathgit-changes-runner.js
More file actions
720 lines (642 loc) · 28.5 KB
/
Copy pathgit-changes-runner.js
File metadata and controls
720 lines (642 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// git-changes-runner.js — runs the git commands, local or remote — see .ai/contexts/changes-view.md
'use strict';
const { runToExit } = require('./run-to-exit');
const fs = require('fs');
const path = require('path');
const { defaultRunRemoteCommand } = require('./remote-attach');
const {
parseStatusPorcelainV2,
parseNumstat,
mergeChanges,
countNewFileDiffAdditions,
countBufferLines,
parseCheckAttr,
diffHeaderNamesPath,
COUNT_STATUS,
} = require('./git-changes');
const DEFAULT_LOCAL_TIMEOUT_MS = 10_000;
const DEFAULT_REMOTE_TIMEOUT_MS = 20_000;
const MAX_DIFF_BYTES = 512 * 1024;
const LOCAL_MAX_BUFFER = 20 * 1024 * 1024;
// Remote stdout caps — see .ai/contexts/changes-view.md ("Remote transport stdout cap").
const STATUS_MAX_STDOUT_BYTES = 2 * 1024 * 1024;
const DIFF_STDOUT_SLACK_BYTES = 64 * 1024;
const DIFF_MAX_STDOUT_BYTES = MAX_DIFF_BYTES + DIFF_STDOUT_SLACK_BYTES;
// An unexpected git failure is reported, bounded — see .ai/contexts/changes-view.md ("Bounded error messages")
const MAX_ERROR_LINES = 5;
const MAX_ERROR_CHARS = 500;
// git's generic fatal exit code, not a "no repository" code — see .ai/contexts/changes-view.md ("Not a repository")
const GIT_FATAL_EXIT_CODE = 128;
// defaultLocalExec's code for a spawn that never ran — see .ai/contexts/changes-view.md ("Not a repository")
const EXEC_FAILED_CODE = -1;
const NOT_A_REPO_REASON = 'not-a-repo';
// Denylist, not allowlist — see .ai/contexts/changes-view.md ("Quoting rule")
function isSafeShellArg(s) {
return typeof s === 'string' && s.length > 0 && s.length <= 4096 && !/[\x00\n\r]/.test(s);
}
function isSafeCwd(cwd) {
return isSafeShellArg(cwd);
}
function hasDotDotSegment(p) {
return p.split(/[/\\]/).includes('..');
}
// Denylist plus a leading-':' shape check — see .ai/contexts/changes-view.md ("Quoting rule").
function isSafeGitPath(p) {
if (!isSafeShellArg(p)) return false;
if (hasDotDotSegment(p)) return false;
if (p[0] === ':') return false;
return true;
}
// `git diff --no-index` operands are filesystem paths, not pathspecs — see .ai/contexts/changes-view.md ("Untracked files")
const NO_INDEX_EMPTY_SIDE = '/dev/null';
function isSafeNoIndexPath(p) {
if (!isSafeGitPath(p)) return false;
if (p[0] === '-') return false;
if (p[0] === '/' || p[0] === '\\') return false;
if (/^[A-Za-z]:/.test(p)) return false;
return true;
}
// fs seam — injected in tests, real fs in production (same pattern as remote-attach.js's spawnFn)
const DEFAULT_FS_OPS = {
realpath: (p) => fs.realpathSync.native(p),
lstat: (p) => fs.lstatSync(p),
stat: (p) => fs.statSync(p),
};
function isInsideRoot(root, candidate, pathOps) {
if (candidate === root) return true;
const rel = pathOps.relative(root, candidate);
if (rel === '') return true;
return rel !== '..' && !rel.startsWith('..' + pathOps.sep) && !pathOps.isAbsolute(rel);
}
// git spells every path with forward slashes — see .ai/contexts/changes-view.md ("Untracked files")
function toGitPath(p, pathOps) {
return pathOps.sep === '/' ? p : p.split(pathOps.sep).join('/');
}
// git follows a symlink to a directory — see .ai/contexts/changes-view.md ("Untracked files")
async function leafSymlinkIsDiffable(resolved, fsOps) {
let target;
try {
target = await fsOps.stat(resolved);
} catch {
return true;
}
return target.isFile();
}
async function resolveParentDir(root, dir, fsOps, pathOps) {
const parent = await fsOps.realpath(dir);
const inside = !!parent && isInsideRoot(root, parent, pathOps);
return { parent, inside, rel: inside ? pathOps.relative(root, parent) : null };
}
function cached(cache, key, compute) {
if (!cache) return compute();
if (!cache.has(key)) cache.set(key, compute());
return cache.get(key);
}
// Containment for a --no-index operand; `cache` shares the per-directory work across one batch — see .ai/contexts/changes-view.md ("Untracked files")
async function resolveLocalNoIndexTarget(root, filePath, fsOps = ASYNC_FS_OPS, pathOps = path, cache = null) {
if (!isSafeNoIndexPath(filePath)) return null;
try {
const realRoot = await cached(cache, '\0root', () => fsOps.realpath(root));
if (!realRoot) return null;
const absolute = pathOps.resolve(realRoot, filePath);
const dirname = pathOps.dirname(absolute);
const dir = await cached(cache, dirname, () => resolveParentDir(realRoot, dirname, fsOps, pathOps));
if (!dir.inside) return null;
const base = pathOps.basename(absolute);
const resolved = pathOps.join(dir.parent, base);
const stat = await fsOps.lstat(resolved);
if (!stat.isFile() && !stat.isSymbolicLink()) return null;
if (stat.isSymbolicLink() && !(await leafSymlinkIsDiffable(resolved, fsOps))) return null;
const operand = toGitPath(dir.rel ? pathOps.join(dir.rel, base) : base, pathOps);
return isSafeNoIndexPath(operand) ? { operand, resolved, stat } : null;
} catch {
return null;
}
}
async function resolveLocalNoIndexOperand(root, filePath, fsOps = ASYNC_FS_OPS, pathOps = path) {
const target = await resolveLocalNoIndexTarget(root, filePath, fsOps, pathOps);
return target ? target.operand : null;
}
const PROC_FD_DIR = '/proc/self/fd';
// The opened file is the one containment resolved — see .ai/contexts/changes-view.md ("Untracked line counts")
async function openedFileIs(handle, resolved, expected, pathOps = path) {
const st = await handle.stat({ bigint: true });
if (!st.isFile()) return null;
if (String(st.dev) !== String(expected.dev) || String(st.ino) !== String(expected.ino)) return null;
let opened = null;
try {
opened = await fs.promises.readlink(`${PROC_FD_DIR}/${handle.fd}`);
} catch {
opened = null;
}
if (opened !== null) return opened === resolved ? st : null;
const parent = pathOps.dirname(resolved);
return (await fs.promises.realpath(parent)) === parent ? st : null;
}
// Never blocks on a FIFO, never reads past the cap, never reads a file other than `resolved` — see .ai/contexts/changes-view.md ("Untracked line counts")
async function readRegularFileUpTo(resolved, maxBytes, expected) {
const flags = fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK || 0) | (fs.constants.O_NOFOLLOW || 0);
const handle = await fs.promises.open(resolved, flags);
try {
const st = await openedFileIs(handle, resolved, expected);
if (!st) return null;
const buf = Buffer.allocUnsafe(Math.min(Number(st.size), maxBytes) + 1);
let n = 0;
while (n < buf.length) {
const { bytesRead } = await handle.read(buf, n, buf.length - n, null);
if (bytesRead === 0) break;
n += bytesRead;
}
return buf.subarray(0, n);
} finally {
await handle.close();
}
}
// Async fs seam for containment and the count pass — see .ai/contexts/changes-view.md ("Untracked line counts")
const ASYNC_FS_OPS = {
realpath: (p) => fs.promises.realpath(p),
lstat: (p) => fs.promises.lstat(p, { bigint: true }),
stat: (p) => fs.promises.stat(p),
readUpTo: readRegularFileUpTo,
};
// Bounds on the local untracked count pass — see .ai/contexts/changes-view.md ("Untracked line counts")
const UNTRACKED_COUNT_LIMITS = Object.freeze({
maxFiles: 500,
maxFileBytes: 1024 * 1024,
maxTotalBytes: 8 * 1024 * 1024,
timeBudgetMs: 1000,
concurrency: 2,
});
// Slots held by count-pass workers, across every pass in the process — see .ai/contexts/changes-view.md ("Untracked line counts")
let countSlotsInUse = 0;
const countSlotWaiters = [];
function untrackedCountSlotsInUse() {
return countSlotsInUse;
}
function untrackedCountSlotWaiters() {
return countSlotWaiters.length;
}
// A pass with no free slot queues for one — see .ai/contexts/changes-view.md ("Untracked line counts")
function acquireCountSlot(capacity) {
if (countSlotsInUse < capacity) {
countSlotsInUse += 1;
return { granted: Promise.resolve(true), cancel() {} };
}
let entry = null;
const granted = new Promise((resolve) => {
entry = { resolve, capacity };
countSlotWaiters.push(entry);
});
return {
granted,
cancel() {
const i = countSlotWaiters.indexOf(entry);
if (i === -1) return;
countSlotWaiters.splice(i, 1);
entry.resolve(false);
},
};
}
function releaseCountSlot() {
countSlotsInUse -= 1;
const i = countSlotWaiters.findIndex((w) => countSlotsInUse < w.capacity);
if (i === -1) return;
const [waiter] = countSlotWaiters.splice(i, 1);
countSlotsInUse += 1;
waiter.resolve(true);
}
async function measureOneUntracked(root, p, ctx) {
const target = await resolveLocalNoIndexTarget(root, p, ctx.fsOps, path, ctx.cache);
if (!target) return { countStatus: COUNT_STATUS.UNAVAILABLE };
if (target.stat.isSymbolicLink()) return { added: 1, deleted: 0 };
const size = Number(target.stat.size);
if (size > ctx.limits.maxFileBytes) return { countStatus: COUNT_STATUS.TOO_LARGE };
if (size > ctx.budget) return { countStatus: COUNT_STATUS.OVER_CAP };
ctx.budget -= size;
let buf = null;
try {
buf = await ctx.fsOps.readUpTo(target.resolved, ctx.limits.maxFileBytes, target.stat);
} catch {
buf = null;
}
if (!buf) return { countStatus: COUNT_STATUS.UNAVAILABLE };
ctx.budget -= buf.length - size;
if (buf.length > ctx.limits.maxFileBytes) return { countStatus: COUNT_STATUS.TOO_LARGE };
return { operand: target.operand, ...countBufferLines(buf) };
}
// Off the main thread's critical path: async, slot-bounded, time-boxed — see .ai/contexts/changes-view.md ("Untracked line counts")
async function measureUntrackedLocal(root, paths, fsOps = ASYNC_FS_OPS, limits = UNTRACKED_COUNT_LIMITS) {
const candidates = paths.slice(0, limits.maxFiles);
const ctx = { fsOps: { ...ASYNC_FS_OPS, ...fsOps }, limits, cache: new Map(), budget: limits.maxTotalBytes };
const results = new Map();
const measured = [];
let next = 0;
let open = true;
const deadline = Date.now() + limits.timeBudgetMs;
const pending = new Set();
const hasWork = () => open && next < candidates.length && Date.now() < deadline;
// One slot per file, so passes queued together interleave — see .ai/contexts/changes-view.md ("Untracked line counts")
async function worker() {
while (hasWork()) {
const slot = acquireCountSlot(limits.concurrency);
pending.add(slot);
const granted = await slot.granted;
pending.delete(slot);
if (!granted) return;
let p;
let outcome;
try {
if (!hasWork()) return;
p = candidates[next++];
outcome = await measureOneUntracked(root, p, ctx);
} catch {
outcome = { countStatus: COUNT_STATUS.UNAVAILABLE };
} finally {
releaseCountSlot();
}
if (!open) return;
if (outcome.operand !== undefined) measured.push({ path: p, ...outcome });
else results.set(p, outcome);
}
}
const workers = Math.min(limits.concurrency, candidates.length);
let timer = null;
const expired = new Promise((resolve) => { timer = setTimeout(resolve, limits.timeBudgetMs); });
await Promise.race([Promise.all(Array.from({ length: workers }, worker)), expired]);
clearTimeout(timer);
open = false;
for (const slot of pending) slot.cancel();
return { results, measured };
}
// A `diff` attribute overrides the content sniff, as it does for git's own diff — see .ai/contexts/changes-view.md ("Untracked line counts")
function settleUntrackedCounts(results, measured, diffAttrs, binaryDrivers = new Set()) {
for (const m of measured) {
const attr = diffAttrs.get(m.operand);
const driverBinary = typeof attr === 'string' && binaryDrivers.has(attr);
const binary = attr === 'unset' || driverBinary || (attr !== 'set' && m.hasNul);
results.set(m.path, binary ? { countStatus: COUNT_STATUS.BINARY } : { added: m.lines, deleted: 0 });
}
return results;
}
const DIFF_ATTR_KEYWORDS = new Set(['set', 'unset', 'unspecified']);
// `git config -z --get-regexp` output → the drivers whose `binary` is true
function parseBinaryDrivers(text) {
const drivers = new Set();
for (const record of String(text || '').split('\0')) {
const nl = record.indexOf('\n');
if (nl === -1) continue;
const key = record.slice(0, nl);
const value = record.slice(nl + 1).trim().toLowerCase();
const m = /^diff\.(.+)\.binary$/s.exec(key);
if (m && ['true', 'yes', 'on', '1'].includes(value)) drivers.add(m[1]);
}
return drivers;
}
// true/false/null (undecidable) — see .ai/contexts/changes-view.md ("Not a repository")
function gitEntryAtOrAbove(startDir, fsOps = DEFAULT_FS_OPS, pathOps = path) {
let dir;
try {
dir = pathOps.resolve(startDir);
} catch {
return null;
}
for (;;) {
try {
fsOps.lstat(pathOps.join(dir, '.git'));
return true;
} catch (err) {
const code = err && err.code;
if (code !== 'ENOENT' && code !== 'ENOTDIR') return null;
}
const parent = pathOps.dirname(dir);
if (parent === dir) return false;
dir = parent;
}
}
// A local spawn fails on the cwd long before it fails on git — see .ai/contexts/changes-view.md ("Not a repository")
function missingCwdError(cwd, fsOps = DEFAULT_FS_OPS) {
if (!fsOps || typeof fsOps.stat !== 'function') {
throw new TypeError('missingCwdError requires fsOps.stat');
}
try {
return fsOps.stat(cwd).isDirectory() ? null : `working directory is not a directory: ${cwd}`;
} catch (err) {
const code = err && err.code;
if (code === 'ENOENT' || code === 'ENOTDIR') return `working directory no longer exists: ${cwd}`;
return null;
}
}
// --literal-pathspecs on every invocation — see .ai/contexts/changes-view.md ("Quoting rule").
function buildGitArgs(args) {
return ['--literal-pathspecs', ...args];
}
// Cut on a line boundary at or under maxBytes, measured in UTF-8 bytes — see .ai/contexts/changes-view.md ("Runner interface")
function truncateDiffContent(content, maxBytes) {
if (Buffer.byteLength(content, 'utf8') <= maxBytes) return { content, truncated: false };
const lines = content.split('\n');
let acc = '';
let accBytes = 0;
for (let i = 0; i < lines.length; i++) {
const chunk = i < lines.length - 1 ? lines[i] + '\n' : lines[i];
const chunkBytes = Buffer.byteLength(chunk, 'utf8');
if (accBytes + chunkBytes > maxBytes) break;
acc += chunk;
accBytes += chunkBytes;
}
return { content: acc, truncated: true };
}
// POSIX single-quote escaping — see .ai/contexts/changes-view.md ("Quoting rule")
function shQuote(s) {
return "'" + String(s).replace(/'/g, "'\\''") + "'";
}
function buildRemoteGitCommand(cwd, args) {
return ['git', '-C', shQuote(cwd), ...args.map(shQuote)].join(' ');
}
// The session's cwd is authoritative: inherited repo-location vars must not redirect git — see .ai/contexts/changes-view.md
const GIT_LOCATION_ENV = ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR', 'GIT_OBJECT_DIRECTORY', 'GIT_PREFIX', 'GIT_NAMESPACE'];
function localGitEnv() {
const env = { ...process.env };
for (const k of GIT_LOCATION_ENV) delete env[k];
return env;
}
async function defaultLocalExec(args, { cwd, timeoutMs, input }) {
const result = await runToExit('git', args, { cwd, env: localGitEnv(), timeoutMs, maxBuffer: LOCAL_MAX_BUFFER, input });
const stdout = result.stdout.toString('utf8');
if (result.code === 0) return { code: 0, stdout, stderr: result.stderr };
return { code: result.code, stdout, stderr: result.stderr || result.message };
}
// Bounds what git wrote — see .ai/contexts/changes-view.md ("Bounded error messages")
function boundErrorMessage(text) {
const trimmed = String(text || '').trim();
if (!trimmed) return '';
const lines = trimmed.split('\n');
let out = lines.slice(0, MAX_ERROR_LINES).join('\n');
let dropped = lines.length > MAX_ERROR_LINES;
if (out.length > MAX_ERROR_CHARS) {
out = out.slice(0, MAX_ERROR_CHARS).trimEnd();
dropped = true;
}
return dropped ? out + '…' : out;
}
// A transport failure often says nothing on stderr — see .ai/contexts/changes-view.md ("Paths are relative to the repository root")
function firstError(result) {
const said = boundErrorMessage(result.stderr);
if (said) return said;
if (result.code === EXEC_FAILED_CODE) return 'git timed out or could not be run';
if (result.code === SSH_FAILED_CODE) return 'the ssh connection failed';
return `git exited with code ${result.code}`;
}
// The two stdout-cap overruns: the remote transport's own, and execFile's maxBuffer — see .ai/contexts/changes-view.md ("Untracked files")
// ssh's own exit code, or a spawn that failed or timed out: nothing git said
const SSH_FAILED_CODE = 255;
function isTransportFailure(result) {
return !!result && (result.code === SSH_FAILED_CODE || result.code === EXEC_FAILED_CODE);
}
function isStdoutCapFailure(result) {
const stderr = (result && result.stderr) || '';
return /stdout exceeded \d+ bytes/.test(stderr) || /maxBuffer length exceeded/i.test(stderr);
}
// {kind, cwd, alias, exec, timeoutMs, fsOps, countLimits} — see .ai/contexts/changes-view.md ("Runner interface")
function createGitChangesRunner({ kind, cwd, alias, exec, timeoutMs, fsOps, countLimits } = {}) {
if (kind !== 'local' && kind !== 'remote') {
throw new Error('createGitChangesRunner requires kind "local" or "remote"');
}
if (!isSafeCwd(cwd)) {
throw new Error('createGitChangesRunner requires a valid cwd');
}
if (kind === 'remote' && (typeof alias !== 'string' || !alias)) {
throw new Error('createGitChangesRunner requires an alias for a remote runner');
}
const effectiveTimeout = timeoutMs || (kind === 'local' ? DEFAULT_LOCAL_TIMEOUT_MS : DEFAULT_REMOTE_TIMEOUT_MS);
const runExec = exec || (kind === 'local'
? (args, localOpts = {}) => defaultLocalExec(args, { cwd: localOpts.cwd || cwd, timeoutMs: effectiveTimeout, input: localOpts.input })
: (command, remoteOpts) => defaultRunRemoteCommand(alias, command, {
timeoutMs: effectiveTimeout,
maxStdoutBytes: remoteOpts && remoteOpts.maxStdoutBytes,
}));
const containmentFs = fsOps || ASYNC_FS_OPS;
// opts.at runs the command in that directory instead of the session cwd; maxStdoutBytes matters only remotely — see .ai/contexts/changes-view.md ("Remote transport stdout cap")
function invoke(args, opts = {}) {
const fullArgs = buildGitArgs(args);
const at = opts.at || cwd;
if (kind === 'local') {
const localOpts = {};
if (opts.at) localOpts.cwd = opts.at;
if (opts.input !== undefined) localOpts.input = opts.input;
return runExec(fullArgs, localOpts);
}
return runExec(buildRemoteGitCommand(at, fullArgs), { maxStdoutBytes: opts.maxStdoutBytes });
}
// Status paths are relative to the repository root, not the session cwd — see .ai/contexts/changes-view.md ("Paths are relative to the repository root")
let rootLookup = null;
// {root, failure}: failure is the transport's own result, never retried at the cwd — see .ai/contexts/changes-view.md ("Paths are relative to the repository root")
function lookUpRoot() {
if (!rootLookup) {
rootLookup = (async () => {
let result;
try {
result = await invoke(['rev-parse', '--show-toplevel']);
} catch (err) {
return { root: null, failure: { code: EXEC_FAILED_CODE, stdout: '', stderr: err.message } };
}
if (isTransportFailure(result)) return { root: null, failure: result };
if (result.code !== 0) return { root: null, failure: null };
const top = String(result.stdout || '').replace(/\r?\n$/, '');
const absolute = kind === 'local' ? path.isAbsolute(top) : top.startsWith('/');
return { root: isSafeCwd(top) && absolute ? top : null, failure: null };
})();
}
return rootLookup;
}
async function repoRoot() {
return (await lookUpRoot()).root;
}
async function gitAtRoot(args, opts = {}) {
const { root, failure } = await lookUpRoot();
if (failure) return failure;
return invoke(args, { ...opts, at: root || cwd });
}
async function diffAttributes(root, operands) {
if (operands.length === 0) return { attrs: new Map(), binaryDrivers: new Set() };
let attrs = new Map();
try {
const result = await invoke(['check-attr', '-z', '--stdin', 'diff'], { at: root, input: operands.join('\0') + '\0' });
if (result.code === 0) attrs = parseCheckAttr(result.stdout);
} catch {
attrs = new Map();
}
const drivers = [...new Set(attrs.values())].filter((v) => !DIFF_ATTR_KEYWORDS.has(v));
if (drivers.length === 0) return { attrs, binaryDrivers: new Set() };
try {
const config = await invoke(['config', '-z', '--get-regexp', '^diff\\..*\\.binary$'], { at: root });
return { attrs, binaryDrivers: config.code === 0 ? parseBinaryDrivers(config.stdout) : new Set() };
} catch {
return { attrs, binaryDrivers: new Set() };
}
}
// {counts, uncountedStatus} for mergeChanges — see .ai/contexts/changes-view.md ("Untracked line counts")
async function untrackedCounts(files, collapsed) {
if (kind !== 'local') return { counts: null, uncountedStatus: COUNT_STATUS.ON_OPEN };
if (collapsed) return { counts: null, uncountedStatus: COUNT_STATUS.COLLAPSED };
const paths = files.filter((f) => f.untracked).map((f) => f.path);
if (paths.length === 0) return { counts: null, uncountedStatus: COUNT_STATUS.OVER_CAP };
const root = await repoRoot();
if (!root) return { counts: null, uncountedStatus: COUNT_STATUS.UNAVAILABLE };
const { results, measured } = await measureUntrackedLocal(root, paths, containmentFs, countLimits || UNTRACKED_COUNT_LIMITS);
const { attrs, binaryDrivers } = await diffAttributes(root, measured.map((m) => m.operand));
return { counts: settleUntrackedCounts(results, measured, attrs, binaryDrivers), uncountedStatus: COUNT_STATUS.OVER_CAP };
}
function cwdRefusal() {
return kind === 'local' ? missingCwdError(cwd, fsOps || DEFAULT_FS_OPS) : null;
}
// Only positive evidence withdraws the panel — see .ai/contexts/changes-view.md ("Not a repository")
async function isWorkTree() {
let probe;
try {
probe = await invoke(['rev-parse', '--is-inside-work-tree']);
} catch (err) {
return { ok: false, error: cwdRefusal() || err.message };
}
if (probe.code === 0) {
const answer = String(probe.stdout || '').trim();
if (answer === 'true' || answer === 'false') return { ok: true, isRepo: answer === 'true' };
return { ok: false, error: 'git rev-parse gave no answer' };
}
if (probe.code === EXEC_FAILED_CODE) return { ok: false, error: cwdRefusal() || firstError(probe) };
if (probe.code !== GIT_FATAL_EXIT_CODE) return { ok: false, error: firstError(probe) };
const gone = cwdRefusal();
if (gone) return { ok: false, error: gone };
const corroborated = kind === 'local'
? gitEntryAtOrAbove(cwd, fsOps || DEFAULT_FS_OPS)
: null;
if (corroborated === false) return { ok: true, isRepo: false };
return { ok: false, error: firstError(probe) };
}
async function cwdHasNoWorkTree() {
const probe = await isWorkTree();
return probe.ok === true && probe.isRepo === false;
}
async function status() {
let results;
try {
results = await Promise.all([
invoke(['status', '--porcelain=v2', '--branch', '-uall', '-z'], { maxStdoutBytes: STATUS_MAX_STDOUT_BYTES }),
invoke(['diff', '--numstat', '-z'], { maxStdoutBytes: STATUS_MAX_STDOUT_BYTES }),
invoke(['diff', '--cached', '--numstat', '-z'], { maxStdoutBytes: STATUS_MAX_STDOUT_BYTES }),
]);
} catch (err) {
return { ok: false, error: err.message };
}
let [st] = results;
const [, unstagedNum, stagedNum] = results;
const failed = results.filter((r) => r.code !== 0);
if (failed.length > 0 && !failed.every(isStdoutCapFailure) && await cwdHasNoWorkTree()) {
return { ok: false, reason: NOT_A_REPO_REASON, error: 'not a git repository' };
}
if (unstagedNum.code !== 0) return { ok: false, error: firstError(unstagedNum) };
if (stagedNum.code !== 0) return { ok: false, error: firstError(stagedNum) };
// A repo too large for -uall falls back to git's collapsed listing — see .ai/contexts/changes-view.md ("Untracked files")
let untrackedCollapsed = false;
if (st.code !== 0) {
if (!isStdoutCapFailure(st)) return { ok: false, error: firstError(st) };
let fallback;
try {
fallback = await invoke(['status', '--porcelain=v2', '--branch', '-z'], { maxStdoutBytes: STATUS_MAX_STDOUT_BYTES });
} catch {
return { ok: false, error: firstError(st) };
}
if (fallback.code !== 0) return { ok: false, error: firstError(st) };
st = fallback;
untrackedCollapsed = true;
}
const parsedStatus = parseStatusPorcelainV2(st.stdout);
const numstatUnstaged = parseNumstat(unstagedNum.stdout);
const numstatStaged = parseNumstat(stagedNum.stdout);
const { counts, uncountedStatus } = await untrackedCounts(parsedStatus.files, untrackedCollapsed);
return { ok: true, ...mergeChanges(parsedStatus, numstatStaged, numstatUnstaged, counts, uncountedStatus), untrackedCollapsed };
}
// The operand git receives is the guard's own, never the caller's — see .ai/contexts/changes-view.md ("Untracked files")
async function resolveUntrackedOperand(filePath, root) {
if (!isSafeNoIndexPath(filePath)) return { ok: false, error: 'invalid path' };
if (kind === 'local') {
const operand = await resolveLocalNoIndexOperand(root, filePath, containmentFs);
return operand ? { ok: true, operand } : { ok: false, error: 'invalid path' };
}
let listed;
try {
listed = await invoke(['ls-files', '--others', '--exclude-standard', '-z', '--', filePath], { at: root, maxStdoutBytes: STATUS_MAX_STDOUT_BYTES });
} catch (err) {
return { ok: false, error: err.message };
}
if (listed.code !== 0) return { ok: false, error: firstError(listed) };
const operand = String(listed.stdout || '').split('\0')[0];
return operand === filePath ? { ok: true, operand } : { ok: false, error: 'invalid path' };
}
// `--no-index` exits 1 on a difference — see .ai/contexts/changes-view.md ("Untracked files")
async function untrackedDiff(filePath) {
if (!isSafeNoIndexPath(filePath)) return { ok: false, error: 'invalid path' };
const lookup = await lookUpRoot();
if (lookup.failure) return { ok: false, error: firstError(lookup.failure) };
const root = lookup.root || cwd;
const contained = await resolveUntrackedOperand(filePath, root);
if (!contained.ok) return { ok: false, error: contained.error };
let result;
try {
result = await invoke(['-c', 'core.quotepath=false', 'diff', '--no-index', '--', NO_INDEX_EMPTY_SIDE, contained.operand],
{ at: root, maxStdoutBytes: DIFF_MAX_STDOUT_BYTES });
} catch (err) {
return { ok: false, error: err.message };
}
if (result.code !== 0 && result.code !== 1) return { ok: false, error: firstError(result) };
const stdout = result.stdout || '';
if (!stdout && (result.stderr || '').trim()) return { ok: false, error: firstError(result) };
if (!diffHeaderNamesPath(stdout, contained.operand)) return { ok: false, error: 'invalid path' };
const { content, truncated } = truncateDiffContent(stdout, MAX_DIFF_BYTES);
const added = truncated ? null : countNewFileDiffAdditions(content);
const countStatus = added !== null ? null : (truncated ? COUNT_STATUS.TOO_LARGE : COUNT_STATUS.BINARY);
return { ok: true, content, truncated, added, deleted: added === null ? null : 0, countStatus };
}
async function diff(filePath, opts = {}) {
if (opts.untracked) return untrackedDiff(filePath);
if (!isSafeGitPath(filePath)) return { ok: false, error: 'invalid path' };
const staged = !!opts.staged;
const args = staged ? ['diff', '--cached', '--', filePath] : ['diff', '--', filePath];
let result;
try {
result = await gitAtRoot(args, { maxStdoutBytes: DIFF_MAX_STDOUT_BYTES });
} catch (err) {
return { ok: false, error: err.message };
}
if (result.code !== 0) return { ok: false, error: firstError(result) };
const { content, truncated } = truncateDiffContent(result.stdout || '', MAX_DIFF_BYTES);
return { ok: true, content, truncated };
}
return { status, diff, isWorkTree, kind, cwd, alias: alias || null };
}
module.exports = {
createGitChangesRunner,
buildRemoteGitCommand,
buildGitArgs,
localGitEnv,
truncateDiffContent,
boundErrorMessage,
shQuote,
isSafeCwd,
isSafeGitPath,
isSafeNoIndexPath,
resolveLocalNoIndexOperand,
measureUntrackedLocal,
readRegularFileUpTo,
untrackedCountSlotsInUse,
untrackedCountSlotWaiters,
parseBinaryDrivers,
UNTRACKED_COUNT_LIMITS,
gitEntryAtOrAbove,
missingCwdError,
MAX_DIFF_BYTES,
STATUS_MAX_STDOUT_BYTES,
DIFF_MAX_STDOUT_BYTES,
MAX_ERROR_LINES,
MAX_ERROR_CHARS,
NOT_A_REPO_REASON,
};