@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,166 @@
1
+ /**
2
+ * git's break-rewrite detection (`diffcore-break.c`) and the option parsing of `-M`, `-C`
3
+ * and `-B` (diff.c's `parse_rename_score`, `diff_opt_break_rewrites`).
4
+ */
5
+
6
+ import { InvalidArgumentError } from "../errors/index.js";
7
+ import { GIT_MAX_SCORE, type SimilaritySide, type SpanCounts } from "./git-similarity.js";
8
+
9
+ /** git's `DEFAULT_BREAK_SCORE`: a pair is broken from 50% of edit on */
10
+ export const DEFAULT_BREAK_SCORE = 30000;
11
+ /** git's `DEFAULT_MERGE_SCORE`: a broken pair that removed less than 60% is merged back */
12
+ export const DEFAULT_MERGE_SCORE = 36000;
13
+ /** git's `MINIMUM_BREAK_SIZE`: smaller pairs are never broken */
14
+ export const MINIMUM_BREAK_SIZE = 400;
15
+
16
+ /**
17
+ * diff.c's `parse_rename_score`: `50%`, `5` (tenths, as `.5`), `.5`, `95.5%`; the score in
18
+ * {@link GIT_MAX_SCORE} units and the text left after it.
19
+ */
20
+ export function parseRenameScore(text: string): { score: number; rest: string } {
21
+ let num = 0;
22
+ let scale = 1;
23
+ let dot = false;
24
+ let at = 0;
25
+ for (; at < text.length; at++) {
26
+ const ch = text[at] as string;
27
+ if (!dot && ch === ".") {
28
+ scale = 1;
29
+ dot = true;
30
+ } else if (ch === "%") {
31
+ scale = dot ? scale * 100 : 100;
32
+ at++;
33
+ break;
34
+ } else if (ch >= "0" && ch <= "9") {
35
+ if (scale < 100000) {
36
+ scale *= 10;
37
+ num = num * 10 + (ch.charCodeAt(0) - 48);
38
+ }
39
+ } else break;
40
+ }
41
+ const score = num >= scale ? GIT_MAX_SCORE : Math.trunc((GIT_MAX_SCORE * num) / scale);
42
+ return { score, rest: text.slice(at) };
43
+ }
44
+
45
+ /**
46
+ * The score of `-M<n>` / `-C<n>` (`true`: none given, the default threshold); git's
47
+ * `error: invalid argument to <option>` (exit 129) for trailing text.
48
+ */
49
+ export function renameScoreOption(
50
+ spec: boolean | string,
51
+ option: "find-renames" | "find-copies",
52
+ ): number | undefined {
53
+ if (spec === true || spec === false || spec === "") return undefined;
54
+ const { score, rest } = parseRenameScore(spec);
55
+ if (rest !== "") {
56
+ throw new InvalidArgumentError(
57
+ option,
58
+ spec,
59
+ `error: invalid argument to ${option}`,
60
+ ).withExitCode(129);
61
+ }
62
+ return score || undefined;
63
+ }
64
+
65
+ /** The two thresholds of `-B[<n>][/<m>]` */
66
+ export interface BreakScores {
67
+ /** Break a pair edited this much (git's `break_score`) */
68
+ breakScore: number;
69
+ /** Merge a broken pair back that removed less than this (git's `merge_score`) */
70
+ mergeScore: number;
71
+ }
72
+
73
+ /** diff.c's `diff_opt_break_rewrites` and diffcore_break's defaults */
74
+ export function parseBreakRewrites(spec: boolean | string): BreakScores {
75
+ let text = spec === true || spec === false ? "" : spec;
76
+ const first = parseRenameScore(text);
77
+ text = first.rest;
78
+ let second = 0;
79
+ const invalid = () =>
80
+ new InvalidArgumentError(
81
+ "break-rewrites",
82
+ String(spec),
83
+ "error: break-rewrites expects <n>/<m> form",
84
+ ).withExitCode(129);
85
+ if (text !== "") {
86
+ if (text[0] !== "/") throw invalid();
87
+ const parsed = parseRenameScore(text.slice(1));
88
+ second = parsed.score;
89
+ if (parsed.rest !== "") throw invalid();
90
+ }
91
+ // diffcore_break keeps both in 16 bits; 0 takes the default
92
+ const breakScore = first.score & 0xffff;
93
+ const mergeScore = second & 0xffff;
94
+ return {
95
+ breakScore: breakScore || DEFAULT_BREAK_SCORE,
96
+ mergeScore: mergeScore || DEFAULT_MERGE_SCORE,
97
+ };
98
+ }
99
+
100
+ /**
101
+ * diffcore-delta.c's `diffcore_count_changes`: the bytes of the destination the source has
102
+ * (`copied`) and the bytes it adds (`added`).
103
+ */
104
+ export function countChanges(src: SpanCounts, dst: SpanCounts): { copied: number; added: number } {
105
+ let copied = 0;
106
+ let added = 0;
107
+ let d = 0;
108
+ for (const [hash, srcCount] of src) {
109
+ while (d < dst.length && (dst[d] as [number, number])[0] < hash) {
110
+ added += (dst[d] as [number, number])[1];
111
+ d++;
112
+ }
113
+ let dstCount = 0;
114
+ if (d < dst.length && (dst[d] as [number, number])[0] === hash) {
115
+ dstCount = (dst[d] as [number, number])[1];
116
+ d++;
117
+ }
118
+ if (srcCount < dstCount) {
119
+ added += dstCount - srcCount;
120
+ copied += srcCount;
121
+ } else copied += dstCount;
122
+ }
123
+ for (; d < dst.length; d++) added += (dst[d] as [number, number])[1];
124
+ return { copied, added };
125
+ }
126
+
127
+ const S_IFMT = 0o170000;
128
+ const isRegular = (mode: number) => (mode & S_IFMT) === 0o100000;
129
+
130
+ /**
131
+ * diffcore-break.c's `should_break`: whether a modification is better shown as a deletion and
132
+ * a creation, and how much of the source it removed (`mergeScore`, git's `*merge_score_p`).
133
+ * `same` is whether both sides are known to be the same object.
134
+ */
135
+ export async function shouldBreak(
136
+ src: SimilaritySide,
137
+ dst: SimilaritySide,
138
+ breakScore: number,
139
+ same: boolean,
140
+ ): Promise<{ broken: boolean; mergeScore: number }> {
141
+ if (isRegular(src.mode) !== isRegular(dst.mode)) {
142
+ return { broken: true, mergeScore: GIT_MAX_SCORE };
143
+ }
144
+ if (same) return { broken: false, mergeScore: 0 };
145
+ const maxSize = Math.max(src.size, dst.size);
146
+ if (maxSize < MINIMUM_BREAK_SIZE) return { broken: false, mergeScore: 0 };
147
+ if (!src.size) return { broken: false, mergeScore: 0 };
148
+ let { copied, added } = countChanges(await src.spans(), await dst.spans());
149
+ if (src.size < copied) copied = src.size;
150
+ if (dst.size < added + copied) added = copied < dst.size ? dst.size - copied : 0;
151
+ const removed = src.size - copied;
152
+ const mergeScore = Math.trunc((removed * GIT_MAX_SCORE) / src.size);
153
+ if (mergeScore > breakScore) return { broken: true, mergeScore };
154
+ const delta = removed + added;
155
+ if ((delta * GIT_MAX_SCORE) / maxSize < breakScore) {
156
+ return { broken: false, mergeScore };
157
+ }
158
+ if (
159
+ src.size * breakScore < removed * GIT_MAX_SCORE &&
160
+ added * 20 < removed &&
161
+ added * 20 < copied
162
+ ) {
163
+ return { broken: false, mergeScore };
164
+ }
165
+ return { broken: true, mergeScore };
166
+ }
@@ -3,5 +3,6 @@
3
3
  */
