@statewalker/vcs-commands 0.13.0 → 0.15.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 (304) hide show
  1. package/dist/commands/add-command.d.ts.map +1 -1
  2. package/dist/commands/add-patch/patch-backend.d.ts +1 -1
  3. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
  4. package/dist/commands/archive/archive-command.d.ts +6 -2
  5. package/dist/commands/archive/archive-command.d.ts.map +1 -1
  6. package/dist/commands/archive/remote-archive-command.d.ts +27 -2
  7. package/dist/commands/archive/remote-archive-command.d.ts.map +1 -1
  8. package/dist/commands/archive/upload-archive-writer.d.ts +11 -5
  9. package/dist/commands/archive/upload-archive-writer.d.ts.map +1 -1
  10. package/dist/commands/branch-command.d.ts +14 -2
  11. package/dist/commands/branch-command.d.ts.map +1 -1
  12. package/dist/commands/bundle/bundle-format.d.ts +6 -1
  13. package/dist/commands/bundle/bundle-format.d.ts.map +1 -1
  14. package/dist/commands/bundle/bundle-uri-host.d.ts +12 -1
  15. package/dist/commands/bundle/bundle-uri-host.d.ts.map +1 -1
  16. package/dist/commands/bundle/bundle-uri.d.ts +7 -1
  17. package/dist/commands/bundle/bundle-uri.d.ts.map +1 -1
  18. package/dist/commands/bundle/bundle-verify.d.ts +4 -1
  19. package/dist/commands/bundle/bundle-verify.d.ts.map +1 -1
  20. package/dist/commands/cat-file/batch-format.d.ts +8 -12
  21. package/dist/commands/cat-file/batch-format.d.ts.map +1 -1
  22. package/dist/commands/cat-file/cat-file-command.d.ts +39 -5
  23. package/dist/commands/cat-file/cat-file-command.d.ts.map +1 -1
  24. package/dist/commands/check-ignore-command.d.ts.map +1 -1
  25. package/dist/commands/checkout-command.d.ts +4 -0
  26. package/dist/commands/checkout-command.d.ts.map +1 -1
  27. package/dist/commands/checkout-paths.d.ts +9 -0
  28. package/dist/commands/checkout-paths.d.ts.map +1 -1
  29. package/dist/commands/cherry-pick-command.d.ts +3 -0
  30. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  31. package/dist/commands/clean-interactive.d.ts +0 -9
  32. package/dist/commands/clean-interactive.d.ts.map +1 -1
  33. package/dist/commands/clone-command.d.ts +17 -0
  34. package/dist/commands/clone-command.d.ts.map +1 -1
  35. package/dist/commands/column/column-config.d.ts +23 -0
  36. package/dist/commands/column/column-config.d.ts.map +1 -0
  37. package/dist/commands/column/column-layout.d.ts +129 -0
  38. package/dist/commands/column/column-layout.d.ts.map +1 -0
  39. package/dist/commands/column/index.d.ts +3 -0
  40. package/dist/commands/column/index.d.ts.map +1 -0
  41. package/dist/commands/column-command.d.ts +59 -0
  42. package/dist/commands/column-command.d.ts.map +1 -0
  43. package/dist/commands/comment-char-config.d.ts +15 -0
  44. package/dist/commands/comment-char-config.d.ts.map +1 -1
  45. package/dist/commands/commit-command.d.ts +30 -2
  46. package/dist/commands/commit-command.d.ts.map +1 -1
  47. package/dist/commands/config-command.d.ts +12 -0
  48. package/dist/commands/config-command.d.ts.map +1 -1
  49. package/dist/commands/credential-helper.d.ts +214 -0
  50. package/dist/commands/credential-helper.d.ts.map +1 -0
  51. package/dist/commands/default-branch.d.ts +33 -3
  52. package/dist/commands/default-branch.d.ts.map +1 -1
  53. package/dist/commands/diff-color-config.d.ts +4 -0
  54. package/dist/commands/diff-color-config.d.ts.map +1 -1
  55. package/dist/commands/diff-command.d.ts +12 -0
  56. package/dist/commands/diff-command.d.ts.map +1 -1
  57. package/dist/commands/fast-export/fast-export-command.d.ts +89 -2
  58. package/dist/commands/fast-export/fast-export-command.d.ts.map +1 -1
  59. package/dist/commands/fast-export/fast-import-command.d.ts +113 -8
  60. package/dist/commands/fast-export/fast-import-command.d.ts.map +1 -1
  61. package/dist/commands/fast-export/fast-stream-support.d.ts +0 -9
  62. package/dist/commands/fast-export/fast-stream-support.d.ts.map +1 -1
  63. package/dist/commands/fetch-command.d.ts +23 -1
  64. package/dist/commands/fetch-command.d.ts.map +1 -1
  65. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -1
  66. package/dist/commands/git-hooks.d.ts +31 -3
  67. package/dist/commands/git-hooks.d.ts.map +1 -1
  68. package/dist/commands/history-command.d.ts +173 -7
  69. package/dist/commands/history-command.d.ts.map +1 -1
  70. package/dist/commands/index-host.d.ts +19 -0
  71. package/dist/commands/index-host.d.ts.map +1 -1
  72. package/dist/commands/index-refresh.d.ts +36 -0
  73. package/dist/commands/index-refresh.d.ts.map +1 -0
  74. package/dist/commands/index.d.ts +9 -0
  75. package/dist/commands/index.d.ts.map +1 -1
  76. package/dist/commands/index.js +7 -6
  77. package/dist/commands/init-command.d.ts +20 -0
  78. package/dist/commands/init-command.d.ts.map +1 -1
  79. package/dist/commands/local-transport.d.ts +40 -2
  80. package/dist/commands/local-transport.d.ts.map +1 -1
  81. package/dist/commands/log-command.d.ts +7 -0
  82. package/dist/commands/log-command.d.ts.map +1 -1
  83. package/dist/commands/log-output.d.ts +11 -0
  84. package/dist/commands/log-output.d.ts.map +1 -1
  85. package/dist/commands/ls-tree-command.d.ts +10 -0
  86. package/dist/commands/ls-tree-command.d.ts.map +1 -1
  87. package/dist/commands/maintenance-schedule-command.d.ts +122 -0
  88. package/dist/commands/maintenance-schedule-command.d.ts.map +1 -0
  89. package/dist/commands/merge-command.d.ts +13 -1
  90. package/dist/commands/merge-command.d.ts.map +1 -1
  91. package/dist/commands/merge-index-command.d.ts +42 -0
  92. package/dist/commands/merge-index-command.d.ts.map +1 -0
  93. package/dist/commands/merge-one-file.d.ts +129 -0
  94. package/dist/commands/merge-one-file.d.ts.map +1 -0
  95. package/dist/commands/merge-support.d.ts +40 -4
  96. package/dist/commands/merge-support.d.ts.map +1 -1
  97. package/dist/commands/object-disk-info.d.ts +35 -0
  98. package/dist/commands/object-disk-info.d.ts.map +1 -0
  99. package/dist/commands/octopus-merge.d.ts +3 -3
  100. package/dist/commands/octopus-merge.d.ts.map +1 -1
  101. package/dist/commands/plumbing-input.d.ts.map +1 -1
  102. package/dist/commands/promisor-remote-reply.d.ts +31 -4
  103. package/dist/commands/promisor-remote-reply.d.ts.map +1 -1
  104. package/dist/commands/promisor-remote.d.ts +8 -1
  105. package/dist/commands/promisor-remote.d.ts.map +1 -1
  106. package/dist/commands/pull-command.d.ts +55 -7
  107. package/dist/commands/pull-command.d.ts.map +1 -1
  108. package/dist/commands/push-command.d.ts +18 -1
  109. package/dist/commands/push-command.d.ts.map +1 -1
  110. package/dist/commands/range-diff/range-diff-command.d.ts +37 -1
  111. package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -1
  112. package/dist/commands/rebase-command.d.ts +13 -1
  113. package/dist/commands/rebase-command.d.ts.map +1 -1
  114. package/dist/commands/ref-filter/for-each-ref-command.d.ts +3 -2
  115. package/dist/commands/ref-filter/for-each-ref-command.d.ts.map +1 -1
  116. package/dist/commands/ref-filter/index.d.ts +1 -0
  117. package/dist/commands/ref-filter/index.d.ts.map +1 -1
  118. package/dist/commands/ref-filter/peel-limit.d.ts +8 -0
  119. package/dist/commands/ref-filter/peel-limit.d.ts.map +1 -0
  120. package/dist/commands/ref-filter/ref-filter.d.ts.map +1 -1
  121. package/dist/commands/ref-filter/ref-format.d.ts +26 -1
  122. package/dist/commands/ref-filter/ref-format.d.ts.map +1 -1
  123. package/dist/commands/ref-filter/ref-listing-command.d.ts +9 -0
  124. package/dist/commands/ref-filter/ref-listing-command.d.ts.map +1 -1
  125. package/dist/commands/remote-transport.d.ts +27 -0
  126. package/dist/commands/remote-transport.d.ts.map +1 -1
  127. package/dist/commands/repack-command.d.ts +18 -2
  128. package/dist/commands/repack-command.d.ts.map +1 -1
  129. package/dist/commands/repo-command.d.ts +2 -7
  130. package/dist/commands/repo-command.d.ts.map +1 -1
  131. package/dist/commands/reset-command.d.ts +4 -1
  132. package/dist/commands/reset-command.d.ts.map +1 -1
  133. package/dist/commands/restore-command.d.ts.map +1 -1
  134. package/dist/commands/rev-list-command.d.ts +65 -0
  135. package/dist/commands/rev-list-command.d.ts.map +1 -1
  136. package/dist/commands/revert-command.d.ts +3 -0
  137. package/dist/commands/revert-command.d.ts.map +1 -1
  138. package/dist/commands/revision-filenames.d.ts.map +1 -1
  139. package/dist/commands/revision-range.d.ts +17 -0
  140. package/dist/commands/revision-range.d.ts.map +1 -1
  141. package/dist/commands/revision-resolver.d.ts +12 -0
  142. package/dist/commands/revision-resolver.d.ts.map +1 -1
  143. package/dist/commands/serve-repository.d.ts +21 -4
  144. package/dist/commands/serve-repository.d.ts.map +1 -1
  145. package/dist/commands/served-lazy-fetch.d.ts +61 -0
  146. package/dist/commands/served-lazy-fetch.d.ts.map +1 -0
  147. package/dist/commands/signing.d.ts +37 -4
  148. package/dist/commands/signing.d.ts.map +1 -1
  149. package/dist/commands/split-cmdline.d.ts +6 -0
  150. package/dist/commands/split-cmdline.d.ts.map +1 -0
  151. package/dist/commands/status-color-config.d.ts +33 -0
  152. package/dist/commands/status-color-config.d.ts.map +1 -0
  153. package/dist/commands/status-command.d.ts +15 -0
  154. package/dist/commands/status-command.d.ts.map +1 -1
  155. package/dist/commands/status-format.d.ts +46 -4
  156. package/dist/commands/status-format.d.ts.map +1 -1
  157. package/dist/commands/tag-command.d.ts +15 -1
  158. package/dist/commands/tag-command.d.ts.map +1 -1
  159. package/dist/commands/tree-restore.d.ts +14 -0
  160. package/dist/commands/tree-restore.d.ts.map +1 -1
  161. package/dist/commands/verify-command.d.ts +22 -2
  162. package/dist/commands/verify-command.d.ts.map +1 -1
  163. package/dist/commands/worktree-add-command.d.ts.map +1 -1
  164. package/dist/commands/worktree-overwrite-checks.d.ts +31 -6
  165. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
  166. package/dist/{commands-CPceUCP2.js → commands-DT3VEv50.js} +15168 -16842
  167. package/dist/errors/commit-errors.d.ts +37 -1
  168. package/dist/errors/commit-errors.d.ts.map +1 -1
  169. package/dist/errors/git-api-error.d.ts +6 -0
  170. package/dist/errors/git-api-error.d.ts.map +1 -1
  171. package/dist/errors/index.js +44 -3
  172. package/dist/{fsck-tbrPZX2q.js → fsck-BZ508Elg.js} +2 -2
  173. package/dist/git-command.d.ts +33 -1
  174. package/dist/git-command.d.ts.map +1 -1
  175. package/dist/git.d.ts +104 -1
  176. package/dist/git.d.ts.map +1 -1
  177. package/dist/index.js +8 -7
  178. package/dist/{remote-transport-C0OSJtor.js → local-transport-D98U6p4d.js} +228 -692
  179. package/dist/{maintenance-run-command-UiSIaeP8.js → maintenance-run-command-DZFnbnbo.js} +2366 -108
  180. package/dist/remote-config/remote-config-store.d.ts +6 -0
  181. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  182. package/dist/remote-transport-zvSqwpOB.js +4865 -0
  183. package/dist/results/cherry-pick-result.d.ts +4 -3
  184. package/dist/results/cherry-pick-result.d.ts.map +1 -1
  185. package/dist/results/diff-output.d.ts +7 -0
  186. package/dist/results/diff-output.d.ts.map +1 -1
  187. package/dist/results/diff-words.d.ts +14 -3
  188. package/dist/results/diff-words.d.ts.map +1 -1
  189. package/dist/results/index.js +1 -1
  190. package/dist/results/line-prefix-sink.d.ts +50 -0
  191. package/dist/results/line-prefix-sink.d.ts.map +1 -0
  192. package/dist/results/merge-result.d.ts +4 -3
  193. package/dist/results/merge-result.d.ts.map +1 -1
  194. package/dist/results/rebase-result.d.ts +5 -0
  195. package/dist/results/rebase-result.d.ts.map +1 -1
  196. package/dist/results/revert-result.d.ts +4 -3
  197. package/dist/results/revert-result.d.ts.map +1 -1
  198. package/dist/{results-CkS1p1CH.js → results-rF81cQse.js} +1788 -247
  199. package/dist/transport-command.d.ts +13 -0
  200. package/dist/transport-command.d.ts.map +1 -1
  201. package/package.json +21 -13
  202. package/src/commands/add-command.ts +15 -2
  203. package/src/commands/add-patch/patch-backend.ts +6 -44
  204. package/src/commands/archive/archive-command.ts +7 -3
  205. package/src/commands/archive/remote-archive-command.ts +45 -4
  206. package/src/commands/archive/upload-archive-writer.ts +34 -22
  207. package/src/commands/branch-command.ts +57 -4
  208. package/src/commands/bundle/bundle-format.ts +10 -2
  209. package/src/commands/bundle/bundle-uri-host.ts +71 -11
  210. package/src/commands/bundle/bundle-uri.ts +11 -4
  211. package/src/commands/bundle/bundle-verify.ts +4 -1
  212. package/src/commands/cat-file/batch-format.ts +17 -18
  213. package/src/commands/cat-file/cat-file-command.ts +182 -48
  214. package/src/commands/check-ignore-command.ts +110 -21
  215. package/src/commands/checkout-command.ts +149 -21
  216. package/src/commands/checkout-index-command.ts +2 -1
  217. package/src/commands/checkout-paths.ts +13 -0
  218. package/src/commands/cherry-pick-command.ts +9 -0
  219. package/src/commands/clean-interactive.ts +20 -74
  220. package/src/commands/clone-command.ts +102 -11
  221. package/src/commands/column/column-config.ts +63 -0
  222. package/src/commands/column/column-layout.ts +396 -0
  223. package/src/commands/column/index.ts +2 -0
  224. package/src/commands/column-command.ts +171 -0
  225. package/src/commands/comment-char-config.ts +42 -0
  226. package/src/commands/commit-command.ts +93 -18
  227. package/src/commands/config-command.ts +28 -0
  228. package/src/commands/credential-helper.ts +807 -0
  229. package/src/commands/default-branch.ts +57 -5
  230. package/src/commands/diff-color-config.ts +23 -10
  231. package/src/commands/diff-command.ts +25 -2
  232. package/src/commands/fast-export/fast-export-command.ts +661 -35
  233. package/src/commands/fast-export/fast-import-command.ts +694 -94
  234. package/src/commands/fast-export/fast-stream-support.ts +18 -1
  235. package/src/commands/fetch-command.ts +116 -16
  236. package/src/commands/fsck/fsck-object-checks.ts +3 -1
  237. package/src/commands/git-hooks.ts +96 -5
  238. package/src/commands/history-command.ts +952 -20
  239. package/src/commands/index-host.ts +51 -0
  240. package/src/commands/index-refresh.ts +121 -0
  241. package/src/commands/index.ts +9 -0
  242. package/src/commands/init-command.ts +57 -4
  243. package/src/commands/legacy-remotes.ts +4 -20
  244. package/src/commands/local-transport.ts +183 -18
  245. package/src/commands/log-command.ts +39 -1
  246. package/src/commands/log-output.ts +31 -4
  247. package/src/commands/ls-tree-command.ts +20 -12
  248. package/src/commands/maintenance-schedule-command.ts +418 -0
  249. package/src/commands/merge-command.ts +42 -8
  250. package/src/commands/merge-index-command.ts +202 -0
  251. package/src/commands/merge-one-file.ts +451 -0
  252. package/src/commands/merge-support.ts +169 -15
  253. package/src/commands/object-disk-info.ts +169 -0
  254. package/src/commands/octopus-merge.ts +37 -128
  255. package/src/commands/plumbing-input.ts +3 -1
  256. package/src/commands/promisor-remote-reply.ts +338 -56
  257. package/src/commands/promisor-remote.ts +39 -6
  258. package/src/commands/pull-command.ts +173 -27
  259. package/src/commands/push-command.ts +55 -6
  260. package/src/commands/range-diff/range-diff-command.ts +86 -3
  261. package/src/commands/read-tree-command.ts +2 -2
  262. package/src/commands/rebase-command.ts +46 -1
  263. package/src/commands/ref-filter/for-each-ref-command.ts +4 -3
  264. package/src/commands/ref-filter/index.ts +1 -0
  265. package/src/commands/ref-filter/peel-limit.ts +7 -0
  266. package/src/commands/ref-filter/ref-filter.ts +68 -7
  267. package/src/commands/ref-filter/ref-format.ts +85 -11
  268. package/src/commands/ref-filter/ref-listing-command.ts +30 -0
  269. package/src/commands/remote-command.ts +2 -2
  270. package/src/commands/remote-transport.ts +66 -3
  271. package/src/commands/repack-command.ts +75 -9
  272. package/src/commands/repo-command.ts +6 -70
  273. package/src/commands/reset-command.ts +11 -1
  274. package/src/commands/restore-command.ts +1 -0
  275. package/src/commands/rev-list-command.ts +205 -3
  276. package/src/commands/revert-command.ts +9 -0
  277. package/src/commands/revision-filenames.ts +32 -4
  278. package/src/commands/revision-range.ts +2 -2
  279. package/src/commands/revision-resolver.ts +21 -1
  280. package/src/commands/serve-repository.ts +42 -11
  281. package/src/commands/served-lazy-fetch.ts +164 -0
  282. package/src/commands/signing.ts +103 -12
  283. package/src/commands/split-cmdline.ts +32 -0
  284. package/src/commands/status-color-config.ts +92 -0
  285. package/src/commands/status-command.ts +50 -2
  286. package/src/commands/status-format.ts +198 -50
  287. package/src/commands/tag-command.ts +57 -2
  288. package/src/commands/tree-restore.ts +17 -0
  289. package/src/commands/verify-command.ts +84 -12
  290. package/src/commands/worktree-add-command.ts +2 -0
  291. package/src/commands/worktree-overwrite-checks.ts +75 -14
  292. package/src/errors/commit-errors.ts +52 -2
  293. package/src/errors/git-api-error.ts +7 -0
  294. package/src/git-command.ts +105 -4
  295. package/src/git.ts +153 -0
  296. package/src/remote-config/remote-config-store.ts +16 -0
  297. package/src/results/cherry-pick-result.ts +4 -3
  298. package/src/results/diff-output.ts +8 -0
  299. package/src/results/diff-words.ts +160 -32
  300. package/src/results/line-prefix-sink.ts +102 -0
  301. package/src/results/merge-result.ts +4 -3
  302. package/src/results/rebase-result.ts +5 -0
  303. package/src/results/revert-result.ts +4 -3
  304. package/src/transport-command.ts +79 -3
