gcf-aif-ext 0.21.0 → 0.22.1
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/README.md +14 -0
- package/commands/gcf.js +74 -1
- package/extension.json +1 -1
- package/lib/agent-permissions.js +16 -9
- package/lib/changes.js +643 -0
- package/lib/config-set.js +19 -1
- package/lib/domain.js +2 -0
- package/lib/edits.js +2 -13
- package/lib/gate.js +13 -1
- package/lib/gcf-paths.js +10 -0
- package/lib/git.js +18 -1
- package/lib/linter.js +5 -3
- package/lib/tree-file.js +107 -0
- package/package.json +1 -1
- package/templates/agents-block.md +2 -0
- package/templates/config.yml +4 -0
package/README.md
CHANGED
|
@@ -50,6 +50,19 @@ Updating preserves `gcf/`, `cnlp.config.json` and the pre-commit hook. Run `ai-f
|
|
|
50
50
|
|
|
51
51
|
On Windows, run `extension add` and `extension update` from PowerShell or cmd, not Git Bash: the installer unpacks with `tar`, and Git Bash's GNU tar reads `C:\…` as a remote host (`Cannot connect to C: resolve failed`). The Windows `tar.exe` handles it.
|
|
52
52
|
|
|
53
|
+
### 0.22.1
|
|
54
|
+
|
|
55
|
+
- An agent may read the history of past runs: the deny group `human-moves` held `ai-factory gcf-harness run*`, which also caught `gcf-harness runs`, the very command `AGENTS.md` tells it to use. The rule is now `ai-factory gcf-harness run *`. A session started after the update takes it; nothing to re-run in the project.
|
|
56
|
+
|
|
57
|
+
### 0.22.0
|
|
58
|
+
|
|
59
|
+
- **`gcf changes`**: what is not committed, anywhere in the project, read only. `gcf changes [--worktree <id>] [--json]` lists the main checkout or a run's worktree: each file's status against the base (a worktree's base is where it left the main checkout, so what the run committed on its branch shows too, with its state `branch`, `open` or `both`), and what a board's click does (`text`, `image`, `binary`, `listed`). `gcf changes diff --path <p>` gives a text file's diff, and `gcf changes file --path <p> --side base|now --json` a picture's bytes as base64. `gcf/.metrics/` and `.gcf-session/` are never listed.
|
|
60
|
+
- **`no_diff:`** in `gcf/config.yml` (`gcf config set no_diff --value=<lines>`, one glob a line, the first list among the settings): files a board lists and never opens. GCF names no stack's files, the list is the project's. Links, submodules, nested repositories, anything on disk that is no regular file and Git LFS files are listed and never opened, with no setting.
|
|
61
|
+
- `gcf edits diff` no longer reads through a link: a new file under `gcf/` that is a link, or lies behind a folder link or a Windows junction, says so and shows no byte of what it points to; a new file past 16 MB is cut, not read whole.
|
|
62
|
+
- A keep no longer fails on the text it collapses itself: `camelcase-member-list` flags three bullets in a row each **headed** by a CamelCase name (`HealthComponent holds the pool`), not sentences that mention one, paths or file names.
|
|
63
|
+
- A move the commit gate refuses says so in its own words and ends with `gcf-refusal: {"code":"gate","problems":[…]}`: the line of the file, what to do, none of git's warnings around it. Before, it read `commit failed:` with git's raw output, and the line number counted from below the frontmatter.
|
|
64
|
+
- The domain's `sections.changes` tells a board the core can read this. Update the board with it: one before 0.20 shows the `no_diff` list as one line of commas, and saving that makes it one glob.
|
|
65
|
+
|
|
53
66
|
### 0.21.0
|
|
54
67
|
|
|
55
68
|
- Bundles `cnlp-kit` 3.10. Its project glossary is GCF's glossary now: **`GLOSSARY.md` at the project root**, beside `cnlp.config.json`, in cnlp's own shape — a `purpose:` block and `terms:` records of `term`, `means` and an optional `avoid` (a comma list). `npx --yes cnlp-kit check` finds it by name and rejects every word it avoids in the systems docs and `PILLARS.md`; `ai-factory gcf design check` (the commit gate) rejects them in design units and work items (M4). An `avoid:` is the project's deny list everywhere: list only a word wrong in every document, and prefer a phrase ("tile hand") to a bare word with other uses.
|
|
@@ -371,6 +384,7 @@ Removal leaves `gcf/` and `cnlp.config.json` in place. It also leaves the pre-co
|
|
|
371
384
|
- branches — `worktree <id>`, `verify <id> --mark`, `integrate [<id>...]`: a run in its own worktree, the `GCF-Verified` trailer, the merge into main and the move; every committed move holds `.gcf-session/main.lock`
|
|
372
385
|
- design and sources — `design check`, `design export`, `design import`, `design part`, `design save`, `design discard`, `design commit`, `design migrate`, `anchor`, `section`, `quotes`, `lint`
|
|
373
386
|
- hand edits under `gcf/` — `edits diff|commit|discard --path <p>…`: what a move's `dirty` refusal names, shown, committed or put back
|
|
387
|
+
- what is uncommitted, anywhere in the project — `changes [--worktree <id>] [--json]`, `changes diff --path <p>`, `changes file --path <p> --side base|now`: read only; the project's `no_diff:` globs, links, submodules, nested repositories and Git LFS files are listed and never opened
|
|
374
388
|
- setup — `init`, `index`, `hooks-install`
|
|
375
389
|
|
|
376
390
|
Every move of an item makes one commit `gcf(<move>): <id>` with every file it touched — the item, history or graveyard, `LIVE.md`, the systems documents, the parent feature, a removal item, the archived plan. `take`, `item new` and `inbox` do not commit. These are plain Node scripts with no LLM in the loop: the same functions the skills call, the pre-commit gate runs, and the harness gates against.
|
package/commands/gcf.js
CHANGED
|
@@ -795,6 +795,72 @@ function registerEdits(gcf) {
|
|
|
795
795
|
});
|
|
796
796
|
}
|
|
797
797
|
|
|
798
|
+
function registerChanges(gcf) {
|
|
799
|
+
const changes = () => lib('changes');
|
|
800
|
+
const TREE = "a run's worktree, by its item id; the main checkout without it";
|
|
801
|
+
// `changes` takes `--worktree` and `--json` too, whatever word they follow: its own are here.
|
|
802
|
+
const allOf = (o, cmd) => cmd?.optsWithGlobals?.() ?? o;
|
|
803
|
+
const group = gcf
|
|
804
|
+
.command('changes')
|
|
805
|
+
.description("What is not committed in the main checkout or a run's worktree: read only")
|
|
806
|
+
.allowExcessArguments(false)
|
|
807
|
+
.option('--worktree <id>', TREE)
|
|
808
|
+
.option('--json', '{tree, base, count, files, trees, more, watch}')
|
|
809
|
+
.action(async (o) => {
|
|
810
|
+
const r = await (await changes()).changesOf(process.cwd(), { worktree: o.worktree });
|
|
811
|
+
out(o.json, r, (x) =>
|
|
812
|
+
x.files.length
|
|
813
|
+
? [
|
|
814
|
+
...x.files.map(
|
|
815
|
+
(f) =>
|
|
816
|
+
`${f.status}${f.state ? ` (${f.state})` : ''} ` +
|
|
817
|
+
`${f.from ? `${f.from} -> ` : ''}${f.path}${f.why ? ` [${f.why}]` : ''}`,
|
|
818
|
+
),
|
|
819
|
+
...(x.more ? [`and ${x.more} more`] : []),
|
|
820
|
+
].join('\n')
|
|
821
|
+
: 'nothing uncommitted',
|
|
822
|
+
);
|
|
823
|
+
});
|
|
824
|
+
group
|
|
825
|
+
.command('diff')
|
|
826
|
+
.description("A text file's diff against the tree's base")
|
|
827
|
+
.requiredOption('--path <path>', 'a path from the list')
|
|
828
|
+
.option('--worktree <id>', TREE)
|
|
829
|
+
.option('--json', '{tree, path, diff, index?, open?, cut}')
|
|
830
|
+
.action(async (own, cmd) => {
|
|
831
|
+
const o = allOf(own, cmd);
|
|
832
|
+
const r = await (await changes()).changeDiff(process.cwd(), o.path, { worktree: o.worktree });
|
|
833
|
+
out(o.json, r, (x) =>
|
|
834
|
+
[
|
|
835
|
+
x.diff,
|
|
836
|
+
x.index && `in the index, not on disk:\n${x.index}`,
|
|
837
|
+
x.open && `not committed:\n${x.open}`,
|
|
838
|
+
x.eol && `line endings only: ${x.eol.join(', ')}`,
|
|
839
|
+
!x.diff &&
|
|
840
|
+
!x.index &&
|
|
841
|
+
!x.open &&
|
|
842
|
+
(x.conflict ? 'conflict: resolve it to see a difference' : 'no difference in content'),
|
|
843
|
+
]
|
|
844
|
+
.filter(Boolean)
|
|
845
|
+
.join('\n'),
|
|
846
|
+
);
|
|
847
|
+
});
|
|
848
|
+
group
|
|
849
|
+
.command('file')
|
|
850
|
+
.description("A picture's bytes before or after, as base64 (a board draws them)")
|
|
851
|
+
.requiredOption('--path <path>', 'a picture from the list')
|
|
852
|
+
.requiredOption('--side <side>', 'base or now')
|
|
853
|
+
.option('--worktree <id>', TREE)
|
|
854
|
+
.option('--json', '{path, side, type, size, base64}')
|
|
855
|
+
.action(async (own, cmd) => {
|
|
856
|
+
const o = allOf(own, cmd);
|
|
857
|
+
const r = await (
|
|
858
|
+
await changes()
|
|
859
|
+
).changeFile(process.cwd(), o.path, o.side, { worktree: o.worktree });
|
|
860
|
+
out(true, r);
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
|
|
798
864
|
function registerMigrate(design) {
|
|
799
865
|
design
|
|
800
866
|
.command('migrate')
|
|
@@ -1658,7 +1724,13 @@ function registerSettings(gcf) {
|
|
|
1658
1724
|
await readConfig(gcfRoot()); // a config the rules refuse says so here
|
|
1659
1725
|
const list = await (await lib('config-set')).projectSettings(process.cwd());
|
|
1660
1726
|
out(o.json, list, (x) =>
|
|
1661
|
-
x
|
|
1727
|
+
x
|
|
1728
|
+
.map((s) =>
|
|
1729
|
+
Array.isArray(s.value)
|
|
1730
|
+
? `${s.key}: # ${s.label}\n${s.value.map((v) => ` ${v}`).join('\n')}`
|
|
1731
|
+
: `${s.key}: ${s.value ?? '-'} # ${s.label}`,
|
|
1732
|
+
)
|
|
1733
|
+
.join('\n'),
|
|
1662
1734
|
);
|
|
1663
1735
|
});
|
|
1664
1736
|
config
|
|
@@ -1902,6 +1974,7 @@ export function register(program) {
|
|
|
1902
1974
|
registerQuotes(gcf);
|
|
1903
1975
|
registerDesign(gcf);
|
|
1904
1976
|
registerEdits(gcf);
|
|
1977
|
+
registerChanges(gcf);
|
|
1905
1978
|
registerWhere(gcf);
|
|
1906
1979
|
registerMoves(gcf);
|
|
1907
1980
|
registerItem(gcf);
|
package/extension.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://raw.githubusercontent.com/lee-to/ai-factory/2.x/schemas/extension.schema.json",
|
|
3
3
|
"name": "gcf-aif-ext",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.22.1",
|
|
5
5
|
"description": "Game Context Framework for AI Factory",
|
|
6
6
|
"skills": [
|
|
7
7
|
"skills/gcf-init",
|
package/lib/agent-permissions.js
CHANGED
|
@@ -130,7 +130,8 @@ export const DENY_GROUPS = [
|
|
|
130
130
|
// The stash stack is the checkout's and every worktree's: a pop may take the operator's.
|
|
131
131
|
id: 'git-discard',
|
|
132
132
|
label:
|
|
133
|
-
'uncommitted work: reset --hard, clean, checkout or restore of all, stash
|
|
133
|
+
'uncommitted work: reset --hard, clean, checkout or restore of all, stash ' +
|
|
134
|
+
'(list and show are free)',
|
|
134
135
|
on: true,
|
|
135
136
|
rules: shell(
|
|
136
137
|
'git reset --hard*',
|
|
@@ -196,7 +197,8 @@ export const DENY_GROUPS = [
|
|
|
196
197
|
'ai-factory gcf edits commit*',
|
|
197
198
|
'ai-factory gcf edits discard*',
|
|
198
199
|
'ai-factory gcf-harness init*',
|
|
199
|
-
|
|
200
|
+
// The space keeps `gcf-harness runs`, the history of past runs, a read the agent may make.
|
|
201
|
+
'ai-factory gcf-harness run *',
|
|
200
202
|
// The autopilot's card session: an agent starts none.
|
|
201
203
|
'ai-factory gcf-harness card*',
|
|
202
204
|
'ai-factory gcf-harness stop*',
|
|
@@ -365,6 +367,17 @@ function checkChange(change, current) {
|
|
|
365
367
|
if (missing) throw new Error(`${missing} is not one of your own deny rules: turn its group off`);
|
|
366
368
|
}
|
|
367
369
|
|
|
370
|
+
/** An old group id in the file gives its value to the parts not written yet, and leaves. */
|
|
371
|
+
function retireAliases(doc) {
|
|
372
|
+
for (const old of Object.keys(ALIASES)) {
|
|
373
|
+
if (!doc.hasIn(['agent', 'groups', old])) continue;
|
|
374
|
+
for (const p of ALIASES[old])
|
|
375
|
+
if (!doc.hasIn(['agent', 'groups', p]))
|
|
376
|
+
doc.setIn(['agent', 'groups', p], doc.getIn(['agent', 'groups', old]));
|
|
377
|
+
doc.deleteIn(['agent', 'groups', old]);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
368
381
|
/**
|
|
369
382
|
* Turn groups on or off, add or remove a custom rule; commit gcf(permissions). Only the
|
|
370
383
|
* `agent:` section changes: the rest of config.yml keeps its keys and comments. The file is
|
|
@@ -386,13 +399,7 @@ export async function setPermissions(projectDir, change, { commit = true } = {})
|
|
|
386
399
|
for (const p of partsOf(id)) doc.setIn(['agent', 'groups', p], true);
|
|
387
400
|
for (const id of change.off ?? [])
|
|
388
401
|
for (const p of partsOf(id)) doc.setIn(['agent', 'groups', p], false);
|
|
389
|
-
|
|
390
|
-
if (doc.hasIn(['agent', 'groups', old])) {
|
|
391
|
-
for (const p of ALIASES[old])
|
|
392
|
-
if (!doc.hasIn(['agent', 'groups', p]))
|
|
393
|
-
doc.setIn(['agent', 'groups', p], doc.getIn(['agent', 'groups', old]));
|
|
394
|
-
doc.deleteIn(['agent', 'groups', old]);
|
|
395
|
-
}
|
|
402
|
+
retireAliases(doc);
|
|
396
403
|
|
|
397
404
|
const deny = [...new Set([...current.deny_extra, ...(change.deny ?? [])])].filter(
|
|
398
405
|
(r) => !change.undeny?.some((u) => sameRule(parseRule(u), parseRule(r))),
|
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
|
|
package/lib/domain.js
CHANGED
|
@@ -807,6 +807,8 @@ const SECTIONS = {
|
|
|
807
807
|
questions: { answer: 'question-answer' },
|
|
808
808
|
// A move's `dirty` refusal: the board reads `gcf edits diff` and offers these.
|
|
809
809
|
edits: { commit: 'edits-commit', discard: 'edits-discard' },
|
|
810
|
+
// `gcf changes` reads what is uncommitted: the board's Changes screen and its header's marker.
|
|
811
|
+
changes: {},
|
|
810
812
|
lessons: {},
|
|
811
813
|
playtest: {
|
|
812
814
|
// The inbox category a note from the playtest screen goes with.
|
package/lib/edits.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// Hand edits under gcf/ (Rich-Chat-Plan §5): what a move's `dirty` refusal names, shown, committed
|
|
2
2
|
// or put back. Reading is anyone's; commit and discard are the operator's (the CLI holds them,
|
|
3
3
|
// and the deny group human-moves): an agent does not decide over a human's unfinished work.
|
|
4
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
5
4
|
import { spawnSync } from 'node:child_process';
|
|
6
5
|
import path from 'node:path';
|
|
7
6
|
import { dirtyGcf, inParts, literal, restoreFromHead } from './git.js';
|
|
8
7
|
import { locked } from './main-lock.js';
|
|
9
8
|
import { refusal } from './refusal.js';
|
|
9
|
+
import { newFileDiff } from './tree-file.js';
|
|
10
10
|
import { commitMove } from './unit-ops.js';
|
|
11
11
|
|
|
12
12
|
/** A path from the project with `/`: the form dirtyGcf gives and a refusal names. */
|
|
@@ -42,24 +42,13 @@ const git = (projectDir, ...args) =>
|
|
|
42
42
|
spawnSync('git', ['-c', 'core.quotePath=false', ...args], { cwd: projectDir, encoding: 'utf8' })
|
|
43
43
|
.stdout ?? '';
|
|
44
44
|
|
|
45
|
-
/** A file git does not know yet as a diff of its own: each line added. */
|
|
46
|
-
function newFileDiff(projectDir, f) {
|
|
47
|
-
const file = path.join(projectDir, f);
|
|
48
|
-
if (!existsSync(file)) return '';
|
|
49
|
-
const text = readFileSync(file, 'utf8');
|
|
50
|
-
if (text.includes('\0')) return `new file ${f}: binary\n`;
|
|
51
|
-
const lines = text.replace(/\n$/, '').split('\n');
|
|
52
|
-
|
|
53
|
-
return `new file ${f}\n--- /dev/null\n+++ b/${f}\n${lines.map((l) => `+${l}`).join('\n')}\n`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
45
|
/** Literal pathspecs: a `[` in a name is no glob that takes its siblings along. */
|
|
57
46
|
const lits = (projectDir, files) => files.map((f) => literal(projectDir, f));
|
|
58
47
|
|
|
59
48
|
// The form a board cuts the diff by, whatever the user's git config says (diff.noprefix,
|
|
60
49
|
// diff.mnemonicPrefix, color.ui=always, an external diff): heads `a/<path> b/<path>` from the
|
|
61
50
|
// project, as dirtyGcf names the paths, not from the repository a project may sit in.
|
|
62
|
-
const DIFF_FORM = [
|
|
51
|
+
export const DIFF_FORM = [
|
|
63
52
|
'--no-color',
|
|
64
53
|
'--no-ext-diff',
|
|
65
54
|
'--no-textconv',
|
package/lib/gate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The GCF checks of the pre-commit gate (GCF-PRD §13.1) over the working tree, cheap ones first.
|
|
2
2
|
// The gate refuses on the first problem; `gcf verify` reports them all as the GCF part of the DoD.
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import { listSystems, legacyCards, readConfig } from './gcf-paths.js';
|
|
5
6
|
import { phasesOf } from './phase.js';
|
|
@@ -57,6 +58,14 @@ async function phaseProblems(root, phase) {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
/** Every problem the gate refuses a commit for, one line each, and the ledger it read. */
|
|
61
|
+
/** A line of a doc's body as its file numbers it: past the frontmatter the body is read without. */
|
|
62
|
+
async function fileLine(doc, line) {
|
|
63
|
+
const text = await readFile(doc.file, 'utf8');
|
|
64
|
+
const at = text.indexOf(doc.body);
|
|
65
|
+
|
|
66
|
+
return at < 0 ? line : text.slice(0, at).split('\n').length - 1 + line;
|
|
67
|
+
}
|
|
68
|
+
|
|
60
69
|
export async function gateProblems(projectDir) {
|
|
61
70
|
const root = path.join(path.resolve(projectDir), 'gcf');
|
|
62
71
|
const ledger = await buildLedger(projectDir);
|
|
@@ -99,7 +108,10 @@ export async function gateProblems(projectDir) {
|
|
|
99
108
|
for (const s of systems) {
|
|
100
109
|
const { issues } = lint(s.body);
|
|
101
110
|
if (issues.length)
|
|
102
|
-
problems.push(
|
|
111
|
+
problems.push(
|
|
112
|
+
`gcf/systems/${s.name}:${await fileLine(s, issues[0].line)}: ` +
|
|
113
|
+
`${issues[0].rule}: ${issues[0].text}`,
|
|
114
|
+
);
|
|
103
115
|
}
|
|
104
116
|
|
|
105
117
|
return { problems, ledger };
|
package/lib/gcf-paths.js
CHANGED
|
@@ -110,6 +110,15 @@ function gatesOf(v = {}) {
|
|
|
110
110
|
);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
// Files listed and never opened on the board's Changes screen (Board-PRD §5.25): the project's
|
|
114
|
+
// call, GCF names no stack's files.
|
|
115
|
+
function noDiffOf(v = []) {
|
|
116
|
+
if (!Array.isArray(v) || !v.every((g) => typeof g === 'string' && g.trim()))
|
|
117
|
+
throw new Error('gcf/config.yml: no_diff must be a list of globs, one per entry');
|
|
118
|
+
|
|
119
|
+
return v;
|
|
120
|
+
}
|
|
121
|
+
|
|
113
122
|
/** @returns {Promise<Record<string, any>>} */
|
|
114
123
|
export async function readConfig(root) {
|
|
115
124
|
return configOf(await readRawConfig(root));
|
|
@@ -166,6 +175,7 @@ export function configOf(parsed) {
|
|
|
166
175
|
run_mode: runModeOf(config.run_mode),
|
|
167
176
|
chat: chatOf(config.chat),
|
|
168
177
|
gates: gatesOf(config.gates),
|
|
178
|
+
no_diff: noDiffOf(config.no_diff),
|
|
169
179
|
};
|
|
170
180
|
}
|
|
171
181
|
|
package/lib/git.js
CHANGED
|
@@ -237,6 +237,23 @@ export function moveTrailers({ key, items = [], proposal } = {}) {
|
|
|
237
237
|
return ['-m', lines.join('\n')];
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Why a commit did not land. The gate's refusal in the words of whoever made the move, with no
|
|
242
|
+
* git around it; anything else as git said it, past its line-ending warnings, which stop nothing.
|
|
243
|
+
*/
|
|
244
|
+
function commitRefusal(message, out) {
|
|
245
|
+
const problems = [...out.matchAll(/^gcf pre-commit: (?!warning: )(.+?)\r?$/gm)].map((m) => m[1]);
|
|
246
|
+
if (!problems.length)
|
|
247
|
+
return new Error(`commit failed: ${out.replace(/^warning: .*\r?\n?/gm, '')}`);
|
|
248
|
+
|
|
249
|
+
return refusal(
|
|
250
|
+
'gate',
|
|
251
|
+
{ problems },
|
|
252
|
+
`cannot record ${message}: the gate refuses ${problems.join('; ')}\n` +
|
|
253
|
+
'Nothing was committed. What it names has to change first (the agent can do it), then try again.',
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
240
257
|
/**
|
|
241
258
|
* Stage and commit exactly `paths`: `git add -- gcf/` would stage a user's unrelated edits
|
|
242
259
|
* under gcf/, and a rollback only restores what it snapshotted. A path that neither exists nor
|
|
@@ -284,7 +301,7 @@ export function commitPaths(projectDir, message, paths, move = {}) {
|
|
|
284
301
|
'commit or discard them, then try again',
|
|
285
302
|
);
|
|
286
303
|
}
|
|
287
|
-
if (res.status !== 0) throw
|
|
304
|
+
if (res.status !== 0) throw commitRefusal(message, out);
|
|
288
305
|
|
|
289
306
|
return spawnSync('git', ['rev-parse', 'HEAD'], {
|
|
290
307
|
cwd: projectDir,
|
package/lib/linter.js
CHANGED
|
@@ -10,7 +10,9 @@ const UNIT_WORDS = [...UNITS, 'мс', 'с', 'сек', 'dmg', 'hp', 'px'].map(esc
|
|
|
10
10
|
const UNIT_NUM = new RegExp(`\\d+(?:[.,]\\d+)?\\s?(?:${UNIT_WORDS})(?![\\p{L}\\p{N}_-])`, 'u');
|
|
11
11
|
// No space before the paren: "respawn the player (out of the arena)" is prose, not a call.
|
|
12
12
|
const SIGNATURE = /\w+\([^)]*\b(?:int|float|bool|string|void|var|ref|out)\b[^)]*\)/;
|
|
13
|
-
|
|
13
|
+
// A bullet headed by a CamelCase name: "HealthComponent holds the pool". One that only mentions a
|
|
14
|
+
// name is a sentence, and a path or a file name is what `code:` is there for.
|
|
15
|
+
const MEMBER_BULLET = /^- `?[A-Z][a-z0-9]+(?:[A-Z][a-z0-9]+)+\b(?!\.[a-z0-9]+\b|\/)/;
|
|
14
16
|
const ENVELOPE_FIELD = /^\s*(?:- )?(?:question|assumed|proposed):/;
|
|
15
17
|
|
|
16
18
|
/**
|
|
@@ -84,7 +86,7 @@ function toggleFence(st, i) {
|
|
|
84
86
|
st.fenceStart = -1;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
|
-
const memberBullet = (line) =>
|
|
89
|
+
const memberBullet = (line) => MEMBER_BULLET.test(line);
|
|
88
90
|
|
|
89
91
|
/** Pattern rules for one prose line (outside fences, not pragma-allowed). */
|
|
90
92
|
function checkLine(st, i) {
|
|
@@ -103,7 +105,7 @@ function checkLine(st, i) {
|
|
|
103
105
|
|
|
104
106
|
if (SIGNATURE.test(line)) flag('signature');
|
|
105
107
|
|
|
106
|
-
// Three or more consecutive bullets each
|
|
108
|
+
// Three or more consecutive bullets each headed by a CamelCase identifier = a member list.
|
|
107
109
|
const prev = st.lines[i - 1] ?? '';
|
|
108
110
|
if (memberBullet(line) && memberBullet(prev) && memberBullet(st.lines[i - 2] ?? '')) {
|
|
109
111
|
flag('camelcase-member-list');
|
package/lib/tree-file.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Reading a file of a checkout for a view (`gcf edits diff`, `gcf changes`): only a regular file
|
|
2
|
+
// that lies inside the tree. A link named `x.png` would read a file outside the project.
|
|
3
|
+
import {
|
|
4
|
+
closeSync,
|
|
5
|
+
constants,
|
|
6
|
+
existsSync,
|
|
7
|
+
fstatSync,
|
|
8
|
+
lstatSync,
|
|
9
|
+
openSync,
|
|
10
|
+
readSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
} from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
|
|
15
|
+
const listed = (why) => ({ kind: 'listed', why });
|
|
16
|
+
|
|
17
|
+
/** Whether `file` really lies under `root`: git walks through a Windows junction as a folder. */
|
|
18
|
+
function inside(root, file) {
|
|
19
|
+
try {
|
|
20
|
+
const rel = path.relative(realpathSync.native(root), realpathSync.native(file));
|
|
21
|
+
|
|
22
|
+
return rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What `rel` (from `root`, `/` allowed at its end: git names an untracked nested repository so)
|
|
30
|
+
* is on disk, with no byte read: `{kind: 'file', size}`, `{kind: 'gone'}`, or `{kind: 'listed',
|
|
31
|
+
* why}` for what is never opened.
|
|
32
|
+
* @param {string} root
|
|
33
|
+
* @param {string} rel
|
|
34
|
+
*/
|
|
35
|
+
export function fileState(root, rel) {
|
|
36
|
+
const file = path.join(root, rel.replace(/\/$/, ''));
|
|
37
|
+
let st;
|
|
38
|
+
try {
|
|
39
|
+
st = lstatSync(file);
|
|
40
|
+
} catch {
|
|
41
|
+
return { kind: 'gone' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (st.isSymbolicLink()) return listed('link');
|
|
45
|
+
if (st.isDirectory())
|
|
46
|
+
return listed(existsSync(path.join(file, '.git')) ? 'nested repository' : 'not a regular file');
|
|
47
|
+
if (!st.isFile()) return listed('not a regular file');
|
|
48
|
+
// The file itself is regular, a folder on its way may be a link.
|
|
49
|
+
if (!inside(root, file)) return listed('link');
|
|
50
|
+
|
|
51
|
+
return { kind: 'file', size: st.size };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The first `max` bytes of `rel` when it is a regular file inside `root`: `{kind: 'file', size,
|
|
56
|
+
* bytes}`, or what fileState says. Windows has no O_NOFOLLOW: there a file swapped for a link
|
|
57
|
+
* between the check and the open is read; this reads the operator's own disk for the operator.
|
|
58
|
+
* @param {string} root
|
|
59
|
+
* @param {string} rel
|
|
60
|
+
* @param {number} max
|
|
61
|
+
*/
|
|
62
|
+
export function readHead(root, rel, max) {
|
|
63
|
+
const state = fileState(root, rel);
|
|
64
|
+
if (state.kind !== 'file') return state;
|
|
65
|
+
try {
|
|
66
|
+
const fd = openSync(path.join(root, rel), constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
67
|
+
try {
|
|
68
|
+
const st = fstatSync(fd);
|
|
69
|
+
if (!st.isFile()) return listed('not a regular file');
|
|
70
|
+
const bytes = Buffer.alloc(Math.min(st.size, max));
|
|
71
|
+
const n = readSync(fd, bytes, 0, bytes.length, 0);
|
|
72
|
+
|
|
73
|
+
return { kind: 'file', size: st.size, bytes: bytes.subarray(0, n) };
|
|
74
|
+
} finally {
|
|
75
|
+
closeSync(fd);
|
|
76
|
+
}
|
|
77
|
+
} catch (e) {
|
|
78
|
+
// ELOOP: it became a link just now. Else the editor holds it, or the rights say no.
|
|
79
|
+
return listed(e.code === 'ELOOP' ? 'link' : 'not readable');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// A new file the edits dialog shows whole: past this it is cut, never read into memory.
|
|
84
|
+
const NEW_FILE_MAX = 16 * 1024 * 1024;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A file git does not know yet as a diff of its own: each line added.
|
|
88
|
+
* @param {string} root
|
|
89
|
+
* @param {string} rel
|
|
90
|
+
* @param {number} [max]
|
|
91
|
+
*/
|
|
92
|
+
export function newFileDiff(root, rel, max = NEW_FILE_MAX) {
|
|
93
|
+
const head = readHead(root, rel, max);
|
|
94
|
+
if (head.kind === 'gone') return '';
|
|
95
|
+
if (head.kind === 'listed') return `new file ${rel}: ${head.why}\n`;
|
|
96
|
+
if (head.bytes.includes(0)) return `new file ${rel}: binary\n`;
|
|
97
|
+
const whole = head.size <= head.bytes.length;
|
|
98
|
+
const text = head.bytes.toString('utf8');
|
|
99
|
+
// Cut: the last line may have lost its end, a letter's bytes too.
|
|
100
|
+
const kept = whole ? text : text.slice(0, Math.max(0, text.lastIndexOf('\n')));
|
|
101
|
+
// An empty file has no line to add.
|
|
102
|
+
const lines = kept ? kept.replace(/\n$/, '').split('\n') : [];
|
|
103
|
+
const end = whole ? '' : `… first ${head.bytes.length} of ${head.size} bytes\n`;
|
|
104
|
+
const added = lines.map((l) => `+${l}\n`).join('');
|
|
105
|
+
|
|
106
|
+
return `new file ${rel}\n--- /dev/null\n+++ b/${rel}\n${added}${end}`;
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -52,6 +52,8 @@ gcf_work:
|
|
|
52
52
|
- in the project chat (the gcf-chat skill) a new item is proposed with the `propose_item_new` tool of the `gcf` MCP server, not made with `item new`: the human's click makes it; so are a block, a drop, a dependency change, an unblock, a verdict, a decision, an answer to an open question and the inbox's add, category, Later and erase, with their `propose_` tools and the `id` they act on; a new item is proposed by its title, never an id
|
|
53
53
|
- a verdict is proposed only after the human said in the talk that they played the item and how it felt, its note in the human's words; the `remove` of a kill, a no or a wontfix is left to the rule unless the human says otherwise
|
|
54
54
|
- a move refused for hand edits under `gcf/` ends its error with `gcf-refusal: {"code":"dirty","paths":[…]}`: `ai-factory gcf edits diff --path <path>` shows them; `gcf edits commit` and `gcf edits discard` are the human's moves, never the agent's
|
|
55
|
+
- a move the commit gate refuses ends its error with `gcf-refusal: {"code":"gate","problems":[…]}`: nothing was committed; fix what it names, then make the move again
|
|
56
|
+
- `ai-factory gcf changes [--worktree <id>]` lists what is uncommitted in the checkout or an item's worktree, `gcf changes diff --path <path>` shows one text file's diff; `no_diff` in `gcf/config.yml` is the project's list of files shown without a diff
|
|
55
57
|
- a chat thread bound to an item reads the item's file, its plan, its run's log and run chat first (the paths its context names), answers the question, and only then proposes a move
|
|
56
58
|
|
|
57
59
|
gcf_forbidden:
|
package/templates/config.yml
CHANGED
|
@@ -33,6 +33,10 @@ auto_compact_pct: 0
|
|
|
33
33
|
# template: ../my-template
|
|
34
34
|
# questions:
|
|
35
35
|
# - which step of this item took the longest, and why?
|
|
36
|
+
# Files the board's Changes screen lists and never opens: generated text nobody reads as a diff.
|
|
37
|
+
# GCF names no stack's files, so the list is yours; a `*` stays in one folder. Uncomment to set:
|
|
38
|
+
# no_diff:
|
|
39
|
+
# - "**/*.prefab"
|
|
36
40
|
# What agent runs may not do lives in an `agent:` section that `ai-factory gcf permissions`
|
|
37
41
|
# and the board write; without it the default deny groups hold.
|
|
38
42
|
# The project's own commands for the auto DoD (Methodology §6); the engine does not matter.
|