4
4
 
5
5
  export * from "./detect-renames.js";
6
+ export * from "./diffcore-break.js";
6
7
  export * from "./git-similarity.js";
7
8
  export { DEFAULT_RENAME_SCORE, MAX_SCORE, SimilarityIndex } from "./similarity-index.js";
@@ -12,9 +12,16 @@
12
12
  * @module
13
13
  */
14
14
 
15
- import { ByteSink } from "@statewalker/vcs-utils";
15
+ import { approxidateCareful, ByteSink } from "@statewalker/vcs-utils";
16
16
  import { encodeGitPath } from "@statewalker/vcs-utils/encoding";
17
17
  import type { BlameEntry, BlameResult } from "../commands/blame-command.js";
18
+ import { parseGitColor } from "../commands/config-value-types.js";
19
+ import {
20
+ type DateMode,
21
+ parseDateMode,
22
+ type ShowDateContext,
23
+ showDate,
24
+ } from "../commands/date-format.js";
18
25
  import { displayWidth } from "./diff-output.js";
19
26
  import { quotePath } from "./quote-path.js";
20
27
 
@@ -24,6 +31,15 @@ export interface BlamePorcelainOptions {
24
31
  linePorcelain?: boolean;
25
32
  /** `core.quotePath` (default true) */
26
33
  quotePath?: boolean;
34
+ /** An `ignored` line under each ignored line (default the result's `markIgnoredLines`) */
35
+ markIgnoredLines?: boolean;
36
+ /** An `unblamable` line under each (default the result's `markUnblamableLines`) */
37
+ markUnblamableLines?: boolean;
38
+ /**
39
+ * Whether the `ignored`/`unblamable` lines are written at all: git 2.50+ writes them
40
+ * (default true), older git marks only its default output
41
+ */
42
+ lineMarks?: boolean;
27
43
  }
