gcf-aif-ext 0.20.2 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GLOSSARY.md +26 -0
- package/README.md +19 -0
- package/commands/gcf.js +92 -6
- package/extension.json +6 -1
- package/injections/improve-plan.md +7 -0
- package/injections/plan-context.md +10 -1
- package/lib/bundle.js +4 -5
- package/lib/changes.js +643 -0
- package/lib/config-set.js +19 -1
- package/lib/design-check.js +47 -18
- package/lib/design-parts.js +27 -17
- package/lib/design.js +24 -3
- package/lib/domain.js +4 -2
- package/lib/edits.js +2 -13
- package/lib/gate.js +13 -1
- package/lib/gcf-paths.js +10 -0
- package/lib/git.js +22 -2
- package/lib/index-gen.js +5 -4
- package/lib/linter.js +5 -3
- package/lib/plan-check.js +52 -2
- package/lib/tree-file.js +107 -0
- package/node_modules/cnlp-kit/CHANGELOG.md +23 -0
- package/node_modules/cnlp-kit/README.md +27 -1
- package/node_modules/cnlp-kit/bin/cnlp.js +1 -1
- package/node_modules/cnlp-kit/cnlp-format.md +13 -0
- package/node_modules/cnlp-kit/package.json +1 -1
- package/node_modules/cnlp-kit/profiles/glossary.md +38 -0
- package/node_modules/cnlp-kit/skills/cnlp-glossary/SKILL.md +50 -0
- package/node_modules/cnlp-kit/src/cnlp.js +10 -5
- package/node_modules/cnlp-kit/src/config.js +40 -3
- package/package.json +3 -2
- package/profiles/game.md +6 -0
- package/profiles/glossary.md +15 -14
- package/profiles/plan.md +52 -0
- package/skills/gcf-adopt/SKILL.md +1 -1
- package/skills/gcf-ingest/SKILL.md +4 -4
- package/skills/gcf-init/SKILL.md +1 -1
- package/skills/gcf-vision/SKILL.md +1 -1
- package/templates/agents-block.md +7 -2
- package/templates/config.yml +4 -0
- package/templates/game.md +3 -0
- package/templates/glossary.md +5 -5
package/lib/changes.js
ADDED
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
// What is not committed yet, and how it differs (docs/Changes-Plan.md, Board-PRD §5.25): the main
|
|
2
|
+
// checkout or a run's worktree, read only. The board shows this answer; the agent commits.
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { DIFF_FORM } from './edits.js';
|
|
7
|
+
import { gcfRoot, readConfig } from './gcf-paths.js';
|
|
8
|
+
import { isValidId } from './id.js';
|
|
9
|
+
import { verifiedBranches, worktreeOf } from './integrate.js';
|
|
10
|
+
import { refusal } from './refusal.js';
|
|
11
|
+
import { fileState, newFileDiff, readHead } from './tree-file.js';
|
|
12
|
+
|
|
13
|
+
// What a run leaves that is not work (the harness's NOT_WORK); all else unwanted is .gitignore's.
|
|
14
|
+
const NOISE = /^(gcf\/\.metrics\/|\.gcf-session\/)/;
|
|
15
|
+
const LIST_MAX = 2000;
|
|
16
|
+
const DIFF_LINES = 3000;
|
|
17
|
+
const DIFF_BYTES = 1_000_000;
|
|
18
|
+
// A diff past this is cut by git's pipe: its whole length is then unknown.
|
|
19
|
+
const DIFF_READ = 16 * 1024 * 1024;
|
|
20
|
+
const LIST_READ = 256 * 1024 * 1024;
|
|
21
|
+
const PICTURE_MAX = 10 * 1024 * 1024;
|
|
22
|
+
const NUL_PROBE = 8000;
|
|
23
|
+
const MODE_WHY = { 120000: 'link', 160000: 'submodule' };
|
|
24
|
+
const WORD = {
|
|
25
|
+
M: 'modified',
|
|
26
|
+
T: 'modified',
|
|
27
|
+
A: 'added',
|
|
28
|
+
D: 'deleted',
|
|
29
|
+
R: 'renamed',
|
|
30
|
+
C: 'copied',
|
|
31
|
+
U: 'conflict',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The picture formats a browser draws, and their types. */
|
|
35
|
+
const PICTURES = {
|
|
36
|
+
png: 'image/png',
|
|
37
|
+
jpg: 'image/jpeg',
|
|
38
|
+
jpeg: 'image/jpeg',
|
|
39
|
+
gif: 'image/gif',
|
|
40
|
+
webp: 'image/webp',
|
|
41
|
+
bmp: 'image/bmp',
|
|
42
|
+
svg: 'image/svg+xml',
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const run = (dir, args, opts = {}) =>
|
|
46
|
+
spawnSync('git', ['-c', 'core.quotePath=false', ...args], {
|
|
47
|
+
cwd: dir,
|
|
48
|
+
encoding: 'utf8',
|
|
49
|
+
maxBuffer: LIST_READ,
|
|
50
|
+
...opts,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const line = (dir, ...args) => {
|
|
54
|
+
const r = run(dir, args);
|
|
55
|
+
|
|
56
|
+
return r.status === 0 ? r.stdout.trim() : null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// A file with a new time and the same content is no change: no index refresh on a read.
|
|
60
|
+
const NO_REFRESH = ['-c', 'diff.autoRefreshIndex=false'];
|
|
61
|
+
|
|
62
|
+
// A record's fields before its path (a name may hold any space), and where its modes end: `1` and
|
|
63
|
+
// `2` have HEAD's, the index's and the disk's, `u` three stages and the disk's.
|
|
64
|
+
const FIELDS = { 1: [8, 6], 2: [9, 6], u: [10, 7] };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A rename status names: a staged one is its new path, with the old name only status knows. One
|
|
68
|
+
* only on disk (a move, then `add -N`) has no end state of a rename: a deleted and an added row.
|
|
69
|
+
*/
|
|
70
|
+
function renamed(row, from) {
|
|
71
|
+
if (row.xy[0] !== '.') return [{ ...row, from }];
|
|
72
|
+
|
|
73
|
+
return [
|
|
74
|
+
{ ...row, type: '1', xy: '.D', path: from, split: true },
|
|
75
|
+
{ ...row, type: '1', xy: '.A' },
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Porcelain v2's records from a `-z` read: type, XY, modes, the path and a rename's old one. */
|
|
80
|
+
export function parseStatus(text, prefix = '') {
|
|
81
|
+
const recs = text.split('\0');
|
|
82
|
+
const ours = (p) => (prefix && p.startsWith(prefix) ? p.slice(prefix.length) : p);
|
|
83
|
+
const out = [];
|
|
84
|
+
for (let i = 0; i < recs.length; i++) {
|
|
85
|
+
const type = recs[i][0];
|
|
86
|
+
if (type === '?') out.push({ type, path: ours(recs[i].slice(2)), modes: [] });
|
|
87
|
+
if (!FIELDS[type]) continue;
|
|
88
|
+
const [before, modesEnd] = FIELDS[type];
|
|
89
|
+
const parts = recs[i].split(' ');
|
|
90
|
+
const row = {
|
|
91
|
+
type,
|
|
92
|
+
xy: parts[1],
|
|
93
|
+
modes: parts.slice(3, modesEnd),
|
|
94
|
+
path: ours(parts.slice(before).join(' ')),
|
|
95
|
+
};
|
|
96
|
+
if (type === '2') out.push(...renamed(row, ours(recs[++i] ?? '')));
|
|
97
|
+
else out.push(row);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** `git diff --raw -z`'s records: the letter, the modes, the path and a rename's old one. */
|
|
104
|
+
function parseRaw(text) {
|
|
105
|
+
const recs = text.split('\0');
|
|
106
|
+
const out = [];
|
|
107
|
+
for (let i = 0; i + 1 < recs.length; i++) {
|
|
108
|
+
const [before, after, , , score] = recs[i].slice(1).split(' ');
|
|
109
|
+
const letter = score[0];
|
|
110
|
+
const two = letter === 'R' || letter === 'C';
|
|
111
|
+
out.push({
|
|
112
|
+
letter,
|
|
113
|
+
modes: [before, after],
|
|
114
|
+
path: recs[two ? i + 2 : i + 1],
|
|
115
|
+
...(two && { from: recs[i + 1] }),
|
|
116
|
+
});
|
|
117
|
+
i += two ? 2 : 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The paths numstat `-z` calls binary; a rename's record is `N\tM\t\0from\0to\0`. */
|
|
124
|
+
function parseBinary(text) {
|
|
125
|
+
const recs = text.split('\0');
|
|
126
|
+
const out = new Set();
|
|
127
|
+
for (let i = 0; i < recs.length; i++) {
|
|
128
|
+
const m = /^([\d-]+)\t([\d-]+)\t(.*)$/s.exec(recs[i]);
|
|
129
|
+
if (!m) continue;
|
|
130
|
+
// A rename names both: a move only on disk (`add -N`) is two rows, each of them binary.
|
|
131
|
+
const names = m[3] ? [m[3]] : [recs[i + 1], recs[i + 2]];
|
|
132
|
+
if (!m[3]) i += 2;
|
|
133
|
+
if (m[1] === '-') for (const n of names) out.add(n);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A `**` at `i`, as a regex and how many characters of the glob it took. */
|
|
140
|
+
function doubleStar(glob, i) {
|
|
141
|
+
const atStart = i === 0 || glob[i - 1] === '/';
|
|
142
|
+
|
|
143
|
+
return atStart && glob[i + 2] === '/' ? ['(?:.*/)?', 3] : ['.*', 2];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const CLASSES = {
|
|
147
|
+
alpha: 'a-zA-Z',
|
|
148
|
+
digit: '0-9',
|
|
149
|
+
alnum: 'a-zA-Z0-9',
|
|
150
|
+
upper: 'A-Z',
|
|
151
|
+
lower: 'a-z',
|
|
152
|
+
xdigit: '0-9a-fA-F',
|
|
153
|
+
space: '\\s',
|
|
154
|
+
punct: '!-\\/:-@\\[-`{-~',
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/** Where the bracket at `i` ends, past `[:digit:]` inside it; a first `]` is its own letter. */
|
|
158
|
+
function bracketEnd(glob, i) {
|
|
159
|
+
let at = glob[i + 1] === '!' || glob[i + 1] === '^' ? i + 2 : i + 1;
|
|
160
|
+
if (glob[at] === ']') at++;
|
|
161
|
+
while (at < glob.length && glob[at] !== ']') {
|
|
162
|
+
const named = glob[at] === '[' && glob[at + 1] === ':' ? glob.indexOf(':]', at + 2) : -1;
|
|
163
|
+
at = named > 0 ? named + 2 : at + 1;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return at < glob.length ? at : -1;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A bracket as a regex: it never takes a `/`, as git's does not. */
|
|
170
|
+
function bracket(glob, i, end) {
|
|
171
|
+
const inner = glob.slice(i + 1, end);
|
|
172
|
+
const not = /^[!^]/.test(inner);
|
|
173
|
+
const body = (not ? inner.slice(1) : inner)
|
|
174
|
+
.replace(/\[:(\w+):\]/g, (_, n) => `\0${n}\0`)
|
|
175
|
+
.replace(/[\\[\]]/g, '\\$&')
|
|
176
|
+
.replace(/\0(\w+)\0/g, (_, n) => CLASSES[n] ?? '');
|
|
177
|
+
|
|
178
|
+
return `(?!/)[${not ? '^' : ''}${body}]`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The piece of a glob at `i`, as a regex and how many characters it took. */
|
|
182
|
+
function globPiece(glob, i) {
|
|
183
|
+
const c = glob[i];
|
|
184
|
+
if (c === '*') return glob[i + 1] === '*' ? doubleStar(glob, i) : ['[^/]*', 1];
|
|
185
|
+
if (c === '?') return ['[^/]', 1];
|
|
186
|
+
const end = c === '[' ? bracketEnd(glob, i) : -1;
|
|
187
|
+
if (end < 0) return [c.replace(/[.+^${}()|\\[\]]/, '\\$&'), 1];
|
|
188
|
+
|
|
189
|
+
return [bracket(glob, i, end), end - i + 1];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* A `no_diff` glob as a test of a path from the project, as git's `:(glob)` reads it (a test holds
|
|
194
|
+
* it to git's answers): `*` stays in a folder, `**` crosses them, and a name as it stands takes
|
|
195
|
+
* itself and all it holds; ending in `/` it takes only what a folder holds, and a pattern ending
|
|
196
|
+
* so nothing, as a file never ends in one. Tested here, as git has no read of a file already gone
|
|
197
|
+
* from the index. A glob that is no pattern matches nothing.
|
|
198
|
+
* @param {string} written
|
|
199
|
+
* @returns {(p: string) => boolean}
|
|
200
|
+
*/
|
|
201
|
+
export function globTest(written) {
|
|
202
|
+
const glob = written.replace(/^(?:\.\/)+/, '');
|
|
203
|
+
const folder = glob.endsWith('/');
|
|
204
|
+
const bare = folder ? glob.slice(0, -1) : glob;
|
|
205
|
+
const asName = (p) => p.startsWith(`${bare}/`) || (!folder && p === bare);
|
|
206
|
+
let re = '';
|
|
207
|
+
for (let i = 0; i < bare.length;) {
|
|
208
|
+
const [piece, took] = globPiece(bare, i);
|
|
209
|
+
re += piece;
|
|
210
|
+
i += took;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const test = new RegExp(`^${re}$`);
|
|
215
|
+
|
|
216
|
+
return (p) => asName(p) || (!folder && test.test(p));
|
|
217
|
+
} catch {
|
|
218
|
+
return asName;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** A `worktree <path>` line's item id, when it is a folder right under the worktrees' home. */
|
|
223
|
+
function worktreeId(root, entry) {
|
|
224
|
+
try {
|
|
225
|
+
const rel = path.relative(root, realpathSync.native(entry));
|
|
226
|
+
|
|
227
|
+
return rel && !path.isAbsolute(rel) && isValidId(rel) ? [rel] : [];
|
|
228
|
+
} catch {
|
|
229
|
+
return []; // Registered and gone: nothing to read.
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The main checkout, then each run's registered worktree; a stale folder there is no tree. */
|
|
234
|
+
function treesOf(projectDir) {
|
|
235
|
+
const main = { id: 'main' };
|
|
236
|
+
const home = path.dirname(worktreeOf(projectDir, 'x'));
|
|
237
|
+
const r = run(projectDir, ['worktree', 'list', '--porcelain']);
|
|
238
|
+
if (r.status !== 0 || !existsSync(home)) return [main];
|
|
239
|
+
const root = realpathSync.native(home);
|
|
240
|
+
const ids = r.stdout
|
|
241
|
+
.split('\n')
|
|
242
|
+
.filter((l) => l.startsWith('worktree '))
|
|
243
|
+
.flatMap((l) => worktreeId(root, l.slice('worktree '.length)))
|
|
244
|
+
.sort();
|
|
245
|
+
if (!ids.length) return [main];
|
|
246
|
+
const verified = new Set(verifiedBranches(projectDir));
|
|
247
|
+
|
|
248
|
+
return [main, ...ids.map((id) => ({ id, verified: verified.has(id) }))];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The tree's folder of this project, its base and its HEAD. */
|
|
252
|
+
function treeOf(projectDir, worktree) {
|
|
253
|
+
const trees = treesOf(projectDir);
|
|
254
|
+
const headOf = (dir) => line(dir, 'rev-parse', '--verify', '--quiet', 'HEAD');
|
|
255
|
+
const main = headOf(projectDir);
|
|
256
|
+
// No commit yet: everything against the empty tree.
|
|
257
|
+
const empty = (dir) =>
|
|
258
|
+
run(dir, ['hash-object', '-t', 'tree', '--stdin'], { input: '' }).stdout?.trim() ?? '';
|
|
259
|
+
if (worktree === undefined) {
|
|
260
|
+
const dir = path.resolve(projectDir);
|
|
261
|
+
|
|
262
|
+
return { dir, trees, base: main ?? empty(dir), head: main };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (!isValidId(worktree)) throw new Error(`${worktree} is not an item id`);
|
|
266
|
+
if (!trees.some((t) => t.id === worktree))
|
|
267
|
+
throw new Error(`${worktree} has no worktree: ${trees.map((t) => t.id).join(', ')}`);
|
|
268
|
+
const prefix = line(projectDir, 'rev-parse', '--show-prefix') ?? '';
|
|
269
|
+
const dir = path.join(worktreeOf(projectDir, worktree), prefix);
|
|
270
|
+
const head = headOf(dir);
|
|
271
|
+
|
|
272
|
+
// Where it left the main checkout: no branch is called main, HEAD may be detached.
|
|
273
|
+
return { dir, trees, base: line(dir, 'merge-base', 'HEAD', main) ?? head, head };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Base to index: no disk, so a touched file is no noise; a rename reads from the base's name. */
|
|
277
|
+
function cachedRows(dir, base) {
|
|
278
|
+
const raw = run(dir, ['diff', '-M', '-C', '--cached', '--raw', '-z', '--relative', base]).stdout;
|
|
279
|
+
const rows = new Map();
|
|
280
|
+
const moved = new Set();
|
|
281
|
+
for (const c of parseRaw(raw)) {
|
|
282
|
+
if (c.letter === 'R') moved.add(c.from);
|
|
283
|
+
const row = { path: c.path, status: WORD[c.letter] ?? 'modified', modes: c.modes };
|
|
284
|
+
rows.set(c.path, c.from ? { ...row, from: c.from } : row);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return { rows, moved };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** A status record over what the index says; only status has it: it nets to nothing. */
|
|
291
|
+
function openRow(had, s) {
|
|
292
|
+
const letter = s.xy[0] === '.' ? s.xy[1] : s.xy[0];
|
|
293
|
+
// The old name status gives may be no name of the base's: `from` is the index's to say.
|
|
294
|
+
const own = letter === 'R' || letter === 'C' ? 'modified' : (WORD[letter] ?? 'modified');
|
|
295
|
+
const row = had ?? { path: s.path, status: own, modes: [] };
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
...row,
|
|
299
|
+
modes: [...row.modes, ...s.modes],
|
|
300
|
+
status: s.xy.includes('D') ? 'deleted' : row.status,
|
|
301
|
+
xy: s.xy,
|
|
302
|
+
...(s.from && { openFrom: s.from }),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The old name of a move only on disk, over what the index says of it: a name itself renamed in
|
|
308
|
+
* the index is the base's old name gone; one only just added was never in the base.
|
|
309
|
+
*/
|
|
310
|
+
function absorbed(rows, moved, s) {
|
|
311
|
+
const had = rows.get(s.path);
|
|
312
|
+
if (!had || (!had.from && had.status !== 'added')) return false;
|
|
313
|
+
rows.delete(s.path);
|
|
314
|
+
// Status has a record of this name of its own too, before or after this one: no row of it.
|
|
315
|
+
moved.add(s.path);
|
|
316
|
+
if (had.from)
|
|
317
|
+
rows.set(had.from, { path: had.from, status: 'deleted', modes: had.modes, xy: '.D' });
|
|
318
|
+
|
|
319
|
+
return true;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Status over the index's rows: one row a path, `?` wins over its own `D.` (`rm --cached`). */
|
|
323
|
+
function withStatus({ rows, moved }, status) {
|
|
324
|
+
for (const s of status) {
|
|
325
|
+
if (s.type === '?') rows.set(s.path, { path: s.path, status: 'untracked', modes: [] });
|
|
326
|
+
else if (s.type === 'u') rows.set(s.path, { path: s.path, status: 'conflict', modes: s.modes });
|
|
327
|
+
else if (!moved.has(s.path) && !(s.split && absorbed(rows, moved, s)))
|
|
328
|
+
rows.set(s.path, openRow(rows.get(s.path), s));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return rows;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** A worktree row's `state`: the branch's paths, status's paths, by its `path` or its `from`. */
|
|
335
|
+
function setStates(dir, tree, rows, status) {
|
|
336
|
+
const branch = new Set();
|
|
337
|
+
if (tree.base !== tree.head) {
|
|
338
|
+
const names = ['diff', '--no-renames', '--name-only', '-z', '--relative', tree.base, 'HEAD'];
|
|
339
|
+
for (const p of run(dir, names).stdout.split('\0')) branch.add(p);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const open = new Set(status.flatMap((s) => (s.from ? [s.path, s.from] : [s.path])));
|
|
343
|
+
const hit = (set, r) => set.has(r.path) || set.has(r.from);
|
|
344
|
+
for (const r of rows.values()) {
|
|
345
|
+
if (!hit(branch, r)) r.state = 'open';
|
|
346
|
+
else r.state = hit(open, r) ? 'both' : 'branch';
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** A status that failed: null for a folder git does not know, else no tree to call clean. */
|
|
351
|
+
function notGit(dir, res) {
|
|
352
|
+
const asked = run(dir, ['rev-parse', '--is-inside-work-tree'], {
|
|
353
|
+
env: { ...process.env, LC_ALL: 'C' },
|
|
354
|
+
});
|
|
355
|
+
// "not a git repository: <path>" names a broken link to one: only the plain answer means none.
|
|
356
|
+
const none = /not a git repository \(or any/i.test(asked.stderr ?? '');
|
|
357
|
+
if (asked.status !== 0 && none && !existsSync(path.join(dir, '.git'))) return null;
|
|
358
|
+
throw new Error(`changes: git status failed: ${(res.stderr || res.error?.message || '').trim()}`);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** One row a path, each its end state against the base (Changes-Plan d9a); null: not in git. */
|
|
362
|
+
function rowsOf(tree, worktree) {
|
|
363
|
+
const { dir } = tree;
|
|
364
|
+
const prefix = line(dir, 'rev-parse', '--show-prefix') ?? '';
|
|
365
|
+
// Renames on, whatever the user's config: a step of a rename chain is no row of its own.
|
|
366
|
+
const form = ['--porcelain=v2', '-z', '--untracked-files=all', '--', '.'];
|
|
367
|
+
const res = run(dir, ['-c', 'status.renames=true', 'status', ...form]);
|
|
368
|
+
if (res.status !== 0) return notGit(dir, res);
|
|
369
|
+
const status = parseStatus(res.stdout, prefix);
|
|
370
|
+
const rows = withStatus(cachedRows(dir, tree.base), status);
|
|
371
|
+
if (worktree !== undefined) setStates(dir, tree, rows, status);
|
|
372
|
+
|
|
373
|
+
return [...rows.values()]
|
|
374
|
+
.filter((r) => !NOISE.test(r.path))
|
|
375
|
+
.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const extOf = (p) => path.extname(p).slice(1).toLowerCase();
|
|
379
|
+
|
|
380
|
+
/** `check-attr`'s answers of `paths`: `filter` and `diff` by path. */
|
|
381
|
+
function attrsOf(dir, paths) {
|
|
382
|
+
const out = new Map();
|
|
383
|
+
if (!paths.length) return out;
|
|
384
|
+
const r = run(dir, ['check-attr', '--stdin', '-z', 'filter', 'diff'], {
|
|
385
|
+
input: paths.join('\0'),
|
|
386
|
+
});
|
|
387
|
+
const t = (r.stdout ?? '').split('\0');
|
|
388
|
+
for (let i = 0; i + 2 < t.length; i += 3)
|
|
389
|
+
out.set(t[i], { ...out.get(t[i]), [t[i + 1]]: t[i + 2] });
|
|
390
|
+
|
|
391
|
+
return out;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** What git's numstat calls binary against the base, with the project's `.gitattributes`. */
|
|
395
|
+
function binaryOf(dir, base) {
|
|
396
|
+
const form = ['--numstat', '-z', '-M', '-C', '--no-ext-diff', '--no-textconv', '--relative'];
|
|
397
|
+
|
|
398
|
+
return parseBinary(run(dir, [...NO_REFRESH, 'diff', ...form, base]).stdout);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** A row's first test: its mode, then what is on disk; no byte of the file is read. */
|
|
402
|
+
function byDisk(dir, r) {
|
|
403
|
+
const why = r.modes.map((m) => MODE_WHY[m]).find(Boolean);
|
|
404
|
+
if (why) return { ...r, view: 'listed', why };
|
|
405
|
+
const state = r.status === 'deleted' ? { kind: 'gone' } : fileState(dir, r.path);
|
|
406
|
+
if (state.kind === 'listed') return { ...r, view: 'listed', why: state.why };
|
|
407
|
+
|
|
408
|
+
return state.kind === 'file' ? { ...r, size: state.size } : r;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** An untracked file: binary by a NUL in its head, as git says of a tracked one. */
|
|
412
|
+
function untrackedView(dir, r) {
|
|
413
|
+
const head = readHead(dir, r.path, NUL_PROBE);
|
|
414
|
+
if (head.kind === 'listed') return { view: 'listed', why: head.why };
|
|
415
|
+
|
|
416
|
+
return { view: head.bytes?.includes(0) ? 'binary' : 'text' };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** What a click on a row past byDisk does, in Changes-Plan's order. */
|
|
420
|
+
function viewOf(dir, r, ctx) {
|
|
421
|
+
const glob = ctx.noDiff.find((g) => g.test(r.path));
|
|
422
|
+
if (glob) return { view: 'listed', why: `no_diff ${glob.text}` };
|
|
423
|
+
const attr = ctx.attrs.get(r.path) ?? {};
|
|
424
|
+
if (attr.filter === 'lfs') return { view: 'listed', why: 'Git LFS' };
|
|
425
|
+
if (PICTURES[extOf(r.path)]) return { view: 'image' };
|
|
426
|
+
if (attr.diff === 'unset' || ctx.binary.has(r.path)) return { view: 'binary' };
|
|
427
|
+
|
|
428
|
+
return r.status === 'untracked' ? untrackedView(dir, r) : { view: 'text' };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Each row with what a click does: a link named `x.png` stays a link. */
|
|
432
|
+
function withViews(tree, shown, noDiff) {
|
|
433
|
+
const { dir } = tree;
|
|
434
|
+
const rows = shown.map((r) => byDisk(dir, r));
|
|
435
|
+
const open = rows.filter((r) => !r.view);
|
|
436
|
+
const ctx = {
|
|
437
|
+
noDiff,
|
|
438
|
+
attrs: attrsOf(
|
|
439
|
+
dir,
|
|
440
|
+
open.map((r) => r.path),
|
|
441
|
+
),
|
|
442
|
+
binary: binaryOf(dir, tree.base),
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
return rows.map((r) => (r.view ? r : { ...r, ...viewOf(dir, r, ctx) }));
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* The project's list of files shown without a diff. A broken config says so on the moves, and a
|
|
450
|
+
* project with no gcf/ has none: the list of changes still reads.
|
|
451
|
+
*/
|
|
452
|
+
async function noDiffOf(projectDir) {
|
|
453
|
+
const list = await Promise.resolve()
|
|
454
|
+
.then(() => readConfig(gcfRoot(projectDir)))
|
|
455
|
+
.then(
|
|
456
|
+
(c) => c.no_diff,
|
|
457
|
+
() => [],
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
return list.map((text) => ({ text, test: globTest(text) }));
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
async function readTree(projectDir, asked) {
|
|
464
|
+
// The list names the checkout `main`: asked by that word it is the checkout.
|
|
465
|
+
const worktree = asked === 'main' ? undefined : asked;
|
|
466
|
+
const tree = treeOf(projectDir, worktree);
|
|
467
|
+
|
|
468
|
+
return { tree, rows: rowsOf(tree, worktree), noDiff: await noDiffOf(projectDir) };
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const outward = ({ modes: _m, xy: _x, openFrom: _o, ...row }) => row;
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* What is uncommitted in the main checkout, or a run's worktree (`worktree`: its item's id).
|
|
475
|
+
* A folder git does not know answers empty.
|
|
476
|
+
* @param {string} projectDir
|
|
477
|
+
* @param {{worktree?: string}} [opts]
|
|
478
|
+
*/
|
|
479
|
+
export async function changesOf(projectDir, { worktree } = {}) {
|
|
480
|
+
const { tree, rows, noDiff } = await readTree(projectDir, worktree);
|
|
481
|
+
const name = worktree ?? 'main';
|
|
482
|
+
if (!rows)
|
|
483
|
+
return { tree: name, base: null, count: 0, files: [], trees: tree.trees, more: 0, watch: null };
|
|
484
|
+
const shown = rows.slice(0, LIST_MAX);
|
|
485
|
+
const gitPath = (p) => line(tree.dir, 'rev-parse', '--git-path', p);
|
|
486
|
+
|
|
487
|
+
return {
|
|
488
|
+
tree: name,
|
|
489
|
+
base: tree.base,
|
|
490
|
+
count: rows.length,
|
|
491
|
+
files: withViews(tree, shown, noDiff).map(outward),
|
|
492
|
+
trees: tree.trees,
|
|
493
|
+
more: rows.length - shown.length,
|
|
494
|
+
watch: { index: gitPath('index'), head: gitPath('HEAD') },
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** The row of `p` with its view, or the refusal of a path the list does not hold. */
|
|
499
|
+
async function rowOf(projectDir, p, worktree) {
|
|
500
|
+
const { tree, rows, noDiff } = await readTree(projectDir, worktree);
|
|
501
|
+
const at = (rows ?? []).slice(0, LIST_MAX).find((r) => r.path === p);
|
|
502
|
+
if (!at)
|
|
503
|
+
throw refusal(
|
|
504
|
+
'clean',
|
|
505
|
+
{ paths: [p], left: [] },
|
|
506
|
+
`changes: ${p} is not among the uncommitted files`,
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
return { tree, row: withViews(tree, [at], noDiff)[0] };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** A diff with no hunk says nothing: git prints the head of a file touched with no change. */
|
|
513
|
+
const said = (d) => /^(?!diff --git |index ).+/m.test(d);
|
|
514
|
+
|
|
515
|
+
/** `text` by the limits; `overrun`: git's pipe was cut, or the file longer than was read. */
|
|
516
|
+
function cutOf(text, overrun = false) {
|
|
517
|
+
const clean = text.replace(/\r$/gm, '');
|
|
518
|
+
const lines = clean.split('\n');
|
|
519
|
+
const total = lines.at(-1) === '' ? lines.length - 1 : lines.length;
|
|
520
|
+
let kept = lines.slice(0, DIFF_LINES).join('\n');
|
|
521
|
+
if (kept.length > DIFF_BYTES) kept = kept.slice(0, kept.lastIndexOf('\n', DIFF_BYTES));
|
|
522
|
+
const shown = kept.split('\n').length;
|
|
523
|
+
if (shown >= total && !overrun) return { text: clean, cut: null };
|
|
524
|
+
|
|
525
|
+
return { text: `${kept}\n`, cut: { shown, lines: overrun ? null : total } };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const NO_PART = { text: '', cut: null };
|
|
529
|
+
|
|
530
|
+
/** git's output ran past the read: the process was stopped there. */
|
|
531
|
+
const overran = (r) =>
|
|
532
|
+
/** @type {NodeJS.ErrnoException | undefined} */ (r.error)?.code === 'ENOBUFS';
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Whether a diff's hunks are line endings alone. Its lines drop their \r, so they would read as no
|
|
536
|
+
* change at all: git asked again, past the \r. A rename or a mode has no hunk: no such thing.
|
|
537
|
+
*/
|
|
538
|
+
function eolOnly(dir, raw, form, specs) {
|
|
539
|
+
if (!raw.includes('\r') || !/^@@ /m.test(raw)) return false;
|
|
540
|
+
const again = run(dir, [...form, '--ignore-cr-at-eol', '--', ...specs], { maxBuffer: DIFF_READ });
|
|
541
|
+
|
|
542
|
+
return !/^@@ /m.test(again.stdout ?? '');
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** One part of a row's diff: nothing when it says nothing. */
|
|
546
|
+
function diffPart(dir, args, names) {
|
|
547
|
+
const specs = names.filter(Boolean).map((n) => `:(literal)${n}`);
|
|
548
|
+
const form = [...NO_REFRESH, 'diff', ...DIFF_FORM, '-M', '-C', ...args];
|
|
549
|
+
const r = run(dir, [...form, '--', ...specs], { maxBuffer: DIFF_READ });
|
|
550
|
+
const raw = r.stdout ?? '';
|
|
551
|
+
const part = cutOf(raw, overran(r));
|
|
552
|
+
|
|
553
|
+
return said(part.text) ? { ...part, eol: eolOnly(dir, raw, form, specs) } : NO_PART;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** A tracked row's parts: two beside `diff`, only where base to disk alone would mislead. */
|
|
557
|
+
function trackedDiff(tree, row) {
|
|
558
|
+
const { dir, base, head } = tree;
|
|
559
|
+
const names = [row.from, row.path];
|
|
560
|
+
const disk = diffPart(dir, [base], names);
|
|
561
|
+
// X and Y both set: the index differs from HEAD and from disk. A commit would take that one.
|
|
562
|
+
const both = row.xy && !row.xy.includes('.');
|
|
563
|
+
const index = both ? diffPart(dir, ['--cached', base], names) : NO_PART;
|
|
564
|
+
const undone = !disk.text && head && base !== head;
|
|
565
|
+
const open = undone ? diffPart(dir, ['HEAD'], [row.openFrom, row.path]) : NO_PART;
|
|
566
|
+
const parts = { diff: disk, index, open };
|
|
567
|
+
const eol = Object.keys(parts).filter((k) => parts[k].eol);
|
|
568
|
+
|
|
569
|
+
return {
|
|
570
|
+
diff: disk.text,
|
|
571
|
+
cut: disk.cut ?? index.cut ?? open.cut,
|
|
572
|
+
...(index.text && { index: index.text }),
|
|
573
|
+
...(open.text && { open: open.text }),
|
|
574
|
+
...(eol.length && { eol }),
|
|
575
|
+
...(row.status === 'conflict' && { conflict: true }),
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* A row's diff against the base (Changes-Plan d10): `diff` base to disk; `index` base to index
|
|
581
|
+
* when the index differs from HEAD and from disk; `open` HEAD to disk when a worktree row's
|
|
582
|
+
* `diff` is empty. Text rows only.
|
|
583
|
+
* @param {string} projectDir
|
|
584
|
+
* @param {string} p
|
|
585
|
+
* @param {{worktree?: string}} [opts]
|
|
586
|
+
*/
|
|
587
|
+
export async function changeDiff(projectDir, p, { worktree } = {}) {
|
|
588
|
+
const { tree, row } = await rowOf(projectDir, p, worktree);
|
|
589
|
+
if (row.view !== 'text')
|
|
590
|
+
throw new Error(`changes diff: ${p} opens no diff: ${row.why ?? row.view}`);
|
|
591
|
+
const head = { tree: worktree ?? 'main', path: p };
|
|
592
|
+
if (row.status !== 'untracked') return { ...head, ...trackedDiff(tree, row) };
|
|
593
|
+
const { text, cut } = cutOf(newFileDiff(tree.dir, p, DIFF_BYTES), row.size > DIFF_BYTES);
|
|
594
|
+
|
|
595
|
+
return { ...head, diff: text, cut };
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** A picture on disk, through fileState: a link never shows what lies outside. */
|
|
599
|
+
function pictureNow(tree, row, p) {
|
|
600
|
+
if (row.status === 'deleted') throw new Error(`changes file: ${p} is deleted: no now`);
|
|
601
|
+
const head = readHead(tree.dir, p, PICTURE_MAX);
|
|
602
|
+
if (head.kind !== 'file') throw new Error(`changes file: ${p} cannot be read`);
|
|
603
|
+
if (head.size > PICTURE_MAX) throw new Error(`changes file: ${p} is past ${PICTURE_MAX} bytes`);
|
|
604
|
+
|
|
605
|
+
return head.bytes;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** A picture in the base, by the name the base has. */
|
|
609
|
+
function pictureBase(tree, row, p) {
|
|
610
|
+
if (row.status === 'added' || row.status === 'untracked')
|
|
611
|
+
throw new Error(`changes file: ${p} is new: no base`);
|
|
612
|
+
const r = run(tree.dir, ['show', `${tree.base}:./${row.from ?? p}`], {
|
|
613
|
+
encoding: 'buffer',
|
|
614
|
+
maxBuffer: PICTURE_MAX + 1,
|
|
615
|
+
});
|
|
616
|
+
if (overran(r)) throw new Error(`changes file: ${p} is past ${PICTURE_MAX} bytes in the base`);
|
|
617
|
+
if (r.status !== 0) throw new Error(`changes file: ${p} has no base`);
|
|
618
|
+
|
|
619
|
+
return r.stdout;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* A picture's bytes before or after, as base64: `cli()` reads stdout as text. An `image` row of
|
|
624
|
+
* the list only.
|
|
625
|
+
* @param {string} projectDir
|
|
626
|
+
* @param {string} p
|
|
627
|
+
* @param {'base' | 'now'} side
|
|
628
|
+
* @param {{worktree?: string}} [opts]
|
|
629
|
+
*/
|
|
630
|
+
export async function changeFile(projectDir, p, side, { worktree } = {}) {
|
|
631
|
+
if (side !== 'base' && side !== 'now') throw new Error('changes file: --side base or now');
|
|
632
|
+
const { tree, row } = await rowOf(projectDir, p, worktree);
|
|
633
|
+
if (row.view !== 'image') throw new Error(`changes file: ${p} is no picture`);
|
|
634
|
+
const bytes = side === 'now' ? pictureNow(tree, row, p) : pictureBase(tree, row, p);
|
|
635
|
+
|
|
636
|
+
return {
|
|
637
|
+
path: p,
|
|
638
|
+
side,
|
|
639
|
+
type: PICTURES[extOf(p)],
|
|
640
|
+
size: bytes.length,
|
|
641
|
+
base64: bytes.toString('base64'),
|
|
642
|
+
};
|
|
643
|
+
}
|
package/lib/config-set.js
CHANGED
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
import { AIF_CONFIG, LANGUAGE_RE, languageIn, readAifConfig } from './aif-config.js';
|
|
29
29
|
import { agentOf } from './agent-permissions.js';
|
|
30
30
|
|
|
31
|
-
/** @typedef {'number' | 'share' | 'mode' | 'command' | 'language' | 'word'} Kind */
|
|
31
|
+
/** @typedef {'number' | 'share' | 'mode' | 'command' | 'language' | 'word' | 'list'} Kind */
|
|
32
32
|
|
|
33
33
|
// Thrown inside the move to leave without a write or a commit.
|
|
34
34
|
const NO_CHANGE = new Error('no change');
|
|
@@ -203,6 +203,11 @@ export const SETTINGS = [
|
|
|
203
203
|
kind: 'number',
|
|
204
204
|
label: 'default spread of a feel envelope around a GDD number (0.3: ±30%)',
|
|
205
205
|
},
|
|
206
|
+
{
|
|
207
|
+
key: 'no_diff',
|
|
208
|
+
kind: 'list',
|
|
209
|
+
label: 'files the board lists and never opens, one glob a line (**/*.prefab)',
|
|
210
|
+
},
|
|
206
211
|
];
|
|
207
212
|
|
|
208
213
|
const BY_KEY = new Map(SETTINGS.map((s) => [s.key, s]));
|
|
@@ -237,6 +242,7 @@ const GROUPS = [
|
|
|
237
242
|
],
|
|
238
243
|
],
|
|
239
244
|
['Design import', ['context_budget', 'pillars_budget', 'envelope_default_spread']],
|
|
245
|
+
['Changes', ['no_diff']],
|
|
240
246
|
];
|
|
241
247
|
const GROUP_OF = new Map(GROUPS.flatMap(([group, keys]) => keys.map((k) => [k, group])));
|
|
242
248
|
|
|
@@ -290,11 +296,23 @@ function languageOf(setting, t) {
|
|
|
290
296
|
return t || null;
|
|
291
297
|
}
|
|
292
298
|
|
|
299
|
+
/** A list, one entry a line, from a text box or a command line; none left takes the key off. */
|
|
300
|
+
function linesOf(t) {
|
|
301
|
+
const list = t
|
|
302
|
+
.split(/\r?\n/)
|
|
303
|
+
.map((l) => l.trim())
|
|
304
|
+
.filter(Boolean);
|
|
305
|
+
|
|
306
|
+
return list.length ? list : null;
|
|
307
|
+
}
|
|
308
|
+
|
|
293
309
|
/** The value of `text` for a setting of `kind`; an empty number goes back to its default. */
|
|
294
310
|
function valueOf(setting, text) {
|
|
295
311
|
const t = String(text ?? '').trim();
|
|
296
312
|
if (setting.kind === 'language') return languageOf(setting, t);
|
|
297
313
|
|
|
314
|
+
if (setting.kind === 'list') return linesOf(t);
|
|
315
|
+
|
|
298
316
|
if (setting.kind === 'command') {
|
|
299
317
|
if (!t) throw new Error(`${setting.key}: name the command, or none`);
|
|
300
318
|
|