@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
@@ -12,7 +12,7 @@ import {
12
12
  parseGitBool,
13
13
  type XdiffAlgorithm,
14
14
  } from "@statewalker/vcs-utils";
15
- import { compareGitPaths, decodeGitPath } from "@statewalker/vcs-utils/encoding";
15
+ import { compareGitPaths, decodeGitPath, encodeGitPath } from "@statewalker/vcs-utils/encoding";
16
16
  import {
17
17
  type AttributesIndex,
18
18
  collectChunks,
@@ -36,14 +36,27 @@ import {
36
36
  import { GitCommand } from "../git-command.js";
37
37
  import { RemoteConfigStore } from "../remote-config/index.js";
38
38
  import {
39
+ type BreakScores,
40
+ countChanges,
39
41
  DEFAULT_RENAME_LIMIT,
40
42
  detectRenames,
41
43
  findFollowSource,
44
+ parseBreakRewrites,
45
+ renameScoreOption,
42
46
  scorePercent,
47
+ shouldBreak,
48
+ spanCounts,
43
49
  } from "../rename/index.js";
44
50
  import { writeCombinedDiff } from "../results/combined-diff.js";
45
51
  import { conflictMarkerSizeOf, writePairCheck } from "../results/diff-check.js";
46
52
  import { MovedLineBuffer } from "../results/diff-color.js";
53
+ import {
54
+ DEFAULT_DIRSTAT,
55
+ type DirstatFile,
56
+ type DirstatParams,
57
+ formatDirstat,
58
+ parseDirstatParams,
59
+ } from "../results/diff-dirstat.js";
47
60
  import { ChangeType, type DiffEntry } from "../results/diff-entry.js";
48
61
  import { diffEntryToPair } from "../results/diff-formatter.js";
49
62
  import { orderByPatterns, parseOrderFile } from "../results/diff-order.js";
@@ -63,6 +76,7 @@ import {
63
76
  formatShortstat,
64
77
  formatStat,
65
78
  formatSummary,
79
+ writeHeaderOnlyPatch,
66
80
  writePairPatch,
67
81
  } from "../results/diff-output.js";
68
82
  import { DiffPickaxe } from "../results/diff-pickaxe.js";
@@ -110,7 +124,7 @@ const WITHOUT_INDEX_ATTRIBUTES: { direction: "checkin"; index: AttributesIndex }
110
124
  * The outputs whose diff git prefetches (diffcore_std's `output_formats_to_prefetch`: the
111
125
  * diffstat kinds, the patch, `--dirstat`)
112
126
  */
113
- const PREFETCH_FORMATS = new Set<string>(["patch", "stat", "numstat", "shortstat"]);
127
+ const PREFETCH_FORMATS = new Set<string>(["patch", "stat", "numstat", "shortstat", "dirstat"]);
114
128
 
115
129
  /**
116
130
  * The repository blobs of a diff's pairs (git's `diff_add_if_missing`: each side whose id is
@@ -145,6 +159,11 @@ export type DiffOutputFormat =
145
159
  | "summary"
146
160
  | "name-only"
147
161
  | "name-status"
162
+ /**
163
+ * `--dirstat`: the share of the change each directory holds (see
164
+ * {@link DiffTextOptions.dirstat}, `diff.dirstat`)
165
+ */
166
+ | "dirstat"
148
167
  /**
149
168
  * `--check`: the whitespace errors and leftover conflict markers the change adds (see
150
169
  * {@link writePairCheck}); it turns the other formats off, and {@link DiffCommand.exitCode}
@@ -164,6 +183,7 @@ export interface DiffTextOptions
164
183
  | "wsHighlight"
165
184
  | "whitespaceRule"
166
185
  | "defaultWordRegex"
186
+ | "anchors"
167
187
  >,
168
188
  DiffColorOptions {
169
189
  /**
@@ -314,6 +334,39 @@ export interface DiffTextOptions
314
334
  pickaxeAll?: boolean;
315
335
  /** `-i` / `--regexp-ignore-case` of `-S`/`-G` (ASCII case folded) */
316
336
  pickaxeIgnoreCase?: boolean;
337
+ /**
338
+ * `-M[<n>]` / `--find-renames[=<n>]`: detect renames, at least this similar (git's
339
+ * syntax: `90%`, `9` and `.9` alike; `true` the default 50%); `false` is `--no-renames`.
340
+ * Wins over {@link DiffCommand.setRenames} and `diff.renames`. Trailing text throws git's
341
+ * `error: invalid argument to find-renames` (exit 129).
342
+ */
343
+ findRenames?: boolean | string;
344
+ /**
345
+ * `-C[<n>]` / `--find-copies[=<n>]`: renames and copies, from the modified paths too (the
346
+ * threshold as {@link findRenames}). With `diff.renames=copies` and nothing else asking,
347
+ * git's second `-C` is found: copies are then found harder.
348
+ */
349
+ findCopies?: boolean | string;
350
+ /** `--find-copies-harder` (or `-C -C`): unchanged paths are copy sources too */
351
+ findCopiesHarder?: boolean;
352
+ /**
353
+ * `-B[<n>][/<m>]` / `--break-rewrites`: modifications that changed at least `<n>` (default
354
+ * 50%) are broken into a deletion and a creation for rename detection, and merged back
355
+ * unless they removed at least `<m>` (default 60%) of the old content, shown then as a
356
+ * complete rewrite (`dissimilarity index`, `M<score>`)
357
+ */
358
+ breakRewrites?: boolean | string;
359
+ /**
360
+ * `--dirstat=<params>` for the `dirstat` format: comma-separated `changes`, `lines`,
361
+ * `files`, `cumulative`, `noncumulative` and a cut-off percentage (default `diff.dirstat`,
362
+ * else `changes,noncumulative,3`)
363
+ */
364
+ dirstat?: string;
365
+ /**
366
+ * `--anchored=<text>`: the patience diff, lines starting with one of these texts kept
367
+ * unchanged where they are unique on both sides
368
+ */
369
+ anchored?: readonly string[];
317
370
  }
318
371
 
319
372
  /** Whether options ask for a pickaxe (git's `DIFF_PICKAXE_KINDS_MASK`) */
@@ -415,8 +468,11 @@ function statusOf(entry: DiffEntry): string {
415
468
  /** diffcore_apply_filter over the queue */
416
469
  function applyDiffFilter(entries: DiffEntry[], filter: Set<string> | undefined): DiffEntry[] {
417
470
  if (!filter) return entries;
418
- // match_filter: a modification with a score is a broken pair (-B), which we never make
419
- const matches = (e: DiffEntry) => filter.has(statusOf(e));
471
+ // match_filter: a modification with a score is a broken pair (-B), filtered as `B`
472
+ const matches = (e: DiffEntry) => {
473
+ const status = statusOf(e);
474
+ return status === "M" && e.score ? filter.has("B") : filter.has(status);
475
+ };
420
476
  if (filter.has("*")) return entries.some(matches) ? entries : [];
421
477
  return entries.filter(matches);
422
478
  }
@@ -467,6 +523,7 @@ function reverseEntry(entry: DiffEntry): DiffEntry {
467
523
  if (entry.readNew) out.readOld = entry.readNew;
468
524
  if (entry.readOld) out.readNew = entry.readOld;
469
525
  if (entry.score !== undefined) out.score = entry.score;
526
+ if (entry.lineRanges) out.lineRanges = entry.lineRanges;
470
527
  return out;
471
528
  }
472
529
 
@@ -502,6 +559,17 @@ interface Side {
502
559
  unknownId?: boolean;
503
560
  }
504
561
 
562
+ /** diffcore's settings of one run (see `DiffCommand.diffcoreSettings`) */
563
+ interface DiffcoreSettings {
564
+ detect: false | "renames" | "copies";
565
+ /** The rename/copy threshold (git's `rename_score`), default 50% */
566
+ score?: number;
567
+ /** `--find-copies-harder` */
568
+ harder: boolean;
569
+ /** `-B` */
570
+ breaks?: BreakScores;
571
+ }
572
+
505
573
  /** An index entry, of any stage */
506
574
  type StageEntry = Parameters<typeof compareWithWorktree>[1];
507
575
 
@@ -562,8 +630,12 @@ function abbrevOption(value: number | undefined): number | undefined {
562
630
  return Math.min(40, Math.max(4, Math.trunc(value)));
563
631
  }
564
632
 
565
- /** `diff.renames` as git_config_rename reads it: `copies`/`copy` detect copies too. */
566
- function renamesValue(value: string | undefined): boolean | "copies" | undefined {
633
+ /**
634
+ * `diff.renames` as git_config_rename reads it: `copies`/`copy` detect copies too.
635
+ *
636
+ * @internal
637
+ */
638
+ export function renamesValue(value: string | undefined): boolean | "copies" | undefined {
567
639
  if (value === undefined) return undefined;
568
640
  const v = value.trim().toLowerCase();
569
641
  if (v === "copies" || v === "copy") return "copies";
@@ -766,8 +838,10 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
766
838
  }
767
839
 
768
840
  /**
769
- * Show intent-to-add entries in a `--cached` comparison as the empty files they name, as
770
- * plumbing's `diff-index --cached` does (porcelain `git diff` sets `ita_invisible_in_index`).
841
+ * Show intent-to-add entries as the empty files they name, as plumbing's
842
+ * `diff-index --cached` and `diff-files` do (porcelain `git diff` sets
843
+ * `ita_invisible_in_index`): in a `--cached` comparison on the index side, against the
844
+ * worktree as the old side (`git add -e`'s diff).
771
845
  *
772
846
  * @internal
773
847
  */
@@ -884,6 +958,86 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
884
958
  const { format, warnings } = readDiffSubmoduleConfig(this.workingCopy);
885
959
  this.configuredSubmoduleFormat = format;
886
960
  this.configWarnings.splice(0, this.configWarnings.length, ...warnings);
961
+ // git_diff_ui_config: a diff.dirstat with errors is warned about, the rest applied
962
+ const dirstat = this.config.value("diff.dirstat");
963
+ this.configuredDirstat = { ...DEFAULT_DIRSTAT };
964
+ if (dirstat !== undefined) {
965
+ const errors = parseDirstatParams(dirstat, this.configuredDirstat);
966
+ if (errors.length > 0) {
967
+ this.configWarnings.push(
968
+ `warning: Found errors in 'diff.dirstat' config variable:\n${errors.join("\n")}\n`,
969
+ );
970
+ }
971
+ }
972
+ }
973
+
974
+ /** `diff.dirstat`'s parameters (git's defaults without it) */
975
+ private configuredDirstat?: DirstatParams;
976
+
977
+ /** The dirstat's parameters: `diff.dirstat`, then {@link DiffTextOptions.dirstat} */
978
+ private dirstatParams(): DirstatParams {
979
+ const params = { ...(this.configuredDirstat ?? DEFAULT_DIRSTAT) };
980
+ const text = this.textOptions.dirstat;
981
+ if (text !== undefined) {
982
+ const errors = parseDirstatParams(text, params);
983
+ if (errors.length > 0) {
984
+ throw new GitApiError(
985
+ `fatal: Failed to parse --dirstat/-X option parameter:\n${errors.join("\n")}\n`,
986
+ ).withExitCode(128);
987
+ }
988
+ }
989
+ return params;
990
+ }
991
+
992
+ /** show_dirstat (or show_dirstat_by_line): the dirstat of the pairs */
993
+ private async writeDirstat(
994
+ sink: ByteSink,
995
+ pairs: DiffFilePair[],
996
+ unmerged: Set<DiffFilePair>,
997
+ options: DiffOutputOptions,
998
+ params: DirstatParams,
999
+ ): Promise<void> {
1000
+ const files: DirstatFile[] = [];
1001
+ if (params.byLine) {
1002
+ for (const pair of pairs) {
1003
+ if (unmerged.has(pair)) {
1004
+ files.push({ name: pair.new?.path ?? pair.old?.path ?? "", changed: 0 });
1005
+ continue;
1006
+ }
1007
+ for (const f of await computeDiffstat([pair], options)) {
1008
+ const damage = f.added + f.deleted;
1009
+ // binary files count bytes: 64 of them a line
1010
+ files.push({ name: f.name, changed: f.isBinary ? Math.ceil(damage / 64) : damage });
1011
+ }
1012
+ }
1013
+ } else {
1014
+ const countLastSpan = this.textOptions.renameCountLastSpan;
1015
+ for (const pair of pairs) {
1016
+ if (unmerged.has(pair)) continue;
1017
+ const { old: one, new: two } = pair;
1018
+ const name = two?.path ?? one?.path ?? "";
1019
+ let damage: number;
1020
+ if (one && two && one.idValid !== false && two.idValid !== false && one.id === two.id) {
1021
+ damage = 0;
1022
+ } else if (params.byFile) damage = 1;
1023
+ else {
1024
+ if (one && two) {
1025
+ const a = await one.read();
1026
+ const b = await two.read();
1027
+ const { copied, added } = countChanges(
1028
+ spanCounts(a, { countLastSpan }),
1029
+ spanCounts(b, { countLastSpan }),
1030
+ );
1031
+ damage = a.length - copied + added;
1032
+ } else if (one) damage = (await one.read()).length;
1033
+ else if (two) damage = (await two.read()).length;
1034
+ else continue;
1035
+ if (!damage) damage = 1;
1036
+ }
1037
+ files.push({ name, changed: damage });
1038
+ }
1039
+ }
1040
+ sink.text(formatDirstat(files, params));
887
1041
  }
888
1042
 
889
1043
  /**
@@ -952,6 +1106,44 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
952
1106
  return this;
953
1107
  }
954
1108
 
1109
+ /**
1110
+ * The queue {@link callFormats} prints instead of comparing the trees: pairs a caller found
1111
+ * itself (`git log -L` queues each commit's pairs with their line ranges, line-log.c's
1112
+ * `line_log_queue_pairs`). diffcore's later stages (pickaxe, order, `--diff-filter`) still
1113
+ * apply.
1114
+ *
1115
+ * @internal
1116
+ */
1117
+ setQueue(entries: DiffEntry[]): this {
1118
+ this.checkCallable();
1119
+ this.presetQueue = entries;
1120
+ return this;
1121
+ }
1122
+
1123
+ private presetQueue?: DiffEntry[];
1124
+
1125
+ /**
1126
+ * diffcore's filtering stages over a queue a caller found (the pickaxe, `-O`,
1127
+ * `--diff-filter`): git 2.43's `git log -L` runs them on the pairs it follows renames with
1128
+ *
1129
+ * @internal
1130
+ */
1131
+ async filterQueue(entries: DiffEntry[]): Promise<DiffEntry[]> {
1132
+ await this.preparePickaxe();
1133
+ return this.selected(entries);
1134
+ }
1135
+
1136
+ /**
1137
+ * The diff driver of a path as a diff of two trees finds it (the worktree's attributes,
1138
+ * `diff.<driver>.*`, the builtin drivers): `git log -L :<funcname>:<file>` finds functions
1139
+ * by its funcname pattern (line-range.c's `userdiff_find_by_path`)
1140
+ *
1141
+ * @internal
1142
+ */
1143
+ async driverOf(path: string): Promise<DiffDriver | undefined> {
1144
+ return this.outputOptions().driver?.(path);
1145
+ }
1146
+
955
1147
  /**
956
1148
  * Detect renames (`-M`, `--no-renames`). Default: off for `call()` (each path as added and
957
1149
  * deleted), `diff.renames` (default on) for `callText()`.
@@ -974,6 +1166,52 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
974
1166
  return true;
975
1167
  }
976
1168
 
1169
+ /** diff.c's `additional_path_headers` (see {@link setAdditionalHeaders}) */
1170
+ private additionalHeaders?: Map<string, readonly string[]>;
1171
+
1172
+ /**
1173
+ * diff.c's `additional_path_headers` (`--remerge-diff`'s `remerge CONFLICT` lines), by path:
1174
+ * the paths within the pathspec keep theirs (log-tree.c's setup_additional_headers). A
1175
+ * pair's headers follow its rename lines; a path with headers and no change is shown as a
1176
+ * `diff --git` line with its headers alone, unless a pickaxe or a `--diff-filter` without
1177
+ * `U` is given (`include_conflict_headers`).
1178
+ */
1179
+ setAdditionalHeaders(headers: ReadonlyMap<string, readonly string[]>): this {
1180
+ this.checkCallable();
1181
+ this.additionalHeaders = new Map(headers);
1182
+ return this;
1183
+ }
1184
+
1185
+ /** The additional headers within the pathspec, undefined for none */
1186
+ private headersInPathspec(): Map<string, readonly string[]> | undefined {
1187
+ const all = this.additionalHeaders;
1188
+ if (!all || all.size === 0) return undefined;
1189
+ if (!this.pathspecItems) return all;
1190
+ const out = new Map<string, readonly string[]>();
1191
+ for (const [path, headers] of all) {
1192
+ if (matchPathspec(this.pathspecItems, path, 0) > 0) out.set(path, headers);
1193
+ }
1194
+ return out.size > 0 ? out : undefined;
1195
+ }
1196
+
1197
+ /**
1198
+ * diff.c's `include_conflict_headers`: headers present, no pickaxe, and no `--diff-filter`
1199
+ * or one that keeps `U`
1200
+ */
1201
+ private includesConflictHeaders(headers: Map<string, readonly string[]> | undefined): boolean {
1202
+ if (!headers || hasPickaxe(this.textOptions)) return false;
1203
+ const filter = this.textOptions.diffFilter;
1204
+ if (filter === undefined) return true;
1205
+ // diff_opt_diff_filter: a lowercase letter starts from every status, each letter then
1206
+ // adds (uppercase) or removes (lowercase) its own
1207
+ let keepsU = /[a-z]/.test(filter);
1208
+ for (const c of filter) {
1209
+ if (c === "U") keepsU = true;
1210
+ else if (c === "u") keepsU = false;
1211
+ }
1212
+ return keepsU;
1213
+ }
1214
+
977
1215
  /** Text output options: context, algorithm, whitespace, `--stat` widths, ... */
978
1216
  setOptions(options: DiffTextOptions): this {
979
1217
  this.checkCallable();
@@ -1050,7 +1288,12 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1050
1288
  const fromContents = this.fromContents;
1051
1289
  const early = this.textOptions.diffFilter === undefined && !fromContents;
1052
1290
  const detect = !early && (this.textOptions.quietDetectsRenames ?? false);
1053
- const collected = await this.collect(detect ? renames : false, false, this.pickaxeReads);
1291
+ const collected = await this.collect(
1292
+ detect ? renames : false,
1293
+ false,
1294
+ this.pickaxeReads,
1295
+ !early,
1296
+ );
1054
1297
  // diff_can_quit_early: the walk stopped at the first change, which a pickaxe (git 2.43,
1055
1298
  // whose quiet diff does not look at contents) may then drop
1056
1299
  const queue = early && this.pickaxe ? collected.entries.slice(0, 1) : collected.entries;
@@ -1138,10 +1381,13 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1138
1381
  let followPath = follow;
1139
1382
  let followChange: ChangeType.RENAME | ChangeType.COPY | undefined;
1140
1383
  let collected: { entries: DiffEntry[]; combined: CombinedEntry[] };
1141
- if (follow !== undefined) {
1384
+ if (this.presetQueue) {
1385
+ await this.preparePickaxe();
1386
+ collected = { entries: this.presetQueue, combined: [] };
1387
+ } else if (follow !== undefined) {
1142
1388
  const spec = this.pathspecItems;
1143
1389
  this.pathspecItems = [parsePathspecItem(`:(literal)${follow}`)];
1144
- collected = await this.collect(false, false);
1390
+ collected = await this.collect(false, false, false, false);
1145
1391
  const only = collected.entries[0];
1146
1392
  if (collected.entries.length === 1 && only?.changeType === ChangeType.ADD) {
1147
1393
  this.pathspecItems = spec;
@@ -1163,11 +1409,15 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1163
1409
  ...(followPath !== undefined ? { followPath } : {}),
1164
1410
  ...(followChange !== undefined ? { followChange } : {}),
1165
1411
  };
1166
- if (collected.entries.length === 0 && collected.combined.length === 0) return followed;
1412
+ // diff_queue_is_empty: headers to include count as a change
1413
+ const headers = this.headersInPathspec();
1414
+ const withHeaders = this.includesConflictHeaders(headers);
1415
+ if (collected.entries.length === 0 && collected.combined.length === 0 && !withHeaders)
1416
+ return followed;
1167
1417
  const sink = new ByteSink();
1168
- const queued = await this.writeFormats(sink, formats, collected);
1418
+ const queued = await this.writeFormats(sink, formats, collected, headers);
1169
1419
  // diffcore_std left nothing (`--diff-filter`, `--skip-to`): no diff, as an empty one
1170
- if (queued === 0 && collected.combined.length === 0) return followed;
1420
+ if (queued === 0 && collected.combined.length === 0 && !withHeaders) return followed;
1171
1421
  return { bytes: sink.bytes(), ...followed };
1172
1422
  });
1173
1423
  }
@@ -1221,6 +1471,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1221
1471
  sink: ByteSink,
1222
1472
  formats: DiffOutputFormat[],
1223
1473
  collected: { entries: DiffEntry[]; combined: CombinedEntry[] },
1474
+ headers?: Map<string, readonly string[]>,
1224
1475
  ): Promise<number> {
1225
1476
  const config = this.config;
1226
1477
  const { combined } = collected;
@@ -1292,6 +1543,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1292
1543
  "numstat",
1293
1544
  "stat",
1294
1545
  "shortstat",
1546
+ "dirstat",
1295
1547
  "summary",
1296
1548
  "patch",
1297
1549
  ];
@@ -1310,8 +1562,9 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1310
1562
  }
