@osdlc/repo-cleaner 0.20.5 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -0
- package/README.md +545 -165
- package/dist/approval.d.ts +91 -0
- package/dist/approval.js +233 -0
- package/dist/archive.d.ts +87 -0
- package/dist/archive.js +252 -0
- package/dist/branches.d.ts +4 -4
- package/dist/branches.js +56 -43
- package/dist/clean.d.ts +30 -70
- package/dist/clean.js +251 -197
- package/dist/cli.d.ts +16 -10
- package/dist/cli.js +252 -103
- package/dist/discover.d.ts +28 -7
- package/dist/discover.js +185 -42
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -2
- package/dist/landing.d.ts +10 -5
- package/dist/landing.js +15 -16
- package/dist/plan.d.ts +18 -11
- package/dist/plan.js +33 -13
- package/dist/processes.d.ts +19 -9
- package/dist/processes.js +51 -25
- package/dist/providers/github.d.ts +21 -3
- package/dist/providers/github.js +149 -27
- package/dist/providers/gitlab.js +37 -17
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/types.d.ts +15 -2
- package/dist/prs.d.ts +19 -10
- package/dist/prs.js +21 -13
- package/dist/receipt.d.ts +68 -3
- package/dist/receipt.js +30 -3
- package/dist/reference.d.ts +1 -1
- package/dist/reference.js +93 -50
- package/dist/remote-mirror.d.ts +2 -1
- package/dist/remote-mirror.js +3 -14
- package/dist/render.d.ts +17 -2
- package/dist/render.js +80 -13
- package/dist/scope.d.ts +15 -0
- package/dist/scope.js +125 -0
- package/dist/size.d.ts +20 -0
- package/dist/size.js +34 -0
- package/dist/status-line.js +1 -0
- package/dist/step-board.js +6 -4
- package/dist/sweep.d.ts +45 -18
- package/dist/sweep.js +151 -127
- package/dist/types.d.ts +89 -10
- package/dist/types.js +50 -6
- package/dist/util.d.ts +2 -0
- package/dist/util.js +25 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/worktrees.js +87 -11
- package/package.json +6 -5
package/dist/clean.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cleaning up: `check` a plan against an approval, then `run` what it shows.
|
|
3
3
|
*
|
|
4
|
-
* `check` resolves the approval (the rows picked, the rows
|
|
5
|
-
* allowed), refuses one that contradicts itself, sweeps every plan repository
|
|
4
|
+
* `check` resolves the approval (the rows selected or picked, the rows
|
|
5
|
+
* spared, the risks allowed), refuses one that contradicts itself, sweeps every plan repository
|
|
6
6
|
* again and drops each selected row that is gone, cannot be deleted, moved
|
|
7
7
|
* since the plan, or carries a risk not allowed. It returns the steps a run
|
|
8
8
|
* would take. `run` sweeps once more and takes those steps in a fixed order,
|
|
9
|
-
*
|
|
9
|
+
* skipping only the steps that depend on a failed one (a failed tip check of a
|
|
10
|
+
* spared remote branch stops the run):
|
|
10
11
|
*
|
|
11
12
|
* 1. spared remote branches, each tip checked with `ls-remote`;
|
|
12
|
-
* 2.
|
|
13
|
+
* 2. with `archive`, one archive per repository, pushed to its remote and
|
|
14
|
+
* checked with `ls-remote` before anything is deleted (see `archive.ts`);
|
|
15
|
+
* 3. worktrees, each unlocked first when its lock's owner is dead, and each
|
|
13
16
|
* checked again for live processes just before it is unlocked or removed;
|
|
14
17
|
* one with submodules is removed with `--force` after its files and every
|
|
15
18
|
* submodule are checked again;
|
|
16
|
-
*
|
|
17
|
-
*
|
|
19
|
+
* 4. local branches;
|
|
20
|
+
* 5. remote branches.
|
|
18
21
|
*
|
|
19
22
|
* A worktree whose directory is gone goes by its own record. One left without
|
|
20
23
|
* its `.git` takes the repository-wide prune, last, and only while that prune
|
|
@@ -23,167 +26,25 @@
|
|
|
23
26
|
* Branch deletes are compare-and-swap on the planned sha. Every step is
|
|
24
27
|
* verified, and the receipt is rewritten after each.
|
|
25
28
|
*/
|
|
26
|
-
import { existsSync
|
|
27
|
-
import {
|
|
29
|
+
import { existsSync } from 'node:fs';
|
|
30
|
+
import { join, resolve } from 'node:path';
|
|
28
31
|
import { shellJoin } from '@osdlc/command-seam';
|
|
29
32
|
import { nodeGuard } from '@osdlc/path-guard/node';
|
|
30
33
|
import { Git, PUSH_REJECTIONS } from '@osdlc/typed-git';
|
|
34
|
+
import { coverageOf, mayContribute, worktreeStamp, pushArchive, } from "./archive.js";
|
|
35
|
+
import { allowed, keyOf, resolveApproval, same, uniq, } from "./approval.js";
|
|
31
36
|
import { localDeleteCommand, remoteDeleteCommand } from "./branches.js";
|
|
32
37
|
import { fingerprint, runnable, sameFingerprint } from "./plan.js";
|
|
33
38
|
import { busyHolders, defaultProcessProbe, parseLockOwner } from "./processes.js";
|
|
34
39
|
import { RECEIPT_VERSION } from "./receipt.js";
|
|
35
40
|
import { defaultCacheDir, mirrorProblem } from "./remote-mirror.js";
|
|
41
|
+
import { sizeOf } from "./size.js";
|
|
36
42
|
import { inspectSubmodules, submoduleHazards } from "./submodules.js";
|
|
37
43
|
import { holdFreedBranches, sweepRepo } from "./sweep.js";
|
|
38
|
-
import { KINDS,
|
|
44
|
+
import { KINDS, RepoCleanerError, judgedRemotes, landedFor, refOf, parseRowId, sweepNotes, } from "./types.js";
|
|
39
45
|
import { errorText } from "./util.js";
|
|
40
46
|
import { removeIsForced, worktreeCommands } from "./worktrees.js";
|
|
41
47
|
import { writeJsonAtomic } from "./write-json.js";
|
|
42
|
-
/**
|
|
43
|
-
* `kind:name`, or a bare name. A word before a colon that is not a kind is an
|
|
44
|
-
* error (a typo such as `wt:x`), but a path is a bare name whatever colons it
|
|
45
|
-
* holds: a prefix with a path separator in it is never a kind.
|
|
46
|
-
*/
|
|
47
|
-
export function parseRowRef(text) {
|
|
48
|
-
if (text === '')
|
|
49
|
-
throw new RepoCleanerError('bad-input', 'an empty row');
|
|
50
|
-
const id = parseRowId(text);
|
|
51
|
-
if (id !== null) {
|
|
52
|
-
if (id.name === '')
|
|
53
|
-
throw new RepoCleanerError('bad-input', `bad row ${JSON.stringify(text)}: no name`);
|
|
54
|
-
return id;
|
|
55
|
-
}
|
|
56
|
-
const colon = text.indexOf(':');
|
|
57
|
-
if (colon > 1 && !/[\\/]/.test(text.slice(0, colon))) {
|
|
58
|
-
throw new RepoCleanerError('bad-input', `bad row ${JSON.stringify(text)}: expected <kind>:<name> with kind one of ${KINDS.join(', ')}, or a bare name`);
|
|
59
|
-
}
|
|
60
|
-
return { kind: null, name: text };
|
|
61
|
-
}
|
|
62
|
-
/** `<risk>` for every row, or `<risk>=<row>` for one. */
|
|
63
|
-
export function parseAllow(text) {
|
|
64
|
-
const eq = text.indexOf('=');
|
|
65
|
-
const risk = eq < 0 ? text : text.slice(0, eq);
|
|
66
|
-
checkRisk(risk);
|
|
67
|
-
if (eq < 0)
|
|
68
|
-
return { risk };
|
|
69
|
-
const row = text.slice(eq + 1);
|
|
70
|
-
if (row === '')
|
|
71
|
-
throw new RepoCleanerError('bad-input', `--allow ${text} names no row`);
|
|
72
|
-
return { risk, row };
|
|
73
|
-
}
|
|
74
|
-
function checkRisk(risk) {
|
|
75
|
-
if (!RISKS.includes(risk)) {
|
|
76
|
-
throw new RepoCleanerError('bad-input', `unknown risk ${JSON.stringify(risk)}: expected one of ${RISKS.join(', ')}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/** A conflict for a reference no sweep with `options` could hold: a kind it left out, or a row outside its `--branch`. */
|
|
80
|
-
function checkScope(options, ref, flag, text) {
|
|
81
|
-
const { only, branch } = options;
|
|
82
|
-
if (ref.kind !== null && !only.includes(ref.kind)) {
|
|
83
|
-
throw new RepoCleanerError('conflict', `${flag} ${text}: the sweep has no ${ref.kind} rows (it sweeps ${only.join(', ')})`);
|
|
84
|
-
}
|
|
85
|
-
const outside = branch !== null &&
|
|
86
|
-
((ref.kind === 'branch' && ref.name !== branch) ||
|
|
87
|
-
(ref.kind === 'remote' && !ref.name.endsWith(`/${branch}`)));
|
|
88
|
-
if (outside) {
|
|
89
|
-
throw new RepoCleanerError('conflict', `${flag} ${text} is not a row of --branch ${branch}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* What can be refused of `approval` before anything is swept: a malformed
|
|
94
|
-
* row or risk, a row a sweep with `options` could never hold, and a row
|
|
95
|
-
* picked and spared as written. A row named two ways, and an `allow` for a
|
|
96
|
-
* spared or unpicked row, are found when the approval is resolved against
|
|
97
|
-
* the plan.
|
|
98
|
-
*/
|
|
99
|
-
export function checkApproval(options, approval) {
|
|
100
|
-
const refs = [
|
|
101
|
-
...(approval.pick ?? []).map((t) => ['--pick', t]),
|
|
102
|
-
...(approval.spare ?? []).map((t) => ['--spare', t]),
|
|
103
|
-
];
|
|
104
|
-
for (const a of approval.allow ?? []) {
|
|
105
|
-
const { risk, row } = typeof a === 'string' ? { risk: a, row: undefined } : a;
|
|
106
|
-
checkRisk(risk);
|
|
107
|
-
if (row !== undefined)
|
|
108
|
-
refs.push(['--allow', row]);
|
|
109
|
-
}
|
|
110
|
-
for (const [flag, text] of refs)
|
|
111
|
-
checkScope(options, parseRowRef(text), flag, text);
|
|
112
|
-
const both = (approval.pick ?? []).find((p) => (approval.spare ?? []).includes(p));
|
|
113
|
-
if (both !== undefined)
|
|
114
|
-
throw new RepoCleanerError('conflict', `${both} is both picked and spared`);
|
|
115
|
-
}
|
|
116
|
-
/** The names a worktree reference may match: as written, against `cwd`, and resolved through symlinks. */
|
|
117
|
-
function pathForms(name, cwd) {
|
|
118
|
-
const abs = isAbsolute(name) ? name : resolve(cwd, name);
|
|
119
|
-
const forms = new Set([name, abs]);
|
|
120
|
-
try {
|
|
121
|
-
forms.add(realpathSync(abs));
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
// A gone worktree has no real path; the plan names it as git did.
|
|
125
|
-
}
|
|
126
|
-
return forms;
|
|
127
|
-
}
|
|
128
|
-
/** The plan row `text` names, its scope checked first so a row the plan could never hold is a conflict, not a miss. */
|
|
129
|
-
function resolveRef(plan, text, flag, cwd) {
|
|
130
|
-
const ref = parseRowRef(text);
|
|
131
|
-
checkScope(plan.options, ref, flag, text);
|
|
132
|
-
const paths = ref.kind === null || ref.kind === 'worktree' ? pathForms(ref.name, cwd) : null;
|
|
133
|
-
const hits = [];
|
|
134
|
-
for (const repo of plan.repos) {
|
|
135
|
-
for (const row of repo.rows) {
|
|
136
|
-
const { kind, name } = parseRowId(row.id);
|
|
137
|
-
if (ref.kind !== null && ref.kind !== kind)
|
|
138
|
-
continue;
|
|
139
|
-
if (kind === 'worktree' ? paths.has(name) : ref.name === name) {
|
|
140
|
-
hits.push({ repo: repo.root, id: row.id });
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (hits.length === 0) {
|
|
145
|
-
throw new RepoCleanerError('not-found', `${flag} ${text}: no such row in the plan`);
|
|
146
|
-
}
|
|
147
|
-
if (hits.length > 1) {
|
|
148
|
-
const where = [...new Set(hits.map((h) => `${h.id} in ${h.repo}`))].join(', ');
|
|
149
|
-
throw new RepoCleanerError('ambiguous', `${flag} ${text} matches ${hits.length} rows (${where}); sweep or plan only the repository it is in${ref.kind === null ? ', or name it by its full id' : ''}`);
|
|
150
|
-
}
|
|
151
|
-
return hits[0];
|
|
152
|
-
}
|
|
153
|
-
const keyOf = (r) => `${r.repo}\0${r.id}`;
|
|
154
|
-
const same = (a, b) => a.repo === b.repo && a.id === b.id;
|
|
155
|
-
const uniq = (rows) => [...new Map(rows.map((r) => [keyOf(r), r])).values()];
|
|
156
|
-
/**
|
|
157
|
-
* `approval` resolved against `plan`, or a {@link RepoCleanerError} for one
|
|
158
|
-
* that contradicts itself or names rows the plan does not have.
|
|
159
|
-
*/
|
|
160
|
-
export function resolveApproval(plan, approval, cwd) {
|
|
161
|
-
checkApproval(plan.options, approval);
|
|
162
|
-
const pick = uniq((approval.pick ?? []).map((t) => resolveRef(plan, t, '--pick', cwd)));
|
|
163
|
-
const spare = uniq((approval.spare ?? []).map((t) => resolveRef(plan, t, '--spare', cwd)));
|
|
164
|
-
const picked = new Set(pick.map(keyOf));
|
|
165
|
-
const spared = new Set(spare.map(keyOf));
|
|
166
|
-
const both = pick.find((p) => spared.has(keyOf(p)));
|
|
167
|
-
if (both !== undefined)
|
|
168
|
-
throw new RepoCleanerError('conflict', `${both.id} is both picked and spared`);
|
|
169
|
-
const allow = (approval.allow ?? []).map((a) => {
|
|
170
|
-
const { risk, row } = typeof a === 'string' ? { risk: a, row: undefined } : a;
|
|
171
|
-
if (row === undefined)
|
|
172
|
-
return { risk, row: null };
|
|
173
|
-
const at = resolveRef(plan, row, '--allow', cwd);
|
|
174
|
-
if (spared.has(keyOf(at))) {
|
|
175
|
-
throw new RepoCleanerError('conflict', `--allow ${risk}=${row}: ${at.id} is spared`);
|
|
176
|
-
}
|
|
177
|
-
if (pick.length > 0 && !picked.has(keyOf(at))) {
|
|
178
|
-
throw new RepoCleanerError('conflict', `--allow ${risk}=${row}: ${at.id} is not picked`);
|
|
179
|
-
}
|
|
180
|
-
return { risk, row: at };
|
|
181
|
-
});
|
|
182
|
-
return { pick, spare, allow };
|
|
183
|
-
}
|
|
184
|
-
function allowed(allow, at, risk) {
|
|
185
|
-
return allow.some((a) => a.risk === risk && (a.row === null || same(a.row, at)));
|
|
186
|
-
}
|
|
187
48
|
/**
|
|
188
49
|
* The plan repository's rows as they stand now, annotated as the sweep
|
|
189
50
|
* annotated them, and, when it had to sweep again, that sweep's notes on what
|
|
@@ -195,14 +56,18 @@ function resweep(repo, plan, kinds, opts) {
|
|
|
195
56
|
// An offline sweep read remote tips from disk, so it cannot stand in for
|
|
196
57
|
// the re-check of remote rows: sweep again and ask the remote, so the
|
|
197
58
|
// preview shows what the run will do.
|
|
198
|
-
const readFromDisk = kinds.includes('remote') &&
|
|
59
|
+
const readFromDisk = kinds.includes('remote') &&
|
|
60
|
+
(given?.repo === undefined ||
|
|
61
|
+
given.repo === null ||
|
|
62
|
+
!judgedRemotes(given.repo).every((r) => given.repo.fetchedRemotes.includes(r.name)));
|
|
199
63
|
if (given !== undefined && given.error === null && !readFromDisk) {
|
|
200
64
|
// Its notes are reported with the rest of that sweep's, by `recheck`.
|
|
201
|
-
return { rows: holdFreedBranches([...given.rows], branchScoped), notes: [] };
|
|
65
|
+
return { rows: holdFreedBranches([...given.rows], branchScoped), notes: [], info: given.repo };
|
|
202
66
|
}
|
|
203
67
|
const swept = sweepRepo(repo.root, {
|
|
204
68
|
...opts,
|
|
205
69
|
only: kinds,
|
|
70
|
+
remotes: repo.remotes,
|
|
206
71
|
against: plan.options.against ?? undefined,
|
|
207
72
|
provider: plan.options.provider,
|
|
208
73
|
fetch: repo.fetched,
|
|
@@ -215,23 +80,23 @@ function resweep(repo, plan, kinds, opts) {
|
|
|
215
80
|
if (swept.repo === null || swept.error !== null) {
|
|
216
81
|
throw new Error(`${repo.root}: ${swept.error ?? 'could not read the repository'}`);
|
|
217
82
|
}
|
|
218
|
-
return { rows: swept.rows, notes: swept.notes };
|
|
83
|
+
return { rows: swept.rows, notes: swept.notes, info: swept.repo };
|
|
219
84
|
}
|
|
220
85
|
/**
|
|
221
|
-
* Re-sweep, then split the selection into rows to run and rows dropped
|
|
222
|
-
*
|
|
223
|
-
*
|
|
86
|
+
* Re-sweep, then split the selection into rows to run and rows dropped: the
|
|
87
|
+
* selected rows, or every row that could run, and the picked rows, less the
|
|
88
|
+
* spared. A row no one picked or selected by name runs only while the
|
|
89
|
+
* re-sweep judges it `delete`.
|
|
224
90
|
*/
|
|
225
|
-
function recheck(plan, approval, opts
|
|
226
|
-
const
|
|
227
|
-
|
|
91
|
+
function recheck(plan, approval, opts) {
|
|
92
|
+
const chosen = new Set([...(approval.select ?? []), ...approval.pick].map(keyOf));
|
|
93
|
+
const mustDelete = (s) => !chosen.has(keyOf(s));
|
|
94
|
+
const spared = new Set([...approval.spare, ...approval.spareMatched].map(keyOf));
|
|
95
|
+
// By default every row that could run: a plan also holds the blocked and
|
|
228
96
|
// structural rows, so a pick of one can be skipped with its reason.
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
.map((row) => ({ repo: r.root, id: row.id }))
|
|
233
|
-
.filter((s) => !spared.has(keyOf(s))))
|
|
234
|
-
: approval.pick;
|
|
97
|
+
const base = approval.select ??
|
|
98
|
+
plan.repos.flatMap((r) => r.rows.filter(runnable).map((row) => ({ repo: r.root, id: row.id })));
|
|
99
|
+
const selected = uniq([...base, ...approval.pick]).filter((s) => !spared.has(keyOf(s)));
|
|
235
100
|
const all = [...selected, ...approval.spare];
|
|
236
101
|
const plannedRows = new Map(plan.repos.flatMap((r) => r.rows.map((row) => [keyOf({ repo: r.root, id: row.id }), row])));
|
|
237
102
|
const kindsIn = new Map();
|
|
@@ -241,6 +106,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
241
106
|
kindsIn.set(s.repo, kinds);
|
|
242
107
|
}
|
|
243
108
|
const currentRows = new Map();
|
|
109
|
+
const infos = new Map();
|
|
244
110
|
const notes = [];
|
|
245
111
|
for (const [root, wanted] of kindsIn) {
|
|
246
112
|
const repo = plan.repos.find((r) => r.root === root);
|
|
@@ -250,6 +116,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
250
116
|
if (wanted.has('branch') && plan.options.only.includes('worktree'))
|
|
251
117
|
wanted.add('worktree');
|
|
252
118
|
const now = resweep(repo, plan, KINDS.filter((k) => wanted.has(k)), opts);
|
|
119
|
+
infos.set(root, now.info);
|
|
253
120
|
for (const row of now.rows)
|
|
254
121
|
currentRows.set(keyOf({ repo: root, id: row.id }), row);
|
|
255
122
|
for (const note of now.notes)
|
|
@@ -275,7 +142,14 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
275
142
|
if (typeof plannedSha !== 'string' || row.evidence.sha !== plannedSha) {
|
|
276
143
|
throw new RepoCleanerError('spared-changed', `spared ${s.id} moved since the plan (planned ${String(plannedSha)}, now ${row.evidence.sha}); refusing to run`);
|
|
277
144
|
}
|
|
278
|
-
verified.push({
|
|
145
|
+
verified.push({
|
|
146
|
+
repo: s.repo,
|
|
147
|
+
row,
|
|
148
|
+
discard: false,
|
|
149
|
+
abandon: false,
|
|
150
|
+
discardNamed: false,
|
|
151
|
+
needs: [],
|
|
152
|
+
});
|
|
279
153
|
}
|
|
280
154
|
const accepted = [];
|
|
281
155
|
const dropped = [];
|
|
@@ -292,7 +166,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
292
166
|
drop(row.verdict === 'blocked' ? 'blocked' : 'keep', row.why);
|
|
293
167
|
continue;
|
|
294
168
|
}
|
|
295
|
-
if (
|
|
169
|
+
if (mustDelete(s) && row.verdict !== 'delete') {
|
|
296
170
|
drop('needs-pick', `${row.verdict}: ${row.why}; pick it to run it`);
|
|
297
171
|
continue;
|
|
298
172
|
}
|
|
@@ -302,22 +176,60 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
302
176
|
drop('changed', `planned ${JSON.stringify(was)}, now ${JSON.stringify(now)}`);
|
|
303
177
|
continue;
|
|
304
178
|
}
|
|
305
|
-
const
|
|
179
|
+
const coverage = opts.archive === undefined
|
|
180
|
+
? null
|
|
181
|
+
: coverageOf(row, opts.archive, (path) => new Git(path, { runner: opts.git }));
|
|
182
|
+
// `discard-archived` grants the risks the archive covers, and only those.
|
|
183
|
+
const archived = (risk) => coverage?.covers.includes(risk) === true && allowed(approval.allow, s, 'discard-archived');
|
|
184
|
+
const grants = (risk) => allowed(approval.allow, s, risk) || archived(risk);
|
|
185
|
+
// A risk the archive covers is asked for as `discard-archived`.
|
|
186
|
+
const asked = (risk) => coverage?.covers.includes(risk) ? 'discard-archived' : risk;
|
|
187
|
+
const needs = [...new Set(row.risks.map(asked))];
|
|
188
|
+
const missing = row.risks.filter((risk) => !grants(risk));
|
|
306
189
|
if (missing.length > 0) {
|
|
307
|
-
drop('not-allowed', `needs ${missing.join(', ')}`);
|
|
190
|
+
drop('not-allowed', `needs ${[...new Set(missing.map(asked))].join(', ')}`);
|
|
308
191
|
continue;
|
|
309
192
|
}
|
|
310
193
|
accepted.push({
|
|
311
194
|
repo: s.repo,
|
|
312
195
|
row,
|
|
313
|
-
discard:
|
|
314
|
-
abandon:
|
|
196
|
+
discard: grants('discard-changes'),
|
|
197
|
+
abandon: grants('abandon-unmerged'),
|
|
198
|
+
discardNamed: allowed(approval.allow, s, 'discard-changes'),
|
|
199
|
+
needs,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
// A worktree judged safe because remote branches hold its HEAD loses that
|
|
203
|
+
// cover when the same run deletes every one of them.
|
|
204
|
+
const deletedRemotes = new Set(accepted.flatMap((a) => a.row.kind === 'remote'
|
|
205
|
+
? [`${a.repo}\0${a.row.evidence.remote}/${a.row.evidence.branch}`]
|
|
206
|
+
: []));
|
|
207
|
+
const coverGone = (a) => {
|
|
208
|
+
if (a.row.kind !== 'worktree' || a.abandon)
|
|
209
|
+
return null;
|
|
210
|
+
const from = a.row.evidence.recoverableFrom ?? [];
|
|
211
|
+
return from.length > 0 && from.every((name) => deletedRemotes.has(`${a.repo}\0${name}`))
|
|
212
|
+
? from
|
|
213
|
+
: null;
|
|
214
|
+
};
|
|
215
|
+
const stillCovered = [];
|
|
216
|
+
for (const a of accepted) {
|
|
217
|
+
const from = coverGone(a);
|
|
218
|
+
if (from === null) {
|
|
219
|
+
stillCovered.push(a);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
dropped.push({
|
|
223
|
+
repo: a.repo,
|
|
224
|
+
row: a.row.id,
|
|
225
|
+
reason: 'not-allowed',
|
|
226
|
+
detail: `needs abandon-unmerged: this run also deletes ${from.join(', ')}, the only remote copies of its HEAD`,
|
|
315
227
|
});
|
|
316
228
|
}
|
|
317
229
|
// A branch freed by its worktree's removal goes only with that worktree.
|
|
318
|
-
const leaving = new Set(
|
|
230
|
+
const leaving = new Set(stillCovered.filter((a) => a.row.kind === 'worktree').map((a) => a.row.name));
|
|
319
231
|
const kept = [];
|
|
320
|
-
for (const a of
|
|
232
|
+
for (const a of stillCovered) {
|
|
321
233
|
const worktree = a.row.kind === 'branch' ? a.row.evidence.worktree : undefined;
|
|
322
234
|
if (worktree === undefined || leaving.has(worktree)) {
|
|
323
235
|
kept.push(a);
|
|
@@ -336,6 +248,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
336
248
|
accepted: kept,
|
|
337
249
|
dropped,
|
|
338
250
|
verified,
|
|
251
|
+
infos,
|
|
339
252
|
notes: opts.sweep === undefined ? notes : uniqueNotes([...sweepNotes(opts.sweep), ...notes]),
|
|
340
253
|
};
|
|
341
254
|
}
|
|
@@ -480,10 +393,10 @@ function stepsFor({ repo, row }) {
|
|
|
480
393
|
];
|
|
481
394
|
}
|
|
482
395
|
}
|
|
483
|
-
function planSteps(plan, approval, opts
|
|
396
|
+
function planSteps(plan, approval, opts) {
|
|
484
397
|
const cwd = opts.cwd ?? process.cwd();
|
|
485
398
|
const git = new Git(cwd, { runner: opts.git });
|
|
486
|
-
const rechecked = recheck(plan, approval, opts
|
|
399
|
+
const rechecked = recheck(plan, approval, opts);
|
|
487
400
|
const unique = onePerRemoteBranch(rechecked, git);
|
|
488
401
|
const dropped = [...unique.dropped];
|
|
489
402
|
// A worktree whose directory is left without its `.git` goes only by the
|
|
@@ -509,12 +422,34 @@ function planSteps(plan, approval, opts, safeOnly) {
|
|
|
509
422
|
const of = accepted.filter((a) => a.row.kind === kind);
|
|
510
423
|
return [...of.filter((a) => !prunesAll(a.row)), ...of.filter((a) => prunesAll(a.row))];
|
|
511
424
|
});
|
|
425
|
+
const archives = new Map();
|
|
426
|
+
const archiveSteps = [];
|
|
427
|
+
if (opts.archive !== undefined) {
|
|
428
|
+
for (const root of new Set(ordered.map((a) => a.repo))) {
|
|
429
|
+
const members = ordered.filter((a) => a.repo === root && mayContribute(a.row));
|
|
430
|
+
if (members.length === 0)
|
|
431
|
+
continue;
|
|
432
|
+
const info = rechecked.infos.get(root);
|
|
433
|
+
if (info.remote === null) {
|
|
434
|
+
throw new RepoCleanerError('conflict', `archive: ${root} has no remote to push the archive to`);
|
|
435
|
+
}
|
|
436
|
+
const step = pending(root, members[0].row.id, 'archive', shellJoin(['git', 'push', info.remote, 'refs/heads/archive/<repo>-local-<date>']));
|
|
437
|
+
archives.set(step, {
|
|
438
|
+
remote: info.remote,
|
|
439
|
+
defaultRef: refOf(landedFor(info)),
|
|
440
|
+
jobs: members.map((a) => ({ id: a.row.id, row: a.row, discardAllowed: a.discardNamed })),
|
|
441
|
+
});
|
|
442
|
+
archiveSteps.push([step, null]);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
512
445
|
const steps = [
|
|
513
446
|
// Spared tips first: one that moved stops the run before anything is deleted.
|
|
514
447
|
...rechecked.verified.map((a) => [verifyStep(a), a]),
|
|
448
|
+
// Then the archives: a push that fails stops the run before any delete.
|
|
449
|
+
...archiveSteps,
|
|
515
450
|
...ordered.flatMap((a) => stepsFor(a).map((step) => [step, a])),
|
|
516
451
|
];
|
|
517
|
-
return { steps, dropped, accepted: ordered, removedHere, notes: rechecked.notes };
|
|
452
|
+
return { steps, dropped, accepted: ordered, removedHere, notes: rechecked.notes, archives };
|
|
518
453
|
}
|
|
519
454
|
/** A worktree row whose deletion takes the repository-wide prune: its directory is left without its `.git`. */
|
|
520
455
|
function prunesAll(row) {
|
|
@@ -556,14 +491,25 @@ export function check(plan, approval, opts = {}) {
|
|
|
556
491
|
const cwd = opts.cwd ?? process.cwd();
|
|
557
492
|
checkMirrors(plan, opts.cacheDir ?? defaultCacheDir());
|
|
558
493
|
const resolved = resolveApproval(plan, approval, cwd);
|
|
559
|
-
|
|
494
|
+
if (opts.archive === undefined && resolved.allow.some((a) => a.risk === 'discard-archived')) {
|
|
495
|
+
throw new RepoCleanerError('conflict', '--allow discard-archived has no effect without archive');
|
|
496
|
+
}
|
|
497
|
+
const { steps, dropped, accepted, notes } = planSteps(plan, resolved, opts);
|
|
560
498
|
for (const [step] of steps)
|
|
561
499
|
step.result = 'would-run';
|
|
562
500
|
const unusedAllows = resolved.allow
|
|
563
|
-
.filter((a) => !accepted.some((row) => row.row.risks.includes(a.risk) &&
|
|
501
|
+
.filter((a) => !accepted.some((row) => (row.needs.includes(a.risk) || row.row.risks.includes(a.risk)) &&
|
|
564
502
|
(a.row === null || same(a.row, { repo: row.repo, id: row.row.id }))))
|
|
565
503
|
.map((a) => ({ risk: a.risk, row: a.row?.id ?? null }));
|
|
566
|
-
return {
|
|
504
|
+
return {
|
|
505
|
+
plan,
|
|
506
|
+
approval: resolved,
|
|
507
|
+
steps: steps.map(([s]) => s),
|
|
508
|
+
dropped,
|
|
509
|
+
unusedAllows,
|
|
510
|
+
notes,
|
|
511
|
+
archive: opts.archive,
|
|
512
|
+
};
|
|
567
513
|
}
|
|
568
514
|
// ── the run ────────────────────────────────────────────────────────────────
|
|
569
515
|
/**
|
|
@@ -602,6 +548,50 @@ function assertSafeToForce(path, git, a) {
|
|
|
602
548
|
if (reasons.length > 0)
|
|
603
549
|
throw new Error(`refused: ${reasons.join('; ')}`);
|
|
604
550
|
}
|
|
551
|
+
/**
|
|
552
|
+
* Whether `step` cannot run once `prior`, an earlier step, failed or was
|
|
553
|
+
* skipped. Only steps in the same repository can depend on each other, and
|
|
554
|
+
* `a` and `priorA` are the rows the two steps act on:
|
|
555
|
+
*
|
|
556
|
+
* - every non-archive step waits on a failed archive push in its repository;
|
|
557
|
+
* - a worktree's remove or prune waits on its unlock;
|
|
558
|
+
* - a branch delete waits on the removal of the worktree that holds the branch;
|
|
559
|
+
* - the repository-wide prune waits on every worktree step in the repository,
|
|
560
|
+
* since it counted on each removal;
|
|
561
|
+
* - a remote delete waits on the delete of the same branch's local copy.
|
|
562
|
+
*/
|
|
563
|
+
function dependsOn(step, a, prior, priorA) {
|
|
564
|
+
// A failed archive push stops the repository's deletes: nothing is
|
|
565
|
+
// removed that was not saved.
|
|
566
|
+
if (prior.action === 'archive' && step.action !== 'archive' && step.repo === prior.repo) {
|
|
567
|
+
return true;
|
|
568
|
+
}
|
|
569
|
+
if (a === null || priorA === null || step.repo !== prior.repo)
|
|
570
|
+
return false;
|
|
571
|
+
const row = a.row;
|
|
572
|
+
const was = priorA.row;
|
|
573
|
+
const worktreeStep = (s) => s.action === 'unlock-worktree' ||
|
|
574
|
+
s.action === 'remove-worktree' ||
|
|
575
|
+
s.action === 'prune-worktrees';
|
|
576
|
+
if (step.action === 'remove-worktree' || step.action === 'prune-worktrees') {
|
|
577
|
+
if (prior.action === 'unlock-worktree' && row.id === was.id)
|
|
578
|
+
return true;
|
|
579
|
+
return prunesAll(row) && worktreeStep(prior);
|
|
580
|
+
}
|
|
581
|
+
if (step.action === 'delete-branch') {
|
|
582
|
+
return (worktreeStep(prior) &&
|
|
583
|
+
was.kind === 'worktree' &&
|
|
584
|
+
row.kind === 'branch' &&
|
|
585
|
+
was.evidence.branch === row.name);
|
|
586
|
+
}
|
|
587
|
+
if (step.action === 'delete-remote') {
|
|
588
|
+
return (prior.action === 'delete-branch' &&
|
|
589
|
+
row.kind === 'remote' &&
|
|
590
|
+
was.kind === 'branch' &&
|
|
591
|
+
row.evidence.branch === was.name);
|
|
592
|
+
}
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
605
595
|
/** A remote step that found the branch already gone: its verification, marked so. */
|
|
606
596
|
class Gone {
|
|
607
597
|
verification;
|
|
@@ -612,12 +602,29 @@ class Gone {
|
|
|
612
602
|
/**
|
|
613
603
|
* Run one step, then confirm it took. Throws with the reason on failure.
|
|
614
604
|
*/
|
|
615
|
-
function execute(step, a, git, probe, removedHere
|
|
605
|
+
function execute(step, a, git, probe, removedHere,
|
|
606
|
+
/** A directory's size on disk, null when it could not be read. */
|
|
607
|
+
measure, archive) {
|
|
616
608
|
const at = git.at(step.repo);
|
|
617
609
|
const remoteTip = (remote, ref) => at.lsRemote(remote, ref).find((r) => r.ref === ref);
|
|
618
610
|
const registered = (path) => at.worktrees().some((w) => resolve(w.path) === resolve(path));
|
|
619
611
|
const row = a?.row;
|
|
620
612
|
switch (step.action) {
|
|
613
|
+
case 'archive': {
|
|
614
|
+
if (archive.job === undefined || archive.options === undefined) {
|
|
615
|
+
throw new Error('archive without its rows');
|
|
616
|
+
}
|
|
617
|
+
const { remote, defaultRef, jobs } = archive.job;
|
|
618
|
+
const pushed = pushArchive(at, remote, defaultRef, jobs, archive.options, archive.now);
|
|
619
|
+
if (pushed === null)
|
|
620
|
+
return 'nothing to archive';
|
|
621
|
+
const { record } = pushed;
|
|
622
|
+
for (const [id, stamp] of pushed.stamps)
|
|
623
|
+
archive.stamps.set(`${step.repo}\0${id}`, stamp);
|
|
624
|
+
step.archive = record;
|
|
625
|
+
step.command = shellJoin(['git', 'push', remote, `${record.sha}:${record.ref}`]);
|
|
626
|
+
return `${record.ref} lists ${record.sha}`;
|
|
627
|
+
}
|
|
621
628
|
case 'unlock-worktree': {
|
|
622
629
|
if (row?.kind !== 'worktree')
|
|
623
630
|
throw new Error('unlock without a worktree');
|
|
@@ -663,14 +670,24 @@ function execute(step, a, git, probe, removedHere) {
|
|
|
663
670
|
throw new Error('remove without a worktree');
|
|
664
671
|
const path = row.name;
|
|
665
672
|
assertNoLiveProcess(path, probe);
|
|
673
|
+
const stamp = archive.stamps.get(`${step.repo}\0${row.id}`);
|
|
674
|
+
if (stamp !== undefined &&
|
|
675
|
+
stamp !==
|
|
676
|
+
worktreeStamp(at.at(path), archive.options, row.risks.includes('discard-changes'))) {
|
|
677
|
+
throw new Error(`refused: ${path} changed since it was archived`);
|
|
678
|
+
}
|
|
666
679
|
const force = removeIsForced(row.evidence);
|
|
667
680
|
if (force)
|
|
668
681
|
assertSafeToForce(path, git, a);
|
|
682
|
+
// Measured last, just before the remove: what this step frees. Recorded
|
|
683
|
+
// only once the remove took; a failed remove frees nothing.
|
|
684
|
+
const bytes = measure(path);
|
|
669
685
|
at.worktreeRemove(path, { force });
|
|
670
686
|
if (existsSync(path))
|
|
671
687
|
throw new Error(`directory still exists: ${path}`);
|
|
672
688
|
if (registered(path))
|
|
673
689
|
throw new Error(`still registered: ${path}`);
|
|
690
|
+
step.freedBytes = bytes;
|
|
674
691
|
return 'directory gone and unregistered';
|
|
675
692
|
}
|
|
676
693
|
case 'delete-branch': {
|
|
@@ -731,8 +748,10 @@ function execute(step, a, git, probe, removedHere) {
|
|
|
731
748
|
/**
|
|
732
749
|
* Take the steps `preview` showed: sweeps again, drops a row that moved since
|
|
733
750
|
* the preview, and runs the rest in order, compare-and-swap, verifying each
|
|
734
|
-
* step
|
|
735
|
-
*
|
|
751
|
+
* step. A failed step skips only the steps that depend on it, except a failed
|
|
752
|
+
* spared-tip check, which stops the run. Only what the preview showed runs; a
|
|
753
|
+
* row it dropped stays dropped. Each worktree directory it removes is measured
|
|
754
|
+
* with `du` just before, into that step's `freedBytes`.
|
|
736
755
|
*
|
|
737
756
|
* @throws RepoCleanerError (`spared-changed`) when a spared remote branch moved
|
|
738
757
|
* since the preview, and a plain `Error` when the re-check cannot read a
|
|
@@ -743,12 +762,23 @@ export function run(preview, opts = {}) {
|
|
|
743
762
|
const cwd = opts.cwd ?? process.cwd();
|
|
744
763
|
// Only the rows the preview would run, pinned to their repositories. A
|
|
745
764
|
// spared tip check is not a selection; the approval carries it again.
|
|
746
|
-
const shown = preview.steps.flatMap((s) => s.action === 'verify-remote' ? [] : [{ repo: s.repo, id: s.row }]);
|
|
747
|
-
|
|
765
|
+
const shown = uniq(preview.steps.flatMap((s) => s.action === 'verify-remote' || s.action === 'archive' ? [] : [{ repo: s.repo, id: s.row }]));
|
|
766
|
+
// The same approval, narrowed to what the preview showed: the rows chosen
|
|
767
|
+
// by name stay picked, every other runnable row it did not show is left
|
|
768
|
+
// alone, and the rest run by the default rule, so a row the preview ran only
|
|
769
|
+
// for its `delete` verdict drops if the re-check now judges it `review`.
|
|
770
|
+
const shownKeys = new Set(shown.map(keyOf));
|
|
771
|
+
const named = new Set([...(preview.approval.select ?? []), ...preview.approval.pick].map(keyOf));
|
|
772
|
+
const unshown = preview.plan.repos.flatMap((r) => r.rows
|
|
773
|
+
.filter(runnable)
|
|
774
|
+
.map((row) => ({ repo: r.root, id: row.id }))
|
|
775
|
+
.filter((s) => !shownKeys.has(keyOf(s))));
|
|
748
776
|
const approval = {
|
|
749
|
-
|
|
777
|
+
select: null,
|
|
778
|
+
pick: shown.filter((s) => named.has(keyOf(s))),
|
|
750
779
|
spare: preview.approval.spare,
|
|
751
|
-
|
|
780
|
+
spareMatched: uniq([...preview.approval.spareMatched, ...unshown]),
|
|
781
|
+
allow: preview.approval.allow.filter((a) => a.row === null || shown.some((p) => same(p, a.row))),
|
|
752
782
|
};
|
|
753
783
|
const receipt = {
|
|
754
784
|
version: RECEIPT_VERSION,
|
|
@@ -769,12 +799,10 @@ export function run(preview, opts = {}) {
|
|
|
769
799
|
save();
|
|
770
800
|
return receipt;
|
|
771
801
|
};
|
|
772
|
-
if (
|
|
802
|
+
if (shown.length === 0)
|
|
773
803
|
return finish('complete');
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
const safeOnly = preview.approval.pick.length === 0;
|
|
777
|
-
const planned = planSteps(preview.plan, approval, { ...opts, sweep: undefined }, safeOnly);
|
|
804
|
+
const archive = opts.archive ?? preview.archive;
|
|
805
|
+
const planned = planSteps(preview.plan, approval, { ...opts, archive, sweep: undefined });
|
|
778
806
|
opts.onNotes?.(planned.notes);
|
|
779
807
|
const known = new Set(receipt.dropped.map((d) => `${d.repo}\0${d.row}`));
|
|
780
808
|
for (const d of planned.dropped) {
|
|
@@ -791,10 +819,29 @@ export function run(preview, opts = {}) {
|
|
|
791
819
|
// Worktrees whose lock this run cleared. typed-git has no `worktree lock`
|
|
792
820
|
// verb, so a failed remove cannot put the lock back; the receipt says so.
|
|
793
821
|
const unlocked = new Set();
|
|
822
|
+
// Steps that failed, or were skipped for one that did, each with the failed
|
|
823
|
+
// step at the root of it.
|
|
824
|
+
const blocked = [];
|
|
825
|
+
const stamps = new Map();
|
|
826
|
+
const measure = (path) => sizeOf(path, { du: opts.du, timeoutMs: opts.sizeTimeoutMs });
|
|
794
827
|
for (const [i, [step, a]] of plannedSteps.entries()) {
|
|
828
|
+
const cause = blocked.find((b) => dependsOn(step, a, b.step, b.a));
|
|
829
|
+
if (cause !== undefined) {
|
|
830
|
+
step.result = 'skipped';
|
|
831
|
+
step.error = `skipped: depends on ${cause.root.action} of ${cause.root.row}, which failed`;
|
|
832
|
+
blocked.push({ step, a, root: cause.root });
|
|
833
|
+
opts.onStepDone?.(i, plannedSteps.length, step);
|
|
834
|
+
save();
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
795
837
|
opts.onStep?.(i, plannedSteps.length, step);
|
|
796
838
|
try {
|
|
797
|
-
const done = execute(step, a, git, probe, removedHere
|
|
839
|
+
const done = execute(step, a, git, probe, removedHere, measure, {
|
|
840
|
+
job: planned.archives.get(step),
|
|
841
|
+
options: archive,
|
|
842
|
+
now: now(),
|
|
843
|
+
stamps,
|
|
844
|
+
});
|
|
798
845
|
if (done instanceof Gone) {
|
|
799
846
|
step.verification = done.verification;
|
|
800
847
|
step.gone = true;
|
|
@@ -815,10 +862,17 @@ export function run(preview, opts = {}) {
|
|
|
815
862
|
// Told before the save, so a receipt that cannot be written still
|
|
816
863
|
// leaves the step's outcome on screen.
|
|
817
864
|
opts.onStepDone?.(i, plannedSteps.length, step);
|
|
818
|
-
|
|
865
|
+
// A failed spared-tip check voids the plan's premise: nothing else runs.
|
|
866
|
+
if (step.action === 'verify-remote')
|
|
867
|
+
return finish('failed');
|
|
868
|
+
blocked.push({ step, a, root: step });
|
|
869
|
+
save();
|
|
870
|
+
continue;
|
|
819
871
|
}
|
|
820
872
|
opts.onStepDone?.(i, plannedSteps.length, step);
|
|
821
873
|
save();
|
|
822
874
|
}
|
|
875
|
+
if (blocked.length > 0)
|
|
876
|
+
return finish('failed');
|
|
823
877
|
return finish('complete');
|
|
824
878
|
}
|