@@ -0,0 +1,418 @@
1
+ /**
2
+ * `git maintenance register`, `unregister`, `start` and `stop` (builtin/gc.c's
3
+ * `maintenance_register`, `maintenance_unregister`, `maintenance_start`, `maintenance_stop`).
4
+ *
5
+ * register and unregister are configuration edits: the repository's path (its worktree top,
6
+ * the git directory when bare, real path) goes into, or out of, `maintenance.repo` of the
7
+ * global configuration or `--config-file`, and register turns the repository's foreground
8
+ * maintenance off (`maintenance.auto=false`) and gives it the `incremental` strategy unless
9
+ * one is set. Every edit goes through {@link ConfigCommand}, so the files' bytes are git's.
10
+ *
11
+ * start and stop drive the operating system's scheduler, which is the host's
12
+ * ({@link GitHostEnvironment.maintenanceScheduler}): start disables the runs of every other
13
+ * scheduler the host has, enables those of the chosen one, then registers; stop disables
14
+ * them on every scheduler and leaves the registration alone. Both hold
15
+ * `<objects>/schedule.lock` while they do. Without a host scheduler git finds none: start
16
+ * dies as git does, stop does nothing.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * await git.maintenanceRegister().call(); // { exitCode: 0, stderr: "" }
21
+ * await git.maintenanceUnregister().setForce(true).call();
22
+ * await git.maintenanceStart().setScheduler("crontab").call(); // needs a host scheduler
23
+ * ```
24
+ */
25
+
26
+ import { atomicFilesOver } from "@statewalker/vcs-utils/files";
27
+ import type {
28
+ GitHostEnvironment,
29
+ MaintenanceFrequency,
30
+ MaintenanceScheduleRequest,
31
+ MaintenanceSchedulerName,
32
+ } from "@statewalker/vcs-working-tree";
33
+ import { expandUserPath } from "@statewalker/vcs-working-tree";
34
+ import { GitApiError } from "../errors/index.js";
35
+ import { GitCommand } from "../git-command.js";
36
+ import { ConfigCommand } from "./config-command.js";
37
+ import { hookHostOf } from "./git-hooks.js";
38
+ import { hostEnvironmentOf } from "./git-ident.js";
39
+ import { objectsDirectoryDisplay } from "./object-store/objects-directory.js";
40
+ import { worktreeHostPath } from "./submodule-model.js";
41
+
42
+ /** How a maintenance subcommand ended, as git's process. */
43
+ export interface MaintenanceScheduleResult {
44
+ /** git's exit status: 0, 128 for a death, 129 for a usage error, 255 for an `error()` return */
45
+ exitCode: number;
46
+ /** git's stderr */
47
+ stderr: string;
48
+ }
49
+
50
+ const KEY = "maintenance.repo";
51
+ const FREQUENCIES: readonly MaintenanceFrequency[] = ["hourly", "daily", "weekly"];
52
+ /** builtin/gc.c's `scheduler_fn[]` order (after `SCHEDULER_AUTO`) */
53
+ const SCHEDULERS: readonly MaintenanceSchedulerName[] = [
54
+ "crontab",
55
+ "systemctl",
56
+ "launchctl",
57
+ "schtasks",
58
+ ];
59
+
60
+ /** A death of git's: its stderr lines and exit status. */
61
+ class Death {
62
+ constructor(
63
+ readonly exitCode: number,
64
+ readonly lines: string[],
65
+ ) {}
66
+ }
67
+
68
+ /** The shared part: the repository's path, git's version, the config edits and deaths. */
69
+ abstract class MaintenanceScheduleBase extends GitCommand<MaintenanceScheduleResult> {
70
+ protected stderrLines: string[] = [];
71
+
72
+ protected get v243(): boolean {
73
+ return this.hooks.compatibility === "2.43";
74
+ }
75
+
76
+ async call(): Promise<MaintenanceScheduleResult> {
77
+ this.checkCallable();
78
+ this.setCallable(false);
79
+ let exitCode: number;
80
+ try {
81
+ exitCode = await this.run();
82
+ } catch (error) {
83
+ if (!(error instanceof Death)) throw error;
84
+ this.stderrLines.push(...error.lines);
85
+ exitCode = error.exitCode;
86
+ }
87
+ return { exitCode, stderr: this.stderrLines.map((l) => `${l}\n`).join("") };
88
+ }
89
+
90
+ protected abstract run(): Promise<number>;
91
+
92
+ /** git's `get_maintpath`: the real path of the worktree top, the git directory when bare. */
93
+ protected async maintPath(): Promise<string> {
94
+ // a bare repository's working copy has no `worktree` (and refuses to be asked for one)
95
+ const top =
96
+ "worktree" in this.workingCopy && this.worktreeAccess
97
+ ? await worktreeHostPath(this.workingCopy)
98
+ : undefined;
99
+ if (top !== undefined) return top;
100
+ const layout = await hookHostOf(this.workingCopy).layout();
101
+ const root = (layout?.files as { rootDirectory?: string } | undefined)?.rootDirectory;
102
+ if (!layout || !root) {
103
+ throw new GitApiError("git maintenance needs the repository's path on the host");
104
+ }
105
+ const rest = layout.gitDir.replace(/^\/+|\/+$/g, "");
106
+ const path = `${root.replace(/\/+$/, "")}${rest ? `/${rest}` : ""}`;
107
+ const environment = hostEnvironmentOf(this.workingCopy);
108
+ return (await environment?.realPath?.(path).catch(() => undefined)) ?? path;
109
+ }
110
+
111
+ /** A config command over this working copy, aimed at `--config-file`, else at `scope`. */
112
+ protected async config(
113
+ file: string | undefined,
114
+ scope: "local" | "global" | "all",
115
+ ): Promise<ConfigCommand> {
116
+ const command = new ConfigCommand(this.workingCopy).setCompatibility(
117
+ this.v243 ? "2.43" : "2.56",
118
+ );
119
+ if (file !== undefined) return command.setFile(file);
120
+ if (scope === "local") return command;
121
+ if (scope === "all") return command;
122
+ // 2.43's `git_global_config(&user, &xdg)` writes `~/.gitconfig` (or $GIT_CONFIG_GLOBAL)
123
+ // even where only the XDG file exists; 2.5x's `git_global_config()` is ConfigCommand's
124
+ if (!this.v243) return command.setScope("global");
125
+ const environment = hostEnvironmentOf(this.workingCopy);
126
+ const path = environment?.globalConfig ?? expandUserPath("~/.gitconfig", environment);
127
+ if (path === undefined) throw new Death(128, ["fatal: $HOME not set"]);
128
+ return command.setFile(path);
129
+ }
130
+
131
+ /**
132
+ * `repo_config_get_string_multi` (or a config set of one file): the values, or undefined
133
+ * when a key without a value makes git refuse the list (`error: missing value for ...`).
134
+ */
135
+ protected async repoValues(file: string | undefined): Promise<string[] | undefined> {
136
+ const values = await (await this.config(file, "all")).get(KEY).callValues();
137
+ if (values.some((v) => v === undefined)) {
138
+ this.stderrLines.push(`error: missing value for '${KEY}'`);
139
+ return undefined;
140
+ }
141
+ return values as string[];
142
+ }
143
+
144
+ /** `repo_config_set`: the repository's config set, or git's death. */
145
+ protected async setLocal(key: string, value: string): Promise<void> {
146
+ const outcome = await (await this.config(undefined, "local")).set(key, value).call();
147
+ if (outcome.exitCode !== 0) {
148
+ throw new Death(128, [
149
+ ...lines(outcome.stderr),
150
+ `fatal: could not set '${key}' to '${value}'`,
151
+ ]);
152
+ }
153
+ }
154
+
155
+ /** maintenance_register's body after its options: the local settings, then the path. */
156
+ protected async register(file: string | undefined): Promise<void> {
157
+ const path = await this.maintPath();
158
+ await this.setLocal("maintenance.auto", "false");
159
+ const strategy = await (await this.config(undefined, "all"))
160
+ .get("maintenance.strategy")
161
+ .callValues();
162
+ if (strategy.length === 0) await this.setLocal("maintenance.strategy", "incremental");
163
+ const values = await this.repoValues(undefined);
164
+ if (values?.includes(path)) return;
165
+ const outcome = await (await this.config(file, "global")).add(KEY, path).call();
166
+ if (outcome.exitCode !== 0) {
167
+ throw new Death(128, [
168
+ ...lines(outcome.stderr),
169
+ `fatal: unable to add '${KEY}' value of '${path}'`,
170
+ ]);
171
+ }
172
+ }
173
+
174
+ /**
175
+ * update_background_schedule: under `<objects>/schedule.lock`, every available scheduler
176
+ * but the one enabled told to disable (their failures ignored), then the chosen one to
177
+ * enable. Returns git's result: 0, or -1 for a failure.
178
+ */
179
+ protected async updateSchedule(enable: MaintenanceSchedulerName | undefined): Promise<number> {
180
+ const objects = await this.hooks.commonGitFile("objects");
181
+ if (!objects) throw new GitApiError("git maintenance needs a files repository");
182
+ const atomic = atomicFilesOver(objects.files, objects.path);
183
+ if (!(await atomic.createExclusive("schedule.lock", new Uint8Array(0)))) {
184
+ if (this.v243) {
185
+ this.stderrLines.push("error: another process is scheduling background maintenance");
186
+ } else {
187
+ // absolute_path("<objects>/schedule"): the objects directory as git names it, from
188
+ // where git runs (the worktree top, the git directory when bare)
189
+ const display = await objectsDirectoryDisplay(this.hooks, this.workingCopy);
190
+ const base = display.startsWith("/") ? display : `${await this.maintPath()}/${display}`;
191
+ const lockPath = `${base}/schedule.lock`;
192
+ this.stderrLines.push(
193
+ `error: unable to create '${lockPath}': File exists.`,
194
+ "",
195
+ "Another scheduled git-maintenance(1) process seems to be running in this",
196
+ "repository. Please make sure no other maintenance processes are running and",
197
+ "then try again. If it still fails, a git-maintenance(1) process may have",
198
+ "crashed in this repository earlier: remove the file manually to continue.",
199
+ );
200
+ }
201
+ return -1;
202
+ }
203
+ try {
204
+ const host = hostEnvironmentOf(this.workingCopy)?.maintenanceScheduler;
205
+ if (!host) return enable === undefined ? 0 : -1;
206
+ for (const scheduler of SCHEDULERS) {
207
+ if (scheduler === enable) continue;
208
+ if (!(await host.isAvailable(scheduler))) continue;
209
+ // git ignores what disabling another scheduler returns, but prints its errors
210
+ await this.askScheduler(this.request(scheduler, false));
211
+ }
212
+ if (enable === undefined) return 0;
213
+ return (await this.askScheduler(this.request(enable, true))) ? 0 : -1;
214
+ } finally {
215
+ await atomic.remove("schedule.lock");
216
+ }
217
+ }
218
+
219
+ private request(
220
+ scheduler: MaintenanceSchedulerName,
221
+ enable: boolean,
222
+ ): MaintenanceScheduleRequest {
223
+ const v243 = this.v243;
224
+ return {
225
+ scheduler,
226
+ enable,
227
+ frequencies: FREQUENCIES,
228
+ command: (frequency) => [
229
+ ...(v243 ? [] : ["-c", "credential.interactive=false", "-c", "core.askPass=true"]),
230
+ "for-each-repo",
231
+ ...(v243 ? [] : ["--keep-going"]),
232
+ `--config=${KEY}`,
233
+ "maintenance",
234
+ "run",
235
+ `--schedule=${frequency}`,
236
+ ],
237
+ minute: Math.floor(Math.random() * 60),
238
+ };
239
+ }
240
+
241
+ /** One scheduler update: whether it succeeded, its `error:` lines printed. */
242
+ private async askScheduler(request: MaintenanceScheduleRequest): Promise<boolean> {
243
+ const host = hostEnvironmentOf(this.workingCopy)?.maintenanceScheduler;
244
+ if (!host) return false;
245
+ const outcome = await host.updateSchedule(request);
246
+ if (outcome === undefined || outcome === true) return true;
247
+ if (outcome === false) return false;
248
+ this.stderrLines.push(...(outcome.errors ?? []));
249
+ return outcome.ok;
250
+ }
251
+ }
252
+
253
+ function lines(text: string): string[] {
254
+ return text === "" ? [] : text.replace(/\n$/, "").split("\n");
255
+ }
256
+
257
+ /**
258
+ * `git maintenance register [--config-file <path>]`: `maintenance.auto=false` and, when no
259
+ * strategy is configured, `maintenance.strategy=incremental` in the repository's config,
260
+ * then the repository's path added to `maintenance.repo` of the global configuration (or of
261
+ * {@link setConfigFile}) unless the whole configuration already lists it.
262
+ */
263
+ export class MaintenanceRegisterCommand extends MaintenanceScheduleBase {
264
+ private configFile?: string;
265
+
266
+ /**
267
+ * `--config-file <path>`: the file the path is added to, an absolute host path or one
268
+ * relative to the worktree top. The "already registered" check still reads the whole
269
+ * configuration, as git's does.
270
+ */
271
+ setConfigFile(path: string | undefined): this {
272
+ this.checkCallable();
273
+ this.configFile = path;
274
+ return this;
275
+ }
276
+
277
+ protected async run(): Promise<number> {
278
+ await this.register(this.configFile);
279
+ return 0;
280
+ }
281
+ }
282
+
283
+ /**
284
+ * `git maintenance unregister [--config-file <path>] [--force]`: the repository's path
285
+ * removed from `maintenance.repo` of the global configuration (or of {@link setConfigFile},
286
+ * the only file then read), the section dropped when it empties. Dies
287
+ * `repository '<path>' is not registered` (128) when the configuration does not list it,
288
+ * unless {@link setForce}.
289
+ */
290
+ export class MaintenanceUnregisterCommand extends MaintenanceScheduleBase {
291
+ private configFile?: string;
292
+ private force = false;
293
+
294
+ /** `--config-file <path>`: the only file read and written (absolute, or from the worktree top). */
295
+ setConfigFile(path: string | undefined): this {
296
+ this.checkCallable();
297
+ this.configFile = path;
298
+ return this;
299
+ }
300
+
301
+ /** `--force`: success even when the repository is not registered. */
302
+ setForce(force: boolean): this {
303
+ this.checkCallable();
304
+ this.force = force;
305
+ return this;
306
+ }
307
+
308
+ protected async run(): Promise<number> {
309
+ const path = await this.maintPath();
310
+ const values = await this.repoValues(this.configFile);
311
+ if (values?.includes(path)) {
312
+ const outcome = await (await this.config(this.configFile, "global"))
313
+ .unset(KEY)
314
+ .setValuePattern(path)
315
+ .setFixedValue(true)
316
+ .setAll(true)
317
+ .call();
318
+ // CONFIG_NOTHING_SET (5): listed by another file than the one edited, fatal even forced
319
+ if (outcome.exitCode !== 0 && (!this.force || outcome.exitCode === 5)) {
320
+ throw new Death(128, [
321
+ ...lines(outcome.stderr),
322
+ `fatal: unable to unset '${KEY}' value of '${path}'`,
323
+ ]);
324
+ }
325
+ } else if (!this.force) {
326
+ throw new Death(128, [`fatal: repository '${path}' is not registered`]);
327
+ }
328
+ return 0;
329
+ }
330
+ }
331
+
332
+ /** `--scheduler` values git takes (`parse_scheduler`, case-insensitive). */
333
+ function parseScheduler(value: string): MaintenanceSchedulerName | "auto" | undefined {
334
+ switch (value.toLowerCase()) {
335
+ case "auto":
336
+ return "auto";
337
+ case "cron":
338
+ case "crontab":
339
+ return "crontab";
340
+ case "systemd":
341
+ case "systemd-timer":
342
+ return "systemctl";
343
+ case "launchctl":
344
+ return "launchctl";
345
+ case "schtasks":
346
+ return "schtasks";
347
+ default:
348
+ return undefined;
349
+ }
350
+ }
351
+
352
+ /**
353
+ * `git maintenance start [--scheduler=<scheduler>]`: the host scheduler
354
+ * ({@link GitHostEnvironment.maintenanceScheduler}) told to remove the runs of every other
355
+ * scheduler it has and to schedule the hourly, daily and weekly runs on the chosen one, then
356
+ * the repository registered ({@link MaintenanceRegisterCommand}). `auto` (the default) picks
357
+ * by the host's `systemName` as git does by its build: launchctl on Darwin, schtasks on
358
+ * Windows, systemd timers else crontab on Linux, crontab elsewhere.
359
+ */
360
+ export class MaintenanceStartCommand extends MaintenanceScheduleBase {
361
+ private scheduler = "auto";
362
+
363
+ /** `--scheduler=<scheduler>`: auto, crontab (cron), systemd (systemd-timer), launchctl, schtasks. */
364
+ setScheduler(scheduler: string): this {
365
+ this.checkCallable();
366
+ this.scheduler = scheduler;
367
+ return this;
368
+ }
369
+
370
+ protected async run(): Promise<number> {
371
+ const parsed = parseScheduler(this.scheduler);
372
+ if (parsed === undefined) {
373
+ throw new Death(129, [`error: unrecognized --scheduler argument '${this.scheduler}'`]);
374
+ }
375
+ const scheduler = await this.resolve(parsed);
376
+ if (!(await this.available(scheduler))) {
377
+ throw new Death(128, [`fatal: ${scheduler} scheduler is not available`]);
378
+ }
379
+ if ((await this.updateSchedule(scheduler)) !== 0) {
380
+ throw new Death(128, ["fatal: failed to set up maintenance schedule"]);
381
+ }
382
+ await this.register(undefined);
383
+ return 0;
384
+ }
385
+
386
+ private async available(scheduler: MaintenanceSchedulerName): Promise<boolean> {
387
+ const host = hostEnvironmentOf(this.workingCopy)?.maintenanceScheduler;
388
+ return host ? await host.isAvailable(scheduler) : false;
389
+ }
390
+
391
+ /** git's `resolve_scheduler`, by the host's system instead of git's build. */
392
+ private async resolve(
393
+ scheduler: MaintenanceSchedulerName | "auto",
394
+ ): Promise<MaintenanceSchedulerName> {
395
+ if (scheduler !== "auto") return scheduler;
396
+ const system = (hostEnvironmentOf(this.workingCopy) as GitHostEnvironment | undefined)
397
+ ?.systemName;
398
+ if (system === "Darwin") return "launchctl";
399
+ if (system !== undefined && /^windows/i.test(system)) return "schtasks";
400
+ if (system === "Linux") {
401
+ if (await this.available("systemctl")) return "systemctl";
402
+ if (await this.available("crontab")) return "crontab";
403
+ throw new Death(128, ["fatal: neither systemd timers nor crontab are available"]);
404
+ }
405
+ return "crontab";
406
+ }
407
+ }
408
+
409
+ /**
410
+ * `git maintenance stop`: the host scheduler told to remove the runs from every scheduler it
411
+ * has. The repository stays registered, as git leaves it. Exit 0 even with no scheduler
412
+ * (nothing to remove) or when a removal fails; 255 when another process holds the lock.
413
+ */
414
+ export class MaintenanceStopCommand extends MaintenanceScheduleBase {
415
+ protected async run(): Promise<number> {
416
+ return (await this.updateSchedule(undefined)) === 0 ? 0 : 255;
417
+ }
418
+ }
@@ -51,6 +51,7 @@ import {
51
51
  indexChanges,
52
52
  indexChangesFromTree,
53
53
  OperationState,
54
+ peelMergeHeads,
54
55
  readIndex,
55
56
  rewriteIndex,
56
57
  storeFlatTree,
@@ -148,6 +149,11 @@ export const UNMERGED_FILES_MESSAGE = (action: "Merging" | "Committing") =>
148
149
  */
149
150
  export class MergeCommand extends GitCommand<MergeResult> {
150
151
  private includes: string[] = [];
152
+ /**
153
+ * The object each merged commit was named by (git's `merge_remote_desc.obj`, the first
154
+ * name's): an annotated tag's own id, which `write_merge_heads` puts in MERGE_HEAD.
155
+ */
156
+ private readonly mergeObjects = new Map<ObjectId, ObjectId>();
151
157
  private fastForwardMode = FastForwardMode.FF;
152
158
  private strategy = MergeStrategy.RECURSIVE;
153
159
  /** Whether `-s` was given (`-s recursive` names itself in git's messages) */
@@ -578,6 +584,11 @@ export class MergeCommand extends GitCommand<MergeResult> {
578
584
  return this;
579
585
  }
580
586
 
587
+ /** git sets `warn_on_auto_comment_char`: 2.5x warns of `core.commentChar=auto` here. */
588
+ protected override autoCommentCharWarningEntryPoints(): readonly string[] {
589
+ return ["call"];
590
+ }
591
+
581
592
  /**
582
593
  * Execute the merge.
583
594
  *
@@ -668,7 +679,9 @@ export class MergeCommand extends GitCommand<MergeResult> {
668
679
  {
669
680
  const targetTree = await this.commits.getTree(mergeHead);
670
681
  const target = await flatTree(this.trees, targetTree);
682
+ // pull_into_void's checkout_fast_forward: the commit merged, no ref
671
683
  const refused = await switchTrees(this.context(worktree), new Map(), target, {
684
+ metadata: { treeish: mergeHead },
672
685
  action: "merge",
673
686
  operation: "merge",
674
687
  targetTree,
@@ -717,6 +730,7 @@ export class MergeCommand extends GitCommand<MergeResult> {
717
730
  const targetTree = await this.commits.getTree(to);
718
731
  const target = await flatTree(this.trees, targetTree);
719
732
  const refused = await switchTrees(this.context(worktree), head, target, {
733
+ metadata: { treeish: to },
720
734
  action: "merge",
721
735
  operation: "merge",
722
736
  targetTree,
@@ -1057,7 +1071,9 @@ export class MergeCommand extends GitCommand<MergeResult> {
1057
1071
  const headTree = await flatTree(this.trees, await this.commits.getTree(headId));
1058
1072
  const srcTreeId = await this.commits.getTree(srcId);
1059
1073
  const srcTree = await flatTree(this.trees, srcTreeId);
1074
+ // checkout_fast_forward's init_checkout_metadata: the commit merged, no ref
1060
1075
  const refused = await switchTrees(this.context(worktree), headTree, srcTree, {
1076
+ metadata: { treeish: srcId },
1061
1077
  action: "merge",
1062
1078
  operation: "merge",
1063
1079
  targetTree: srcTreeId,
@@ -1514,7 +1530,10 @@ export class MergeCommand extends GitCommand<MergeResult> {
1514
1530
  return giveUp([refused.message], stop ? output.slice(0, stop.output) : output);
1515
1531
  }
1516
1532
  const writeState = async (text: string) => {
1517
- await state.write("MERGE_HEAD", remoteIds.map((id) => `${id}\n`).join(""));
1533
+ await state.write(
1534
+ "MERGE_HEAD",
1535
+ remoteIds.map((id) => `${this.mergeObjectOf(id)}\n`).join(""),
1536
+ );
1518
1537
  await state.write("MERGE_MSG", text);
1519
1538
  await state.write("MERGE_MODE", this.ffMode() === FastForwardMode.NO_FF ? "no-ff" : "");
1520
1539
  };
@@ -1590,13 +1609,23 @@ export class MergeCommand extends GitCommand<MergeResult> {
1590
1609
  return { status: MergeStatus.MERGED, newHead: newCommitId, mergedCommits, output };
1591
1610
  }
1592
1611
 
1593
- /** The heads MERGE_HEAD names (one per line: several for an octopus). */
1612
+ /**
1613
+ * The commits MERGE_HEAD names (one per line: several for an octopus), each peeled as
1614
+ * git's `read_merge_heads` → `lookup_commit_reference` (a merged tag is named by its own id).
1615
+ */
1594
1616
  private async mergeHeads(state: OperationState): Promise<ObjectId[]> {
1595
1617
  const text = await state.read("MERGE_HEAD");
1596
- const lines = (text ?? "").split("\n").filter((l) => /^[0-9a-f]{40,64}$/.test(l));
1597
- if (lines.length > 0) return lines;
1598
- const single = await state.ref("MERGE_HEAD");
1599
- return single ? [single] : [];
1618
+ let lines = (text ?? "").split("\n").filter((l) => /^[0-9a-f]{40,64}$/.test(l));
1619
+ if (lines.length === 0) {
1620
+ const single = await state.ref("MERGE_HEAD");
1621
+ lines = single ? [single] : [];
1622
+ }
1623
+ return peelMergeHeads(this.revisionResolver(), lines);
1624
+ }
1625
+
1626
+ /** The object a merged commit was named by (the commit itself unless a tag named it). */
1627
+ private mergeObjectOf(commitId: ObjectId): ObjectId {
1628
+ return this.mergeObjects.get(commitId) ?? commitId;
1600
1629
  }
1601
1630
 
1602
1631
  /** `git merge --continue`: commits the resolved merge with MERGE_MSG. */
@@ -2052,7 +2081,7 @@ export class MergeCommand extends GitCommand<MergeResult> {
2052
2081
  srcId: ObjectId,
2053
2082
  message: string,
2054
2083
  ): Promise<void> {
2055
- await state.setRef("MERGE_HEAD", srcId);
2084
+ await state.setRef("MERGE_HEAD", this.mergeObjectOf(srcId));
2056
2085
  await state.write("MERGE_MSG", message);
2057
2086
  await state.write("MERGE_MODE", this.ffMode() === FastForwardMode.NO_FF ? "no-ff" : "");
2058
2087
  }
@@ -2185,7 +2214,12 @@ export class MergeCommand extends GitCommand<MergeResult> {
2185
2214
  */
2186
2215
  private async mergeHeadOf(name: string): Promise<ObjectId> {
2187
2216
  try {
2188
- return await this.resolveRef(name);
2217
+ const commitId = await this.resolveRef(name);
2218
+ if (!this.mergeObjects.has(commitId)) {
2219
+ const objectId = await this.resolveRevision(name, { hint: "committish" });
2220
+ this.mergeObjects.set(commitId, objectId);
2221
+ }
2222
+ return commitId;
2189
2223
  } catch (error) {
2190
2224
  if (!(error instanceof NotACommitError)) throw error;
2191
2225
  const lines = error.objectType