slicekit 0.7.0 → 0.7.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/package.json +1 -1
- package/src/build/pr.js +12 -8
- package/src/build/record.js +5 -12
- package/src/check/ci-event.js +17 -2
- package/src/check/rules/layout.js +10 -1
- package/src/check/rules/queue.js +12 -2
- package/src/cli/status.js +30 -3
- package/src/init.js +21 -0
- package/src/paths.js +1 -0
- package/src/state/build.js +115 -7
- package/src/state/covers.js +85 -5
package/package.json
CHANGED
package/src/build/pr.js
CHANGED
|
@@ -60,8 +60,8 @@ function readNotes(root, notes) {
|
|
|
60
60
|
|
|
61
61
|
const lines = (out) => out.split('\n').filter((l) => l !== '');
|
|
62
62
|
|
|
63
|
-
// The worktree is clean, or holds only the state file
|
|
64
|
-
//
|
|
63
|
+
// The worktree is clean, or holds only the state file of a draft-pr recording whose bookkeeping
|
|
64
|
+
// commit did not land: that commit is made now. A slice branch commits no BUILD.md (D-100).
|
|
65
65
|
function cleanOrFinish(wt, cfg, id, current) {
|
|
66
66
|
// Paths from name lists, never from status columns: git() trims its output, and a leading space
|
|
67
67
|
// is part of the first porcelain line.
|
|
@@ -73,10 +73,9 @@ function cleanOrFinish(wt, cfg, id, current) {
|
|
|
73
73
|
if (files.length === 0) return;
|
|
74
74
|
const paths = kitPaths(wt, cfg);
|
|
75
75
|
const stateRel = posixRel(wt, path.join(paths.state, stateFileName(id)));
|
|
76
|
-
const
|
|
77
|
-
const onlyBookkeeping = files.includes(stateRel) && files.every((f) => f === stateRel || f === buildRel);
|
|
76
|
+
const onlyBookkeeping = files.length === 1 && files[0] === stateRel;
|
|
78
77
|
if (current.state === 'draft-pr' && onlyBookkeeping && pendingDraftPr(wt, stateRel, current)) {
|
|
79
|
-
commitBookkeeping(wt, id, 'draft-pr',
|
|
78
|
+
commitBookkeeping(wt, id, 'draft-pr', [stateRel]);
|
|
80
79
|
return;
|
|
81
80
|
}
|
|
82
81
|
throw new BuildError(`the worktree has uncommitted changes (${files.slice(0, 5).join(', ')}); build commit them first`);
|
|
@@ -158,9 +157,12 @@ export function openSlicePr(root, cfg, host, id, { notes, worktree, env = proces
|
|
|
158
157
|
// here, before the push, rather than after it.
|
|
159
158
|
const afterReview = current.state !== 'building' && current.state !== 'draft-pr';
|
|
160
159
|
// A head that adds only bookkeeping (a verdict, the state, BUILD.md) to the reviewed one is the same work.
|
|
161
|
-
if (afterReview && current.pr && headCovers(wt, current.pr.headSha, headSha(wt))) {
|
|
160
|
+
if (afterReview && current.pr && headCovers(wt, current.pr.headSha, headSha(wt), { merges: false })) {
|
|
162
161
|
throw new BuildError(`${id} has nothing new since review of ${current.pr.headSha.slice(0, 7)}; push a fix to return to draft-pr`);
|
|
163
162
|
}
|
|
163
|
+
// A head that adds to the reviewed one a merge of the default branch leaving the pull request's
|
|
164
|
+
// files as they were (D-100): the verdict still holds, so the head is pushed and the state kept.
|
|
165
|
+
const keep = afterReview && current.pr && headCovers(wt, current.pr.headSha, headSha(wt)) ? current.state : null;
|
|
164
166
|
// A path another slice of the same Order step already changes on origin (D-06): a plan defect,
|
|
165
167
|
// refused before the notes, the body and the commands, so a defect in the plan stops the run before
|
|
166
168
|
// minutes of test runs, and before anything is pushed. After the behind-origin refusal, which a
|
|
@@ -240,10 +242,12 @@ export function openSlicePr(root, cfg, host, id, { notes, worktree, env = proces
|
|
|
240
242
|
const made = openOrUpdate(wt, cfg, host, { title, body, draft: true });
|
|
241
243
|
// The absolute session-root path the next in-loop reviewer writes (null once the loop's rounds are
|
|
242
244
|
// used or an independent verdict is recorded): recording draft-pr adds no verdict, so the count read before is the count after.
|
|
245
|
+
// A kept state asks for no new review: no verdict path.
|
|
246
|
+
if (keep) return { number: made.number, url: made.url, created: made.created, verdictPath: null, local: local ? testCommand : null, movedHead: null, kept: keep };
|
|
243
247
|
const verdictPath = nextVerdictPath(root, id, current.verdicts ?? []);
|
|
244
248
|
// The reviewer writes there at once: its directory is made here, as the path is printed.
|
|
245
249
|
if (verdictPath) fs.mkdirSync(path.dirname(verdictPath), { recursive: true });
|
|
246
|
-
const result = { number: made.number, url: made.url, created: made.created, verdictPath, local: local ? testCommand : null, movedHead: null };
|
|
250
|
+
const result = { number: made.number, url: made.url, created: made.created, verdictPath, local: local ? testCommand : null, movedHead: null, kept: null };
|
|
247
251
|
// `build finish` marked it ready; a slice back at draft-pr has a draft pull request again.
|
|
248
252
|
if (livePr?.draft === false) host.markDraft(made.number);
|
|
249
253
|
// At draft-pr a head that moved since the recorded one (a merge of the default branch, any commit)
|
|
@@ -251,7 +255,7 @@ export function openSlicePr(root, cfg, host, id, { notes, worktree, env = proces
|
|
|
251
255
|
// that adds only bookkeeping to the recorded one is the same work: nothing is recorded.
|
|
252
256
|
if (current.state === 'draft-pr') {
|
|
253
257
|
const head = headSha(wt);
|
|
254
|
-
if (current.pr?.headSha && !headCovers(wt, current.pr.headSha, head)) {
|
|
258
|
+
if (current.pr?.headSha && !headCovers(wt, current.pr.headSha, head, { merges: false })) {
|
|
255
259
|
recordHead(wt, cfg, id, { number: made.number, url: made.url, headSha: head });
|
|
256
260
|
commitState(wt, cfg, id, 'head');
|
|
257
261
|
pushBranch(wt, { protectedBranches: cfg.protectedBranches, defaultBranch: cfg.defaultBranch });
|
package/src/build/record.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
// State moves of a `build` step (REQ-1.8, D-35): setState
|
|
2
|
-
// touches nothing else, so a verdict given on the head before it still holds.
|
|
1
|
+
// State moves of a `build` step (REQ-1.8, D-35): setState and one bookkeeping commit of the slice's
|
|
2
|
+
// state file that touches nothing else, so a verdict given on the head before it still holds. A slice
|
|
3
|
+
// branch commits no BUILD.md: the sync after the merge regenerates it on the default branch (D-100).
|
|
3
4
|
import path from 'node:path';
|
|
4
|
-
import { exists, readText, writeText } from '../fs.js';
|
|
5
5
|
import { commitPaths } from '../git.js';
|
|
6
6
|
import { stateFileName } from '../ids.js';
|
|
7
7
|
import { kitPaths } from '../paths.js';
|
|
8
|
-
import { renderBuild } from '../state/build.js';
|
|
9
8
|
import { appendHistory, setState } from '../state/index.js';
|
|
10
9
|
|
|
11
10
|
const posixRel = (wt, abs) => path.relative(wt, abs).split(path.sep).join('/');
|
|
@@ -31,14 +30,8 @@ export function recordAmendment(wt, cfg, id, evidence) {
|
|
|
31
30
|
return entry;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
//
|
|
35
|
-
// and BUILD.md: `chore(slicekit): <id> <what>`.
|
|
33
|
+
// One bookkeeping commit of the slice's state file alone: `chore(slicekit): <id> <what>`.
|
|
36
34
|
export function commitState(wt, cfg, id, what) {
|
|
37
35
|
const paths = kitPaths(wt, cfg);
|
|
38
|
-
|
|
39
|
-
const after = renderBuild(wt, cfg, before);
|
|
40
|
-
if (after !== before) writeText(paths.build, after);
|
|
41
|
-
const files = [posixRel(wt, path.join(paths.state, stateFileName(id)))];
|
|
42
|
-
if (exists(paths.build)) files.push(posixRel(wt, paths.build));
|
|
43
|
-
return commitBookkeeping(wt, id, what, files);
|
|
36
|
+
return commitBookkeeping(wt, id, what, [posixRel(wt, path.join(paths.state, stateFileName(id)))]);
|
|
44
37
|
}
|
package/src/check/ci-event.js
CHANGED
|
@@ -92,11 +92,26 @@ export const SYNC_BRANCH = /^sync\/[0-9a-f]{7,40}$/;
|
|
|
92
92
|
// merge commit's second parent when the event has none). Otherwise the current branch and HEAD.
|
|
93
93
|
export function branchTip(ctx) {
|
|
94
94
|
if (!ctx.git?.isRepo || !ctx.git.hasCommits) return null;
|
|
95
|
-
const ref = ctx.env
|
|
96
|
-
if (ctx.ci &&
|
|
95
|
+
const ref = headRef(ctx.env);
|
|
96
|
+
if (ctx.ci && ref) {
|
|
97
97
|
const headSha = readCiEvent(ctx.env)?.headSha;
|
|
98
98
|
const tip = (headSha && /^[0-9a-f]{7,40}$/.test(headSha) && resolveCommit(ctx.root, headSha)) || resolveCommit(ctx.root, 'HEAD^2') || 'HEAD';
|
|
99
99
|
return { branch: ref, tip };
|
|
100
100
|
}
|
|
101
101
|
return ctx.git.branch ? { branch: ctx.git.branch, tip: 'HEAD' } : null;
|
|
102
102
|
}
|
|
103
|
+
|
|
104
|
+
// The pull request's head branch GitHub Actions names in GITHUB_HEAD_REF, or null when unset or empty.
|
|
105
|
+
function headRef(env) {
|
|
106
|
+
const ref = env?.GITHUB_HEAD_REF;
|
|
107
|
+
return typeof ref === 'string' && ref !== '' ? ref : null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// The name of the branch judged: branchTip's, else HEAD's branch name (a branch with no commit yet).
|
|
111
|
+
// With a detached HEAD, without --ci too, it is the pull request's head branch when CI names it: the
|
|
112
|
+
// test job checks out the pull request's merge commit detached (P8/S-58, review B-4).
|
|
113
|
+
export function judgedBranch(ctx) {
|
|
114
|
+
const branch = branchTip(ctx)?.branch ?? ctx.git?.branch ?? null;
|
|
115
|
+
if (branch) return branch;
|
|
116
|
+
return ctx.git?.isRepo ? headRef(ctx.env) : null;
|
|
117
|
+
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
// layout (03 REQ-3.7, 01 REQ-1.1, REQ-1.10): every path slicekit init lays down must exist, as the
|
|
2
2
|
// kind it was laid down as; and no file under .claude/ may be one the manifest ships only for
|
|
3
3
|
// another shape. A file under .claude/ that no manifest entry names is the team's and never a
|
|
4
|
-
// finding.
|
|
4
|
+
// finding. `.gitattributes` carries the BUILD.md merge rule init writes (REQ-1.6, D-100). With no
|
|
5
|
+
// .slicekit/config.json the rule is silent: there is no install to judge.
|
|
5
6
|
import fs from 'node:fs';
|
|
6
7
|
import path from 'node:path';
|
|
7
8
|
import { listFiles } from '../../fs.js';
|
|
9
|
+
import { lines } from '../../markdown.js';
|
|
8
10
|
import { loadManifest } from '../../render/manifest.js';
|
|
11
|
+
import { BUILD_MERGE_RULE } from '../../state/build.js';
|
|
9
12
|
|
|
10
13
|
export const id = 'layout';
|
|
11
14
|
export const blocking = true;
|
|
@@ -39,6 +42,12 @@ export function run(ctx) {
|
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
const squash = (line) => line.trim().replace(/\s+/g, ' ').replace(/^\//, '');
|
|
46
|
+
const attributes = kindOf(paths.gitattributes) === 'file' ? lines(fs.readFileSync(paths.gitattributes, 'utf8')).map(squash) : [];
|
|
47
|
+
if (!attributes.includes(BUILD_MERGE_RULE)) {
|
|
48
|
+
out.push({ file: rel(paths.gitattributes), line: 1, message: `${rel(paths.gitattributes)} lacks the line ${BUILD_MERGE_RULE}; run slicekit init` });
|
|
49
|
+
}
|
|
50
|
+
|
|
42
51
|
if (kindOf(paths.claude) === 'dir') {
|
|
43
52
|
const ours = new Set();
|
|
44
53
|
const foreign = new Set();
|
package/src/check/rules/queue.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
// queue (01 REQ-1.6, 03 REQ-3.7): BUILD.md is a view of the slice files and their state, so its
|
|
2
2
|
// generated region must read exactly as `slicekit status --write` would write it. The text below
|
|
3
|
-
// the marker is the team's and is never compared.
|
|
3
|
+
// the marker is the team's and is never compared. On a slice branch the rule is silent: the branch
|
|
4
|
+
// commits no BUILD.md of its own, and the sync after its merge regenerates it (D-100).
|
|
4
5
|
import fs from 'node:fs';
|
|
5
6
|
import path from 'node:path';
|
|
6
7
|
import { readText } from '../../fs.js';
|
|
7
8
|
import { lines } from '../../markdown.js';
|
|
8
9
|
import { findSliceFiles } from '../../slicefile.js';
|
|
9
10
|
import { generatedRegion, renderBuild, MARKER } from '../../state/build.js';
|
|
11
|
+
import { sliceFromBranch } from '../../ids.js';
|
|
12
|
+
import { judgedBranch } from '../ci-event.js';
|
|
10
13
|
|
|
11
14
|
export const id = 'queue';
|
|
12
15
|
export const blocking = true;
|
|
@@ -56,8 +59,15 @@ function unplanned(ctx) {
|
|
|
56
59
|
return states.length === 0 && findSliceFiles(ctx.root, ctx.cfg).length === 0;
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
// The branch judged is a slice branch: HEAD's, or with --ci or a detached HEAD the pull request's
|
|
63
|
+
// head branch CI names (judgedBranch).
|
|
64
|
+
function onSliceBranch(ctx) {
|
|
65
|
+
const branch = judgedBranch(ctx);
|
|
66
|
+
return Boolean(branch && sliceFromBranch(branch));
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
export function run(ctx) {
|
|
60
|
-
if (unplanned(ctx)) return [];
|
|
70
|
+
if (unplanned(ctx) || onSliceBranch(ctx)) return [];
|
|
61
71
|
const file = toPosix(path.relative(ctx.root, ctx.paths.build));
|
|
62
72
|
const at = (line, message) => ({ file, line, message });
|
|
63
73
|
if (!fs.existsSync(ctx.paths.build)) return [at(0, `${file} is missing; run slicekit status --write`)];
|
package/src/cli/status.js
CHANGED
|
@@ -1,24 +1,49 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
1
2
|
import { parseArgs } from 'node:util';
|
|
2
3
|
import { ConfigError, loadConfig } from '../config.js';
|
|
3
4
|
import { exists, readText, writeText } from '../fs.js';
|
|
4
5
|
import { kitPaths } from '../paths.js';
|
|
5
6
|
import { SliceFileError, findSliceFiles, parseSliceFile } from '../slicefile.js';
|
|
6
7
|
import { programStage } from '../stage.js';
|
|
7
|
-
import { renderBuild } from '../state/build.js';
|
|
8
|
+
import { mergeBuild, renderBuild } from '../state/build.js';
|
|
8
9
|
import { LockError, readSlice } from '../state/index.js';
|
|
9
10
|
import { syncFromHost } from '../state/sync.js';
|
|
10
11
|
import { ReleaseError, applyRelease, onDefaultTip } from '../release-version.js';
|
|
11
12
|
import { getHost } from '../adapters/githost/index.js';
|
|
12
13
|
import { GitHostError } from '../adapters/githost/errors.js';
|
|
13
14
|
|
|
14
|
-
const USAGE = 'status [--json] [--write] [--sync]';
|
|
15
|
+
const USAGE = 'status [--json] [--write] [--sync] | status --merge-driver <ancestor> <current> <other>';
|
|
15
16
|
|
|
16
17
|
const OPTIONS = {
|
|
17
18
|
json: { type: 'boolean' },
|
|
18
19
|
write: { type: 'boolean' },
|
|
19
20
|
sync: { type: 'boolean' },
|
|
21
|
+
'merge-driver': { type: 'boolean' },
|
|
20
22
|
};
|
|
21
23
|
|
|
24
|
+
// The merge driver git runs for BUILD.md on a local merge (REQ-1.6, D-100): BUILD.md rendered from
|
|
25
|
+
// the merged state files is written to <current>, the path git gave as %A. Exit 0 is a clean merge;
|
|
26
|
+
// any other exit leaves BUILD.md conflicted.
|
|
27
|
+
function mergeDriver(root, files, io) {
|
|
28
|
+
if (files.length !== 3) {
|
|
29
|
+
io.stderr(`slicekit: --merge-driver takes three paths, %O %A %B\nusage: slicekit ${USAGE}\n`);
|
|
30
|
+
return 2;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const cfg = loadConfig(root);
|
|
34
|
+
const read = (f) => {
|
|
35
|
+
const abs = path.resolve(root, f);
|
|
36
|
+
return exists(abs) ? readText(abs) : '';
|
|
37
|
+
};
|
|
38
|
+
const [ancestor, current, other] = files.map(read);
|
|
39
|
+
writeText(path.resolve(root, files[1]), mergeBuild(root, cfg, { ancestor, current, other, env: io.env ?? process.env }));
|
|
40
|
+
return 0;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
io.stderr(`slicekit: BUILD.md merge driver: ${e.message}\n`);
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
22
47
|
function slices(root, cfg) {
|
|
23
48
|
return findSliceFiles(root, cfg).map((f) => {
|
|
24
49
|
const slice = parseSliceFile(readText(f.path), f.path);
|
|
@@ -38,13 +63,15 @@ const expected = (e) =>
|
|
|
38
63
|
// Without --json, changes go to stdout and warnings and errors to stderr.
|
|
39
64
|
async function run(args, io) {
|
|
40
65
|
let values;
|
|
66
|
+
let positionals;
|
|
41
67
|
try {
|
|
42
|
-
({ values } = parseArgs({ args, options: OPTIONS, allowPositionals:
|
|
68
|
+
({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: args.includes('--merge-driver') }));
|
|
43
69
|
} catch (e) {
|
|
44
70
|
io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
|
|
45
71
|
return 2;
|
|
46
72
|
}
|
|
47
73
|
const root = io.cwd;
|
|
74
|
+
if (values['merge-driver']) return mergeDriver(root, positionals, io);
|
|
48
75
|
const warnings = [];
|
|
49
76
|
const synced = [];
|
|
50
77
|
let version = null;
|
package/src/init.js
CHANGED
|
@@ -7,6 +7,7 @@ import { currentBranch, detectDefaultBranch, git, GitError, hasCommits, isRepo }
|
|
|
7
7
|
import { GITHOOKS, HOOKS_PATH, hooksPathValue, isInstalledHooksPath } from './githooks.js';
|
|
8
8
|
import { probeCli } from './hook-cli.js';
|
|
9
9
|
import { kitPaths } from './paths.js';
|
|
10
|
+
import { BUILD_MERGE_DRIVER, BUILD_MERGE_RULE, mergeDriverCommand } from './state/build.js';
|
|
10
11
|
import { kitVersion, renderAll } from './render/index.js';
|
|
11
12
|
import { readSettings } from './render/settings.js';
|
|
12
13
|
import { WORKFLOW_DRIFT, WORKFLOW_PATH, workflowState, workflowSteps } from './workflow.js';
|
|
@@ -122,8 +123,27 @@ function attributeRules(root, cfg) {
|
|
|
122
123
|
return [
|
|
123
124
|
{ rule: '.githooks/* text eol=lf', forms: new Set(['.githooks/* text eol=lf', '/.githooks/* text eol=lf']) },
|
|
124
125
|
{ rule: intake, forms: new Set([intake, `/${intake}`]) },
|
|
126
|
+
{ rule: BUILD_MERGE_RULE, forms: new Set([BUILD_MERGE_RULE, `/${BUILD_MERGE_RULE}`]) },
|
|
125
127
|
];
|
|
126
128
|
}
|
|
129
|
+
|
|
130
|
+
// The merge driver the BUILD.md rule names, set in this clone's git config (REQ-1.6, D-100): a local
|
|
131
|
+
// merge regenerates BUILD.md from the merged state files. Set once; a second init leaves one entry.
|
|
132
|
+
function setMergeDriver(root, cfg) {
|
|
133
|
+
const want = mergeDriverCommand(cfg.cli);
|
|
134
|
+
const key = `merge.${BUILD_MERGE_DRIVER}`;
|
|
135
|
+
let have = [];
|
|
136
|
+
try {
|
|
137
|
+
have = git(root, ['config', '--local', '--get-all', `${key}.driver`]).split('\n').filter(Boolean);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
if (!(e instanceof GitError)) throw e;
|
|
140
|
+
}
|
|
141
|
+
if (have.length !== 1 || have[0] !== want) {
|
|
142
|
+
if (have.length > 0) git(root, ['config', '--local', '--unset-all', `${key}.driver`]);
|
|
143
|
+
git(root, ['config', '--local', `${key}.driver`, want]);
|
|
144
|
+
}
|
|
145
|
+
git(root, ['config', '--local', `${key}.name`, 'slicekit: BUILD.md rendered from the merged state files']);
|
|
146
|
+
}
|
|
127
147
|
const NOT_A_REPO = 'not a git repository';
|
|
128
148
|
const UNBORN_PROTECTED = 'commit this scaffold with ALLOW_PROTECTED_COMMIT=1 git commit …, or start on a slice branch';
|
|
129
149
|
|
|
@@ -405,6 +425,7 @@ export function init(root, { name, shape, docsRoot, commands = {}, cli, defaultB
|
|
|
405
425
|
if (repo) {
|
|
406
426
|
stageHooks(root, hooks, warnings);
|
|
407
427
|
setHooksPath(root, warnings);
|
|
428
|
+
setMergeDriver(root, cfg);
|
|
408
429
|
}
|
|
409
430
|
// The CI workflow (REQ-3.11): written when absent; an existing one is the team's and stays as is.
|
|
410
431
|
const workflow = workflowState(root, cfg);
|
package/src/paths.js
CHANGED
package/src/state/build.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
1
2
|
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
2
4
|
import path from 'node:path';
|
|
3
5
|
import { fileURLToPath } from 'node:url';
|
|
4
6
|
import { readText } from '../fs.js';
|
|
7
|
+
import { git, GitError } from '../git.js';
|
|
5
8
|
import { headings, lines } from '../markdown.js';
|
|
6
9
|
import { kitPaths } from '../paths.js';
|
|
7
10
|
import { findSliceFiles, parsePhaseFile, parseSliceFile, phaseOfDir } from '../slicefile.js';
|
|
8
11
|
import { readSlice } from './index.js';
|
|
9
12
|
|
|
10
13
|
export const MARKER = '<!-- slicekit:generated:end -->';
|
|
11
|
-
|
|
14
|
+
|
|
15
|
+
// The merge driver of REQ-1.6 (D-100): `.gitattributes` names it for BUILD.md, and init sets its
|
|
16
|
+
// command in the clone's git config. git runs the command with %O %A %B on a local merge.
|
|
17
|
+
export const BUILD_MERGE_DRIVER = 'slicekit-build';
|
|
18
|
+
export const BUILD_MERGE_RULE = `BUILD.md merge=${BUILD_MERGE_DRIVER}`;
|
|
19
|
+
export const mergeDriverCommand = (cli) => `${cli} status --merge-driver %O %A %B`;
|
|
12
20
|
|
|
13
21
|
const TEMPLATE = fileURLToPath(new URL('../../templates/docs/BUILD.md', import.meta.url));
|
|
14
22
|
const TITLE = '# BUILD — the queue';
|
|
@@ -24,6 +32,7 @@ export function statusCell(state) {
|
|
|
24
32
|
return 'Todo';
|
|
25
33
|
}
|
|
26
34
|
|
|
35
|
+
const posixRel = (root, abs) => path.relative(root, abs).split(path.sep).join('/');
|
|
27
36
|
const cell = (s) => String(s).replace(/\s*\n\s*/g, ' ').replace(/(?<!\\)\|/g, '\\|');
|
|
28
37
|
|
|
29
38
|
// The text under a level-2 heading, up to the next level-2 heading, trimmed; null when absent.
|
|
@@ -131,19 +140,34 @@ export function renderBuild(root, cfg, existingText = '', { onWarning = () => {}
|
|
|
131
140
|
for (const f of sliceFiles) {
|
|
132
141
|
const slice = parseSliceFile(readText(f.path), f.path);
|
|
133
142
|
const list = slicesByPhase.get(f.phaseId) ?? [];
|
|
134
|
-
list.push({ slice, state: readSlice(root, cfg, slice.id) });
|
|
143
|
+
list.push({ slice, state: readSlice(root, cfg, slice.id), rel: posixRel(root, f.path) });
|
|
135
144
|
slicesByPhase.set(f.phaseId, list);
|
|
136
145
|
}
|
|
137
146
|
|
|
147
|
+
// A phase whose slices are all merged or released is one line under "Merged phases"; every other
|
|
148
|
+
// phase has its table, one row per slice with a link to its slice file and no Done when (D-105).
|
|
149
|
+
const merged = (phase) => {
|
|
150
|
+
const list = slicesByPhase.get(phase.id) ?? [];
|
|
151
|
+
return list.length > 0 && list.every(({ state }) => DONE.has(state.state));
|
|
152
|
+
};
|
|
153
|
+
const folded = found.filter(merged);
|
|
138
154
|
const out = [TITLE, '', preamble(cfg)];
|
|
155
|
+
if (folded.length > 0) {
|
|
156
|
+
out.push('', '## Merged phases', '');
|
|
157
|
+
for (const phase of folded) {
|
|
158
|
+
const count = slicesByPhase.get(phase.id).length;
|
|
159
|
+
const folder = posixRel(root, phase.dir);
|
|
160
|
+
out.push(`- Phase ${phase.n} — ${phase.name}: ${count} ${count === 1 ? 'slice' : 'slices'}, all merged; slice files in \`${folder}/\`.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
139
163
|
for (const phase of found) {
|
|
164
|
+
if (merged(phase)) continue;
|
|
140
165
|
out.push('', `## Phase ${phase.n} — ${phase.name}`, '');
|
|
141
|
-
out.push('| Slice | Name |
|
|
142
|
-
for (const { slice, state } of slicesByPhase.get(phase.id) ?? []) {
|
|
166
|
+
out.push('| Slice | Name | Group | Slice file | Status |', '|---|---|---|---|---|');
|
|
167
|
+
for (const { slice, state, rel } of slicesByPhase.get(phase.id) ?? []) {
|
|
143
168
|
const short = slice.id.split('/')[1];
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
out.push(`| ${short} | ${cell(slice.name)} | ${cell(done)} | ${cell(slice.group)} | ${statusCell(state)} |`);
|
|
169
|
+
const link = `[${cell(path.posix.basename(rel))}](${rel})`;
|
|
170
|
+
out.push(`| ${short} | ${cell(slice.name)} | ${cell(slice.group)} | ${link} | ${statusCell(state)} |`);
|
|
147
171
|
}
|
|
148
172
|
if (phase.order) out.push('', phase.order);
|
|
149
173
|
}
|
|
@@ -154,3 +178,87 @@ export function renderBuild(root, cfg, existingText = '', { onWarning = () => {}
|
|
|
154
178
|
const rest = tail === null ? migratedTail(existingText ?? '', generatedPhases) : prunedTail(tail, generatedPhases);
|
|
155
179
|
return `${generated}\n${MARKER}\n${rest}`;
|
|
156
180
|
}
|
|
181
|
+
|
|
182
|
+
// The other side of the merge git is running: `git merge` names each merged head in a
|
|
183
|
+
// GITHEAD_<sha> variable of the driver's environment. Null when there is not exactly one.
|
|
184
|
+
function otherHead(env) {
|
|
185
|
+
const heads = Object.keys(env)
|
|
186
|
+
.map((k) => /^GITHEAD_([0-9a-f]{40}|[0-9a-f]{64})$/i.exec(k)?.[1])
|
|
187
|
+
.filter(Boolean);
|
|
188
|
+
return heads.length === 1 ? heads[0] : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// path → blob id of every file under `dirs` in `commit`; an empty map for no commit.
|
|
192
|
+
function blobs(root, commit, dirs) {
|
|
193
|
+
const out = new Map();
|
|
194
|
+
if (!commit) return out;
|
|
195
|
+
const listed = git(root, ['ls-tree', '-r', '-z', '--full-tree', commit, '--', ...dirs]);
|
|
196
|
+
for (const entry of listed.split('\0').filter(Boolean)) {
|
|
197
|
+
const tab = entry.indexOf('\t');
|
|
198
|
+
const [, type, sha] = entry.slice(0, tab).split(' ');
|
|
199
|
+
if (type === 'blob') out.set(entry.slice(tab + 1), sha);
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The content of each blob id, read by one `git cat-file --batch`.
|
|
205
|
+
function readBlobs(root, shas) {
|
|
206
|
+
const out = new Map();
|
|
207
|
+
if (shas.length === 0) return out;
|
|
208
|
+
const raw = execFileSync('git', ['cat-file', '--batch'], { cwd: root, input: `${shas.join('\n')}\n`, maxBuffer: 1 << 28 });
|
|
209
|
+
let at = 0;
|
|
210
|
+
while (at < raw.length) {
|
|
211
|
+
const nl = raw.indexOf(10, at);
|
|
212
|
+
const [sha, , size] = raw.subarray(at, nl).toString('utf8').split(' ');
|
|
213
|
+
const start = nl + 1;
|
|
214
|
+
out.set(sha, raw.subarray(start, start + Number(size)));
|
|
215
|
+
at = start + Number(size) + 1;
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const tryGit = (root, args) => {
|
|
221
|
+
try {
|
|
222
|
+
return git(root, args);
|
|
223
|
+
} catch (e) {
|
|
224
|
+
if (e instanceof GitError) return null;
|
|
225
|
+
throw e;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// BUILD.md for a local merge (the driver of REQ-1.6, D-100): `ancestor`, `current` and `other` are
|
|
230
|
+
// the texts of %O, %A and %B. The slice files and state files are merged path by path, the way git
|
|
231
|
+
// merges them (a side that left a file as the merge base had it takes the other side's), into a
|
|
232
|
+
// temporary tree, and BUILD.md is rendered from it. The text below the marker is the side's that
|
|
233
|
+
// changed it, else this side's. With no other head to read, BUILD.md is rendered from the working tree.
|
|
234
|
+
export function mergeBuild(root, cfg, { ancestor = '', current = '', other = '', env = process.env } = {}) {
|
|
235
|
+
const changedTail = generatedRegion(current).tail !== generatedRegion(ancestor).tail;
|
|
236
|
+
const existing = changedTail ? current : other;
|
|
237
|
+
const theirs = otherHead(env);
|
|
238
|
+
const head = tryGit(root, ['rev-parse', '--verify', '--quiet', 'HEAD']);
|
|
239
|
+
if (!theirs || !head || !tryGit(root, ['rev-parse', '--verify', '--quiet', `${theirs}^{commit}`])) {
|
|
240
|
+
return renderBuild(root, cfg, existing);
|
|
241
|
+
}
|
|
242
|
+
const paths = kitPaths(root, cfg);
|
|
243
|
+
const dirs = [paths.state, paths.phases].map((d) => posixRel(root, d));
|
|
244
|
+
const base = tryGit(root, ['merge-base', head, theirs]);
|
|
245
|
+
const [o, a, b] = [base, head, theirs].map((c) => blobs(root, c, dirs));
|
|
246
|
+
const chosen = new Map();
|
|
247
|
+
for (const file of new Set([...a.keys(), ...b.keys()])) {
|
|
248
|
+
const [was, ours, them] = [o.get(file), a.get(file), b.get(file)];
|
|
249
|
+
const pick = ours === was ? them : ours;
|
|
250
|
+
if (pick) chosen.set(file, pick);
|
|
251
|
+
}
|
|
252
|
+
const content = readBlobs(root, [...new Set(chosen.values())]);
|
|
253
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'slicekit-build-merge-'));
|
|
254
|
+
try {
|
|
255
|
+
for (const [file, sha] of chosen) {
|
|
256
|
+
const abs = path.join(tmp, ...file.split('/'));
|
|
257
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
258
|
+
fs.writeFileSync(abs, content.get(sha));
|
|
259
|
+
}
|
|
260
|
+
return renderBuild(tmp, cfg, existing);
|
|
261
|
+
} finally {
|
|
262
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
263
|
+
}
|
|
264
|
+
}
|
package/src/state/covers.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
// D-35: a verdict given on head `from` still holds on a later head `to` when `to` descends from
|
|
2
2
|
// `from` and every change between them is the kit's own bookkeeping: the state directory, the
|
|
3
3
|
// review verdicts, the journal directory (every file under it) and BUILD.md. Without this, committing the verdict and the state
|
|
4
|
-
// file would itself make every verdict stale.
|
|
4
|
+
// file would itself make every verdict stale. A merge of the default branch that leaves every file
|
|
5
|
+
// the pull request changes as it was also keeps the verdict (D-100). Any doubt (unknown commit, no
|
|
6
|
+
// repository, no config) is "no".
|
|
5
7
|
import path from 'node:path';
|
|
6
8
|
import { isSha, sameSha } from '../evidence.js';
|
|
9
|
+
import { loadConfig } from '../config.js';
|
|
7
10
|
import { GitError, git, resolveCommit } from '../git.js';
|
|
8
11
|
import { kitPaths } from '../paths.js';
|
|
9
12
|
|
|
@@ -33,7 +36,8 @@ export function isBookkeepingPath(root, rel) {
|
|
|
33
36
|
return bookkeepingTest(root)(rel);
|
|
34
37
|
}
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
// `merges: false` judges by D-35 alone: a merge of the default branch is a moved head (D-43).
|
|
40
|
+
export function headCovers(root, from, to, { merges = true } = {}) {
|
|
37
41
|
if (!isSha(from) || !isSha(to)) return false;
|
|
38
42
|
if (sameSha(from, to)) return true;
|
|
39
43
|
try {
|
|
@@ -47,12 +51,88 @@ export function headCovers(root, from, to) {
|
|
|
47
51
|
if (e instanceof GitError) return false;
|
|
48
52
|
throw e;
|
|
49
53
|
}
|
|
50
|
-
const out = git(root, ['diff', '--name-only', '--no-renames', a, b, '--']);
|
|
51
|
-
const changed = out === '' ? [] : out.split('\n');
|
|
52
54
|
const kept = bookkeeping(root);
|
|
53
|
-
|
|
55
|
+
if (names(root, ['diff', '--name-only', '--no-renames', a, b, '--']).every((f) => inBookkeeping(kept, f))) {
|
|
56
|
+
// A bookkeeping-only difference that a merge reached by putting back older content is no cover.
|
|
57
|
+
return !merges || firstParentLine(root, a, b).every(({ sha, parents }) => parents.length < 2 || keepsSecondParent(root, kept, sha, parents));
|
|
58
|
+
}
|
|
59
|
+
return merges && mergedDefaultOnly(root, kept, a, b);
|
|
54
60
|
} catch (e) {
|
|
55
61
|
if (e instanceof GitError) return false;
|
|
56
62
|
throw e;
|
|
57
63
|
}
|
|
58
64
|
}
|
|
65
|
+
|
|
66
|
+
const names = (root, args) => git(root, args).split('\n').filter(Boolean);
|
|
67
|
+
|
|
68
|
+
// The default branch's tips, on origin first, then local, that resolve; none without a readable config.
|
|
69
|
+
function defaultTips(root) {
|
|
70
|
+
let name;
|
|
71
|
+
try {
|
|
72
|
+
name = loadConfig(root).defaultBranch;
|
|
73
|
+
} catch {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
if (typeof name !== 'string' || name === '') return [];
|
|
77
|
+
return [`refs/remotes/origin/${name}`, `refs/heads/${name}`]
|
|
78
|
+
.map((ref) => resolveCommit(root, ref))
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const isAncestor = (root, x, y) => {
|
|
83
|
+
try {
|
|
84
|
+
git(root, ['merge-base', '--is-ancestor', x, y]);
|
|
85
|
+
return true;
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (e instanceof GitError) return false;
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// D-100, refining D-35: B adds to A, on its first-parent line, only bookkeeping commits and merges
|
|
93
|
+
// whose second parent the default branch reaches and which give every file the slice branch did not
|
|
94
|
+
// change that parent's content, and every file the pull request changes (its
|
|
95
|
+
// changes against the default branch's merge base at A and at B, bookkeeping aside) has the same
|
|
96
|
+
// content at B as at A.
|
|
97
|
+
// The commits B adds to A on its first-parent line, each with its parents.
|
|
98
|
+
const firstParentLine = (root, a, b) =>
|
|
99
|
+
names(root, ['rev-list', '--first-parent', '--parents', b, `^${a}`]).map((line) => {
|
|
100
|
+
const [sha, ...parents] = line.split(' ');
|
|
101
|
+
return { sha, parents };
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Whether merge `sha` gives every file its first parent did not change since it met the second
|
|
105
|
+
// parent, bookkeeping aside, the second parent's content: a merge that puts back older content of a
|
|
106
|
+
// file only the default branch changed does not.
|
|
107
|
+
function keepsSecondParent(root, kept, sha, parents) {
|
|
108
|
+
const base = git(root, ['merge-base', parents[0], parents[1]]);
|
|
109
|
+
const ownSide = new Set(names(root, ['diff', '--name-only', '--no-renames', base, parents[0], '--']));
|
|
110
|
+
return names(root, ['diff', '--name-only', '--no-renames', parents[1], sha, '--']).every((f) => inBookkeeping(kept, f) || ownSide.has(f));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function mergedDefaultOnly(root, kept, a, b) {
|
|
114
|
+
const tips = defaultTips(root);
|
|
115
|
+
if (tips.length === 0) return false;
|
|
116
|
+
for (const { sha, parents } of firstParentLine(root, a, b)) {
|
|
117
|
+
if (parents.length >= 2) {
|
|
118
|
+
if (!tips.some((t) => isAncestor(root, parents[1], t))) return false;
|
|
119
|
+
if (!keepsSecondParent(root, kept, sha, parents)) return false;
|
|
120
|
+
} else {
|
|
121
|
+
const own = names(root, ['diff-tree', '--no-commit-id', '--name-only', '--no-renames', '-r', '--root', sha]);
|
|
122
|
+
if (!own.every((f) => inBookkeeping(kept, f))) return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const own = new Set();
|
|
126
|
+
for (const head of [a, b]) {
|
|
127
|
+
const base = git(root, ['merge-base', head, tips[0]]);
|
|
128
|
+
for (const f of names(root, ['diff', '--name-only', '--no-renames', base, head, '--'])) if (!inBookkeeping(kept, f)) own.add(f);
|
|
129
|
+
}
|
|
130
|
+
if (own.size === 0) return true;
|
|
131
|
+
const files = [...own];
|
|
132
|
+
const blobs = (head) => {
|
|
133
|
+
const listed = git(root, ['ls-tree', '--full-tree', head, '--', ...files]);
|
|
134
|
+
return new Map(listed.split('\n').filter(Boolean).map((l) => [l.slice(l.indexOf('\t') + 1), l.split(' ')[2].split('\t')[0]]));
|
|
135
|
+
};
|
|
136
|
+
const [at, bt] = [blobs(a), blobs(b)];
|
|
137
|
+
return files.every((f) => at.get(f) === bt.get(f));
|
|
138
|
+
}
|