1311
1563
  for (const c of combined) await this.writeCombined(sink, c, options, combinedLengths);
1312
1564
  }
1313
- // diff_flush of an empty queue prints nothing
1314
- if (pairs.length === 0) return 0;
1565
+ // diff_flush of an empty queue prints nothing (unless headers are to be shown)
1566
+ if (pairs.length === 0 && !headers) return 0;
1567
+ if (headers) options.additionalHeaders = (name) => headers.get(name);
1315
1568
  // diff_setup_done: --check turns the other formats off
1316
1569
  if (formats.includes("check")) {
1317
1570
  const check = { conflictMarkerSize: this.conflictMarkerSizeOf() };
@@ -1350,6 +1603,13 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1350
1603
  }
1351
1604
  continue;
1352
1605
  }
1606
+ if (format === "dirstat") {
1607
+ const params = this.dirstatParams();
1608
+ await this.writeDirstat(sink, pairs, unmerged, options, params);
1609
+ // only the dirstat by lines comes with the diffstat's separator
1610
+ if (params.byLine) separator = true;
1611
+ continue;
1612
+ }
1353
1613
  separator = true;
1354
1614
  switch (format) {
1355
1615
  case "patch": {
@@ -1376,9 +1636,13 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1376
1636
  moved?.colors && moved.colorMoved !== "no"
1377
1637
  ? new MovedLineBuffer(moved.colorMoved, moved.colorMovedWs)
1378
1638
  : sink;
1379
- for (const pair of pairs) {
1639
+ for (const pair of this.withHeaderOnlyPairs(pairs, full, headers)) {
1380
1640
  const before = out.length;
1381
- if (unmerged.has(pair)) {
1641
+ if (typeof pair === "string") {
1642
+ if (this.includesConflictHeaders(headers)) {
1643
+ writeHeaderOnlyPatch(out, pair, headers?.get(pair) ?? [], options);
1644
+ }
1645
+ } else if (unmerged.has(pair)) {
1382
1646
  out.text(`* Unmerged path ${pair.new?.path ?? pair.old?.path}\n`);
1383
1647
  } else await writePairPatch(out, pair, options);
1384
1648
  // builtin_diff's found_changes: the pair showed something
@@ -1441,6 +1705,32 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1441
1705
  return pairs.length;
1442
1706
  }
1443
1707
 
1708
+ /**
1709
+ * create_filepairs_for_header_only_notifications: the pairs, and the paths with headers
1710
+ * none of them names first (as strings), sorted by first name (diffcore_fix_diff_index)
1711
+ */
1712
+ private withHeaderOnlyPairs(
1713
+ pairs: DiffFilePair[],
1714
+ full: (pair: DiffFilePair) => DiffFilePair,
1715
+ headers: Map<string, readonly string[]> | undefined,
1716
+ ): (DiffFilePair | string)[] {
1717
+ if (!headers) return pairs;
1718
+ const nameOf = (pair: DiffFilePair) => {
1719
+ const f = full(pair);
1720
+ return f.old?.path ?? f.new?.path ?? "";
1721
+ };
1722
+ const present = new Set(pairs.map(nameOf));
1723
+ const out: { name: string; item: DiffFilePair | string }[] = pairs.map((pair) => ({
1724
+ name: nameOf(pair),
1725
+ item: pair,
1726
+ }));
1727
+ for (const path of headers.keys()) {
1728
+ if (!present.has(path)) out.push({ name: path, item: path });
1729
+ }
1730
+ out.sort((a, b) => compareBytes(a.name, b.name));
1731
+ return out.map((e) => e.item);
1732
+ }
1733
+
1444
1734
  /**
1445
1735
  * builtin_diff's submodule branch: `--submodule=log` (show_submodule_diff_summary) or
1446
1736
  * `--submodule=diff` (show_submodule_inline_diff) for a pair of submodules (or a submodule
@@ -1859,6 +2149,12 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1859
2149
  wsErrorHighlight: _wsErrorHighlight,
1860
2150
  colorCompatibility: _colorCompatibility,
1861
2151
  driverCompatibility: _driverCompatibility,
2152
+ findRenames: _findRenames,
2153
+ findCopies: _findCopies,
2154
+ findCopiesHarder: _findCopiesHarder,
2155
+ breakRewrites: _breakRewrites,
2156
+ dirstat: _dirstat,
2157
+ anchored,
1862
2158
  ...text
1863
2159
  } = this.textOptions;
1864
2160
  const colorSettings = this.colorSettings;
@@ -1878,7 +2174,13 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1878
2174
  : {}),
1879
2175
  ...text,
1880
2176
  ...(abbrev !== undefined ? { abbrev } : {}),
1881
- algorithm: text.algorithm ?? algorithmValue(get("diff.algorithm")) ?? "myers",
2177
+ algorithm:
2178
+ anchored && anchored.length > 0
2179
+ ? "patience"
2180
+ : (text.algorithm ?? algorithmValue(get("diff.algorithm")) ?? "myers"),
2181
+ ...(anchored && anchored.length > 0
2182
+ ? { anchors: anchored.map((a) => encodeGitPath(a)) }
2183
+ : {}),
1882
2184
  indentHeuristic: text.indentHeuristic ?? boolValue(get("diff.indentHeuristic")) ?? true,
1883
2185
  ...(driver ? { driver } : {}),
1884
2186
  ...(ignoreRegex.length > 0 ? { ignoreRegex } : {}),
@@ -2073,8 +2375,12 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
2073
2375
  renames: boolean | "copies",
2074
2376
  patch = false,
2075
2377
  prefetch = false,
2378
+ diffcore = true,
2076
2379
  ): Promise<{ entries: DiffEntry[]; combined: CombinedEntry[] }> {
2077
2380
  // option parsing fails before anything is read
2381
+ const core = diffcore
2382
+ ? this.diffcoreSettings(renames)
2383
+ : { detect: false as const, harder: false };
2078
2384
  if (this.textOptions.diffFilter !== undefined) parseDiffFilter(this.textOptions.diffFilter);
2079
2385
  this.ignoreRegex();
2080
2386
  await this.preparePickaxe();
@@ -2175,7 +2481,24 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
2175
2481
  // diffcore_std: an output that reads the blobs fetches a partial clone's absent ones in
2176
2482
  // one batch first (diff_queued_diff_prefetch), before rename detection
2177
2483
  if (prefetch) await this.blobs.prefetch?.(prefetchIds(entries));
2178
- if (renames) await this.detectRenames(entries, renames === "copies");
2484
+ if (core.breaks || core.detect) {
2485
+ const oldFiles = async (): Promise<Map<string, Side>> => {
2486
+ if (this.newTree || this.cached || this.oldTree || !worktree) {
2487
+ return this.newTree
2488
+ ? this.treeSide(this.oldTree ?? "HEAD", this.oldTree === undefined)
2489
+ : oldSide;
2490
+ }
2491
+ // diff-files: the index's entries
2492
+ const out = new Map<string, Side>();
2493
+ for await (const entry of this.staging.entries()) {
2494
+ if (entry.stage === 0 && !entry.intentToAdd) {
2495
+ out.set(entry.path, { mode: entry.mode, id: entry.objectId });
2496
+ }
2497
+ }
2498
+ return out;
2499
+ };
2500
+ await this.diffcore(entries, core, oldFiles);
2501
+ }
2179
2502
  return { entries: this.withTreeEntries(entries, oldSide, newSide), combined };
2180
2503
  }
2181
2504
 
@@ -2481,51 +2804,264 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
2481
2804
  }
2482
2805
 
2483
2806
  /**
2484
- * git's `diffcore_rename` over the added and deleted paths; `copies` (DIFF_DETECT_COPY,
2485
- * `diff.renames=copies`): the modified paths are sources too, which stay (copied only)
2807
+ * diffcore's settings of a run: rename or copy detection (`renames` as configured or set,
2808
+ * then `-M`/`-C`), its threshold, `--find-copies-harder`, `-B`; option errors are thrown here
2809
+ */
2810
+ private diffcoreSettings(renames: boolean | "copies"): DiffcoreSettings {
2811
+ const o = this.textOptions;
2812
+ let detect: DiffcoreSettings["detect"] =
2813
+ renames === "copies" ? "copies" : renames ? "renames" : false;
2814
+ let score: number | undefined;
2815
+ let harder = o.findCopiesHarder === true;
2816
+ if (o.findRenames !== undefined) {
2817
+ if (o.findRenames === false) detect = false;
2818
+ else {
2819
+ detect = "renames";
2820
+ score = renameScoreOption(o.findRenames, "find-renames");
2821
+ }
2822
+ }
2823
+ if (o.findCopies !== undefined && o.findCopies !== false) {
2824
+ // diff_opt_find_copies: a second -C (copies already on, from diff.renames) finds harder
2825
+ if (detect === "copies" && o.findRenames === undefined) harder = true;
2826
+ detect = "copies";
2827
+ score = renameScoreOption(o.findCopies, "find-copies") ?? score;
2828
+ }
2829
+ const breaks =
2830
+ o.breakRewrites !== undefined && o.breakRewrites !== false
2831
+ ? parseBreakRewrites(o.breakRewrites)
2832
+ : undefined;
2833
+ return {
2834
+ detect,
2835
+ harder: detect === "copies" && harder,
2836
+ ...(score !== undefined ? { score } : {}),
2837
+ ...(breaks ? { breaks } : {}),
2838
+ };
2839
+ }
2840
+
2841
+ /**
2842
+ * diffcore_std's pair stages: `diffcore_break` (`-B`), `diffcore_rename` (renames, copies,
2843
+ * `--find-copies-harder`), `diffcore_merge_broken`, then `diff_resolve_rename_copy`. The
2844
+ * entries are changed in place, in git's queue order.
2486
2845
  */
2487
- private async detectRenames(entries: DiffEntry[], copies = false): Promise<void> {
2846
+ private async diffcore(
2847
+ entries: DiffEntry[],
2848
+ core: DiffcoreSettings,
2849
+ oldFiles: () => Promise<Map<string, Side>>,
2850
+ ): Promise<void> {
2851
+ const readBlob = async (id: ObjectId) => collectChunks((await this.blobs.load(id)) ?? []);
2852
+ const countLastSpan = this.textOptions.renameCountLastSpan;
2853
+ /** git's diff_filepair extras: a broken half's score, a renamed creation's source */
2854
+ const broken = new Map<DiffEntry, number>();
2855
+ const source = new Map<DiffEntry, { path: string; breakScore?: number }>();
2856
+ const uses = new Map<string, number>();
2857
+
2858
+ if (core.breaks) {
2859
+ const { breakScore, mergeScore } = core.breaks;
2860
+ const out: DiffEntry[] = [];
2861
+ for (const e of entries) {
2862
+ const blobMode = (m: number | undefined) =>
2863
+ m !== undefined && m !== 0 && m !== GITLINK && (m & 0o170000) !== 0o040000;
2864
+ if (
2865
+ e.changeType !== ChangeType.MODIFY ||
2866
+ e.unmerged ||
2867
+ !blobMode(e.oldMode) ||
2868
+ !blobMode(e.newMode) ||
2869
+ e.oldId === undefined
2870
+ ) {
2871
+ out.push(e);
2872
+ continue;
2873
+ }
2874
+ const oldContent = () => (e.readOld ? e.readOld() : readBlob(e.oldId as ObjectId));
2875
+ const newContent = () => (e.readNew ? e.readNew() : readBlob(e.newId as ObjectId));
2876
+ const sideOf = async (mode: number, read: () => Promise<Uint8Array>) => {
2877
+ const content = await read();
2878
+ return {
2879
+ mode,
2880
+ size: content.length,
2881
+ spans: async () => spanCounts(content, { countLastSpan }),
2882
+ };
2883
+ };
2884
+ const same = !e.readOld && !e.readNew && !e.newIdUnknown && e.oldId === e.newId;
2885
+ const verdict = await shouldBreak(
2886
+ await sideOf(e.oldMode as number, oldContent),
2887
+ await sideOf(e.newMode as number, newContent),
2888
+ breakScore,
2889
+ same,
2890
+ );
2891
+ if (!verdict.broken) {
2892
+ out.push(e);
2893
+ continue;
2894
+ }
2895
+ const score = verdict.mergeScore < mergeScore ? 0 : verdict.mergeScore;
2896
+ const del: DiffEntry = {
2897
+ changeType: ChangeType.DELETE,
2898
+ oldPath: e.oldPath,
2899
+ oldId: e.oldId,
2900
+ oldMode: e.oldMode,
2901
+ ...(e.readOld ? { readOld: e.readOld } : {}),
2902
+ };
2903
+ const add: DiffEntry = {
2904
+ changeType: ChangeType.ADD,
2905
+ newPath: e.newPath,
2906
+ newId: e.newId,
2907
+ newMode: e.newMode,
2908
+ ...(e.readNew ? { readNew: e.readNew } : {}),
2909
+ };
2910
+ broken.set(del, score);
2911
+ broken.set(add, score);
2912
+ out.push(del, add);
2913
+ }
2914
+ entries.splice(0, entries.length, ...out);
2915
+ }
2916
+
2917
+ if (core.detect) await this.detectRenames(entries, core, oldFiles, broken, source, uses);
2918
+
2919
+ if (core.breaks) {
2920
+ // diffcore_merge_broken: halves that both survived are one modification again
2921
+ const onePath = (e: DiffEntry) =>
2922
+ source.get(e)?.path ?? (e.changeType === ChangeType.ADD ? e.newPath : e.oldPath);
2923
+ const twoPath = (e: DiffEntry) =>
2924
+ e.changeType === ChangeType.DELETE ? e.oldPath : e.newPath;
2925
+ const samePath = (e: DiffEntry) => broken.has(e) && onePath(e) === twoPath(e);
2926
+ const out: DiffEntry[] = [];
2927
+ const merged = new Set<DiffEntry>();
2928
+ for (let i = 0; i < entries.length; i++) {
2929
+ const p = entries[i] as DiffEntry;
2930
+ if (merged.has(p)) continue;
2931
+ if (samePath(p)) {
2932
+ let peer: DiffEntry | undefined;
2933
+ for (let j = i + 1; j < entries.length; j++) {
2934
+ const pp = entries[j] as DiffEntry;
2935
+ if (!merged.has(pp) && samePath(pp) && onePath(p) === twoPath(pp)) {
2936
+ peer = pp;
2937
+ break;
2938
+ }
2939
+ }
2940
+ if (peer) {
2941
+ merged.add(peer);
2942
+ const d = p.changeType === ChangeType.DELETE ? p : peer;
2943
+ const c = d === p ? peer : p;
2944
+ const score = broken.get(p) ?? 0;
2945
+ out.push({
2946
+ changeType: ChangeType.MODIFY,
2947
+ oldPath: d.oldPath,
2948
+ newPath: c.newPath,
2949
+ oldId: d.oldId,
2950
+ newId: c.newId,
2951
+ oldMode: d.oldMode,
2952
+ newMode: c.newMode,
2953
+ ...(d.readOld ? { readOld: d.readOld } : {}),
2954
+ ...(c.readNew ? { readNew: c.readNew } : {}),
2955
+ ...(score ? { score: scorePercent(score) } : {}),
2956
+ });
2957
+ const from = d.oldPath ?? "";
2958
+ uses.set(from, (uses.get(from) ?? 0) + 1);
2959
+ continue;
2960
+ }
2961
+ }
2962
+ out.push(p);
2963
+ }
2964
+ entries.splice(0, entries.length, ...out);
2965
+ }
2966
+
2967
+ // diff_resolve_rename_copy: in queue order, each renamed pair takes one use of its source
2968
+ for (const e of entries) {
2969
+ const from = source.get(e);
2970
+ if (!from) continue;
2971
+ if (from.path === e.newPath) {
2972
+ // a rename joined a broken pair again: a modification (a rewrite with its score)
2973
+ e.changeType = ChangeType.MODIFY;
2974
+ if (from.breakScore) e.score = scorePercent(from.breakScore);
2975
+ else delete e.score;
2976
+ continue;
2977
+ }
2978
+ const left = (uses.get(from.path) ?? 0) - 1;
2979
+ uses.set(from.path, left);
2980
+ e.changeType = left > 0 ? ChangeType.COPY : ChangeType.RENAME;
2981
+ }
2982
+ }
2983
+
2984
+ /**
2985
+ * git's `diffcore_rename` over the queue: deleted paths (broken deletions too) as sources,
2986
+ * with copies the modified paths (and, harder, the unmodified ones) as sources that stay,
2987
+ * created paths as destinations. A destination paired becomes a rename (resolved into a
2988
+ * copy or a rename by {@link diffcore}); a deletion its rename takes is dropped.
2989
+ */
2990
+ private async detectRenames(
2991
+ entries: DiffEntry[],
2992
+ core: DiffcoreSettings,
2993
+ oldFiles: () => Promise<Map<string, Side>>,
2994
+ broken: Map<DiffEntry, number>,
2995
+ source: Map<DiffEntry, { path: string; breakScore?: number }>,
2996
+ uses: Map<string, number>,
2997
+ ): Promise<void> {
2998
+ const copies = core.detect === "copies";
2488
2999
  const sources = entries.filter(
2489
3000
  (e) =>
2490
- e.changeType === ChangeType.DELETE ||
3001
+ (e.changeType === ChangeType.DELETE && !e.unmerged) ||
2491
3002
  (copies && e.changeType === ChangeType.MODIFY && !e.unmerged),
2492
3003
  );
2493
3004
  // a submodule of git's null id (diff-index of a moved one) matches no source exactly,
2494
3005
  // and gitlinks pair only exactly
2495
3006
  const destinations = entries.filter((e) => e.changeType === ChangeType.ADD && !e.newIdUnknown);
3007
+ // --find-copies-harder: every unchanged path of the old side is a source too
3008
+ const unmodified: DiffEntry[] = [];
3009
+ if (copies && core.harder && destinations.length > 0) {
3010
+ const listed = new Set(entries.map((e) => e.oldPath ?? e.newPath));
3011
+ for (const [path, side] of await oldFiles()) {
3012
+ if (listed.has(path) || !this.matches(path)) continue;
3013
+ unmodified.push({
3014
+ changeType: ChangeType.MODIFY,
3015
+ oldPath: path,
3016
+ newPath: path,
3017
+ oldId: side.id,
3018
+ newId: side.id,
3019
+ oldMode: side.mode as FileModeValue,
3020
+ newMode: side.mode as FileModeValue,
3021
+ });
3022
+ }
3023
+ sources.push(...unmodified);
3024
+ }
3025
+ for (const e of sources) {
3026
+ const kept = e.changeType === ChangeType.MODIFY || (broken.has(e) && !broken.get(e));
3027
+ uses.set(e.oldPath ?? "", kept ? 1 : 0);
3028
+ }
2496
3029
  if (sources.length === 0 || destinations.length === 0) return;
2497
3030
  const byPath = new Map<string, DiffEntry>();
2498
3031
  for (const e of sources) byPath.set(`s:${e.oldPath}`, e);
2499
3032
  for (const e of destinations) byPath.set(`d:${e.newPath}`, e);
2500
- const readBlob = async (id: ObjectId) => {
2501
- const chunks: Uint8Array[] = [];
2502
- let size = 0;
2503
- for await (const c of (await this.blobs.load(id)) ?? []) {
2504
- chunks.push(c);
2505
- size += c.length;
2506
- }
2507
- const out = new Uint8Array(size);
2508
- let at = 0;
2509
- for (const c of chunks) {
2510
- out.set(c, at);
2511
- at += c.length;
2512
- }
2513
- return out;
2514
- };
3033
+ const unmodifiedSet = new Set(unmodified);
2515
3034
  const limit =
2516
3035
  this.renameLimit ?? intValue(this.config.value("diff.renameLimit")) ?? DEFAULT_RENAME_LIMIT;
3036
+ let needed = 0;
3037
+ let degraded = false;
2517
3038
  const pairs = await detectRenames(
2518
- sources.map((e) => ({
2519
- path: e.oldPath ?? "",
2520
- mode: e.oldMode ?? 0,
2521
- id: e.oldId ?? "",
2522
- ...(e.changeType === ChangeType.MODIFY ? { kept: true } : {}),
2523
- })),
3039
+ sources.map((e) => {
3040
+ const kept = e.changeType === ChangeType.MODIFY || (broken.has(e) && !broken.get(e));
3041
+ return {
3042
+ path: e.oldPath ?? "",
3043
+ mode: e.oldMode ?? 0,
3044
+ id: e.oldId ?? "",
3045
+ ...(kept ? { kept: true } : {}),
3046
+ ...(unmodifiedSet.has(e) ? { unmodified: true } : {}),
3047
+ };
3048
+ }),
2524
3049
  destinations.map((e) => ({ path: e.newPath ?? "", mode: e.newMode ?? 0, id: e.newId ?? "" })),
2525
3050
  {
2526
3051
  renameLimit: limit,
2527
3052
  countLastSpan: this.textOptions.renameCountLastSpan,
2528
3053
  copies,
3054
+ breakMode: sources.some((e) => broken.has(e)),
3055
+ ...(core.score !== undefined ? { minimumScore: core.score } : {}),
3056
+ onRenameLimit: (n) => {
3057
+ needed = n;
3058
+ },
3059
+ onDegraded: () => {
3060
+ degraded = true;
3061
+ },
3062
+ onUses: (counted) => {
3063
+ for (const [path, n] of counted) uses.set(path, n);
3064
+ },
2529
3065
  // a partial clone's absent blobs, fetched in one batch (a worktree file is no blob)
2530
3066
  prefetch: async (wanted) => {
2531
3067
  const ids: ObjectId[] = [];
@@ -2548,27 +3084,51 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
2548
3084
  load: async (c, role) => {
2549
3085
  const e = byPath.get(`${role === "source" ? "s" : "d"}:${c.path}`);
2550
3086
  if (role === "destination" && e?.readNew) return e.readNew();
2551
- return readBlob(c.id);
3087
+ return collectChunks((await this.blobs.load(c.id)) ?? []);
2552
3088
  },
2553
3089
  },
2554
3090
  );
2555
- const drop = new Set<DiffEntry>();
3091
+ // diff_warn_rename_limit
3092
+ if (degraded) {
3093
+ this.configWarnings.push(
3094
+ "warning: only found copies from modified paths due to too many files.",
3095
+ );
3096
+ } else if (needed) {
3097
+ this.configWarnings.push(
3098
+ "warning: exhaustive rename detection was skipped due to too many files.",
3099
+ );
3100
+ }
3101
+ if (needed) {
3102
+ this.configWarnings.push(
3103
+ `warning: you may want to set your diff.renameLimit variable to at least ${needed} and retry the command.`,
3104
+ );
3105
+ }
3106
+ const renamedHalves = new Set<string>();
2556
3107
  for (const pair of pairs) {
2557
3108
  const src = byPath.get(`s:${pair.from}`);
2558
3109
  const dst = byPath.get(`d:${pair.to}`);
2559
3110
  if (!src || !dst) continue;
2560
- // a deleted source taken by a rename or copy is no deletion any more
2561
- if (src.changeType === ChangeType.DELETE) drop.add(src);
2562
- dst.changeType = pair.copy ? ChangeType.COPY : ChangeType.RENAME;
3111
+ const breakScore = broken.get(src);
3112
+ source.set(dst, {
3113
+ path: pair.from,
3114
+ ...(breakScore !== undefined ? { breakScore } : {}),
3115
+ });
3116
+ if (broken.has(dst)) renamedHalves.add(dst.newPath ?? "");
3117
+ dst.changeType = ChangeType.RENAME;
2563
3118
  dst.oldPath = src.oldPath;
2564
3119
  dst.oldId = src.oldId;
2565
3120
  dst.oldMode = src.oldMode;
2566
- dst.score = scorePercent(pair.score);
2567
- }
2568
- if (drop.size > 0) {
2569
- const kept = entries.filter((e) => !drop.has(e));
2570
- entries.splice(0, entries.length, ...kept);
3121
+ if (src.readOld) dst.readOld = src.readOld;
3122
+ dst.score = scorePercent(pair.from === pair.to ? (breakScore ?? 0) : pair.score);
2571
3123
  }
3124
+ // the deletions a rename or copy takes away: a broken one when its own creation was
3125
+ // paired, any other once used
3126
+ const kept = entries.filter((e) => {
3127
+ if (e.changeType !== ChangeType.DELETE || e.unmerged) return true;
3128
+ if (broken.has(e)) return !renamedHalves.has(e.oldPath ?? "");
3129
+ return !(uses.get(e.oldPath ?? "") ?? 0);
3130
+ });
3131
+ entries.splice(0, entries.length, ...kept);
2572
3132
  }
2573
3133
 
2574
3134
  /**
@@ -2886,7 +3446,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
2886
3446
  }
2887
3447
  if (!this.matches(entry.path)) continue;
2888
3448
  const side = await compareWith(entry, current);
2889
- if (changedOnly && !entry.intentToAdd) {
3449
+ if (changedOnly && (!entry.intentToAdd || this.intentToAddVisible)) {
2890
3450
  // diff-files: an entry its file still holds is no change on either side, left out
2891
3451
  // of both (the index side is the entry, as `indexSide` gives it)
2892
3452
  if (side && side.id === entry.objectId && side.mode === entry.mode && !side.dirty) {