28
44
 
29
45
  /** Options of {@link formatBlame} (git's default output) */
@@ -54,8 +70,50 @@ export interface BlameFormatOptions {
54
70
  * it only when a boundary commit is shown (default false)
55
71
  */
56
72
  alwaysWidenAbbrev?: boolean;
73
+ /** `?` before the ids of ignored lines (default the result's `markIgnoredLines`) */
74
+ markIgnoredLines?: boolean;
75
+ /** `*` before the ids of unblamable lines (default the result's `markUnblamableLines`) */
76
+ markUnblamableLines?: boolean;
77
+ /**
78
+ * git annotate's columns (`-c`): `<id>\t(<author>\t<date>\t<line>)<text>`, no boundary
79
+ * marks and no colors
80
+ */
81
+ annotate?: boolean;
82
+ /**
83
+ * The date mode (`--date`, a `DateMode` or as written; default the result's `blame.date`,
84
+ * else `iso`)
85
+ */
86
+ date?: DateMode | string;
87
+ /** What relative and human dates and `color.blame.highlightRecent` are relative to */
88
+ dateContext?: ShowDateContext;
89
+ /**
90
+ * Color the metadata of a line repeating the previous line's commit (`--color-lines`).
91
+ * With neither this nor `colorByAge`, the result's `blame.coloring` applies.
92
+ */
93
+ colorLines?: boolean;
94
+ /** Color the metadata by the author's date (`--color-by-age`) */
95
+ colorByAge?: boolean;
96
+ /**
97
+ * `--color-lines` repeats only within an entry, not across adjacent entries of one commit
98
+ * (git before 2.54, whose `--ignore-rev` left such entries; default false)
99
+ */
100
+ colorLinesWithinEntries?: boolean;
101
+ /** `color.blame.repeatedLines`'s escape sequence (default the result's, else cyan) */
102
+ repeatedLinesColor?: string;
103
+ /**
104
+ * `color.blame.highlightRecent`: `<color>,<date>,<color>,...` (default the result's, else
105
+ * `blue,12 month ago,white,1 month ago,red`)
106
+ */
107
+ highlightRecent?: string;
57
108
  }
58
109
 
110
+ /** An error git dies with on a bad `color.blame.highlightRecent` */
111
+ export class BlameColorError extends Error {}
112
+
113
+ const GIT_COLOR_RESET = "\x1b[m";
114
+ const GIT_COLOR_CYAN = "\x1b[36m";
115
+ const DEFAULT_HIGHLIGHT_RECENT = "blue,12 month ago,white,1 month ago,red";
116
+
59
117
  /** The commit's subject as `find_commit_subject` finds it: its first non-blank line */
