@statewalker/vcs-commands 0.7.0 → 0.8.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 (410) hide show
  1. package/dist/commands/add-command.d.ts +38 -0
  2. package/dist/commands/add-command.d.ts.map +1 -1
  3. package/dist/commands/am/am-command.d.ts +55 -1
  4. package/dist/commands/am/am-command.d.ts.map +1 -1
  5. package/dist/commands/am/index.d.ts +1 -0
  6. package/dist/commands/am/index.d.ts.map +1 -1
  7. package/dist/commands/am/mailsplit.d.ts +88 -0
  8. package/dist/commands/am/mailsplit.d.ts.map +1 -0
  9. package/dist/commands/apply/apply-command.d.ts.map +1 -1
  10. package/dist/commands/apply/apply-engine.d.ts.map +1 -1
  11. package/dist/commands/apply/apply-parse.d.ts.map +1 -1
  12. package/dist/commands/bisect/bisect-command.d.ts +2 -0
  13. package/dist/commands/bisect/bisect-command.d.ts.map +1 -1
  14. package/dist/commands/branch-command.d.ts +148 -18
  15. package/dist/commands/branch-command.d.ts.map +1 -1
  16. package/dist/commands/branch-info-command.d.ts +55 -0
  17. package/dist/commands/branch-info-command.d.ts.map +1 -0
  18. package/dist/commands/branch-tracking.d.ts +106 -17
  19. package/dist/commands/branch-tracking.d.ts.map +1 -1
  20. package/dist/commands/branch-upstream-command.d.ts +83 -0
  21. package/dist/commands/branch-upstream-command.d.ts.map +1 -0
  22. package/dist/commands/checkout-command.d.ts +19 -3
  23. package/dist/commands/checkout-command.d.ts.map +1 -1
  24. package/dist/commands/cherry-pick-command.d.ts +59 -0
  25. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  26. package/dist/commands/clean-command.d.ts +12 -0
  27. package/dist/commands/clean-command.d.ts.map +1 -1
  28. package/dist/commands/clone-command.d.ts.map +1 -1
  29. package/dist/commands/commit-command.d.ts +191 -5
  30. package/dist/commands/commit-command.d.ts.map +1 -1
  31. package/dist/commands/commit-reuse.d.ts +33 -0
  32. package/dist/commands/commit-reuse.d.ts.map +1 -0
  33. package/dist/commands/config-command.d.ts +236 -0
  34. package/dist/commands/config-command.d.ts.map +1 -0
  35. package/dist/commands/config-value-types.d.ts +91 -0
  36. package/dist/commands/config-value-types.d.ts.map +1 -0
  37. package/dist/commands/core-config.d.ts +20 -0
  38. package/dist/commands/core-config.d.ts.map +1 -1
  39. package/dist/commands/diff-color-config.d.ts +73 -0
  40. package/dist/commands/diff-color-config.d.ts.map +1 -0
  41. package/dist/commands/diff-command.d.ts +252 -5
  42. package/dist/commands/diff-command.d.ts.map +1 -1
  43. package/dist/commands/fetch-command.d.ts +20 -0
  44. package/dist/commands/fetch-command.d.ts.map +1 -1
  45. package/dist/commands/fmt-merge-msg-command.d.ts +124 -0
  46. package/dist/commands/fmt-merge-msg-command.d.ts.map +1 -0
  47. package/dist/commands/fork-point.d.ts +38 -0
  48. package/dist/commands/fork-point.d.ts.map +1 -0
  49. package/dist/commands/format-patch-command.d.ts +41 -1
  50. package/dist/commands/format-patch-command.d.ts.map +1 -1
  51. package/dist/commands/fsck/fetch-fsck.d.ts +51 -0
  52. package/dist/commands/fsck/fetch-fsck.d.ts.map +1 -1
  53. package/dist/commands/gc-command.d.ts +8 -0
  54. package/dist/commands/gc-command.d.ts.map +1 -1
  55. package/dist/commands/git-describe.d.ts +4 -0
  56. package/dist/commands/git-describe.d.ts.map +1 -1
  57. package/dist/commands/git-hooks.d.ts +35 -0
  58. package/dist/commands/git-hooks.d.ts.map +1 -1
  59. package/dist/commands/grep/grep-command.d.ts +210 -0
  60. package/dist/commands/grep/grep-command.d.ts.map +1 -0
  61. package/dist/commands/grep/grep-engine.d.ts +218 -0
  62. package/dist/commands/grep/grep-engine.d.ts.map +1 -0
  63. package/dist/commands/grep/index.d.ts +3 -0
  64. package/dist/commands/grep/index.d.ts.map +1 -0
  65. package/dist/commands/index-adder.d.ts +13 -2
  66. package/dist/commands/index-adder.d.ts.map +1 -1
  67. package/dist/commands/index.d.ts +26 -0
  68. package/dist/commands/index.d.ts.map +1 -1
  69. package/dist/commands/index.js +3 -3
  70. package/dist/commands/interpret-trailers-command.d.ts +90 -0
  71. package/dist/commands/interpret-trailers-command.d.ts.map +1 -0
  72. package/dist/commands/local-transport.d.ts +36 -5
  73. package/dist/commands/local-transport.d.ts.map +1 -1
  74. package/dist/commands/log-command.d.ts +296 -11
  75. package/dist/commands/log-command.d.ts.map +1 -1
  76. package/dist/commands/log-graph.d.ts +26 -1
  77. package/dist/commands/log-graph.d.ts.map +1 -1
  78. package/dist/commands/log-output.d.ts +133 -5
  79. package/dist/commands/log-output.d.ts.map +1 -1
  80. package/dist/commands/ls-remote-command.d.ts +2 -0
  81. package/dist/commands/ls-remote-command.d.ts.map +1 -1
  82. package/dist/commands/mailmap.d.ts +30 -0
  83. package/dist/commands/mailmap.d.ts.map +1 -1
  84. package/dist/commands/merge-base-command.d.ts +60 -0
  85. package/dist/commands/merge-base-command.d.ts.map +1 -0
  86. package/dist/commands/merge-bases.d.ts +23 -0
  87. package/dist/commands/merge-bases.d.ts.map +1 -1
  88. package/dist/commands/merge-command.d.ts +96 -9
  89. package/dist/commands/merge-command.d.ts.map +1 -1
  90. package/dist/commands/merge-support.d.ts.map +1 -1
  91. package/dist/commands/mv-command.d.ts +4 -0
  92. package/dist/commands/mv-command.d.ts.map +1 -1
  93. package/dist/commands/name-rev.d.ts +48 -0
  94. package/dist/commands/name-rev.d.ts.map +1 -0
  95. package/dist/commands/notes-command.d.ts.map +1 -1
  96. package/dist/commands/pathspec-file.d.ts +39 -0
  97. package/dist/commands/pathspec-file.d.ts.map +1 -0
  98. package/dist/commands/pathspec.d.ts +3 -0
  99. package/dist/commands/pathspec.d.ts.map +1 -1
  100. package/dist/commands/pretty-format.d.ts +43 -3
  101. package/dist/commands/pretty-format.d.ts.map +1 -1
  102. package/dist/commands/promisor-remote.d.ts.map +1 -1
  103. package/dist/commands/pull-command.d.ts +10 -0
  104. package/dist/commands/pull-command.d.ts.map +1 -1
  105. package/dist/commands/push-command.d.ts +160 -9
  106. package/dist/commands/push-command.d.ts.map +1 -1
  107. package/dist/commands/read-tree-command.d.ts +6 -0
  108. package/dist/commands/read-tree-command.d.ts.map +1 -1
  109. package/dist/commands/rebase-command.d.ts +168 -6
  110. package/dist/commands/rebase-command.d.ts.map +1 -1
  111. package/dist/commands/ref-filter/for-each-ref-command.d.ts +121 -0
  112. package/dist/commands/ref-filter/for-each-ref-command.d.ts.map +1 -0
  113. package/dist/commands/ref-filter/index.d.ts +8 -0
  114. package/dist/commands/ref-filter/index.d.ts.map +1 -0
  115. package/dist/commands/ref-filter/ref-atoms.d.ts +88 -0
  116. package/dist/commands/ref-filter/ref-atoms.d.ts.map +1 -0
  117. package/dist/commands/ref-filter/ref-filter.d.ts +178 -0
  118. package/dist/commands/ref-filter/ref-filter.d.ts.map +1 -0
  119. package/dist/commands/ref-filter/ref-format.d.ts +222 -0
  120. package/dist/commands/ref-filter/ref-format.d.ts.map +1 -0
  121. package/dist/commands/ref-filter/ref-listing-command.d.ts +56 -0
  122. package/dist/commands/ref-filter/ref-listing-command.d.ts.map +1 -0
  123. package/dist/commands/ref-filter/ref-quote.d.ts +20 -0
  124. package/dist/commands/ref-filter/ref-quote.d.ts.map +1 -0
  125. package/dist/commands/ref-filter/version-compare.d.ts +20 -0
  126. package/dist/commands/ref-filter/version-compare.d.ts.map +1 -0
  127. package/dist/commands/reflog-command.d.ts +120 -0
  128. package/dist/commands/reflog-command.d.ts.map +1 -1
  129. package/dist/commands/reflog-walk.d.ts +72 -0
  130. package/dist/commands/reflog-walk.d.ts.map +1 -0
  131. package/dist/commands/remote-command.d.ts +138 -29
  132. package/dist/commands/remote-command.d.ts.map +1 -1
  133. package/dist/commands/remote-transport.d.ts +7 -1
  134. package/dist/commands/remote-transport.d.ts.map +1 -1
  135. package/dist/commands/reset-command.d.ts +2 -3
  136. package/dist/commands/reset-command.d.ts.map +1 -1
  137. package/dist/commands/restore-command.d.ts +2 -3
  138. package/dist/commands/restore-command.d.ts.map +1 -1
  139. package/dist/commands/rev-filter/commit-filter.d.ts +95 -0
  140. package/dist/commands/rev-filter/commit-filter.d.ts.map +1 -0
  141. package/dist/commands/rev-filter/commit-grep.d.ts +91 -0
  142. package/dist/commands/rev-filter/commit-grep.d.ts.map +1 -0
  143. package/dist/commands/rev-filter/index.d.ts +3 -0
  144. package/dist/commands/rev-filter/index.d.ts.map +1 -0
  145. package/dist/commands/rev-list-command.d.ts +154 -2
  146. package/dist/commands/rev-list-command.d.ts.map +1 -1
  147. package/dist/commands/revert-command.d.ts +32 -0
  148. package/dist/commands/revert-command.d.ts.map +1 -1
  149. package/dist/commands/revision-range.d.ts +32 -1
  150. package/dist/commands/revision-range.d.ts.map +1 -1
  151. package/dist/commands/revision-resolver.d.ts +15 -0
  152. package/dist/commands/revision-resolver.d.ts.map +1 -1
  153. package/dist/commands/rm-command.d.ts +70 -2
  154. package/dist/commands/rm-command.d.ts.map +1 -1
  155. package/dist/commands/sequencer-pick.d.ts +84 -4
  156. package/dist/commands/sequencer-pick.d.ts.map +1 -1
  157. package/dist/commands/sequencer-support.d.ts +2 -0
  158. package/dist/commands/sequencer-support.d.ts.map +1 -1
  159. package/dist/commands/serve-repository.d.ts +7 -0
  160. package/dist/commands/serve-repository.d.ts.map +1 -1
  161. package/dist/commands/show-command.d.ts +130 -0
  162. package/dist/commands/show-command.d.ts.map +1 -0
  163. package/dist/commands/show-commit.d.ts +11 -0
  164. package/dist/commands/show-commit.d.ts.map +1 -0
  165. package/dist/commands/signature-check.d.ts +164 -0
  166. package/dist/commands/signature-check.d.ts.map +1 -0
  167. package/dist/commands/signing.d.ts +160 -0
  168. package/dist/commands/signing.d.ts.map +1 -0
  169. package/dist/commands/sparse-checkout-command.d.ts.map +1 -1
  170. package/dist/commands/sparse-index-view.d.ts +45 -0
  171. package/dist/commands/sparse-index-view.d.ts.map +1 -0
  172. package/dist/commands/ssh-signer.d.ts +29 -0
  173. package/dist/commands/ssh-signer.d.ts.map +1 -0
  174. package/dist/commands/ssh-verifier.d.ts +42 -0
  175. package/dist/commands/ssh-verifier.d.ts.map +1 -0
  176. package/dist/commands/stash-apply-command.d.ts +5 -0
  177. package/dist/commands/stash-apply-command.d.ts.map +1 -1
  178. package/dist/commands/stash-branch-command.d.ts +34 -0
  179. package/dist/commands/stash-branch-command.d.ts.map +1 -0
  180. package/dist/commands/stash-create-command.d.ts +17 -0
  181. package/dist/commands/stash-create-command.d.ts.map +1 -1
  182. package/dist/commands/stash-info.d.ts +93 -0
  183. package/dist/commands/stash-info.d.ts.map +1 -0
  184. package/dist/commands/stash-list-command.d.ts +16 -0
  185. package/dist/commands/stash-list-command.d.ts.map +1 -1
  186. package/dist/commands/stash-pop-command.d.ts +39 -0
  187. package/dist/commands/stash-pop-command.d.ts.map +1 -0
  188. package/dist/commands/stash-show-command.d.ts +48 -0
  189. package/dist/commands/stash-show-command.d.ts.map +1 -0
  190. package/dist/commands/stash-store-command.d.ts +29 -0
  191. package/dist/commands/stash-store-command.d.ts.map +1 -0
  192. package/dist/commands/stash-working-tree.d.ts.map +1 -1
  193. package/dist/commands/status-command.d.ts.map +1 -1
  194. package/dist/commands/status-header.d.ts +5 -0
  195. package/dist/commands/status-header.d.ts.map +1 -1
  196. package/dist/commands/submodule-add-command.d.ts +89 -0
  197. package/dist/commands/submodule-add-command.d.ts.map +1 -0
  198. package/dist/commands/submodule-auth.d.ts +90 -0
  199. package/dist/commands/submodule-auth.d.ts.map +1 -0
  200. package/dist/commands/submodule-commands.d.ts +375 -0
  201. package/dist/commands/submodule-commands.d.ts.map +1 -0
  202. package/dist/commands/submodule-model.d.ts +288 -0
  203. package/dist/commands/submodule-model.d.ts.map +1 -0
  204. package/dist/commands/submodule-update-command.d.ts +198 -0
  205. package/dist/commands/submodule-update-command.d.ts.map +1 -0
  206. package/dist/commands/switch-command.d.ts.map +1 -1
  207. package/dist/commands/tag-command.d.ts +259 -10
  208. package/dist/commands/tag-command.d.ts.map +1 -1
  209. package/dist/commands/trailers.d.ts +218 -17
  210. package/dist/commands/trailers.d.ts.map +1 -1
  211. package/dist/commands/tree-restore.d.ts +7 -0
  212. package/dist/commands/tree-restore.d.ts.map +1 -1
  213. package/dist/commands/verify-command.d.ts +92 -0
  214. package/dist/commands/verify-command.d.ts.map +1 -0
  215. package/dist/commands/worktree-add-command.d.ts +79 -7
  216. package/dist/commands/worktree-add-command.d.ts.map +1 -1
  217. package/dist/commands/worktree-path-rules.d.ts +66 -0
  218. package/dist/commands/worktree-path-rules.d.ts.map +1 -0
  219. package/dist/commands/worktree-remove-command.d.ts +10 -3
  220. package/dist/commands/worktree-remove-command.d.ts.map +1 -1
  221. package/dist/commands/worktree-repair-command.d.ts +15 -2
  222. package/dist/commands/worktree-repair-command.d.ts.map +1 -1
  223. package/dist/commands/worktree-support.d.ts +5 -0
  224. package/dist/commands/worktree-support.d.ts.map +1 -1
  225. package/dist/{commands-DIJ91NnD.js → commands-ISM52N0n.js} +70736 -54492
  226. package/dist/errors/checkout-errors.d.ts +19 -0
  227. package/dist/errors/checkout-errors.d.ts.map +1 -1
  228. package/dist/errors/command-errors.d.ts.map +1 -1
  229. package/dist/errors/index.js +57 -3
  230. package/dist/errors/ref-errors.d.ts +19 -0
  231. package/dist/errors/ref-errors.d.ts.map +1 -1
  232. package/dist/errors/worktree-errors.d.ts +6 -1
  233. package/dist/errors/worktree-errors.d.ts.map +1 -1
  234. package/dist/{git-command-BGiOJ8Zk.js → fsck-DLRpbwTj.js} +6230 -71
  235. package/dist/git-command.d.ts +27 -1
  236. package/dist/git-command.d.ts.map +1 -1
  237. package/dist/git.d.ts +267 -1
  238. package/dist/git.d.ts.map +1 -1
  239. package/dist/index.js +5 -5
  240. package/dist/remote-config/index.d.ts +1 -1
  241. package/dist/remote-config/index.d.ts.map +1 -1
  242. package/dist/remote-config/remote-config-store.d.ts +55 -0
  243. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  244. package/dist/results/combined-diff.d.ts +3 -0
  245. package/dist/results/combined-diff.d.ts.map +1 -1
  246. package/dist/results/diff-check.d.ts +38 -0
  247. package/dist/results/diff-check.d.ts.map +1 -0
  248. package/dist/results/diff-color.d.ts +135 -0
  249. package/dist/results/diff-color.d.ts.map +1 -0
  250. package/dist/results/diff-output.d.ts +85 -1
  251. package/dist/results/diff-output.d.ts.map +1 -1
  252. package/dist/results/diff-pickaxe.d.ts +67 -0
  253. package/dist/results/diff-pickaxe.d.ts.map +1 -0
  254. package/dist/results/diff-words.d.ts +55 -0
  255. package/dist/results/diff-words.d.ts.map +1 -0
  256. package/dist/results/index.d.ts +4 -0
  257. package/dist/results/index.d.ts.map +1 -1
  258. package/dist/results/index.js +2 -2
  259. package/dist/results/push-result.d.ts +7 -0
  260. package/dist/results/push-result.d.ts.map +1 -1
  261. package/dist/results/rebase-result.d.ts +2 -0
  262. package/dist/results/rebase-result.d.ts.map +1 -1
  263. package/dist/results/stash-result.d.ts +6 -0
  264. package/dist/results/stash-result.d.ts.map +1 -1
  265. package/dist/{results-WVlBhVb1.js → results-TB2IJ64h.js} +2168 -403
  266. package/dist/whitespace/index.d.ts +2 -0
  267. package/dist/whitespace/index.d.ts.map +1 -0
  268. package/dist/whitespace/whitespace-rule.d.ts +94 -0
  269. package/dist/whitespace/whitespace-rule.d.ts.map +1 -0
  270. package/package.json +12 -12
  271. package/src/commands/add-command.ts +178 -17
  272. package/src/commands/am/am-command.ts +262 -42
  273. package/src/commands/am/index.ts +1 -0
  274. package/src/commands/am/mailsplit.ts +326 -0
  275. package/src/commands/apply/apply-command.ts +6 -6
  276. package/src/commands/apply/apply-engine.ts +1 -1
  277. package/src/commands/apply/apply-parse.ts +7 -8
  278. package/src/commands/bisect/bisect-command.ts +18 -5
  279. package/src/commands/branch-command.ts +528 -53
  280. package/src/commands/branch-info-command.ts +150 -0
  281. package/src/commands/branch-tracking.ts +335 -60
  282. package/src/commands/branch-upstream-command.ts +218 -0
  283. package/src/commands/checkout-command.ts +69 -43
  284. package/src/commands/cherry-pick-command.ts +143 -0
  285. package/src/commands/clean-command.ts +34 -3
  286. package/src/commands/clone-command.ts +2 -1
  287. package/src/commands/commit-command.ts +782 -64
  288. package/src/commands/commit-reuse.ts +94 -0
  289. package/src/commands/config-command.ts +1256 -0
  290. package/src/commands/config-value-types.ts +358 -0
  291. package/src/commands/core-config.ts +40 -0
  292. package/src/commands/diff-color-config.ts +274 -0
  293. package/src/commands/diff-command.ts +1009 -49
  294. package/src/commands/fetch-command.ts +142 -8
  295. package/src/commands/fmt-merge-msg-command.ts +715 -0
  296. package/src/commands/fork-point.ts +63 -0
  297. package/src/commands/format-patch-command.ts +314 -46
  298. package/src/commands/fsck/fetch-fsck.ts +172 -18
  299. package/src/commands/gc-command.ts +31 -1
  300. package/src/commands/git-describe.ts +31 -11
  301. package/src/commands/git-hooks.ts +81 -0
  302. package/src/commands/grep/grep-command.ts +1077 -0
  303. package/src/commands/grep/grep-engine.ts +1024 -0
  304. package/src/commands/grep/index.ts +2 -0
  305. package/src/commands/index-adder.ts +46 -7
  306. package/src/commands/index.ts +26 -0
  307. package/src/commands/interpret-trailers-command.ts +256 -0
  308. package/src/commands/local-transport.ts +109 -10
  309. package/src/commands/log-command.ts +1246 -210
  310. package/src/commands/log-graph.ts +123 -26
  311. package/src/commands/log-output.ts +280 -21
  312. package/src/commands/ls-remote-command.ts +5 -0
  313. package/src/commands/mailmap.ts +119 -0
  314. package/src/commands/merge-base-command.ts +170 -0
  315. package/src/commands/merge-bases.ts +63 -8
  316. package/src/commands/merge-command.ts +330 -82
  317. package/src/commands/merge-support.ts +14 -1
  318. package/src/commands/mv-command.ts +6 -3
  319. package/src/commands/name-rev.ts +171 -0
  320. package/src/commands/notes-command.ts +11 -1
  321. package/src/commands/pathspec-file.ts +72 -0
  322. package/src/commands/pathspec.ts +14 -2
  323. package/src/commands/pretty-format.ts +103 -19
  324. package/src/commands/promisor-remote.ts +10 -1
  325. package/src/commands/pull-command.ts +26 -7
  326. package/src/commands/push-command.ts +682 -65
  327. package/src/commands/read-tree-command.ts +28 -1
  328. package/src/commands/rebase-command.ts +933 -129
  329. package/src/commands/ref-filter/for-each-ref-command.ts +298 -0
  330. package/src/commands/ref-filter/index.ts +7 -0
  331. package/src/commands/ref-filter/ref-atoms.ts +581 -0
  332. package/src/commands/ref-filter/ref-filter.ts +651 -0
  333. package/src/commands/ref-filter/ref-format.ts +1070 -0
  334. package/src/commands/ref-filter/ref-listing-command.ts +421 -0
  335. package/src/commands/ref-filter/ref-quote.ts +69 -0
  336. package/src/commands/ref-filter/version-compare.ts +177 -0
  337. package/src/commands/reflog-command.ts +492 -2
  338. package/src/commands/reflog-walk.ts +234 -0
  339. package/src/commands/remote-command.ts +449 -69
  340. package/src/commands/remote-transport.ts +9 -1
  341. package/src/commands/reset-command.ts +36 -31
  342. package/src/commands/restore-command.ts +12 -41
  343. package/src/commands/rev-filter/commit-filter.ts +159 -0
  344. package/src/commands/rev-filter/commit-grep.ts +444 -0
  345. package/src/commands/rev-filter/index.ts +2 -0
  346. package/src/commands/rev-list-command.ts +402 -15
  347. package/src/commands/revert-command.ts +82 -0
  348. package/src/commands/revision-range.ts +204 -4
  349. package/src/commands/revision-resolver.ts +23 -8
  350. package/src/commands/rm-command.ts +282 -21
  351. package/src/commands/sequencer-pick.ts +498 -85
  352. package/src/commands/sequencer-support.ts +20 -2
  353. package/src/commands/serve-repository.ts +18 -3
  354. package/src/commands/show-command.ts +490 -0
  355. package/src/commands/show-commit.ts +24 -0
  356. package/src/commands/signature-check.ts +657 -0
  357. package/src/commands/signing.ts +385 -0
  358. package/src/commands/sparse-checkout-command.ts +2 -9
  359. package/src/commands/sparse-checkout-support.ts +2 -2
  360. package/src/commands/sparse-index-view.ts +135 -0
  361. package/src/commands/ssh-signer.ts +199 -0
  362. package/src/commands/ssh-verifier.ts +523 -0
  363. package/src/commands/stash-apply-command.ts +16 -4
  364. package/src/commands/stash-branch-command.ts +88 -0
  365. package/src/commands/stash-create-command.ts +28 -0
  366. package/src/commands/stash-info.ts +243 -0
  367. package/src/commands/stash-list-command.ts +33 -0
  368. package/src/commands/stash-pop-command.ts +83 -0
  369. package/src/commands/stash-show-command.ts +141 -0
  370. package/src/commands/stash-store-command.ts +82 -0
  371. package/src/commands/stash-working-tree.ts +2 -2
  372. package/src/commands/status-command.ts +5 -2
  373. package/src/commands/status-header.ts +41 -7
  374. package/src/commands/submodule-add-command.ts +509 -0
  375. package/src/commands/submodule-auth.ts +276 -0
  376. package/src/commands/submodule-commands.ts +1517 -0
  377. package/src/commands/submodule-model.ts +930 -0
  378. package/src/commands/submodule-update-command.ts +1092 -0
  379. package/src/commands/switch-command.ts +3 -1
  380. package/src/commands/tag-command.ts +876 -64
  381. package/src/commands/trailers.ts +837 -100
  382. package/src/commands/tree-restore.ts +38 -10
  383. package/src/commands/verify-command.ts +235 -0
  384. package/src/commands/worktree-add-command.ts +268 -24
  385. package/src/commands/worktree-path-rules.ts +183 -0
  386. package/src/commands/worktree-remove-command.ts +39 -5
  387. package/src/commands/worktree-repair-command.ts +52 -5
  388. package/src/commands/worktree-support.ts +22 -1
  389. package/src/errors/checkout-errors.ts +29 -0
  390. package/src/errors/command-errors.ts +2 -1
  391. package/src/errors/ref-errors.ts +23 -0
  392. package/src/errors/worktree-errors.ts +8 -1
  393. package/src/git-command.ts +66 -1
  394. package/src/git.ts +404 -1
  395. package/src/remote-config/index.ts +2 -0
  396. package/src/remote-config/remote-config-store.ts +101 -0
  397. package/src/results/combined-diff.ts +38 -18
  398. package/src/results/diff-check.ts +185 -0
  399. package/src/results/diff-color.ts +868 -0
  400. package/src/results/diff-output.ts +195 -30
  401. package/src/results/diff-pickaxe.ts +304 -0
  402. package/src/results/diff-words.ts +395 -0
  403. package/src/results/index.ts +4 -0
  404. package/src/results/push-result.ts +7 -0
  405. package/src/results/rebase-result.ts +3 -0
  406. package/src/results/stash-result.ts +7 -0
  407. package/src/whitespace/index.ts +1 -0
  408. package/src/{commands/apply/whitespace.ts → whitespace/whitespace-rule.ts} +173 -23
  409. package/dist/commands/apply/whitespace.d.ts +0 -58
  410. package/dist/commands/apply/whitespace.d.ts.map +0 -1
