@statewalker/vcs-commands 0.5.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 (561) hide show
  1. package/README.md +123 -8
  2. package/dist/commands/add-command.d.ts +265 -34
  3. package/dist/commands/add-command.d.ts.map +1 -1
  4. package/dist/commands/add-patch/add-interactive.d.ts +46 -0
  5. package/dist/commands/add-patch/add-interactive.d.ts.map +1 -0
  6. package/dist/commands/add-patch/add-patch.d.ts +139 -0
  7. package/dist/commands/add-patch/add-patch.d.ts.map +1 -0
  8. package/dist/commands/add-patch/index.d.ts +6 -0
  9. package/dist/commands/add-patch/index.d.ts.map +1 -0
  10. package/dist/commands/add-patch/patch-backend.d.ts +80 -0
  11. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -0
  12. package/dist/commands/add-patch/patch-modes.d.ts +71 -0
  13. package/dist/commands/add-patch/patch-modes.d.ts.map +1 -0
  14. package/dist/commands/add-patch/patch-session.d.ts +49 -0
  15. package/dist/commands/add-patch/patch-session.d.ts.map +1 -0
  16. package/dist/commands/am/am-command.d.ts +175 -0
  17. package/dist/commands/am/am-command.d.ts.map +1 -0
  18. package/dist/commands/am/index.d.ts +3 -0
  19. package/dist/commands/am/index.d.ts.map +1 -0
  20. package/dist/commands/am/mailinfo.d.ts +58 -0
  21. package/dist/commands/am/mailinfo.d.ts.map +1 -0
  22. package/dist/commands/am/patch-format.d.ts +24 -0
  23. package/dist/commands/am/patch-format.d.ts.map +1 -0
  24. package/dist/commands/apply/apply-command.d.ts +153 -0
  25. package/dist/commands/apply/apply-command.d.ts.map +1 -0
  26. package/dist/commands/apply/apply-engine.d.ts +195 -0
  27. package/dist/commands/apply/apply-engine.d.ts.map +1 -0
  28. package/dist/commands/apply/apply-parse.d.ts +86 -0
  29. package/dist/commands/apply/apply-parse.d.ts.map +1 -0
  30. package/dist/commands/apply/apply-types.d.ts +70 -0
  31. package/dist/commands/apply/apply-types.d.ts.map +1 -0
  32. package/dist/commands/apply/index.d.ts +6 -0
  33. package/dist/commands/apply/index.d.ts.map +1 -0
  34. package/dist/commands/apply/latin1.d.ts +32 -0
  35. package/dist/commands/apply/latin1.d.ts.map +1 -0
  36. package/dist/commands/apply/whitespace.d.ts +58 -0
  37. package/dist/commands/apply/whitespace.d.ts.map +1 -0
  38. package/dist/commands/attributes-source.d.ts +26 -0
  39. package/dist/commands/attributes-source.d.ts.map +1 -0
  40. package/dist/commands/backfill-command.d.ts +110 -0
  41. package/dist/commands/backfill-command.d.ts.map +1 -0
  42. package/dist/commands/bisect/bisect-checkout.d.ts +64 -0
  43. package/dist/commands/bisect/bisect-checkout.d.ts.map +1 -0
  44. package/dist/commands/bisect/bisect-command.d.ts +201 -0
  45. package/dist/commands/bisect/bisect-command.d.ts.map +1 -0
  46. package/dist/commands/bisect/bisect-engine.d.ts +219 -0
  47. package/dist/commands/bisect/bisect-engine.d.ts.map +1 -0
  48. package/dist/commands/bisect/bisect-graph.d.ts +127 -0
  49. package/dist/commands/bisect/bisect-graph.d.ts.map +1 -0
  50. package/dist/commands/bisect/bisect-quote.d.ts +27 -0
  51. package/dist/commands/bisect/bisect-quote.d.ts.map +1 -0
  52. package/dist/commands/bisect/index.d.ts +5 -0
  53. package/dist/commands/bisect/index.d.ts.map +1 -0
  54. package/dist/commands/blame-command.d.ts +122 -131
  55. package/dist/commands/blame-command.d.ts.map +1 -1
  56. package/dist/commands/branch-command.d.ts +52 -2
  57. package/dist/commands/branch-command.d.ts.map +1 -1
  58. package/dist/commands/branch-tracking.d.ts +98 -0
  59. package/dist/commands/branch-tracking.d.ts.map +1 -0
  60. package/dist/commands/checkout-command.d.ts +300 -18
  61. package/dist/commands/checkout-command.d.ts.map +1 -1
  62. package/dist/commands/checkout-paths.d.ts +124 -0
  63. package/dist/commands/checkout-paths.d.ts.map +1 -0
  64. package/dist/commands/cherry-pick-command.d.ts +50 -42
  65. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  66. package/dist/commands/clean-command.d.ts +49 -16
  67. package/dist/commands/clean-command.d.ts.map +1 -1
  68. package/dist/commands/clone-command.d.ts +134 -0
  69. package/dist/commands/clone-command.d.ts.map +1 -1
  70. package/dist/commands/comment-char-config.d.ts +35 -0
  71. package/dist/commands/comment-char-config.d.ts.map +1 -0
  72. package/dist/commands/commit-command.d.ts +311 -17
  73. package/dist/commands/commit-command.d.ts.map +1 -1
  74. package/dist/commands/core-config.d.ts +78 -1
  75. package/dist/commands/core-config.d.ts.map +1 -1
  76. package/dist/commands/date-format.d.ts +58 -0
  77. package/dist/commands/date-format.d.ts.map +1 -0
  78. package/dist/commands/default-branch.d.ts +70 -0
  79. package/dist/commands/default-branch.d.ts.map +1 -0
  80. package/dist/commands/describe-command.d.ts.map +1 -1
  81. package/dist/commands/diff-command.d.ts +377 -30
  82. package/dist/commands/diff-command.d.ts.map +1 -1
  83. package/dist/commands/diff-textconv.d.ts +40 -0
  84. package/dist/commands/diff-textconv.d.ts.map +1 -0
  85. package/dist/commands/fetch-command.d.ts +149 -10
  86. package/dist/commands/fetch-command.d.ts.map +1 -1
  87. package/dist/commands/format-patch-command.d.ts +164 -0
  88. package/dist/commands/format-patch-command.d.ts.map +1 -0
  89. package/dist/commands/fsck/fetch-fsck.d.ts +60 -0
  90. package/dist/commands/fsck/fetch-fsck.d.ts.map +1 -0
  91. package/dist/commands/fsck/fsck-command.d.ts +227 -0
  92. package/dist/commands/fsck/fsck-command.d.ts.map +1 -0
  93. package/dist/commands/fsck/fsck-loose.d.ts +71 -0
  94. package/dist/commands/fsck/fsck-loose.d.ts.map +1 -0
  95. package/dist/commands/fsck/fsck-msg-ids.d.ts +122 -0
  96. package/dist/commands/fsck/fsck-msg-ids.d.ts.map +1 -0
  97. package/dist/commands/fsck/fsck-object-checks.d.ts +159 -0
  98. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -0
  99. package/dist/commands/fsck/fsck-object-table.d.ts +140 -0
  100. package/dist/commands/fsck/fsck-object-table.d.ts.map +1 -0
  101. package/dist/commands/fsck/fsck-pack.d.ts +122 -0
  102. package/dist/commands/fsck/fsck-pack.d.ts.map +1 -0
  103. package/dist/commands/fsck/index.d.ts +6 -0
  104. package/dist/commands/fsck/index.d.ts.map +1 -0
  105. package/dist/commands/fsck/verify-pack-command.d.ts +81 -0
  106. package/dist/commands/fsck/verify-pack-command.d.ts.map +1 -0
  107. package/dist/commands/gc-command.d.ts +106 -39
  108. package/dist/commands/gc-command.d.ts.map +1 -1
  109. package/dist/commands/git-date.d.ts +2 -0
  110. package/dist/commands/git-date.d.ts.map +1 -0
  111. package/dist/commands/git-describe.d.ts +67 -0
  112. package/dist/commands/git-describe.d.ts.map +1 -0
  113. package/dist/commands/git-hooks.d.ts +424 -0
  114. package/dist/commands/git-hooks.d.ts.map +1 -0
  115. package/dist/commands/git-ident.d.ts +128 -0
  116. package/dist/commands/git-ident.d.ts.map +1 -0
  117. package/dist/commands/hook-command.d.ts +96 -0
  118. package/dist/commands/hook-command.d.ts.map +1 -0
  119. package/dist/commands/index-adder.d.ts +233 -0
  120. package/dist/commands/index-adder.d.ts.map +1 -0
  121. package/dist/commands/index-host.d.ts +86 -0
  122. package/dist/commands/index-host.d.ts.map +1 -0
  123. package/dist/commands/index.d.ts +56 -0
  124. package/dist/commands/index.d.ts.map +1 -1
  125. package/dist/commands/index.js +3 -2
  126. package/dist/commands/init-command.d.ts +62 -4
  127. package/dist/commands/init-command.d.ts.map +1 -1
  128. package/dist/commands/legacy-remotes.d.ts +55 -0
  129. package/dist/commands/legacy-remotes.d.ts.map +1 -0
  130. package/dist/commands/local-clone.d.ts +60 -0
  131. package/dist/commands/local-clone.d.ts.map +1 -0
  132. package/dist/commands/local-transport.d.ts +117 -0
  133. package/dist/commands/local-transport.d.ts.map +1 -0
  134. package/dist/commands/log-command.d.ts +216 -24
  135. package/dist/commands/log-command.d.ts.map +1 -1
  136. package/dist/commands/log-graph.d.ts +120 -0
  137. package/dist/commands/log-graph.d.ts.map +1 -0
  138. package/dist/commands/log-output.d.ts +323 -0
  139. package/dist/commands/log-output.d.ts.map +1 -0
  140. package/dist/commands/ls-remote-command.d.ts +23 -7
  141. package/dist/commands/ls-remote-command.d.ts.map +1 -1
  142. package/dist/commands/mailmap.d.ts +31 -0
  143. package/dist/commands/mailmap.d.ts.map +1 -0
  144. package/dist/commands/merge-bases.d.ts +85 -0
  145. package/dist/commands/merge-bases.d.ts.map +1 -0
  146. package/dist/commands/merge-command.d.ts +221 -74
  147. package/dist/commands/merge-command.d.ts.map +1 -1
  148. package/dist/commands/merge-config.d.ts +13 -0
  149. package/dist/commands/merge-config.d.ts.map +1 -0
  150. package/dist/commands/merge-drivers.d.ts +130 -0
  151. package/dist/commands/merge-drivers.d.ts.map +1 -0
  152. package/dist/commands/merge-support.d.ts +260 -0
  153. package/dist/commands/merge-support.d.ts.map +1 -0
  154. package/dist/commands/message-cleanup.d.ts +86 -0
  155. package/dist/commands/message-cleanup.d.ts.map +1 -0
  156. package/dist/commands/mv-command.d.ts +94 -0
  157. package/dist/commands/mv-command.d.ts.map +1 -0
  158. package/dist/commands/notes-command.d.ts +256 -0
  159. package/dist/commands/notes-command.d.ts.map +1 -0
  160. package/dist/commands/notes-commands.d.ts +41 -0
  161. package/dist/commands/notes-commands.d.ts.map +1 -0
  162. package/dist/commands/notes-merge-command.d.ts +105 -0
  163. package/dist/commands/notes-merge-command.d.ts.map +1 -0
  164. package/dist/commands/notes-support.d.ts +125 -0
  165. package/dist/commands/notes-support.d.ts.map +1 -0
  166. package/dist/commands/notes-tree.d.ts +116 -0
  167. package/dist/commands/notes-tree.d.ts.map +1 -0
  168. package/dist/commands/object-id-input.d.ts +20 -0
  169. package/dist/commands/object-id-input.d.ts.map +1 -0
  170. package/dist/commands/octopus-merge.d.ts +59 -0
  171. package/dist/commands/octopus-merge.d.ts.map +1 -0
  172. package/dist/commands/ort-merge.d.ts +126 -0
  173. package/dist/commands/ort-merge.d.ts.map +1 -0
  174. package/dist/commands/ort-recursive.d.ts +53 -0
  175. package/dist/commands/ort-recursive.d.ts.map +1 -0
  176. package/dist/commands/pack-refs-command.d.ts +20 -0
  177. package/dist/commands/pack-refs-command.d.ts.map +1 -1
  178. package/dist/commands/patch-id.d.ts +44 -0
  179. package/dist/commands/patch-id.d.ts.map +1 -0
  180. package/dist/commands/pathspec-attributes.d.ts +64 -0
  181. package/dist/commands/pathspec-attributes.d.ts.map +1 -0
  182. package/dist/commands/pathspec.d.ts +324 -0
  183. package/dist/commands/pathspec.d.ts.map +1 -0
  184. package/dist/commands/post-index-change.d.ts +33 -0
  185. package/dist/commands/post-index-change.d.ts.map +1 -0
  186. package/dist/commands/pretty-format.d.ts +222 -0
  187. package/dist/commands/pretty-format.d.ts.map +1 -0
  188. package/dist/commands/promisor-remote.d.ts +80 -0
  189. package/dist/commands/promisor-remote.d.ts.map +1 -0
  190. package/dist/commands/pull-command.d.ts +59 -8
  191. package/dist/commands/pull-command.d.ts.map +1 -1
  192. package/dist/commands/push-command.d.ts +24 -8
  193. package/dist/commands/push-command.d.ts.map +1 -1
  194. package/dist/commands/read-tree-command.d.ts +112 -0
  195. package/dist/commands/read-tree-command.d.ts.map +1 -0
  196. package/dist/commands/rebase-command.d.ts +507 -75
  197. package/dist/commands/rebase-command.d.ts.map +1 -1
  198. package/dist/commands/rebase-todo.d.ts +130 -0
  199. package/dist/commands/rebase-todo.d.ts.map +1 -0
  200. package/dist/commands/ref-decoration-filter.d.ts +53 -0
  201. package/dist/commands/ref-decoration-filter.d.ts.map +1 -0
  202. package/dist/commands/reflog-messages.d.ts +98 -0
  203. package/dist/commands/reflog-messages.d.ts.map +1 -0
  204. package/dist/commands/remote-command.d.ts +31 -0
  205. package/dist/commands/remote-command.d.ts.map +1 -1
  206. package/dist/commands/remote-transport.d.ts +120 -0
  207. package/dist/commands/remote-transport.d.ts.map +1 -0
  208. package/dist/commands/reset-command.d.ts +161 -7
  209. package/dist/commands/reset-command.d.ts.map +1 -1
  210. package/dist/commands/restore-command.d.ts +103 -0
  211. package/dist/commands/restore-command.d.ts.map +1 -0
  212. package/dist/commands/rev-list-command.d.ts +78 -0
  213. package/dist/commands/rev-list-command.d.ts.map +1 -0
  214. package/dist/commands/rev-parse-command.d.ts +89 -0
  215. package/dist/commands/rev-parse-command.d.ts.map +1 -0
  216. package/dist/commands/revert-command.d.ts +46 -38
  217. package/dist/commands/revert-command.d.ts.map +1 -1
  218. package/dist/commands/revision-filenames.d.ts +95 -0
  219. package/dist/commands/revision-filenames.d.ts.map +1 -0
  220. package/dist/commands/revision-range.d.ts +67 -0
  221. package/dist/commands/revision-range.d.ts.map +1 -0
  222. package/dist/commands/revision-resolver.d.ts +325 -0
  223. package/dist/commands/revision-resolver.d.ts.map +1 -0
  224. package/dist/commands/rm-command.d.ts +69 -7
  225. package/dist/commands/rm-command.d.ts.map +1 -1
  226. package/dist/commands/sequencer-pick.d.ts +191 -0
  227. package/dist/commands/sequencer-pick.d.ts.map +1 -0
  228. package/dist/commands/sequencer-support.d.ts +125 -0
  229. package/dist/commands/sequencer-support.d.ts.map +1 -0
  230. package/dist/commands/serve-repository.d.ts +52 -0
  231. package/dist/commands/serve-repository.d.ts.map +1 -0
  232. package/dist/commands/sparse-checkout-command.d.ts +190 -0
  233. package/dist/commands/sparse-checkout-command.d.ts.map +1 -0
  234. package/dist/commands/sparse-checkout-support.d.ts +268 -0
  235. package/dist/commands/sparse-checkout-support.d.ts.map +1 -0
  236. package/dist/commands/stash-apply-command.d.ts +42 -8
  237. package/dist/commands/stash-apply-command.d.ts.map +1 -1
  238. package/dist/commands/stash-create-command.d.ts +156 -2
  239. package/dist/commands/stash-create-command.d.ts.map +1 -1
  240. package/dist/commands/stash-drop-command.d.ts +2 -0
  241. package/dist/commands/stash-drop-command.d.ts.map +1 -1
  242. package/dist/commands/stash-working-tree.d.ts +23 -0
  243. package/dist/commands/stash-working-tree.d.ts.map +1 -1
  244. package/dist/commands/status-command.d.ts +300 -15
  245. package/dist/commands/status-command.d.ts.map +1 -1
  246. package/dist/commands/status-format.d.ts +155 -0
  247. package/dist/commands/status-format.d.ts.map +1 -0
  248. package/dist/commands/status-header.d.ts +64 -0
  249. package/dist/commands/status-header.d.ts.map +1 -0
  250. package/dist/commands/strerror.d.ts +7 -0
  251. package/dist/commands/strerror.d.ts.map +1 -0
  252. package/dist/commands/submodule-config.d.ts +38 -0
  253. package/dist/commands/submodule-config.d.ts.map +1 -0
  254. package/dist/commands/submodule-summary-command.d.ts +80 -0
  255. package/dist/commands/submodule-summary-command.d.ts.map +1 -0
  256. package/dist/commands/submodule-summary.d.ts +150 -0
  257. package/dist/commands/submodule-summary.d.ts.map +1 -0
  258. package/dist/commands/switch-command.d.ts +132 -0
  259. package/dist/commands/switch-command.d.ts.map +1 -0
  260. package/dist/commands/tag-command.d.ts +23 -2
  261. package/dist/commands/tag-command.d.ts.map +1 -1
  262. package/dist/commands/tracking-info.d.ts +64 -0
  263. package/dist/commands/tracking-info.d.ts.map +1 -0
  264. package/dist/commands/trailers.d.ts +73 -0
  265. package/dist/commands/trailers.d.ts.map +1 -0
  266. package/dist/commands/tree-restore.d.ts +85 -1
  267. package/dist/commands/tree-restore.d.ts.map +1 -1
  268. package/dist/commands/untracked-cache-support.d.ts +54 -0
  269. package/dist/commands/untracked-cache-support.d.ts.map +1 -0
  270. package/dist/commands/worktree-add-command.d.ts +108 -0
  271. package/dist/commands/worktree-add-command.d.ts.map +1 -0
  272. package/dist/commands/worktree-list-command.d.ts +69 -0
  273. package/dist/commands/worktree-list-command.d.ts.map +1 -0
  274. package/dist/commands/worktree-lock-command.d.ts +44 -0
  275. package/dist/commands/worktree-lock-command.d.ts.map +1 -0
  276. package/dist/commands/worktree-overwrite-checks.d.ts +146 -6
  277. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
  278. package/dist/commands/worktree-prune-command.d.ts +48 -0
  279. package/dist/commands/worktree-prune-command.d.ts.map +1 -0
  280. package/dist/commands/worktree-remove-command.d.ts +59 -0
  281. package/dist/commands/worktree-remove-command.d.ts.map +1 -0
  282. package/dist/commands/worktree-repair-command.d.ts +35 -0
  283. package/dist/commands/worktree-repair-command.d.ts.map +1 -0
  284. package/dist/commands/worktree-support.d.ts +64 -0
  285. package/dist/commands/worktree-support.d.ts.map +1 -0
  286. package/dist/commands-DIJ91NnD.js +62225 -0
  287. package/dist/core-commands/add.command.impl.d.ts.map +1 -1
  288. package/dist/errors/add-errors.d.ts +55 -0
  289. package/dist/errors/add-errors.d.ts.map +1 -1
  290. package/dist/errors/checkout-errors.d.ts +16 -0
  291. package/dist/errors/checkout-errors.d.ts.map +1 -1
  292. package/dist/errors/command-errors.d.ts +77 -0
  293. package/dist/errors/command-errors.d.ts.map +1 -1
  294. package/dist/errors/commit-errors.d.ts +9 -0
  295. package/dist/errors/commit-errors.d.ts.map +1 -1
  296. package/dist/errors/git-api-error.d.ts +59 -2
  297. package/dist/errors/git-api-error.d.ts.map +1 -1
  298. package/dist/errors/index.d.ts +1 -0
  299. package/dist/errors/index.d.ts.map +1 -1
  300. package/dist/errors/index.js +354 -19
  301. package/dist/errors/rebase-errors.d.ts +9 -0
  302. package/dist/errors/rebase-errors.d.ts.map +1 -1
  303. package/dist/errors/ref-errors.d.ts +32 -0
  304. package/dist/errors/ref-errors.d.ts.map +1 -1
  305. package/dist/errors/transport-errors.d.ts +12 -2
  306. package/dist/errors/transport-errors.d.ts.map +1 -1
  307. package/dist/errors/worktree-errors.d.ts +35 -0
  308. package/dist/errors/worktree-errors.d.ts.map +1 -0
  309. package/dist/git-command-BGiOJ8Zk.js +7246 -0
  310. package/dist/git-command.d.ts +428 -32
  311. package/dist/git-command.d.ts.map +1 -1
  312. package/dist/git.d.ts +315 -5
  313. package/dist/git.d.ts.map +1 -1
  314. package/dist/index.d.ts +1 -1
  315. package/dist/index.d.ts.map +1 -1
  316. package/dist/index.js +5 -5
  317. package/dist/remote-config/index.d.ts +1 -1
  318. package/dist/remote-config/index.d.ts.map +1 -1
  319. package/dist/remote-config/remote-config-store.d.ts +167 -6
  320. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  321. package/dist/remote-config/remote-refs.d.ts +7 -3
  322. package/dist/remote-config/remote-refs.d.ts.map +1 -1
  323. package/dist/rename/detect-renames.d.ts +148 -0
  324. package/dist/rename/detect-renames.d.ts.map +1 -0
  325. package/dist/rename/git-similarity.d.ts +44 -0
  326. package/dist/rename/git-similarity.d.ts.map +1 -0
  327. package/dist/rename/index.d.ts +2 -0
  328. package/dist/rename/index.d.ts.map +1 -1
  329. package/dist/results/binary-patch.d.ts +11 -0
  330. package/dist/results/binary-patch.d.ts.map +1 -0
  331. package/dist/results/blame-format.d.ts +55 -0
  332. package/dist/results/blame-format.d.ts.map +1 -0
  333. package/dist/results/cherry-pick-result.d.ts +10 -0
  334. package/dist/results/cherry-pick-result.d.ts.map +1 -1
  335. package/dist/results/clone-result.d.ts +18 -0
  336. package/dist/results/clone-result.d.ts.map +1 -1
  337. package/dist/results/combined-diff.d.ts +77 -0
  338. package/dist/results/combined-diff.d.ts.map +1 -0
  339. package/dist/results/diff-entry.d.ts +32 -0
  340. package/dist/results/diff-entry.d.ts.map +1 -1
  341. package/dist/results/diff-formatter.d.ts +34 -60
  342. package/dist/results/diff-formatter.d.ts.map +1 -1
  343. package/dist/results/diff-order.d.ts +25 -0
  344. package/dist/results/diff-order.d.ts.map +1 -0
  345. package/dist/results/diff-output.d.ts +249 -0
  346. package/dist/results/diff-output.d.ts.map +1 -0
  347. package/dist/results/fetch-result.d.ts +15 -0
  348. package/dist/results/fetch-result.d.ts.map +1 -1
  349. package/dist/results/index.d.ts +5 -0
  350. package/dist/results/index.d.ts.map +1 -1
  351. package/dist/results/index.js +2 -252
  352. package/dist/results/init-result.d.ts +7 -0
  353. package/dist/results/init-result.d.ts.map +1 -1
  354. package/dist/results/merge-result.d.ts +15 -1
  355. package/dist/results/merge-result.d.ts.map +1 -1
  356. package/dist/results/pull-result.d.ts +17 -3
  357. package/dist/results/pull-result.d.ts.map +1 -1
  358. package/dist/results/push-result.d.ts +47 -0
  359. package/dist/results/push-result.d.ts.map +1 -1
  360. package/dist/results/quote-path.d.ts +27 -0
  361. package/dist/results/quote-path.d.ts.map +1 -0
  362. package/dist/results/rebase-result.d.ts +28 -1
  363. package/dist/results/rebase-result.d.ts.map +1 -1
  364. package/dist/results/revert-result.d.ts +10 -0
  365. package/dist/results/revert-result.d.ts.map +1 -1
  366. package/dist/results/stash-result.d.ts +5 -0
  367. package/dist/results/stash-result.d.ts.map +1 -1
  368. package/dist/results/status-result.d.ts +60 -6
  369. package/dist/results/status-result.d.ts.map +1 -1
  370. package/dist/results-WVlBhVb1.js +2935 -0
  371. package/dist/transport-command.d.ts +44 -1
  372. package/dist/transport-command.d.ts.map +1 -1
  373. package/package.json +15 -11
  374. package/src/commands/add-command.ts +1291 -226
  375. package/src/commands/add-patch/add-interactive.ts +941 -0
  376. package/src/commands/add-patch/add-patch.ts +1169 -0
  377. package/src/commands/add-patch/index.ts +5 -0
  378. package/src/commands/add-patch/patch-backend.ts +239 -0
  379. package/src/commands/add-patch/patch-modes.ts +249 -0
  380. package/src/commands/add-patch/patch-session.ts +159 -0
  381. package/src/commands/am/am-command.ts +1111 -0
  382. package/src/commands/am/index.ts +2 -0
  383. package/src/commands/am/mailinfo.ts +1015 -0
  384. package/src/commands/am/patch-format.ts +121 -0
  385. package/src/commands/apply/apply-command.ts +842 -0
  386. package/src/commands/apply/apply-engine.ts +1602 -0
  387. package/src/commands/apply/apply-parse.ts +1210 -0
  388. package/src/commands/apply/apply-types.ts +112 -0
  389. package/src/commands/apply/index.ts +5 -0
  390. package/src/commands/apply/latin1.ts +171 -0
  391. package/src/commands/apply/whitespace.ts +260 -0
  392. package/src/commands/attributes-source.ts +96 -0
  393. package/src/commands/backfill-command.ts +694 -0
  394. package/src/commands/bisect/bisect-checkout.ts +217 -0
  395. package/src/commands/bisect/bisect-command.ts +788 -0
  396. package/src/commands/bisect/bisect-engine.ts +1080 -0
  397. package/src/commands/bisect/bisect-graph.ts +595 -0
  398. package/src/commands/bisect/bisect-quote.ts +92 -0
  399. package/src/commands/bisect/index.ts +4 -0
  400. package/src/commands/blame-command.ts +711 -986
  401. package/src/commands/branch-command.ts +354 -32
  402. package/src/commands/branch-tracking.ts +331 -0
  403. package/src/commands/checkout-command.ts +1290 -243
  404. package/src/commands/checkout-paths.ts +784 -0
  405. package/src/commands/cherry-pick-command.ts +156 -396
  406. package/src/commands/clean-command.ts +504 -156
  407. package/src/commands/clone-command.ts +748 -69
  408. package/src/commands/comment-char-config.ts +160 -0
  409. package/src/commands/commit-command.ts +1782 -166
  410. package/src/commands/core-config.ts +166 -6
  411. package/src/commands/date-format.ts +668 -0
  412. package/src/commands/default-branch.ts +196 -0
  413. package/src/commands/describe-command.ts +2 -0
  414. package/src/commands/diff-command.ts +1916 -110
  415. package/src/commands/diff-textconv.ts +50 -0
  416. package/src/commands/fetch-command.ts +622 -68
  417. package/src/commands/format-patch-command.ts +974 -0
  418. package/src/commands/fsck/fetch-fsck.ts +229 -0
  419. package/src/commands/fsck/fsck-command.ts +3464 -0
  420. package/src/commands/fsck/fsck-loose.ts +297 -0
  421. package/src/commands/fsck/fsck-msg-ids.ts +176 -0
  422. package/src/commands/fsck/fsck-object-checks.ts +1085 -0
  423. package/src/commands/fsck/fsck-object-table.ts +373 -0
  424. package/src/commands/fsck/fsck-pack.ts +634 -0
  425. package/src/commands/fsck/index.ts +5 -0
  426. package/src/commands/fsck/verify-pack-command.ts +252 -0
  427. package/src/commands/gc-command.ts +324 -77
  428. package/src/commands/git-date.ts +9 -0
  429. package/src/commands/git-describe.ts +316 -0
  430. package/src/commands/git-hooks.ts +1483 -0
  431. package/src/commands/git-ident.ts +344 -0
  432. package/src/commands/hook-command.ts +247 -0
  433. package/src/commands/index-adder.ts +711 -0
  434. package/src/commands/index-host.ts +291 -0
  435. package/src/commands/index.ts +84 -0
  436. package/src/commands/init-command.ts +194 -14
  437. package/src/commands/legacy-remotes.ts +224 -0
  438. package/src/commands/local-clone.ts +183 -0
  439. package/src/commands/local-transport.ts +762 -0
  440. package/src/commands/log-command.ts +1930 -170
  441. package/src/commands/log-graph.ts +671 -0
  442. package/src/commands/log-output.ts +1359 -0
  443. package/src/commands/ls-remote-command.ts +105 -58
  444. package/src/commands/mailmap.ts +129 -0
  445. package/src/commands/merge-bases.ts +296 -0
  446. package/src/commands/merge-command.ts +1416 -491
  447. package/src/commands/merge-config.ts +25 -0
  448. package/src/commands/merge-drivers.ts +306 -0
  449. package/src/commands/merge-support.ts +1219 -0
  450. package/src/commands/message-cleanup.ts +275 -0
  451. package/src/commands/mv-command.ts +976 -0
  452. package/src/commands/notes-command.ts +890 -0
  453. package/src/commands/notes-commands.ts +87 -0
  454. package/src/commands/notes-merge-command.ts +777 -0
  455. package/src/commands/notes-support.ts +402 -0
  456. package/src/commands/notes-tree.ts +617 -0
  457. package/src/commands/object-id-input.ts +27 -0
  458. package/src/commands/octopus-merge.ts +378 -0
  459. package/src/commands/ort-merge.ts +1660 -0
  460. package/src/commands/ort-recursive.ts +160 -0
  461. package/src/commands/pack-refs-command.ts +39 -22
  462. package/src/commands/patch-id.ts +204 -0
  463. package/src/commands/pathspec-attributes.ts +162 -0
  464. package/src/commands/pathspec.ts +1199 -0
  465. package/src/commands/post-index-change.ts +62 -0
  466. package/src/commands/pretty-format.ts +1432 -0
  467. package/src/commands/promisor-remote.ts +298 -0
  468. package/src/commands/pull-command.ts +326 -33
  469. package/src/commands/push-command.ts +257 -29
  470. package/src/commands/read-tree-command.ts +861 -0
  471. package/src/commands/rebase-command.ts +3820 -372
  472. package/src/commands/rebase-todo.ts +584 -0
  473. package/src/commands/ref-decoration-filter.ts +118 -0
  474. package/src/commands/reflog-messages.ts +177 -0
  475. package/src/commands/remote-command.ts +124 -23
  476. package/src/commands/remote-transport.ts +383 -0
  477. package/src/commands/reset-command.ts +821 -104
  478. package/src/commands/restore-command.ts +350 -0
  479. package/src/commands/rev-list-command.ts +167 -0
  480. package/src/commands/rev-parse-command.ts +299 -0
  481. package/src/commands/revert-command.ts +152 -399
  482. package/src/commands/revision-filenames.ts +254 -0
  483. package/src/commands/revision-range.ts +222 -0
  484. package/src/commands/revision-resolver.ts +1397 -0
  485. package/src/commands/rm-command.ts +613 -67
  486. package/src/commands/sequencer-pick.ts +874 -0
  487. package/src/commands/sequencer-support.ts +876 -0
  488. package/src/commands/serve-repository.ts +125 -0
  489. package/src/commands/sparse-checkout-command.ts +1389 -0
  490. package/src/commands/sparse-checkout-support.ts +793 -0
  491. package/src/commands/stash-apply-command.ts +376 -343
  492. package/src/commands/stash-create-command.ts +625 -42
  493. package/src/commands/stash-drop-command.ts +15 -2
  494. package/src/commands/stash-working-tree.ts +168 -24
  495. package/src/commands/status-command.ts +1326 -105
  496. package/src/commands/status-format.ts +775 -0
  497. package/src/commands/status-header.ts +332 -0
  498. package/src/commands/strerror.ts +29 -0
  499. package/src/commands/submodule-config.ts +94 -0
  500. package/src/commands/submodule-summary-command.ts +333 -0
  501. package/src/commands/submodule-summary.ts +446 -0
  502. package/src/commands/switch-command.ts +402 -0
  503. package/src/commands/tag-command.ts +129 -41
  504. package/src/commands/tracking-info.ts +201 -0
  505. package/src/commands/trailers.ts +417 -0
  506. package/src/commands/tree-restore.ts +204 -4
  507. package/src/commands/untracked-cache-support.ts +198 -0
  508. package/src/commands/worktree-add-command.ts +458 -0
  509. package/src/commands/worktree-list-command.ts +206 -0
  510. package/src/commands/worktree-lock-command.ts +96 -0
  511. package/src/commands/worktree-overwrite-checks.ts +452 -26
  512. package/src/commands/worktree-prune-command.ts +99 -0
  513. package/src/commands/worktree-remove-command.ts +186 -0
  514. package/src/commands/worktree-repair-command.ts +59 -0
  515. package/src/commands/worktree-support.ts +214 -0
  516. package/src/core-commands/add.command.impl.ts +8 -2
  517. package/src/errors/add-errors.ts +45 -0
  518. package/src/errors/checkout-errors.ts +26 -2
  519. package/src/errors/command-errors.ts +101 -1
  520. package/src/errors/commit-errors.ts +18 -2
  521. package/src/errors/git-api-error.ts +82 -2
  522. package/src/errors/index.ts +1 -0
  523. package/src/errors/merge-errors.ts +2 -2
  524. package/src/errors/rebase-errors.ts +12 -0
  525. package/src/errors/ref-errors.ts +56 -2
  526. package/src/errors/remote-errors.ts +2 -2
  527. package/src/errors/stash-errors.ts +1 -1
  528. package/src/errors/transport-errors.ts +20 -4
  529. package/src/errors/worktree-errors.ts +49 -0
  530. package/src/git-command.ts +1207 -118
  531. package/src/git.ts +538 -12
  532. package/src/index.ts +1 -1
  533. package/src/remote-config/index.ts +2 -0
  534. package/src/remote-config/remote-config-store.ts +378 -18
  535. package/src/remote-config/remote-refs.ts +22 -6
  536. package/src/rename/detect-renames.ts +715 -0
  537. package/src/rename/git-similarity.ts +116 -0
  538. package/src/rename/index.ts +2 -0
  539. package/src/results/binary-patch.ts +401 -0
  540. package/src/results/blame-format.ts +239 -0
  541. package/src/results/cherry-pick-result.ts +10 -0
  542. package/src/results/clone-result.ts +21 -0
  543. package/src/results/combined-diff.ts +585 -0
  544. package/src/results/diff-entry.ts +35 -0
  545. package/src/results/diff-formatter.ts +152 -288
  546. package/src/results/diff-order.ts +64 -0
  547. package/src/results/diff-output.ts +1053 -0
  548. package/src/results/fetch-result.ts +15 -0
  549. package/src/results/index.ts +5 -0
  550. package/src/results/init-result.ts +9 -0
  551. package/src/results/merge-result.ts +15 -1
  552. package/src/results/pull-result.ts +17 -3
  553. package/src/results/push-result.ts +48 -0
  554. package/src/results/quote-path.ts +103 -0
  555. package/src/results/rebase-result.ts +37 -0
  556. package/src/results/revert-result.ts +10 -0
  557. package/src/results/stash-result.ts +6 -0
  558. package/src/results/status-result.ts +127 -46
  559. package/src/transport-command.ts +121 -2
  560. package/dist/commands-CsLNyvZ1.js +0 -12587
  561. package/dist/status-result-BpyUK1Sg.js +0 -519
