@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.
Files changed (58) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/README.md +562 -167
  3. package/dist/approval.d.ts +91 -0
  4. package/dist/approval.js +233 -0
  5. package/dist/archive.d.ts +87 -0
  6. package/dist/archive.js +252 -0
  7. package/dist/branches.d.ts +4 -4
  8. package/dist/branches.js +56 -43
  9. package/dist/candidates.d.ts +42 -0
  10. package/dist/candidates.js +42 -0
  11. package/dist/clean.d.ts +66 -70
  12. package/dist/clean.js +386 -218
  13. package/dist/cli.d.ts +16 -10
  14. package/dist/cli.js +273 -102
  15. package/dist/discover.d.ts +28 -7
  16. package/dist/discover.js +185 -42
  17. package/dist/index.d.ts +12 -5
  18. package/dist/index.js +11 -4
  19. package/dist/landing.d.ts +10 -5
  20. package/dist/landing.js +15 -16
  21. package/dist/plan.d.ts +18 -11
  22. package/dist/plan.js +33 -13
  23. package/dist/processes.d.ts +19 -9
  24. package/dist/processes.js +51 -25
  25. package/dist/providers/github.d.ts +21 -3
  26. package/dist/providers/github.js +149 -27
  27. package/dist/providers/gitlab.js +37 -17
  28. package/dist/providers/index.d.ts +1 -1
  29. package/dist/providers/index.js +1 -1
  30. package/dist/providers/types.d.ts +15 -2
  31. package/dist/prs.d.ts +19 -10
  32. package/dist/prs.js +21 -13
  33. package/dist/receipt.d.ts +68 -3
  34. package/dist/receipt.js +30 -3
  35. package/dist/reference.d.ts +1 -1
  36. package/dist/reference.js +99 -51
  37. package/dist/remote-mirror.d.ts +2 -1
  38. package/dist/remote-mirror.js +3 -14
  39. package/dist/render.d.ts +17 -2
  40. package/dist/render.js +81 -14
  41. package/dist/resolve.d.ts +12 -1
  42. package/dist/resolve.js +15 -3
  43. package/dist/scope.d.ts +15 -0
  44. package/dist/scope.js +125 -0
  45. package/dist/size.d.ts +20 -0
  46. package/dist/size.js +34 -0
  47. package/dist/status-line.js +1 -0
  48. package/dist/step-board.js +6 -4
  49. package/dist/sweep.d.ts +50 -18
  50. package/dist/sweep.js +152 -127
  51. package/dist/types.d.ts +89 -10
  52. package/dist/types.js +50 -6
  53. package/dist/util.d.ts +2 -0
  54. package/dist/util.js +25 -0
  55. package/dist/version.generated.d.ts +1 -1
  56. package/dist/version.generated.js +1 -1
  57. package/dist/worktrees.js +87 -11
  58. package/package.json +6 -5
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;
package/dist/util.js CHANGED
@@ -34,3 +34,28 @@ function rawErrorText(err) {
34
34
  const oneLine = (text) => text.replace(/\s*\n\s*/g, ' ').trim();
35
35
  /** `1 commit`, `3 commits`. */
36
36
  export const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
37
+ /** A glob as a regular expression: `*` stays inside a `/`-separated segment, `**` does not, `?` is one character. */
38
+ export function globRegExp(glob) {
39
+ let out = '';
40
+ for (let i = 0; i < glob.length; i++) {
41
+ const c = glob[i];
42
+ if (c === '*') {
43
+ if (glob[i + 1] === '*') {
44
+ i += 1;
45
+ if (glob[i + 1] === '/') {
46
+ i += 1;
47
+ out += '(?:.*/)?';
48
+ }
49
+ else
50
+ out += '.*';
51
+ }
52
+ else
53
+ out += '[^/]*';
54
+ }
55
+ else if (c === '?')
56
+ out += '[^/]';
57
+ else
58
+ out += c.replace(/[.+^${}()|[\]\\]/g, '\\$&');
59
+ }
60
+ return new RegExp(`^${out}$`);
61
+ }
@@ -1,2 +1,2 @@
1
1
  /** This package's version, from its package.json when it was built. */
2
- export declare const VERSION = "0.20.5";
2
+ export declare const VERSION = "0.24.0";
@@ -1,3 +1,3 @@
1
1
  // Generated by cli-tool-stamp-version from package.json. Do not edit.
2
2
  /** This package's version, from its package.json when it was built. */
3
- export const VERSION = '0.20.5';
3
+ export const VERSION = '0.24.0';
package/dist/worktrees.js CHANGED
@@ -13,7 +13,7 @@ import { gapSentence, mergedFact, recordPr } from "./branches.js";
13
13
  import { confirmationNote, judgeLanding, landingConfirmed, recordLanding, unmatchedSentence, } from "./landing.js";
14
14
  import { busyHolders, isIdleHolder, parseLockOwner } from "./processes.js";
15
15
  import { inspectSubmodules, readIndexHazards, submoduleFacts, unpushedPhrase, unreadablePhrase, worktreeGitDirs, } from "./submodules.js";
16
- import { landedRef, makeRow, risksFor, } from "./types.js";
16
+ import { landedFor, refOf, makeRow, risksFor, } from "./types.js";
17
17
  import { isDir } from "./util.js";
18
18
  /** The command line, not run, that removes the worktree at `path`. */
19
19
  export function worktreeRemoveCommand(root, path, opts = {}) {
@@ -216,6 +216,8 @@ export function gatherWorktrees(repo, prs, cwd, git, only, probe, tick) {
216
216
  }
217
217
  function classifyWorktree(opts) {
218
218
  const { path, branch, head, isBare, isDetached, isPrimary, repo, prs, cwd, git } = opts;
219
+ const against = landedFor(repo);
220
+ const landedOn = refOf(against);
219
221
  const evidence = {
220
222
  path,
221
223
  branch: branch || null,
@@ -271,7 +273,7 @@ function classifyWorktree(opts) {
271
273
  compareAgainst = upstream.value;
272
274
  }
273
275
  if (!compareAgainst) {
274
- compareAgainst = landedRef(repo);
276
+ compareAgainst = landedOn;
275
277
  }
276
278
  // `try`: `compareAgainst` is a guess — the fallback `origin/main` need not
277
279
  // exist locally, and an unresolvable range renders as zero unpushed commits.
@@ -285,7 +287,7 @@ function classifyWorktree(opts) {
285
287
  const branchExists = !!branch && git.at(repo.root).resolveRef(branch) !== null;
286
288
  evidence.branchExists = branchExists;
287
289
  const pr = branch ? prs.get(branch) : undefined;
288
- const { landed, gap } = recordPr(evidence, pr, head ?? '', repo);
290
+ const { landed, gap } = recordPr(evidence, pr, head ?? '', against);
289
291
  if (unpushed.length > 0)
290
292
  evidence.uniqueCommits = unpushed.length;
291
293
  // A merged PR at HEAD lands every commit, even ones the fallback
@@ -293,17 +295,20 @@ function classifyWorktree(opts) {
293
295
  // A rebase merge, cherry-pick or squash merge leaves HEAD off the default
294
296
  // branch; the patches say whether the work is there anyway. Judged from HEAD,
295
297
  // so a dirty tree is still caught by the dirty-file check below.
296
- const patch = landed || !onDisk || head === null ? null : judgeLanding(repo, head, pr, gap, git);
298
+ const patch = landed || !onDisk || head === null
299
+ ? null
300
+ : judgeLanding({ root: repo.root, ref: landedOn }, head, pr, gap, git);
297
301
  if (patch !== null)
298
302
  recordLanding(evidence, patch);
299
303
  const patchLanded = patch !== null && patch.check !== null;
300
- const unlanded = unpushed.length > 0 && !landed && !landingConfirmed(patch);
304
+ let unlanded = unpushed.length > 0 && !landed && !landingConfirmed(patch);
301
305
  if (onDisk && head !== null) {
306
+ evidence.landedAgainst = landedOn;
302
307
  // `soft`: a missing default ref reads as not merged.
303
308
  evidence.merged = mergedFact({
304
309
  landed,
305
310
  patchLanded,
306
- ancestor: git.at(repo.root).soft.isAncestor(head, landedRef(repo)),
311
+ ancestor: git.at(repo.root).soft.isAncestor(head, landedOn),
307
312
  });
308
313
  }
309
314
  const dirty = dirtyFiles.length > 0 || submoduleDirty > 0;
@@ -313,6 +318,28 @@ function classifyWorktree(opts) {
313
318
  // own unpushed commits covers them.
314
319
  const submoduleWork = submodules.unpushed.length > 0;
315
320
  const submoduleWorkText = submoduleWork ? unpushedPhrase(submodules.unpushed) : '';
321
+ // Work that a remote branch holds is not lost with the worktree. Only a
322
+ // clean worktree on a used branch: a dirty one has files no remote holds;
323
+ // a fresh branch is left alone (see `branchUse`).
324
+ let remoteNote = null;
325
+ const settled = landed || patchLanded || evidence.merged === true;
326
+ if (onDisk && !settled && !dirty && !submoduleWork && branch && head !== null && branchExists) {
327
+ const holders = remoteBranchesHolding(repo, git, head);
328
+ if (holders.length > 0) {
329
+ const used = branchUse(git.at(repo.root), branch, repo.mainBranch);
330
+ evidence.branchUsed = used;
331
+ if (used !== null) {
332
+ const recoverable = holders.filter((h) => repo.fetchedRemotes.includes(remoteOf(h, repo.allRemotes) ?? ''));
333
+ if (recoverable.length > 0) {
334
+ evidence.recoverableFrom = recoverable;
335
+ unlanded = false;
336
+ }
337
+ else {
338
+ remoteNote = `HEAD is on ${holders.join(', ')}, but this sweep did not fetch, so those remote-tracking refs may be stale; \`--fetch\` would check them.`;
339
+ }
340
+ }
341
+ }
342
+ }
316
343
  const risks = risksFor({
317
344
  'abandon-unmerged': unlanded || submoduleWork,
318
345
  'discard-changes': dirty,
@@ -379,9 +406,10 @@ function classifyWorktree(opts) {
379
406
  : []),
380
407
  ...(patchLanded && unlanded
381
408
  ? [
382
- `Its commits are patch-equivalent to commits on ${landedRef(repo)} (${patch.check}), but ${confirmationNote(patch)}.`,
409
+ `Its commits are patch-equivalent to commits on ${landedOn} (${patch.check}), but ${confirmationNote(patch)}.`,
383
410
  ]
384
411
  : []),
412
+ ...(remoteNote !== null ? [remoteNote] : []),
385
413
  'Decide whether to preserve, push, or discard.',
386
414
  ],
387
415
  });
@@ -390,7 +418,7 @@ function classifyWorktree(opts) {
390
418
  if (pr && landed)
391
419
  return deletes(`clean; PR #${pr.number} merged${forced}`);
392
420
  if (patchLanded) {
393
- return deletes(`clean; patch-equivalent to commits on ${landedRef(repo)} (${patch.check}); ${confirmationNote(patch)}${forced}`);
421
+ return deletes(`clean; patch-equivalent to commits on ${landedOn} (${patch.check}); ${confirmationNote(patch)}${forced}`);
394
422
  }
395
423
  if (branch && !branchExists) {
396
424
  return deletes(`branch \`${branch}\` no longer exists${forced}`);
@@ -410,18 +438,23 @@ function classifyWorktree(opts) {
410
438
  const used = branchUse(git.at(repo.root), branch, repo.mainBranch);
411
439
  evidence.branchUsed = used;
412
440
  if (used !== null) {
413
- return deletes(`clean; merged into ${landedRef(repo)}, and ${USE_REASON[used]}${forced}`);
441
+ return deletes(`clean; merged into ${landedOn}, and ${USE_REASON[used]}${forced}`);
414
442
  }
415
443
  }
444
+ if (evidence.recoverableFrom !== undefined) {
445
+ return deletes(`clean; HEAD is on ${evidence.recoverableFrom.join(', ')}, and the branch was worked on (${USE_REASON[evidence.branchUsed]})${forced}`);
446
+ }
416
447
  const notes = [`Worktree \`${path}\` is clean.`];
417
448
  if (branch)
418
449
  notes.push(`Branch: \`${branch}\`.`);
419
450
  notes.push(pr ? `PR #${pr.number} is ${pr.state}.` : 'No associated PR.');
420
451
  if (pr && gap)
421
- notes.push(gapSentence(pr, gap, repo));
422
- const unmatched = patch === null ? null : unmatchedSentence(patch, repo);
452
+ notes.push(gapSentence(pr, gap, against));
453
+ const unmatched = patch === null ? null : unmatchedSentence(patch, landedOn);
423
454
  if (unmatched !== null)
424
455
  notes.push(unmatched);
456
+ if (remoteNote !== null)
457
+ notes.push(remoteNote);
425
458
  return row({
426
459
  verdict: 'review',
427
460
  action: 'remove',
@@ -431,6 +464,49 @@ function classifyWorktree(opts) {
431
464
  risks,
432
465
  });
433
466
  }
467
+ /**
468
+ * The remote-tracking branches whose history holds `head`, as
469
+ * `<remote>/<name>`, on the default remote and every remote the sweep judges.
470
+ * Each remote's default branch is left out: ancestry of it has its own rule.
471
+ * Empty when there is no remote or the lookup fails.
472
+ */
473
+ function remoteBranchesHolding(repo, git, head) {
474
+ const defaults = new Map(repo.remotes.map((r) => [r.name, r.defaultBranch]));
475
+ if (repo.remote !== null)
476
+ defaults.set(repo.remote, repo.mainBranch);
477
+ if (defaults.size === 0)
478
+ return [];
479
+ // One walk of history for every remote, not one per remote.
480
+ const prefixes = [...defaults.keys()].map((r) => `refs/remotes/${r}/`);
481
+ const rows = git.at(repo.root).try.forEachRef(prefixes, { format: '%(refname)', contains: head });
482
+ if (!rows.ok)
483
+ return [];
484
+ const out = [];
485
+ for (const ref of rows.value) {
486
+ const name = ref.slice('refs/remotes/'.length);
487
+ const remote = remoteOf(name, repo.allRemotes);
488
+ const defaultBranch = remote === null ? undefined : defaults.get(remote);
489
+ if (defaultBranch === undefined)
490
+ continue;
491
+ if (name === `${remote}/HEAD` || name === `${remote}/${defaultBranch}`)
492
+ continue;
493
+ if (!out.includes(name))
494
+ out.push(name);
495
+ }
496
+ return out.sort();
497
+ }
498
+ /**
499
+ * The remote `name` (`origin/feat/x`) is a branch of: the longest remote name
500
+ * it starts with, since git allows `/` in one (`origin/mirror`). Null for none.
501
+ */
502
+ function remoteOf(name, remotes) {
503
+ let found = null;
504
+ for (const r of remotes) {
505
+ if (name.startsWith(`${r}/`) && (found === null || r.length > found.length))
506
+ found = r;
507
+ }
508
+ return found;
509
+ }
434
510
  const USE_REASON = {
435
511
  commits: 'the branch has commits made on it',
436
512
  upstream: 'the branch was pushed to its own upstream',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osdlc/repo-cleaner",
3
- "version": "0.20.5",
3
+ "version": "0.24.0",
4
4
  "description": "What has been merged into the default branch, and what can go away. Inventories local branches, worktrees and remote branches across one repository or many, classifying each with the evidence behind the verdict and the command that would act on it. Plans and applies approved deletes, compare-and-swap.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": "sksizer",
@@ -49,11 +49,12 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "zod": "^4.6.6",
52
- "@osdlc/cli-tool": "^0.19.0",
53
- "@osdlc/command-seam": "^0.15.0",
52
+ "@osdlc/cli-tool": "^0.21.0",
53
+ "@osdlc/command-seam": "^0.15.1",
54
+ "@osdlc/file-lock": "^0.4.0",
54
55
  "@osdlc/path-guard": "^0.3.1",
55
- "@osdlc/typed-gh": "^0.18.1",
56
- "@osdlc/typed-git": "^0.17.0",
56
+ "@osdlc/typed-gh": "^0.19.2",
57
+ "@osdlc/typed-git": "^0.21.0",
57
58
  "@osdlc/ux": "^0.1.1"
58
59
  },
59
60
  "devDependencies": {