@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
@@ -0,0 +1,433 @@
1
+ /**
2
+ * `git range-diff` (builtin/range-diff.c and range-diff.c): two versions of a patch series
3
+ * compared commit by commit.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * // git range-diff main..topic main..topic-v2
8
+ * const text = await git.rangeDiff().addArgument("main..topic").addArgument("main..topic-v2").callText();
9
+ * // "1: 4de457d = 1: a4b3333 s/5/A/\n2: fccce22 ! 2: f51d370 s/4/A/\n @@ ..."
10
+ * // git range-diff topic...topic-v2, and git range-diff main topic topic-v2, the same
11
+ * ```
12
+ *
13
+ * The arguments are git's: `<base> <old-tip> <new-tip>`, `<old-range> <new-range>` or
14
+ * `<old-tip>...<new-tip>` (either side defaulting to HEAD), each form recognized as git
15
+ * recognizes it, a `--` among them saying which one is meant; what follows the ranges (paths
16
+ * after `--`) goes to the `git log` that reads each range.
17
+ */
18
+
19
+ import type { ObjectId } from "@statewalker/vcs-core";
20
+
21
+ import { GitApiError } from "../../errors/index.js";
22
+ import { GitCommand } from "../../git-command.js";
23
+ import { bytesToLatin1, latin1ToBytes } from "../apply/latin1.js";
24
+ import { readDiffColorSettings } from "../diff-color-config.js";
25
+ import type { LogOutputOptions } from "../log-output.js";
26
+ import { formatCommitPretty } from "../pretty-format.js";
27
+ import { RevListCommand } from "../rev-list-command.js";
28
+ import {
29
+ findExactMatches,
30
+ getCorrespondences,
31
+ parseRangeDiffMaxMemory,
32
+ RANGE_DIFF_CREATION_FACTOR_DEFAULT,
33
+ RangeDiffError,
34
+ RangeDiffMemoryError,
35
+ type RangeDiffPatch,
36
+ readRangeDiffPatches,
37
+ writeRangeDiff,
38
+ } from "./range-diff.js";
39
+
40
+ const USAGE = [
41
+ "usage: git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>",
42
+ " or: git range-diff [<options>] <old-tip>...<new-tip>",
43
+ " or: git range-diff [<options>] <base> <old-tip> <new-tip>",
44
+ ].join("\n");
45
+
46
+ /** git's MINIMUM_ABBREV */
47
+ const MINIMUM_ABBREV = 4;
48
+
49
+ /** `--notes`, `--notes=<ref>` and `--no-notes` as given: true, the ref, false. */
50
+ export type RangeDiffNotesOption = boolean | string;
51
+
52
+ /**
53
+ * What the `git log` reading a range is told of notes (`--show-notes-by-default` and the
54
+ * notes options after it, revision.c's display_notes_opt).
55
+ */
56
+ export function rangeDiffNotesOptions(
57
+ given: readonly RangeDiffNotesOption[],
58
+ ): Pick<LogOutputOptions, "notes" | "notesRefs"> {
59
+ let show = false;
60
+ let useDefault = -1;
61
+ let refs: string[] = [];
62
+ for (const value of given) {
63
+ if (value === true) {
64
+ show = true;
65
+ useDefault = 1;
66
+ } else if (value === false) {
67
+ show = false;
68
+ useDefault = -1;
69
+ refs = [];
70
+ } else {
71
+ show = true;
72
+ refs.push(value);
73
+ }
74
+ }
75
+ // --show-notes-by-default: the default notes when no notes option was given
76
+ if (given.length === 0) return { notes: true };
77
+ if (!show) return { notes: false };
78
+ if (useDefault > 0 || (useDefault === -1 && refs.length === 0))
79
+ return { notes: true, notesRefs: refs };
80
+ return { notesRefs: refs };
81
+ }
82
+
83
+ /** What reads the patches of a range and prints the pairs (shared with format-patch). */
84
+ export interface RangeDiffRun {
85
+ creationFactor: number;
86
+ /** `--notes`... passed to the log reading each range */
87
+ notes: readonly RangeDiffNotesOption[];
88
+ /** More arguments of that log (paths after `--`) */
89
+ logArgs?: readonly string[];
90
+ dualColor: boolean;
91
+ /** The color of the output (`--color`): undefined reads the configuration */
92
+ color?: boolean;
93
+ leftOnly?: boolean;
94
+ rightOnly?: boolean;
95
+ patch?: boolean;
96
+ stat?: boolean;
97
+ noPatch?: boolean;
98
+ /** `--abbrev=<n>` (undefined: the default length) */
99
+ abbrev?: number;
100
+ /** `--max-memory`: the largest cost matrix in bytes (default 4 GiB) */
101
+ maxMemory?: bigint;
102
+ }
103
+
104
+ /**
105
+ * The base of commands printing range-diffs (`show_range_diff`): `git range-diff` and
106
+ * format-patch's `--range-diff`.
107
+ */
108
+ export abstract class RangeDiffBase<T> extends GitCommand<T> {
109
+ /** `show_range_diff`: the output for two ranges, as bytes. */
110
+ protected async showRangeDiff(
111
+ range1: string,
112
+ range2: string,
113
+ run: RangeDiffRun,
114
+ ): Promise<Uint8Array> {
115
+ if (run.leftOnly && run.rightOnly) {
116
+ throw new GitApiError(
117
+ "error: options '--left-only' and '--right-only' cannot be used together",
118
+ {
119
+ exitCode: 255,
120
+ },
121
+ );
122
+ }
123
+ const a = await this.readPatches(range1, run);
124
+ const b = await this.readPatches(range2, run);
125
+ findExactMatches(a, b);
126
+ try {
127
+ getCorrespondences(a, b, run.creationFactor, run.maxMemory);
128
+ } catch (error) {
129
+ if (error instanceof RangeDiffMemoryError)
130
+ throw new GitApiError(`fatal: ${error.message}`, { exitCode: 128 });
131
+ throw error;
132
+ }
133
+ const colors =
134
+ run.color === false
135
+ ? undefined
136
+ : readDiffColorSettings(this.workingCopy, run.color ? { color: true } : {}).colors;
137
+ const text = await writeRangeDiff(a, b, {
138
+ ...(colors ? { colors } : {}),
139
+ dualColor: run.dualColor,
140
+ leftOnly: !!run.leftOnly,
141
+ rightOnly: !!run.rightOnly,
142
+ patch: !!run.patch,
143
+ stat: !!run.stat,
144
+ noPatch: !!run.noPatch,
145
+ abbrev: (id) => this.shortId(id, run.abbrev),
146
+ subject: (id) => this.subjectOf(id),
147
+ });
148
+ return latin1ToBytes(text);
149
+ }
150
+
151
+ private async readPatches(range: string, run: RangeDiffRun): Promise<RangeDiffPatch[]> {
152
+ const log = new RevListCommand(this._workingCopy)
153
+ .setNoMerges(true)
154
+ .setOrder("date")
155
+ .setReverse(true)
156
+ .setDiffOptions({ color: false, noPrefix: true, submoduleFormat: "short" });
157
+ log.addArgument(range);
158
+ for (const arg of run.logArgs ?? []) log.addArgument(arg);
159
+ let bytes: Uint8Array;
160
+ try {
161
+ bytes = await log.callLogBytes({
162
+ patch: true,
163
+ pretty: "medium",
164
+ decorate: "no",
165
+ abbrevCommit: false,
166
+ ...rangeDiffNotesOptions(run.notes),
167
+ });
168
+ } catch (error) {
169
+ const message = error instanceof Error ? error.message : String(error);
170
+ throw new GitApiError(`${message}\nerror: could not parse log for '${range}'`, {
171
+ exitCode: 255,
172
+ });
173
+ }
174
+ try {
175
+ return readRangeDiffPatches(bytesToLatin1(bytes));
176
+ } catch (error) {
177
+ if (error instanceof RangeDiffError)
178
+ throw new GitApiError(
179
+ `error: ${error.message}\nerror: could not parse log for '${range}'`,
180
+ {
181
+ exitCode: 255,
182
+ },
183
+ );
184
+ throw error;
185
+ }
186
+ }
187
+
188
+ /** `find_unique_abbrev(oid, abbrev)` */
189
+ private async shortId(id: ObjectId, abbrev: number | undefined): Promise<string> {
190
+ if (abbrev === undefined) return this.abbreviateObjectId(id);
191
+ if (abbrev >= id.length) return id;
192
+ const lengths = await this.idAbbreviator().lengths([id], abbrev);
193
+ return id.slice(0, lengths.get(id) ?? abbrev);
194
+ }
195
+
196
+ /** `pp_commit_easy(CMIT_FMT_ONELINE)` as a binary string. */
197
+ private async subjectOf(id: ObjectId): Promise<string> {
198
+ const commit = await this.commits.load(id);
199
+ if (!commit) return "";
200
+ const subject = await formatCommitPretty("%s", commit, id);
201
+ return bytesToLatin1(new TextEncoder().encode(subject));
202
+ }
203
+
204
+ /** `is_range_diff_range`: a revision argument with both a positive and a negative side. */
205
+ protected async isRangeDiffRange(arg: string): Promise<boolean> {
206
+ let entries: { id: ObjectId; exclude?: boolean }[];
207
+ try {
208
+ ({ entries } = await new RevListCommand(this._workingCopy).addRevision(arg).resolve());
209
+ } catch {
210
+ // setup_revisions with a `--` after the argument: what is no revision dies
211
+ throw new GitApiError(`fatal: bad revision '${arg}'`, { exitCode: 128 });
212
+ }
213
+ // the flags are the objects': one named on both sides (`main..main`) is only negative
214
+ const negative = new Set(entries.filter((e) => e.exclude).map((e) => e.id));
215
+ return negative.size > 0 && entries.some((e) => !negative.has(e.id));
216
+ }
217
+
218
+ /** `repo_get_oid_committish` succeeds */
219
+ protected async isCommittish(arg: string): Promise<boolean> {
220
+ const resolver = this.revisionResolver();
221
+ const id = await resolver.resolve(arg).catch(() => undefined);
222
+ if (id === undefined) return false;
223
+ return (await resolver.commitReference(id).catch(() => undefined)) !== undefined;
224
+ }
225
+ }
226
+
227
+ /** `git range-diff`. */
228
+ export class RangeDiffCommand extends RangeDiffBase<Uint8Array> {
229
+ private readonly args: string[] = [];
230
+ private creationFactor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
231
+ /** `--no-dual-color` (1), `--dual-color` (0), neither (-1) */
232
+ private simpleColor = -1;
233
+ private color?: boolean;
234
+ private leftOnly = false;
235
+ private rightOnly = false;
236
+ private readonly notes: RangeDiffNotesOption[] = [];
237
+ private noPatch = false;
238
+ private stat = false;
239
+ private patch = false;
240
+ private abbrev?: number;
241
+ private maxMemory?: string | bigint;
242
+
243
+ /**
244
+ * A positional argument as git takes it: a range, a commit, `--`, or what follows the
245
+ * ranges (paths after `--`).
246
+ */
247
+ addArgument(arg: string): this {
248
+ this.checkCallable();
249
+ this.args.push(arg);
250
+ return this;
251
+ }
252
+
253
+ /** A path the ranges' logs are limited to (after a `--`, added when not there yet). */
254
+ addPath(path: string): this {
255
+ this.checkCallable();
256
+ if (!this.args.includes("--")) this.args.push("--");
257
+ this.args.push(path);
258
+ return this;
259
+ }
260
+
261
+ /** `--creation-factor=<percent>` (default 60). */
262
+ setCreationFactor(factor: number): this {
263
+ this.checkCallable();
264
+ this.creationFactor = factor;
265
+ return this;
266
+ }
267
+
268
+ /** `--dual-color` (true: also forces colors) / `--no-dual-color` (false). */
269
+ setDualColor(dual: boolean): this {
270
+ this.checkCallable();
271
+ this.simpleColor = dual ? 0 : 1;
272
+ return this;
273
+ }
274
+
275
+ /** `--color` / `--no-color` (unset: `color.diff`/`color.ui`, `auto` is none). */
276
+ setColor(color: boolean): this {
277
+ this.checkCallable();
278
+ this.color = color;
279
+ return this;
280
+ }
281
+
282
+ /** `--left-only`: only what concerns the first range. */
283
+ setLeftOnly(value: boolean): this {
284
+ this.checkCallable();
285
+ this.leftOnly = value;
286
+ return this;
287
+ }
288
+
289
+ /** `--right-only`: only what concerns the second range. */
290
+ setRightOnly(value: boolean): this {
291
+ this.checkCallable();
292
+ this.rightOnly = value;
293
+ return this;
294
+ }
295
+
296
+ /** `--notes` (true), `--notes=<ref>`, `--no-notes` (false), in order. */
297
+ addNotes(value: RangeDiffNotesOption): this {
298
+ this.checkCallable();
299
+ this.notes.push(value);
300
+ return this;
301
+ }
302
+
303
+ /** `-s` / `--no-patch`: only the pairs. */
304
+ setNoPatch(value: boolean): this {
305
+ this.checkCallable();
306
+ this.noPatch = value;
307
+ return this;
308
+ }
309
+
310
+ /** `--stat`: the diffstat of each changed pair's patches. */
311
+ setStat(value: boolean): this {
312
+ this.checkCallable();
313
+ this.stat = value;
314
+ return this;
315
+ }
316
+
317
+ /** `-p`: the patches' diff too (with `--stat`). */
318
+ setPatch(value: boolean): this {
319
+ this.checkCallable();
320
+ this.patch = value;
321
+ return this;
322
+ }
323
+
324
+ /** `--abbrev[=<n>]` of the commit ids (true: the default length). */
325
+ setAbbrev(abbrev: number | true): this {
326
+ this.checkCallable();
327
+ this.abbrev = abbrev === true ? undefined : Math.max(abbrev, MINIMUM_ABBREV);
328
+ return this;
329
+ }
330
+
331
+ /**
332
+ * `--max-memory=<size>` (git 2.52): the largest cost matrix built, in bytes or with a `k`,
333
+ * `m` or `g` unit (default 4 GiB, which ours applies whatever the version). A value git
334
+ * refuses fails the call with its `invalid max-memory value` and the usage (exit 129).
335
+ */
336
+ setMaxMemory(size: string | number | bigint): this {
337
+ this.checkCallable();
338
+ this.maxMemory = typeof size === "number" ? BigInt(size) : size;
339
+ return this;
340
+ }
341
+
342
+ /** The output as bytes. */
343
+ async call(): Promise<Uint8Array> {
344
+ this.checkCallable();
345
+ this.setCallable(false);
346
+ let maxMemory: bigint | undefined;
347
+ if (typeof this.maxMemory === "string") {
348
+ maxMemory = parseRangeDiffMaxMemory(this.maxMemory);
349
+ if (maxMemory === undefined)
350
+ throw new GitApiError(`error: invalid max-memory value: ${this.maxMemory}\n\n${USAGE}`, {
351
+ exitCode: 129,
352
+ });
353
+ } else maxMemory = this.maxMemory;
354
+ const { range1, range2, logArgs } = await this.ranges();
355
+ const color = this.simpleColor === 0 ? true : this.color;
356
+ return this.showRangeDiff(range1, range2, {
357
+ creationFactor: this.creationFactor,
358
+ notes: this.notes,
359
+ logArgs,
360
+ dualColor: this.simpleColor < 1,
361
+ ...(color !== undefined ? { color } : {}),
362
+ leftOnly: this.leftOnly,
363
+ rightOnly: this.rightOnly,
364
+ patch: this.patch,
365
+ stat: this.stat,
366
+ noPatch: this.noPatch,
367
+ ...(this.abbrev !== undefined ? { abbrev: this.abbrev } : {}),
368
+ ...(maxMemory !== undefined ? { maxMemory } : {}),
369
+ });
370
+ }
371
+
372
+ /** {@link call} as bytes. */
373
+ callBytes(): Promise<Uint8Array> {
374
+ return this.call();
375
+ }
376
+
377
+ /** {@link call} as text (UTF-8). */
378
+ async callText(): Promise<string> {
379
+ return new TextDecoder().decode(await this.call());
380
+ }
381
+
382
+ /** cmd_range_diff's reading of the arguments. */
383
+ private async ranges(): Promise<{ range1: string; range2: string; logArgs: string[] }> {
384
+ const argv = this.args;
385
+ const dashDash = argv.indexOf("--");
386
+ const usage = (message: string) =>
387
+ new GitApiError(`fatal: ${message}\n\n${USAGE}`, { exitCode: 129 });
388
+ const argc = argv.length;
389
+ const arg = (k: number) => argv[k] as string;
390
+ if (
391
+ dashDash === 3 ||
392
+ (dashDash < 0 &&
393
+ argc > 2 &&
394
+ (await this.isCommittish(arg(0))) &&
395
+ (await this.isCommittish(arg(1))) &&
396
+ (await this.isCommittish(arg(2))))
397
+ ) {
398
+ if (dashDash >= 0)
399
+ for (const k of [0, 1, 2])
400
+ if (!(await this.isCommittish(arg(k)))) throw usage(`not a revision: '${arg(k)}'`);
401
+ return {
402
+ range1: `${arg(0)}..${arg(1)}`,
403
+ range2: `${arg(0)}..${arg(2)}`,
404
+ logArgs: argv.slice(dashDash < 0 ? 3 : dashDash),
405
+ };
406
+ }
407
+ if (
408
+ dashDash === 2 ||
409
+ (dashDash < 0 &&
410
+ argc > 1 &&
411
+ (await this.isRangeDiffRange(arg(0))) &&
412
+ (await this.isRangeDiffRange(arg(1))))
413
+ ) {
414
+ if (dashDash >= 0)
415
+ for (const k of [0, 1])
416
+ if (!(await this.isRangeDiffRange(arg(k))))
417
+ throw usage(`not a commit range: '${arg(k)}'`);
418
+ return { range1: arg(0), range2: arg(1), logArgs: argv.slice(dashDash < 0 ? 2 : dashDash) };
419
+ }
420
+ if (dashDash === 1 || (dashDash < 0 && argc > 0 && arg(0).includes("..."))) {
421
+ const dots = arg(0).indexOf("...");
422
+ if (dots < 0) throw usage(`not a symmetric range: '${arg(0)}'`);
423
+ const a = dots === 0 ? "HEAD" : arg(0).slice(0, dots);
424
+ const b = arg(0).slice(dots + 3) || "HEAD";
425
+ return {
426
+ range1: `${b}..${a}`,
427
+ range2: `${a}..${b}`,
428
+ logArgs: argv.slice(dashDash < 0 ? 1 : dashDash),
429
+ };
430
+ }
431
+ throw usage("need two commit ranges");
432
+ }
433
+ }