@@ -1,14 +1,21 @@
1
1
  /**
2
- * The trailers of a commit message as git's trailer.c finds them (`%(trailers)` of a pretty
3
- * format, `git interpret-trailers --parse`): the last paragraph when every line of it is a
4
- * trailer, or when it has a git-generated one (`Signed-off-by: `, `(cherry picked from
5
- * commit `) and at least a quarter of its lines are trailers, continuation lines folded in.
2
+ * git's trailer engine (trailer.c): finding the trailer block of a message, formatting it
3
+ * (`%(trailers)` of a pretty format), adding and editing trailers (`git interpret-trailers`,
4
+ * the `--trailer` of commit, tag, merge, rebase and cherry-pick) with the `trailer.*`
5
+ * configuration, and sequencer.c's sign-off (`append_signoff`).
6
+ *
7
+ * The block is the last paragraph when every line of it is a trailer, or when it has a
8
+ * git-generated one (`Signed-off-by: `, `(cherry picked from commit `) or a configured one
9
+ * and at least a quarter of its lines are trailers, continuation lines folded in.
6
10
  *
7
11
  * @example
8
12
  * ```typescript
9
13
  * const message = "Fix\n\nBody.\n\nSigned-off-by: A <a@b>\nAcked-by: C <c@d>\n";
10
14
  * formatTrailersOf(message, { onlyTrailers: true, valueOnly: true, separator: ", " });
11
15
  * // "A <a@b>, C <c@d>"
16
+ *
17
+ * const { output } = await interpretTrailers("Fix\n", { trailers: ["Reviewed-by: R <r@x>"] });
18
+ * // "Fix\n\nReviewed-by: R <r@x>\n"
12
19
  * ```
13
20
  *
14
21
  * @module
@@ -16,20 +23,72 @@
16
23
 
17
24
  import type { WorkingCopy } from "@statewalker/vcs-working-tree";
18
25
 
26
+ import { GitApiError } from "../errors/index.js";
19
27
  import { RemoteConfigStore } from "../remote-config/index.js";
20
28
  import { commentPrefixOf } from "./message-cleanup.js";
21
29
 
22
- /** The configuration trailers are read with (`trailer.*`, `core.commentChar`). */
30
+ /** Where a new trailer goes (`--where`, `trailer.where`, `trailer.<key>.where`). */
31
+ export type TrailerWhere = "end" | "after" | "before" | "start";
32
+ /** What to do when a trailer of the key exists (`--if-exists`, `trailer.ifexists`). */
33
+ export type TrailerIfExists =
34
+ | "addIfDifferentNeighbor"
35
+ | "addIfDifferent"
36
+ | "add"
37
+ | "replace"
38
+ | "doNothing";
39
+ /** What to do when no trailer of the key exists (`--if-missing`, `trailer.ifmissing`). */
40
+ export type TrailerIfMissing = "add" | "doNothing";
41
+
42
+ /** One `trailer.<name>.*` section (git's `conf_info`). */
43
+ export interface TrailerItemConfig {
44
+ /** `<name>`, the key alias (`--trailer <name>=<value>`) */
45
+ name: string;
46
+ /** `trailer.<name>.key`: the key written for the trailer */
47
+ key?: string;
48
+ /**
49
+ * `trailer.<name>.command` (deprecated): a shell command, its first `$ARG` replaced by the
50
+ * value, whose output is the value; also adds the trailer to every message
51
+ */
52
+ command?: string;
53
+ /** `trailer.<name>.cmd`: a shell command given the value as `$1`, whose output is the value */
54
+ cmd?: string;
55
+ /** `trailer.<name>.where` (default `trailer.where`) */
56
+ where?: TrailerWhere;
57
+ /** `trailer.<name>.ifexists` (default `trailer.ifexists`) */
58
+ ifExists?: TrailerIfExists;
59
+ /** `trailer.<name>.ifmissing` (default `trailer.ifmissing`) */
60
+ ifMissing?: TrailerIfMissing;
61
+ }
62
+
63
+ /** The configuration trailers are read and written with (`trailer.*`, `core.commentChar`). */
23
64
  export interface TrailerConfig {
24
65
  /** `trailer.separators`: the characters that end a trailer's key (default `:`) */
25
66
  separators?: string;
26
67
  /** `core.commentChar` / `core.commentString` (default `#`) */
27
68
  commentString?: string;
69
+ /** `trailer.where` (default `end`) */
70
+ where?: TrailerWhere;
71
+ /** `trailer.ifexists` (default `addIfDifferentNeighbor`) */
72
+ ifExists?: TrailerIfExists;
73
+ /** `trailer.ifmissing` (default `add`) */
74
+ ifMissing?: TrailerIfMissing;
28
75
  /**
29
- * The `trailer.<name>.*` sections in the configuration's order: a trailer whose key starts
30
- * a name (or its `key`) is recognized, and shown with the `key` given
76
+ * The `trailer.<name>.*` sections in the configuration's order (names without case): a
77
+ * trailer whose key starts a name (or its `key`) is recognized, and shown with the `key`
31
78
  */
32
- items?: readonly { name: string; key?: string }[];
79
+ items?: readonly TrailerItemConfig[];
80
+ /**
81
+ * git's warnings reading the configuration (`warning: unknown value 'x' for key
82
+ * 'trailer.where'`, `warning: more than one trailer.a.key`), each a line
83
+ */
84
+ warnings?: readonly string[];
85
+ /**
86
+ * The git version whose behaviour to follow where versions differ (default `2.56`): git
87
+ * 2.43 takes `https://x` for a trailer (2.45+ refuses a `://` right after the key), its
88
+ * `--trim-empty` also drops the non-trailer lines of a block that are empty, and its
89
+ * `interpret-trailers` reads an input without a final line end as it is (2.44+ completes it)
90
+ */
91
+ compatibility?: "2.43" | "2.56";
33
92
  }
