@osdlc/repo-cleaner 0.25.0 → 0.26.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # @osdlc/repo-cleaner
2
2
 
3
+ ## 0.26.0
4
+
5
+ ### Minor Changes
6
+
7
+ - f75dee2: A worktree whose commits ahead could not be counted, because the base does not resolve (a remote never fetched, say) or its HEAD does not (its branch deleted), is no longer read as having none. `evidence.aheadUnknown` is set, the row carries `abandon-unmerged` and is held for review, and its reason names what did not resolve, such as `commits ahead of origin/main not known: origin/main does not resolve`.
8
+ - b120464: A worktree whose directory is gone, and whose HEAD no local branch, tag or remote-tracking branch holds (a detached worktree with its own commits, or one whose branch was deleted), is no longer pruned by default. Its prune would leave those commits unreachable, so the row is `review` with `abandon-unmerged`, and `evidence.headOnRef` says what was found; a HEAD git cannot place is held the same way. A gone worktree on a branch that still exists, or at a commit a ref holds, is pruned as before.
9
+
10
+ ### Patch Changes
11
+
12
+ - 93e864e: The reason of a worktree that holds work leaves out counts of zero and says one and many properly: `1 commit ahead of origin/main` instead of `0 dirty file(s), 1 commit(s) ahead of origin/main`. Submodules with unpushed work read the same way: `submodule sub (1 stash) holds work no remote has`, `unpushed work in submodules a (…), b (…)`.
13
+ - Updated dependencies [3b7fc6b]
14
+ - @osdlc/typed-git@0.22.1
15
+
3
16
  ## 0.25.0
4
17
 
5
18
  ### Minor Changes
package/README.md CHANGED
@@ -576,7 +576,13 @@ A **verdict** is what the sweep concluded about a row:
576
576
  | `keep` | Structural (the default branch, the current checkout, the primary worktree), or in use (an open pull request) | Never deletes a structural row; deletes one in use only when picked and `confirm-open-pr` is allowed |
577
577
 
578
578
  The report shows `prune` for a `delete` row whose worktree directory is
579
- already gone: only the stale record goes.
579
+ already gone: only the stale record goes. The record holds the worktree's
580
+ HEAD, so one on a branch that still exists, or at a commit a local branch, a
581
+ tag or a remote-tracking branch holds, is a `delete`. A detached one with its
582
+ own commits, or one whose branch was deleted, would leave its HEAD on no ref:
583
+ its prune is a `review` that carries `abandon-unmerged`, and
584
+ `evidence.headOnRef` is `false`. One whose HEAD git cannot place (`null`) is
585
+ held the same way.
580
586
 
581
587
  A **risk** is what deleting a row would lose. Each row lists its risks, and
582
588
  you approve them by name with `--allow`:
@@ -705,7 +711,12 @@ Everything else is held back. A branch that is neither merged nor an ancestor
705
711
  is `review` with the reasons spelled out, not a guess. A worktree
706
712
  holding uncommitted or unpushed work is never proposed for removal whatever
707
713
  else is true of it — losing an hour of someone's work costs more than any
708
- amount of clutter.
714
+ amount of clutter. A worktree's commits are counted against its upstream, else
715
+ `<remote>/<default>`; when that does not resolve (a remote never fetched), or
716
+ the worktree's HEAD does not (its branch deleted), the count is not known,
717
+ which is not none: `evidence.aheadUnknown` is set, the reason names what did
718
+ not resolve, and the row carries `abandon-unmerged`. Untracked files count whatever
719
+ `status.showUntrackedFiles` says, since `git worktree remove` deletes them.
709
720
 
710
721
  A clean worktree whose HEAD is merged by ancestry alone, with no merged PR
