@statewalker/vcs-commands 0.8.0 → 0.9.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 (297) hide show
  1. package/dist/commands/add-command.d.ts +32 -0
  2. package/dist/commands/add-command.d.ts.map +1 -1
  3. package/dist/commands/add-patch/add-patch.d.ts +7 -0
  4. package/dist/commands/add-patch/add-patch.d.ts.map +1 -1
  5. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
  6. package/dist/commands/am/am-command.d.ts +47 -0
  7. package/dist/commands/am/am-command.d.ts.map +1 -1
  8. package/dist/commands/apply/apply-command.d.ts +2 -0
  9. package/dist/commands/apply/apply-command.d.ts.map +1 -1
  10. package/dist/commands/apply/apply-parse.d.ts +7 -0
  11. package/dist/commands/apply/apply-parse.d.ts.map +1 -1
  12. package/dist/commands/apply/index.d.ts +1 -1
  13. package/dist/commands/apply/index.d.ts.map +1 -1
  14. package/dist/commands/blame-command.d.ts +303 -8
  15. package/dist/commands/blame-command.d.ts.map +1 -1
  16. package/dist/commands/blame-ignore.d.ts +48 -0
  17. package/dist/commands/blame-ignore.d.ts.map +1 -0
  18. package/dist/commands/check-mailmap-command.d.ts +49 -0
  19. package/dist/commands/check-mailmap-command.d.ts.map +1 -0
  20. package/dist/commands/checkout-command.d.ts +7 -0
  21. package/dist/commands/checkout-command.d.ts.map +1 -1
  22. package/dist/commands/checkout-paths.d.ts +4 -0
  23. package/dist/commands/checkout-paths.d.ts.map +1 -1
  24. package/dist/commands/cherry-command.d.ts +61 -0
  25. package/dist/commands/cherry-command.d.ts.map +1 -0
  26. package/dist/commands/cherry-pick-command.d.ts +23 -1
  27. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  28. package/dist/commands/clean-command.d.ts +32 -0
  29. package/dist/commands/clean-command.d.ts.map +1 -1
  30. package/dist/commands/clean-interactive.d.ts +56 -0
  31. package/dist/commands/clean-interactive.d.ts.map +1 -0
  32. package/dist/commands/clone-command.d.ts +116 -2
  33. package/dist/commands/clone-command.d.ts.map +1 -1
  34. package/dist/commands/commit-command.d.ts +50 -2
  35. package/dist/commands/commit-command.d.ts.map +1 -1
  36. package/dist/commands/describe-command.d.ts +71 -64
  37. package/dist/commands/describe-command.d.ts.map +1 -1
  38. package/dist/commands/diff-command.d.ts +118 -6
  39. package/dist/commands/diff-command.d.ts.map +1 -1
  40. package/dist/commands/fetch-command.d.ts +60 -0
  41. package/dist/commands/fetch-command.d.ts.map +1 -1
  42. package/dist/commands/fetch-multiple-command.d.ts +193 -0
  43. package/dist/commands/fetch-multiple-command.d.ts.map +1 -0
  44. package/dist/commands/format-patch-command.d.ts +28 -2
  45. package/dist/commands/format-patch-command.d.ts.map +1 -1
  46. package/dist/commands/git-describe.d.ts +77 -4
  47. package/dist/commands/git-describe.d.ts.map +1 -1
  48. package/dist/commands/git-hooks.d.ts +15 -1
  49. package/dist/commands/git-hooks.d.ts.map +1 -1
  50. package/dist/commands/index.d.ts +12 -0
  51. package/dist/commands/index.d.ts.map +1 -1
  52. package/dist/commands/index.js +5 -3
  53. package/dist/commands/init-command.d.ts +89 -0
  54. package/dist/commands/init-command.d.ts.map +1 -1
  55. package/dist/commands/line-log.d.ts +161 -0
  56. package/dist/commands/line-log.d.ts.map +1 -0
  57. package/dist/commands/log-command.d.ts +235 -6
  58. package/dist/commands/log-command.d.ts.map +1 -1
  59. package/dist/commands/log-graph.d.ts +4 -2
  60. package/dist/commands/log-graph.d.ts.map +1 -1
  61. package/dist/commands/log-output.d.ts +66 -1
  62. package/dist/commands/log-output.d.ts.map +1 -1
  63. package/dist/commands/mailmap.d.ts +24 -1
  64. package/dist/commands/mailmap.d.ts.map +1 -1
  65. package/dist/commands/merge-command.d.ts +51 -1
  66. package/dist/commands/merge-command.d.ts.map +1 -1
  67. package/dist/commands/merge-drivers.d.ts +2 -0
  68. package/dist/commands/merge-drivers.d.ts.map +1 -1
  69. package/dist/commands/merge-strategy-options.d.ts +69 -0
  70. package/dist/commands/merge-strategy-options.d.ts.map +1 -0
  71. package/dist/commands/merge-support.d.ts +10 -0
  72. package/dist/commands/merge-support.d.ts.map +1 -1
  73. package/dist/commands/message-cleanup.d.ts +2 -0
  74. package/dist/commands/message-cleanup.d.ts.map +1 -1
  75. package/dist/commands/name-rev-command.d.ts +65 -0
  76. package/dist/commands/name-rev-command.d.ts.map +1 -0
  77. package/dist/commands/name-rev.d.ts +110 -22
  78. package/dist/commands/name-rev.d.ts.map +1 -1
  79. package/dist/commands/octopus-merge.d.ts +26 -1
  80. package/dist/commands/octopus-merge.d.ts.map +1 -1
  81. package/dist/commands/ort-merge.d.ts +16 -0
  82. package/dist/commands/ort-merge.d.ts.map +1 -1
  83. package/dist/commands/ort-recursive.d.ts.map +1 -1
  84. package/dist/commands/patch-id.d.ts +58 -0
  85. package/dist/commands/patch-id.d.ts.map +1 -1
  86. package/dist/commands/pull-command.d.ts +182 -38
  87. package/dist/commands/pull-command.d.ts.map +1 -1
  88. package/dist/commands/push-command.d.ts +65 -0
  89. package/dist/commands/push-command.d.ts.map +1 -1
  90. package/dist/commands/range-diff/index.d.ts +4 -0
  91. package/dist/commands/range-diff/index.d.ts.map +1 -0
  92. package/dist/commands/range-diff/linear-assignment.d.ts +27 -0
  93. package/dist/commands/range-diff/linear-assignment.d.ts.map +1 -0
  94. package/dist/commands/range-diff/range-diff-command.d.ts +121 -0
  95. package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -0
  96. package/dist/commands/range-diff/range-diff.d.ts +106 -0
  97. package/dist/commands/range-diff/range-diff.d.ts.map +1 -0
  98. package/dist/commands/read-tree-command.d.ts +14 -0
  99. package/dist/commands/read-tree-command.d.ts.map +1 -1
  100. package/dist/commands/rebase-command.d.ts +21 -2
  101. package/dist/commands/rebase-command.d.ts.map +1 -1
  102. package/dist/commands/remerge-diff.d.ts +59 -0
  103. package/dist/commands/remerge-diff.d.ts.map +1 -0
  104. package/dist/commands/remote-command.d.ts +41 -0
  105. package/dist/commands/remote-command.d.ts.map +1 -1
  106. package/dist/commands/remote-show-command.d.ts +204 -0
  107. package/dist/commands/remote-show-command.d.ts.map +1 -0
  108. package/dist/commands/reset-command.d.ts +19 -1
  109. package/dist/commands/reset-command.d.ts.map +1 -1
  110. package/dist/commands/restore-command.d.ts +7 -0
  111. package/dist/commands/restore-command.d.ts.map +1 -1
  112. package/dist/commands/rev-list-command.d.ts +75 -3
  113. package/dist/commands/rev-list-command.d.ts.map +1 -1
  114. package/dist/commands/revert-command.d.ts +22 -1
  115. package/dist/commands/revert-command.d.ts.map +1 -1
  116. package/dist/commands/revision-range.d.ts +2 -0
  117. package/dist/commands/revision-range.d.ts.map +1 -1
  118. package/dist/commands/sequencer-pick.d.ts +102 -1
  119. package/dist/commands/sequencer-pick.d.ts.map +1 -1
  120. package/dist/commands/sequencer-support.d.ts +10 -1
  121. package/dist/commands/sequencer-support.d.ts.map +1 -1
  122. package/dist/commands/shortlog-command.d.ts +109 -0
  123. package/dist/commands/shortlog-command.d.ts.map +1 -0
  124. package/dist/commands/shortlog.d.ts +151 -0
  125. package/dist/commands/shortlog.d.ts.map +1 -0
  126. package/dist/commands/status-command.d.ts +31 -2
  127. package/dist/commands/status-command.d.ts.map +1 -1
  128. package/dist/commands/submodule-commands.d.ts +90 -2
  129. package/dist/commands/submodule-commands.d.ts.map +1 -1
  130. package/dist/commands/submodule-fetch.d.ts +149 -0
  131. package/dist/commands/submodule-fetch.d.ts.map +1 -0
  132. package/dist/commands/submodule-model.d.ts +8 -0
  133. package/dist/commands/submodule-model.d.ts.map +1 -1
  134. package/dist/commands/submodule-unpack.d.ts +120 -0
  135. package/dist/commands/submodule-unpack.d.ts.map +1 -0
  136. package/dist/commands/submodule-update-command.d.ts +8 -0
  137. package/dist/commands/submodule-update-command.d.ts.map +1 -1
  138. package/dist/commands/subtree-shift.d.ts +27 -0
  139. package/dist/commands/subtree-shift.d.ts.map +1 -0
  140. package/dist/commands/switch-command.d.ts +3 -0
  141. package/dist/commands/switch-command.d.ts.map +1 -1
  142. package/dist/commands/tree-restore.d.ts.map +1 -1
  143. package/dist/commands/worktree-overwrite-checks.d.ts +2 -0
  144. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
  145. package/dist/{commands-ISM52N0n.js → commands-CNvli5bl.js} +39426 -61169
  146. package/dist/errors/command-errors.d.ts +25 -0
  147. package/dist/errors/command-errors.d.ts.map +1 -1
  148. package/dist/errors/index.js +29 -1
  149. package/dist/fsck-CPeDh9fh.js +5138 -0
  150. package/dist/git-command.d.ts +31 -1
  151. package/dist/git-command.d.ts.map +1 -1
  152. package/dist/git.d.ts +97 -1
  153. package/dist/git.d.ts.map +1 -1
  154. package/dist/guarded-transport-command.d.ts +82 -0
  155. package/dist/guarded-transport-command.d.ts.map +1 -0
  156. package/dist/index.d.ts +1 -0
  157. package/dist/index.d.ts.map +1 -1
  158. package/dist/index.js +6 -5
  159. package/dist/remote-transport-CCmBadm-.js +1606 -0
  160. package/dist/rename/detect-renames.d.ts +20 -0
  161. package/dist/rename/detect-renames.d.ts.map +1 -1
  162. package/dist/rename/diffcore-break.d.ts +51 -0
  163. package/dist/rename/diffcore-break.d.ts.map +1 -0
  164. package/dist/rename/index.d.ts +1 -0
  165. package/dist/rename/index.d.ts.map +1 -1
  166. package/dist/results/blame-format.d.ts +62 -1
  167. package/dist/results/blame-format.d.ts.map +1 -1
  168. package/dist/results/clone-result.d.ts +9 -0
  169. package/dist/results/clone-result.d.ts.map +1 -1
  170. package/dist/results/diff-dirstat.d.ts +36 -0
  171. package/dist/results/diff-dirstat.d.ts.map +1 -0
  172. package/dist/results/diff-entry.d.ts +10 -0
  173. package/dist/results/diff-entry.d.ts.map +1 -1
  174. package/dist/results/diff-formatter.d.ts.map +1 -1
  175. package/dist/results/diff-output.d.ts +18 -0
  176. package/dist/results/diff-output.d.ts.map +1 -1
  177. package/dist/results/fetch-result.d.ts +12 -1
  178. package/dist/results/fetch-result.d.ts.map +1 -1
  179. package/dist/results/index.d.ts +2 -0
  180. package/dist/results/index.d.ts.map +1 -1
  181. package/dist/results/index.js +2 -2
  182. package/dist/results/init-result.d.ts +8 -1
  183. package/dist/results/init-result.d.ts.map +1 -1
  184. package/dist/results/line-range-hunks.d.ts +27 -0
  185. package/dist/results/line-range-hunks.d.ts.map +1 -0
  186. package/dist/results/merge-result.d.ts +8 -1
  187. package/dist/results/merge-result.d.ts.map +1 -1
  188. package/dist/results/pull-result.d.ts +11 -0
  189. package/dist/results/pull-result.d.ts.map +1 -1
  190. package/dist/results/push-result.d.ts +5 -0
  191. package/dist/results/push-result.d.ts.map +1 -1
  192. package/dist/results/status-result.d.ts +1 -1
  193. package/dist/results/status-result.d.ts.map +1 -1
  194. package/dist/results-BDuEgdLU.js +46847 -0
  195. package/dist/transport-command.d.ts +14 -0
  196. package/dist/transport-command.d.ts.map +1 -1
  197. package/package.json +12 -12
  198. package/src/commands/add-command.ts +115 -0
  199. package/src/commands/add-patch/add-patch.ts +8 -0
  200. package/src/commands/add-patch/patch-backend.ts +20 -2
  201. package/src/commands/am/am-command.ts +138 -4
  202. package/src/commands/apply/apply-command.ts +52 -9
  203. package/src/commands/apply/apply-parse.ts +10 -0
  204. package/src/commands/apply/index.ts +8 -1
  205. package/src/commands/blame-command.ts +1450 -51
  206. package/src/commands/blame-ignore.ts +307 -0
  207. package/src/commands/check-mailmap-command.ts +129 -0
  208. package/src/commands/checkout-command.ts +97 -12
  209. package/src/commands/checkout-paths.ts +28 -0
  210. package/src/commands/cherry-command.ts +187 -0
  211. package/src/commands/cherry-pick-command.ts +42 -1
  212. package/src/commands/clean-command.ts +92 -12
  213. package/src/commands/clean-interactive.ts +522 -0
  214. package/src/commands/clone-command.ts +596 -44
  215. package/src/commands/commit-command.ts +180 -47
  216. package/src/commands/describe-command.ts +341 -254
  217. package/src/commands/diff-command.ts +616 -56
  218. package/src/commands/fetch-command.ts +353 -6
  219. package/src/commands/fetch-multiple-command.ts +665 -0
  220. package/src/commands/format-patch-command.ts +189 -77
  221. package/src/commands/git-describe.ts +456 -217
  222. package/src/commands/git-hooks.ts +42 -3
  223. package/src/commands/index.ts +17 -0
  224. package/src/commands/init-command.ts +603 -49
  225. package/src/commands/line-log.ts +931 -0
  226. package/src/commands/log-command.ts +1414 -177
  227. package/src/commands/log-graph.ts +6 -3
  228. package/src/commands/log-output.ts +173 -10
  229. package/src/commands/mailmap.ts +41 -5
  230. package/src/commands/merge-command.ts +274 -16
  231. package/src/commands/merge-drivers.ts +3 -0
  232. package/src/commands/merge-strategy-options.ts +224 -0
  233. package/src/commands/merge-support.ts +101 -21
  234. package/src/commands/message-cleanup.ts +1 -1
  235. package/src/commands/name-rev-command.ts +184 -0
  236. package/src/commands/name-rev.ts +700 -109
  237. package/src/commands/octopus-merge.ts +35 -11
  238. package/src/commands/ort-merge.ts +41 -7
  239. package/src/commands/ort-recursive.ts +40 -2
  240. package/src/commands/patch-id.ts +109 -0
  241. package/src/commands/pull-command.ts +718 -224
  242. package/src/commands/push-command.ts +353 -3
  243. package/src/commands/range-diff/index.ts +3 -0
  244. package/src/commands/range-diff/linear-assignment.ts +203 -0
  245. package/src/commands/range-diff/range-diff-command.ts +433 -0
  246. package/src/commands/range-diff/range-diff.ts +617 -0
  247. package/src/commands/read-tree-command.ts +74 -3
  248. package/src/commands/rebase-command.ts +141 -65
  249. package/src/commands/remerge-diff.ts +241 -0
  250. package/src/commands/remote-command.ts +98 -10
  251. package/src/commands/remote-show-command.ts +836 -0
  252. package/src/commands/reset-command.ts +78 -3
  253. package/src/commands/restore-command.ts +15 -0
  254. package/src/commands/rev-list-command.ts +187 -11
  255. package/src/commands/revert-command.ts +41 -1
  256. package/src/commands/revision-range.ts +3 -1
  257. package/src/commands/sequencer-pick.ts +323 -29
  258. package/src/commands/sequencer-support.ts +164 -2
  259. package/src/commands/shortlog-command.ts +354 -0
  260. package/src/commands/shortlog.ts +361 -0
  261. package/src/commands/show-command.ts +2 -1
  262. package/src/commands/status-command.ts +115 -25
  263. package/src/commands/status-format.ts +1 -1
  264. package/src/commands/submodule-commands.ts +321 -3
  265. package/src/commands/submodule-fetch.ts +789 -0
  266. package/src/commands/submodule-model.ts +9 -1
  267. package/src/commands/submodule-unpack.ts +265 -0
  268. package/src/commands/submodule-update-command.ts +39 -5
  269. package/src/commands/subtree-shift.ts +215 -0
  270. package/src/commands/switch-command.ts +9 -0
  271. package/src/commands/tree-restore.ts +6 -1
  272. package/src/commands/worktree-overwrite-checks.ts +5 -0
  273. package/src/errors/command-errors.ts +35 -0
  274. package/src/git-command.ts +87 -1
  275. package/src/git.ts +146 -0
  276. package/src/guarded-transport-command.ts +171 -0
  277. package/src/index.ts +1 -0
  278. package/src/rename/detect-renames.ts +42 -8
  279. package/src/rename/diffcore-break.ts +166 -0
  280. package/src/rename/index.ts +1 -0
  281. package/src/results/blame-format.ts +268 -43
  282. package/src/results/clone-result.ts +6 -0
  283. package/src/results/diff-dirstat.ts +117 -0
  284. package/src/results/diff-entry.ts +12 -0
  285. package/src/results/diff-formatter.ts +1 -0
  286. package/src/results/diff-output.ts +127 -2
  287. package/src/results/fetch-result.ts +12 -1
  288. package/src/results/index.ts +2 -0
  289. package/src/results/init-result.ts +10 -1
  290. package/src/results/line-range-hunks.ts +126 -0
  291. package/src/results/merge-result.ts +9 -0
  292. package/src/results/pull-result.ts +7 -0
  293. package/src/results/push-result.ts +5 -0
  294. package/src/results/status-result.ts +2 -2
  295. package/src/transport-command.ts +34 -3
  296. package/dist/fsck-DLRpbwTj.js +0 -13405
  297. package/dist/results-TB2IJ64h.js +0 -4700
