@statewalker/vcs-commands 0.6.0 → 0.7.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 (166) hide show
  1. package/dist/commands/apply/apply-command.d.ts.map +1 -1
  2. package/dist/commands/apply/apply-engine.d.ts.map +1 -1
  3. package/dist/commands/apply/apply-parse.d.ts.map +1 -1
  4. package/dist/commands/bisect/bisect-engine.d.ts.map +1 -1
  5. package/dist/commands/clone-command.d.ts +17 -0
  6. package/dist/commands/clone-command.d.ts.map +1 -1
  7. package/dist/commands/comment-char-config.d.ts +35 -0
  8. package/dist/commands/comment-char-config.d.ts.map +1 -0
  9. package/dist/commands/commit-command.d.ts.map +1 -1
  10. package/dist/commands/core-config.d.ts.map +1 -1
  11. package/dist/commands/default-branch.d.ts +70 -0
  12. package/dist/commands/default-branch.d.ts.map +1 -0
  13. package/dist/commands/diff-command.d.ts.map +1 -1
  14. package/dist/commands/fetch-command.d.ts.map +1 -1
  15. package/dist/commands/format-patch-command.d.ts.map +1 -1
  16. package/dist/commands/fsck/fsck-command.d.ts +2 -0
  17. package/dist/commands/fsck/fsck-command.d.ts.map +1 -1
  18. package/dist/commands/fsck/fsck-loose.d.ts +3 -0
  19. package/dist/commands/fsck/fsck-loose.d.ts.map +1 -1
  20. package/dist/commands/fsck/fsck-object-checks.d.ts +25 -14
  21. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -1
  22. package/dist/commands/fsck/fsck-object-table.d.ts +15 -7
  23. package/dist/commands/fsck/fsck-object-table.d.ts.map +1 -1
  24. package/dist/commands/fsck/fsck-pack.d.ts +11 -4
  25. package/dist/commands/fsck/fsck-pack.d.ts.map +1 -1
  26. package/dist/commands/fsck/verify-pack-command.d.ts.map +1 -1
  27. package/dist/commands/gc-command.d.ts +72 -48
  28. package/dist/commands/gc-command.d.ts.map +1 -1
  29. package/dist/commands/git-date.d.ts +1 -65
  30. package/dist/commands/git-date.d.ts.map +1 -1
  31. package/dist/commands/git-hooks.d.ts +8 -0
  32. package/dist/commands/git-hooks.d.ts.map +1 -1
  33. package/dist/commands/index-adder.d.ts +2 -0
  34. package/dist/commands/index-adder.d.ts.map +1 -1
  35. package/dist/commands/index.d.ts +2 -0
  36. package/dist/commands/index.d.ts.map +1 -1
  37. package/dist/commands/index.js +3 -3
  38. package/dist/commands/init-command.d.ts +50 -3
  39. package/dist/commands/init-command.d.ts.map +1 -1
  40. package/dist/commands/legacy-remotes.d.ts +55 -0
  41. package/dist/commands/legacy-remotes.d.ts.map +1 -0
  42. package/dist/commands/log-command.d.ts.map +1 -1
  43. package/dist/commands/ls-remote-command.d.ts.map +1 -1
  44. package/dist/commands/merge-command.d.ts +12 -0
  45. package/dist/commands/merge-command.d.ts.map +1 -1
  46. package/dist/commands/merge-support.d.ts +11 -3
  47. package/dist/commands/merge-support.d.ts.map +1 -1
  48. package/dist/commands/notes-merge-command.d.ts.map +1 -1
  49. package/dist/commands/notes-tree.d.ts +11 -1
  50. package/dist/commands/notes-tree.d.ts.map +1 -1
  51. package/dist/commands/patch-id.d.ts +3 -0
  52. package/dist/commands/patch-id.d.ts.map +1 -1
  53. package/dist/commands/pretty-format.d.ts.map +1 -1
  54. package/dist/commands/pull-command.d.ts +18 -0
  55. package/dist/commands/pull-command.d.ts.map +1 -1
  56. package/dist/commands/push-command.d.ts.map +1 -1
  57. package/dist/commands/read-tree-command.d.ts.map +1 -1
  58. package/dist/commands/rebase-command.d.ts.map +1 -1
  59. package/dist/commands/remote-command.d.ts.map +1 -1
  60. package/dist/commands/reset-command.d.ts.map +1 -1
  61. package/dist/commands/rev-parse-command.d.ts +9 -0
  62. package/dist/commands/rev-parse-command.d.ts.map +1 -1
  63. package/dist/commands/revision-resolver.d.ts +6 -2
  64. package/dist/commands/revision-resolver.d.ts.map +1 -1
  65. package/dist/commands/sequencer-pick.d.ts +1 -1
  66. package/dist/commands/sequencer-pick.d.ts.map +1 -1
  67. package/dist/commands/sequencer-support.d.ts.map +1 -1
  68. package/dist/commands/stash-create-command.d.ts +1 -1
  69. package/dist/commands/stash-create-command.d.ts.map +1 -1
  70. package/dist/commands/status-command.d.ts.map +1 -1
  71. package/dist/commands/submodule-summary-command.d.ts.map +1 -1
  72. package/dist/commands/untracked-cache-support.d.ts +2 -1
  73. package/dist/commands/untracked-cache-support.d.ts.map +1 -1
  74. package/dist/commands/worktree-list-command.d.ts.map +1 -1
  75. package/dist/{commands-DnmlfwGV.js → commands-DIJ91NnD.js} +1111 -506
  76. package/dist/{git-command-DxvwPrPB.js → git-command-BGiOJ8Zk.js} +630 -996
  77. package/dist/git-command.d.ts +16 -1
  78. package/dist/git-command.d.ts.map +1 -1
  79. package/dist/git.d.ts +9 -2
  80. package/dist/git.d.ts.map +1 -1
  81. package/dist/index.js +4 -4
  82. package/dist/remote-config/index.d.ts +1 -1
  83. package/dist/remote-config/index.d.ts.map +1 -1
  84. package/dist/remote-config/remote-config-store.d.ts +62 -3
  85. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  86. package/dist/rename/detect-renames.d.ts.map +1 -1
  87. package/dist/results/clone-result.d.ts +8 -0
  88. package/dist/results/clone-result.d.ts.map +1 -1
  89. package/dist/results/diff-output.d.ts.map +1 -1
  90. package/dist/results/index.js +1 -1
  91. package/dist/results/init-result.d.ts +7 -0
  92. package/dist/results/init-result.d.ts.map +1 -1
  93. package/dist/results/pull-result.d.ts +5 -0
  94. package/dist/results/pull-result.d.ts.map +1 -1
  95. package/dist/results/push-result.d.ts +5 -0
  96. package/dist/results/push-result.d.ts.map +1 -1
  97. package/dist/results/status-result.d.ts.map +1 -1
  98. package/dist/{results-BZGa2Z3I.js → results-WVlBhVb1.js} +11 -10
  99. package/package.json +12 -12
  100. package/src/commands/add-patch/add-interactive.ts +3 -3
  101. package/src/commands/am/am-command.ts +1 -1
  102. package/src/commands/apply/apply-command.ts +12 -2
  103. package/src/commands/apply/apply-engine.ts +5 -6
  104. package/src/commands/apply/apply-parse.ts +3 -2
  105. package/src/commands/attributes-source.ts +1 -1
  106. package/src/commands/bisect/bisect-engine.ts +2 -1
  107. package/src/commands/checkout-command.ts +1 -1
  108. package/src/commands/clone-command.ts +116 -4
  109. package/src/commands/comment-char-config.ts +160 -0
  110. package/src/commands/commit-command.ts +8 -7
  111. package/src/commands/core-config.ts +7 -2
  112. package/src/commands/default-branch.ts +196 -0
  113. package/src/commands/diff-command.ts +45 -27
  114. package/src/commands/fetch-command.ts +19 -5
  115. package/src/commands/format-patch-command.ts +7 -2
  116. package/src/commands/fsck/fsck-command.ts +183 -66
  117. package/src/commands/fsck/fsck-loose.ts +14 -4
  118. package/src/commands/fsck/fsck-object-checks.ts +87 -46
  119. package/src/commands/fsck/fsck-object-table.ts +52 -42
  120. package/src/commands/fsck/fsck-pack.ts +64 -34
  121. package/src/commands/fsck/verify-pack-command.ts +5 -2
  122. package/src/commands/gc-command.ts +208 -107
  123. package/src/commands/git-date.ts +9 -895
  124. package/src/commands/git-hooks.ts +11 -0
  125. package/src/commands/index-adder.ts +8 -3
  126. package/src/commands/index.ts +2 -0
  127. package/src/commands/init-command.ts +167 -11
  128. package/src/commands/legacy-remotes.ts +224 -0
  129. package/src/commands/log-command.ts +37 -22
  130. package/src/commands/ls-remote-command.ts +4 -0
  131. package/src/commands/merge-command.ts +31 -1
  132. package/src/commands/merge-support.ts +29 -8
  133. package/src/commands/notes-merge-command.ts +23 -14
  134. package/src/commands/notes-tree.ts +28 -13
  135. package/src/commands/octopus-merge.ts +3 -2
  136. package/src/commands/patch-id.ts +16 -8
  137. package/src/commands/pretty-format.ts +2 -1
  138. package/src/commands/pull-command.ts +128 -20
  139. package/src/commands/push-command.ts +23 -8
  140. package/src/commands/read-tree-command.ts +6 -2
  141. package/src/commands/rebase-command.ts +24 -13
  142. package/src/commands/remote-command.ts +35 -5
  143. package/src/commands/reset-command.ts +4 -11
  144. package/src/commands/rev-parse-command.ts +22 -0
  145. package/src/commands/revision-resolver.ts +32 -14
  146. package/src/commands/sequencer-pick.ts +7 -2
  147. package/src/commands/sequencer-support.ts +10 -3
  148. package/src/commands/stash-apply-command.ts +1 -1
  149. package/src/commands/stash-create-command.ts +2 -2
  150. package/src/commands/status-command.ts +2 -4
  151. package/src/commands/status-header.ts +3 -1
  152. package/src/commands/submodule-summary-command.ts +6 -7
  153. package/src/commands/untracked-cache-support.ts +16 -12
  154. package/src/commands/worktree-list-command.ts +3 -5
  155. package/src/core-commands/add.command.impl.ts +1 -1
  156. package/src/git-command.ts +32 -2
  157. package/src/git.ts +16 -3
  158. package/src/remote-config/index.ts +2 -0
  159. package/src/remote-config/remote-config-store.ts +137 -11
  160. package/src/rename/detect-renames.ts +5 -1
  161. package/src/results/clone-result.ts +9 -0
  162. package/src/results/diff-output.ts +9 -7
  163. package/src/results/init-result.ts +9 -0
  164. package/src/results/pull-result.ts +5 -0
  165. package/src/results/push-result.ts +5 -0
  166. package/src/results/status-result.ts +5 -2