@@ -0,0 +1,1432 @@
1
+ /**
2
+ * git's user formats (`--format=<format>`, `rebase.instructionFormat`): the placeholders
3
+ * pretty.c's format_commit_message expands for one commit.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * await formatCommitPretty("# %s (%an)", commit, id, { abbreviate });
8
+ * // "# Fix the parser (A U Thor)"
9
+ * const decorations = await loadRefDecorations({ refs, tags });
10
+ * await formatCommitPretty("%h%d", commit, id, { abbreviate, decorations });
11
+ * // "1a2b3c4 (HEAD -> main, tag: v1, origin/main)"
12
+ * ```
13
+ *
14
+ * Supported: `%H %h %T %t %P %p`, the author and committer parts `%an %aN %ae %aE %al %aL
15
+ * %ad %aD %ar %at %ai %aI %as %ah` (and `%c…`; `%aN`, `%aE`, `%aL` through the mailmap given, `%ad` in the date mode given),
16
+ * `%s %f %b %B %e`, `%n %%` and `%x<hex>` (a byte above 0x7f as the repository's byte strings
17
+ * keep a raw byte: U+DC80 to U+DCFF, which `encodeGitPath` writes back as that byte), `%d %D`
18
+ * and `%(decorate:…)` from {@link loadRefDecorations}, `%m` (`>`, or the mark given), `%G?`
19
+ * (`N` unsigned; a signed commit is `E`, as git prints for a signature it cannot check) and
20
+ * the other `%G` parts (empty; `%GT` `undefined`), `%N` (the notes given, else kept as
21
+ * written, as git's sequencer keeps it), `%gd %gD %gs %gn %gN %ge %gE` (the reflog entry
22
+ * given, else empty), colors (`%Cred`, `%C(...)`: written only with `color`, or `always,`),
23
+ * padding (`%<(N)`, `%>(N)`, `%><(N)`, `%>>(N)`, the `|` column forms, `trunc`, `ltrunc`,
24
+ * `mtrunc`), wrapping (`%w(width,indent1,indent2)`), the `%+`, `%-` and `% ` magic,
25
+ * `%(trailers[:options])` (trailer.c's block detection and formatting, with the
26
+ * {@link TrailerConfig} given), `%(describe[:options])` (with the describer given), `%S`
27
+ * (the source given) and git 2.5x's `%(count)`/`%(total)`. A placeholder git does not
28
+ * know is kept as written, as git keeps an unknown one.
29
+ *
30
+ * @module
31
+ */
32
+
33
+ import type { ObjectId, PersonIdent } from "@statewalker/vcs-core";
34
+ import { gitCharWidth } from "@statewalker/vcs-utils/encoding";
35
+ import { formatRfc2822Date } from "./git-date.js";
36
+ import type { DescribeOptions } from "./git-describe.js";
37
+ import {
38
+ createRefDecorationFilter,
39
+ decorateFullRefs,
40
+ type RefDecorationFilterOptions,
41
+ } from "./ref-decoration-filter.js";
42
+ import { formatGitDate } from "./sequencer-support.js";
43
+ import { appendTrailers, type TrailerConfig, type TrailerFormatOptions } from "./trailers.js";
44
+
45
+ /** What a format reads of the commit besides its fields. */
46
+ export interface PrettyFormatOptions {
47
+ /**
48
+ * The abbreviation of an id for `%h`, `%t`, `%p` (git's find_unique_abbrev with the
49
+ * context's `abbrev`); the full id when absent, as with an abbrev of 0
50
+ */
51
+ abbreviate?: (id: ObjectId) => Promise<string>;
52
+ /** Now, in seconds since the epoch, for `%ar` and `%ah` (default: the clock) */
53
+ now?: number;
54
+ /**
55
+ * The local time zone's offset east of UTC, in minutes, at a time in seconds (`%ah` hides
56
+ * the zone of a date in it); default: the host's (`Date.getTimezoneOffset`)
57
+ */
58
+ localTzOffset?: (time: number) => number;
59
+ /** The decorations `%d`, `%D` and `%(decorate)` show (see {@link loadRefDecorations}) */
60
+ decorations?: RefDecorations;
61
+ /** `%m`: the commit's mark (`<`, `>`, `-` boundary, `^` uninteresting); default `>` */
62
+ mark?: string;
63
+ /** `%N`: the commit's notes; without, `%N` is kept as written (git's sequencer) */
64
+ notes?: string;
65
+ /** `%gd`, `%gD`, `%gs`, `%gn`…: the reflog entry shown; empty without */
66
+ reflog?: { selector?: string; shortSelector?: string; message?: string; ident?: PersonIdent };
67
+ /** Whether colors are written (git's want_color; default false, as without a terminal) */
68
+ color?: boolean;
69
+ /**
70
+ * `%(trailers)`: the trailer configuration (`trailer.separators`, `trailer.<name>.key`,
71
+ * the comment prefix; see {@link trailerConfigOf}); default git's (`:`, `#`, no items)
72
+ */
73
+ trailers?: TrailerConfig;
74
+ /**
75
+ * `%(describe)`: git describe of a commit (see {@link createDescriber}); without, the
76
+ * placeholder is kept as written
77
+ */
78
+ describe?: (id: ObjectId, options: DescribeOptions) => Promise<string>;
79
+ /**
80
+ * `%S`: the name on the command line the commit was reached from (`git log --source`, or
81
+ * a `%S` in log's format); without, `%S` is kept as written, as git keeps it without a
82
+ * revision walk's sources
83
+ */
84
+ source?: string;
85
+ /**
86
+ * `%(count)` and `%(total)` (git 2.5x): the walk's number of this commit and the count
87
+ * (`git log` gives 0 and 0, `format-patch` the patch numbers); without, git's "not
88
+ * supported by this command"
89
+ */
90
+ count?: { nr: number; total: number };
91
+ /**
92
+ * `%ad` and `%cd`: the date in the walk's date mode (`--date`, `log.date`); default git's
93
+ * default mode
94
+ */
95
+ formatDate?: (ident: PersonIdent) => string;
96
+ /**
97
+ * `%aN`, `%aE`, `%aL` (and `%c…`): the identity through the mailmap (see `Mailmap`);
98
+ * without, they are `%an`, `%ae`, `%al`
99
+ */
100
+ mailmap?: (name: string, email: string) => { name: string; email: string };
101
+ }
102
+
103
+ /** One name a commit is decorated with (log-tree.c's name_decoration). */
104
+ export interface RefDecoration {
105
+ type: "head" | "local" | "remote" | "tag" | "stash" | "grafted" | "none";
106
+ /** The full ref name, `HEAD`, `grafted` or `replaced` */
107
+ name: string;
108
+ }
109
+
110
+ /** The decorations of each object, as load_ref_decorations collects them. */
111
+ export interface RefDecorations {
112
+ /** An object's decorations, in git's order (the last added first) */
113
+ get(id: ObjectId): readonly RefDecoration[] | undefined;
114
+ /** The branch HEAD names (`refs/heads/main`); undefined when detached */
115
+ headBranch?: string;
116
+ /** Full ref names are shown (`log.decorate=full`, `--decorate=full`); short ones by default */
117
+ fullRefs?: boolean;
118
+ }
119
+
120
+ /** Options of {@link loadRefDecorations}. */
121
+ export interface LoadRefDecorationsOptions {
122
+ /** The refs decorated (every ref, then HEAD) */
123
+ refs: {
124
+ list(prefix?: string): AsyncIterable<{ name: string; objectId?: ObjectId; target?: string }>;
125
+ resolve(name: string): Promise<{ objectId?: ObjectId } | undefined>;
126
+ };
127
+ /** Annotated tags: a tag ref decorates each object of its chain */
128
+ tags?: {
129
+ has(id: ObjectId): Promise<boolean>;
130
+ load(id: ObjectId): Promise<{ object: ObjectId } | undefined>;
131
+ };
132
+ /**
133
+ * `git log`'s default decoration filter: only HEAD, `refs/heads/`, `refs/tags/`,
134
+ * `refs/remotes/`, `refs/stash` and the replace refs (default false: every ref, as git's
135
+ * sequencer loads them)
136
+ */
137
+ logFilter?: boolean;
138
+ /**
139
+ * `git log`'s decoration options and settings (see `createRefDecorationFilter`):
140
+ * `--decorate-refs`, `--decorate-refs-exclude`, `--clear-decorations`, and the
141
+ * configuration's `log.excludeDecoration`, `log.initialDecorationSet` and `log.decorate`
142
+ * (`full`: full ref names). Given, the refs are filtered as `git log` filters them, as with
143
+ * `logFilter`.
144
+ */
145
+ logOptions?: Omit<RefDecorationFilterOptions, "replaceRefBase"> & {
146
+ /** `--decorate=short|full` (default `log.decorate`, else short) */
147
+ decorate?: "short" | "full";
148
+ };
149
+ /** Where replace refs live (default `refs/replace/`); their originals read `replaced` */
150
+ replaceRefBase?: string;
151
+ /** Whether replace refs apply (default true); off, they decorate nothing */
152
+ useReplaceRefs?: boolean;
153
+ /** The grafted commits (and shallow ones), each decorated `grafted` */
154
+ grafted?: Iterable<ObjectId>;
155
+ /** Whether an object is stored (a replaced original that is not is not decorated) */
156
+ hasObject?: (id: ObjectId) => Promise<boolean>;
157
+ /**
158
+ * Receives what git prints on stderr while decorating: `warning: invalid replace ref
159
+ * <name>` for a ref under the replace base that does not start with an object id below it
160
+ */
161
+ onWarning?: (message: string) => void;
162
+ }
163
+
164
+ /**
165
+ * The decorations of a repository's objects, as git's load_ref_decorations makes them: each
166
+ * ref in name order, then HEAD, then each graft, every one put before those its object
167
+ * already has; a ref is typed by its namespace, an annotated tag decorates every object of
168
+ * its chain as a tag, a replace ref decorates the object it replaces as `replaced`.
169
+ */
170
+ export async function loadRefDecorations(
171
+ options: LoadRefDecorationsOptions,
172
+ ): Promise<RefDecorations> {
173
+ const map = new Map<ObjectId, RefDecoration[]>();
174
+ const add = (id: ObjectId, decoration: RefDecoration) => {
175
+ const list = map.get(id);
176
+ if (list) list.unshift(decoration);
177
+ else map.set(id, [decoration]);
178
+ };
179
+ const replaceBase = options.replaceRefBase ?? "refs/replace/";
180
+ const logOptions = options.logOptions;
181
+ const included =
182
+ options.logFilter || logOptions
183
+ ? createRefDecorationFilter({ ...logOptions, replaceRefBase: replaceBase })
184
+ : () => true;
185
+ const fullRefs = logOptions?.decorate
186
+ ? logOptions.decorate === "full"
187
+ : decorateFullRefs(logOptions?.config);
188
+ const typeOf = (name: string): RefDecoration["type"] => {
189
+ if (name === "HEAD") return "head";
190
+ if (name.startsWith("refs/heads/")) return "local";
191
+ if (name.startsWith("refs/tags/")) return "tag";
192
+ if (name.startsWith("refs/remotes/")) return "remote";
193
+ if (name === "refs/stash") return "stash";
194
+ return "none";
195
+ };
196
+ const decorateRef = async (name: string, value: ObjectId | undefined) => {
197
+ if (!included(name)) return;
198
+ if (name.startsWith(replaceBase)) {
199
+ if (options.useReplaceRefs === false) return;
200
+ const hex = name.slice(replaceBase.length);
201
+ const hexLength = /^[0-9a-fA-F]{64}/.test(hex) ? 64 : 40;
202
+ if (!/^[0-9a-fA-F]{40}/.test(hex)) {
203
+ // add_ref_decoration: get_oid_hex of what follows the base
204
+ options.onWarning?.(`warning: invalid replace ref ${name}`);
205
+ return;
206
+ }
207
+ const original = hex.slice(0, hexLength).toLowerCase();
208
+ if (options.hasObject && !(await options.hasObject(original))) return;
209
+ add(original, { type: "grafted", name: "replaced" });
210
+ return;
211
+ }
212
+ if (!value) return;
213
+ add(value, { type: typeOf(name), name });
214
+ // an annotated tag: each object of its chain is decorated as a tag
215
+ let id = value;
216
+ for (let depth = 0; depth < 32 && options.tags && (await options.tags.has(id)); depth++) {
217
+ const tag = await options.tags.load(id);
218
+ if (!tag) break;
219
+ id = tag.object;
220
+ add(id, { type: "tag", name });
221
+ }
222
+ };
223
+ const names: { name: string; objectId?: ObjectId; target?: string }[] = [];
224
+ for await (const ref of options.refs.list()) {
225
+ if (ref.name === "HEAD" || !ref.name.startsWith("refs/")) continue;
226
+ names.push(ref);
227
+ }
228
+ names.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
229
+ for (const ref of names) {
230
+ const value =
231
+ ref.objectId && ref.target === undefined
232
+ ? ref.objectId
233
+ : (await options.refs.resolve(ref.name))?.objectId;
234
+ await decorateRef(ref.name, value);
235
+ }
236
+ let headBranch: string | undefined;
237
+ for await (const ref of options.refs.list("HEAD")) {
238
+ if (ref.name === "HEAD" && ref.target !== undefined) headBranch = ref.target;
239
+ }
240
+ await decorateRef("HEAD", (await options.refs.resolve("HEAD"))?.objectId);
241
+ for (const id of options.grafted ?? []) add(id, { type: "grafted", name: "grafted" });
242
+ return {
243
+ get: (id) => map.get(id),
244
+ ...(headBranch?.startsWith("refs/") ? { headBranch } : {}),
245
+ ...(fullRefs ? { fullRefs } : {}),
246
+ };
247
+ }
248
+
249
+ /** git's prettify_refname: `refs/heads/`, `refs/tags/`, `refs/remotes/` dropped. */
250
+ function prettifyRefName(name: string): string {
251
+ for (const prefix of ["refs/heads/", "refs/tags/", "refs/remotes/"]) {
252
+ if (name.startsWith(prefix)) return name.slice(prefix.length);
253
+ }
254
+ return name;
255
+ }
256
+
257
+ /**
258
+ * log-tree.c's format_decorations, without colors: ` (HEAD -> main, tag: v1)` by default,
259
+ * the separators given otherwise; empty when the object has no decorations.
260
+ */
261
+ export function formatRefDecorations(
262
+ decorations: RefDecorations | undefined,
263
+ id: ObjectId,
264
+ opts: { prefix?: string; suffix?: string; separator?: string; pointer?: string; tag?: string },
265
+ ): string {
266
+ const list = decorations?.get(id);
267
+ if (!list) return "";
268
+ let prefix = opts.prefix ?? " (";
269
+ const suffix = opts.suffix ?? ")";
270
+ const separator = opts.separator ?? ", ";
271
+ const pointer = opts.pointer ?? " -> ";
272
+ const tag = opts.tag ?? "tag: ";
273
+ // current_pointed_by_HEAD: HEAD's branch, when both are in the list
274
+ const head = list.find((d) => d.type === "head");
275
+ const current =
276
+ head && decorations?.headBranch
277
+ ? list.find((d) => d.type === "local" && d.name === decorations.headBranch)
278
+ : undefined;
279
+ let out = "";
280
+ for (const d of list) {
281
+ if (d === current) continue;
282
+ out += prefix;
283
+ if (tag && d.type === "tag") out += tag;
284
+ // show_name: full names with --decorate=full
285
+ const show = (name: string) => (decorations?.fullRefs ? name : prettifyRefName(name));
286
+ out += show(d.name);
287
+ if (current && d.type === "head") out += pointer + show(current.name);
288
+ prefix = separator;
289
+ }
290
+ return out + suffix;
291
+ }
292
+
293
+ /** What {@link formatCommitPretty} reads of a commit. */
294
+ export interface PrettyCommit {
295
+ tree: ObjectId;
296
+ parents: ObjectId[];
297
+ author: PersonIdent;
298
+ committer: PersonIdent;
299
+ message: string;
300
+ encoding?: string;
301
+ }
302
+
303
+ const pad2 = (n: number) => String(n).padStart(2, "0");
304
+
305
+ /** The ident's time shifted into its own zone, as a UTC Date, and the zone in minutes. */
306
+ function zoned(ident: PersonIdent): { d: Date; minutes: number; tz: string } {
307
+ const tz = /^[+-]\d{4}$/.test(ident.tzOffset) ? ident.tzOffset : "+0000";
308
+ const sign = tz.startsWith("-") ? -1 : 1;
309
+ const minutes = sign * (Number(tz.slice(1, 3)) * 60 + Number(tz.slice(3, 5)));
310
+ return { d: new Date((ident.timestamp + minutes * 60) * 1000), minutes, tz };
311
+ }
312
+
313
+ /** git's DATE_ISO8601: `2005-04-07 22:13:13 +0200`. */
314
+ function isoDate(ident: PersonIdent): string {
315
+ const { d, tz } = zoned(ident);
316
+ return (
317
+ `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())} ` +
318
+ `${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}:${pad2(d.getUTCSeconds())} ${tz}`
319
+ );
320
+ }
321
+
322
+ /** git's DATE_ISO8601_STRICT (2.5x): `2005-04-07T22:13:13+02:00`, `Z` for UTC. */
323
+ function strictIsoDate(ident: PersonIdent): string {
324
+ const { d, minutes, tz } = zoned(ident);
325
+ const zone = minutes === 0 ? "Z" : `${tz.slice(0, 3)}:${tz.slice(3)}`;
326
+ return (
327
+ `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())}T` +
328
+ `${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}:${pad2(d.getUTCSeconds())}${zone}`
329
+ );
330
+ }
331
+
332
+ /** git's DATE_SHORT: `2005-04-07`. */
333
+ function shortDate(ident: PersonIdent): string {
334
+ const { d } = zoned(ident);
335
+ return `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())}`;
336
+ }
337
+
338
+ const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
339
+ const MONTH_NAMES = [
340
+ "Jan",
341
+ "Feb",
342
+ "Mar",
343
+ "Apr",
344
+ "May",
345
+ "Jun",
346
+ "Jul",
347
+ "Aug",
348
+ "Sep",
349
+ "Oct",
350
+ "Nov",
351
+ "Dec",
352
+ ];
353
+
354
+ /**
355
+ * git's DATE_HUMAN (date.c show_date_normal with the current time): today's dates relative,
356
+ * this week's `Wed 14:03`, this year's `Mar 3`, older ones `Mar 3 2019`; the zone shown only
357
+ * when it is not the local one (and the date is near).
358
+ */
359
+ export function humanDate(
360
+ ident: PersonIdent,
361
+ now: number,
362
+ localTzOffset: (time: number) => number,
363
+ ): string {
364
+ const { d, tz } = zoned(ident);
365
+ const nowMinutes = localTzOffset(now);
366
+ const human = new Date((now + nowMinutes * 60) * 1000);
367
+ const sign = nowMinutes < 0 ? "-" : "+";
368
+ const abs = Math.abs(nowMinutes);
369
+ const humanTz = `${sign}${pad2(Math.floor(abs / 60))}${pad2(abs % 60)}`;
370
+ const hide = { year: false, date: false, wday: false, time: false, seconds: false, tz: false };
371
+ hide.tz = tz === humanTz;
372
+ hide.year = d.getUTCFullYear() === human.getUTCFullYear();
373
+ if (hide.year && d.getUTCMonth() === human.getUTCMonth()) {
374
+ if (d.getUTCDate() > human.getUTCDate()) {
375
+ // a future date: think time zones
376
+ } else if (d.getUTCDate() === human.getUTCDate()) {
377
+ hide.date = hide.wday = true;
378
+ } else if (d.getUTCDate() + 5 > human.getUTCDate()) {
379
+ hide.date = true;
380
+ }
381
+ }
382
+ if (hide.wday) return relativeDate(ident.timestamp, now);
383
+ hide.seconds = true;
384
+ hide.tz ||= !hide.date;
385
+ hide.wday = hide.time = !hide.year;
386
+ let out = "";
387
+ if (!hide.wday) out += `${WEEKDAYS[d.getUTCDay()]} `;
388
+ if (!hide.date) out += `${MONTH_NAMES[d.getUTCMonth()]} ${d.getUTCDate()} `;
389
+ if (!hide.time) out += `${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}`;
390
+ else out = out.replace(/[ \t\n\r]+$/, "");
391
+ if (!hide.year) out += ` ${d.getUTCFullYear()}`;
392
+ if (!hide.tz) out += ` ${tz}`;
393
+ return out;
394
+ }
395
+
396
+ /** git's show_date_relative: `5 minutes ago`, `2 years, 3 months ago`, `in the future`. */
397
+ export function relativeDate(time: number, now: number): string {
398
+ if (now < time) return "in the future";
399
+ const n = (count: number, one: string) => `${count} ${one}${count === 1 ? "" : "s"}`;
400
+ let diff = now - time;
401
+ if (diff < 90) return `${n(diff, "second")} ago`;
402
+ diff = Math.floor((diff + 30) / 60);
403
+ if (diff < 90) return `${n(diff, "minute")} ago`;
404
+ diff = Math.floor((diff + 30) / 60);
405
+ if (diff < 36) return `${n(diff, "hour")} ago`;
406
+ diff = Math.floor((diff + 12) / 24);
407
+ if (diff < 14) return `${n(diff, "day")} ago`;
408
+ if (diff < 70) return `${n(Math.floor((diff + 3) / 7), "week")} ago`;
409
+ if (diff < 365) return `${n(Math.floor((diff + 15) / 30), "month")} ago`;
410
+ if (diff < 1825) {
411
+ const total = Math.floor((diff * 12 * 2 + 365) / (365 * 2));
412
+ const years = Math.floor(total / 12);
413
+ const months = total % 12;
414
+ return months ? `${n(years, "year")}, ${n(months, "month")} ago` : `${n(years, "year")} ago`;
415
+ }
416
+ return `${n(Math.floor((diff + 183) / 365), "year")} ago`;
417
+ }
418
+
419
+ /** The message's lines: each with its line end. */
420
+ function lineAt(msg: string, at: number): { line: string; next: number } {
421
+ const nl = msg.indexOf("\n", at);
422
+ const next = nl < 0 ? msg.length : nl + 1;
423
+ return { line: msg.slice(at, next), next };
424
+ }
425
+
426
+ /** git's isspace (sane_ctype): space, tab, LF and CR only, never NBSP or other spaces */
427
+ const GIT_SPACE = /[ \t\n\r]/;
428
+ const TRAILING_SPACE = /[ \t\n\r]+$/;
429
+ const isBlank = (line: string) => line.replace(TRAILING_SPACE, "") === "";
430
+
431
+ /** pretty.c's skip_blank_lines. */
432
+ function skipBlankLines(msg: string, at: number): number {
433
+ let i = at;
434
+ while (i < msg.length) {
435
+ const { line, next } = lineAt(msg, i);
436
+ if (!isBlank(line)) break;
437
+ i = next;
438
+ }
439
+ return i;
440
+ }
441
+
442
+ /** pretty.c's format_subject: the first paragraph's lines, right-trimmed, joined by `sep`. */
443
+ function formatSubject(msg: string, at: number, sep: string): { text: string; end: number } {
444
+ const parts: string[] = [];
445
+ let i = at;
446
+ while (i < msg.length) {
447
+ const { line, next } = lineAt(msg, i);
448
+ i = next;
449
+ const trimmed = line.replace(TRAILING_SPACE, "");
450
+ if (trimmed === "") break;
451
+ parts.push(trimmed);
452
+ }
453
+ return { text: parts.join(sep), end: i };
454
+ }
455
+
456
+ const isTitleChar = (c: string) => /^[A-Za-z0-9._]$/.test(c);
457
+
458
+ /** pretty.c's format_sanitized_subject: `Fix the parser.` → `Fix-the-parser`. */
459
+ export function sanitizedSubject(line: string): string {
460
+ let out = "";
461
+ let space = 2;
462
+ for (let i = 0; i < line.length; i++) {
463
+ const c = line[i] as string;
464
+ if (isTitleChar(c)) {
465
+ if (space === 1) out += "-";
466
+ space = 0;
467
+ out += c;
468
+ if (c === ".") while (line[i + 1] === ".") i++;
469
+ } else space |= 1;
470
+ }
471
+ return out.replace(/[.-]+$/, "");
472
+ }
473
+
474
+ /** A byte of `%x`: ASCII as itself, a byte above 0x7f as the byte strings keep it raw. */
475
+ const rawByte = (n: number) => String.fromCharCode(n < 0x80 ? n : 0xdc00 + n);
476
+
477
+ /** git's hex2chr: two hex digits; -1 when not. */
478
+ function hex2chr(text: string, at: number): number {
479
+ const hex = /^[0-9a-fA-F]{2}/.exec(text.slice(at, at + 2));
480
+ return hex ? Number.parseInt(hex[0], 16) : -1;
481
+ }
482
+
483
+ /** strbuf_expand_literal: `%n` and `%x<hex>`; 0 when the placeholder is neither. */
484
+ function expandLiteral(fmt: string, at: number): { text: string; taken: number } {
485
+ if (fmt[at] === "n") return { text: "\n", taken: 1 };
486
+ if (fmt[at] === "x") {
487
+ const ch = hex2chr(fmt, at + 1);
488
+ if (ch >= 0) return { text: rawByte(ch), taken: 3 };
489
+ }
490
+ return { text: "", taken: 0 };
491
+ }
492
+
493
+ /** pretty.c's expand_string_arg: an argument's `%n`, `%x<hex>` and `%%` expanded. */
494
+ function expandStringArg(arg: string): string {
495
+ let out = "";
496
+ for (let i = 0; i < arg.length; i++) {
497
+ if (arg[i] !== "%") {
498
+ out += arg[i];
499
+ continue;
500
+ }
501
+ if (arg[i + 1] === "%") {
502
+ out += "%";
503
+ i++;
504
+ continue;
505
+ }
506
+ const literal = expandLiteral(arg, i + 1);
507
+ if (literal.taken > 0) {
508
+ out += literal.text;
509
+ i += literal.taken;
510
+ } else out += "%";
511
+ }
512
+ return out;
513
+ }
514
+
515
+ const ANSI_COLORS = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"];
516
+ const ATTRS: Record<string, [number, number]> = {
517
+ bold: [1, 22],
518
+ dim: [2, 22],
519
+ italic: [3, 23],
520
+ ul: [4, 24],
521
+ blink: [5, 25],
522
+ reverse: [7, 27],
523
+ strike: [9, 29],
524
+ };
525
+
526
+ /** Thrown for a color git cannot parse ("unable to parse --pretty format"). */
527
+ export class PrettyFormatError extends Error {
528
+ constructor(message: string) {
529
+ super(message);
530
+ this.name = "PrettyFormatError";
531
+ }
532
+ }
533
+
534
+ /** color.c's color_parse_mem: the escape sequence of a color spec (`bold red`, `#ff0000`). */
535
+ export function parseColorSpec(spec: string): string {
536
+ const words = spec.split(/[ \t\n\r]+/).filter((w) => w !== "");
537
+ if (words.length === 0) return "";
538
+ type Color =
539
+ | { kind: "normal" }
540
+ | { kind: "ansi"; value: number }
541
+ | { kind: "256"; value: number }
542
+ | { kind: "rgb"; r: number; g: number; b: number };
543
+ const parseOne = (word: string): Color | undefined => {
544
+ const w = word.toLowerCase();
545
+ if (w === "normal") return { kind: "normal" };
546
+ if ((word.length === 7 || word.length === 4) && word[0] === "#") {
547
+ const per = word.length === 7 ? 2 : 1;
548
+ const parts = [1, 1 + per, 1 + 2 * per].map((at) => word.slice(at, at + per));
549
+ if (parts.every((p) => /^[0-9a-fA-F]+$/.test(p))) {
550
+ const [r, g, b] = parts.map((p) => Number.parseInt(per === 1 ? p + p : p, 16)) as [
551
+ number,
552
+ number,
553
+ number,
554
+ ];
555
+ return { kind: "rgb", r, g, b };
556
+ }
557
+ }
558
+ if (w === "default") return { kind: "ansi", value: 39 };
559
+ let base = 30;
560
+ let name = w;
561
+ if (name.startsWith("bright")) {
562
+ base = 90;
563
+ name = name.slice(6);
564
+ }
565
+ const index = ANSI_COLORS.indexOf(name);
566
+ if (index >= 0) return { kind: "ansi", value: base + index };
567
+ if (/^-?\d+$/.test(word)) {
568
+ const val = Number(word);
569
+ if (val === -1) return { kind: "normal" };
570
+ if (val >= 0 && val < 8) return { kind: "ansi", value: 30 + val };
571
+ if (val >= 8 && val < 16) return { kind: "ansi", value: 90 + val - 8 };
572
+ if (val >= 16 && val < 256) return { kind: "256", value: val };
573
+ }
574
+ return undefined;
575
+ };
576
+ let reset = false;
577
+ let attr = 0;
578
+ let fg: Color | undefined;
579
+ let bg: Color | undefined;
580
+ for (const word of words) {
581
+ if (word.toLowerCase() === "reset") {
582
+ reset = true;
583
+ continue;
584
+ }
585
+ const color = parseOne(word);
586
+ if (color) {
587
+ if (!fg) fg = color;
588
+ else if (!bg) bg = color;
589
+ else throw new PrettyFormatError("fatal: unable to parse --pretty format");
590
+ continue;
591
+ }
592
+ let name = word.toLowerCase();
593
+ let negate = false;
594
+ if (name.startsWith("no")) {
595
+ negate = true;
596
+ name = name.slice(2).replace(/^-/, "");
597
+ }
598
+ const a = ATTRS[name];
599
+ if (!a) throw new PrettyFormatError("fatal: unable to parse --pretty format");
600
+ attr |= 1 << (negate ? a[1] : a[0]);
601
+ }
602
+ const output = (c: Color, background: boolean): string | undefined => {
603
+ const offset = background ? 10 : 0;
604
+ switch (c.kind) {
605
+ case "normal":
606
+ return undefined;
607
+ case "ansi":
608
+ return String(c.value + offset);
609
+ case "256":
610
+ return `${38 + offset};5;${c.value}`;
611
+ case "rgb":
612
+ return `${38 + offset};2;${c.r};${c.g};${c.b}`;
613
+ }
614
+ };
615
+ const fgOut = fg ? output(fg, false) : undefined;
616
+ const bgOut = bg ? output(bg, true) : undefined;
617
+ if (!reset && attr === 0 && fgOut === undefined && bgOut === undefined) return "";
618
+ const parts: string[] = [];
619
+ if (reset) parts.push("");
620
+ for (let i = 0; attr; i++) {
621
+ if (!(attr & (1 << i))) continue;
622
+ attr &= ~(1 << i);
623
+ parts.push(String(i));
624
+ }
625
+ if (fgOut !== undefined) parts.push(fgOut);
626
+ if (bgOut !== undefined) parts.push(bgOut);
627
+ // a reset is an empty first field: "\x1b[;31m"
628
+ return `\x1b[${parts.join(";")}m`;
629
+ }
630
+
631
+ /** The length of the ANSI color sequence at `at` (display_mode_esc_sequence_len); 0 if none. */
632
+ function escapeLength(text: string, at: number): number {
633
+ if (text[at] !== "\x1b" || text[at + 1] !== "[") return 0;
634
+ let i = at + 2;
635
+ while (i < text.length && /[0-9;]/.test(text[i] as string)) i++;
636
+ return text[i] === "m" ? i + 1 - at : 0;
637
+ }
638
+
639
+ /**
640
+ * The columns a code point takes (git's utf8_width, its unicode-width tables: wide East Asian
641
+ * 2, combining and other non-spacing 0), a control character none.
642
+ */
643
+ function charWidth(cp: number): number {
644
+ return Math.max(gitCharWidth(cp), 0);
645
+ }
646
+
647
+ /** utf8_strnwidth with skip_ansi: the columns of `text`, color sequences taking none. */
648
+ function displayWidth(text: string): number {
649
+ let w = 0;
650
+ for (let i = 0; i < text.length; ) {
651
+ const esc = escapeLength(text, i);
652
+ if (esc) {
653
+ i += esc;
654
+ continue;
655
+ }
656
+ const cp = text.codePointAt(i) as number;
657
+ w += charWidth(cp);
658
+ i += cp > 0xffff ? 2 : 1;
659
+ }
660
+ return w;
661
+ }
662
+
663
+ /** strbuf_utf8_replace: the columns [pos, pos + width) of `text` replaced by `subst`. */
664
+ function utf8Replace(text: string, pos: number, width: number, subst: string): string {
665
+ let out = "";
666
+ let w = 0;
667
+ let replaced = false;
668
+ for (let i = 0; i < text.length; ) {
669
+ const esc = escapeLength(text, i);
670
+ if (esc) {
671
+ out += text.slice(i, i + esc);
672
+ i += esc;
673
+ continue;
674
+ }
675
+ const cp = text.codePointAt(i) as number;
676
+ const len = cp > 0xffff ? 2 : 1;
677
+ const cw = charWidth(cp);
678
+ // a character of no width (a combining mark) is kept, attached to what precedes it
679
+ if (cw && w >= pos && w < pos + width) {
680
+ if (!replaced) {
681
+ out += subst;
682
+ replaced = true;
683
+ }
684
+ } else out += text.slice(i, i + len);
685
+ w += cw;
686
+ i += len;
687
+ }
688
+ if (!replaced) out += subst;
689
+ return out;
690
+ }
691
+
692
+ const isAlnum = (c: string | undefined) => c !== undefined && /^[A-Za-z0-9]$/.test(c);
693
+
694
+ /** utf8.c's strbuf_add_wrapped_text: `text` wrapped at `width` columns. */
695
+ export function wrapText(text: string, indent1: number, indent2: number, width: number): string {
696
+ if (width <= 0) {
697
+ // strbuf_add_indented_text
698
+ let out = "";
699
+ let indent = Math.max(indent1, 0);
700
+ let i = 0;
701
+ while (i < text.length) {
702
+ const nl = text.indexOf("\n", i);
703
+ const eol = nl < 0 ? text.length : nl + 1;
704
+ out += " ".repeat(indent) + text.slice(i, eol);
705
+ i = eol;
706
+ indent = indent2;
707
+ }
708
+ return out;
709
+ }
710
+ let out = "";
711
+ let bol = 0;
712
+ let w = indent1;
713
+ let indent = indent1;
714
+ let space: number | undefined;
715
+ if (indent < 0) {
716
+ w = -indent;
717
+ space = 0;
718
+ }
719
+ let i = 0;
720
+ for (;;) {
721
+ let esc = escapeLength(text, i);
722
+ while (esc) {
723
+ i += esc;
724
+ esc = escapeLength(text, i);
725
+ }
726
+ const c = text[i];
727
+ if (c === undefined || GIT_SPACE.test(c)) {
728
+ if (w <= width || space === undefined) {
729
+ let start = bol;
730
+ if (c === undefined && i === start) return out;
731
+ if (space !== undefined) start = space;
732
+ else out += " ".repeat(Math.max(indent, 0));
733
+ out += text.slice(start, i);
734
+ if (c === undefined) return out;
735
+ space = i;
736
+ if (c === "\t") w |= 0x07;
737
+ else if (c === "\n") {
738
+ space++;
739
+ if (text[space] === "\n") {
740
+ out += "\n";
741
+ // new line
742
+ out += "\n";
743
+ bol = space + (GIT_SPACE.test(text[space] ?? "") ? 1 : 0);
744
+ i = bol;
745
+ space = undefined;
746
+ w = indent = indent2;
747
+ continue;
748
+ } else if (!isAlnum(text[space])) {
749
+ out += "\n";
750
+ bol = space + (GIT_SPACE.test(text[space] ?? "") ? 1 : 0);
751
+ i = bol;
752
+ space = undefined;
753
+ w = indent = indent2;
754
+ continue;
755
+ } else out += " ";
756
+ }
757
+ w++;
758
+ i++;
759
+ } else {
760
+ out += "\n";
761
+ bol = space + (GIT_SPACE.test(text[space] ?? "") ? 1 : 0);
762
+ i = bol;
763
+ space = undefined;
764
+ w = indent = indent2;
765
+ }
766
+ continue;
767
+ }
768
+ const cp = text.codePointAt(i) as number;
769
+ // utf8_width as is: a control character takes a column back (-1)
770
+ w += gitCharWidth(cp);
771
+ i += cp > 0xffff ? 2 : 1;
772
+ }
773
+ }
774
+
775
+ /** The output being built, and the state git's format_commit_context keeps. */
776
+ interface FormatState {
777
+ sb: string;
778
+ /** %w: where the text to wrap starts, and the wrapping in force */
779
+ wrapStart: number;
780
+ width: number;
781
+ indent1: number;
782
+ indent2: number;
783
+ /** %<( and friends: the padding of the next placeholder */
784
+ flush: "none" | "left" | "right" | "both" | "left-steal";
785
+ padding: number;
786
+ truncate: "none" | "left" | "middle" | "right";
787
+ /** whether %C(auto) is in force (colors only with options.color) */
788
+ autoColor: boolean;
789
+ }
790
+
791
+ /** git's rewrap_message_tail. */
792
+ function rewrapTail(st: FormatState, width: number, indent1: number, indent2: number): void {
793
+ if (st.width === width && st.indent1 === indent1 && st.indent2 === indent2) return;
794
+ if (st.wrapStart < st.sb.length) {
795
+ st.sb =
796
+ st.sb.slice(0, st.wrapStart) +
797
+ wrapText(st.sb.slice(st.wrapStart), st.indent1, st.indent2, st.width);
798
+ }
799
+ st.wrapStart = st.sb.length;
800
+ st.width = width;
801
+ st.indent1 = indent1;
802
+ st.indent2 = indent2;
803
+ }
804
+
805
+ /** git's FORMATTING_LIMIT */
806
+ const FORMATTING_LIMIT = 16 * 1024;
807
+
808
+ /** pretty.c's parse_padding_placeholder; 0 when it is none. */
809
+ function parsePadding(fmt: string, at: number, st: FormatState): number {
810
+ let ch = at;
811
+ let flush: FormatState["flush"];
812
+ const first = fmt[ch++];
813
+ if (first === "<") flush = "right";
814
+ else if (first === ">") {
815
+ if (fmt[ch] === "<") {
816
+ flush = "both";
817
+ ch++;
818
+ } else if (fmt[ch] === ">") {
819
+ flush = "left-steal";
820
+ ch++;
821
+ } else flush = "left";
822
+ } else return 0;
823
+ let toColumn = false;
824
+ if (fmt[ch] === "|") {
825
+ toColumn = true;
826
+ ch++;
827
+ }
828
+ if (fmt[ch] !== "(") return 0;
829
+ const start = ch + 1;
830
+ let end = start;
831
+ while (end < fmt.length && fmt[end] !== "," && fmt[end] !== ")") end++;
832
+ if (end >= fmt.length || end === start) return 0;
833
+ const num = /^[ \t\n\r]*[+-]?\d+/.exec(fmt.slice(start, end));
834
+ if (!num) return 0;
835
+ let width = Number.parseInt(num[0], 10);
836
+ if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT || width === 0) return 0;
837
+ if (width < 0) {
838
+ // the terminal's width: 80 without a terminal
839
+ if (toColumn) width += 80;
840
+ if (width < 0) return 0;
841
+ }
842
+ let truncate: FormatState["truncate"] = "none";
843
+ let close = end;
844
+ if (fmt[end] === ",") {
845
+ const s = end + 1;
846
+ close = fmt.indexOf(")", s);
847
+ if (close < 0 || close === s) return 0;
848
+ const rest = fmt.slice(s);
849
+ if (rest.startsWith("trunc)")) truncate = "right";
850
+ else if (rest.startsWith("ltrunc)")) truncate = "left";
851
+ else if (rest.startsWith("mtrunc)")) truncate = "middle";
852
+ else return 0;
853
+ }
854
+ st.padding = toColumn ? -width : width;
855
+ st.flush = flush;
856
+ st.truncate = truncate;
857
+ return close - at + 1;
858
+ }
859
+
860
+ /**
861
+ * pretty.c's match_placeholder_arg_value: `<name>` or `<name>=<value>` at `at`, followed by
862
+ * `,` (taken) or `)` (left); the value runs to the next `,` or `)`.
863
+ */
864
+ function matchArgValue(
865
+ fmt: string,
866
+ at: number,
867
+ name: string,
868
+ ): { value?: string; end: number } | undefined {
869
+ if (!fmt.startsWith(name, at)) return undefined;
870
+ let p = at + name.length;
871
+ let value: string | undefined;
872
+ if (fmt[p] === "=") {
873
+ let e = p + 1;
874
+ while (e < fmt.length && fmt[e] !== "," && fmt[e] !== ")") e++;
875
+ value = fmt.slice(p + 1, e);
876
+ p = e;
877
+ } else if (fmt[p] !== "," && fmt[p] !== ")") return undefined;
878
+ if (fmt[p] === ",") return { ...(value !== undefined ? { value } : {}), end: p + 1 };
879
+ if (fmt[p] === ")") return { ...(value !== undefined ? { value } : {}), end: p };
880
+ return undefined;
881
+ }
882
+
883
+ /** git_parse_maybe_bool: true/yes/on, false/no/off, "" (false), a number; else -1. */
884
+ function parseMaybeBool(value: string): boolean | undefined {
885
+ const v = value.toLowerCase();
886
+ if (v === "true" || v === "yes" || v === "on") return true;
887
+ if (v === "false" || v === "no" || v === "off" || v === "") return false;
888
+ const n = /^([+-]?\d+)([kmg]?)$/.exec(v);
889
+ if (n) return Number(n[1]) !== 0;
890
+ return undefined;
891
+ }
892
+
893
+ /** pretty.c's match_placeholder_bool_arg: `<name>` (true) or `<name>=<bool>`. */
894
+ function matchBoolArg(
895
+ fmt: string,
896
+ at: number,
897
+ name: string,
898
+ ): { value: boolean; end: number } | undefined {
899
+ const arg = matchArgValue(fmt, at, name);
900
+ if (!arg) return undefined;
901
+ if (arg.value === undefined) return { value: true, end: arg.end };
902
+ const value = parseMaybeBool(arg.value);
903
+ return value === undefined ? undefined : { value, end: arg.end };
904
+ }
905
+
906
+ /** The commit and what the format reads besides it. */
907
+ interface FormatContext {
908
+ commit: PrettyCommit;
909
+ id: ObjectId;
910
+ options: PrettyFormatOptions;
911
+ }
912
+
913
+ /**
914
+ * pretty.c's format_commit_one: one placeholder at `fmt[at]` (after `%`) expanded at the end
915
+ * of `st.sb`; the characters it takes, 0 for one git does not know (or this does not
916
+ * support).
917
+ */
918
+ async function formatOne(
919
+ fmt: string,
920
+ at: number,
921
+ st: FormatState,
922
+ cx: FormatContext,
923
+ ): Promise<number> {
924
+ const { commit, id, options } = cx;
925
+ const literal = expandLiteral(fmt, at);
926
+ if (literal.taken > 0) {
927
+ st.sb += literal.text;
928
+ return literal.taken;
929
+ }
930
+ const c = fmt[at];
931
+ const abbrev = (oid: ObjectId) => (options.abbreviate ? options.abbreviate(oid) : oid);
932
+ switch (c) {
933
+ case "C": {
934
+ if (fmt.startsWith("(auto)", at + 1)) {
935
+ st.autoColor = !!options.color;
936
+ if (options.color && st.sb.length > 0) st.sb += "\x1b[m";
937
+ return 7;
938
+ }
939
+ if (fmt[at + 1] === "(") {
940
+ const end = fmt.indexOf(")", at + 2);
941
+ if (end < 0) return 0;
942
+ let spec = fmt.slice(at + 2, end);
943
+ let want = !!options.color;
944
+ if (spec.startsWith("auto,")) spec = spec.slice(5);
945
+ else if (spec.startsWith("always,")) {
946
+ spec = spec.slice(7);
947
+ want = true;
948
+ }
949
+ st.autoColor = false;
950
+ if (!want) return end - at + 1;
951
+ st.sb += parseColorSpec(spec);
952
+ return end - at + 1;
953
+ }
954
+ const basic: Record<string, string> = {
955
+ red: "\x1b[31m",
956
+ green: "\x1b[32m",
957
+ blue: "\x1b[34m",
958
+ reset: "\x1b[m",
959
+ };
960
+ for (const name of ["red", "green", "blue", "reset"]) {
961
+ if (fmt.startsWith(name, at + 1)) {
962
+ st.autoColor = false;
963
+ if (options.color) st.sb += basic[name];
964
+ return 1 + name.length;
965
+ }
966
+ }
967
+ // %C<unknown> is a literal and the previous %C(auto) stays
968
+ return 0;
969
+ }
970
+ case "w": {
971
+ if (fmt[at + 1] !== "(") return 0;
972
+ const end = fmt.indexOf(")", at + 2);
973
+ if (end < 0) return 0;
974
+ const args = fmt.slice(at + 2, end);
975
+ let width = 0;
976
+ let indent1 = 0;
977
+ let indent2 = 0;
978
+ if (args !== "") {
979
+ const m = /^[ \t\n\r]*\+?(\d*)(?:,[ \t\n\r]*\+?(\d*)(?:,[ \t\n\r]*\+?(\d*))?)?$/.exec(args);
980
+ if (!m) return 0;
981
+ width = Number(m[1] || 0);
982
+ indent1 = Number(m[2] || 0);
983
+ indent2 = Number(m[3] || 0);
984
+ }
985
+ if (width > FORMATTING_LIMIT || indent1 > FORMATTING_LIMIT || indent2 > FORMATTING_LIMIT)
986
+ return 0;
987
+ rewrapTail(st, width, indent1, indent2);
988
+ return end - at + 1;
989
+ }
990
+ case "<":
991
+ case ">":
992
+ return parsePadding(fmt, at, st);
993
+ }
994
+ switch (c) {
995
+ case "H":
996
+ st.sb += id;
997
+ return 1;
998
+ case "h":
999
+ st.sb += await abbrev(id);
1000
+ return 1;
1001
+ case "T":
1002
+ st.sb += commit.tree;
1003
+ return 1;
1004
+ case "t":
1005
+ st.sb += await abbrev(commit.tree);
1006
+ return 1;
1007
+ case "P":
1008
+ st.sb += commit.parents.join(" ");
1009
+ return 1;
1010
+ case "p": {
1011
+ const parts: string[] = [];
1012
+ for (const p of commit.parents) parts.push(await abbrev(p));
1013
+ st.sb += parts.join(" ");
1014
+ return 1;
1015
+ }
1016
+ case "m":
1017
+ st.sb += options.mark ?? ">";
1018
+ return 1;
1019
+ case "d":
1020
+ st.sb += formatRefDecorations(options.decorations, id, {});
1021
+ return 1;
1022
+ case "D":
1023
+ st.sb += formatRefDecorations(options.decorations, id, { prefix: "", suffix: "" });
1024
+ return 1;
1025
+ case "g": {
1026
+ const reflog = options.reflog;
1027
+ switch (fmt[at + 1]) {
1028
+ case "d":
1029
+ st.sb += reflog?.selector ?? "";
1030
+ return 2;
1031
+ case "D":
1032
+ st.sb += reflog?.selector ?? "";
1033
+ return 2;
1034
+ case "s":
1035
+ st.sb += reflog?.message ?? "";
1036
+ return 2;
1037
+ case "n":
1038
+ case "N":
1039
+ case "e":
1040
+ case "E": {
1041
+ if (!reflog?.ident) return 2;
1042
+ const part = formatPersonPart(reflog.ident, fmt[at + 1], options);
1043
+ if (part !== undefined) st.sb += part;
1044
+ return 2;
1045
+ }
1046
+ }
1047
+ return 0;
1048
+ }
1049
+ case "N":
1050
+ if (options.notes === undefined) return 0;
1051
+ st.sb += options.notes;
1052
+ return 1;
1053
+ case "G": {
1054
+ const signed = !!(commit as { gpgSignature?: string }).gpgSignature;
1055
+ switch (fmt[at + 1]) {
1056
+ case "?":
1057
+ st.sb += signed ? "E" : "N";
1058
+ return 2;
1059
+ case "G":
1060
+ case "S":
1061
+ case "K":
1062
+ case "F":
1063
+ case "P":
1064
+ return 2;
1065
+ case "T":
1066
+ st.sb += "undefined";
1067
+ return 2;
1068
+ }
1069
+ return 0;
1070
+ }
1071
+ }
1072
+ if (fmt.startsWith("(decorate", at)) {
1073
+ let i = at + 9;
1074
+ const opts: Record<string, string> = {};
1075
+ if (fmt[i] === ":") {
1076
+ i++;
1077
+ for (;;) {
1078
+ const m = /^(prefix|suffix|separator|pointer|tag)=/.exec(fmt.slice(i));
1079
+ if (!m) break;
1080
+ // match_placeholder_arg_value: the value runs to the next "," or ")"
1081
+ const valueStart = i + m[0].length;
1082
+ let valueEnd = valueStart;
1083
+ while (valueEnd < fmt.length && fmt[valueEnd] !== "," && fmt[valueEnd] !== ")") valueEnd++;
1084
+ if (valueEnd >= fmt.length) break;
1085
+ opts[m[1] as string] = expandStringArg(fmt.slice(valueStart, valueEnd));
1086
+ i = fmt[valueEnd] === "," ? valueEnd + 1 : valueEnd;
1087
+ }
1088
+ }
1089
+ if (fmt[i] !== ")") return 0;
1090
+ st.sb += formatRefDecorations(options.decorations, id, opts);
1091
+ return i - at + 1;
1092
+ }
1093
+ if (fmt.startsWith("(describe", at)) {
1094
+ let i = at + 9;
1095
+ const describeOptions: { tags?: boolean; abbrev?: number; match: string[]; exclude: string[] } =
1096
+ { match: [], exclude: [] };
1097
+ if (fmt[i] === ":") {
1098
+ i++;
1099
+ // parse_describe_args: the options it knows, up to the first it does not
1100
+ for (;;) {
1101
+ const tagsArg = matchBoolArg(fmt, i, "tags");
1102
+ if (tagsArg) {
1103
+ describeOptions.tags = tagsArg.value;
1104
+ i = tagsArg.end;
1105
+ continue;
1106
+ }
1107
+ const abbrevArg = matchArgValue(fmt, i, "abbrev");
1108
+ if (abbrevArg) {
1109
+ if (abbrevArg.value === undefined || !/^[+-]?\d+$/.test(abbrevArg.value)) return 0;
1110
+ let n = Number.parseInt(abbrevArg.value, 10);
1111
+ // parse_opt_abbrev_cb: at least 4 (MINIMUM_ABBREV), at most the hash's length
1112
+ if (n && n < 4) n = 4;
1113
+ else if (n > 40) n = 40;
1114
+ describeOptions.abbrev = n;
1115
+ i = abbrevArg.end;
1116
+ continue;
1117
+ }
1118
+ const listArg = matchArgValue(fmt, i, "exclude") ?? matchArgValue(fmt, i, "match");
1119
+ if (listArg) {
1120
+ if (!listArg.value) return 0;
1121
+ (fmt.startsWith("exclude", i) ? describeOptions.exclude : describeOptions.match).push(
1122
+ listArg.value,
1123
+ );
1124
+ i = listArg.end;
1125
+ continue;
1126
+ }
1127
+ break;
1128
+ }
1129
+ }
1130
+ if (fmt[i] !== ")" || !options.describe) return 0;
1131
+ const out = await options.describe(id, {
1132
+ ...(describeOptions.tags !== undefined ? { tags: describeOptions.tags } : {}),
1133
+ ...(describeOptions.abbrev !== undefined ? { abbrev: describeOptions.abbrev } : {}),
1134
+ ...(describeOptions.match.length ? { match: describeOptions.match } : {}),
1135
+ ...(describeOptions.exclude.length ? { exclude: describeOptions.exclude } : {}),
1136
+ });
1137
+ st.sb += out.replace(/[ \t\n\r]+$/, "");
1138
+ return i - at + 1;
1139
+ }
1140
+ if (fmt.startsWith("(count)", at) || fmt.startsWith("(total)", at)) {
1141
+ const name = fmt.slice(at, at + 7);
1142
+ if (!options.count)
1143
+ throw new PrettyFormatError(`fatal: %${name} is not supported by this command`);
1144
+ const { nr, total } = options.count;
1145
+ st.sb +=
1146
+ name === "(count)" ? String(nr).padStart(String(Math.abs(total)).length, "0") : String(total);
1147
+ return 7;
1148
+ }
1149
+ if (c === "S") {
1150
+ if (options.source === undefined || options.source === "") return 0;
1151
+ st.sb += options.source;
1152
+ return 1;
1153
+ }
1154
+ if (fmt.startsWith("(trailers", at)) {
1155
+ let i = at + 9;
1156
+ const trailerOptions: TrailerFormatOptions & { keys?: string[] } = {};
1157
+ if (fmt[i] === ":") {
1158
+ i++;
1159
+ // format_set_trailers_options
1160
+ for (;;) {
1161
+ if (fmt[i] === ")") break;
1162
+ const key = matchArgValue(fmt, i, "key");
1163
+ if (key) {
1164
+ if (key.value === undefined) return 0;
1165
+ trailerOptions.keys = [...(trailerOptions.keys ?? []), key.value];
1166
+ trailerOptions.onlyTrailers = true;
1167
+ i = key.end;
1168
+ continue;
1169
+ }
1170
+ const separator = matchArgValue(fmt, i, "separator");
1171
+ if (separator) {
1172
+ trailerOptions.separator = expandStringArg(separator.value ?? "");
1173
+ i = separator.end;
1174
+ continue;
1175
+ }
1176
+ const kvSeparator = matchArgValue(fmt, i, "key_value_separator");
1177
+ if (kvSeparator) {
1178
+ trailerOptions.keyValueSeparator = expandStringArg(kvSeparator.value ?? "");
1179
+ i = kvSeparator.end;
1180
+ continue;
1181
+ }
1182
+ const flag = (
1183
+ [
1184
+ ["only", "onlyTrailers"],
1185
+ ["unfold", "unfold"],
1186
+ ["keyonly", "keyOnly"],
1187
+ ["valueonly", "valueOnly"],
1188
+ ] as const
1189
+ ).find(([name]) => matchBoolArg(fmt, i, name));
1190
+ if (!flag) return 0;
1191
+ const arg = matchBoolArg(fmt, i, flag[0]) as { value: boolean; end: number };
1192
+ trailerOptions[flag[1]] = arg.value;
1193
+ i = arg.end;
1194
+ }
1195
+ }
1196
+ if (fmt[i] !== ")") return 0;
1197
+ const start = skipBlankLines(commit.message, 0);
1198
+ st.sb = appendTrailers(st.sb, commit.message.slice(start), trailerOptions, options.trailers);
1199
+ return i - at + 1;
1200
+ }
1201
+ switch (c) {
1202
+ case "a":
1203
+ case "c": {
1204
+ const ident = c === "a" ? commit.author : commit.committer;
1205
+ const part = formatPersonPart(ident, fmt[at + 1], options);
1206
+ if (part === undefined) return 0;
1207
+ st.sb += part;
1208
+ return 2;
1209
+ }
1210
+ case "e":
1211
+ st.sb += commit.encoding ?? "";
1212
+ return 1;
1213
+ case "B":
1214
+ st.sb += commit.message;
1215
+ return 1;
1216
+ case "s": {
1217
+ const start = skipBlankLines(commit.message, 0);
1218
+ st.sb += formatSubject(commit.message, start, " ").text;
1219
+ return 1;
1220
+ }
1221
+ case "f": {
1222
+ const start = skipBlankLines(commit.message, 0);
1223
+ const nl = commit.message.indexOf("\n", start);
1224
+ const line = commit.message.slice(start, nl < 0 ? commit.message.length : nl);
1225
+ st.sb += sanitizedSubject(line);
1226
+ return 1;
1227
+ }
1228
+ case "b": {
1229
+ const start = skipBlankLines(commit.message, 0);
1230
+ const { end } = formatSubject(commit.message, start, " ");
1231
+ st.sb += commit.message.slice(skipBlankLines(commit.message, end));
1232
+ return 1;
1233
+ }
1234
+ }
1235
+ return 0;
1236
+ }
1237
+
1238
+ /** pretty.c's format_and_pad_commit: the next placeholder, padded or truncated. */
1239
+ async function formatAndPad(
1240
+ fmt: string,
1241
+ at: number,
1242
+ st: FormatState,
1243
+ cx: FormatContext,
1244
+ ): Promise<number> {
1245
+ let padding = st.padding;
1246
+ if (padding < 0) {
1247
+ const nl = st.sb.lastIndexOf("\n");
1248
+ const occupied = displayWidth(nl < 0 ? st.sb : st.sb.slice(nl));
1249
+ padding = -padding - occupied;
1250
+ }
1251
+ const outer = st.sb;
1252
+ st.sb = "";
1253
+ const flushType = st.flush;
1254
+ const truncate = st.truncate;
1255
+ st.flush = "none";
1256
+ let total = 0;
1257
+ let p = at;
1258
+ for (;;) {
1259
+ const modifier = fmt[p] === "C";
1260
+ const consumed = await formatOne(fmt, p, st, cx);
1261
+ total += consumed;
1262
+ if (!modifier) break;
1263
+ p += consumed;
1264
+ if (fmt[p] !== "%") break;
1265
+ p++;
1266
+ total++;
1267
+ }
1268
+ let local = st.sb;
1269
+ let sb = outer;
1270
+ const len = displayWidth(local);
1271
+ let flush = flushType;
1272
+ if (flush === "left-steal") {
1273
+ // take the spaces (and keep the colors) at the end of what is already written
1274
+ let ch = sb.length - 1;
1275
+ while (len > padding && ch > 0) {
1276
+ if (sb[ch] === " ") {
1277
+ ch--;
1278
+ padding++;
1279
+ continue;
1280
+ }
1281
+ if (sb[ch] !== "m") break;
1282
+ let q = ch - 1;
1283
+ while (q > 0 && ch - q < 10 && sb[q] !== "\x1b") q--;
1284
+ if (sb[q] !== "\x1b" || ch + 1 - q !== escapeLength(sb, q)) break;
1285
+ local = sb.slice(q, ch + 1) + local;
1286
+ ch = q - 1;
1287
+ }
1288
+ sb = sb.slice(0, ch + 1);
1289
+ flush = "left";
1290
+ }
1291
+ if (len > padding) {
1292
+ switch (truncate) {
1293
+ case "left":
1294
+ local = utf8Replace(local, 0, len - (padding - 2), "..");
1295
+ break;
1296
+ case "middle":
1297
+ local = utf8Replace(local, Math.trunc(padding / 2) - 1, len - (padding - 2), "..");
1298
+ break;
1299
+ case "right":
1300
+ local = utf8Replace(local, padding - 2, len - (padding - 2), "..");
1301
+ break;
1302
+ }
1303
+ sb += local;
1304
+ } else {
1305
+ let offset = 0;
1306
+ if (flush === "left") offset = padding - len;
1307
+ else if (flush === "both") offset = Math.trunc((padding - len) / 2);
1308
+ sb += " ".repeat(offset) + local + " ".repeat(padding - len - offset);
1309
+ }
1310
+ st.sb = sb;
1311
+ return total;
1312
+ }
1313
+
1314
+ /** pretty.c's format_person_part; undefined for a part it does not know. */
1315
+ function formatPersonPart(
1316
+ ident: PersonIdent,
1317
+ part: string | undefined,
1318
+ options: PrettyFormatOptions,
1319
+ ): string | undefined {
1320
+ let { name, email } = ident;
1321
+ if ((part === "N" || part === "E" || part === "L") && options.mailmap) {
1322
+ ({ name, email } = options.mailmap(name, email));
1323
+ }
1324
+ switch (part) {
1325
+ case "n":
1326
+ case "N":
1327
+ return name;
1328
+ case "e":
1329
+ case "E":
1330
+ return email;
1331
+ case "l":
1332
+ case "L": {
1333
+ const at = email.indexOf("@");
1334
+ return at < 0 ? email : email.slice(0, at);
1335
+ }
1336
+ case "t":
1337
+ return String(ident.timestamp);
1338
+ case "d":
1339
+ return options.formatDate ? options.formatDate(ident) : formatGitDate(ident);
1340
+ case "D":
1341
+ return formatRfc2822Date(ident.timestamp, zoned(ident).tz);
1342
+ case "r":
1343
+ return relativeDate(ident.timestamp, options.now ?? Math.floor(Date.now() / 1000));
1344
+ case "i":
1345
+ return isoDate(ident);
1346
+ case "I":
1347
+ return strictIsoDate(ident);
1348
+ case "s":
1349
+ return shortDate(ident);
1350
+ case "h":
1351
+ return humanDate(
1352
+ ident,
1353
+ options.now ?? Math.floor(Date.now() / 1000),
1354
+ options.localTzOffset ?? ((time) => -new Date(time * 1000).getTimezoneOffset()),
1355
+ );
1356
+ default:
1357
+ return undefined;
1358
+ }
1359
+ }
1360
+
1361
+ /**
1362
+ * `format` expanded for commit `id` as git's format_commit_message expands a user format:
1363
+ * each `%<placeholder>`, with the `%+` (a newline before a non-empty expansion), `%-` (the
1364
+ * newlines before an empty one removed) and `% ` (a space before a non-empty one) magic,
1365
+ * the padding a `%<(…)` gives the next placeholder, and the wrapping of `%w(…)`.
1366
+ */
1367
+ export async function formatCommitPretty(
1368
+ format: string,
1369
+ commit: PrettyCommit,
1370
+ id: ObjectId,
1371
+ options: PrettyFormatOptions = {},
1372
+ ): Promise<string> {
1373
+ const st: FormatState = {
1374
+ sb: "",
1375
+ wrapStart: 0,
1376
+ width: 0,
1377
+ indent1: 0,
1378
+ indent2: 0,
1379
+ flush: "none",
1380
+ padding: 0,
1381
+ truncate: "none",
1382
+ autoColor: false,
1383
+ };
1384
+ const cx: FormatContext = { commit, id, options };
1385
+ let i = 0;
1386
+ while (i < format.length) {
1387
+ const pct = format.indexOf("%", i);
1388
+ if (pct < 0) {
1389
+ st.sb += format.slice(i);
1390
+ break;
1391
+ }
1392
+ st.sb += format.slice(i, pct);
1393
+ i = pct + 1;
1394
+ if (format[i] === "%") {
1395
+ st.sb += "%";
1396
+ i++;
1397
+ continue;
1398
+ }
1399
+ const consumed = await formatItem(format, i, st, cx);
1400
+ if (consumed === 0) st.sb += "%";
1401
+ else i += consumed;
1402
+ }
1403
+ rewrapTail(st, 0, 0, 0);
1404
+ return st.sb;
1405
+ }
1406
+
1407
+ /** pretty.c's format_commit_item: the magic, then the placeholder (padded when asked). */
1408
+ async function formatItem(
1409
+ fmt: string,
1410
+ at: number,
1411
+ st: FormatState,
1412
+ cx: FormatContext,
1413
+ ): Promise<number> {
1414
+ const first = fmt[at];
1415
+ const magic = first === "-" || first === "+" || first === " " ? first : "";
1416
+ let p = at;
1417
+ if (magic) {
1418
+ p++;
1419
+ // `%+w()` cannot be handled: refused, kept as written
1420
+ if (fmt[p] === "w") return 0;
1421
+ }
1422
+ const before = st.sb.length;
1423
+ const consumed =
1424
+ st.flush === "none" ? await formatOne(fmt, p, st, cx) : await formatAndPad(fmt, p, st, cx);
1425
+ if (!magic) return consumed;
1426
+ if (st.sb.length === before && magic === "-") st.sb = st.sb.replace(/\n+$/, "");
1427
+ else if (st.sb.length !== before) {
1428
+ if (magic === "+") st.sb = `${st.sb.slice(0, before)}\n${st.sb.slice(before)}`;
1429
+ else if (magic === " ") st.sb = `${st.sb.slice(0, before)} ${st.sb.slice(before)}`;
1430
+ }
1431
+ return consumed + 1;
1432
+ }