60
118
  function subjectOf(message: string, id: string): string {
61
119
  const lines = message.split("\n");
@@ -79,23 +137,6 @@ function fileLines(content: Uint8Array): Uint8Array[] {
79
137
  return out;
80
138
  }
81
139
 
82
- function tzValue(tz: string): number {
83
- const n = Number.parseInt(tz, 10);
84
- return Number.isNaN(n) ? 0 : n;
85
- }
86
-
87
- /** `show_date` with `DATE_ISO8601`: "2005-04-07 15:13:13 -0700" in the given zone */
88
- function isoDate(time: number, tz: string): string {
89
- const t = tzValue(tz);
90
- const minutes = Math.sign(t) * (Math.floor(Math.abs(t) / 100) * 60 + (Math.abs(t) % 100));
91
- const d = new Date((time + minutes * 60) * 1000);
92
- const p = (n: number, w = 2) => String(n).padStart(w, "0");
93
- const zone = `${t < 0 ? "-" : "+"}${p(Math.abs(t), 4)}`;
94
- return `${p(d.getUTCFullYear(), 4)}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())} ${p(
95
- d.getUTCHours(),
96
- )}:${p(d.getUTCMinutes())}:${p(d.getUTCSeconds())} ${zone}`;
97
- }
98
-
99
140
  /** Commits blamed for more than one path (git's `MORE_THAN_ONE_PATH`) */
100
141
  function multiPathCommits(entries: BlameEntry[]): Set<string> {
101
142
  const paths = new Map<string, Set<string>>();
@@ -119,6 +160,15 @@ export function formatBlamePorcelain(
119
160
  ): Uint8Array {
120
161
  const quote = options.quotePath ?? true;
121
162
  const repeat = options.linePorcelain ?? false;
163
+ const lineMarks = options.lineMarks ?? true;
164
+ const markIgnored = lineMarks && (options.markIgnoredLines ?? result.markIgnoredLines ?? false);
165
+ const markUnblamable =
166
+ lineMarks && (options.markUnblamableLines ?? result.markUnblamableLines ?? false);
167
+ // emit_porcelain_per_line_details
168
+ const perLine = (e: BlameEntry) => {
169
+ if (markUnblamable && e.unblamable) sink.text("unblamable\n");
170
+ if (markIgnored && e.ignored) sink.text("ignored\n");
171
+ };
122
172
  const lines = fileLines(result.content ?? new Uint8Array(0));
123
173
  const multi = multiPathCommits(result.entries);
124
174
  const shown = new Set<string>();
@@ -145,11 +195,13 @@ export function formatBlamePorcelain(
145
195
  for (const e of result.entries) {
146
196
  sink.text(`${e.commitId} ${e.sourceStart} ${e.resultStart} ${e.lineCount}\n`);
147
197
  details(e, repeat);
198
+ perLine(e);
148
199
  let last: Uint8Array | undefined;
149
200
  for (let cnt = 0; cnt < e.lineCount; cnt++) {
150
201
  if (cnt) {
151
202
  sink.text(`${e.commitId} ${e.sourceStart + cnt} ${e.resultStart + cnt}\n`);
152
203
  if (repeat) details(e, true);
204
+ perLine(e);
153
205
  }
154
206
  last = lines[e.resultStart - 1 + cnt] ?? new Uint8Array(0);
155
207
  sink.text("\t");
@@ -160,10 +212,70 @@ export function formatBlamePorcelain(
160
212
  return sink.bytes();
161
213
  }
162
214
 
163
- /** git's default `blame` output */
215
+ /**
216
+ * builtin/blame.c's `parse_color_fields`: the colors and the dates they end at (the last
217
+ * one never ends)
218
+ */
219
+ function parseColorFields(spec: string, now?: number): Array<{ hop: number; color: string }> {
220
+ const fields: Array<{ hop: number; color: string }> = [];
221
+ let color: string | undefined;
222
+ for (const item of spec.split(",")) {
223
+ if (color === undefined) {
224
+ const parsed = parseGitColor(item);
225
+ if (parsed === undefined) throw new BlameColorError(`fatal: expecting a color: ${item}`);
226
+ color = parsed;
227
+ } else {
228
+ fields.push({ hop: approxidateCareful(item, now).timestamp, color });
229
+ color = undefined;
230
+ }
231
+ }
232
+ if (color === undefined) throw new BlameColorError("fatal: must end with a color");
233
+ fields.push({ hop: Number.POSITIVE_INFINITY, color });
234
+ return fields;
235
+ }
236
+
237
+ /** The width git pads dates to (`blame_date_width`) */
238
+ function dateWidth(mode: DateMode): number {
239
+ switch (mode.type) {
240
+ case "rfc2822":
241
+ return "Thu, 19 Oct 2006 16:00:04 -0700".length;
242
+ case "iso8601-strict":
243
+ return "2006-10-19T16:00:04-07:00".length;
244
+ case "iso8601":
245
+ return "2006-10-19 16:00:04 -0700".length;
246
+ case "raw":
247
+ return "1161298804 -0700".length;
248
+ case "unix":
249
+ return "1161298804".length;
250
+ case "short":
251
+ return "2006-10-19".length;
252
+ case "relative":
253
+ return "4 years, 11 months ago".length;
254
+ case "human":
255
+ return "Thu Oct 19 16:00".length;
256
+ case "normal":
257
+ return "Thu Oct 19 16:00:04 2006 -0700".length;
258
+ case "strftime":
259
+ return new TextEncoder().encode(showDate(0, "+0000", mode)).length;
260
+ }
261
+ }
262
+
263
+ /** printf's `%*s`: right-aligned in `width` bytes */
264
+ function padBytes(text: string, width: number): string {
265
+ const n = new TextEncoder().encode(text).length;
266
+ return n >= width ? text : " ".repeat(width - n) + text;
267
+ }
268
+
269
+ /**
270
+ * git's default `blame` output (and `blame -c`, git annotate's columns, with `annotate`)
271
+ *
272
+ * @throws BlameColorError for a `highlightRecent` git cannot parse
273
+ */
164
274
  export function formatBlame(result: BlameResult, options: BlameFormatOptions = {}): Uint8Array {
165
275
  const lines = fileLines(result.content ?? new Uint8Array(0));
166
276
  const entries = result.entries;
277
+ const config = result.output ?? {};
278
+ const annotate = options.annotate ?? false;
167
279
  let showName = options.showName ?? false;
168
280
  let longestFile = 0;
169
281
  let longestAuthor = 0;
@@ -171,10 +283,19 @@ export function formatBlame(result: BlameResult, options: BlameFormatOptions = {
171
283
  let longestDst = 0;
172
284
  let maxMarks = 0;
173
285
  const seen = new Set<string>();
286
+ const showEmail = options.showEmail ?? config.showEmail ?? false;
287
+ const blankBoundary = options.blankBoundary ?? config.blankBoundary ?? false;
174
288
  const authorOf = (e: BlameEntry) =>
175
- options.showEmail ? `<${e.commit.author.email}>` : e.commit.author.name;
289
+ showEmail ? `<${e.commit.author.email}>` : e.commit.author.name;
290
+ const markIgnored = options.markIgnoredLines ?? result.markIgnoredLines ?? false;
291
+ const markUnblamable = options.markUnblamableLines ?? result.markUnblamableLines ?? false;
292
+ /** print_marks: `^` for a boundary (not with -b or -c), `*` unblamable, `?` ignored */
293
+ const marksOf = (e: BlameEntry) =>
294
+ (e.boundary && !blankBoundary && !annotate ? "^" : "") +
295
+ (markUnblamable && e.unblamable ? "*" : "") +
296
+ (markIgnored && e.ignored ? "?" : "");
176
297
  for (const e of entries) {
177
- const marks = e.boundary && !options.blankBoundary ? 1 : 0;
298
+ const marks = marksOf(e).length;
178
299
  if (maxMarks < marks) maxMarks = marks;
179
300
  if (e.sourcePath !== result.path) showName = true;
180
301
  const len = encodeGitPath(e.sourcePath).length;
@@ -199,41 +320,145 @@ export function formatBlame(result: BlameResult, options: BlameFormatOptions = {
199
320
  abbrev += 1;
200
321
  }
201
322
 
323
+ const dateOption = options.date ?? config.date ?? "iso";
324
+ const mode = typeof dateOption === "string" ? parseDateMode(dateOption) : dateOption;
325
+ const width = dateWidth(mode);
326
+ const context = options.dateContext ?? {};
327
+ const formatTime = (c: BlameEntry["commit"]) => {
328
+ if (options.rawTimestamp) return `${c.author.timestamp} ${c.author.tzOffset}`;
329
+ const text = showDate(c.author.timestamp, c.author.tzOffset, mode, context);
330
+ const w = displayWidth(text);
331
+ return w < width ? text + " ".repeat(width - w) : text;
332
+ };
333
+
334
+ // colors: the command line's, else blame.coloring; never with -c
335
+ let colorLines = options.colorLines ?? false;
336
+ let colorByAge = options.colorByAge ?? false;
337
+ if (!colorLines && !colorByAge) {
338
+ colorLines = config.colorLines ?? false;
339
+ colorByAge = config.colorByAge ?? false;
340
+ }
341
+ if (annotate) colorLines = colorByAge = false;
342
+ const repeatedColor =
343
+ (options.repeatedLinesColor ?? config.repeatedLinesColor ?? "") || GIT_COLOR_CYAN;
344
+ const colorFields = parseColorFields(
345
+ options.highlightRecent ?? config.highlightRecent ?? DEFAULT_HIGHLIGHT_RECENT,
346
+ context.now,
347
+ );
348
+
202
349
  const sink = new ByteSink();
350
+ let prev: BlameEntry | undefined;
203
351
  for (const e of entries) {
204
352
  const c = e.commit;
353
+ let defaultColor: string | undefined;
354
+ let color: string | undefined;
355
+ let reset: string | undefined;
356
+ if (colorByAge) {
357
+ // determine_line_heat
358
+ let i = 0;
359
+ while (i < colorFields.length - 1 && c.author.timestamp > colorFields[i].hop) i++;
360
+ defaultColor = colorFields[i].color;
361
+ color = defaultColor;
362
+ reset = GIT_COLOR_RESET;
363
+ }
205
364
  let last: Uint8Array | undefined;
206
365
  for (let cnt = 0; cnt < e.lineCount; cnt++) {
366
+ if (colorLines) {
367
+ if (cnt > 0 || (!options.colorLinesWithinEntries && prev && prev.commitId === e.commitId)) {
368
+ color = repeatedColor;
369
+ reset = GIT_COLOR_RESET;
370
+ } else {
371
+ color = defaultColor;
372
+ reset = defaultColor ? GIT_COLOR_RESET : undefined;
373
+ }
374
+ }
207
375
  let length = options.longObjectName ? 40 : abbrev;
208
376
  let hex = e.commitId;
209
- if (e.boundary && options.blankBoundary) hex = " ".repeat(hex.length);
210
- let line = "";
211
- if (e.boundary && !options.blankBoundary) {
212
- line += "^";
213
- length--;
214
- }
215
- line += hex.slice(0, Math.min(length, 40));
216
- if (showName) {
217
- const pad = Math.max(0, longestFile - encodeGitPath(e.sourcePath).length);
218
- line += ` ${e.sourcePath}${" ".repeat(pad)}`;
219
- }
220
- if (options.showNumber) {
221
- line += ` ${String(e.sourceStart + cnt).padStart(maxOrigDigits)}`;
377
+ if (e.boundary && blankBoundary) hex = " ".repeat(hex.length);
378
+ const marks = marksOf(e);
379
+ let line = (color ?? "") + marks;
380
+ length -= marks.length;
381
+ line += hex.slice(0, Math.max(0, Math.min(length, 40)));
382
+ if (annotate) {
383
+ line += `\t(${padBytes(authorOf(e), 10)}\t${padBytes(formatTime(c), 10)}\t${
384
+ e.resultStart + cnt
385
+ })`;
386
+ } else {
387
+ if (showName) {
388
+ const pad = Math.max(0, longestFile - encodeGitPath(e.sourcePath).length);
389
+ line += ` ${e.sourcePath}${" ".repeat(pad)}`;
390
+ }
391
+ if (options.showNumber) {
392
+ line += ` ${String(e.sourceStart + cnt).padStart(maxOrigDigits)}`;
393
+ }
394
+ if (!options.noAuthor) {
395
+ const name = authorOf(e);
396
+ const pad = Math.max(0, longestAuthor - displayWidth(name));
397
+ line += ` (${name}${" ".repeat(pad)} ${padBytes(formatTime(c), 10)}`;
398
+ }
399
+ line += ` ${String(e.resultStart + cnt).padStart(maxDigits)}) `;
222
400
  }
223
- if (!options.noAuthor) {
224
- const name = authorOf(e);
225
- const pad = Math.max(0, longestAuthor - displayWidth(name));
226
- const time = options.rawTimestamp
227
- ? `${c.author.timestamp} ${c.author.tzOffset}`
228
- : isoDate(c.author.timestamp, c.author.tzOffset);
229
- line += ` (${name}${" ".repeat(pad)} ${time.padStart(10)}`;
230
- }
231
- line += ` ${String(e.resultStart + cnt).padStart(maxDigits)}) `;
401
+ if (reset) line += reset;
232
402
  sink.text(line);
233
403
  last = lines[e.resultStart - 1 + cnt] ?? new Uint8Array(0);
234
404
  sink.push(last);
235
405
  }
236
406
  if (last && last.length > 0 && last[last.length - 1] !== 0x0a) sink.text("\n");
407
+ prev = e;
408
+ }
409
+ return sink.bytes();
410
+ }
411
+
412
+ /** Options of {@link formatAnnotate} */
413
+ export type AnnotateFormatOptions = Omit<
414
+ BlameFormatOptions,
415
+ "annotate" | "date" | "colorLines" | "colorByAge" | "showName" | "showNumber" | "noAuthor"
416
+ >;
417
+
418
+ /**
419
+ * `git annotate`: blame's `-c` columns with ISO dates whatever `--date` says (only the
420
+ * `-local` of `blame.date` is kept, as git keeps it)
421
+ */
422
+ export function formatAnnotate(
423
+ result: BlameResult,
424
+ options: AnnotateFormatOptions = {},
425
+ ): Uint8Array {
426
+ const configured = result.output?.date;
427
+ let local = false;
428
+ if (configured !== undefined) {
429
+ try {
430
+ local = parseDateMode(configured).local;
431
+ } catch {
432
+ local = false;
433
+ }
434
+ }
435
+ return formatBlame(result, { ...options, annotate: true, date: { type: "iso8601", local } });
436
+ }
437
+
438
+ /** `git blame --incremental`: each entry as blame found it, with its commit's details once */
439
+ export function formatBlameIncremental(
440
+ result: BlameResult,
441
+ options: Pick<BlamePorcelainOptions, "quotePath"> = {},
442
+ ): Uint8Array {
443
+ const quote = options.quotePath ?? true;
444
+ const shown = new Set<string>();
445
+ const sink = new ByteSink();
446
+ for (const e of result.incremental ?? []) {
447
+ sink.text(`${e.commitId} ${e.sourceStart} ${e.resultStart} ${e.lineCount}\n`);
448
+ if (!shown.has(e.commitId)) {
449
+ shown.add(e.commitId);
450
+ const c = e.commit;
451
+ sink.text(
452
+ `author ${c.author.name}\nauthor-mail <${c.author.email}>\nauthor-time ${c.author.timestamp}\nauthor-tz ${c.author.tzOffset}\n` +
453
+ `committer ${c.committer.name}\ncommitter-mail <${c.committer.email}>\ncommitter-time ${c.committer.timestamp}\ncommitter-tz ${c.committer.tzOffset}\n` +
454
+ `summary ${subjectOf(c.message, e.commitId)}\n`,
455
+ );
456
+ if (e.boundary) sink.text("boundary\n");
457
+ }
458
+ if (e.previous) {
459
+ sink.text(`previous ${e.previous.commitId} ${quotePath(e.previous.path, quote)}\n`);
460
+ }
461
+ sink.text(`filename ${quotePath(e.sourcePath, quote)}\n`);
237
462
  }
238
463
  return sink.bytes();
239
464
  }
@@ -36,6 +36,12 @@ export interface CloneResult {
36
36
  */
37
37
  hookExitCode?: number;
38
38
 
39
+ /**
40
+ * `--recurse-submodules`: what the submodules' `git submodule update` printed (stdout,
41
+ * stderr) and its exit status, which git's clone exits with (the clone itself stands)
42
+ */
43
+ submodules?: { stdout: string; stderr: string; exitCode: number };
44
+
39
45
  /**
40
46
  * What git prints on stderr besides progress: `--reference-if-able`'s "info: Could not add
41
47
  * alternate for '<path>': <reason>" lines, each with its newline