@@ -1,18 +1,27 @@
1
- import type { ObjectId } from "@statewalker/vcs-core";
1
+ import type { FilesApi, History } from "@statewalker/vcs-core";
2
+ import { type GcResult, parseExpiryDate, gc as storeGc } from "@statewalker/vcs-store-files";
2
3
  import { parseGitInt } from "@statewalker/vcs-utils";
4
+ import { parseGitBool } from "@statewalker/vcs-utils/encoding";
3
5
 
4
6
  import { GitApiError } from "../errors/index.js";
5
7
  import { GitCommand } from "../git-command.js";
6
8
  import { RemoteConfigStore } from "../remote-config/index.js";
9
+ import { hostEnvironmentOf } from "./git-ident.js";
7
10
  import { WorktreePruneCommand } from "./worktree-prune-command.js";
8
11
 
9
12
  /**
10
13
  * Result of garbage collection
11
14
  */
12
15
  export interface GCCommandResult {
13
- /** Number of objects removed */
16
+ /**
17
+ * Objects the repository no longer stores: distinct object ids (loose or packed) in its own
18
+ * objects directory before the gc, less those after it. 0 without a files backend.
19
+ */
14
20
  objectsRemoved: number;
15
- /** Bytes freed by removing objects */
21
+ /**
22
+ * Bytes freed in the objects directory (loose object files and the pack directory's files),
23
+ * before less after: negative when the gc made it grow (a cruft pack beside packs kept)
24
+ */
16
25
  bytesFreed: number;
17
26
  /** Whether refs were packed */
18
27
  refsPacked: boolean;
@@ -25,19 +34,26 @@ export interface GCCommandResult {
25
34
  worktreesPruned: string[];
26
35
  /**
27
36
  * `setAuto(true)` only: why nothing was done (git's `gc --auto` returning at once): no
28
- * housekeeping needed (`not-needed`, gc.auto 0 included), or the pre-auto-gc hook refused
29
- * it (`hook`)
37
+ * housekeeping needed (`not-needed`, gc.auto 0 included), the pre-auto-gc hook refused
38
+ * it (`hook`), or another gc holds `gc.pid` (`running`)
30
39
  */
31
- autoSkipped?: "not-needed" | "hook";
40
+ autoSkipped?: "not-needed" | "hook" | "running";
32
41
  /** `setAuto(true)` and run: git's "Auto packing the repository ..." lines (its stderr) */
33
42
  messages?: string[];
34
43
  /**
35
44
  * git's error lines (its stderr): first the alternates' (`alternatesErrors()`, once: git's
36
45
  * repack and prune children repeat them), then those while packing refs (also in the hook
37
46
  * output): a loose ref whose object is missing stays loose, a ref changed before its prune
38
- * keeps its loose file
47
+ * keeps its loose file; then the reflog expiry's
39
48
  */
40
49
  errors?: string[];
50
+ /** git's warnings (a bitmap not written, `--auto`'s "too many unreachable loose objects") */
51
+ warnings?: string[];
52
+ /**
53
+ * What the store's gc did (repack, prune, reflog expiry, rerere gc, commit-graph), where the
54
+ * repository is a files repository; absent otherwise (only refs and worktrees are handled)
55
+ */
56
+ details?: GcResult;
41
57
  }
42
58
 
43
59
  /** git's defaults: `gc.auto` loose objects, `gc.autoPackLimit` packs. */
@@ -45,38 +61,33 @@ const GC_AUTO_DEFAULT = 6700;
45
61
  const GC_AUTO_PACK_LIMIT_DEFAULT = 50;
46
62
 
47
63
  /**
48
- * Run garbage collection on the repository.
49
- *
50
- * Equivalent to `git gc`.
64
+ * Run garbage collection on the repository: `git gc`.
51
65
  *
52
- * Garbage collection removes unreachable objects and optimizes
53
- * repository storage. This includes:
54
- * - Removing objects not reachable from any ref
55
- * - Packing loose refs into packed-refs
56
- * - Repacking objects into pack files (optional)
57
- *
58
- * Based on JGit's GarbageCollectCommand.
66
+ * Over a files repository it runs what `git gc` runs, in git's order (builtin/gc.c): `pack-refs
67
+ * --all --prune` (`gc.packRefs`), `reflog expire --all` (`gc.reflogExpire*`), `worktree prune
68
+ * --expire <gc.worktreePruneExpire>` and `rerere gc` (`gc.rerereResolved`/`Unresolved`), the
69
+ * repack (`repack -d -l` with `--cruft --cruft-expiration=<gc.pruneExpire>` (`gc.cruftPacks`,
70
+ * default), `-A --unpack-unreachable=<expire>` without cruft packs, or `-a` for `--prune=now`;
71
+ * `--keep-pack` for `--keep-largest-pack`/`gc.bigPackThreshold`; `-f --depth --window` for
72
+ * `--aggressive`; a bitmap in a bare repository), `update-server-info`, `prune --expire
73
+ * <expire>` (with `prune_shallow`), and the commit-graph (`gc.writeCommitGraph`). git 2.43 runs
74
+ * the worktree prune and rerere gc after the prune, 2.5x before the repack: the hook runner's
75
+ * compatibility decides. Without a files repository only refs are packed and worktrees pruned.
59
76
  *
60
77
  * @example
61
78
  * ```typescript
62
- * // Basic garbage collection
63
79
  * const result = await git.gc().call();
64
80
  * console.log(`Removed ${result.objectsRemoved} objects`);
65
81
  *
66
- * // Aggressive GC with repacking
67
- * const result = await git.gc()
68
- * .setAggressive(true)
69
- * .call();
82
+ * // git gc --aggressive --prune=now
83
+ * await git.gc().setAggressive(true).setPrune("now").call();
70
84
  *
71
- * // GC with expiration date (keep recent unreachable objects)
72
- * const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000);
73
- * const result = await git.gc()
74
- * .setExpire(twoWeeksAgo)
75
- * .call();
85
+ * // git gc --no-prune
86
+ * await git.gc().setPrune(false).call();
76
87
  * ```
77
88
  */
78
89
  export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
79
- private _packRefs = true;
90
+ private _packRefs?: boolean;
80
91
 
81
92
  /** git's fsck and gc read the objects as stored, never through replace refs. */
82
93
  protected override readsReplacedObjects(): boolean {
@@ -84,15 +95,21 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
84
95
  }
85
96
  private _aggressive = false;
86
97
  private _expire?: Date;
98
+ private _prune?: string | boolean;
87
99
  private _auto = false;
100
+ private _quiet = false;
101
+ private _cruft?: boolean;
102
+ private _maxCruftSize?: number;
103
+ private _keepLargestPack?: boolean;
88
104
 
89
105
  /**
90
106
  * `git gc --auto`: housekeeping only when needed (git's `need_to_gc`: more loose objects
91
107
  * than `gc.auto`, estimated from `objects/17` as git does, default 6700, or more packs
92
108
  * without a `.keep` than `gc.autoPackLimit`, default 50; 0 or less turns either check
93
109
  * off, `gc.auto=0` all of auto gc), and only when the pre-auto-gc hook, run first, agrees.
94
- * No command runs it by itself: callers decide when (git's `maintenance.auto` after a
95
- * commit, merge or fetch is not reproduced).
110
+ * With too many packs the repack is a full one, else an incremental one (the reachable loose
111
+ * objects into a new pack). No command runs it by itself: callers decide when (git's
112
+ * `maintenance.auto` after a commit, merge or fetch is not reproduced).
96
113
  *
97
114
  * @example
98
115
  * ```typescript
@@ -106,10 +123,50 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
106
123
  return this;
107
124
  }
108
125
 
126
+ /** `--quiet`: no "Auto packing the repository ..." messages. */
127
+ setQuiet(quiet: boolean): this {
128
+ this.checkCallable();
129
+ this._quiet = quiet;
130
+ return this;
131
+ }
132
+
133
+ /**
134
+ * `--prune=<date>` (a git expiry date: `now`, `never`, `2.weeks.ago`, a `Date`), `--prune`
135
+ * (true: `gc.pruneExpire`, default two weeks) or `--no-prune` (false: nothing unreachable
136
+ * expires, `git prune` does not run).
137
+ */
138
+ setPrune(prune: string | Date | boolean): this {
139
+ this.checkCallable();
140
+ if (prune instanceof Date) this._expire = prune;
141
+ else this._prune = prune;
142
+ return this;
143
+ }
144
+
145
+ /** `--cruft` / `--no-cruft` (default `gc.cruftPacks`, true). */
146
+ setCruft(cruft: boolean): this {
147
+ this.checkCallable();
148
+ this._cruft = cruft;
149
+ return this;
150
+ }
151
+
152
+ /** `--max-cruft-size=<bytes>` (default `gc.maxCruftSize`). */
153
+ setMaxCruftSize(bytes: number): this {
154
+ this.checkCallable();
155
+ this._maxCruftSize = bytes;
156
+ return this;
157
+ }
158
+
109
159
  /**
110
- * Whether `gc --auto` has work to do (git's `need_to_gc` / `odb_optimize_required` for the
111
- * incremental strategy), read from the common directory's `objects/`.
160
+ * `--keep-largest-pack` (true) / `--no-keep-largest-pack` (false): the largest non-cruft
161
+ * pack is kept as it is; false also ignores `gc.bigPackThreshold`.
112
162
  */
163
+ setKeepLargestPack(keep: boolean): this {
164
+ this.checkCallable();
165
+ this._keepLargestPack = keep;
166
+ return this;
167
+ }
168
+
169
+ /** git's `need_to_gc`: whether `gc --auto` has work to do. */
113
170
  private async needsAutoGc(): Promise<boolean> {
114
171
  const config = RemoteConfigStore.from(this.workingCopy);
115
172
  // git_config_int: k, m and g units, hex and octal; git dies on anything else
@@ -151,16 +208,15 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
151
208
  }
152
209
  // the loose objects estimated from one fan-out directory, against the limit rounded up
153
210
  // to a multiple of 256
154
- const inSeventeen = (await names("17")).filter((n) => /^[0-9a-f]{38}$/.test(n)).length;
211
+ // names of the repository's format only (git's for_each_loose_file_in_objdir)
212
+ const loose = new RegExp(`^[0-9a-f]{${this.objectFormat.hexLength - 2}}$`);
213
+ const inSeventeen = (await names("17")).filter((n) => loose.test(n)).length;
155
214
  return inSeventeen * 256 > Math.ceil(gcAuto / 256) * 256;
156
215
  }