711
722
  or patch match, needs one more sign, because a worktree that `git worktree
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * A sweep's rows as plain data for a host that runs its own phases (a
3
- * `git` plugin of host-cleaner): what could be deleted, what would object,
3
+ * `git` plugin of workstation-cleaner): what could be deleted, what would object,
4
4
  * and what it depends on. `removeCandidate` (in `clean.ts`) takes one back.
5
5
  */
6
6
  import { type Action, type Blocker, type Kind, type Risk, type Row, type RowId, type Sweep, type Verdict } from './types.ts';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * A sweep's rows as plain data for a host that runs its own phases (a
3
- * `git` plugin of host-cleaner): what could be deleted, what would object,
3
+ * `git` plugin of workstation-cleaner): what could be deleted, what would object,
4
4
  * and what it depends on. `removeCandidate` (in `clean.ts`) takes one back.
5
5
  */
6
6
  import { fingerprint } from "./plan.js";
@@ -112,7 +112,7 @@ export declare function submoduleHazards(report: SubmoduleReport, opts?: {
112
112
  discard?: boolean;
113
113
  abandon?: boolean;
114
114
  }): string[];
115
- /** `submodule(s) sub (1 unpushed commit), deps/x (2 stashes)`. */
115
+ /** `submodule sub (1 unpushed commit)`, or `submodules sub (1 unpushed commit), deps/x (2 stashes)`. */
116
116
  export declare function unpushedPhrase(subs: readonly Submodule[]): string;
117
117
  /** `submodule(s) sub (<why it could not be read>)`. */
118
118
  export declare function unreadablePhrase(subs: readonly Submodule[]): string;
@@ -260,7 +260,8 @@ export function submoduleHazards(report, opts = {}) {
260
260
  if (report.unreadable.length > 0)
261
261
  out.push(`could not read ${unreadablePhrase(report.unreadable)}`);
262
262
  if (opts.abandon !== true && report.unpushed.length > 0) {
263
- out.push(`${unpushedPhrase(report.unpushed)} hold work no remote has`);
263
+ const hold = report.unpushed.length === 1 ? 'holds' : 'hold';
264
+ out.push(`${unpushedPhrase(report.unpushed)} ${hold} work no remote has`);
264
265
  }
265
266
  const hidden = report.submodules.filter((s) => s.hiddenIndexPaths.length > 0);
266
267
  if (hidden.length > 0)
@@ -273,9 +274,10 @@ export function submoduleHazards(report, opts = {}) {
273
274
  }
274
275
  return out;
275
276
  }
276
- /** `submodule(s) sub (1 unpushed commit), deps/x (2 stashes)`. */
277
+ /** `submodule sub (1 unpushed commit)`, or `submodules sub (1 unpushed commit), deps/x (2 stashes)`. */
277
278
  export function unpushedPhrase(subs) {
278
- return `submodule(s) ${subs.map((s) => `${s.path} (${unsharedPhrase(s)})`).join(', ')}`;
279
+ const which = subs.length === 1 ? 'submodule' : 'submodules';
280
+ return `${which} ${subs.map((s) => `${s.path} (${unsharedPhrase(s)})`).join(', ')}`;
279
281
  }
280
282
  /** `submodule(s) sub (<why it could not be read>)`. */
281
283
  export function unreadablePhrase(subs) {
package/dist/types.d.ts CHANGED
@@ -143,6 +143,11 @@ export interface WorktreeEvidence extends LandingEvidence {
143
143
  unpushedCommits?: number;
144
144
  /** What `unpushedCommits` was counted against: the upstream, or the default branch. */
145
145
  compareAgainst?: string;
146
+ /**
147
+ * True when `compareAgainst` did not resolve (a remote never fetched), so
148
+ * `unpushedCommits` is not known; the row then carries `abandon-unmerged`.
149
+ */
150
+ aheadUnknown?: boolean;
146
151
  branchExists?: boolean;
147
152
  /** Set for a worktree merged by ancestry alone; null when its branch looks freshly made. */
148
153
  branchUsed?: BranchUse | null;
@@ -171,6 +176,13 @@ export interface WorktreeEvidence extends LandingEvidence {
171
176
  recoverableFrom?: string[];
172
177
  /** Its submodules make plain `git worktree remove` refuse, so the remove is forced. */
173
178
  forcedForSubmodules?: boolean;
179
+ /**
180
+ * Set for a gone worktree whose branch does not hold its HEAD (detached, or
181
+ * the branch was deleted): whether a local branch, tag or remote-tracking
182
+ * branch does. Null when git could not say. Not true: the prune would leave
183
+ * the commit unreachable, so it needs `abandon-unmerged`.
184
+ */
185
+ headOnRef?: boolean | null;
174
186
  }
175
187
  export interface BranchEvidence extends LandingEvidence {
176
188
  branch: string;
@@ -1,2 +1,2 @@
1
1
  /** This package's version, from its package.json when it was built. */
2
- export declare const VERSION = "0.25.0";
2
+ export declare const VERSION = "0.26.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.25.0';
3
+ export const VERSION = '0.26.0';
@@ -8,6 +8,27 @@
8
8
  import type { Git } from '@osdlc/typed-git';
9
9
  import { type ProcessProbe } from './processes.ts';
10
10
  import { type PRInfo, type RepoInfo, type WorktreeEvidence, type WorktreeRow } from './types.ts';
11
+ /** What {@link workPhrase} words. */
12
+ export interface HeldWork {
13
+ /** Modified, staged and untracked files in the worktree itself. */
14
+ dirtyFiles: number;
15
+ /** The same, summed over its submodules. */
16
+ submoduleDirtyFiles: number;
17
+ /** The submodules those are in. */
18
+ dirtySubmodules: readonly string[];
19
+ /** Commits ahead of `against`. */
20
+ ahead: number;
21
+ /** Set when `ahead` is not known: what did not resolve, `against` or `HEAD`. */
22
+ unresolved?: string | undefined;
23
+ against: string;
24
+ /** `unpushedPhrase` of the submodules with work no remote has, or `''`. */
25
+ submoduleWork: string;
26
+ }
27
+ /**
28
+ * The reason of a worktree that holds work: `1 dirty file, 2 commits ahead of
29
+ * origin/main`. A count of zero is left out.
30
+ */
31
+ export declare function workPhrase(w: HeldWork): string;
11
32
  /** The command line, not run, that removes the worktree at `path`. */
12
33
  export declare function worktreeRemoveCommand(root: string, path: string, opts?: {
13
34
  force?: boolean;
package/dist/worktrees.js CHANGED
@@ -14,7 +14,47 @@ import { confirmationNote, judgeLanding, landingConfirmed, recordLanding, unmatc
14
14
  import { busyHolders, isIdleHolder, parseLockOwner } from "./processes.js";
15
15
  import { inspectSubmodules, readIndexHazards, submoduleFacts, unpushedPhrase, unreadablePhrase, worktreeGitDirs, } from "./submodules.js";
16
16
  import { landedFor, refOf, makeRow, risksFor, } from "./types.js";
17
- import { isDir } from "./util.js";
17
+ import { isDir, plural } from "./util.js";
18
+ /**
19
+ * The reason of a worktree that holds work: `1 dirty file, 2 commits ahead of
20
+ * origin/main`. A count of zero is left out.
21
+ */
22
+ export function workPhrase(w) {
23
+ const files = (n) => plural(n, 'dirty file', 'dirty files');
24
+ const parts = [];
25
+ if (w.dirtyFiles > 0)
26
+ parts.push(files(w.dirtyFiles));
27
+ if (w.submoduleDirtyFiles > 0) {
28
+ const where = `${w.dirtySubmodules.length === 1 ? 'submodule' : 'submodules'} ${w.dirtySubmodules.join(', ')}`;
29
+ parts.push(w.dirtyFiles > 0
30
+ ? `${w.submoduleDirtyFiles} in ${where}`
31
+ : `${files(w.submoduleDirtyFiles)} in ${where}`);
32
+ }
33
+ if (w.unresolved !== undefined) {
34
+ parts.push(`commits ahead of ${w.against} not known: ${w.unresolved} does not resolve`);
35
+ }
36
+ else if (w.ahead > 0) {
37
+ parts.push(`${plural(w.ahead, 'commit', 'commits')} ahead of ${w.against}`);
38
+ }
39
+ const said = parts.join(', ');
40
+ if (w.submoduleWork === '')
41
+ return said;
42
+ const work = `unpushed work in ${w.submoduleWork}`;
43
+ return said === '' ? work : `${said}; ${work}`;
44
+ }
45
+ /**
46
+ * Whether a local branch, a tag or a remote-tracking branch holds `head`;
47
+ * null when there is no `head`, or git cannot say (its commit is missing, say).
48
+ */
49
+ function headOnRef(git, head) {
50
+ if (head === null)
51
+ return null;
52
+ const held = git.try.forEachRef(['refs/heads/', 'refs/tags/', 'refs/remotes/'], {
53
+ format: '%(refname)',
54
+ contains: head,
55
+ });
56
+ return held.ok ? held.value.length > 0 : null;
57
+ }
18
58
  /** The command line, not run, that removes the worktree at `path`. */
19
59
  export function worktreeRemoveCommand(root, path, opts = {}) {
20
60
  return shellJoin([
@@ -262,6 +302,8 @@ function classifyWorktree(opts) {
262
302
  evidence.forcedForSubmodules = true;
263
303
  let dirtyFiles = [];
264
304
  let unpushed = [];
305
+ /** What did not resolve when the commits ahead could not be counted. */
306
+ let unresolved;
265
307
  if (onDisk) {
266
308
  dirtyFiles = git.at(path).statusPorcelain();
267
309
  let compareAgainst = '';
@@ -276,10 +318,16 @@ function classifyWorktree(opts) {
276
318
  compareAgainst = landedOn;
277
319
  }
278
320
  // `try`: `compareAgainst` is a guess — the fallback `origin/main` need not
279
- // exist locally, and an unresolvable range renders as zero unpushed commits.
321
+ // exist locally (a remote never fetched). A range that does not resolve
322
+ // leaves the count unknown, which is not none.
280
323
  const range = git.at(path).try.logLines({ range: `${compareAgainst}..HEAD`, format: '%h %s' });
281
324
  if (range.ok)
282
325
  unpushed = range.value;
326
+ else {
327
+ evidence.aheadUnknown = true;
328
+ // A deleted branch leaves HEAD unborn while the base resolves.
329
+ unresolved = git.at(path).resolveRef(compareAgainst) === null ? compareAgainst : 'HEAD';
330
+ }
283
331
  evidence.compareAgainst = compareAgainst;
284
332
  }
285
333
  evidence.dirtyFiles = dirtyFiles.length;
@@ -301,7 +349,7 @@ function classifyWorktree(opts) {
301
349
  if (patch !== null)
302
350
  recordLanding(evidence, patch);
303
351
  const patchLanded = patch !== null && patch.check !== null;
304
- let unlanded = unpushed.length > 0 && !landed && !landingConfirmed(patch);
352
+ let unlanded = (unpushed.length > 0 || evidence.aheadUnknown === true) && !landed && !landingConfirmed(patch);
305
353
  if (onDisk && head !== null) {
306
354
  evidence.landedAgainst = landedOn;
307
355
  // `soft`: a missing default ref reads as not merged.
@@ -340,8 +388,16 @@ function classifyWorktree(opts) {
340
388
  }
341
389
  }
342
390
  }
391
+ // A gone worktree's HEAD is kept by its admin dir alone, which the prune
392
+ // deletes: on a branch that still exists the branch holds it, else only
393
+ // another ref can. One no ref holds, or one that cannot be read, is lost.
394
+ let headLost = false;
395
+ if (!onDisk && !(branch && branchExists)) {
396
+ evidence.headOnRef = headOnRef(git.at(repo.root), head);
397
+ headLost = evidence.headOnRef !== true;
398
+ }
343
399
  const risks = risksFor({
344
- 'abandon-unmerged': unlanded || submoduleWork,
400
+ 'abandon-unmerged': unlanded || submoduleWork || headLost,
345
401
  'discard-changes': dirty,
346
402
  'confirm-open-pr': pr?.state === 'OPEN',
347
403
  });
@@ -362,6 +418,29 @@ function classifyWorktree(opts) {
362
418
  notes: blockers.map(([, advice]) => advice),
363
419
  });
364
420
  }
421
+ if (!onDisk && headLost) {
422
+ const short = head === null ? null : head.slice(0, 12);
423
+ const held = evidence.headOnRef === false
424
+ ? `its HEAD ${short} is on no branch, tag or remote branch`
425
+ : `no ref could be shown to hold its HEAD${short === null ? '' : ` ${short}`}`;
426
+ const inSubmodules = submoduleWork
427
+ ? `; its git dir keeps work no remote has in ${submoduleWorkText}`
428
+ : '';
429
+ return row({
430
+ verdict: 'review',
431
+ action: 'prune',
432
+ why: `directory gone, but ${held}${inSubmodules}`,
433
+ commands: pruneOnly,
434
+ risks,
435
+ notes: [
436
+ `Worktree ${path} is gone, and ${held}: pruning it leaves ${head === null ? 'that HEAD' : head} unreachable.`,
437
+ ...(submoduleWork
438
+ ? [`Its git dir also keeps ${submoduleWorkText}; pruning it deletes them.`]
439
+ : []),
440
+ `Make a branch at it (\`git branch <name> ${head ?? '<sha>'}\`), or allow abandon-unmerged to prune it anyway.`,
441
+ ],
442
+ });
443
+ }
365
444
  if (!onDisk && submoduleWork) {
366
445
  return row({
367
446
  verdict: 'review',
@@ -386,14 +465,18 @@ function classifyWorktree(opts) {
386
465
  });
387
466
  }
388
467
  if (dirty || unlanded || submoduleWork) {
389
- const inSubmodules = submoduleDirty > 0
390
- ? `, ${submoduleDirty} in submodule(s) ${submodules.dirty.map((s) => s.path).join(', ')}`
391
- : '';
392
- const workInSubmodules = submoduleWork ? `; unpushed work in ${submoduleWorkText}` : '';
393
468
  return row({
394
469
  verdict: 'review',
395
470
  action: 'remove',
396
- why: `${dirtyFiles.length} dirty file(s)${inSubmodules}, ${unpushed.length} commit(s) ahead of ${evidence.compareAgainst}${workInSubmodules}`,
471
+ why: workPhrase({
472
+ dirtyFiles: dirtyFiles.length,
473
+ submoduleDirtyFiles: submoduleDirty,
474
+ dirtySubmodules: submodules.dirty.map((s) => s.path),
475
+ ahead: unpushed.length,
476
+ unresolved,
477
+ against: evidence.compareAgainst ?? landedOn,
478
+ submoduleWork: submoduleWorkText,
479
+ }),
397
480
  commands: removes,
398
481
  risks,
399
482
  notes: [
@@ -410,6 +493,13 @@ function classifyWorktree(opts) {
410
493
  ]
411
494
  : []),
412
495
  ...(remoteNote !== null ? [remoteNote] : []),
496
+ ...(unresolved === undefined
497
+ ? []
498
+ : [
499
+ unresolved === 'HEAD'
500
+ ? 'Its HEAD does not resolve, so its commits could not be counted.'
501
+ : `${unresolved} does not resolve, so its commits could not be counted; \`--fetch\` may make it resolve.`,
502
+ ]),
413
503
  'Decide whether to preserve, push, or discard.',
414
504
  ],
415
505
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osdlc/repo-cleaner",
3
- "version": "0.25.0",
3
+ "version": "0.26.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,13 +49,13 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "zod": "^4.6.6",
52
- "@osdlc/command-seam": "^0.15.1",
53
52
  "@osdlc/cli-tool": "^0.22.0",
53
+ "@osdlc/command-seam": "^0.15.1",
54
54
  "@osdlc/file-lock": "^0.4.0",
55
55
  "@osdlc/path-guard": "^0.3.1",
56
- "@osdlc/typed-git": "^0.22.0",
57
- "@osdlc/ux": "^0.1.1",
58
- "@osdlc/typed-gh": "^0.20.0"
56
+ "@osdlc/typed-gh": "^0.20.0",
57
+ "@osdlc/typed-git": "^0.22.1",
58
+ "@osdlc/ux": "^0.1.1"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^26.1.0",