@osdlc/repo-cleaner 0.20.4 → 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 +72 -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/sweep.js
CHANGED
|
@@ -10,99 +10,18 @@ import { existsSync, realpathSync } from 'node:fs';
|
|
|
10
10
|
import { isAbsolute, resolve } from 'node:path';
|
|
11
11
|
import { Git, checkoutRoot } from '@osdlc/typed-git';
|
|
12
12
|
import { Gh } from '@osdlc/typed-gh';
|
|
13
|
-
import { HELD_BY_WORKTREE, gatherLocalBranches, gatherRemoteBranches
|
|
14
|
-
import { discoverRepo } from "./discover.js";
|
|
13
|
+
import { HELD_BY_WORKTREE, gatherLocalBranches, gatherRemoteBranches } from "./branches.js";
|
|
14
|
+
import { discoverRepo, resolveRemoteDefaults } from "./discover.js";
|
|
15
15
|
import { defaultProcessProbe } from "./processes.js";
|
|
16
16
|
import { lookupPrs, prsUnchecked } from "./prs.js";
|
|
17
|
-
import {
|
|
18
|
-
import { defaultCacheDir, ensureMirror,
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
17
|
+
import { providers } from "./providers/index.js";
|
|
18
|
+
import { defaultCacheDir, ensureMirror, redactUrl } from "./remote-mirror.js";
|
|
19
|
+
import { sizeOf } from "./size.js";
|
|
20
|
+
import { resolveEntries } from "./resolve.js";
|
|
21
|
+
import { sweptWith } from "./scope.js";
|
|
22
|
+
import { RepoCleanerError, judgedRemotes, countRows, emptyScan, reclaimableOf, findDuplicateCheckouts, } from "./types.js";
|
|
23
|
+
import { errorText, isDir } from "./util.js";
|
|
22
24
|
import { gatherWorktrees } from "./worktrees.js";
|
|
23
|
-
const LOCAL = ['worktree', 'branch'];
|
|
24
|
-
/**
|
|
25
|
-
* The sweep `targets` and `opts` ask for, as a plan records it. Throws a
|
|
26
|
-
* {@link RepoCleanerError} for options that contradict each other or would
|
|
27
|
-
* have no effect, before anything is read.
|
|
28
|
-
*/
|
|
29
|
-
export function sweptWith(targets, opts) {
|
|
30
|
-
const cwd = opts.cwd ?? process.cwd();
|
|
31
|
-
const conflict = (message) => {
|
|
32
|
-
throw new RepoCleanerError('conflict', message);
|
|
33
|
-
};
|
|
34
|
-
if (targets.some((t) => t.trim() === '')) {
|
|
35
|
-
throw new RepoCleanerError('bad-input', 'a target is empty');
|
|
36
|
-
}
|
|
37
|
-
const only = opts.only === undefined ? undefined : [...new Set(opts.only)];
|
|
38
|
-
const unknown = (only ?? []).filter((k) => !KINDS.includes(k));
|
|
39
|
-
if (unknown.length > 0) {
|
|
40
|
-
throw new RepoCleanerError('bad-input', `unknown kind(s) ${unknown.join(', ')}: expected ${KINDS.join(', ')}`);
|
|
41
|
-
}
|
|
42
|
-
if (only !== undefined && only.length === 0) {
|
|
43
|
-
throw new RepoCleanerError('bad-input', 'only names no kind');
|
|
44
|
-
}
|
|
45
|
-
if (opts.provider !== undefined &&
|
|
46
|
-
!PROVIDER_CHOICES.includes(opts.provider)) {
|
|
47
|
-
throw new RepoCleanerError('bad-input', `unknown provider ${JSON.stringify(opts.provider)}`);
|
|
48
|
-
}
|
|
49
|
-
const exists = (t) => existsSync(resolve(cwd, expandTilde(t)));
|
|
50
|
-
const isUrl = targets.map((t) => isRemoteUrl(t, exists));
|
|
51
|
-
const urls = isUrl.some(Boolean);
|
|
52
|
-
if (urls && !isUrl.every(Boolean)) {
|
|
53
|
-
conflict('a remote URL target sweeps remote branches only; sweep it apart from checkouts');
|
|
54
|
-
}
|
|
55
|
-
if (urls && only !== undefined && only.some((k) => k !== 'remote')) {
|
|
56
|
-
conflict(`a remote URL has only remote branches; drop --only ${only.join(',')}`);
|
|
57
|
-
}
|
|
58
|
-
if (urls && opts.branch !== undefined) {
|
|
59
|
-
conflict('--branch reads one branch of a checkout; it does not take a remote URL');
|
|
60
|
-
}
|
|
61
|
-
if (urls && opts.offline === true) {
|
|
62
|
-
conflict('a remote URL target is fetched to be swept; --offline would have no effect');
|
|
63
|
-
}
|
|
64
|
-
if (opts.branch !== undefined && targets.length > 1) {
|
|
65
|
-
conflict(`--branch sweeps one repository; ${targets.length} targets given`);
|
|
66
|
-
}
|
|
67
|
-
const remote = urls || opts.remote === true || (only ?? []).includes('remote');
|
|
68
|
-
if (opts.remote === true && only !== undefined && !only.includes('remote')) {
|
|
69
|
-
conflict(`--remote includes remote branches, but --only ${only.join(',')} leaves them out`);
|
|
70
|
-
}
|
|
71
|
-
if (opts.offline === true && !remote) {
|
|
72
|
-
conflict('--offline has no effect without --remote: nothing is fetched anyway');
|
|
73
|
-
}
|
|
74
|
-
if (opts.provider !== undefined && !remote) {
|
|
75
|
-
conflict('--provider has no effect without --remote: pull requests are looked up only for remote branches');
|
|
76
|
-
}
|
|
77
|
-
const kinds = urls
|
|
78
|
-
? ['remote']
|
|
79
|
-
: only !== undefined
|
|
80
|
-
? KINDS.filter((k) => only.includes(k))
|
|
81
|
-
: remote
|
|
82
|
-
? [...LOCAL, 'remote']
|
|
83
|
-
: [...LOCAL];
|
|
84
|
-
// `HEAD` is the branch checked out at the target, resolved here so the
|
|
85
|
-
// options checked before the sweep are the ones the plan records.
|
|
86
|
-
let branch = opts.branch ?? null;
|
|
87
|
-
if (branch === 'HEAD') {
|
|
88
|
-
const at = targets[0] === undefined ? cwd : resolve(cwd, expandTilde(targets[0]));
|
|
89
|
-
try {
|
|
90
|
-
branch = resolveBranch('HEAD', new Git(at, { runner: opts.git }));
|
|
91
|
-
}
|
|
92
|
-
catch (err) {
|
|
93
|
-
throw new RepoCleanerError('conflict', err instanceof Error ? err.message : String(err));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return {
|
|
97
|
-
only: kinds,
|
|
98
|
-
remote,
|
|
99
|
-
offline: opts.offline === true,
|
|
100
|
-
against: opts.against ?? null,
|
|
101
|
-
provider: remote ? (opts.provider ?? 'auto') : 'none',
|
|
102
|
-
branch,
|
|
103
|
-
urls,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
25
|
/**
|
|
107
26
|
* Whether the checkout `dir` is in tracks any file under it. True as well when
|
|
108
27
|
* git cannot say: then widening to that checkout reports why it cannot be
|
|
@@ -171,9 +90,10 @@ export function sweep(targets, opts = {}) {
|
|
|
171
90
|
...resolution.repos.map((root, i) => sweepRepo(root, {
|
|
172
91
|
...opts,
|
|
173
92
|
only: options.only,
|
|
93
|
+
remotes: options.remotes,
|
|
174
94
|
against: options.against ?? undefined,
|
|
175
95
|
provider: options.provider,
|
|
176
|
-
fetch: options.
|
|
96
|
+
fetch: options.offline ? [] : options.fetch ? 'local' : 'scope',
|
|
177
97
|
branch,
|
|
178
98
|
cwd,
|
|
179
99
|
position: { index: i + 1, of },
|
|
@@ -185,6 +105,7 @@ export function sweep(targets, opts = {}) {
|
|
|
185
105
|
scan: resolution.scan,
|
|
186
106
|
duplicates: findDuplicateCheckouts(repos.map((s) => s.repo)),
|
|
187
107
|
counts: countRows(repos.flatMap((s) => s.rows)),
|
|
108
|
+
reclaimableBytes: opts.sizes === true ? sumReclaimable(repos) : null,
|
|
188
109
|
options,
|
|
189
110
|
};
|
|
190
111
|
}
|
|
@@ -194,12 +115,9 @@ export function sweep(targets, opts = {}) {
|
|
|
194
115
|
* a CLI that is missing or signed out only when a remote branch could be
|
|
195
116
|
* deleted on its account, which then needs confirm-open-pr.
|
|
196
117
|
*/
|
|
197
|
-
function providerNote(
|
|
198
|
-
const status = repo.provider;
|
|
118
|
+
function providerNote(status, kinds, choice) {
|
|
199
119
|
const unchecked = status === null ? null : prsUnchecked(status);
|
|
200
|
-
|
|
201
|
-
// at; the local rows already say the lookup failed.
|
|
202
|
-
if (status === null || unchecked === null || repo.remote === null)
|
|
120
|
+
if (status === null || unchecked === null)
|
|
203
121
|
return null;
|
|
204
122
|
// A local remote names no forge, and `auto` asks GitHub for any host that
|
|
205
123
|
// is not GitLab's, so a host that is not a forge lands here too. github.com
|
|
@@ -211,6 +129,9 @@ function providerNote(repo, kinds, choice) {
|
|
|
211
129
|
if (unchecked.kind === 'list-failed') {
|
|
212
130
|
return `Listing ${status.noun}s with \`${status.listing}\` failed (${status.listError}); judged on git evidence alone.${hint}`;
|
|
213
131
|
}
|
|
132
|
+
if (unchecked.kind === 'incomplete') {
|
|
133
|
+
return `The ${status.noun} listing from \`${status.listing}\` is incomplete (${status.incomplete}), so a remote branch with no ${status.noun} found needs confirm-open-pr to delete.${hint}`;
|
|
134
|
+
}
|
|
214
135
|
if (!kinds.has('remote'))
|
|
215
136
|
return null;
|
|
216
137
|
const fix = unchecked.kind === 'not-installed'
|
|
@@ -218,12 +139,38 @@ function providerNote(repo, kinds, choice) {
|
|
|
218
139
|
: `Sign in to \`${status.tool}\``;
|
|
219
140
|
return `Could not check for open ${status.noun}s: ${unchecked.why}, so deleting a remote branch needs confirm-open-pr. ${fix}, or pass \`--allow confirm-open-pr\` once you have checked.${hint}`;
|
|
220
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Each worktree whose directory exists (by `isDir`, so one that lost its `.git`
|
|
144
|
+
* too) gets its size in its evidence; null when `du` could not say. A gone
|
|
145
|
+
* worktree has none.
|
|
146
|
+
*/
|
|
147
|
+
function readSizes(rows, opts, tick) {
|
|
148
|
+
const onDisk = rows.filter((row) => isDir(row.evidence.path));
|
|
149
|
+
for (const [i, row] of onDisk.entries()) {
|
|
150
|
+
tick(i, onDisk.length);
|
|
151
|
+
row.evidence.sizeBytes = sizeOf(row.evidence.path, {
|
|
152
|
+
du: opts.du,
|
|
153
|
+
timeoutMs: opts.sizeTimeoutMs,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** The repositories' `reclaimableBytes` added up; an unreadable one counts for nothing. */
|
|
158
|
+
function sumReclaimable(repos) {
|
|
159
|
+
const total = { delete: 0, review: 0, unsized: 0 };
|
|
160
|
+
for (const repo of repos) {
|
|
161
|
+
total.delete += repo.reclaimableBytes?.delete ?? 0;
|
|
162
|
+
total.review += repo.reclaimableBytes?.review ?? 0;
|
|
163
|
+
total.unsized += repo.reclaimableBytes?.unsized ?? 0;
|
|
164
|
+
}
|
|
165
|
+
return total;
|
|
166
|
+
}
|
|
221
167
|
function errorSweep(path, err) {
|
|
222
168
|
return {
|
|
223
169
|
path,
|
|
224
170
|
repo: null,
|
|
225
171
|
rows: [],
|
|
226
172
|
counts: countRows([]),
|
|
173
|
+
reclaimableBytes: null,
|
|
227
174
|
notes: [],
|
|
228
175
|
error: err instanceof Error ? err.message : String(err),
|
|
229
176
|
};
|
|
@@ -259,7 +206,8 @@ export function sweepRepo(path, opts) {
|
|
|
259
206
|
let repo;
|
|
260
207
|
try {
|
|
261
208
|
repo = discoverRepo(path, {
|
|
262
|
-
|
|
209
|
+
remotes: opts.remotes,
|
|
210
|
+
against: opts.against ?? null,
|
|
263
211
|
allowProvider: opts.provider !== 'none',
|
|
264
212
|
provider: opts.provider,
|
|
265
213
|
git,
|
|
@@ -276,38 +224,87 @@ export function sweepRepo(path, opts) {
|
|
|
276
224
|
}
|
|
277
225
|
// A branch sweep judges the worktree on its own evidence, wherever the caller stands.
|
|
278
226
|
const cwd = only === undefined ? opts.cwd : repo.root;
|
|
279
|
-
|
|
227
|
+
const toFetch = fetchSet(repo, opts.fetch);
|
|
228
|
+
if (toFetch.length > 0) {
|
|
280
229
|
report('fetch');
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
230
|
+
// Several remotes in one `git fetch --multiple`, fetched at once.
|
|
231
|
+
const flags = { prune: true, quiet: true, noWriteFetchHead: true };
|
|
232
|
+
const at = git.at(repo.root);
|
|
233
|
+
const fetched = toFetch.length === 1
|
|
234
|
+
? at.try.fetch(toFetch[0], undefined, flags)
|
|
235
|
+
: at.try.fetchMany(toFetch, { ...flags, jobs: Math.min(toFetch.length, FETCH_JOBS) });
|
|
284
236
|
// Unlike the lookups below, a failed fetch fails the repository: every
|
|
285
237
|
// row, local ones included, would be judged against a default branch
|
|
286
|
-
// older than the one asked for.
|
|
238
|
+
// older than the one asked for. Git names the remote that failed.
|
|
287
239
|
if (!fetched.ok) {
|
|
288
240
|
return {
|
|
289
|
-
...errorSweep(path, new Error(`fetch failed: ${errorText(fetched.error)}`)),
|
|
241
|
+
...errorSweep(path, new Error(`fetch of ${toFetch.join(', ')} failed: ${errorText(fetched.error)}`)),
|
|
290
242
|
repo,
|
|
291
243
|
};
|
|
292
244
|
}
|
|
293
|
-
|
|
245
|
+
}
|
|
246
|
+
repo = {
|
|
247
|
+
...repo,
|
|
248
|
+
fetchedRemotes: toFetch,
|
|
249
|
+
};
|
|
250
|
+
try {
|
|
251
|
+
repo = resolveRemoteDefaults(repo, git);
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
return { ...errorSweep(path, err), repo };
|
|
255
|
+
}
|
|
256
|
+
if (only !== undefined && repo.remotes.some((r) => r.defaultBranch === only)) {
|
|
257
|
+
throw new RepoCleanerError('conflict', `--branch ${only} is a remote's default branch, which is never a candidate for deletion`);
|
|
294
258
|
}
|
|
295
259
|
const notes = [];
|
|
260
|
+
if (repo.remote === null &&
|
|
261
|
+
repo.against === null &&
|
|
262
|
+
(kinds.has('worktree') || kinds.has('branch')) &&
|
|
263
|
+
repo.allRemotes.length > 0) {
|
|
264
|
+
notes.push(`No default remote (no origin, and the default branch tracks none), so local rows are judged against the local \`${repo.mainBranch}\`; pass \`--against <remote>\` to judge them against a remote.`);
|
|
265
|
+
}
|
|
296
266
|
try {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
267
|
+
// One lookup per remote: the default remote's answer serves both its
|
|
268
|
+
// own branches and the local rows. A repository with no default remote
|
|
269
|
+
// keys its local rows' lookup by ''.
|
|
270
|
+
const root = repo.root;
|
|
271
|
+
// The default remote's pull requests are for the local rows: with none in
|
|
272
|
+
// scope, it is asked only when it is judged itself.
|
|
273
|
+
const local = kinds.has('worktree') || kinds.has('branch');
|
|
274
|
+
const statuses = new Map(local ? [[repo.remote ?? '', repo.provider]] : []);
|
|
275
|
+
for (const r of judgedRemotes(repo))
|
|
276
|
+
if (!statuses.has(r.name))
|
|
277
|
+
statuses.set(r.name, r.provider);
|
|
278
|
+
const answered = new Map();
|
|
279
|
+
for (const [name, status] of statuses) {
|
|
280
|
+
if (status === null) {
|
|
281
|
+
answered.set(name, { prs: new Map(), status });
|
|
282
|
+
continue;
|
|
306
283
|
}
|
|
307
|
-
|
|
284
|
+
if (status.authenticated)
|
|
285
|
+
report('prs', undefined, undefined, status.tool);
|
|
286
|
+
const looked = lookupPrs({ root, provider: status }, known[status.name], only);
|
|
287
|
+
const after = looked.error !== null
|
|
288
|
+
? { ...status, listError: looked.error }
|
|
289
|
+
: looked.incomplete !== null
|
|
290
|
+
? { ...status, incomplete: looked.incomplete }
|
|
291
|
+
: status;
|
|
292
|
+
answered.set(name, { prs: looked.prs, status: after });
|
|
293
|
+
// With no remote there is no remote branch to gate and no forge to point
|
|
294
|
+
// at; the local rows already say the lookup failed.
|
|
295
|
+
const note = name === '' ? null : providerNote(after, kinds, opts.provider);
|
|
308
296
|
if (note !== null)
|
|
309
|
-
notes.push(note);
|
|
297
|
+
notes.push(statuses.size > 1 ? `${name}: ${note}` : note);
|
|
310
298
|
}
|
|
299
|
+
const prs = answered.get(repo.remote ?? '')?.prs ?? new Map();
|
|
300
|
+
repo = {
|
|
301
|
+
...repo,
|
|
302
|
+
provider: answered.get(repo.remote ?? '')?.status ?? repo.provider,
|
|
303
|
+
remotes: repo.remotes.map((r) => ({
|
|
304
|
+
...r,
|
|
305
|
+
provider: answered.get(r.name)?.status ?? r.provider,
|
|
306
|
+
})),
|
|
307
|
+
};
|
|
311
308
|
const rows = [];
|
|
312
309
|
// Worktrees are gathered whenever local branches are, even if they are not
|
|
313
310
|
// reported: a branch checked out in one must not be judged on its own.
|
|
@@ -317,8 +314,11 @@ export function sweepRepo(path, opts) {
|
|
|
317
314
|
;
|
|
318
315
|
[worktreeRows, worktreeBranches] = gatherWorktrees(repo, prs, cwd, git, { branch: only, path: opts.worktree }, opts.probe ?? defaultProcessProbe(), ticker('worktree'));
|
|
319
316
|
}
|
|
320
|
-
if (kinds.has('worktree'))
|
|
317
|
+
if (kinds.has('worktree')) {
|
|
318
|
+
if (opts.sizes === true)
|
|
319
|
+
readSizes(worktreeRows, opts, ticker('size'));
|
|
321
320
|
rows.push(...worktreeRows);
|
|
321
|
+
}
|
|
322
322
|
let localBranchNames = new Set();
|
|
323
323
|
if (kinds.has('branch') || kinds.has('remote')) {
|
|
324
324
|
// A branch whose worktree goes in the same run is judged on its own:
|
|
@@ -344,20 +344,30 @@ export function sweepRepo(path, opts) {
|
|
|
344
344
|
rows.push(...localRows);
|
|
345
345
|
}
|
|
346
346
|
if (kinds.has('remote')) {
|
|
347
|
-
const remote
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
347
|
+
for (const remote of judgedRemotes(repo)) {
|
|
348
|
+
const gathered = gatherRemoteBranches(repo, remote, answered.get(remote.name).prs, localBranchNames, git, {
|
|
349
|
+
live: toFetch.includes(remote.name) || opts.liveRemoteTips === true,
|
|
350
|
+
strict: opts.liveRemoteTips === true && !toFetch.includes(remote.name),
|
|
351
|
+
only,
|
|
352
|
+
tick: ticker('remote'),
|
|
353
|
+
});
|
|
354
|
+
rows.push(...gathered.rows);
|
|
355
|
+
if (gathered.note !== null)
|
|
356
|
+
notes.push(gathered.note);
|
|
357
|
+
}
|
|
356
358
|
}
|
|
357
359
|
const annotated = opts.annotate === undefined
|
|
358
360
|
? rows
|
|
359
361
|
: holdFreedBranches(opts.annotate(repo, rows).map(blockedHasNoAction), only !== undefined);
|
|
360
|
-
return {
|
|
362
|
+
return {
|
|
363
|
+
path,
|
|
364
|
+
repo,
|
|
365
|
+
rows: annotated,
|
|
366
|
+
counts: countRows(annotated),
|
|
367
|
+
reclaimableBytes: opts.sizes === true ? reclaimableOf(annotated) : null,
|
|
368
|
+
notes,
|
|
369
|
+
error: null,
|
|
370
|
+
};
|
|
361
371
|
}
|
|
362
372
|
catch (err) {
|
|
363
373
|
if (err instanceof RepoCleanerError)
|
|
@@ -425,8 +435,9 @@ export function inspectWorktree(path, opts = {}) {
|
|
|
425
435
|
const swept = sweepRepo(there ? real : cwd, {
|
|
426
436
|
...opts,
|
|
427
437
|
only: ['worktree'],
|
|
438
|
+
remotes: [],
|
|
428
439
|
provider: 'none',
|
|
429
|
-
fetch:
|
|
440
|
+
fetch: [],
|
|
430
441
|
cwd,
|
|
431
442
|
worktree: real,
|
|
432
443
|
});
|
|
@@ -438,3 +449,16 @@ export function inspectWorktree(path, opts = {}) {
|
|
|
438
449
|
}
|
|
439
450
|
return row;
|
|
440
451
|
}
|
|
452
|
+
/** The most remotes one repository fetches at once. */
|
|
453
|
+
const FETCH_JOBS = 8;
|
|
454
|
+
/** The remotes `fetch` names for `repo`; see {@link RepoSweepOptions.fetch}. */
|
|
455
|
+
function fetchSet(repo, fetch) {
|
|
456
|
+
if (Array.isArray(fetch))
|
|
457
|
+
return [...fetch];
|
|
458
|
+
const names = judgedRemotes(repo).map((r) => r.name);
|
|
459
|
+
if (fetch === 'local' && repo.remote !== null)
|
|
460
|
+
names.unshift(repo.remote);
|
|
461
|
+
if ((names.length > 0 || fetch === 'local') && repo.against !== null)
|
|
462
|
+
names.push(repo.against);
|
|
463
|
+
return [...new Set(names)];
|
|
464
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -55,12 +55,17 @@ export type Action = (typeof ACTIONS)[number];
|
|
|
55
55
|
* (or its submodules') commits and stashes no remote has
|
|
56
56
|
* `discard-changes` drops modified, staged or untracked files in a worktree
|
|
57
57
|
* or any of its submodules
|
|
58
|
+
* `discard-archived` with `archive`, takes the place of both on a row whose
|
|
59
|
+
* work the archive holds: the work leaves the machine
|
|
60
|
+
* for an archive branch on the remote, and is deleted
|
|
61
|
+
* here. A file the archive leaves out, and a submodule's
|
|
62
|
+
* work, keep their own risk
|
|
58
63
|
* `confirm-open-pr` the branch has an open pull request, or open pull
|
|
59
64
|
* requests could not be checked (the CLI is missing or
|
|
60
65
|
* signed out, or the listing failed) so one cannot be
|
|
61
66
|
* ruled out; deleting the remote branch closes it
|
|
62
67
|
*/
|
|
63
|
-
export declare const RISKS: readonly ["abandon-unmerged", "discard-changes", "confirm-open-pr"];
|
|
68
|
+
export declare const RISKS: readonly ["abandon-unmerged", "discard-changes", "discard-archived", "confirm-open-pr"];
|
|
64
69
|
export type Risk = (typeof RISKS)[number];
|
|
65
70
|
/** The risks whose flag is set, in canonical order. */
|
|
66
71
|
export declare function risksFor(flags: Partial<Record<Risk, boolean>>): Risk[];
|
|
@@ -88,6 +93,8 @@ export type PrEvidence = Omit<PRInfo, 'headRef'> & {
|
|
|
88
93
|
};
|
|
89
94
|
/** Evidence every kind of row records about whether its work landed. */
|
|
90
95
|
export interface LandingEvidence {
|
|
96
|
+
/** The default branch the row was judged against, as a ref: `origin/main`, or `main` without a remote. */
|
|
97
|
+
landedAgainst?: string;
|
|
91
98
|
pr?: PrEvidence;
|
|
92
99
|
/** The work is on the default branch: by ancestry, an exact-head merged PR, or a patch match. Absent on a row not judged. */
|
|
93
100
|
merged?: boolean;
|
|
@@ -122,6 +129,12 @@ export interface WorktreeEvidence extends LandingEvidence {
|
|
|
122
129
|
bare: boolean;
|
|
123
130
|
detached: boolean;
|
|
124
131
|
primary: boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Bytes the directory takes on disk, read only with the `sizes` option; null
|
|
134
|
+
* when `du` could not say. Absent for a directory that is gone. Not part of
|
|
135
|
+
* a plan's fingerprint: growth after planning does not drop the row.
|
|
136
|
+
*/
|
|
137
|
+
sizeBytes?: number | null;
|
|
125
138
|
/** The directory is there and is still a checkout. */
|
|
126
139
|
onDisk?: boolean;
|
|
127
140
|
/** The directory is there but is no longer a checkout (its `.git` is gone). */
|
|
@@ -151,6 +164,11 @@ export interface WorktreeEvidence extends LandingEvidence {
|
|
|
151
164
|
otherWorktrees?: string[];
|
|
152
165
|
submodules?: SubmoduleEvidence[];
|
|
153
166
|
submoduleDirtyFiles?: number;
|
|
167
|
+
/**
|
|
168
|
+
* Remote-tracking branches (`origin/feature`) of the fetched remote whose history holds HEAD,
|
|
169
|
+
* the default branch excluded. Set only for a clean, used worktree of a fetched sweep.
|
|
170
|
+
*/
|
|
171
|
+
recoverableFrom?: string[];
|
|
154
172
|
/** Its submodules make plain `git worktree remove` refuse, so the remove is forced. */
|
|
155
173
|
forcedForSubmodules?: boolean;
|
|
156
174
|
}
|
|
@@ -160,7 +178,6 @@ export interface BranchEvidence extends LandingEvidence {
|
|
|
160
178
|
track: string | null;
|
|
161
179
|
lastCommit: string;
|
|
162
180
|
sha: string;
|
|
163
|
-
compareAgainst?: string;
|
|
164
181
|
/** Null when the remote default branch was not found. */
|
|
165
182
|
ancestorOfRemoteMain?: boolean | null;
|
|
166
183
|
/** The worktree whose removal frees this branch; it is deleted only when that worktree goes in the same run. */
|
|
@@ -216,6 +233,24 @@ type Defaulted = 'commands' | 'notes' | 'risks' | 'blockers';
|
|
|
216
233
|
export type RowFields<R extends Row> = Omit<R, Defaulted | 'id' | 'kind' | 'name' | 'evidence'> & Partial<Pick<R, Defaulted>>;
|
|
217
234
|
/** Build a row, defaulting its lists to empty. */
|
|
218
235
|
export declare function makeRow<R extends Row>(partial: Omit<R, Defaulted | 'id'> & Partial<Pick<R, Defaulted>>): R;
|
|
236
|
+
/**
|
|
237
|
+
* One remote a sweep touches, as it resolved it: the default remote, each
|
|
238
|
+
* remote whose branches it judges, and the `against` remote.
|
|
239
|
+
*/
|
|
240
|
+
export interface RemoteInfo {
|
|
241
|
+
name: string;
|
|
242
|
+
/** The sweep judges this remote's branches. */
|
|
243
|
+
judged: boolean;
|
|
244
|
+
/**
|
|
245
|
+
* Its own default branch, which is kept: what `refs/remotes/<name>/HEAD`
|
|
246
|
+
* points at, else `main`, then `master`, where the remote has them.
|
|
247
|
+
*/
|
|
248
|
+
defaultBranch: string;
|
|
249
|
+
/** As {@link RepoInfo.slug}, for this remote. */
|
|
250
|
+
slug: string | null;
|
|
251
|
+
/** As {@link RepoInfo.provider}, for this remote; null for a remote not judged. */
|
|
252
|
+
provider: ProviderStatus | null;
|
|
253
|
+
}
|
|
219
254
|
/** What the sweep learned about a repository before inventorying it. */
|
|
220
255
|
export interface RepoInfo {
|
|
221
256
|
/** The main checkout's root, even when the sweep was pointed at a worktree. */
|
|
@@ -225,20 +260,51 @@ export interface RepoInfo {
|
|
|
225
260
|
* host, or null when the remote names no repository.
|
|
226
261
|
*/
|
|
227
262
|
slug: string | null;
|
|
263
|
+
/** The local default branch: the default remote's default branch, or `main`, then `master`, without one. */
|
|
228
264
|
mainBranch: string;
|
|
265
|
+
/**
|
|
266
|
+
* The default remote: `origin`, else the remote the local default branch
|
|
267
|
+
* tracks, else the only remote. Null when none of those exists. Local rows
|
|
268
|
+
* are judged against its default branch.
|
|
269
|
+
*/
|
|
229
270
|
remote: string | null;
|
|
230
271
|
currentBranch: string;
|
|
231
272
|
/**
|
|
232
|
-
* The provider asked for
|
|
233
|
-
* none was asked (`provider: 'none'`,
|
|
273
|
+
* The default remote's provider, asked for the local rows' pull requests,
|
|
274
|
+
* and what its probe found. Null when none was asked (`provider: 'none'`,
|
|
275
|
+
* or a sweep without remote branches).
|
|
234
276
|
*/
|
|
235
277
|
provider: ProviderStatus | null;
|
|
236
|
-
/**
|
|
237
|
-
|
|
278
|
+
/** Every remote the sweep fetched before classifying, the `against` remote included. */
|
|
279
|
+
fetchedRemotes: string[];
|
|
280
|
+
/** Every remote the repository has, touched or not. */
|
|
281
|
+
allRemotes: string[];
|
|
282
|
+
/**
|
|
283
|
+
* Every remote the sweep touches, each with its own default branch: the
|
|
284
|
+
* default remote, the remotes whose branches it judges (`judged`, in the
|
|
285
|
+
* order asked), and the `against` remote.
|
|
286
|
+
*/
|
|
287
|
+
remotes: RemoteInfo[];
|
|
288
|
+
/** The remote whose default branch every row is judged against instead of its own; null without one. */
|
|
289
|
+
against: string | null;
|
|
290
|
+
}
|
|
291
|
+
/** The remotes whose branches the sweep judges, in the order asked. */
|
|
292
|
+
export declare function judgedRemotes(repo: RepoInfo): RemoteInfo[];
|
|
293
|
+
/** A default branch a row's work can land on, and the remote it is on; null for the local default branch. */
|
|
294
|
+
export interface LandedAgainst {
|
|
295
|
+
remote: string | null;
|
|
296
|
+
branch: string;
|
|
238
297
|
}
|
|
239
|
-
/**
|
|
240
|
-
export declare function
|
|
241
|
-
/**
|
|
298
|
+
/** `<remote>/<branch>`, or the bare branch without a remote. */
|
|
299
|
+
export declare function refOf(against: LandedAgainst): string;
|
|
300
|
+
/**
|
|
301
|
+
* What rows of `remote` are judged against; no `remote` for local rows
|
|
302
|
+
* (worktrees and local branches). With `against`, every row is judged against
|
|
303
|
+
* that. Otherwise a remote's branches are judged against its own default
|
|
304
|
+
* branch, and local rows against the default remote's.
|
|
305
|
+
*/
|
|
306
|
+
export declare function landedFor(repo: RepoInfo, remote?: RemoteInfo): LandedAgainst;
|
|
307
|
+
/** True when the repository has a default remote the sweep did not fetch, so its refs may be stale. */
|
|
242
308
|
export declare function isUnfetched(repo: RepoInfo): boolean;
|
|
243
309
|
/** Rows of one kind, counted by what the sweep concluded. */
|
|
244
310
|
export interface Bucket {
|
|
@@ -259,6 +325,15 @@ export declare function emptyBucket(): Bucket;
|
|
|
259
325
|
export declare function countRows(rows: readonly Row[]): Counts;
|
|
260
326
|
/** `counts` summed across kinds. */
|
|
261
327
|
export declare function totalOf(counts: Counts): Bucket;
|
|
328
|
+
/** Bytes the `delete` rows and the `review` rows take on disk; see {@link reclaimableOf}. */
|
|
329
|
+
export interface Reclaimable {
|
|
330
|
+
delete: number;
|
|
331
|
+
review: number;
|
|
332
|
+
/** How many `delete` or `review` worktrees have no size read, so the totals are lower bounds. */
|
|
333
|
+
unsized: number;
|
|
334
|
+
}
|
|
335
|
+
/** The bytes `rows` would free, per verdict. Rows with no size read count for nothing. */
|
|
336
|
+
export declare function reclaimableOf(rows: readonly Row[]): Reclaimable;
|
|
262
337
|
/** One repository's rows, or the reason there are none. */
|
|
263
338
|
export interface RepoSweep {
|
|
264
339
|
/** The path the sweep was pointed at, whether or not it could be read. */
|
|
@@ -267,6 +342,8 @@ export interface RepoSweep {
|
|
|
267
342
|
repo: RepoInfo | null;
|
|
268
343
|
rows: Row[];
|
|
269
344
|
counts: Counts;
|
|
345
|
+
/** Bytes freed by the `delete` rows and by the `review` rows; null when `sizes` was off. */
|
|
346
|
+
reclaimableBytes: Reclaimable | null;
|
|
270
347
|
/**
|
|
271
348
|
* What the sweep could not look up for this repository and judged without,
|
|
272
349
|
* one sentence each: a remote branch listing or a pull request listing that
|
|
@@ -322,6 +399,8 @@ export interface Sweep {
|
|
|
322
399
|
duplicates: DuplicateCheckout[];
|
|
323
400
|
/** Every repository's counts, summed per kind. */
|
|
324
401
|
counts: Counts;
|
|
402
|
+
/** Every repository's `reclaimableBytes`, summed; null when `sizes` was off. */
|
|
403
|
+
reclaimableBytes: Reclaimable | null;
|
|
325
404
|
/** What was swept, for a plan. */
|
|
326
405
|
options: SweptWith;
|
|
327
406
|
}
|
|
@@ -343,7 +422,7 @@ export interface SweepProgress {
|
|
|
343
422
|
repo: string;
|
|
344
423
|
index: number;
|
|
345
424
|
of: number;
|
|
346
|
-
step: 'discover' | 'fetch' | 'prs' | Kind;
|
|
425
|
+
step: 'discover' | 'fetch' | 'prs' | 'size' | Kind;
|
|
347
426
|
done?: number;
|
|
348
427
|
total?: number;
|
|
349
428
|
/** The CLI asked during the `prs` step: `gh`, `glab`. */
|
package/dist/types.js
CHANGED
|
@@ -49,12 +49,22 @@ export const ACTIONS = ['remove', 'prune', 'delete'];
|
|
|
49
49
|
* (or its submodules') commits and stashes no remote has
|
|
50
50
|
* `discard-changes` drops modified, staged or untracked files in a worktree
|
|
51
51
|
* or any of its submodules
|
|
52
|
+
* `discard-archived` with `archive`, takes the place of both on a row whose
|
|
53
|
+
* work the archive holds: the work leaves the machine
|
|
54
|
+
* for an archive branch on the remote, and is deleted
|
|
55
|
+
* here. A file the archive leaves out, and a submodule's
|
|
56
|
+
* work, keep their own risk
|
|
52
57
|
* `confirm-open-pr` the branch has an open pull request, or open pull
|
|
53
58
|
* requests could not be checked (the CLI is missing or
|
|
54
59
|
* signed out, or the listing failed) so one cannot be
|
|
55
60
|
* ruled out; deleting the remote branch closes it
|
|
56
61
|
*/
|
|
57
|
-
export const RISKS = [
|
|
62
|
+
export const RISKS = [
|
|
63
|
+
'abandon-unmerged',
|
|
64
|
+
'discard-changes',
|
|
65
|
+
'discard-archived',
|
|
66
|
+
'confirm-open-pr',
|
|
67
|
+
];
|
|
58
68
|
/** The risks whose flag is set, in canonical order. */
|
|
59
69
|
export function risksFor(flags) {
|
|
60
70
|
return RISKS.filter((name) => flags[name] === true);
|
|
@@ -74,13 +84,32 @@ export function makeRow(partial) {
|
|
|
74
84
|
blockers: partial.blockers ?? [],
|
|
75
85
|
};
|
|
76
86
|
}
|
|
77
|
-
/** The
|
|
78
|
-
export function
|
|
79
|
-
return repo.
|
|
87
|
+
/** The remotes whose branches the sweep judges, in the order asked. */
|
|
88
|
+
export function judgedRemotes(repo) {
|
|
89
|
+
return repo.remotes.filter((r) => r.judged);
|
|
80
90
|
}
|
|
81
|
-
/**
|
|
91
|
+
/** `<remote>/<branch>`, or the bare branch without a remote. */
|
|
92
|
+
export function refOf(against) {
|
|
93
|
+
return against.remote === null ? against.branch : `${against.remote}/${against.branch}`;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* What rows of `remote` are judged against; no `remote` for local rows
|
|
97
|
+
* (worktrees and local branches). With `against`, every row is judged against
|
|
98
|
+
* that. Otherwise a remote's branches are judged against its own default
|
|
99
|
+
* branch, and local rows against the default remote's.
|
|
100
|
+
*/
|
|
101
|
+
export function landedFor(repo, remote) {
|
|
102
|
+
if (repo.against !== null) {
|
|
103
|
+
const against = repo.remotes.find((r) => r.name === repo.against);
|
|
104
|
+
return { remote: repo.against, branch: against?.defaultBranch ?? repo.mainBranch };
|
|
105
|
+
}
|
|
106
|
+
if (remote === undefined)
|
|
107
|
+
return { remote: repo.remote, branch: repo.mainBranch };
|
|
108
|
+
return { remote: remote.name, branch: remote.defaultBranch };
|
|
109
|
+
}
|
|
110
|
+
/** True when the repository has a default remote the sweep did not fetch, so its refs may be stale. */
|
|
82
111
|
export function isUnfetched(repo) {
|
|
83
|
-
return repo.remote !== null && !repo.
|
|
112
|
+
return repo.remote !== null && !repo.fetchedRemotes.includes(repo.remote);
|
|
84
113
|
}
|
|
85
114
|
export function emptyBucket() {
|
|
86
115
|
return { total: 0, merged: 0, delete: 0, prune: 0, review: 0, blocked: 0, keep: 0 };
|
|
@@ -109,6 +138,21 @@ export function totalOf(counts) {
|
|
|
109
138
|
}
|
|
110
139
|
return out;
|
|
111
140
|
}
|
|
141
|
+
/** The bytes `rows` would free, per verdict. Rows with no size read count for nothing. */
|
|
142
|
+
export function reclaimableOf(rows) {
|
|
143
|
+
const out = { delete: 0, review: 0, unsized: 0 };
|
|
144
|
+
for (const row of rows) {
|
|
145
|
+
if (row.kind !== 'worktree')
|
|
146
|
+
continue;
|
|
147
|
+
if (row.verdict !== 'delete' && row.verdict !== 'review')
|
|
148
|
+
continue;
|
|
149
|
+
if (typeof row.evidence.sizeBytes === 'number')
|
|
150
|
+
out[row.verdict] += row.evidence.sizeBytes;
|
|
151
|
+
else if (row.evidence.sizeBytes === null)
|
|
152
|
+
out.unsized += 1;
|
|
153
|
+
}
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
112
156
|
/** Every repository's `notes` in `sweep`, each with its repository. */
|
|
113
157
|
export function sweepNotes(sweep) {
|
|
114
158
|
return sweep.repos.flatMap((r) => r.notes.map((note) => ({ repo: r.repo?.root ?? r.path, note })));
|
package/dist/util.d.ts
CHANGED
|
@@ -11,3 +11,5 @@ export declare function isDir(path: string): boolean;
|
|
|
11
11
|
export declare function errorText(err: unknown): string;
|
|
12
12
|
/** `1 commit`, `3 commits`. */
|
|
13
13
|
export declare const plural: (n: number, one: string, many: string) => string;
|
|
14
|
+
/** A glob as a regular expression: `*` stays inside a `/`-separated segment, `**` does not, `?` is one character. */
|
|
15
|
+
export declare function globRegExp(glob: string): RegExp;
|