157
216
 
158
217
  /**
159
- * Set aggressive mode.
160
- *
161
- * In aggressive mode, GC will also repack objects for better compression.
162
- *
163
- * @param aggressive Whether to run aggressive GC
218
+ * `--aggressive`: deltas searched again (`repack -f`) with `gc.aggressiveWindow` (250) and
219
+ * `gc.aggressiveDepth` (50).
164
220
  */
165
221
  setAggressive(aggressive: boolean): this {
166
222
  this.checkCallable();
@@ -169,11 +225,8 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
169
225
  }
170
226
 
171
227
  /**
172
- * Set whether to pack refs.
173
- *
174
- * When true (default), loose refs are packed into packed-refs.
175
- *
176
- * @param packRefs Whether to pack refs
228
+ * Whether to pack refs (`git pack-refs --all --prune`). Default: `gc.packRefs` (true;
229
+ * `notbare` packs them only in a repository with a worktree).
177
230
  */
178
231
  setPackRefs(packRefs: boolean): this {
179
232
  this.checkCallable();
@@ -182,13 +235,8 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
182
235
  }
183
236
 
184
237
  /**
185
- * Set expiration date for unreachable objects.
186
- *
187
- * Objects unreachable but modified after this date will not be deleted.
188
- * This provides a grace period to prevent deleting objects that might
189
- * still be needed by in-progress operations.
190
- *
191
- * @param expire Expiration date (objects older than this may be deleted)
238
+ * `--prune=<date>`: unreachable objects older than this date expire; younger ones stay
239
+ * (loose, or in a cruft pack) in case a write in progress needs them.
192
240
  */
