@osdlc/repo-cleaner 0.20.5 → 0.24.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 +92 -0
- package/README.md +562 -167
- 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/candidates.d.ts +42 -0
- package/dist/candidates.js +42 -0
- package/dist/clean.d.ts +66 -70
- package/dist/clean.js +386 -218
- package/dist/cli.d.ts +16 -10
- package/dist/cli.js +273 -102
- package/dist/discover.d.ts +28 -7
- package/dist/discover.js +185 -42
- package/dist/index.d.ts +12 -5
- package/dist/index.js +11 -4
- 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 +99 -51
- 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 +81 -14
- package/dist/resolve.d.ts +12 -1
- package/dist/resolve.js +15 -3
- 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 +50 -18
- package/dist/sweep.js +152 -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,166 +26,43 @@
|
|
|
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 { isAbsolute, join, resolve } from 'node:path';
|
|
29
|
+
import { existsSync } from 'node:fs';
|
|
30
|
+
import { isAbsolute, join, resolve, sep } 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, makeRow, 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
48
|
/**
|
|
43
|
-
* `
|
|
44
|
-
*
|
|
45
|
-
*
|
|
49
|
+
* What `row` needs allowed, given `isAllowed`: the risks not granted
|
|
50
|
+
* (`missing`, phrased as asked, `discard-archived` where the archive covers
|
|
51
|
+
* one), the risks to ask for (`needs`), and whether the remove may discard
|
|
52
|
+
* changes or abandon unmerged work.
|
|
46
53
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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)));
|
|
54
|
+
function judgeRisks(row, isAllowed, archive, gitAt) {
|
|
55
|
+
const coverage = archive === undefined ? null : coverageOf(row, archive, gitAt);
|
|
56
|
+
// `discard-archived` grants the risks the archive covers, and only those.
|
|
57
|
+
const archived = (risk) => coverage?.covers.includes(risk) === true && isAllowed('discard-archived');
|
|
58
|
+
const grants = (risk) => isAllowed(risk) || archived(risk);
|
|
59
|
+
const asked = (risk) => (coverage?.covers.includes(risk) ? 'discard-archived' : risk);
|
|
60
|
+
return {
|
|
61
|
+
needs: [...new Set(row.risks.map(asked))],
|
|
62
|
+
missing: [...new Set(row.risks.filter((risk) => !grants(risk)).map(asked))],
|
|
63
|
+
discard: grants('discard-changes'),
|
|
64
|
+
abandon: grants('abandon-unmerged'),
|
|
65
|
+
};
|
|
186
66
|
}
|
|
187
67
|
/**
|
|
188
68
|
* The plan repository's rows as they stand now, annotated as the sweep
|
|
@@ -195,14 +75,18 @@ function resweep(repo, plan, kinds, opts) {
|
|
|
195
75
|
// An offline sweep read remote tips from disk, so it cannot stand in for
|
|
196
76
|
// the re-check of remote rows: sweep again and ask the remote, so the
|
|
197
77
|
// preview shows what the run will do.
|
|
198
|
-
const readFromDisk = kinds.includes('remote') &&
|
|
78
|
+
const readFromDisk = kinds.includes('remote') &&
|
|
79
|
+
(given?.repo === undefined ||
|
|
80
|
+
given.repo === null ||
|
|
81
|
+
!judgedRemotes(given.repo).every((r) => given.repo.fetchedRemotes.includes(r.name)));
|
|
199
82
|
if (given !== undefined && given.error === null && !readFromDisk) {
|
|
200
83
|
// Its notes are reported with the rest of that sweep's, by `recheck`.
|
|
201
|
-
return { rows: holdFreedBranches([...given.rows], branchScoped), notes: [] };
|
|
84
|
+
return { rows: holdFreedBranches([...given.rows], branchScoped), notes: [], info: given.repo };
|
|
202
85
|
}
|
|
203
86
|
const swept = sweepRepo(repo.root, {
|
|
204
87
|
...opts,
|
|
205
88
|
only: kinds,
|
|
89
|
+
remotes: repo.remotes,
|
|
206
90
|
against: plan.options.against ?? undefined,
|
|
207
91
|
provider: plan.options.provider,
|
|
208
92
|
fetch: repo.fetched,
|
|
@@ -215,23 +99,23 @@ function resweep(repo, plan, kinds, opts) {
|
|
|
215
99
|
if (swept.repo === null || swept.error !== null) {
|
|
216
100
|
throw new Error(`${repo.root}: ${swept.error ?? 'could not read the repository'}`);
|
|
217
101
|
}
|
|
218
|
-
return { rows: swept.rows, notes: swept.notes };
|
|
102
|
+
return { rows: swept.rows, notes: swept.notes, info: swept.repo };
|
|
219
103
|
}
|
|
220
104
|
/**
|
|
221
|
-
* Re-sweep, then split the selection into rows to run and rows dropped
|
|
222
|
-
*
|
|
223
|
-
*
|
|
105
|
+
* Re-sweep, then split the selection into rows to run and rows dropped: the
|
|
106
|
+
* selected rows, or every row that could run, and the picked rows, less the
|
|
107
|
+
* spared. A row no one picked or selected by name runs only while the
|
|
108
|
+
* re-sweep judges it `delete`.
|
|
224
109
|
*/
|
|
225
|
-
function recheck(plan, approval, opts
|
|
226
|
-
const
|
|
227
|
-
|
|
110
|
+
function recheck(plan, approval, opts) {
|
|
111
|
+
const chosen = new Set([...(approval.select ?? []), ...approval.pick].map(keyOf));
|
|
112
|
+
const mustDelete = (s) => !chosen.has(keyOf(s));
|
|
113
|
+
const spared = new Set([...approval.spare, ...approval.spareMatched].map(keyOf));
|
|
114
|
+
// By default every row that could run: a plan also holds the blocked and
|
|
228
115
|
// 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;
|
|
116
|
+
const base = approval.select ??
|
|
117
|
+
plan.repos.flatMap((r) => r.rows.filter(runnable).map((row) => ({ repo: r.root, id: row.id })));
|
|
118
|
+
const selected = uniq([...base, ...approval.pick]).filter((s) => !spared.has(keyOf(s)));
|
|
235
119
|
const all = [...selected, ...approval.spare];
|
|
236
120
|
const plannedRows = new Map(plan.repos.flatMap((r) => r.rows.map((row) => [keyOf({ repo: r.root, id: row.id }), row])));
|
|
237
121
|
const kindsIn = new Map();
|
|
@@ -241,6 +125,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
241
125
|
kindsIn.set(s.repo, kinds);
|
|
242
126
|
}
|
|
243
127
|
const currentRows = new Map();
|
|
128
|
+
const infos = new Map();
|
|
244
129
|
const notes = [];
|
|
245
130
|
for (const [root, wanted] of kindsIn) {
|
|
246
131
|
const repo = plan.repos.find((r) => r.root === root);
|
|
@@ -250,6 +135,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
250
135
|
if (wanted.has('branch') && plan.options.only.includes('worktree'))
|
|
251
136
|
wanted.add('worktree');
|
|
252
137
|
const now = resweep(repo, plan, KINDS.filter((k) => wanted.has(k)), opts);
|
|
138
|
+
infos.set(root, now.info);
|
|
253
139
|
for (const row of now.rows)
|
|
254
140
|
currentRows.set(keyOf({ repo: root, id: row.id }), row);
|
|
255
141
|
for (const note of now.notes)
|
|
@@ -275,7 +161,14 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
275
161
|
if (typeof plannedSha !== 'string' || row.evidence.sha !== plannedSha) {
|
|
276
162
|
throw new RepoCleanerError('spared-changed', `spared ${s.id} moved since the plan (planned ${String(plannedSha)}, now ${row.evidence.sha}); refusing to run`);
|
|
277
163
|
}
|
|
278
|
-
verified.push({
|
|
164
|
+
verified.push({
|
|
165
|
+
repo: s.repo,
|
|
166
|
+
row,
|
|
167
|
+
discard: false,
|
|
168
|
+
abandon: false,
|
|
169
|
+
discardNamed: false,
|
|
170
|
+
needs: [],
|
|
171
|
+
});
|
|
279
172
|
}
|
|
280
173
|
const accepted = [];
|
|
281
174
|
const dropped = [];
|
|
@@ -292,7 +185,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
292
185
|
drop(row.verdict === 'blocked' ? 'blocked' : 'keep', row.why);
|
|
293
186
|
continue;
|
|
294
187
|
}
|
|
295
|
-
if (
|
|
188
|
+
if (mustDelete(s) && row.verdict !== 'delete') {
|
|
296
189
|
drop('needs-pick', `${row.verdict}: ${row.why}; pick it to run it`);
|
|
297
190
|
continue;
|
|
298
191
|
}
|
|
@@ -302,22 +195,51 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
302
195
|
drop('changed', `planned ${JSON.stringify(was)}, now ${JSON.stringify(now)}`);
|
|
303
196
|
continue;
|
|
304
197
|
}
|
|
305
|
-
const
|
|
306
|
-
if (missing.length > 0) {
|
|
307
|
-
drop('not-allowed', `needs ${missing.join(', ')}`);
|
|
198
|
+
const judged = judgeRisks(row, (risk) => allowed(approval.allow, s, risk), opts.archive, (path) => new Git(path, { runner: opts.git }));
|
|
199
|
+
if (judged.missing.length > 0) {
|
|
200
|
+
drop('not-allowed', `needs ${judged.missing.join(', ')}`);
|
|
308
201
|
continue;
|
|
309
202
|
}
|
|
310
203
|
accepted.push({
|
|
311
204
|
repo: s.repo,
|
|
312
205
|
row,
|
|
313
|
-
discard:
|
|
314
|
-
abandon:
|
|
206
|
+
discard: judged.discard,
|
|
207
|
+
abandon: judged.abandon,
|
|
208
|
+
discardNamed: allowed(approval.allow, s, 'discard-changes'),
|
|
209
|
+
needs: judged.needs,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
// A worktree judged safe because remote branches hold its HEAD loses that
|
|
213
|
+
// cover when the same run deletes every one of them.
|
|
214
|
+
const deletedRemotes = new Set(accepted.flatMap((a) => a.row.kind === 'remote'
|
|
215
|
+
? [`${a.repo}\0${a.row.evidence.remote}/${a.row.evidence.branch}`]
|
|
216
|
+
: []));
|
|
217
|
+
const coverGone = (a) => {
|
|
218
|
+
if (a.row.kind !== 'worktree' || a.abandon)
|
|
219
|
+
return null;
|
|
220
|
+
const from = a.row.evidence.recoverableFrom ?? [];
|
|
221
|
+
return from.length > 0 && from.every((name) => deletedRemotes.has(`${a.repo}\0${name}`))
|
|
222
|
+
? from
|
|
223
|
+
: null;
|
|
224
|
+
};
|
|
225
|
+
const stillCovered = [];
|
|
226
|
+
for (const a of accepted) {
|
|
227
|
+
const from = coverGone(a);
|
|
228
|
+
if (from === null) {
|
|
229
|
+
stillCovered.push(a);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
dropped.push({
|
|
233
|
+
repo: a.repo,
|
|
234
|
+
row: a.row.id,
|
|
235
|
+
reason: 'not-allowed',
|
|
236
|
+
detail: `needs abandon-unmerged: this run also deletes ${from.join(', ')}, the only remote copies of its HEAD`,
|
|
315
237
|
});
|
|
316
238
|
}
|
|
317
239
|
// A branch freed by its worktree's removal goes only with that worktree.
|
|
318
|
-
const leaving = new Set(
|
|
240
|
+
const leaving = new Set(stillCovered.filter((a) => a.row.kind === 'worktree').map((a) => a.row.name));
|
|
319
241
|
const kept = [];
|
|
320
|
-
for (const a of
|
|
242
|
+
for (const a of stillCovered) {
|
|
321
243
|
const worktree = a.row.kind === 'branch' ? a.row.evidence.worktree : undefined;
|
|
322
244
|
if (worktree === undefined || leaving.has(worktree)) {
|
|
323
245
|
kept.push(a);
|
|
@@ -336,6 +258,7 @@ function recheck(plan, approval, opts, safeOnly) {
|
|
|
336
258
|
accepted: kept,
|
|
337
259
|
dropped,
|
|
338
260
|
verified,
|
|
261
|
+
infos,
|
|
339
262
|
notes: opts.sweep === undefined ? notes : uniqueNotes([...sweepNotes(opts.sweep), ...notes]),
|
|
340
263
|
};
|
|
341
264
|
}
|
|
@@ -480,10 +403,10 @@ function stepsFor({ repo, row }) {
|
|
|
480
403
|
];
|
|
481
404
|
}
|
|
482
405
|
}
|
|
483
|
-
function planSteps(plan, approval, opts
|
|
406
|
+
function planSteps(plan, approval, opts) {
|
|
484
407
|
const cwd = opts.cwd ?? process.cwd();
|
|
485
408
|
const git = new Git(cwd, { runner: opts.git });
|
|
486
|
-
const rechecked = recheck(plan, approval, opts
|
|
409
|
+
const rechecked = recheck(plan, approval, opts);
|
|
487
410
|
const unique = onePerRemoteBranch(rechecked, git);
|
|
488
411
|
const dropped = [...unique.dropped];
|
|
489
412
|
// A worktree whose directory is left without its `.git` goes only by the
|
|
@@ -509,12 +432,34 @@ function planSteps(plan, approval, opts, safeOnly) {
|
|
|
509
432
|
const of = accepted.filter((a) => a.row.kind === kind);
|
|
510
433
|
return [...of.filter((a) => !prunesAll(a.row)), ...of.filter((a) => prunesAll(a.row))];
|
|
511
434
|
});
|
|
435
|
+
const archives = new Map();
|
|
436
|
+
const archiveSteps = [];
|
|
437
|
+
if (opts.archive !== undefined) {
|
|
438
|
+
for (const root of new Set(ordered.map((a) => a.repo))) {
|
|
439
|
+
const members = ordered.filter((a) => a.repo === root && mayContribute(a.row));
|
|
440
|
+
if (members.length === 0)
|
|
441
|
+
continue;
|
|
442
|
+
const info = rechecked.infos.get(root);
|
|
443
|
+
if (info.remote === null) {
|
|
444
|
+
throw new RepoCleanerError('conflict', `archive: ${root} has no remote to push the archive to`);
|
|
445
|
+
}
|
|
446
|
+
const step = pending(root, members[0].row.id, 'archive', shellJoin(['git', 'push', info.remote, 'refs/heads/archive/<repo>-local-<date>']));
|
|
447
|
+
archives.set(step, {
|
|
448
|
+
remote: info.remote,
|
|
449
|
+
defaultRef: refOf(landedFor(info)),
|
|
450
|
+
jobs: members.map((a) => ({ id: a.row.id, row: a.row, discardAllowed: a.discardNamed })),
|
|
451
|
+
});
|
|
452
|
+
archiveSteps.push([step, null]);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
512
455
|
const steps = [
|
|
513
456
|
// Spared tips first: one that moved stops the run before anything is deleted.
|
|
514
457
|
...rechecked.verified.map((a) => [verifyStep(a), a]),
|
|
458
|
+
// Then the archives: a push that fails stops the run before any delete.
|
|
459
|
+
...archiveSteps,
|
|
515
460
|
...ordered.flatMap((a) => stepsFor(a).map((step) => [step, a])),
|
|
516
461
|
];
|
|
517
|
-
return { steps, dropped, accepted: ordered, removedHere, notes: rechecked.notes };
|
|
462
|
+
return { steps, dropped, accepted: ordered, removedHere, notes: rechecked.notes, archives };
|
|
518
463
|
}
|
|
519
464
|
/** A worktree row whose deletion takes the repository-wide prune: its directory is left without its `.git`. */
|
|
520
465
|
function prunesAll(row) {
|
|
@@ -556,14 +501,25 @@ export function check(plan, approval, opts = {}) {
|
|
|
556
501
|
const cwd = opts.cwd ?? process.cwd();
|
|
557
502
|
checkMirrors(plan, opts.cacheDir ?? defaultCacheDir());
|
|
558
503
|
const resolved = resolveApproval(plan, approval, cwd);
|
|
559
|
-
|
|
504
|
+
if (opts.archive === undefined && resolved.allow.some((a) => a.risk === 'discard-archived')) {
|
|
505
|
+
throw new RepoCleanerError('conflict', '--allow discard-archived has no effect without archive');
|
|
506
|
+
}
|
|
507
|
+
const { steps, dropped, accepted, notes } = planSteps(plan, resolved, opts);
|
|
560
508
|
for (const [step] of steps)
|
|
561
509
|
step.result = 'would-run';
|
|
562
510
|
const unusedAllows = resolved.allow
|
|
563
|
-
.filter((a) => !accepted.some((row) => row.row.risks.includes(a.risk) &&
|
|
511
|
+
.filter((a) => !accepted.some((row) => (row.needs.includes(a.risk) || row.row.risks.includes(a.risk)) &&
|
|
564
512
|
(a.row === null || same(a.row, { repo: row.repo, id: row.row.id }))))
|
|
565
513
|
.map((a) => ({ risk: a.risk, row: a.row?.id ?? null }));
|
|
566
|
-
return {
|
|
514
|
+
return {
|
|
515
|
+
plan,
|
|
516
|
+
approval: resolved,
|
|
517
|
+
steps: steps.map(([s]) => s),
|
|
518
|
+
dropped,
|
|
519
|
+
unusedAllows,
|
|
520
|
+
notes,
|
|
521
|
+
archive: opts.archive,
|
|
522
|
+
};
|
|
567
523
|
}
|
|
568
524
|
// ── the run ────────────────────────────────────────────────────────────────
|
|
569
525
|
/**
|
|
@@ -602,6 +558,50 @@ function assertSafeToForce(path, git, a) {
|
|
|
602
558
|
if (reasons.length > 0)
|
|
603
559
|
throw new Error(`refused: ${reasons.join('; ')}`);
|
|
604
560
|
}
|
|
561
|
+
/**
|
|
562
|
+
* Whether `step` cannot run once `prior`, an earlier step, failed or was
|
|
563
|
+
* skipped. Only steps in the same repository can depend on each other, and
|
|
564
|
+
* `a` and `priorA` are the rows the two steps act on:
|
|
565
|
+
*
|
|
566
|
+
* - every non-archive step waits on a failed archive push in its repository;
|
|
567
|
+
* - a worktree's remove or prune waits on its unlock;
|
|
568
|
+
* - a branch delete waits on the removal of the worktree that holds the branch;
|
|
569
|
+
* - the repository-wide prune waits on every worktree step in the repository,
|
|
570
|
+
* since it counted on each removal;
|
|
571
|
+
* - a remote delete waits on the delete of the same branch's local copy.
|
|
572
|
+
*/
|
|
573
|
+
function dependsOn(step, a, prior, priorA) {
|
|
574
|
+
// A failed archive push stops the repository's deletes: nothing is
|
|
575
|
+
// removed that was not saved.
|
|
576
|
+
if (prior.action === 'archive' && step.action !== 'archive' && step.repo === prior.repo) {
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
if (a === null || priorA === null || step.repo !== prior.repo)
|
|
580
|
+
return false;
|
|
581
|
+
const row = a.row;
|
|
582
|
+
const was = priorA.row;
|
|
583
|
+
const worktreeStep = (s) => s.action === 'unlock-worktree' ||
|
|
584
|
+
s.action === 'remove-worktree' ||
|
|
585
|
+
s.action === 'prune-worktrees';
|
|
586
|
+
if (step.action === 'remove-worktree' || step.action === 'prune-worktrees') {
|
|
587
|
+
if (prior.action === 'unlock-worktree' && row.id === was.id)
|
|
588
|
+
return true;
|
|
589
|
+
return prunesAll(row) && worktreeStep(prior);
|
|
590
|
+
}
|
|
591
|
+
if (step.action === 'delete-branch') {
|
|
592
|
+
return (worktreeStep(prior) &&
|
|
593
|
+
was.kind === 'worktree' &&
|
|
594
|
+
row.kind === 'branch' &&
|
|
595
|
+
was.evidence.branch === row.name);
|
|
596
|
+
}
|
|
597
|
+
if (step.action === 'delete-remote') {
|
|
598
|
+
return (prior.action === 'delete-branch' &&
|
|
599
|
+
row.kind === 'remote' &&
|
|
600
|
+
was.kind === 'branch' &&
|
|
601
|
+
row.evidence.branch === was.name);
|
|
602
|
+
}
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
605
|
/** A remote step that found the branch already gone: its verification, marked so. */
|
|
606
606
|
class Gone {
|
|
607
607
|
verification;
|
|
@@ -612,12 +612,29 @@ class Gone {
|
|
|
612
612
|
/**
|
|
613
613
|
* Run one step, then confirm it took. Throws with the reason on failure.
|
|
614
614
|
*/
|
|
615
|
-
function execute(step, a, git, probe, removedHere
|
|
615
|
+
function execute(step, a, git, probe, removedHere,
|
|
616
|
+
/** A directory's size on disk, null when it could not be read. */
|
|
617
|
+
measure, archive) {
|
|
616
618
|
const at = git.at(step.repo);
|
|
617
619
|
const remoteTip = (remote, ref) => at.lsRemote(remote, ref).find((r) => r.ref === ref);
|
|
618
620
|
const registered = (path) => at.worktrees().some((w) => resolve(w.path) === resolve(path));
|
|
619
621
|
const row = a?.row;
|
|
620
622
|
switch (step.action) {
|
|
623
|
+
case 'archive': {
|
|
624
|
+
if (archive.job === undefined || archive.options === undefined) {
|
|
625
|
+
throw new Error('archive without its rows');
|
|
626
|
+
}
|
|
627
|
+
const { remote, defaultRef, jobs } = archive.job;
|
|
628
|
+
const pushed = pushArchive(at, remote, defaultRef, jobs, archive.options, archive.now);
|
|
629
|
+
if (pushed === null)
|
|
630
|
+
return 'nothing to archive';
|
|
631
|
+
const { record } = pushed;
|
|
632
|
+
for (const [id, stamp] of pushed.stamps)
|
|
633
|
+
archive.stamps.set(`${step.repo}\0${id}`, stamp);
|
|
634
|
+
step.archive = record;
|
|
635
|
+
step.command = shellJoin(['git', 'push', remote, `${record.sha}:${record.ref}`]);
|
|
636
|
+
return `${record.ref} lists ${record.sha}`;
|
|
637
|
+
}
|
|
621
638
|
case 'unlock-worktree': {
|
|
622
639
|
if (row?.kind !== 'worktree')
|
|
623
640
|
throw new Error('unlock without a worktree');
|
|
@@ -663,14 +680,24 @@ function execute(step, a, git, probe, removedHere) {
|
|
|
663
680
|
throw new Error('remove without a worktree');
|
|
664
681
|
const path = row.name;
|
|
665
682
|
assertNoLiveProcess(path, probe);
|
|
683
|
+
const stamp = archive.stamps.get(`${step.repo}\0${row.id}`);
|
|
684
|
+
if (stamp !== undefined &&
|
|
685
|
+
stamp !==
|
|
686
|
+
worktreeStamp(at.at(path), archive.options, row.risks.includes('discard-changes'))) {
|
|
687
|
+
throw new Error(`refused: ${path} changed since it was archived`);
|
|
688
|
+
}
|
|
666
689
|
const force = removeIsForced(row.evidence);
|
|
667
690
|
if (force)
|
|
668
691
|
assertSafeToForce(path, git, a);
|
|
692
|
+
// Measured last, just before the remove: what this step frees. Recorded
|
|
693
|
+
// only once the remove took; a failed remove frees nothing.
|
|
694
|
+
const bytes = measure(path);
|
|
669
695
|
at.worktreeRemove(path, { force });
|
|
670
696
|
if (existsSync(path))
|
|
671
697
|
throw new Error(`directory still exists: ${path}`);
|
|
672
698
|
if (registered(path))
|
|
673
699
|
throw new Error(`still registered: ${path}`);
|
|
700
|
+
step.freedBytes = bytes;
|
|
674
701
|
return 'directory gone and unregistered';
|
|
675
702
|
}
|
|
676
703
|
case 'delete-branch': {
|
|
@@ -728,11 +755,39 @@ function execute(step, a, git, probe, removedHere) {
|
|
|
728
755
|
}
|
|
729
756
|
}
|
|
730
757
|
}
|
|
758
|
+
/** Run one step and record how it went on the step: its `result`, `verification` and `error`. Never throws. */
|
|
759
|
+
function runStep(step, a, ctx, archive) {
|
|
760
|
+
try {
|
|
761
|
+
const done = execute(step, a, ctx.git, ctx.probe, ctx.removedHere, ctx.measure, {
|
|
762
|
+
...archive,
|
|
763
|
+
stamps: ctx.stamps,
|
|
764
|
+
});
|
|
765
|
+
if (done instanceof Gone) {
|
|
766
|
+
step.verification = done.verification;
|
|
767
|
+
step.gone = true;
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
step.verification = done;
|
|
771
|
+
}
|
|
772
|
+
step.result = 'ok';
|
|
773
|
+
if (step.action === 'unlock-worktree')
|
|
774
|
+
ctx.unlocked.add(a.row.name);
|
|
775
|
+
}
|
|
776
|
+
catch (err) {
|
|
777
|
+
step.result = 'failed';
|
|
778
|
+
step.error = errorText(err);
|
|
779
|
+
if (a !== null && step.action !== 'unlock-worktree' && ctx.unlocked.has(a.row.name)) {
|
|
780
|
+
step.error += `; the lock on ${a.row.name} was cleared earlier in this run and is not restored`;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
731
784
|
/**
|
|
732
785
|
* Take the steps `preview` showed: sweeps again, drops a row that moved since
|
|
733
786
|
* the preview, and runs the rest in order, compare-and-swap, verifying each
|
|
734
|
-
* step
|
|
735
|
-
*
|
|
787
|
+
* step. A failed step skips only the steps that depend on it, except a failed
|
|
788
|
+
* spared-tip check, which stops the run. Only what the preview showed runs; a
|
|
789
|
+
* row it dropped stays dropped. Each worktree directory it removes is measured
|
|
790
|
+
* with `du` just before, into that step's `freedBytes`.
|
|
736
791
|
*
|
|
737
792
|
* @throws RepoCleanerError (`spared-changed`) when a spared remote branch moved
|
|
738
793
|
* since the preview, and a plain `Error` when the re-check cannot read a
|
|
@@ -743,12 +798,23 @@ export function run(preview, opts = {}) {
|
|
|
743
798
|
const cwd = opts.cwd ?? process.cwd();
|
|
744
799
|
// Only the rows the preview would run, pinned to their repositories. A
|
|
745
800
|
// 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
|
-
|
|
801
|
+
const shown = uniq(preview.steps.flatMap((s) => s.action === 'verify-remote' || s.action === 'archive' ? [] : [{ repo: s.repo, id: s.row }]));
|
|
802
|
+
// The same approval, narrowed to what the preview showed: the rows chosen
|
|
803
|
+
// by name stay picked, every other runnable row it did not show is left
|
|
804
|
+
// alone, and the rest run by the default rule, so a row the preview ran only
|
|
805
|
+
// for its `delete` verdict drops if the re-check now judges it `review`.
|
|
806
|
+
const shownKeys = new Set(shown.map(keyOf));
|
|
807
|
+
const named = new Set([...(preview.approval.select ?? []), ...preview.approval.pick].map(keyOf));
|
|
808
|
+
const unshown = preview.plan.repos.flatMap((r) => r.rows
|
|
809
|
+
.filter(runnable)
|
|
810
|
+
.map((row) => ({ repo: r.root, id: row.id }))
|
|
811
|
+
.filter((s) => !shownKeys.has(keyOf(s))));
|
|
748
812
|
const approval = {
|
|
749
|
-
|
|
813
|
+
select: null,
|
|
814
|
+
pick: shown.filter((s) => named.has(keyOf(s))),
|
|
750
815
|
spare: preview.approval.spare,
|
|
751
|
-
|
|
816
|
+
spareMatched: uniq([...preview.approval.spareMatched, ...unshown]),
|
|
817
|
+
allow: preview.approval.allow.filter((a) => a.row === null || shown.some((p) => same(p, a.row))),
|
|
752
818
|
};
|
|
753
819
|
const receipt = {
|
|
754
820
|
version: RECEIPT_VERSION,
|
|
@@ -769,12 +835,10 @@ export function run(preview, opts = {}) {
|
|
|
769
835
|
save();
|
|
770
836
|
return receipt;
|
|
771
837
|
};
|
|
772
|
-
if (
|
|
838
|
+
if (shown.length === 0)
|
|
773
839
|
return finish('complete');
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
const safeOnly = preview.approval.pick.length === 0;
|
|
777
|
-
const planned = planSteps(preview.plan, approval, { ...opts, sweep: undefined }, safeOnly);
|
|
840
|
+
const archive = opts.archive ?? preview.archive;
|
|
841
|
+
const planned = planSteps(preview.plan, approval, { ...opts, archive, sweep: undefined });
|
|
778
842
|
opts.onNotes?.(planned.notes);
|
|
779
843
|
const known = new Set(receipt.dropped.map((d) => `${d.repo}\0${d.row}`));
|
|
780
844
|
for (const d of planned.dropped) {
|
|
@@ -788,37 +852,141 @@ export function run(preview, opts = {}) {
|
|
|
788
852
|
const git = new Git(cwd, { runner: opts.git });
|
|
789
853
|
const probe = opts.probe ?? defaultProcessProbe();
|
|
790
854
|
const { removedHere } = planned;
|
|
791
|
-
// Worktrees whose lock this run cleared. typed-git has no `worktree lock`
|
|
792
|
-
// verb, so a failed remove cannot put the lock back; the receipt says so.
|
|
793
855
|
const unlocked = new Set();
|
|
856
|
+
// Steps that failed, or were skipped for one that did, each with the failed
|
|
857
|
+
// step at the root of it.
|
|
858
|
+
const blocked = [];
|
|
859
|
+
const stamps = new Map();
|
|
860
|
+
const measure = (path) => sizeOf(path, { du: opts.du, timeoutMs: opts.sizeTimeoutMs });
|
|
861
|
+
const ctx = { git, probe, removedHere, measure, unlocked, stamps };
|
|
794
862
|
for (const [i, [step, a]] of plannedSteps.entries()) {
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
step.gone = true;
|
|
801
|
-
}
|
|
802
|
-
else {
|
|
803
|
-
step.verification = done;
|
|
804
|
-
}
|
|
805
|
-
step.result = 'ok';
|
|
806
|
-
if (step.action === 'unlock-worktree')
|
|
807
|
-
unlocked.add(a.row.name);
|
|
808
|
-
}
|
|
809
|
-
catch (err) {
|
|
810
|
-
step.result = 'failed';
|
|
811
|
-
step.error = errorText(err);
|
|
812
|
-
if (a !== null && step.action !== 'unlock-worktree' && unlocked.has(a.row.name)) {
|
|
813
|
-
step.error += `; the lock on ${a.row.name} was cleared earlier in this run and is not restored`;
|
|
814
|
-
}
|
|
815
|
-
// Told before the save, so a receipt that cannot be written still
|
|
816
|
-
// leaves the step's outcome on screen.
|
|
863
|
+
const cause = blocked.find((b) => dependsOn(step, a, b.step, b.a));
|
|
864
|
+
if (cause !== undefined) {
|
|
865
|
+
step.result = 'skipped';
|
|
866
|
+
step.error = `skipped: depends on ${cause.root.action} of ${cause.root.row}, which failed`;
|
|
867
|
+
blocked.push({ step, a, root: cause.root });
|
|
817
868
|
opts.onStepDone?.(i, plannedSteps.length, step);
|
|
818
|
-
|
|
869
|
+
save();
|
|
870
|
+
continue;
|
|
819
871
|
}
|
|
872
|
+
opts.onStep?.(i, plannedSteps.length, step);
|
|
873
|
+
runStep(step, a, ctx, { job: planned.archives.get(step), options: archive, now: now() });
|
|
874
|
+
// Told before the save, so a receipt that cannot be written still
|
|
875
|
+
// leaves the step's outcome on screen.
|
|
820
876
|
opts.onStepDone?.(i, plannedSteps.length, step);
|
|
877
|
+
if (step.result === 'failed') {
|
|
878
|
+
// A failed spared-tip check voids the plan's premise: nothing else runs.
|
|
879
|
+
if (step.action === 'verify-remote')
|
|
880
|
+
return finish('failed');
|
|
881
|
+
blocked.push({ step, a, root: step });
|
|
882
|
+
}
|
|
821
883
|
save();
|
|
822
884
|
}
|
|
885
|
+
if (blocked.length > 0)
|
|
886
|
+
return finish('failed');
|
|
823
887
|
return finish('complete');
|
|
824
888
|
}
|
|
889
|
+
/** The row a candidate stands for. */
|
|
890
|
+
function rowOfCandidate(c) {
|
|
891
|
+
return makeRow({
|
|
892
|
+
kind: c.kind,
|
|
893
|
+
name: c.path,
|
|
894
|
+
verdict: c.verdict,
|
|
895
|
+
action: c.action,
|
|
896
|
+
why: c.why,
|
|
897
|
+
risks: c.risks,
|
|
898
|
+
blockers: c.blockers,
|
|
899
|
+
evidence: c.evidence,
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Delete one candidate as `run` takes one row: the archive first when
|
|
904
|
+
* `archive` is given and the row holds unlanded work, then the live-process
|
|
905
|
+
* and unsafe-force checks, the removal and its verification, stopping at the
|
|
906
|
+
* first failure. The caller has already re-checked the candidate; this
|
|
907
|
+
* refuses only what is unsafe now.
|
|
908
|
+
*
|
|
909
|
+
* A worktree whose removal needs `git worktree prune` is refused when the
|
|
910
|
+
* prune would also drop another worktree of its repository, since this sees
|
|
911
|
+
* only the one row.
|
|
912
|
+
*
|
|
913
|
+
* Does not throw for a refused or failed delete: see `CandidateRemoval`.
|
|
914
|
+
*/
|
|
915
|
+
export function removeCandidate(candidate, opts = {}) {
|
|
916
|
+
const now = opts.now ?? (() => new Date());
|
|
917
|
+
const refused = (error) => ({
|
|
918
|
+
steps: [],
|
|
919
|
+
ok: false,
|
|
920
|
+
archiveFailed: false,
|
|
921
|
+
error,
|
|
922
|
+
});
|
|
923
|
+
const row = rowOfCandidate(candidate);
|
|
924
|
+
if (!runnable(row))
|
|
925
|
+
return refused(`refused: ${candidate.id} ${candidate.why}`);
|
|
926
|
+
const cwd = opts.cwd ?? process.cwd();
|
|
927
|
+
// Anchored without folding `..`, as the sweep does, so a `..` after a symlink is judged where it really is.
|
|
928
|
+
const absCwd = isAbsolute(cwd) ? cwd : `${process.cwd()}${sep}${cwd}`;
|
|
929
|
+
if (row.kind === 'worktree' && nodeGuard.contains(row.name, absCwd)) {
|
|
930
|
+
return refused(`refused: ${candidate.id} is the worktree the caller stands in`);
|
|
931
|
+
}
|
|
932
|
+
const allow = new Set(opts.allow ?? []);
|
|
933
|
+
let judged;
|
|
934
|
+
try {
|
|
935
|
+
judged = judgeRisks(row, (risk) => allow.has(risk), opts.archive, (path) => new Git(path, { runner: opts.git }));
|
|
936
|
+
}
|
|
937
|
+
catch (err) {
|
|
938
|
+
return refused(`refused: ${candidate.id}: ${errorText(err)}`);
|
|
939
|
+
}
|
|
940
|
+
if (judged.missing.length > 0) {
|
|
941
|
+
return refused(`refused: ${candidate.id} needs ${judged.missing.join(', ')}`);
|
|
942
|
+
}
|
|
943
|
+
const a = {
|
|
944
|
+
repo: candidate.repo,
|
|
945
|
+
row,
|
|
946
|
+
discard: judged.discard,
|
|
947
|
+
abandon: judged.abandon,
|
|
948
|
+
discardNamed: allow.has('discard-changes'),
|
|
949
|
+
needs: judged.needs,
|
|
950
|
+
};
|
|
951
|
+
const steps = stepsFor(a).map((step) => [step, a]);
|
|
952
|
+
let job;
|
|
953
|
+
if (opts.archive !== undefined && mayContribute(row)) {
|
|
954
|
+
if (candidate.remote === null) {
|
|
955
|
+
return refused(`archive: ${candidate.repo} has no remote to push the archive to`);
|
|
956
|
+
}
|
|
957
|
+
const step = pending(candidate.repo, row.id, 'archive', shellJoin(['git', 'push', candidate.remote, 'refs/heads/archive/<repo>-local-<date>']));
|
|
958
|
+
job = {
|
|
959
|
+
remote: candidate.remote,
|
|
960
|
+
defaultRef: candidate.landedRef,
|
|
961
|
+
jobs: [{ id: row.id, row, discardAllowed: a.discardNamed }],
|
|
962
|
+
};
|
|
963
|
+
steps.unshift([step, null]);
|
|
964
|
+
}
|
|
965
|
+
const ctx = {
|
|
966
|
+
git: new Git(cwd, { runner: opts.git }),
|
|
967
|
+
probe: opts.probe ?? defaultProcessProbe(),
|
|
968
|
+
removedHere: new Set(row.kind === 'worktree' ? [resolve(row.name)] : []),
|
|
969
|
+
measure: (path) => sizeOf(path, { du: opts.du }),
|
|
970
|
+
unlocked: new Set(),
|
|
971
|
+
stamps: new Map(),
|
|
972
|
+
};
|
|
973
|
+
const taken = [];
|
|
974
|
+
for (const [step, accepted] of steps) {
|
|
975
|
+
taken.push(step);
|
|
976
|
+
runStep(step, accepted, ctx, {
|
|
977
|
+
job: step.action === 'archive' ? job : undefined,
|
|
978
|
+
options: opts.archive,
|
|
979
|
+
now: now(),
|
|
980
|
+
});
|
|
981
|
+
opts.onStepDone?.(step);
|
|
982
|
+
if (step.result === 'failed') {
|
|
983
|
+
return {
|
|
984
|
+
steps: taken,
|
|
985
|
+
ok: false,
|
|
986
|
+
archiveFailed: step.action === 'archive',
|
|
987
|
+
error: step.error ?? 'failed',
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return { steps: taken, ok: true, archiveFailed: false, error: null };
|
|
992
|
+
}
|