@@ -35,7 +35,7 @@ import {
35
35
  MergeStatus,
36
36
  MergeStrategy,
37
37
  } from "../results/merge-result.js";
38
- import { CommitCommand, type CommitResult } from "./commit-command.js";
38
+ import { CommitCommand, type CommitEditor, type CommitResult } from "./commit-command.js";
39
39
  import { coreBool, worktreeModeTrust } from "./core-config.js";
40
40
  import {
41
41
  DEFAULT_MERGE_LOG_LENGTH,
@@ -44,6 +44,7 @@ import {
44
44
  } from "./fmt-merge-msg-command.js";
45
45
  import { configBool, conflictStyleOf } from "./merge-config.js";
46
46
  import { ortConfigOptions } from "./merge-drivers.js";
47
+ import { parseStrategyOptions, strategyOrtOptions } from "./merge-strategy-options.js";
47
48
  import {
48
49
  conflictsComment,
49
50
  flatTree,
@@ -63,8 +64,9 @@ import {
63
64
  cleanupWith,
64
65
  commitCleanupMode,
65
66
  commitMessageFromOption,
67
+ preparedMessageCleanup,
66
68
  } from "./message-cleanup.js";
67
- import { octopusMerge } from "./octopus-merge.js";
69
+ import { octopusMerge, threeway } from "./octopus-merge.js";
68
70
  import { ortResultEntries } from "./ort-merge.js";
69
71
  import { mergeBaseTree, mergeStoredTrees } from "./ort-recursive.js";
70
72
  import { reflogMessages, updateHead, updateHeadExpecting } from "./reflog-messages.js";
@@ -148,7 +150,11 @@ export class MergeCommand extends GitCommand<MergeResult> {
148
150
  private includes: string[] = [];
149
151
  private fastForwardMode = FastForwardMode.FF;
150
152
  private strategy = MergeStrategy.RECURSIVE;
153
+ /** Whether `-s` was given (`-s recursive` names itself in git's messages) */
154
+ private strategyGiven = false;
151
155
  private contentStrategy?: ContentMergeStrategy;
156
+ /** `-X <option>`s, in order */
157
+ private strategyOptions: string[] = [];
152
158
  private squash = false;
153
159
  private commit = true;
154
160
  private message?: string;
@@ -260,6 +266,9 @@ export class MergeCommand extends GitCommand<MergeResult> {
260
266
  /** `--log[=<n>]` / `--no-log` (undefined: `merge.log`) */
261
267
  private log?: number;
262
268
  private signoff = false;
269
+ /** `--edit` / `--no-edit` (undefined: no editor, as git without a terminal) */
270
+ private edit?: boolean;
271
+ private editor?: CommitEditor;
263
272
  private intoName?: string;
264
273
  /** FETCH_HEAD-style lines naming the heads (a pull's), instead of merge_name's */
265
274
  private mergeNames?: string;
@@ -296,13 +305,37 @@ export class MergeCommand extends GitCommand<MergeResult> {
296
305
  * - RECURSIVE (default): git's merge-ort
297
306
  * - OURS: Keep our tree, ignore their changes (creates merge commit)
298
307
  * - THEIRS: Replace our tree with theirs (creates merge commit)
299
- * - RESOLVE: as RECURSIVE
308
+ * - RESOLVE: git-merge-resolve (a trivial in-index merge, else `read-tree -m --aggressive`
309
+ * and git-merge-one-file)
310
+ * - SUBTREE: merge-ort with the other side shifted to ours' shape (never a fast-forward)
300
311
  *
301
312
  * @param strategy Merge strategy
302
313
  */
303
314
  setStrategy(strategy: MergeStrategy): this {
304
315
  this.checkCallable();
305
316
  this.strategy = strategy;
317
+ this.strategyGiven = true;
318
+ return this;
319
+ }
320
+
321
+ /**
322
+ * git's `-X <option>`s, in order (merge-ort's parse_merge_opt): `ours`, `theirs`,
323
+ * `subtree[=<path>]`, `patience`, `histogram`, `diff-algorithm=<name>`,
324
+ * `ignore-space-change`, `ignore-all-space`, `ignore-space-at-eol`, `ignore-cr-at-eol`,
325
+ * `renormalize`, `no-renormalize`, `no-renames`, `find-renames[=<n>]`,
326
+ * `rename-threshold=<n>`. An unknown one fails the merge when merge-ort runs
327
+ * ({@link UnknownStrategyOptionError}, git's `unknown strategy option`).
328
+ */
329
+ setStrategyOptions(options: readonly string[]): this {
330
+ this.checkCallable();
331
+ this.strategyOptions = [...options];
332
+ return this;
333
+ }
334
+
335
+ /** Adds one `-X <option>` (see {@link setStrategyOptions}). */
336
+ addStrategyOption(option: string): this {
337
+ this.checkCallable();
338
+ this.strategyOptions.push(option);
306
339
  return this;
307
340
  }
308
341
 
@@ -465,6 +498,32 @@ export class MergeCommand extends GitCommand<MergeResult> {
465
498
  return this;
466
499
  }
467
500
 
501
+ /**
502
+ * `--edit` (true) / `--no-edit` (false): with `--edit` the merge commit's message goes
503
+ * through the editor ({@link setEditor}) as git's prepare_to_commit runs it: MERGE_MSG
504
+ * written with git's help comment below the message (and the sign-off), the
505
+ * `prepare-commit-msg` hook, the editor, `commit-msg`, then the text cleaned up as an
506
+ * edited message (`commit.cleanup`, else comments stripped). Without an editor set the
507
+ * message is taken as the editor would leave it. Default: no edit (git's without a
508
+ * terminal); a merge that stops before its commit runs no editor.
509
+ */
510
+ setEdit(edit: boolean): this {
511
+ this.checkCallable();
512
+ this.edit = edit;
513
+ return this;
514
+ }
515
+
516
+ /**
517
+ * The editor `--edit` runs over MERGE_MSG: called with `"MERGE_MSG"` and the text, it
518
+ * returns the edited text (anything else keeps the text as it is), as
519
+ * `CommitCommand.setEditor`.
520
+ */
521
+ setEditor(editor: CommitEditor): this {
522
+ this.checkCallable();
523
+ this.editor = editor;
524
+ return this;
525
+ }
526
+
468
527
  /**
469
528
  * `--into-name <branch>`: the default message names this branch as the one merged into
470
529
  * (`into <branch>` left out as for HEAD's under `merge.suppressDest`).
@@ -872,7 +931,9 @@ export class MergeCommand extends GitCommand<MergeResult> {
872
931
  * makes a merge commit, whatever was asked.
873
932
  */
874
933
  private ffMode(): FastForwardMode {
875
- return this.strategy === MergeStrategy.OURS ? FastForwardMode.NO_FF : this.fastForwardMode;
934
+ return this.strategy === MergeStrategy.OURS || this.strategy === MergeStrategy.SUBTREE
935
+ ? FastForwardMode.NO_FF
936
+ : this.fastForwardMode;
876
937
  }
877
938
 
878
939
  /** git's get_octopus_merge_bases: the merge bases of HEAD and every head, folded. */
@@ -1024,6 +1085,9 @@ export class MergeCommand extends GitCommand<MergeResult> {
1024
1085
  worktree: Worktree | undefined,
1025
1086
  mergeBases: ObjectId[],
1026
1087
  ): Promise<MergeResult> {
1088
+ if (this.strategy === MergeStrategy.RESOLVE) {
1089
+ return this.doResolve(headId, srcId, srcName, head, state, worktree, mergeBases);
1090
+ }
1027
1091
  // no merge base (unrelated histories allowed): merge-ort merges against the empty tree
1028
1092
  const baseId = mergeBases[0] as ObjectId | undefined;
1029
1093
  const headCommit = await this.commits.load(headId);
@@ -1041,12 +1105,22 @@ export class MergeCommand extends GitCommand<MergeResult> {
1041
1105
  } else if (this.strategy === MergeStrategy.THEIRS) {
1042
1106
  target = await flatTree(this.trees, srcCommit.tree);
1043
1107
  } else {
1108
+ // try_merge_strategy: `-X` read (and refused) before anything is merged
1109
+ const xopts = parseStrategyOptions(this.strategyOptions);
1110
+ const subtreeShift =
1111
+ xopts.subtreeShift ?? (this.strategy === MergeStrategy.SUBTREE ? "" : undefined);
1044
1112
  const common = {
1045
1113
  style: conflictStyleOf(this.workingCopy),
1046
1114
  ignoreCase: coreBool(this.workingCopy, "ignorecase", false),
1047
1115
  bigFileThreshold: this.bigFileThreshold,
1048
- ...ortConfigOptions(this.workingCopy, worktree, { renormalize: this.renormalize }),
1116
+ ...ortConfigOptions(this.workingCopy, worktree, {
1117
+ renormalize: xopts.renormalize ?? this.renormalize,
1118
+ }),
1119
+ ...strategyOrtOptions(this.inProcess ? undefined : this.workingCopy, xopts),
1120
+ // the inner merges of a virtual merge base are shifted too
1121
+ ...(subtreeShift !== undefined ? { subtreeShift } : {}),
1049
1122
  };
1123
+
1050
1124
  // several merge bases: merge-ort's virtual merge base, built in memory
1051
1125
  const base = await mergeBaseTree(
1052
1126
  {
@@ -1070,7 +1144,7 @@ export class MergeCommand extends GitCommand<MergeResult> {
1070
1144
  oursLabel: "HEAD",
1071
1145
  theirsLabel: srcName,
1072
1146
  baseLabel: base.label,
1073
- favor: favorOf(this.contentStrategy),
1147
+ favor: favorOf(this.contentStrategy) ?? xopts.favor,
1074
1148
  },
1075
1149
  );
1076
1150
  target = await ortResultEntries(this.blobs, merge);
@@ -1216,6 +1290,96 @@ export class MergeCommand extends GitCommand<MergeResult> {
1216
1290
  };
1217
1291
  }
1218
1292
 
1293
+ /**
1294
+ * `-s resolve` of one head (builtin/merge.c with git-merge-resolve): with one merge base and
1295
+ * a commit to make, first git's "really trivial in-index merge" (`read-tree` with
1296
+ * trivial_merges_only: every path taken whole from a side, committed as `In-index merge`);
1297
+ * otherwise, or when that is not trivial ("Nope."), git-merge-resolve's
1298
+ * `read-tree -m --aggressive` and git-merge-one-file for the paths left.
1299
+ */
1300
+ private async doResolve(
1301
+ headId: ObjectId,
1302
+ srcId: ObjectId,
1303
+ srcName: string,
1304
+ head: Awaited<ReturnType<typeof this.refsStore.get>>,
1305
+ state: OperationState,
1306
+ worktree: Worktree | undefined,
1307
+ mergeBases: ObjectId[],
1308
+ ): Promise<MergeResult> {
1309
+ const output: string[] = [];
1310
+ const message = await this.mergeMessage(headId, [{ id: srcId, name: srcName }]);
1311
+ const mergedCommits = [headId, srcId];
1312
+ let refusedTrivial = "";
1313
+ if (mergeBases.length === 1 && this.commit && !this.squash) {
1314
+ const headTreeId = await this.commits.getTree(headId);
1315
+ const headTree = await flatTree(this.trees, headTreeId);
1316
+ const { index } = await readIndex(this.staging);
1317
+ const staged = indexChanges(index, headTree);
1318
+ if (staged.length > 0) {
1319
+ return {
1320
+ status: MergeStatus.FAILED,
1321
+ newHead: headId,
1322
+ mergedCommits,
1323
+ message: `error: Your local changes to the following files would be overwritten by merge:\n ${staged.join(" ")}`,
1324
+ failingPaths: new Map(staged.map((p) => [p, MergeFailureReason.DIRTY_INDEX])),
1325
+ };
1326
+ }
1327
+ output.push("Trying really trivial in-index merge...");
1328
+ const base = await flatTree(this.trees, await this.commits.getTree(mergeBases[0]));
1329
+ const remote = await flatTree(this.trees, await this.commits.getTree(srcId));
1330
+ const target = trivialMerge(base, headTree, remote);
1331
+ if (target) {
1332
+ const refused = await switchTrees(this.context(worktree), headTree, target, {
1333
+ action: "merge",
1334
+ operation: "merge",
1335
+ });
1336
+ if (!refused) {
1337
+ output.push("Wonderful.");
1338
+ const treeId = await storeFlatTree(this.trees, target);
1339
+ const writeState = async (text: string) => this.writeMergeState(state, srcId, text);
1340
+ const hooked = await this.mergeCommitHooks(state, message, writeState);
1341
+ if (hooked.refused !== undefined) {
1342
+ return {
1343
+ status: MergeStatus.FAILED,
1344
+ newHead: headId,
1345
+ mergedCommits,
1346
+ message: hooked.refused,
1347
+ output,
1348
+ };
1349
+ }
1350
+ const newCommitId = await this.storeCommit(treeId, mergedCommits, hooked.message, () =>
1351
+ writeState(hooked.stateText ?? message),
1352
+ );
1353
+ await this.updateHead(head, newCommitId, "In-index merge");
1354
+ await state.removeRef("AUTO_MERGE");
1355
+ output.push("In-index merge");
1356
+ return { status: MergeStatus.MERGED, newHead: newCommitId, mergedCommits, output };
1357
+ }
1358
+ refusedTrivial = `${refused.message}\n`;
1359
+ } else {
1360
+ // unpack-trees' trivial_merges_only refusal
1361
+ refusedTrivial = "error: Merge requires file-level merging\n";
1362
+ }
1363
+ output.push("Nope.");
1364
+ }
1365
+ const result = await this.doOctopus(
1366
+ headId,
1367
+ head,
1368
+ [{ id: srcId, name: srcName }],
1369
+ false,
1370
+ message,
1371
+ state,
1372
+ worktree,
1373
+ );
1374
+ return {
1375
+ ...result,
1376
+ output: [...output, ...(result.output ?? [])],
1377
+ ...(refusedTrivial && result.message !== undefined
1378
+ ? { message: `${refusedTrivial}${result.message}` }
1379
+ : {}),
1380
+ };
1381
+ }
1382
+
1219
1383
  /**
1220
1384
  * A merge of several heads: git-merge-octopus (the heads merged one after the other) or,
1221
1385
  * with strategy `ours`, git-merge-ours (HEAD's tree); written when it merged or left
@@ -1237,7 +1401,12 @@ export class MergeCommand extends GitCommand<MergeResult> {
1237
1401
  const headTree = await flatTree(this.trees, await this.commits.getTree(headId));
1238
1402
  const { index } = await readIndex(this.staging);
1239
1403
  const staged = indexChanges(index, headTree);
1240
- const strategy = this.strategy === MergeStrategy.OURS ? "ours" : "octopus";
1404
+ const strategy =
1405
+ this.strategy === MergeStrategy.OURS
1406
+ ? "ours"
1407
+ : this.strategy === MergeStrategy.RESOLVE
1408
+ ? "resolve"
1409
+ : "octopus";
1241
1410
  // git restores the state it saved (reset --hard, stash apply): AUTO_MERGE as for ort
1242
1411
  const giveUp = async (lines: string[], output: string[] = []): Promise<MergeResult> => {
1243
1412
  const tree = (await this.commits.getTree(headId)) as ObjectId;
@@ -1251,11 +1420,18 @@ export class MergeCommand extends GitCommand<MergeResult> {
1251
1420
  };
1252
1421
  };
1253
1422
  // git-merge-resolve and merge-ort handle two heads only
1254
- if (this.strategy === MergeStrategy.RESOLVE) {
1423
+ if (this.strategy === MergeStrategy.RESOLVE && remotes.length > 1) {
1255
1424
  const tree = (await this.commits.getTree(headId)) as ObjectId;
1256
1425
  const restored = await this.restoreStateQuirk(state, tree, worktree);
1257
1426
  return failed(`${restored}Merge with strategy resolve failed.`);
1258
1427
  }
1428
+ if (this.strategy === MergeStrategy.SUBTREE) {
1429
+ const tree = (await this.commits.getTree(headId)) as ObjectId;
1430
+ const restored = await this.restoreStateQuirk(state, tree, worktree);
1431
+ return failed(
1432
+ `error: Not handling anything other than two heads merge.\n${restored}Merge with strategy subtree failed.`,
1433
+ );
1434
+ }
1259
1435
  let outcome: Awaited<ReturnType<typeof octopusMerge>>;
1260
1436
  if (this.strategy === MergeStrategy.OURS) {
1261
1437
  // git-merge-ours: refuses an index that differs from HEAD, else HEAD's tree
@@ -1280,6 +1456,7 @@ export class MergeCommand extends GitCommand<MergeResult> {
1280
1456
  headId,
1281
1457
  remotes,
1282
1458
  staged,
1459
+ { resolve: this.strategy === MergeStrategy.RESOLVE },
1283
1460
  );
1284
1461
  }
1285
1462
  const output = [...outcome.output];
@@ -1772,8 +1949,10 @@ export class MergeCommand extends GitCommand<MergeResult> {
1772
1949
  writeState: (text: string) => Promise<void>,
1773
1950
  ): Promise<{ message: string; refused?: string; stateText?: string }> {
1774
1951
  // (a merge a rebase makes in process runs no hooks of its own)
1775
- const signed = this.signedOff(message);
1776
- if (!this.hooks.enabled || this.inProcess) {
1952
+ const edit = this.edit === true && !this.inProcess;
1953
+ const signed = this.signedOff(edit ? `${message}\n${this.editorComment()}` : message);
1954
+ const hooksRun = this.hooks.enabled && !this.inProcess;
1955
+ if (!hooksRun && !edit) {
1777
1956
  return { message: this.cleanup(signed), stateText: signed };
1778
1957
  }
1779
1958
  const env = {
@@ -1788,17 +1967,26 @@ export class MergeCommand extends GitCommand<MergeResult> {
1788
1967
  refused: "Not committing merge; use 'git commit' to complete the merge.",
1789
1968
  };
1790
1969
  };
1791
- if (!this.noVerify) {
1970
+ if (hooksRun && !this.noVerify) {
1792
1971
  const pre = await this.hooks.run("pre-merge-commit", { env });
1793
1972
  if (pre && pre.exitCode !== 0) return abort();
1794
1973
  if (pre) await this.staging.read();
1795
1974
  }
1796
1975
  // prepare_to_commit writes merge_msg as it is (no newline added), signed off
1797
1976
  await writeState(signed);
1798
- const file = await this.hooks.gitPath("MERGE_MSG");
1799
- const prepare = await this.hooks.run("prepare-commit-msg", { args: [file, "merge"], env });
1800
- if (prepare && prepare.exitCode !== 0) return abort();
1801
- if (!this.noVerify) {
1977
+ if (hooksRun) {
1978
+ const file = await this.hooks.gitPath("MERGE_MSG");
1979
+ const prepare = await this.hooks.run("prepare-commit-msg", { args: [file, "merge"], env });
1980
+ if (prepare && prepare.exitCode !== 0) return abort();
1981
+ }
1982
+ if (edit && this.editor) {
1983
+ // launch_editor over MERGE_MSG
1984
+ const current = (await state.read("MERGE_MSG")) ?? "";
1985
+ const edited = await this.editor("MERGE_MSG", current);
1986
+ if (typeof edited === "string") await state.write("MERGE_MSG", edited);
1987
+ }
1988
+ if (hooksRun && !this.noVerify) {
1989
+ const file = await this.hooks.gitPath("MERGE_MSG");
1802
1990
  const check = await this.hooks.run("commit-msg", { args: [file], env });
1803
1991
  if (check && check.exitCode !== 0) return abort();
1804
1992
  }
@@ -1806,7 +1994,10 @@ export class MergeCommand extends GitCommand<MergeResult> {
1806
1994
  // the commit ends the merge (remove_merge_branch_state)
1807
1995
  await state.removeRef("MERGE_HEAD");
1808
1996
  await state.remove("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE");
1809
- return { message: this.cleanup(text), stateText: text };
1997
+ const cleaned = edit
1998
+ ? preparedMessageCleanup(this.workingCopy, text, this.cleanupMode)
1999
+ : this.cleanup(text);
2000
+ return { message: cleaned, stateText: text };
1810
2001
  }
1811
2002
 
1812
2003
  /** MERGE_HEAD, MERGE_MSG and MERGE_MODE of a merge stopped before its commit. */
@@ -1826,11 +2017,52 @@ export class MergeCommand extends GitCommand<MergeResult> {
1826
2017
  return "ours";
1827
2018
  case MergeStrategy.THEIRS:
1828
2019
  return "theirs";
2020
+ case MergeStrategy.RESOLVE:
2021
+ return "resolve";
2022
+ case MergeStrategy.SUBTREE:
2023
+ return "subtree";
2024
+ case MergeStrategy.RECURSIVE:
2025
+ return this.strategyGiven ? "recursive" : "ort";
1829
2026
  default:
1830
2027
  return "ort";
1831
2028
  }
1832
2029
  }
1833
2030
 
2031
+ /**
2032
+ * prepare_to_commit's help below the message for the editor (merge_editor_comment and
2033
+ * its scissors or no-scissors line), each line commented.
2034
+ */
2035
+ private editorComment(): string {
2036
+ const comment = trailerConfigOf(this.workingCopy).commentString;
2037
+ const commented = (text: string) =>
2038
+ text
2039
+ .split("\n")
2040
+ .slice(0, -1)
2041
+ .map((line) => (line === "" ? `${comment}\n` : `${comment} ${line}\n`))
2042
+ .join("");
2043
+ const scissors =
2044
+ (this.cleanupMode ?? RemoteConfigStore.from(this.workingCopy).value("commit.cleanup")) ===
2045
+ "scissors";
2046
+ let out = "";
2047
+ if (scissors) {
2048
+ out += `${comment} ------------------------ >8 ------------------------\n`;
2049
+ out += commented(
2050
+ "Do not modify or remove the line above.\nEverything below it will be ignored.\n",
2051
+ );
2052
+ out += commented("\n");
2053
+ }
2054
+ out += commented(
2055
+ "Please enter a commit message to explain why this merge is necessary,\n" +
2056
+ "especially if it merges an updated upstream into a topic branch.\n\n",
2057
+ );
2058
+ out += scissors
2059
+ ? commented("An empty message aborts the commit.\n")
2060
+ : commented(
2061
+ `Lines starting with '${comment}' will be ignored, and an empty message aborts\nthe commit.\n`,
2062
+ );
2063
+ return out;
2064
+ }
2065
+
1834
2066
  /** git's `cleanup_message` of a merge commit's message (prepare_to_commit, no editor). */
1835
2067
  private cleanup(message: string): string {
1836
2068
  const mode = commitCleanupMode(this.workingCopy, this.cleanupMode);
@@ -1954,3 +2186,29 @@ export function favorOf(
1954
2186
  return undefined;
1955
2187
  }
1956
2188
  }
2189
+
2190
+ /**
2191
+ * builtin/merge.c's read_tree_trivial: the merge of `head` and `remote` against `base` when
2192
+ * every path is trivial (taken whole from a side: unpack-trees' threeway_merge without
2193
+ * `--aggressive`), else undefined. A path that is a file on one side and a directory on
2194
+ * another is never trivial.
2195
+ */
2196
+ function trivialMerge(
2197
+ base: Map<string, CheckedEntry>,
2198
+ head: Map<string, CheckedEntry>,
2199
+ remote: Map<string, CheckedEntry>,
2200
+ ): Map<string, CheckedEntry> | undefined {
2201
+ const paths = new Set([...base.keys(), ...head.keys(), ...remote.keys()]);
2202
+ const dirs = new Set<string>();
2203
+ for (const p of paths) {
2204
+ for (let i = p.indexOf("/"); i >= 0; i = p.indexOf("/", i + 1)) dirs.add(p.slice(0, i));
2205
+ }
2206
+ const out = new Map<string, CheckedEntry>();
2207
+ for (const path of [...paths].sort(compareGitPaths)) {
2208
+ if (dirs.has(path)) return undefined;
2209
+ const r = threeway([base.get(path)], head.get(path), remote.get(path), false);
2210
+ if (r.kind === "merged") out.set(path, r.entry);
2211
+ else if (r.kind !== "removed") return undefined;
2212
+ }
2213
+ return out;
2214
+ }
@@ -150,6 +150,8 @@ export interface LowLevelMergeOptions {
150
150
  bigFileThreshold?: number;
151
151
  /** The diff algorithm: merge-ort's histogram, `checkout -m`'s myers */
152
152
  algorithm?: LineMergeOptions["algorithm"];
153
+ /** xdiff's whitespace bits (merge-ort's `-X ignore-space-change` and the like) */
154
+ whitespace?: number;
153
155
  }
154
156
 
155
157
  /** What {@link lowLevelMerge} gives: `binary` is git's `LL_MERGE_BINARY_CONFLICT`. */
@@ -217,6 +219,7 @@ export async function lowLevelMerge(
217
219
  favor: driver.name === "union" ? "union" : favor,
218
220
  markerSize,
219
221
  ...(options.algorithm ? { algorithm: options.algorithm } : {}),
222
+ ...(options.whitespace ? { whitespace: options.whitespace } : {}),
220
223
  });
221
224
  return { status: lines.conflicts > 0 ? "conflict" : "ok", content: lines.content };
222
225
  }
@@ -0,0 +1,224 @@
1
+ /**
2
+ * git's merge strategy options (`-X <option>`, merge-ort's `parse_merge_opt`): the side
3
+ * conflicting hunks are resolved to, the subtree shift, renormalization, and what tunes the
4
+ * diffs of the merge (algorithm, whitespace, rename detection).
5
+ *
6
+ * ```ts
7
+ * const options = parseStrategyOptions(["ignore-space-change", "find-renames=40%"]);
8
+ * ```
9
+ */
10
+
11
+ import {
12
+ XDF_IGNORE_CR_AT_EOL,
13
+ XDF_IGNORE_WHITESPACE,
14
+ XDF_IGNORE_WHITESPACE_AT_EOL,
15
+ XDF_IGNORE_WHITESPACE_CHANGE,
16
+ type XdiffAlgorithm,
17
+ } from "@statewalker/vcs-utils";
18
+ import type { WorkingCopy } from "@statewalker/vcs-working-tree";
19
+
20
+ import { GitApiError } from "../errors/git-api-error.js";
21
+ import { GIT_MAX_SCORE } from "../rename/index.js";
22
+ import { configValue } from "./merge-config.js";
23
+ import type { OrtMergeOptions } from "./ort-merge.js";
24
+
25
+ /** What the `-X` options of a merge set (merge-ort's merge_options, as parse_merge_opt fills it). */
26
+ export interface MergeStrategyOptions {
27
+ /** `ours` / `theirs`: conflicting hunks resolved to that side (recursive_variant) */
28
+ favor?: "ours" | "theirs";
29
+ /** `subtree` (`""`: the shift detected) or `subtree=<path>` (subtree_shift) */
30
+ subtreeShift?: string;
31
+ /** `patience`, `histogram`, `diff-algorithm=<name>` (the xdl_opts algorithm bits) */
32
+ algorithm?: "myers" | "patience" | "histogram";
33
+ /** `diff-algorithm=minimal` (XDF_NEED_MINIMAL: kept until another algorithm clears it) */
34
+ minimal?: boolean;
35
+ /** `ignore-space-change`, `ignore-all-space`, `ignore-space-at-eol`, `ignore-cr-at-eol` */
36
+ whitespace?: number;
37
+ /** `renormalize` / `no-renormalize` */
38
+ renormalize?: boolean;
39
+ /** `no-renames` (false), `find-renames[=<n>]` / `rename-threshold=<n>` (true) */
40
+ renames?: boolean;
41
+ /** The rename score of `find-renames=<n>` (0..60000; 0: the default 50%) */
42
+ renameScore?: number;
43
+ }
44
+
45
+ /** `fatal: unknown strategy option: -X<option>` (exit 128): git merge's death. */
46
+ export class UnknownStrategyOptionError extends GitApiError {
47
+ readonly option: string;
48
+
49
+ constructor(option: string) {
50
+ super(`fatal: unknown strategy option: -X${option}`, { exitCode: 128 });
51
+ this.name = "UnknownStrategyOptionError";
52
+ this.option = option;
53
+ }
54
+ }
55
+
56
+ /** diff.c's parse_algorithm_value: the algorithm of a name, undefined when unknown. */
57
+ export function parseDiffAlgorithm(
58
+ value: string,
59
+ ): "myers" | "minimal" | "patience" | "histogram" | undefined {
60
+ const v = value.toLowerCase();
61
+ if (v === "myers" || v === "default") return "myers";
62
+ if (v === "minimal" || v === "patience" || v === "histogram") return v;
63
+ return undefined;
64
+ }
65
+
66
+ /**
67
+ * diff.c's parse_rename_score: `50`, `50%`, `.5`, `0.5` as a score of {@link GIT_MAX_SCORE};
68
+ * with the rest of the text not read (`rest`), as git checks it.
69
+ */
70
+ export function parseRenameScore(text: string): { score: number; rest: string } {
71
+ let num = 0;
72
+ let scale = 1;
73
+ let dot = false;
74
+ let i = 0;
75
+ for (; i < text.length; i++) {
76
+ const ch = text[i];
77
+ if (!dot && ch === ".") {
78
+ scale = 1;
79
+ dot = true;
80
+ } else if (ch === "%") {
81
+ scale = dot ? scale * 100 : 100;
82
+ i++;
83
+ break;
84
+ } else if (ch >= "0" && ch <= "9") {
85
+ if (scale < 100000) {
86
+ scale *= 10;
87
+ num = num * 10 + (ch.charCodeAt(0) - 48);
88
+ }
89
+ } else {
90
+ break;
91
+ }
92
+ }
93
+ const score = num >= scale ? GIT_MAX_SCORE : Math.floor((GIT_MAX_SCORE * num) / scale);
94
+ return { score, rest: text.slice(i) };
95
+ }
96
+
97
+ /**
98
+ * merge-ort's parse_merge_opt: applies one `-X` option to `options`; false for an option git
99
+ * does not know (or a value it refuses), leaving `options` as it was.
100
+ */
101
+ export function applyStrategyOption(options: MergeStrategyOptions, s: string): boolean {
102
+ if (!s) return false;
103
+ const after = (prefix: string) => (s.startsWith(prefix) ? s.slice(prefix.length) : undefined);
104
+ const whitespace: Record<string, number> = {
105
+ "ignore-space-change": XDF_IGNORE_WHITESPACE_CHANGE,
106
+ "ignore-all-space": XDF_IGNORE_WHITESPACE,
107
+ "ignore-space-at-eol": XDF_IGNORE_WHITESPACE_AT_EOL,
108
+ "ignore-cr-at-eol": XDF_IGNORE_CR_AT_EOL,
109
+ };
110
+ const subtree = after("subtree=");
111
+ const algorithm = after("diff-algorithm=");
112
+ const threshold = after("find-renames=") ?? after("rename-threshold=");
113
+ if (s === "ours" || s === "theirs") options.favor = s;
114
+ else if (s === "subtree") options.subtreeShift = "";
115
+ else if (subtree !== undefined) options.subtreeShift = subtree;
116
+ else if (s === "patience" || s === "histogram") options.algorithm = s;
117
+ else if (algorithm !== undefined) {
118
+ const value = parseDiffAlgorithm(algorithm);
119
+ if (value === undefined) return false;
120
+ // the algorithm bits cleared, XDF_NEED_MINIMAL added (never removed)
121
+ if (value === "minimal") {
122
+ options.algorithm = "myers";
123
+ options.minimal = true;
124
+ } else {
125
+ options.algorithm = value;
126
+ }
127
+ } else if (whitespace[s] !== undefined) {
128
+ options.whitespace = (options.whitespace ?? 0) | whitespace[s];
129
+ } else if (s === "renormalize") options.renormalize = true;
130
+ else if (s === "no-renormalize") options.renormalize = false;
131
+ else if (s === "no-renames") options.renames = false;
132
+ else if (s === "find-renames") {
133
+ options.renames = true;
134
+ options.renameScore = 0;
135
+ } else if (threshold !== undefined) {
136
+ const { score, rest } = parseRenameScore(threshold);
137
+ if (rest !== "") return false;
138
+ options.renameScore = score;
139
+ options.renames = true;
140
+ } else {
141
+ return false;
142
+ }
143
+ return true;
144
+ }
145
+
146
+ /**
147
+ * The `-X` options of a merge, in order (the last of each kind wins); an unknown one throws
148
+ * {@link UnknownStrategyOptionError}, as `git merge` dies, unless `lenient` (the sequencer's
149
+ * do_recursive_merge, which ignores parse_merge_opt's answer).
150
+ */
151
+ export function parseStrategyOptions(
152
+ list: readonly string[],
153
+ lenient = false,
154
+ ): MergeStrategyOptions {
155
+ const options: MergeStrategyOptions = {};
156
+ for (const option of list) {
157
+ if (!applyStrategyOption(options, option) && !lenient) {
158
+ throw new UnknownStrategyOptionError(option);
159
+ }
160
+ }
161
+ return options;
162
+ }
163
+
164
+ /**
165
+ * What the `-X` options make of merge-ort's diff options, over a merge's own: the algorithm
166
+ * (the option, else `diff.algorithm` as init_ui_merge_options reads it, else histogram), the
167
+ * whitespace bits, rename detection and its score. The favored side, renormalization and
168
+ * the subtree shift are the caller's to apply.
169
+ */
170
+ export function strategyOrtOptions(
171
+ workingCopy: WorkingCopy | undefined,
172
+ options: MergeStrategyOptions,
173
+ ): Partial<OrtMergeOptions> {
174
+ const out: Partial<OrtMergeOptions> = {};
175
+ let algorithm: XdiffAlgorithm | undefined;
176
+ if (options.algorithm) algorithm = options.algorithm;
177
+ else if (workingCopy) {
178
+ const configured = configValue(workingCopy, "diff.algorithm");
179
+ const value = configured !== undefined ? parseDiffAlgorithm(configured) : undefined;
180
+ if (value) algorithm = value;
181
+ }
182
+ if (options.minimal && (algorithm ?? "histogram") === "myers") algorithm = "minimal";
183
+ if (algorithm && algorithm !== "histogram") out.algorithm = algorithm;
184
+ if (options.whitespace) out.whitespace = options.whitespace;
185
+ if (options.renames !== undefined) out.renames = options.renames;
186
+ if (options.renameScore) out.renameScore = options.renameScore;
187
+ return out;
188
+ }
189
+
190
+ /** quote.c's quote_cmdline of `-X` options as rebase saves them (`rebase-merge/strategy_opts`). */
191
+ export function quoteStrategyOptions(list: readonly string[]): string {
192
+ return list.map((arg) => `"${arg.replace(/["\\]/g, (c) => `\\${c}`)}"`).join(" ");
193
+ }
194
+
195
+ /** split_cmdline of `rebase-merge/strategy_opts`: the options it holds, quotes removed. */
196
+ export function splitStrategyOptions(text: string): string[] {
197
+ const out: string[] = [];
198
+ let current = "";
199
+ let quote = "";
200
+ let started = false;
201
+ for (let i = 0; i < text.length; i++) {
202
+ const c = text[i];
203
+ if (quote) {
204
+ if (c === quote) quote = "";
205
+ else if (c === "\\" && quote === '"' && i + 1 < text.length) current += text[++i];
206
+ else current += c;
207
+ } else if (c === '"' || c === "'") {
208
+ quote = c;
209
+ started = true;
210
+ } else if (c === "\\" && i + 1 < text.length) {
211
+ current += text[++i];
212
+ started = true;
213
+ } else if (/\s/.test(c)) {
214
+ if (started) out.push(current);
215
+ current = "";
216
+ started = false;
217
+ } else {
218
+ current += c;
219
+ started = true;
220
+ }
221
+ }
222
+ if (started) out.push(current);
223
+ return out;
224
+ }