193
241
  setExpire(expire: Date): this {
194
242
  this.checkCallable();
@@ -196,6 +244,41 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
196
244
  return this;
197
245
  }
198
246
 
247
+ /** The repository's files backend: its FilesApi, common git directory and history. */
248
+ private async filesRepository(): Promise<
249
+ { files: FilesApi; gitDir: string; history: History } | undefined
250
+ > {
251
+ const history = this.workingCopy.history as History | undefined;
252
+ if (!history?.objects || !history.packs) return undefined;
253
+ const objects = await this.hooks.commonGitFile("objects");
254
+ if (!objects) return undefined;
255
+ return { files: objects.files, gitDir: objects.path.replace(/\/?objects$/, ""), history };
256
+ }
257
+
258
+ /** The prune expiry in milliseconds before now, undefined for the config's. */
259
+ private pruneExpireMs(): number | undefined {
260
+ if (this._expire) return Math.max(0, Date.now() - this._expire.getTime());
261
+ if (typeof this._prune !== "string") return undefined;
262
+ const nowMs = Date.now();
263
+ const cutoff = parseExpiryDate(this._prune, Math.floor(nowMs / 1000), "--prune");
264
+ if (cutoff === 0) return Number.POSITIVE_INFINITY;
265
+ if (cutoff === Number.POSITIVE_INFINITY) return 0;
266
+ return Math.max(0, nowMs - cutoff * 1000);
267
+ }
268
+
269
+ /** `gc.packRefs`: a boolean, or `notbare` (packed unless the repository is bare). */
270
+ private packRefsConfigured(): boolean {
271
+ if (this._packRefs !== undefined) return this._packRefs;
272
+ const config = RemoteConfigStore.from(this.workingCopy);
273
+ const value = config.value("gc.packRefs");
274
+ if (value === undefined) return true;
275
+ if (value === "notbare") {
276
+ const bare = config.value("core.bare");
277
+ return !(bare !== undefined && (parseGitBool(bare) ?? false));
278
+ }
279
+ return parseGitBool(value) ?? true;
280
+ }
281
+
199
282
  /**
200
283
  * Execute the garbage collection.
201
284
  *
@@ -206,51 +289,32 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
206
289
  this.setCallable(false);
207
290
 
208
291
  const startTime = Date.now();
209
- const objectsRemoved = 0;
210
- const bytesFreed = 0;
211
292
  let refsPacked = false;
212
293
  const messages: string[] = [];
294
+ const skipped = (autoSkipped: "not-needed" | "hook" | "running"): GCCommandResult => ({
295
+ objectsRemoved: 0,
296
+ bytesFreed: 0,
297
+ refsPacked,
298
+ durationMs: Date.now() - startTime,
299
+ worktreesPruned: [],
300
+ autoSkipped,
301
+ });
213
302
  if (this._auto) {
214
- const skipped = (autoSkipped: "not-needed" | "hook"): GCCommandResult => ({
215
- objectsRemoved,
216
- bytesFreed,
217
- refsPacked,
218
- durationMs: Date.now() - startTime,
219
- worktreesPruned: [],
220
- autoSkipped,
221
- });
222
303
  if (!(await this.needsAutoGc())) return skipped("not-needed");
223
304
  // git's `gc --auto`: the pre-auto-gc hook may refuse the housekeeping
224
305
  const hook = await this.hooks.run("pre-auto-gc");
225
306
  if (hook && hook.exitCode !== 0) return skipped("hook");
226
- messages.push(
227
- "Auto packing the repository for optimum performance.",
228
- 'See "git help gc" for manual housekeeping.',
229
- );
230
- }
231
-
232
- // Collect all ref roots
233
- const roots: ObjectId[] = [];
234
- for await (const ref of this.refsStore.list("refs/")) {
235
- if ("objectId" in ref && ref.objectId) {
236
- roots.push(ref.objectId);
237
- }
238
- }
239
-
240
- // Also include HEAD if it points to a commit
241
- const head = await this.refsStore.resolve("HEAD");
242
- if (head?.objectId) {
243
- roots.push(head.objectId);
307
+ if (!this._quiet)
308
+ messages.push(
309
+ "Auto packing the repository for optimum performance.",
310
+ 'See "git help gc" for manual housekeeping.',
311
+ );
244
312
  }
245
313
 
246
- // Run garbage collection if GCController is available
247
- // Note: This requires access to the Repository's gc property
248
- // For now, we'll check if the ref store supports the gc operations
249
-
250
- // Check if refs support packRefs
251
- // (`pack-refs --all --prune`; git 2.5x's `gc --auto` adds `--auto`, should_pack_refs)
314
+ // `pack-refs --all --prune` (git 2.5x's `gc --auto` adds `--auto`, should_pack_refs)
252
315
  const errors: string[] = [];
253
- if (this._packRefs && this.refsStore.packRefs) {
316
+ const packRefs = async () => {
317
+ if (!this.refsStore.packRefs) return;
254
318
  await this.refsStore.packRefs([], {
255
319
  all: true,
256
320
  deleteLoose: true,
@@ -261,41 +325,78 @@ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
261
325
  },
262
326
  });
263
327
  refsPacked = true;
264
- }
328
+ };
265
329
 
266
- // git gc prunes the administrative files of gone worktrees (gc.worktreePruneExpire)
330
+ // `worktree prune --expire <gc.worktreePruneExpire>`
267
331
  const worktreesPruned: string[] = [];
268
- if (this.workingCopy.worktrees) {
332
+ const pruneWorktrees = async () => {
333
+ if (!this.workingCopy.worktrees) return;
269
334
  const expire =
270
335
  RemoteConfigStore.from(this.workingCopy).value("gc.worktreePruneExpire") ?? "3.months.ago";
271
336
  const { pruned } = await new WorktreePruneCommand(this.workingCopy).setExpire(expire).call();
272
337
  for (const p of pruned) worktreesPruned.push(p.id);
273
- }
274
-
275
- // Note: Full GC requires Repository.gc which provides access to
276
- // GCController with collectGarbage(). This command provides the
277
- // interface but full implementation requires Repository access.
278
- // The WorkingCopy interface doesn't expose gc, so we'd need to extend
279
- // the interface or use a different pattern.
338
+ };
280
339
 
281
- // For aggressive mode, we could trigger repack here
282
- // But that also requires access to the GCController
283
- // Reserved for future implementation:
284
- // - this._aggressive: trigger full repack with better compression
285
- // - this._expire: pass to collectGarbage() for expiration filtering
286
- void this._aggressive;
287
- void this._expire;
340
+ const repository = await this.filesRepository();
341
+ if (!repository) {
342
+ // no files backend: refs and worktrees only
343
+ if (this.packRefsConfigured()) await packRefs();
344
+ await pruneWorktrees();
345
+ return {
346
+ objectsRemoved: 0,
347
+ bytesFreed: 0,
348
+ refsPacked,
349
+ durationMs: Date.now() - startTime,
350
+ worktreesPruned,
351
+ ...(this._auto ? { messages } : {}),
352
+ ...(errors.length > 0 || this.alternatesErrors().length > 0
353
+ ? { errors: [...this.alternatesErrors(), ...errors] }
354
+ : {}),
355
+ };
356
+ }
288
357
 
358
+ const environment = hostEnvironmentOf(this.workingCopy);
359
+ const pruneExpireMs = this.pruneExpireMs();
360
+ const details = await storeGc({
361
+ history: repository.history,
362
+ files: repository.files,
363
+ gitDir: repository.gitDir,
364
+ compatibility: this.hooks.compatibility,
365
+ ...(environment ? { environment } : {}),
366
+ packRefs: this.packRefsConfigured(),
367
+ packRefsStep: packRefs,
368
+ beforeObjects: pruneWorktrees,
369
+ afterObjects: pruneWorktrees,
370
+ ...(this._prune === false ? { prune: false } : {}),
371
+ ...(pruneExpireMs !== undefined ? { pruneExpireMs } : {}),
372
+ ...(this._cruft !== undefined ? { cruftPacks: this._cruft } : {}),
373
+ ...(this._maxCruftSize !== undefined ? { maxCruftSize: this._maxCruftSize } : {}),
374
+ ...(this._keepLargestPack !== undefined ? { keepLargestPack: this._keepLargestPack } : {}),
375
+ ...(this._aggressive ? { aggressive: true } : {}),
376
+ ...(this._auto ? { auto: true } : {}),
377
+ });
378
+ if (details.skipped) {
379
+ if (this._auto) return skipped("running");
380
+ throw new GitApiError(
381
+ "fatal: gc is already running on this repository (gc.pid is held; remove it if no gc runs)",
382
+ );
383
+ }
384
+ const storage = details.storage;
385
+ const allErrors = [...this.alternatesErrors(), ...errors, ...(details.errors ?? [])];
386
+ // the alternates' lines come once (the store's gc repeats them)
387
+ const uniqueErrors = allErrors.filter(
388
+ (line, i) => !this.alternatesErrors().includes(line) || allErrors.indexOf(line) === i,
389
+ );
289
390
  return {
290
- objectsRemoved,
291
- bytesFreed,
391
+ objectsRemoved: storage ? storage.before.objects - storage.after.objects : 0,
392
+ bytesFreed: storage ? storage.before.bytes - storage.after.bytes : 0,
292
393
  refsPacked,
293
394
  durationMs: Date.now() - startTime,
294
395
  worktreesPruned,
295
396
  ...(this._auto ? { messages } : {}),
296
- ...(errors.length > 0 || this.alternatesErrors().length > 0
297
- ? { errors: [...this.alternatesErrors(), ...errors] }
298
- : {}),
397
+ ...(uniqueErrors.length > 0 ? { errors: uniqueErrors } : {}),
398
+ ...(details.warnings ? { warnings: details.warnings } : {}),
399
+ details,
299
400
  };
300
401
  }
301
402
  }