34
93
 
35
94
  /** What git's process_trailer_options carries for formatting trailers. */
@@ -50,32 +109,261 @@ export interface TrailerFormatOptions {
50
109
  keys?: readonly string[];
51
110
  }
52
111
 
53
- /** The trailer configuration of a working copy (`trailer.separators`, `trailer.<name>.key`). */
112
+ /** A trailer to add (`--trailer <key>[(=|:)<value>]`) and how, over the configuration. */
113
+ export interface NewTrailer {
114
+ /** `<key>`, `<key>:<value>`, `<key>=<value>` (any of `trailer.separators` or `=`) */
115
+ text: string;
116
+ /** `--where` given before it */
117
+ where?: TrailerWhere;
118
+ /** `--if-exists` given before it */
119
+ ifExists?: TrailerIfExists;
120
+ /** `--if-missing` given before it */
121
+ ifMissing?: TrailerIfMissing;
122
+ }
123
+
124
+ /** A shell command of `trailer.<name>.cmd` / `.command` for the host to run. */
125
+ export interface TrailerCommand {
126
+ /** The command line (`command` with its `$ARG` replaced, or `cmd`) */
127
+ command: string;
128
+ /** The arguments after it (`cmd`'s value; none for `command`) */
129
+ args: string[];
130
+ }
131
+
132
+ /** What a trailer command did. */
133
+ export interface TrailerCommandOutcome {
134
+ exitCode: number;
135
+ stdout: string;
136
+ /** What it wrote on stderr (git's own stderr shows it) */
137
+ stderr?: string;
138
+ }
139
+
140
+ /**
141
+ * Runs trailer commands as git's `apply_command` does: through the shell (`sh -c`), stdin
142
+ * closed, without the variables naming the repository (`local_repo_env`). Undefined when the
143
+ * command could not be started (git reports it as failed).
144
+ */
145
+ export type TrailerCommandRunner = (
146
+ command: TrailerCommand,
147
+ ) => Promise<TrailerCommandOutcome | undefined>;
148
+
149
+ /** How {@link interpretTrailers} edits a message (`git interpret-trailers` options). */
150
+ export interface InterpretTrailersOptions {
151
+ /** The trailers to add (`--trailer`), in order; a string takes the options below */
152
+ trailers?: readonly (string | NewTrailer)[];
153
+ /** `--where` for the trailers given as strings */
154
+ where?: TrailerWhere;
155
+ /** `--if-exists` for the trailers given as strings */
156
+ ifExists?: TrailerIfExists;
157
+ /** `--if-missing` for the trailers given as strings */
158
+ ifMissing?: TrailerIfMissing;
159
+ /** `--trim-empty`: trailers with an empty value are left out */
160
+ trimEmpty?: boolean;
161
+ /** `--only-trailers`: only the trailers are output */
162
+ onlyTrailers?: boolean;
163
+ /** `--only-input`: no `trailer.<name>.command` trailers (and no `trailers` allowed) */
164
+ onlyInput?: boolean;
165
+ /** `--unfold`: continuation lines joined into one */
166
+ unfold?: boolean;
167
+ /** `--parse`: `--only-trailers --only-input --unfold` */
168
+ parse?: boolean;
169
+ /** `--no-divider`: a `---` line does not end the message */
170
+ noDivider?: boolean;
171
+ /**
172
+ * Runs `trailer.<name>.cmd` / `.command` (see {@link TrailerCommandRunner}). Absent, as in a
173
+ * host that runs no commands: commands are not run, a trailer keeps the value given and no
174
+ * `.command` trailer is added on its own.
175
+ */
176
+ runCommand?: TrailerCommandRunner;
177
+ }
178
+
179
+ /** What {@link interpretTrailers} gave. */
180
+ export interface InterpretTrailersResult {
181
+ /** The message with its trailers (git's stdout) */
182
+ output: string;
183
+ /** git's stderr: `error:` lines and the commands' stderr, in order */
184
+ errors: string;
185
+ }
186
+
187
+ /** One line of a trailer block as git's trailer iterator gives it. */
188
+ export interface TrailerLine {
189
+ /** The line as written, continuation lines included */
190
+ raw: string;
191
+ /** The key (the configured `key` where one matches); the whole line for a non-trailer */
192
+ key: string;
193
+ /** The value, unfolded; empty for a non-trailer */
194
+ value: string;
195
+ /** Whether it is a trailer (`<key><separator><value>`) */
196
+ isTrailer: boolean;
197
+ }
198
+
199
+ /** git's `trailer_set_where`: the placement named (without case); undefined when unknown. */
200
+ export function parseTrailerWhere(value: string): TrailerWhere | undefined {
201
+ const lower = lowerAscii(value);
202
+ for (const where of ["after", "before", "end", "start"] as const)
203
+ if (lower === where) return where;
204
+ return undefined;
205
+ }
206
+
207
+ /** git's `trailer_set_if_exists`: the action named (without case); undefined when unknown. */
208
+ export function parseTrailerIfExists(value: string): TrailerIfExists | undefined {
209
+ const lower = lowerAscii(value);
210
+ for (const action of [
211
+ "addIfDifferent",
212
+ "addIfDifferentNeighbor",
213
+ "add",
214
+ "replace",
215
+ "doNothing",
216
+ ] as const) {
217
+ if (lower === lowerAscii(action)) return action;
218
+ }
219
+ return undefined;
220
+ }
221
+
222
+ /** git's `trailer_set_if_missing`: the action named (without case); undefined when unknown. */
223
+ export function parseTrailerIfMissing(value: string): TrailerIfMissing | undefined {
224
+ const lower = lowerAscii(value);
225
+ if (lower === "donothing") return "doNothing";
226
+ if (lower === "add") return "add";
227
+ return undefined;
228
+ }
229
+
230
+ /** The trailer configuration of a working copy (`trailer.*`, the comment prefix). */
54
231
  export function trailerConfigOf(workingCopy: WorkingCopy): TrailerConfig {
55
232
  const config = RemoteConfigStore.from(workingCopy);
56
- const items: { name: string; key?: string }[] = [];
57
- const variables = ["key", "command", "cmd", "where", "ifexists", "ifmissing"];
58
- for (const name of config.subsections("trailer")) {
59
- if (!variables.some((v) => config.values(`trailer.${name}.${v}`).length > 0)) continue;
60
- // get_conf_item: the names are one item without case
61
- const known = items.find((i) => lowerAscii(i.name) === lowerAscii(name));
62
- const key = config.value(`trailer.${name}.key`);
63
- if (known) {
64
- if (key !== undefined) known.key = key;
65
- } else items.push(key !== undefined ? { name, key } : { name });
233
+ let entries = config.entriesWithPrefix("trailer.");
234
+ if (!entries) {
235
+ // a config that cannot list its variables in order: section by section
236
+ entries = [];
237
+ for (const variable of ["where", "ifexists", "ifmissing", "separators"]) {
238
+ for (const value of config.values(`trailer.${variable}`)) {
239
+ entries.push({ key: `trailer.${variable}`, value });
240
+ }
241
+ }
242
+ for (const name of config.subsections("trailer")) {
243
+ for (const variable of TRAILER_VARIABLES) {
244
+ for (const value of config.values(`trailer.${name}.${variable}`)) {
245
+ entries.push({ key: `trailer.${name}.${variable}`, value });
246
+ }
247
+ }
248
+ }
249
+ }
250
+ return trailerConfigFromEntries(entries, commentPrefixOf(workingCopy));
251
+ }
252
+
253
+ const TRAILER_VARIABLES = ["key", "command", "cmd", "where", "ifexists", "ifmissing"] as const;
254
+
255
+ /**
256
+ * The trailer configuration from `trailer.*` variables in the configuration's order (keys
257
+ * canonical: section and variable in lower case), as git's `trailer_config_init` reads them:
258
+ * `trailer.where`/`ifexists`/`ifmissing`/`separators` first, then each `trailer.<name>.*`.
259
+ * A `key`, `command`, `cmd` or `separators` without a value is git's fatal config error.
260
+ */
261
+ export function trailerConfigFromEntries(
262
+ entries: readonly { key: string; value: string | undefined }[],
263
+ commentString?: string,
264
+ ): TrailerConfig {
265
+ const warnings: string[] = [];
266
+ const unknown = (value: string | undefined, key: string) =>
267
+ warnings.push(`warning: unknown value '${value ?? "(null)"}' for key '${key}'`);
268
+ const missing = (key: string): never => {
269
+ throw new GitApiError(
270
+ `error: missing value for '${key}'\nfatal: bad config variable '${key}'`,
271
+ {
272
+ exitCode: 128,
273
+ },
274
+ );
275
+ };
276
+ const out: {
277
+ separators?: string;
278
+ where?: TrailerWhere;
279
+ ifExists?: TrailerIfExists;
280
+ ifMissing?: TrailerIfMissing;
281
+ } = {};
282
+ // git_trailer_default_config
283
+ for (const { key, value } of entries) {
284
+ const rest = key.slice("trailer.".length);
285
+ if (!key.startsWith("trailer.") || rest.includes(".")) continue;
286
+ if (rest === "where") {
287
+ const where = value === undefined ? undefined : parseTrailerWhere(value);
288
+ if (where) out.where = where;
289
+ else if (value !== undefined) unknown(value, key);
290
+ } else if (rest === "ifexists") {
291
+ const action = value === undefined ? undefined : parseTrailerIfExists(value);
292
+ if (action) out.ifExists = action;
293
+ else if (value !== undefined) unknown(value, key);
294
+ } else if (rest === "ifmissing") {
295
+ const action = value === undefined ? undefined : parseTrailerIfMissing(value);
296
+ if (action) out.ifMissing = action;
297
+ else if (value !== undefined) unknown(value, key);
298
+ } else if (rest === "separators") {
299
+ if (value === undefined) missing(key);
300
+ else out.separators = value;
301
+ }
302
+ }
303
+ // git_trailer_config: one item per name (without case), in the order first met
304
+ const items: TrailerItemConfig[] = [];
305
+ for (const { key, value } of entries) {
306
+ if (!key.startsWith("trailer.")) continue;
307
+ const rest = key.slice("trailer.".length);
308
+ const dot = rest.lastIndexOf(".");
309
+ if (dot < 0) continue;
310
+ const variable = rest.slice(dot + 1);
311
+ if (!(TRAILER_VARIABLES as readonly string[]).includes(variable)) continue;
312
+ const name = rest.slice(0, dot);
313
+ let item = items.find((i) => lowerAscii(i.name) === lowerAscii(name));
314
+ if (!item) {
315
+ item = { name };
316
+ items.push(item);
317
+ }
318
+ switch (variable) {
319
+ case "key":
320
+ case "command":
321
+ case "cmd": {
322
+ const field = variable;
323
+ if (item[field] !== undefined) warnings.push(`warning: more than one ${key}`);
324
+ if (value === undefined) missing(key);
325
+ else item[field] = value;
326
+ break;
327
+ }
328
+ case "where": {
329
+ const where = value === undefined ? undefined : parseTrailerWhere(value);
330
+ if (where) item.where = where;
331
+ else unknown(value, key);
332
+ break;
333
+ }
334
+ case "ifexists": {
335
+ const action = value === undefined ? undefined : parseTrailerIfExists(value);
336
+ if (action) item.ifExists = action;
337
+ else unknown(value, key);
338
+ break;
339
+ }
340
+ case "ifmissing": {
341
+ const action = value === undefined ? undefined : parseTrailerIfMissing(value);
342
+ if (action) item.ifMissing = action;
343
+ else unknown(value, key);
344
+ break;
345
+ }
346
+ }
66
347
  }
67
- const separators = config.value("trailer.separators");
68
348
  return {
69
- ...(separators ? { separators } : {}),
70
- commentString: commentPrefixOf(workingCopy),
349
+ ...out,
350
+ ...(commentString !== undefined ? { commentString } : {}),
71
351
  items,
352
+ ...(warnings.length > 0 ? { warnings } : {}),
72
353
  };
73
354
  }
74
355
 
75
356
  const lowerAscii = (s: string) => s.replace(/[A-Z]/g, (c) => c.toLowerCase());
76
357
  /** strncasecmp(a, b, n) === 0 */
77
- const sameStart = (a: string, b: string, n: number) =>
78
- a.length >= n && b.length >= n && lowerAscii(a.slice(0, n)) === lowerAscii(b.slice(0, n));
358
+ function sameStart(a: string, b: string, n: number): boolean {
359
+ for (let i = 0; i < n; i++) {
360
+ const x = a[i];
361
+ const y = b[i];
362
+ if (x === undefined || y === undefined) return x === y;
363
+ if (x !== y && lowerAscii(x) !== lowerAscii(y)) return false;
364
+ }
365
+ return true;
366
+ }
79
367
  /** git's isspace (sane_ctype): space, tab, LF, CR */
80
368
  const isSpace = (c: string | undefined) => c === " " || c === "\t" || c === "\n" || c === "\r";
81
369
  const isAlnum = (c: string | undefined) => c !== undefined && /^[A-Za-z0-9]$/.test(c);
@@ -102,16 +390,40 @@ function lastLine(buf: string, len: number): number {
102
390
  return 0;
103
391
  }
104
392
 
393
+ /** The configuration with git's defaults. */
394
+ interface Resolved {
395
+ separators: string;
396
+ commentString: string;
397
+ where: TrailerWhere;
398
+ ifExists: TrailerIfExists;
399
+ ifMissing: TrailerIfMissing;
400
+ items: readonly TrailerItemConfig[];
401
+ legacy: boolean;
402
+ }
403
+
404
+ function resolve(config: TrailerConfig): Resolved {
405
+ return {
406
+ separators: config.separators || ":",
407
+ commentString: config.commentString || "#",
408
+ where: config.where ?? "end",
409
+ ifExists: config.ifExists ?? "addIfDifferentNeighbor",
410
+ ifMissing: config.ifMissing ?? "add",
411
+ items: config.items ?? [],
412
+ legacy: config.compatibility === "2.43",
413
+ };
414
+ }
415
+
105
416
  /**
106
417
  * git's find_separator: where the separator of `<token><optional blanks><separator>` is in
107
- * `line`, -1 when it is not a trailer line (a `://` right after the token is a URL).
418
+ * `line` from `at`, -1 when it is not a trailer line (a `://` right after the token is a URL,
419
+ * git 2.45+). 0 when the line starts with a separator.
108
420
  */
109
- function findSeparator(line: string, at: number, separators: string): number {
421
+ function findSeparator(line: string, at: number, separators: string, legacy: boolean): number {
110
422
  let whitespace = false;
111
- for (let c = at; c < line.length && line[c] !== "\n"; c++) {
423
+ for (let c = at; c < line.length; c++) {
112
424
  const ch = line[c] as string;
113
425
  if (separators.includes(ch)) {
114
- if (!whitespace && line.startsWith("://", c)) return -1;
426
+ if (!legacy && !whitespace && line.startsWith("://", c)) return -1;
115
427
  return c - at;
116
428
  }
117
429
  if (!whitespace && (isAlnum(ch) || ch === "-")) continue;
@@ -131,12 +443,19 @@ function tokenLength(token: string): number {
131
443
  return len;
132
444
  }
133
445
 
446
+ /** git's token_matches_item: `tok`'s first `len` characters start the item's name or key. */
447
+ function tokenMatches(tok: string, item: TrailerItemConfig, len: number): boolean {
448
+ if (sameStart(tok, item.name, len)) return true;
449
+ return item.key !== undefined && sameStart(tok, item.key, len);
450
+ }
451
+
134
452
  const GIT_GENERATED_PREFIXES = ["Signed-off-by: ", "(cherry picked from commit "];
135
453
  const CUT_LINE = "------------------------ >8 ------------------------";
136
454
 
137
455
  /** commit.c's ignored_log_message_bytes: the trailing comments, blank lines, scissors. */
138
456
  function ignoredBytes(buf: string, len: number, comment: string): number {
139
- // wt_status_locate_end
457
+ // wt_status_locate_end (strstr over the whole input; wt-status.c's cut_line ends with its
458
+ // line end, so a cut line with text after it is no cut line)
140
459
  let cutoff = len;
141
460
  const pattern = `\n${comment} ${CUT_LINE}\n`;
142
461
  if (buf.startsWith(pattern.slice(1))) cutoff = 0;
@@ -166,14 +485,32 @@ function ignoredBytes(buf: string, len: number, comment: string): number {
166
485
  return boc ? len - boc : len - cutoff;
167
486
  }
168
487
 
169
- /** One line of the trailer block: a trailer (`token`), else another line. */
170
- interface TrailerItem {
171
- token?: string;
172
- value: string;
488
+ /**
489
+ * commit.c's ignored_log_message_bytes: how many characters at the end of `message` are not
490
+ * part of the log message (trailing comments and blank lines, an old `Conflicts:` block, the
491
+ * scissors line and what follows it): the `ignoreFooter` of {@link appendSignoff} for a
492
+ * commit message being edited.
493
+ */
494
+ export function ignoredLogMessageBytes(message: string, commentString = "#"): number {
495
+ return ignoredBytes(message, message.length, commentString || "#");
496
+ }
497
+
498
+ /** trailer.c's find_end_of_log_message: before a `---` line (unless `noDivider`) and the ignored tail. */
499
+ function endOfLogMessage(input: string, noDivider: boolean, comment: string): number {
500
+ let end = input.length;
501
+ if (!noDivider) {
502
+ for (let s = 0; s < input.length; s = nextLine(input, s)) {
503
+ if (input.startsWith("---", s) && isSpace(input[s + 3])) {
504
+ end = s;
505
+ break;
506
+ }
507
+ }
508
+ }
509
+ return end - ignoredBytes(input, end, comment);
173
510
  }
174
511
 
175
512
  /** trailer.c's find_trailer_block_start: where the block starts, `len` when there is none. */
176
- function trailerBlockStart(buf: string, len: number, config: Required<TrailerConfig>): number {
513
+ function trailerBlockStart(buf: string, len: number, config: Resolved): number {
177
514
  const comment = config.commentString;
178
515
  let s = 0;
179
516
  for (; s < len; s = nextLine(buf, s)) {
@@ -206,7 +543,7 @@ function trailerBlockStart(buf: string, len: number, config: Required<TrailerCon
206
543
  recognizedPrefix = true;
207
544
  continue;
208
545
  }
209
- const separatorPos = findSeparator(buf, l, config.separators);
546
+ const separatorPos = findSeparator(buf, l, config.separators, config.legacy);
210
547
  if (separatorPos >= 1 && !isSpace(buf[l])) {
211
548
  trailerLines++;
212
549
  possibleContinuationLines = 0;
@@ -225,12 +562,6 @@ function trailerBlockStart(buf: string, len: number, config: Required<TrailerCon
225
562
  return len;
226
563
  }
227
564
 
228
- /** git's token_matches_item: `tok`'s first `len` characters start the item's name or key. */
229
- function tokenMatches(tok: string, item: { name: string; key?: string }, len: number): boolean {
230
- if (sameStart(tok, item.name, len)) return true;
231
- return item.key !== undefined && sameStart(tok, item.key, len);
232
- }
233
-
234
565
  /** git's unfold_value: each line end and the blanks after it one space, then trimmed. */
235
566
  function unfoldValue(value: string): string {
236
567
  let out = "";
@@ -245,15 +576,19 @@ function unfoldValue(value: string): string {
245
576
  return trim(out);
246
577
  }
247
578
 
248
- /**
249
- * trailer.c's trailer_block_get (no `---` divider): where the block is and its lines, a
250
- * continuation (a line starting with a blank) joined to the trailer before it.
251
- */
252
- function trailerBlock(
253
- message: string,
254
- config: Required<TrailerConfig>,
255
- ): { start: number; end: number; lines: string[] } {
256
- const end = message.length - ignoredBytes(message, message.length, config.commentString);
579
+ /** A trailer block (trailer.c's `trailer_block`). */
580
+ interface Block {
581
+ start: number;
582
+ end: number;
583
+ /** Its lines, a continuation (a line starting with a blank) joined to the trailer before it */
584
+ lines: string[];
585
+ /** A blank line comes right before `start` */
586
+ blankLineBefore: boolean;
587
+ }
588
+
589
+ /** trailer.c's trailer_block_get. */
590
+ function trailerBlock(message: string, config: Resolved, noDivider: boolean): Block {
591
+ const end = endOfLogMessage(message, noDivider, config.commentString);
257
592
  const start = trailerBlockStart(message, end, config);
258
593
  const lines: string[] = [];
259
594
  let lastIsTrailer = false;
@@ -266,25 +601,442 @@ function trailerBlock(
266
601
  continue;
267
602
  }
268
603
  lines.push(line);
269
- lastIsTrailer = findSeparator(line, 0, config.separators) >= 1;
604
+ lastIsTrailer = findSeparator(line, 0, config.separators, config.legacy) >= 1;
270
605
  }
271
- return { start, end, lines };
606
+ // ends_with_blank_line(message, start)
607
+ const ll = lastLine(message, start);
608
+ return { start, end, lines, blankLineBefore: ll >= 0 && isBlankLine(message, ll) };
272
609
  }
273
610
 
274
- /** The configuration with git's defaults. */
275
- function withDefaults(trailerConfig: TrailerConfig): Required<TrailerConfig> {
611
+ /** A line of a block, or a trailer to add (git's `trailer_item`). */
612
+ interface Item {
613
+ /** undefined for a line that is not a trailer */
614
+ token?: string;
615
+ value: string;
616
+ }
617
+
618
+ /** The settings a trailer is added with (git's `conf_info`). */
619
+ interface Conf {
620
+ command?: string;
621
+ cmd?: string;
622
+ where: TrailerWhere;
623
+ ifExists: TrailerIfExists;
624
+ ifMissing: TrailerIfMissing;
625
+ }
626
+
627
+ /** A trailer to add (git's `arg_item`). */
628
+ interface ArgItem {
629
+ token: string;
630
+ value: string;
631
+ conf: Conf;
632
+ }
633
+
634
+ function confOf(item: TrailerItemConfig | undefined, config: Resolved): Conf {
276
635
  return {
277
- separators: trailerConfig.separators || ":",
278
- commentString: trailerConfig.commentString || "#",
279
- items: trailerConfig.items ?? [],
636
+ ...(item?.command !== undefined ? { command: item.command } : {}),
637
+ ...(item?.cmd !== undefined ? { cmd: item.cmd } : {}),
638
+ where: item?.where ?? config.where,
639
+ ifExists: item?.ifExists ?? config.ifExists,
640
+ ifMissing: item?.ifMissing ?? config.ifMissing,
280
641
  };
281
642
  }
282
643
 
283
644
  /**
284
- * sequencer.c's append_signoff (no footer to ignore; `dedup` is APPEND_SIGNOFF_DEDUP): `sob` (`Signed-off-by:
285
- * <ident>` and a line end) appended to `message`, after a blank line unless the message
286
- * ends with a trailer block; nothing appended when the block's last line is `sob` already
287
- * (has_conforming_footer), or when the message is `sob` alone.
645
+ * git's parse_trailer: the token and value of `trailer` around its separator (the whole
646
+ * text as the token for -1), the token replaced by the `key` of the item it names.
647
+ */
648
+ function parseTrailer(
649
+ trailer: string,
650
+ separatorPos: number,
651
+ config: Resolved,
652
+ ): { token: string; value: string; item?: TrailerItemConfig } {
653
+ let token: string;
654
+ let value = "";
655
+ if (separatorPos !== -1) {
656
+ token = trim(trailer.slice(0, separatorPos));
657
+ value = trim(trailer.slice(separatorPos + 1));
658
+ } else token = trim(trailer);
659
+ const len = tokenLength(token);
660
+ const item = config.items.find((i) => tokenMatches(token, i, len));
661
+ if (item) return { token: item.key ?? token, value, item };
662
+ return { token, value };
663
+ }
664
+
665
+ /** trailer.c's parse_trailers: the block of `message` and its items. */
666
+ function parseTrailers(
667
+ message: string,
668
+ options: { unfold?: boolean; onlyTrailers?: boolean; noDivider: boolean },
669
+ config: Resolved,
670
+ ): { block: Block; items: Item[] } {
671
+ const block = trailerBlock(message, config, options.noDivider);
672
+ const items: Item[] = [];
673
+ for (const trailer of block.lines) {
674
+ if (trailer.startsWith(config.commentString)) continue;
675
+ const separatorPos = findSeparator(trailer, 0, config.separators, config.legacy);
676
+ if (separatorPos >= 1) {
677
+ const parsed = parseTrailer(trailer, separatorPos, config);
678
+ items.push({
679
+ token: parsed.token,
680
+ value: options.unfold ? unfoldValue(parsed.value) : parsed.value,
681
+ });
682
+ } else if (!options.onlyTrailers) {
683
+ items.push({ value: trailer.endsWith("\n") ? trailer.slice(0, -1) : trailer });
684
+ }
685
+ }
686
+ return { block, items };
687
+ }
688
+
689
+ /**
690
+ * The lines of the trailer block of `message` (sequencer.c's trailer_iterator: no `---`
691
+ * divider, values unfolded, non-trailer and comment lines included).
692
+ *
693
+ * @example
694
+ * ```typescript
695
+ * trailerLinesOf("Fix\n\nAcked-by: A\n").map((t) => [t.key, t.value]); // [["Acked-by", "A"]]
696
+ * ```
697
+ */
698
+ export function trailerLinesOf(message: string, trailerConfig: TrailerConfig = {}): TrailerLine[] {
699
+ const config = resolve(trailerConfig);
700
+ const { lines } = trailerBlock(message, config, true);
701
+ return lines.map((raw) => {
702
+ const separatorPos = findSeparator(raw, 0, config.separators, config.legacy);
703
+ const parsed = parseTrailer(raw, separatorPos, config);
704
+ return {
705
+ raw,
706
+ key: parsed.token,
707
+ value: unfoldValue(parsed.value),
708
+ isTrailer: separatorPos >= 1,
709
+ };
710
+ });
711
+ }
712
+
713
+ /** git's same_token: the tokens without final punctuation agree over the shorter, without case. */
714
+ function sameToken(a: Item, b: ArgItem): boolean {
715
+ if (a.token === undefined) return false;
716
+ const len = Math.min(tokenLength(a.token), tokenLength(b.token));
717
+ return sameStart(a.token, b.token, len);
718
+ }
719
+
720
+ /** git's same_trailer: same token and the same value without case. */
721
+ function sameTrailer(a: Item, b: ArgItem): boolean {
722
+ return sameToken(a, b) && lowerAscii(a.value) === lowerAscii(b.value);
723
+ }
724
+
725
+ const afterOrEnd = (where: TrailerWhere) => where === "after" || where === "end";
726
+
727
+ /** The state of one run of the engine: the commands' runner and git's stderr. */
728
+ interface Run {
729
+ config: Resolved;
730
+ runCommand?: TrailerCommandRunner;
731
+ errors: string[];
732
+ }
733
+
734
+ /** git's apply_command: the command's trimmed output, "" (and an error) when it failed. */
735
+ async function applyCommand(conf: Conf, arg: string, run: Run): Promise<string> {
736
+ let command: TrailerCommand;
737
+ if (conf.cmd !== undefined) command = { command: conf.cmd, args: [arg] };
738
+ else {
739
+ const text = conf.command ?? "";
740
+ const at = text.indexOf("$ARG");
741
+ command = {
742
+ command: at < 0 ? text : text.slice(0, at) + arg + text.slice(at + "$ARG".length),
743
+ args: [],
744
+ };
745
+ }
746
+ const outcome = await run.runCommand?.(command);
747
+ if (outcome?.stderr) run.errors.push(outcome.stderr);
748
+ if (outcome?.exitCode !== 0) {
749
+ run.errors.push(`error: running trailer command '${command.command}' failed\n`);
750
+ return "";
751
+ }
752
+ return trim(outcome.stdout);
753
+ }
754
+
755
+ /** git's apply_item_command: the value of a trailer with a command, from its output. */
756
+ async function applyItemCommand(inTok: Item | undefined, arg: ArgItem, run: Run): Promise<void> {
757
+ if (!run.runCommand) return;
758
+ if (arg.conf.command === undefined && arg.conf.cmd === undefined) return;
759
+ const value = arg.value ? arg.value : (inTok?.value ?? "");
760
+ arg.value = await applyCommand(arg.conf, value, run);
761
+ }
762
+
763
+ /** git's add_arg_to_input_list: after `on` (after, end) or before it. */
764
+ function addArg(list: Item[], on: Item, arg: ArgItem): void {
765
+ const at = list.indexOf(on);
766
+ const item: Item = { token: arg.token, value: arg.value };
767
+ list.splice(afterOrEnd(arg.conf.where) ? at + 1 : at, 0, item);
768
+ }
769
+
770
+ /** git's check_if_different: no trailer from `inTok` on (toward the start for after/end) is the same. */
771
+ function checkIfDifferent(list: Item[], inTok: Item, arg: ArgItem, checkAll: boolean): boolean {
772
+ const step = afterOrEnd(arg.conf.where) ? -1 : 1;
773
+ let i = list.indexOf(inTok);
774
+ do {
775
+ if (sameTrailer(list[i] as Item, arg)) return false;
776
+ i += step;
777
+ if (i < 0 || i >= list.length) break;
778
+ } while (checkAll);
779
+ return true;
780
+ }
781
+
782
+ /** git's apply_arg_if_exists. */
783
+ async function applyIfExists(
784
+ list: Item[],
785
+ inTok: Item,
786
+ arg: ArgItem,
787
+ on: Item,
788
+ run: Run,
789
+ ): Promise<void> {
790
+ switch (arg.conf.ifExists) {
791
+ case "doNothing":
792
+ return;
793
+ case "replace":
794
+ await applyItemCommand(inTok, arg, run);
795
+ addArg(list, on, arg);
796
+ list.splice(list.indexOf(inTok), 1);
797
+ return;
798
+ case "add":
799
+ await applyItemCommand(inTok, arg, run);
800
+ addArg(list, on, arg);
801
+ return;
802
+ case "addIfDifferent":
803
+ await applyItemCommand(inTok, arg, run);
804
+ if (checkIfDifferent(list, inTok, arg, true)) addArg(list, on, arg);
805
+ return;
806
+ case "addIfDifferentNeighbor":
807
+ await applyItemCommand(inTok, arg, run);
808
+ if (checkIfDifferent(list, on, arg, false)) addArg(list, on, arg);
809
+ return;
810
+ }
811
+ }
812
+
813
+ /** git's process_trailers_lists: each trailer to add applied to the block's items. */
814
+ async function processLists(list: Item[], args: ArgItem[], run: Run): Promise<void> {
815
+ for (const arg of args) {
816
+ // find_same_and_apply_arg
817
+ const where = arg.conf.where;
818
+ const middle = where === "after" || where === "before";
819
+ const backwards = afterOrEnd(where);
820
+ let applied = false;
821
+ if (list.length > 0) {
822
+ const start = (backwards ? list[list.length - 1] : list[0]) as Item;
823
+ const order = backwards ? [...list].reverse() : [...list];
824
+ for (const inTok of order) {
825
+ if (!sameToken(inTok, arg)) continue;
826
+ await applyIfExists(list, inTok, arg, middle ? inTok : start, run);
827
+ applied = true;
828
+ break;
829
+ }
830
+ }
831
+ if (applied) continue;
832
+ // apply_arg_if_missing
833
+ if (arg.conf.ifMissing === "doNothing") continue;
834
+ await applyItemCommand(undefined, arg, run);
835
+ const item: Item = { token: arg.token, value: arg.value };
836
+ if (afterOrEnd(where)) list.push(item);
837
+ else list.unshift(item);
838
+ }
839
+ }
840
+
841
+ /** git's parse_trailers_from_config: a trailer for each item with a `command`. */
842
+ function argsFromConfig(run: Run): ArgItem[] {
843
+ // a host that runs no commands adds none
844
+ if (!run.runCommand) return [];
845
+ return run.config.items
846
+ .filter((item) => item.command !== undefined)
847
+ .map((item) => ({ token: item.key ?? item.name, value: "", conf: confOf(item, run.config) }));
848
+ }
849
+
850
+ /** git's parse_trailers_from_command_line_args (`=` is a separator there too). */
851
+ function argsFromCommandLine(trailers: readonly NewTrailer[], run: Run): ArgItem[] {
852
+ const config = run.config;
853
+ const separators = `=${config.separators}`;
854
+ const args: ArgItem[] = [];
855
+ for (const trailer of trailers) {
856
+ const separatorPos = findSeparator(trailer.text, 0, separators, config.legacy);
857
+ if (separatorPos === 0) {
858
+ run.errors.push(`error: empty trailer token in trailer '${trim(trailer.text)}'\n`);
859
+ continue;
860
+ }
861
+ const parsed = parseTrailer(trailer.text, separatorPos, config);
862
+ const conf = confOf(parsed.item, config);
863
+ if (trailer.where) conf.where = trailer.where;
864
+ if (trailer.ifExists) conf.ifExists = trailer.ifExists;
865
+ if (trailer.ifMissing) conf.ifMissing = trailer.ifMissing;
866
+ args.push({ token: parsed.token, value: parsed.value, conf });
867
+ }
868
+ return args;
869
+ }
870
+
871
+ /** git's last_non_space_char. */
872
+ function lastNonSpace(s: string): string {
873
+ for (let i = s.length - 1; i >= 0; i--) if (!isSpace(s[i])) return s[i] as string;
874
+ return "";
875
+ }
876
+
877
+ /** format_trailers for interpret-trailers (2.43: print_all). */
878
+ function formatItems(
879
+ items: readonly Item[],
880
+ options: { trimEmpty?: boolean; onlyTrailers?: boolean },
881
+ config: Resolved,
882
+ ): string {
883
+ let out = "";
884
+ for (const item of items) {
885
+ if (item.token !== undefined) {
886
+ if (options.trimEmpty && item.value === "") continue;
887
+ const c = lastNonSpace(item.token);
888
+ // 2.43's print_tok_val prints nothing for a token of blanks
889
+ if (config.legacy && !c) continue;
890
+ out += item.token;
891
+ if (c && !config.separators.includes(c)) out += `${config.separators[0]} `;
892
+ out += `${item.value}\n`;
893
+ } else if (!options.onlyTrailers) {
894
+ if (config.legacy && options.trimEmpty && item.value === "") continue;
895
+ out += `${item.value}\n`;
896
+ }
897
+ }
898
+ return out;
899
+ }
900
+
901
+ /** The trailers as {@link NewTrailer}s, strings taking the options' placement. */
902
+ function newTrailersOf(options: InterpretTrailersOptions): NewTrailer[] {
903
+ return (options.trailers ?? []).map((t) =>
904
+ typeof t === "string"
905
+ ? {
906
+ text: t,
907
+ ...(options.where ? { where: options.where } : {}),
908
+ ...(options.ifExists ? { ifExists: options.ifExists } : {}),
909
+ ...(options.ifMissing ? { ifMissing: options.ifMissing } : {}),
910
+ }
911
+ : t,
912
+ );
913
+ }
914
+
915
+ /**
916
+ * trailer.c's process_trailers: `message` with its trailer block parsed, the configured and
917
+ * given trailers applied, and the block written back (the text around it kept as it is).
918
+ */
919
+ export async function processTrailers(
920
+ message: string,
921
+ options: InterpretTrailersOptions = {},
922
+ trailerConfig: TrailerConfig = {},
923
+ ): Promise<InterpretTrailersResult> {
924
+ const config = resolve(trailerConfig);
925
+ const onlyTrailers = options.onlyTrailers || options.parse;
926
+ const onlyInput = options.onlyInput || options.parse;
927
+ const unfold = options.unfold || options.parse;
928
+ const run: Run = {
929
+ config,
930
+ ...(options.runCommand ? { runCommand: options.runCommand } : {}),
931
+ errors: [],
932
+ };
933
+ const { block, items } = parseTrailers(
934
+ message,
935
+ { unfold: !!unfold, onlyTrailers: !!onlyTrailers, noDivider: !!options.noDivider },
936
+ config,
937
+ );
938
+ let out = "";
939
+ if (!onlyTrailers) out += message.slice(0, block.start);
940
+ if (!onlyTrailers && !block.blankLineBefore) out += "\n";
941
+ if (!onlyInput) {
942
+ const args = [...argsFromConfig(run), ...argsFromCommandLine(newTrailersOf(options), run)];
943
+ await processLists(items, args, run);
944
+ }
945
+ out += formatItems(
946
+ items,
947
+ { trimEmpty: !!options.trimEmpty, onlyTrailers: !!onlyTrailers },
948
+ config,
949
+ );
950
+ if (!onlyTrailers) out += message.slice(block.end);
951
+ return { output: out, errors: run.errors.join("") };
952
+ }
953
+
954
+ /**
955
+ * `git interpret-trailers` on one input: the message completed with a line end (as git 2.44+
956
+ * reads its input), then {@link processTrailers}. `trailers` with `onlyInput`/`parse` is git's usage
957
+ * error.
958
+ *
959
+ * @example
960
+ * ```typescript
961
+ * const config = { items: [{ name: "ack", key: "Acked-by" }] };
962
+ * (await interpretTrailers("Fix\n", { trailers: ["ack: Peff"] }, config)).output;
963
+ * // "Fix\n\nAcked-by: Peff\n"
964
+ * ```
965
+ */
966
+ export async function interpretTrailers(
967
+ message: string,
968
+ options: InterpretTrailersOptions = {},
969
+ trailerConfig: TrailerConfig = {},
970
+ ): Promise<InterpretTrailersResult> {
971
+ if ((options.onlyInput || options.parse) && (options.trailers?.length ?? 0) > 0) {
972
+ throw new GitApiError("error: --trailer with --only-input does not make sense", {
973
+ exitCode: 129,
974
+ });
975
+ }
976
+ // read_input_file's strbuf_complete_line (2.44+; 2.43 reads the input as it is)
977
+ const complete =
978
+ trailerConfig.compatibility !== "2.43" && message.length > 0 && !message.endsWith("\n");
979
+ return processTrailers(complete ? `${message}\n` : message, options, trailerConfig);
980
+ }
981
+
982
+ /**
983
+ * git's validate_trailer_args: the `error:` line refusing the first `--trailer` argument that
984
+ * is empty or starts with a separator; undefined when all are fine.
985
+ */
986
+ export function validateTrailerArgs(
987
+ args: readonly string[],
988
+ trailerConfig: TrailerConfig = {},
989
+ ): string | undefined {
990
+ const config = resolve(trailerConfig);
991
+ const separators = `=${config.separators}`;
992
+ for (const text of args) {
993
+ if (!text) return "error: empty --trailer argument";
994
+ if (findSeparator(text, 0, separators, config.legacy) === 0) {
995
+ return `error: invalid trailer '${text}': missing key before separator`;
996
+ }
997
+ }
998
+ return undefined;
999
+ }
1000
+
1001
+ /**
1002
+ * trailer.c's amend_strbuf_with_trailers, what `commit`, `tag`, `merge`, `rebase` and
1003
+ * `cherry-pick` do with their `--trailer` arguments: each added to `message` as
1004
+ * `interpret-trailers --no-divider --trailer <arg>` adds it (the configured `where`, `ifexists`,
1005
+ * `ifmissing` and commands apply). An empty argument throws git's `empty --trailer argument`.
1006
+ *
1007
+ * @example
1008
+ * ```typescript
1009
+ * (await amendWithTrailers("Fix\n", ["Closes: #12"])).output; // "Fix\n\nCloses: #12\n"
1010
+ * ```
1011
+ */
1012
+ export async function amendWithTrailers(
1013
+ message: string,
1014
+ args: readonly string[],
1015
+ trailerConfig: TrailerConfig = {},
1016
+ runCommand?: TrailerCommandRunner,
1017
+ ): Promise<InterpretTrailersResult> {
1018
+ if (args.some((a) => a === "")) {
1019
+ throw new GitApiError("error: empty --trailer argument", { exitCode: 128 });
1020
+ }
1021
+ return processTrailers(
1022
+ message,
1023
+ { trailers: args, noDivider: true, ...(runCommand ? { runCommand } : {}) },
1024
+ trailerConfig,
1025
+ );
1026
+ }
1027
+
1028
+ /** git's `Signed-off-by: <name> <email>` line (sign_off_header and fmt_name), with its line end. */
1029
+ export function signoffLine(ident: { name: string; email: string }): string {
1030
+ return `Signed-off-by: ${ident.name} <${ident.email}>\n`;
1031
+ }
1032
+
1033
+ /**
1034
+ * sequencer.c's append_signoff: `sob` (`Signed-off-by: <ident>` and a line end, see
1035
+ * {@link signoffLine}) added to `message` before its last `ignoreFooter` characters (commit's
1036
+ * comments and scissors, `ignored_log_message_bytes`), after a blank line unless the message
1037
+ * ends with a trailer block; nothing added when the block's last line is `sob` already
1038
+ * (has_conforming_footer), or when the message is `sob` alone. `dedup`
1039
+ * (APPEND_SIGNOFF_DEDUP, format-patch) adds nothing when the block has `sob` anywhere.
288
1040
  *
289
1041
  * @example
290
1042
  * ```typescript
@@ -297,56 +1049,37 @@ export function appendSignoff(
297
1049
  sob: string,
298
1050
  trailerConfig: TrailerConfig = {},
299
1051
  dedup = false,
1052
+ ignoreFooter = 0,
300
1053
  ): string {
301
1054
  // strbuf_complete_line
302
- let out = message.length > 0 && !message.endsWith("\n") ? `${message}\n` : message;
1055
+ let out =
1056
+ ignoreFooter === 0 && message.length > 0 && !message.endsWith("\n") ? `${message}\n` : message;
1057
+ const len = out.length - ignoreFooter;
1058
+ const body = out.slice(0, len);
303
1059
  let footer: 0 | 1 | 2 | 3;
304
- if (out === sob) footer = 3;
1060
+ if (body === sob) footer = 3;
305
1061
  else {
306
- const { lines } = trailerBlock(out, withDefaults(trailerConfig));
1062
+ // has_conforming_footer
1063
+ const { lines } = trailerBlock(body, resolve(trailerConfig), true);
307
1064
  let found = 0;
308
1065
  lines.forEach((line, k) => {
309
1066
  if (line.startsWith(sob)) found = k + 1;
310
1067
  });
311
1068
  footer = lines.length === 0 ? 0 : found === lines.length ? 3 : found ? 2 : 1;
312
1069
  }
1070
+ const insert = (text: string) => {
1071
+ const at = out.length - ignoreFooter;
1072
+ out = out.slice(0, at) + text + out.slice(at);
1073
+ };
313
1074
  if (footer === 0) {
314
- if (out.length === 0) out = "\n\n";
315
- else if (out.length === 1) out += "\n";
316
- else if (out[out.length - 2] !== "\n") out += "\n";
1075
+ if (len === 0) insert("\n\n");
1076
+ else if (len === 1) insert("\n");
1077
+ else if (out[len - 2] !== "\n") insert("\n");
317
1078
  }
318
- // APPEND_SIGNOFF_DEDUP: not added again when the footer has it anywhere
319
- if (footer !== 3 && !(dedup && footer === 2)) out += sob;
1079
+ if (footer !== 3 && !(dedup && footer === 2)) insert(sob);
320
1080
  return out;
321
1081
  }
322
1082
 
323
- /** The block of `message` and its items (trailer.c's parse_trailers, no `---` divider). */
324
- function parseTrailers(
325
- message: string,
326
- options: TrailerFormatOptions,
327
- config: Required<TrailerConfig>,
328
- ): { start: number; end: number; items: TrailerItem[] } {
329
- const { start, end, lines } = trailerBlock(message, config);
330
- const items: TrailerItem[] = [];
331
- for (const trailer of lines) {
332
- if (trailer.startsWith(config.commentString)) continue;
333
- const separatorPos = findSeparator(trailer, 0, config.separators);
334
- if (separatorPos >= 1) {
335
- // parse_trailer: the token as configured (an item's key, when it names one)
336
- let token = trim(trailer.slice(0, separatorPos));
337
- let value = trim(trailer.slice(separatorPos + 1));
338
- const len = tokenLength(token);
339
- const item = config.items.find((i) => tokenMatches(token, i, len));
340
- if (item?.key !== undefined) token = item.key;
341
- if (options.unfold) value = unfoldValue(value);
342
- items.push({ token, value });
343
- } else if (!options.onlyTrailers) {
344
- items.push({ value: trailer.endsWith("\n") ? trailer.slice(0, -1) : trailer });
345
- }
346
- }
347
- return { start, end, items };
348
- }
349
-
350
1083
  /**
351
1084
  * trailer.c's format_trailers_from_commit: the trailer block of `message` (from its
352
1085
  * subject), whole when no option changes it, else each trailer formatted. `before` is what
@@ -359,8 +1092,12 @@ export function appendTrailers(
359
1092
  options: TrailerFormatOptions = {},
360
1093
  trailerConfig: TrailerConfig = {},
361
1094
  ): string {
362
- const config = withDefaults(trailerConfig);
363
- const block = parseTrailers(message, options, config);
1095
+ const config = resolve(trailerConfig);
1096
+ const { block, items } = parseTrailers(
1097
+ message,
1098
+ { unfold: !!options.unfold, onlyTrailers: !!options.onlyTrailers, noDivider: true },
1099
+ config,
1100
+ );
364
1101
  const filtered = options.keys !== undefined;
365
1102
  if (
366
1103
  !options.onlyTrailers &&
@@ -375,7 +1112,7 @@ export function appendTrailers(
375
1112
  }
376
1113
  let out = before;
377
1114
  const origLength = out.length;
378
- for (const item of block.items) {
1115
+ for (const item of items) {
379
1116
  if (item.token !== undefined) {
380
1117
  const token = item.token;
381
1118
  const keep =
@@ -391,7 +1128,7 @@ export function appendTrailers(
391
1128
  if (!options.keyOnly && !options.valueOnly) {
392
1129
  if (options.keyValueSeparator !== undefined) out += options.keyValueSeparator;
393
1130
  else {
394
- const last = token.replace(/[ \t\n\r]+$/, "").slice(-1);
1131
+ const last = lastNonSpace(token);
395
1132
  if (last && !config.separators.includes(last)) out += `${config.separators[0]} `;
396
1133
  }
397
1134
  }