@statewalker/vcs-commands 0.4.0 → 0.6.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 (561) hide show
  1. package/README.md +123 -8
  2. package/dist/commands/add-command.d.ts +265 -34
  3. package/dist/commands/add-command.d.ts.map +1 -1
  4. package/dist/commands/add-patch/add-interactive.d.ts +46 -0
  5. package/dist/commands/add-patch/add-interactive.d.ts.map +1 -0
  6. package/dist/commands/add-patch/add-patch.d.ts +139 -0
  7. package/dist/commands/add-patch/add-patch.d.ts.map +1 -0
  8. package/dist/commands/add-patch/index.d.ts +6 -0
  9. package/dist/commands/add-patch/index.d.ts.map +1 -0
  10. package/dist/commands/add-patch/patch-backend.d.ts +80 -0
  11. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -0
  12. package/dist/commands/add-patch/patch-modes.d.ts +71 -0
  13. package/dist/commands/add-patch/patch-modes.d.ts.map +1 -0
  14. package/dist/commands/add-patch/patch-session.d.ts +49 -0
  15. package/dist/commands/add-patch/patch-session.d.ts.map +1 -0
  16. package/dist/commands/am/am-command.d.ts +175 -0
  17. package/dist/commands/am/am-command.d.ts.map +1 -0
  18. package/dist/commands/am/index.d.ts +3 -0
  19. package/dist/commands/am/index.d.ts.map +1 -0
  20. package/dist/commands/am/mailinfo.d.ts +58 -0
  21. package/dist/commands/am/mailinfo.d.ts.map +1 -0
  22. package/dist/commands/am/patch-format.d.ts +24 -0
  23. package/dist/commands/am/patch-format.d.ts.map +1 -0
  24. package/dist/commands/apply/apply-command.d.ts +153 -0
  25. package/dist/commands/apply/apply-command.d.ts.map +1 -0
  26. package/dist/commands/apply/apply-engine.d.ts +195 -0
  27. package/dist/commands/apply/apply-engine.d.ts.map +1 -0
  28. package/dist/commands/apply/apply-parse.d.ts +86 -0
  29. package/dist/commands/apply/apply-parse.d.ts.map +1 -0
  30. package/dist/commands/apply/apply-types.d.ts +70 -0
  31. package/dist/commands/apply/apply-types.d.ts.map +1 -0
  32. package/dist/commands/apply/index.d.ts +6 -0
  33. package/dist/commands/apply/index.d.ts.map +1 -0
  34. package/dist/commands/apply/latin1.d.ts +32 -0
  35. package/dist/commands/apply/latin1.d.ts.map +1 -0
  36. package/dist/commands/apply/whitespace.d.ts +58 -0
  37. package/dist/commands/apply/whitespace.d.ts.map +1 -0
  38. package/dist/commands/attributes-source.d.ts +26 -0
  39. package/dist/commands/attributes-source.d.ts.map +1 -0
  40. package/dist/commands/backfill-command.d.ts +110 -0
  41. package/dist/commands/backfill-command.d.ts.map +1 -0
  42. package/dist/commands/bisect/bisect-checkout.d.ts +64 -0
  43. package/dist/commands/bisect/bisect-checkout.d.ts.map +1 -0
  44. package/dist/commands/bisect/bisect-command.d.ts +201 -0
  45. package/dist/commands/bisect/bisect-command.d.ts.map +1 -0
  46. package/dist/commands/bisect/bisect-engine.d.ts +219 -0
  47. package/dist/commands/bisect/bisect-engine.d.ts.map +1 -0
  48. package/dist/commands/bisect/bisect-graph.d.ts +127 -0
  49. package/dist/commands/bisect/bisect-graph.d.ts.map +1 -0
  50. package/dist/commands/bisect/bisect-quote.d.ts +27 -0
  51. package/dist/commands/bisect/bisect-quote.d.ts.map +1 -0
  52. package/dist/commands/bisect/index.d.ts +5 -0
  53. package/dist/commands/bisect/index.d.ts.map +1 -0
  54. package/dist/commands/blame-command.d.ts +122 -131
  55. package/dist/commands/blame-command.d.ts.map +1 -1
  56. package/dist/commands/branch-command.d.ts +52 -2
  57. package/dist/commands/branch-command.d.ts.map +1 -1
  58. package/dist/commands/branch-tracking.d.ts +98 -0
  59. package/dist/commands/branch-tracking.d.ts.map +1 -0
  60. package/dist/commands/checkout-command.d.ts +299 -34
  61. package/dist/commands/checkout-command.d.ts.map +1 -1
  62. package/dist/commands/checkout-paths.d.ts +124 -0
  63. package/dist/commands/checkout-paths.d.ts.map +1 -0
  64. package/dist/commands/cherry-pick-command.d.ts +50 -42
  65. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  66. package/dist/commands/clean-command.d.ts +67 -4
  67. package/dist/commands/clean-command.d.ts.map +1 -1
  68. package/dist/commands/clone-command.d.ts +179 -7
  69. package/dist/commands/clone-command.d.ts.map +1 -1
  70. package/dist/commands/commit-command.d.ts +311 -17
  71. package/dist/commands/commit-command.d.ts.map +1 -1
  72. package/dist/commands/core-config.d.ts +93 -0
  73. package/dist/commands/core-config.d.ts.map +1 -0
  74. package/dist/commands/date-format.d.ts +58 -0
  75. package/dist/commands/date-format.d.ts.map +1 -0
  76. package/dist/commands/describe-command.d.ts.map +1 -1
  77. package/dist/commands/diff-command.d.ts +377 -30
  78. package/dist/commands/diff-command.d.ts.map +1 -1
  79. package/dist/commands/diff-textconv.d.ts +40 -0
  80. package/dist/commands/diff-textconv.d.ts.map +1 -0
  81. package/dist/commands/fetch-command.d.ts +244 -23
  82. package/dist/commands/fetch-command.d.ts.map +1 -1
  83. package/dist/commands/format-patch-command.d.ts +164 -0
  84. package/dist/commands/format-patch-command.d.ts.map +1 -0
  85. package/dist/commands/fsck/fetch-fsck.d.ts +60 -0
  86. package/dist/commands/fsck/fetch-fsck.d.ts.map +1 -0
  87. package/dist/commands/fsck/fsck-command.d.ts +225 -0
  88. package/dist/commands/fsck/fsck-command.d.ts.map +1 -0
  89. package/dist/commands/fsck/fsck-loose.d.ts +68 -0
  90. package/dist/commands/fsck/fsck-loose.d.ts.map +1 -0
  91. package/dist/commands/fsck/fsck-msg-ids.d.ts +122 -0
  92. package/dist/commands/fsck/fsck-msg-ids.d.ts.map +1 -0
  93. package/dist/commands/fsck/fsck-object-checks.d.ts +148 -0
  94. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -0
  95. package/dist/commands/fsck/fsck-object-table.d.ts +132 -0
  96. package/dist/commands/fsck/fsck-object-table.d.ts.map +1 -0
  97. package/dist/commands/fsck/fsck-pack.d.ts +115 -0
  98. package/dist/commands/fsck/fsck-pack.d.ts.map +1 -0
  99. package/dist/commands/fsck/index.d.ts +6 -0
  100. package/dist/commands/fsck/index.d.ts.map +1 -0
  101. package/dist/commands/fsck/verify-pack-command.d.ts +81 -0
  102. package/dist/commands/fsck/verify-pack-command.d.ts.map +1 -0
  103. package/dist/commands/gc-command.d.ts +43 -0
  104. package/dist/commands/gc-command.d.ts.map +1 -1
  105. package/dist/commands/git-date.d.ts +66 -0
  106. package/dist/commands/git-date.d.ts.map +1 -0
  107. package/dist/commands/git-describe.d.ts +67 -0
  108. package/dist/commands/git-describe.d.ts.map +1 -0
  109. package/dist/commands/git-hooks.d.ts +416 -0
  110. package/dist/commands/git-hooks.d.ts.map +1 -0
  111. package/dist/commands/git-ident.d.ts +128 -0
  112. package/dist/commands/git-ident.d.ts.map +1 -0
  113. package/dist/commands/hook-command.d.ts +96 -0
  114. package/dist/commands/hook-command.d.ts.map +1 -0
  115. package/dist/commands/index-adder.d.ts +231 -0
  116. package/dist/commands/index-adder.d.ts.map +1 -0
  117. package/dist/commands/index-host.d.ts +86 -0
  118. package/dist/commands/index-host.d.ts.map +1 -0
  119. package/dist/commands/index.d.ts +55 -0
  120. package/dist/commands/index.d.ts.map +1 -1
  121. package/dist/commands/index.js +3 -2
  122. package/dist/commands/init-command.d.ts +12 -1
  123. package/dist/commands/init-command.d.ts.map +1 -1
  124. package/dist/commands/local-clone.d.ts +60 -0
  125. package/dist/commands/local-clone.d.ts.map +1 -0
  126. package/dist/commands/local-transport.d.ts +117 -0
  127. package/dist/commands/local-transport.d.ts.map +1 -0
  128. package/dist/commands/log-command.d.ts +216 -24
  129. package/dist/commands/log-command.d.ts.map +1 -1
  130. package/dist/commands/log-graph.d.ts +120 -0
  131. package/dist/commands/log-graph.d.ts.map +1 -0
  132. package/dist/commands/log-output.d.ts +323 -0
  133. package/dist/commands/log-output.d.ts.map +1 -0
  134. package/dist/commands/ls-remote-command.d.ts +23 -7
  135. package/dist/commands/ls-remote-command.d.ts.map +1 -1
  136. package/dist/commands/mailmap.d.ts +31 -0
  137. package/dist/commands/mailmap.d.ts.map +1 -0
  138. package/dist/commands/merge-bases.d.ts +85 -0
  139. package/dist/commands/merge-bases.d.ts.map +1 -0
  140. package/dist/commands/merge-command.d.ts +209 -74
  141. package/dist/commands/merge-command.d.ts.map +1 -1
  142. package/dist/commands/merge-config.d.ts +13 -0
  143. package/dist/commands/merge-config.d.ts.map +1 -0
  144. package/dist/commands/merge-drivers.d.ts +130 -0
  145. package/dist/commands/merge-drivers.d.ts.map +1 -0
  146. package/dist/commands/merge-support.d.ts +252 -0
  147. package/dist/commands/merge-support.d.ts.map +1 -0
  148. package/dist/commands/message-cleanup.d.ts +86 -0
  149. package/dist/commands/message-cleanup.d.ts.map +1 -0
  150. package/dist/commands/mv-command.d.ts +94 -0
  151. package/dist/commands/mv-command.d.ts.map +1 -0
  152. package/dist/commands/notes-command.d.ts +256 -0
  153. package/dist/commands/notes-command.d.ts.map +1 -0
  154. package/dist/commands/notes-commands.d.ts +41 -0
  155. package/dist/commands/notes-commands.d.ts.map +1 -0
  156. package/dist/commands/notes-merge-command.d.ts +105 -0
  157. package/dist/commands/notes-merge-command.d.ts.map +1 -0
  158. package/dist/commands/notes-support.d.ts +125 -0
  159. package/dist/commands/notes-support.d.ts.map +1 -0
  160. package/dist/commands/notes-tree.d.ts +106 -0
  161. package/dist/commands/notes-tree.d.ts.map +1 -0
  162. package/dist/commands/object-id-input.d.ts +20 -0
  163. package/dist/commands/object-id-input.d.ts.map +1 -0
  164. package/dist/commands/octopus-merge.d.ts +59 -0
  165. package/dist/commands/octopus-merge.d.ts.map +1 -0
  166. package/dist/commands/ort-merge.d.ts +126 -0
  167. package/dist/commands/ort-merge.d.ts.map +1 -0
  168. package/dist/commands/ort-recursive.d.ts +53 -0
  169. package/dist/commands/ort-recursive.d.ts.map +1 -0
  170. package/dist/commands/pack-refs-command.d.ts +20 -0
  171. package/dist/commands/pack-refs-command.d.ts.map +1 -1
  172. package/dist/commands/patch-id.d.ts +41 -0
  173. package/dist/commands/patch-id.d.ts.map +1 -0
  174. package/dist/commands/pathspec-attributes.d.ts +64 -0
  175. package/dist/commands/pathspec-attributes.d.ts.map +1 -0
  176. package/dist/commands/pathspec.d.ts +324 -0
  177. package/dist/commands/pathspec.d.ts.map +1 -0
  178. package/dist/commands/post-index-change.d.ts +33 -0
  179. package/dist/commands/post-index-change.d.ts.map +1 -0
  180. package/dist/commands/pretty-format.d.ts +222 -0
  181. package/dist/commands/pretty-format.d.ts.map +1 -0
  182. package/dist/commands/promisor-remote.d.ts +80 -0
  183. package/dist/commands/promisor-remote.d.ts.map +1 -0
  184. package/dist/commands/pull-command.d.ts +41 -8
  185. package/dist/commands/pull-command.d.ts.map +1 -1
  186. package/dist/commands/push-command.d.ts +24 -8
  187. package/dist/commands/push-command.d.ts.map +1 -1
  188. package/dist/commands/read-tree-command.d.ts +112 -0
  189. package/dist/commands/read-tree-command.d.ts.map +1 -0
  190. package/dist/commands/rebase-command.d.ts +507 -75
  191. package/dist/commands/rebase-command.d.ts.map +1 -1
  192. package/dist/commands/rebase-todo.d.ts +130 -0
  193. package/dist/commands/rebase-todo.d.ts.map +1 -0
  194. package/dist/commands/ref-decoration-filter.d.ts +53 -0
  195. package/dist/commands/ref-decoration-filter.d.ts.map +1 -0
  196. package/dist/commands/reflog-messages.d.ts +98 -0
  197. package/dist/commands/reflog-messages.d.ts.map +1 -0
  198. package/dist/commands/remote-command.d.ts +146 -6
  199. package/dist/commands/remote-command.d.ts.map +1 -1
  200. package/dist/commands/remote-transport.d.ts +120 -0
  201. package/dist/commands/remote-transport.d.ts.map +1 -0
  202. package/dist/commands/reset-command.d.ts +161 -7
  203. package/dist/commands/reset-command.d.ts.map +1 -1
  204. package/dist/commands/restore-command.d.ts +103 -0
  205. package/dist/commands/restore-command.d.ts.map +1 -0
  206. package/dist/commands/rev-list-command.d.ts +78 -0
  207. package/dist/commands/rev-list-command.d.ts.map +1 -0
  208. package/dist/commands/rev-parse-command.d.ts +80 -0
  209. package/dist/commands/rev-parse-command.d.ts.map +1 -0
  210. package/dist/commands/revert-command.d.ts +46 -38
  211. package/dist/commands/revert-command.d.ts.map +1 -1
  212. package/dist/commands/revision-filenames.d.ts +95 -0
  213. package/dist/commands/revision-filenames.d.ts.map +1 -0
  214. package/dist/commands/revision-range.d.ts +67 -0
  215. package/dist/commands/revision-range.d.ts.map +1 -0
  216. package/dist/commands/revision-resolver.d.ts +321 -0
  217. package/dist/commands/revision-resolver.d.ts.map +1 -0
  218. package/dist/commands/rm-command.d.ts +69 -7
  219. package/dist/commands/rm-command.d.ts.map +1 -1
  220. package/dist/commands/sequencer-pick.d.ts +191 -0
  221. package/dist/commands/sequencer-pick.d.ts.map +1 -0
  222. package/dist/commands/sequencer-support.d.ts +125 -0
  223. package/dist/commands/sequencer-support.d.ts.map +1 -0
  224. package/dist/commands/serve-repository.d.ts +52 -0
  225. package/dist/commands/serve-repository.d.ts.map +1 -0
  226. package/dist/commands/sparse-checkout-command.d.ts +190 -0
  227. package/dist/commands/sparse-checkout-command.d.ts.map +1 -0
  228. package/dist/commands/sparse-checkout-support.d.ts +268 -0
  229. package/dist/commands/sparse-checkout-support.d.ts.map +1 -0
  230. package/dist/commands/stash-apply-command.d.ts +69 -13
  231. package/dist/commands/stash-apply-command.d.ts.map +1 -1
  232. package/dist/commands/stash-create-command.d.ts +214 -26
  233. package/dist/commands/stash-create-command.d.ts.map +1 -1
  234. package/dist/commands/stash-drop-command.d.ts +10 -2
  235. package/dist/commands/stash-drop-command.d.ts.map +1 -1
  236. package/dist/commands/stash-list-command.d.ts +2 -4
  237. package/dist/commands/stash-list-command.d.ts.map +1 -1
  238. package/dist/commands/stash-working-tree.d.ts +95 -0
  239. package/dist/commands/stash-working-tree.d.ts.map +1 -0
  240. package/dist/commands/status-command.d.ts +320 -28
  241. package/dist/commands/status-command.d.ts.map +1 -1
  242. package/dist/commands/status-format.d.ts +155 -0
  243. package/dist/commands/status-format.d.ts.map +1 -0
  244. package/dist/commands/status-header.d.ts +64 -0
  245. package/dist/commands/status-header.d.ts.map +1 -0
  246. package/dist/commands/strerror.d.ts +7 -0
  247. package/dist/commands/strerror.d.ts.map +1 -0
  248. package/dist/commands/submodule-config.d.ts +38 -0
  249. package/dist/commands/submodule-config.d.ts.map +1 -0
  250. package/dist/commands/submodule-summary-command.d.ts +80 -0
  251. package/dist/commands/submodule-summary-command.d.ts.map +1 -0
  252. package/dist/commands/submodule-summary.d.ts +150 -0
  253. package/dist/commands/submodule-summary.d.ts.map +1 -0
  254. package/dist/commands/switch-command.d.ts +132 -0
  255. package/dist/commands/switch-command.d.ts.map +1 -0
  256. package/dist/commands/tag-command.d.ts +23 -2
  257. package/dist/commands/tag-command.d.ts.map +1 -1
  258. package/dist/commands/tracking-info.d.ts +64 -0
  259. package/dist/commands/tracking-info.d.ts.map +1 -0
  260. package/dist/commands/trailers.d.ts +73 -0
  261. package/dist/commands/trailers.d.ts.map +1 -0
  262. package/dist/commands/tree-restore.d.ts +137 -6
  263. package/dist/commands/tree-restore.d.ts.map +1 -1
  264. package/dist/commands/untracked-cache-support.d.ts +53 -0
  265. package/dist/commands/untracked-cache-support.d.ts.map +1 -0
  266. package/dist/commands/worktree-add-command.d.ts +108 -0
  267. package/dist/commands/worktree-add-command.d.ts.map +1 -0
  268. package/dist/commands/worktree-list-command.d.ts +69 -0
  269. package/dist/commands/worktree-list-command.d.ts.map +1 -0
  270. package/dist/commands/worktree-lock-command.d.ts +44 -0
  271. package/dist/commands/worktree-lock-command.d.ts.map +1 -0
  272. package/dist/commands/worktree-overwrite-checks.d.ts +204 -0
  273. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -0
  274. package/dist/commands/worktree-prune-command.d.ts +48 -0
  275. package/dist/commands/worktree-prune-command.d.ts.map +1 -0
  276. package/dist/commands/worktree-remove-command.d.ts +59 -0
  277. package/dist/commands/worktree-remove-command.d.ts.map +1 -0
  278. package/dist/commands/worktree-repair-command.d.ts +35 -0
  279. package/dist/commands/worktree-repair-command.d.ts.map +1 -0
  280. package/dist/commands/worktree-support.d.ts +64 -0
  281. package/dist/commands/worktree-support.d.ts.map +1 -0
  282. package/dist/commands-DnmlfwGV.js +61620 -0
  283. package/dist/core-commands/add.command.impl.d.ts.map +1 -1
  284. package/dist/core-commands/index.d.ts +0 -3
  285. package/dist/core-commands/index.d.ts.map +1 -1
  286. package/dist/errors/add-errors.d.ts +55 -0
  287. package/dist/errors/add-errors.d.ts.map +1 -1
  288. package/dist/errors/checkout-errors.d.ts +29 -0
  289. package/dist/errors/checkout-errors.d.ts.map +1 -1
  290. package/dist/errors/command-errors.d.ts +77 -0
  291. package/dist/errors/command-errors.d.ts.map +1 -1
  292. package/dist/errors/commit-errors.d.ts +9 -0
  293. package/dist/errors/commit-errors.d.ts.map +1 -1
  294. package/dist/errors/git-api-error.d.ts +59 -2
  295. package/dist/errors/git-api-error.d.ts.map +1 -1
  296. package/dist/errors/index.d.ts +1 -0
  297. package/dist/errors/index.d.ts.map +1 -1
  298. package/dist/errors/index.js +371 -19
  299. package/dist/errors/rebase-errors.d.ts +9 -0
  300. package/dist/errors/rebase-errors.d.ts.map +1 -1
  301. package/dist/errors/ref-errors.d.ts +32 -0
  302. package/dist/errors/ref-errors.d.ts.map +1 -1
  303. package/dist/errors/transport-errors.d.ts +12 -2
  304. package/dist/errors/transport-errors.d.ts.map +1 -1
  305. package/dist/errors/worktree-errors.d.ts +35 -0
  306. package/dist/errors/worktree-errors.d.ts.map +1 -0
  307. package/dist/git-command-DxvwPrPB.js +7612 -0
  308. package/dist/git-command.d.ts +422 -32
  309. package/dist/git-command.d.ts.map +1 -1
  310. package/dist/git.d.ts +326 -5
  311. package/dist/git.d.ts.map +1 -1
  312. package/dist/index.d.ts +1 -1
  313. package/dist/index.d.ts.map +1 -1
  314. package/dist/index.js +5 -5
  315. package/dist/remote-config/index.d.ts +1 -0
  316. package/dist/remote-config/index.d.ts.map +1 -1
  317. package/dist/remote-config/remote-config-store.d.ts +123 -3
  318. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  319. package/dist/remote-config/remote-refs.d.ts +41 -0
  320. package/dist/remote-config/remote-refs.d.ts.map +1 -0
  321. package/dist/rename/detect-renames.d.ts +148 -0
  322. package/dist/rename/detect-renames.d.ts.map +1 -0
  323. package/dist/rename/git-similarity.d.ts +44 -0
  324. package/dist/rename/git-similarity.d.ts.map +1 -0
  325. package/dist/rename/index.d.ts +2 -0
  326. package/dist/rename/index.d.ts.map +1 -1
  327. package/dist/results/binary-patch.d.ts +11 -0
  328. package/dist/results/binary-patch.d.ts.map +1 -0
  329. package/dist/results/blame-format.d.ts +55 -0
  330. package/dist/results/blame-format.d.ts.map +1 -0
  331. package/dist/results/cherry-pick-result.d.ts +10 -0
  332. package/dist/results/cherry-pick-result.d.ts.map +1 -1
  333. package/dist/results/clone-result.d.ts +16 -0
  334. package/dist/results/clone-result.d.ts.map +1 -1
  335. package/dist/results/combined-diff.d.ts +77 -0
  336. package/dist/results/combined-diff.d.ts.map +1 -0
  337. package/dist/results/diff-entry.d.ts +32 -0
  338. package/dist/results/diff-entry.d.ts.map +1 -1
  339. package/dist/results/diff-formatter.d.ts +34 -60
  340. package/dist/results/diff-formatter.d.ts.map +1 -1
  341. package/dist/results/diff-order.d.ts +25 -0
  342. package/dist/results/diff-order.d.ts.map +1 -0
  343. package/dist/results/diff-output.d.ts +249 -0
  344. package/dist/results/diff-output.d.ts.map +1 -0
  345. package/dist/results/fetch-result.d.ts +24 -1
  346. package/dist/results/fetch-result.d.ts.map +1 -1
  347. package/dist/results/index.d.ts +5 -0
  348. package/dist/results/index.d.ts.map +1 -1
  349. package/dist/results/index.js +2 -252
  350. package/dist/results/merge-result.d.ts +15 -1
  351. package/dist/results/merge-result.d.ts.map +1 -1
  352. package/dist/results/pull-result.d.ts +12 -3
  353. package/dist/results/pull-result.d.ts.map +1 -1
  354. package/dist/results/push-result.d.ts +42 -0
  355. package/dist/results/push-result.d.ts.map +1 -1
  356. package/dist/results/quote-path.d.ts +27 -0
  357. package/dist/results/quote-path.d.ts.map +1 -0
  358. package/dist/results/rebase-result.d.ts +28 -1
  359. package/dist/results/rebase-result.d.ts.map +1 -1
  360. package/dist/results/revert-result.d.ts +10 -0
  361. package/dist/results/revert-result.d.ts.map +1 -1
  362. package/dist/results/stash-result.d.ts +5 -0
  363. package/dist/results/stash-result.d.ts.map +1 -1
  364. package/dist/results/status-result.d.ts +143 -18
  365. package/dist/results/status-result.d.ts.map +1 -1
  366. package/dist/results-BZGa2Z3I.js +2934 -0
  367. package/dist/transport-command.d.ts +56 -1
  368. package/dist/transport-command.d.ts.map +1 -1
  369. package/package.json +15 -10
  370. package/src/commands/add-command.ts +1291 -224
  371. package/src/commands/add-patch/add-interactive.ts +941 -0
  372. package/src/commands/add-patch/add-patch.ts +1169 -0
  373. package/src/commands/add-patch/index.ts +5 -0
  374. package/src/commands/add-patch/patch-backend.ts +239 -0
  375. package/src/commands/add-patch/patch-modes.ts +249 -0
  376. package/src/commands/add-patch/patch-session.ts +159 -0
  377. package/src/commands/am/am-command.ts +1111 -0
  378. package/src/commands/am/index.ts +2 -0
  379. package/src/commands/am/mailinfo.ts +1015 -0
  380. package/src/commands/am/patch-format.ts +121 -0
  381. package/src/commands/apply/apply-command.ts +832 -0
  382. package/src/commands/apply/apply-engine.ts +1603 -0
  383. package/src/commands/apply/apply-parse.ts +1209 -0
  384. package/src/commands/apply/apply-types.ts +112 -0
  385. package/src/commands/apply/index.ts +5 -0
  386. package/src/commands/apply/latin1.ts +171 -0
  387. package/src/commands/apply/whitespace.ts +260 -0
  388. package/src/commands/attributes-source.ts +96 -0
  389. package/src/commands/backfill-command.ts +694 -0
  390. package/src/commands/bisect/bisect-checkout.ts +217 -0
  391. package/src/commands/bisect/bisect-command.ts +788 -0
  392. package/src/commands/bisect/bisect-engine.ts +1079 -0
  393. package/src/commands/bisect/bisect-graph.ts +595 -0
  394. package/src/commands/bisect/bisect-quote.ts +92 -0
  395. package/src/commands/bisect/index.ts +4 -0
  396. package/src/commands/blame-command.ts +711 -986
  397. package/src/commands/branch-command.ts +354 -32
  398. package/src/commands/branch-tracking.ts +331 -0
  399. package/src/commands/checkout-command.ts +1426 -297
  400. package/src/commands/checkout-paths.ts +784 -0
  401. package/src/commands/cherry-pick-command.ts +156 -396
  402. package/src/commands/clean-command.ts +532 -85
  403. package/src/commands/clone-command.ts +826 -55
  404. package/src/commands/commit-command.ts +1781 -166
  405. package/src/commands/core-config.ts +196 -0
  406. package/src/commands/date-format.ts +668 -0
  407. package/src/commands/describe-command.ts +2 -0
  408. package/src/commands/diff-command.ts +1898 -110
  409. package/src/commands/diff-textconv.ts +50 -0
  410. package/src/commands/fetch-command.ts +1038 -79
  411. package/src/commands/format-patch-command.ts +969 -0
  412. package/src/commands/fsck/fetch-fsck.ts +229 -0
  413. package/src/commands/fsck/fsck-command.ts +3347 -0
  414. package/src/commands/fsck/fsck-loose.ts +287 -0
  415. package/src/commands/fsck/fsck-msg-ids.ts +176 -0
  416. package/src/commands/fsck/fsck-object-checks.ts +1044 -0
  417. package/src/commands/fsck/fsck-object-table.ts +363 -0
  418. package/src/commands/fsck/fsck-pack.ts +604 -0
  419. package/src/commands/fsck/index.ts +5 -0
  420. package/src/commands/fsck/verify-pack-command.ts +249 -0
  421. package/src/commands/gc-command.ts +147 -1
  422. package/src/commands/git-date.ts +895 -0
  423. package/src/commands/git-describe.ts +316 -0
  424. package/src/commands/git-hooks.ts +1472 -0
  425. package/src/commands/git-ident.ts +344 -0
  426. package/src/commands/hook-command.ts +247 -0
  427. package/src/commands/index-adder.ts +706 -0
  428. package/src/commands/index-host.ts +291 -0
  429. package/src/commands/index.ts +83 -0
  430. package/src/commands/init-command.ts +60 -11
  431. package/src/commands/local-clone.ts +183 -0
  432. package/src/commands/local-transport.ts +762 -0
  433. package/src/commands/log-command.ts +1915 -170
  434. package/src/commands/log-graph.ts +671 -0
  435. package/src/commands/log-output.ts +1359 -0
  436. package/src/commands/ls-remote-command.ts +101 -58
  437. package/src/commands/mailmap.ts +129 -0
  438. package/src/commands/merge-bases.ts +296 -0
  439. package/src/commands/merge-command.ts +1386 -491
  440. package/src/commands/merge-config.ts +25 -0
  441. package/src/commands/merge-drivers.ts +306 -0
  442. package/src/commands/merge-support.ts +1198 -0
  443. package/src/commands/message-cleanup.ts +275 -0
  444. package/src/commands/mv-command.ts +976 -0
  445. package/src/commands/notes-command.ts +890 -0
  446. package/src/commands/notes-commands.ts +87 -0
  447. package/src/commands/notes-merge-command.ts +768 -0
  448. package/src/commands/notes-support.ts +402 -0
  449. package/src/commands/notes-tree.ts +602 -0
  450. package/src/commands/object-id-input.ts +27 -0
  451. package/src/commands/octopus-merge.ts +377 -0
  452. package/src/commands/ort-merge.ts +1660 -0
  453. package/src/commands/ort-recursive.ts +160 -0
  454. package/src/commands/pack-refs-command.ts +39 -22
  455. package/src/commands/patch-id.ts +196 -0
  456. package/src/commands/pathspec-attributes.ts +162 -0
  457. package/src/commands/pathspec.ts +1199 -0
  458. package/src/commands/post-index-change.ts +62 -0
  459. package/src/commands/pretty-format.ts +1431 -0
  460. package/src/commands/promisor-remote.ts +298 -0
  461. package/src/commands/pull-command.ts +207 -22
  462. package/src/commands/push-command.ts +236 -23
  463. package/src/commands/read-tree-command.ts +857 -0
  464. package/src/commands/rebase-command.ts +3808 -371
  465. package/src/commands/rebase-todo.ts +584 -0
  466. package/src/commands/ref-decoration-filter.ts +118 -0
  467. package/src/commands/reflog-messages.ts +177 -0
  468. package/src/commands/remote-command.ts +500 -19
  469. package/src/commands/remote-transport.ts +383 -0
  470. package/src/commands/reset-command.ts +828 -91
  471. package/src/commands/restore-command.ts +350 -0
  472. package/src/commands/rev-list-command.ts +167 -0
  473. package/src/commands/rev-parse-command.ts +277 -0
  474. package/src/commands/revert-command.ts +152 -399
  475. package/src/commands/revision-filenames.ts +254 -0
  476. package/src/commands/revision-range.ts +222 -0
  477. package/src/commands/revision-resolver.ts +1379 -0
  478. package/src/commands/rm-command.ts +613 -67
  479. package/src/commands/sequencer-pick.ts +869 -0
  480. package/src/commands/sequencer-support.ts +869 -0
  481. package/src/commands/serve-repository.ts +125 -0
  482. package/src/commands/sparse-checkout-command.ts +1389 -0
  483. package/src/commands/sparse-checkout-support.ts +793 -0
  484. package/src/commands/stash-apply-command.ts +692 -124
  485. package/src/commands/stash-create-command.ts +752 -89
  486. package/src/commands/stash-drop-command.ts +67 -6
  487. package/src/commands/stash-list-command.ts +10 -4
  488. package/src/commands/stash-working-tree.ts +387 -0
  489. package/src/commands/status-command.ts +1426 -97
  490. package/src/commands/status-format.ts +775 -0
  491. package/src/commands/status-header.ts +330 -0
  492. package/src/commands/strerror.ts +29 -0
  493. package/src/commands/submodule-config.ts +94 -0
  494. package/src/commands/submodule-summary-command.ts +334 -0
  495. package/src/commands/submodule-summary.ts +446 -0
  496. package/src/commands/switch-command.ts +402 -0
  497. package/src/commands/tag-command.ts +129 -41
  498. package/src/commands/tracking-info.ts +201 -0
  499. package/src/commands/trailers.ts +417 -0
  500. package/src/commands/tree-restore.ts +344 -15
  501. package/src/commands/untracked-cache-support.ts +194 -0
  502. package/src/commands/worktree-add-command.ts +458 -0
  503. package/src/commands/worktree-list-command.ts +208 -0
  504. package/src/commands/worktree-lock-command.ts +96 -0
  505. package/src/commands/worktree-overwrite-checks.ts +641 -0
  506. package/src/commands/worktree-prune-command.ts +99 -0
  507. package/src/commands/worktree-remove-command.ts +186 -0
  508. package/src/commands/worktree-repair-command.ts +59 -0
  509. package/src/commands/worktree-support.ts +214 -0
  510. package/src/core-commands/add.command.impl.ts +9 -5
  511. package/src/core-commands/index.ts +0 -8
  512. package/src/errors/add-errors.ts +45 -0
  513. package/src/errors/checkout-errors.ts +46 -2
  514. package/src/errors/command-errors.ts +101 -1
  515. package/src/errors/commit-errors.ts +18 -2
  516. package/src/errors/git-api-error.ts +82 -2
  517. package/src/errors/index.ts +1 -0
  518. package/src/errors/merge-errors.ts +2 -2
  519. package/src/errors/rebase-errors.ts +12 -0
  520. package/src/errors/ref-errors.ts +56 -2
  521. package/src/errors/remote-errors.ts +2 -2
  522. package/src/errors/stash-errors.ts +1 -1
  523. package/src/errors/transport-errors.ts +20 -4
  524. package/src/errors/worktree-errors.ts +49 -0
  525. package/src/git-command.ts +1218 -118
  526. package/src/git.ts +552 -11
  527. package/src/index.ts +1 -1
  528. package/src/remote-config/index.ts +1 -0
  529. package/src/remote-config/remote-config-store.ts +326 -16
  530. package/src/remote-config/remote-refs.ts +119 -0
  531. package/src/rename/detect-renames.ts +711 -0
  532. package/src/rename/git-similarity.ts +116 -0
  533. package/src/rename/index.ts +2 -0
  534. package/src/results/binary-patch.ts +401 -0
  535. package/src/results/blame-format.ts +239 -0
  536. package/src/results/cherry-pick-result.ts +10 -0
  537. package/src/results/clone-result.ts +18 -0
  538. package/src/results/combined-diff.ts +585 -0
  539. package/src/results/diff-entry.ts +35 -0
  540. package/src/results/diff-formatter.ts +152 -288
  541. package/src/results/diff-order.ts +64 -0
  542. package/src/results/diff-output.ts +1051 -0
  543. package/src/results/fetch-result.ts +24 -1
  544. package/src/results/index.ts +5 -0
  545. package/src/results/merge-result.ts +15 -1
  546. package/src/results/pull-result.ts +12 -3
  547. package/src/results/push-result.ts +43 -0
  548. package/src/results/quote-path.ts +103 -0
  549. package/src/results/rebase-result.ts +37 -0
  550. package/src/results/revert-result.ts +10 -0
  551. package/src/results/stash-result.ts +6 -0
  552. package/src/results/status-result.ts +288 -27
  553. package/src/transport-command.ts +139 -2
  554. package/dist/commands-BpeXZTC3.js +0 -10278
  555. package/dist/core-commands/checkout.command.d.ts +0 -103
  556. package/dist/core-commands/checkout.command.d.ts.map +0 -1
  557. package/dist/core-commands/checkout.command.impl.d.ts +0 -110
  558. package/dist/core-commands/checkout.command.impl.d.ts.map +0 -1
  559. package/dist/status-result-BapZoQaR.js +0 -411
  560. package/src/core-commands/checkout.command.impl.ts +0 -564
  561. package/src/core-commands/checkout.command.ts +0 -125
@@ -0,0 +1,1198 @@
1
+ /**
2
+ * What `merge`, `cherry-pick`, `revert` and `rebase` share: flat trees, the operation's
3
+ * state files (MERGE_HEAD, MERGE_MSG, sequencer/, rebase-merge/, ...), and writing a merge's
4
+ * result to the worktree and the index as git's merge-ort does (`merge_switch_to_result`: a
5
+ * two-way unpack-trees from HEAD to the result tree, refused before anything is written
6
+ * where it would overwrite local changes or untracked files, then the conflicts staged).
7
+ */
8
+
9
+ import {
10
+ type Blobs,
11
+ collectTreeBytes,
12
+ FileMode,
13
+ type ObjectId,
14
+ type RefLogOptions,
15
+ type TreeEntry,
16
+ } from "@statewalker/vcs-core";
17
+ import { bytesToHex, createSha1 } from "@statewalker/vcs-utils";
18
+ import { compareGitPaths, decodeGitPath, encodeGitPath } from "@statewalker/vcs-utils/encoding";
19
+ import {
20
+ attributesIndexOf,
21
+ type CacheTree,
22
+ type Checkout,
23
+ cacheTreeFullyValid,
24
+ entryStatOf,
25
+ type IndexEntryOptions,
26
+ invalidateCacheTreeAgainstIndex,
27
+ invalidateCacheTreeForIntentToAdd,
28
+ invalidateCacheTreePath,
29
+ type Staging,
30
+ updateCacheTree,
31
+ type WorkingCopy,
32
+ type Worktree,
33
+ } from "@statewalker/vcs-working-tree";
34
+ import type { CommandRefs, CommandTrees } from "../git-command.js";
35
+ import { coreBool, worktreeModeTrust } from "./core-config.js";
36
+ import type { OrtConflict } from "./ort-merge.js";
37
+ import { markIndexUpdate } from "./post-index-change.js";
38
+ import { pushSparseWarnings, SparseUnpack } from "./sparse-checkout-support.js";
39
+ import {
40
+ planFlatRestore,
41
+ planTreeRestore,
42
+ restoreCaseOptions,
43
+ type TreeRestorePlan,
44
+ writeTreeRestorePlan,
45
+ } from "./tree-restore.js";
46
+ import {
47
+ type CheckedEntry,
48
+ entryNotUptodate,
49
+ findInTheWay,
50
+ overwriteMessage,
51
+ pruneEmptyParents,
52
+ removeCaseVariants,
53
+ } from "./worktree-overwrite-checks.js";
54
+
55
+ const sameEntry = (a: CheckedEntry | undefined, b: CheckedEntry | undefined) =>
56
+ a === undefined || b === undefined ? a === b : a.objectId === b.objectId && a.mode === b.mode;
57
+
58
+ /** The blobs, links and gitlinks of a tree, by path (none for no tree). */
59
+ export async function flatTree(
60
+ trees: CommandTrees,
61
+ treeId: ObjectId | undefined,
62
+ ): Promise<Map<string, CheckedEntry>> {
63
+ const out = new Map<string, CheckedEntry>();
64
+ if (!treeId) return out;
65
+ const walk = async (id: ObjectId, prefix: string): Promise<void> => {
66
+ for await (const entry of trees.loadTree(id)) {
67
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
68
+ if (entry.mode === FileMode.TREE) await walk(entry.id, path);
69
+ else out.set(path, { objectId: entry.id, mode: entry.mode });
70
+ }
71
+ };
72
+ await walk(treeId, "");
73
+ return out;
74
+ }
75
+
76
+ /**
77
+ * The trees `ids` as flat maps for a merge of them, as merge-ort reads them: a directory
78
+ * that every one of the trees holds with the same tree id is not read at all, it stands in
79
+ * each map as one entry of mode TREE carrying that id (merge-ort resolves such a directory
80
+ * at once, `side1_matches_mbase && side2_matches_mbase`). Every other directory is read.
81
+ * `dirs` gives, per tree, the id of each directory read or stood in for.
82
+ */
83
+ export async function prunedFlatTrees(
84
+ trees: CommandTrees,
85
+ ids: (ObjectId | undefined)[],
86
+ ): Promise<{ flats: Map<string, CheckedEntry>[]; dirs: Map<string, ObjectId>[] }> {
87
+ const flats = ids.map(() => new Map<string, CheckedEntry>());
88
+ const dirs = ids.map(() => new Map<string, ObjectId>());
89
+ const read = async (id: ObjectId | undefined) => {
90
+ const out: { name: string; mode: number; id: ObjectId }[] = [];
91
+ if (id) for await (const e of trees.loadTree(id)) out.push(e);
92
+ return out;
93
+ };
94
+ const walk = async (level: (ObjectId | undefined)[], prefix: string): Promise<void> => {
95
+ const listed = await Promise.all(level.map(read));
96
+ const byName = new Map<string, ({ mode: number; id: ObjectId } | undefined)[]>();
97
+ listed.forEach((entries, i) => {
98
+ for (const e of entries) {
99
+ let slot = byName.get(e.name);
100
+ if (!slot) {
101
+ slot = level.map(() => undefined);
102
+ byName.set(e.name, slot);
103
+ }
104
+ slot[i] = e;
105
+ }
106
+ });
107
+ for (const [name, slot] of byName) {
108
+ const path = prefix ? `${prefix}/${name}` : name;
109
+ const first = slot[0];
110
+ if (
111
+ first?.mode === FileMode.TREE &&
112
+ slot.every((e) => e !== undefined && e.mode === FileMode.TREE && e.id === first.id)
113
+ ) {
114
+ for (let i = 0; i < slot.length; i++) {
115
+ flats[i].set(path, { objectId: first.id, mode: FileMode.TREE });
116
+ dirs[i].set(path, first.id);
117
+ }
118
+ continue;
119
+ }
120
+ const sub: (ObjectId | undefined)[] = slot.map(() => undefined);
121
+ let nested = false;
122
+ slot.forEach((e, i) => {
123
+ if (!e) return;
124
+ if (e.mode === FileMode.TREE) {
125
+ sub[i] = e.id;
126
+ dirs[i].set(path, e.id);
127
+ nested = true;
128
+ } else flats[i].set(path, { objectId: e.id, mode: e.mode });
129
+ });
130
+ if (nested) await walk(sub, path);
131
+ }
132
+ };
133
+ await walk(ids, "");
134
+ return { flats, dirs };
135
+ }
136
+
137
+ /**
138
+ * The paths where the trees `from` and `to` differ, with their entries on each side (a
139
+ * path one side lacks is absent from its map): what comparing their flat maps finds, read
140
+ * without descending into a subtree both hold with the same id.
141
+ */
142
+ export async function treeDelta(
143
+ trees: CommandTrees,
144
+ from: ObjectId | undefined,
145
+ to: ObjectId | undefined,
146
+ ): Promise<{ from: Map<string, CheckedEntry>; to: Map<string, CheckedEntry> }> {
147
+ const out = { from: new Map<string, CheckedEntry>(), to: new Map<string, CheckedEntry>() };
148
+ const read = async (id: ObjectId | undefined) => {
149
+ const map = new Map<string, { mode: number; id: ObjectId }>();
150
+ if (id) for await (const e of trees.loadTree(id)) map.set(e.name, { mode: e.mode, id: e.id });
151
+ return map;
152
+ };
153
+ const addAll = async (side: Map<string, CheckedEntry>, id: ObjectId, prefix: string) => {
154
+ for await (const e of trees.loadTree(id)) {
155
+ const path = `${prefix}/${e.name}`;
156
+ if (e.mode === FileMode.TREE) await addAll(side, e.id, path);
157
+ else side.set(path, { objectId: e.id, mode: e.mode });
158
+ }
159
+ };
160
+ const walk = async (a: ObjectId | undefined, b: ObjectId | undefined, prefix: string) => {
161
+ if (a === b) return;
162
+ const [x, y] = await Promise.all([read(a), read(b)]);
163
+ for (const name of new Set([...x.keys(), ...y.keys()])) {
164
+ const ex = x.get(name);
165
+ const ey = y.get(name);
166
+ if (ex && ey && ex.mode === ey.mode && ex.id === ey.id) continue;
167
+ const path = prefix ? `${prefix}/${name}` : name;
168
+ const xTree = ex?.mode === FileMode.TREE;
169
+ const yTree = ey?.mode === FileMode.TREE;
170
+ if (xTree && yTree) {
171
+ await walk(ex.id, ey.id, path);
172
+ continue;
173
+ }
174
+ if (ex) {
175
+ if (xTree) await addAll(out.from, ex.id, path);
176
+ else out.from.set(path, { objectId: ex.id, mode: ex.mode });
177
+ }
178
+ if (ey) {
179
+ if (yTree) await addAll(out.to, ey.id, path);
180
+ else out.to.set(path, { objectId: ey.id, mode: ey.mode });
181
+ }
182
+ }
183
+ };
184
+ await walk(from, to, "");
185
+ return out;
186
+ }
187
+
188
+ /** The index's entries of one directory: its files, its subdirectories, its tree id. */
189
+ interface IndexDir {
190
+ files: Map<string, CheckedEntry>;
191
+ dirs: Map<string, IndexDir>;
192
+ id?: ObjectId;
193
+ }
194
+
195
+ /** The index's stage-0 entries as directories, each with the id its tree would have. */
196
+ async function indexDirs(index: Map<string, CheckedEntry>): Promise<IndexDir> {
197
+ const root: IndexDir = { files: new Map(), dirs: new Map() };
198
+ for (const [path, entry] of index) {
199
+ let dir = root;
200
+ let from = 0;
201
+ for (let k = path.indexOf("/"); k >= 0; k = path.indexOf("/", from)) {
202
+ const name = path.slice(from, k);
203
+ let next = dir.dirs.get(name);
204
+ if (!next) {
205
+ next = { files: new Map(), dirs: new Map() };
206
+ dir.dirs.set(name, next);
207
+ }
208
+ dir = next;
209
+ from = k + 1;
210
+ }
211
+ dir.files.set(path.slice(from), entry);
212
+ }
213
+ const hash = async (dir: IndexDir): Promise<ObjectId> => {
214
+ const entries: TreeEntry[] = [];
215
+ for (const [name, sub] of dir.dirs)
216
+ entries.push({ name, mode: FileMode.TREE, id: await hash(sub) });
217
+ for (const [name, e] of dir.files) entries.push({ name, mode: e.mode, id: e.objectId });
218
+ let id: ObjectId;
219
+ try {
220
+ const bytes = await collectTreeBytes(entries);
221
+ const hasher = createSha1().update(new TextEncoder().encode(`tree ${bytes.length}\0`));
222
+ id = bytesToHex(hasher.update(bytes).digest());
223
+ } catch {
224
+ // a name no tree can hold: never the id of a stored tree
225
+ id = "";
226
+ }
227
+ dir.id = id;
228
+ return id;
229
+ };
230
+ await hash(root);
231
+ return root;
232
+ }
233
+
234
+ /**
235
+ * The paths where the index's stage-0 entries differ from the tree `treeId`, sorted (as
236
+ * {@link indexChanges} over the tree's flat map). As git's cache-tree, each directory of the
237
+ * index is given the id its tree would have: a subtree of `treeId` with that id holds what
238
+ * the index holds there and is not read.
239
+ */
240
+ export async function indexChangesFromTree(
241
+ index: Map<string, CheckedEntry>,
242
+ trees: CommandTrees,
243
+ treeId: ObjectId | undefined,
244
+ ): Promise<string[]> {
245
+ const out: string[] = [];
246
+ const all = (dir: IndexDir, prefix: string) => {
247
+ for (const name of dir.files.keys()) out.push(join(prefix, name));
248
+ for (const [name, sub] of dir.dirs) all(sub, join(prefix, name));
249
+ };
250
+ const allOfTree = async (id: ObjectId, prefix: string): Promise<void> => {
251
+ for await (const e of trees.loadTree(id)) {
252
+ const path = join(prefix, e.name);
253
+ if (e.mode === FileMode.TREE) await allOfTree(e.id, path);
254
+ else out.push(path);
255
+ }
256
+ };
257
+ const compare = async (
258
+ id: ObjectId | undefined,
259
+ dir: IndexDir | undefined,
260
+ prefix: string,
261
+ ): Promise<void> => {
262
+ if (!id) {
263
+ if (dir) all(dir, prefix);
264
+ return;
265
+ }
266
+ if (!dir) return allOfTree(id, prefix);
267
+ if (dir.id === id) return;
268
+ const files = new Map<string, { mode: number; id: ObjectId }>();
269
+ const subtrees = new Map<string, ObjectId>();
270
+ for await (const e of trees.loadTree(id)) {
271
+ if (e.mode === FileMode.TREE) subtrees.set(e.name, e.id);
272
+ else files.set(e.name, { mode: e.mode, id: e.id });
273
+ }
274
+ for (const name of new Set([...files.keys(), ...dir.files.keys()])) {
275
+ const t = files.get(name);
276
+ const i = dir.files.get(name);
277
+ if (!t || !i || t.id !== i.objectId || t.mode !== i.mode) out.push(join(prefix, name));
278
+ }
279
+ for (const name of new Set([...subtrees.keys(), ...dir.dirs.keys()])) {
280
+ await compare(subtrees.get(name), dir.dirs.get(name), join(prefix, name));
281
+ }
282
+ };
283
+ await compare(treeId, index.size > 0 ? await indexDirs(index) : undefined, "");
284
+ return out.sort();
285
+ }
286
+
287
+ const join = (prefix: string, name: string) => (prefix ? `${prefix}/${name}` : name);
288
+
289
+ /** Stores a flat path map as a tree. */
290
+ export async function storeFlatTree(
291
+ trees: CommandTrees,
292
+ entries: Map<string, CheckedEntry>,
293
+ ): Promise<ObjectId> {
294
+ type Node = Map<string, Node | CheckedEntry>;
295
+ const root: Node = new Map();
296
+ for (const [path, entry] of entries) {
297
+ const parts = path.split("/");
298
+ let node = root;
299
+ for (const part of parts.slice(0, -1)) {
300
+ let next = node.get(part);
301
+ if (!(next instanceof Map)) {
302
+ next = new Map();
303
+ node.set(part, next);
304
+ }
305
+ node = next;
306
+ }
307
+ node.set(parts[parts.length - 1], entry);
308
+ }
309
+ const store = async (node: Node): Promise<ObjectId> => {
310
+ const list: { name: string; mode: number; id: ObjectId }[] = [];
311
+ for (const [name, value] of node) {
312
+ if (value instanceof Map) list.push({ name, mode: FileMode.TREE, id: await store(value) });
313
+ else list.push({ name, mode: value.mode, id: value.objectId });
314
+ }
315
+ return trees.storeTree(list);
316
+ };
317
+ return store(root);
318
+ }
319
+
320
+ /**
321
+ * Stores `target` as a tree, given that `base` is the flat form of the stored tree
322
+ * `baseTree`: only the directories holding a changed path are stored again, every other
323
+ * subtree is taken from `baseTree` as it is (what a pick changes is a few paths of a large
324
+ * tree). Without a base tree the whole map is stored.
325
+ */
326
+ export async function storeFlatTreeOver(
327
+ trees: CommandTrees,
328
+ baseTree: ObjectId | undefined,
329
+ base: Map<string, CheckedEntry>,
330
+ target: Map<string, CheckedEntry>,
331
+ ): Promise<ObjectId> {
332
+ if (!baseTree) return storeFlatTree(trees, target);
333
+ const changes = new Map<string, CheckedEntry | null>();
334
+ for (const [path, entry] of target) {
335
+ if (!sameEntry(base.get(path), entry)) changes.set(path, entry);
336
+ }
337
+ for (const path of base.keys()) if (!target.has(path)) changes.set(path, null);
338
+ // a directory the target takes whole (one TREE entry): what the base held under it goes
339
+ const whole = [...changes].filter(([, e]) => e?.mode === FileMode.TREE).map(([p]) => `${p}/`);
340
+ if (whole.length > 0) {
341
+ for (const path of [...changes.keys()]) {
342
+ if (whole.some((dir) => path.startsWith(dir))) changes.delete(path);
343
+ }
344
+ }
345
+ if (changes.size === 0) return baseTree;
346
+ const rebuild = async (
347
+ treeId: ObjectId | undefined,
348
+ edits: Map<string, CheckedEntry | null>,
349
+ ): Promise<ObjectId | undefined> => {
350
+ const entries = new Map<string, { mode: number; id: ObjectId }>();
351
+ if (treeId) {
352
+ for await (const e of trees.loadTree(treeId)) entries.set(e.name, { mode: e.mode, id: e.id });
353
+ }
354
+ const direct = new Map<string, CheckedEntry | null>();
355
+ const nested = new Map<string, Map<string, CheckedEntry | null>>();
356
+ for (const [path, entry] of edits) {
357
+ const k = path.indexOf("/");
358
+ if (k < 0) direct.set(path, entry);
359
+ else {
360
+ const name = path.slice(0, k);
361
+ const sub = nested.get(name) ?? new Map<string, CheckedEntry | null>();
362
+ sub.set(path.slice(k + 1), entry);
363
+ nested.set(name, sub);
364
+ }
365
+ }
366
+ // removals first (a file `d` making way for `d/x`), then directories, then files
367
+ for (const [name, entry] of direct) if (!entry) entries.delete(name);
368
+ for (const [name, sub] of nested) {
369
+ const current = entries.get(name);
370
+ const childTree = current?.mode === FileMode.TREE ? current.id : undefined;
371
+ const id = await rebuild(childTree, sub);
372
+ if (id) entries.set(name, { mode: FileMode.TREE, id });
373
+ else if (childTree) entries.delete(name);
374
+ }
375
+ for (const [name, entry] of direct) {
376
+ if (entry) entries.set(name, { mode: entry.mode, id: entry.objectId });
377
+ }
378
+ if (entries.size === 0) return undefined;
379
+ return trees.storeTree([...entries].map(([name, e]) => ({ name, mode: e.mode, id: e.id })));
380
+ };
381
+ return (await rebuild(baseTree, changes)) ?? (await trees.storeTree([]));
382
+ }
383
+
384
+ /**
385
+ * Flat trees by tree id, for an operation that reads the same trees again and again (a
386
+ * rebase: each pick's result is the next pick's HEAD, each commit the next one's parent).
387
+ * Keeps the last few.
388
+ */
389
+ export class FlatTreeCache {
390
+ private readonly cache = new Map<ObjectId, Map<string, CheckedEntry>>();
391
+
392
+ constructor(
393
+ private readonly trees: CommandTrees,
394
+ private readonly size = 8,
395
+ ) {}
396
+
397
+ /** The flat tree (none for no tree); the map is shared, never to be changed. */
398
+ async get(treeId: ObjectId | undefined): Promise<Map<string, CheckedEntry>> {
399
+ if (!treeId) return new Map();
400
+ const hit = this.cache.get(treeId);
401
+ if (hit) {
402
+ this.cache.delete(treeId);
403
+ this.cache.set(treeId, hit);
404
+ return hit;
405
+ }
406
+ const flat = await flatTree(this.trees, treeId);
407
+ this.put(treeId, flat);
408
+ return flat;
409
+ }
410
+
411
+ /** Records the flat form of a tree just stored. */
412
+ put(treeId: ObjectId, flat: Map<string, CheckedEntry>): void {
413
+ this.cache.delete(treeId);
414
+ this.cache.set(treeId, flat);
415
+ while (this.cache.size > this.size) {
416
+ const oldest = this.cache.keys().next().value as ObjectId;
417
+ this.cache.delete(oldest);
418
+ }
419
+ }
420
+ }
421
+
422
+ /**
423
+ * The restore plan of the paths in `writing`, taken from the flat `target` (the tree
424
+ * `targetTree`): each path validated and its blob checked as {@link planTreeRestore} does,
425
+ * without walking the whole tree. With `core.ignorecase` on a worktree that folds case (or
426
+ * cannot tell) the whole tree is planned (case collisions are found across it).
427
+ */
428
+ export async function planPathsRestore(
429
+ ctx: MergeWriteContext,
430
+ target: Map<string, CheckedEntry>,
431
+ targetTree: ObjectId | undefined,
432
+ writing: Set<string>,
433
+ operation: string,
434
+ ): Promise<TreeRestorePlan> {
435
+ const worktree = ctx.worktree;
436
+ const options = {
437
+ symlinks: true,
438
+ windows: worktree?.capabilities?.windowsPaths === true,
439
+ reservedNames: worktree?.capabilities?.reservedNames,
440
+ };
441
+ const caseOptions = await restoreCaseOptions(
442
+ worktree,
443
+ coreBool(ctx.workingCopy, "ignorecase", false),
444
+ );
445
+ if (caseOptions.ignoreCase && caseOptions.foldsCase !== false) {
446
+ const tree = targetTree ?? (await storeFlatTree(ctx.trees, target));
447
+ const plan = await planTreeRestore(ctx.trees, ctx.blobs, tree, operation, {
448
+ ...options,
449
+ ...caseOptions,
450
+ writing,
451
+ });
452
+ return {
453
+ ...plan,
454
+ entries: plan.entries.filter((e) => writing.has(e.path)),
455
+ gitlinks: plan.gitlinks.filter((e) => writing.has(e.path)),
456
+ };
457
+ }
458
+ return planFlatRestore(ctx.blobs, target, writing, operation, options);
459
+ }
460
+
461
+ /** The index: its stage-0 entries by path, and the paths with unmerged entries. */
462
+ export async function readIndex(
463
+ staging: Staging,
464
+ ): Promise<{ index: Map<string, CheckedEntry>; unmerged: string[] }> {
465
+ const index = new Map<string, CheckedEntry>();
466
+ const unmerged = new Set<string>();
467
+ for (const entry of await indexEntryList(staging)) {
468
+ if (entry.stage === 0) index.set(entry.path, entry);
469
+ else unmerged.add(entry.path);
470
+ }
471
+ return { index, unmerged: [...unmerged].sort() };
472
+ }
473
+
474
+ /**
475
+ * The tree of the index, as git's `write_index_as_tree` (`cherry-pick -n`, `revert -n`):
476
+ * unless the index holds a fully valid cache-tree, the index file is written again with
477
+ * one. That write smudges racily clean entries and gives the index a new timestamp, which
478
+ * later overwrite checks compare entries with (an entry as new as it is read again).
479
+ */
480
+ export async function writeIndexAsTree(
481
+ staging: Staging,
482
+ trees: CommandTrees,
483
+ options: { write?: boolean } = {},
484
+ ): Promise<ObjectId> {
485
+ if (!staging.getCacheTree || !staging.setCacheTree) return staging.writeTree(trees);
486
+ const previous = staging.getCacheTree();
487
+ const treeId = await staging.writeTree(trees);
488
+ if (!previous || !cacheTreeFullyValid(previous)) {
489
+ // the valid directories kept, the others computed (cache_tree_update)
490
+ const cacheTree = await updateCacheTree(trees, treeId, previous);
491
+ for (const entry of await indexEntryList(staging)) {
492
+ if (entry.intentToAdd) {
493
+ invalidateCacheTreeForIntentToAdd(cacheTree, entry.path);
494
+ }
495
+ }
496
+ staging.setCacheTree(cacheTree);
497
+ // unless the caller writes the index itself (into the lock it holds)
498
+ if (options.write !== false) await staging.write();
499
+ }
500
+ return treeId;
501
+ }
502
+
503
+ /**
504
+ * git's `cache_tree_update(WRITE_TREE_SILENT | WRITE_TREE_REPAIR)` before an index write
505
+ * (`checkout` after its unpack-trees): unless the cache-tree is fully valid, the invalid
506
+ * directories are computed from the index (the valid ones kept, a stored tree included), the
507
+ * directories of intent-to-add entries left invalid; nothing where the index has conflicts.
508
+ * Sets the cache-tree the next `write` writes.
509
+ */
510
+ export async function updateIndexCacheTree(staging: Staging, trees: CommandTrees): Promise<void> {
511
+ if (!staging.getCacheTree || !staging.setCacheTree) return;
512
+ if (await staging.hasConflicts()) return;
513
+ const previous = staging.getCacheTree();
514
+ if (previous && cacheTreeFullyValid(previous)) return;
515
+ const treeId = await staging.writeTree(trees);
516
+ const cacheTree = await updateCacheTree(trees, treeId, previous);
517
+ for (const entry of await indexEntryList(staging)) {
518
+ if (entry.intentToAdd) {
519
+ invalidateCacheTreeForIntentToAdd(cacheTree, entry.path);
520
+ }
521
+ }
522
+ staging.setCacheTree(cacheTree);
523
+ }
524
+
525
+ /** Every index entry in the index's order, at once where the staging can give them so. */
526
+ async function indexEntryList(staging: Staging): Promise<readonly IndexEntryInput[]> {
527
+ const list = staging.entryList?.();
528
+ if (list) return list;
529
+ const out: IndexEntryInput[] = [];
530
+ for await (const entry of staging.entries()) out.push(entry);
531
+ return out;
532
+ }
533
+
534
+ /** An index entry to write: path, blob, mode, stage and the file's stat. */
535
+ export type IndexEntryInput = IndexEntryOptions;
536
+
537
+ /**
538
+ * Rewrites the index: the entries of each path in `changes` replaced by the ones given
539
+ * (none: the path removed, every stage), every other entry kept as it is.
540
+ */
541
+ export async function rewriteIndex(
542
+ staging: Staging,
543
+ changes: Map<string, IndexEntryInput[]>,
544
+ options: {
545
+ updateCacheTreeWith?: CommandTrees;
546
+ cacheTree?: CacheTree;
547
+ /**
548
+ * Whether the kept entries count as up to date (default true: the merge family refreshes
549
+ * the whole index first). False for a write without a refresh (`git apply --cached`):
550
+ * the write then smudges the racily clean entries it keeps, as `write_locked_index`
551
+ */
552
+ upToDate?: boolean;
553
+ /** False: the index built in memory, left for the caller to write (through its lock) */
554
+ write?: boolean;
555
+ } = {},
556
+ ): Promise<void> {
557
+ // the kept entries are in the index's order already: only the changes are sorted, then
558
+ // both merged (git's order: the paths' bytes, then the stage)
559
+ const kept: IndexEntryInput[] = [];
560
+ for (const entry of await indexEntryList(staging)) {
561
+ if (!changes.has(entry.path)) kept.push(entry);
562
+ }
563
+ const keys = new Map<string, Uint8Array>();
564
+ const key = (path: string) => {
565
+ let k = keys.get(path);
566
+ if (!k) {
567
+ k = encodeGitPath(path);
568
+ keys.set(path, k);
569
+ }
570
+ return k;
571
+ };
572
+ const compareBytes = (a: string, b: string) => {
573
+ const x = key(a);
574
+ const y = key(b);
575
+ for (let i = 0; i < Math.min(x.length, y.length); i++) if (x[i] !== y[i]) return x[i] - y[i];
576
+ return x.length - y.length;
577
+ };
578
+ const compare = (a: IndexEntryInput, b: IndexEntryInput) => {
579
+ if (a.path === b.path) return (a.stage ?? 0) - (b.stage ?? 0);
580
+ // below the surrogates, UTF-16 code units order as the UTF-8 bytes do
581
+ const x = a.path;
582
+ const y = b.path;
583
+ const n = Math.min(x.length, y.length);
584
+ for (let i = 0; i < n; i++) {
585
+ const cx = x.charCodeAt(i);
586
+ const cy = y.charCodeAt(i);
587
+ if (cx === cy) continue;
588
+ return cx < 0xd800 && cy < 0xd800 ? cx - cy : compareBytes(x, y);
589
+ }
590
+ return x.length - y.length;
591
+ };
592
+ const added = [...changes.values()].flat().sort(compare);
593
+ const builder = staging.createBuilder();
594
+ let a = 0;
595
+ for (const entry of kept) {
596
+ while (a < added.length && compare(added[a], entry) < 0) builder.add(added[a++]);
597
+ builder.add(entry);
598
+ }
599
+ while (a < added.length) builder.add(added[a++]);
600
+ // the index is rewritten around the changed paths only: the kept entries count as up to
601
+ // date (as after the whole-index refresh git runs first), so the write checks no untouched
602
+ // racily clean file and the command reads only the paths it writes
603
+ await builder.finish({ upToDate: options.upToDate ?? true });
604
+ // a cache-tree computed for the entries just built
605
+ if (options.cacheTree) staging.setCacheTree?.(options.cacheTree);
606
+ // unpack-trees' cache_tree_update before its index write
607
+ if (options.updateCacheTreeWith) await updateIndexCacheTree(staging, options.updateCacheTreeWith);
608
+ if (options.write !== false) await staging.write();
609
+ }
610
+
611
+ /** Whether the index holds exactly `tree` (no staged change; git's index_differs_from). */
612
+ export function indexMatches(
613
+ index: Map<string, CheckedEntry>,
614
+ tree: Map<string, CheckedEntry>,
615
+ ): boolean {
616
+ return indexChanges(index, tree).length === 0;
617
+ }
618
+
619
+ /** The paths where the index differs from `tree`, sorted. */
620
+ export function indexChanges(
621
+ index: Map<string, CheckedEntry>,
622
+ tree: Map<string, CheckedEntry>,
623
+ ): string[] {
624
+ const out: string[] = [];
625
+ for (const path of new Set([...index.keys(), ...tree.keys()])) {
626
+ if (!sameEntry(index.get(path), tree.get(path))) out.push(path);
627
+ }
628
+ return out.sort();
629
+ }
630
+
631
+ /**
632
+ * The state files of an operation in progress, in git's directory: plain files through the
633
+ * checkout (`MERGE_MSG`, `sequencer/todo`, ...), pseudo refs through the ref store
634
+ * (`MERGE_HEAD`, `ORIG_HEAD`, `CHERRY_PICK_HEAD`, `REVERT_HEAD`, `AUTO_MERGE`).
635
+ */
636
+ export class OperationState {
637
+ constructor(
638
+ private readonly checkout: Checkout,
639
+ private readonly refs: CommandRefs,
640
+ ) {}
641
+
642
+ /** Whether the checkout can keep state files at all. */
643
+ get supported(): boolean {
644
+ return typeof this.checkout.writeStateFile === "function";
645
+ }
646
+
647
+ async read(name: string): Promise<string | undefined> {
648
+ const bytes = await this.checkout.readStateFile?.(name);
649
+ // a path in a state file (MERGE_MSG's conflicts) is its bytes, as git writes it
650
+ return bytes ? decodeGitPath(bytes) : undefined;
651
+ }
652
+
653
+ async write(name: string, content: string | Uint8Array): Promise<void> {
654
+ const bytes = typeof content === "string" ? encodeGitPath(content) : content;
655
+ await this.checkout.writeStateFile?.(name, bytes);
656
+ }
657
+
658
+ /** Appends to a state file (O_APPEND where the checkout can; else a rewrite). */
659
+ async append(name: string, content: string): Promise<void> {
660
+ if (this.checkout.appendStateFile) {
661
+ // the same bytes `write` would store (escaped bytes as themselves)
662
+ await this.checkout.appendStateFile(name, encodeGitPath(content));
663
+ return;
664
+ }
665
+ await this.write(name, `${(await this.read(name)) ?? ""}${content}`);
666
+ }
667
+
668
+ async remove(...names: string[]): Promise<void> {
669
+ for (const name of names) await this.checkout.removeStateFile?.(name);
670
+ }
671
+
672
+ /** The names in a state directory, sorted (empty: none, or a checkout that cannot list). */
673
+ async listDir(name: string): Promise<string[]> {
674
+ return (await this.checkout.listStateDir?.(name)) ?? [];
675
+ }
676
+
677
+ async exists(name: string): Promise<boolean> {
678
+ if ((await this.checkout.readStateFile?.(name)) !== undefined) return true;
679
+ return ((await this.checkout.listStateDir?.(name)) ?? []).length > 0;
680
+ }
681
+
682
+ async ref(name: string): Promise<ObjectId | undefined> {
683
+ const ref = await this.refs.get(name).catch(() => undefined);
684
+ return ref && "objectId" in ref && ref.objectId ? ref.objectId : undefined;
685
+ }
686
+
687
+ /**
688
+ * Writes a pseudo ref. With `old` (an id, or `null` for none), as git's refs_update_ref
689
+ * given the value it read: a ref store with a transaction hook shows it, and checks it under
690
+ * the lock (a store that cannot batch the ref writes it plainly).
691
+ */
692
+ async setRef(
693
+ name: string,
694
+ id: ObjectId,
695
+ options?: { old: ObjectId | null; log?: RefLogOptions },
696
+ ): Promise<void> {
697
+ if (options) {
698
+ const result = await this.refs.batch([{ name, old: options.old, new: id }], options.log);
699
+ if (result.ok) return;
700
+ if (
701
+ !result.rejected.every((r) => r.reason === "invalid-name" || r.reason === "unsupported")
702
+ ) {
703
+ throw Object.assign(
704
+ new Error(
705
+ `cannot update ${name}: ${result.rejected.map((r) => `${r.name}: ${r.reason}`).join(", ")}`,
706
+ ),
707
+ { rejected: result.rejected },
708
+ );
709
+ }
710
+ }
711
+ await this.refs.set(name, id);
712
+ }
713
+
714
+ async removeRef(...names: string[]): Promise<void> {
715
+ for (const name of names) {
716
+ if (await this.refs.has(name).catch(() => false)) await this.refs.delete(name);
717
+ }
718
+ }
719
+
720
+ /** Each name on its own, a failure ignored (git's unconditional `refs_delete_ref` calls). */
721
+ private async removeRefsIgnoringFailures(names: string[]): Promise<void> {
722
+ for (const name of names) await this.removeRef(name).catch(() => undefined);
723
+ }
724
+
725
+ /**
726
+ * Deletes pseudo refs whether they exist or not, as git's unconditional `refs_delete_ref`
727
+ * calls (AUTO_MERGE after a commit): a ref store with a transaction hook tells it of the
728
+ * deletion either way. Without one nothing can tell, and only the refs there are removed
729
+ * (a check is far cheaper than a deletion's transaction). A failure is ignored, as git
730
+ * ignores it there.
731
+ */
732
+ async deleteRef(...names: string[]): Promise<void> {
733
+ if (!refsWatched(this.refs)) {
734
+ await this.removeRefsIgnoringFailures(names);
735
+ return;
736
+ }
737
+ for (const name of names) await this.refs.delete(name).catch(() => false);
738
+ }
739
+
740
+ /**
741
+ * A pseudo ref git's sequencer writes and deletes again before anyone else could read it
742
+ * (CHERRY_PICK_HEAD of a rebase's pick that stops): written and deleted where a
743
+ * transaction hook hears both, else only deleted (a stale one goes either way).
744
+ */
745
+ async writeAndDeleteRef(name: string, id: ObjectId): Promise<void> {
746
+ if (refsWatched(this.refs)) await this.setRef(name, id);
747
+ await this.deleteRef(name);
748
+ }
749
+
750
+ /**
751
+ * Runs `body` with pseudo ref `name` at `id`, deleted once `body` succeeds (git's sequencer:
752
+ * CHERRY_PICK_HEAD written before the commit, deleted by do_commit after it). Only hooks
753
+ * can read it meanwhile, and they run only where a transaction hook is installed: without
754
+ * one it is written only when `body` fails, which leaves it, as git does.
755
+ */
756
+ async withRefDuring<T>(name: string, id: ObjectId, body: () => Promise<T>): Promise<T> {
757
+ const watched = refsWatched(this.refs);
758
+ if (watched) await this.setRef(name, id);
759
+ let result: T;
760
+ try {
761
+ result = await body();
762
+ } catch (error) {
763
+ if (!watched) await this.setRef(name, id).catch(() => undefined);
764
+ throw error;
765
+ }
766
+ await this.deleteRef(name);
767
+ return result;
768
+ }
769
+ }
770
+
771
+ /**
772
+ * Whether a reference-transaction hook is installed on `refs` (the files store while a
773
+ * command with a hook runner runs, or a receive-pack): writes it alone would see can be
774
+ * skipped without one.
775
+ */
776
+ export function refsWatched(refs: unknown): boolean {
777
+ return (refs as { getTransactionHook?(): unknown }).getTransactionHook?.() !== undefined;
778
+ }
779
+
780
+ /** The parts of a command a merge's write needs. */
781
+ export interface MergeWriteContext {
782
+ workingCopy: WorkingCopy;
783
+ worktree?: Worktree;
784
+ staging: Staging;
785
+ blobs: Blobs;
786
+ trees: CommandTrees;
787
+ /**
788
+ * Collects what the sparse checkout prints on stderr while the write applies its patterns
789
+ * (git's `warning:` lines for files left despite the patterns), one entry per line.
790
+ */
791
+ sparseWarnings?: string[];
792
+ }
793
+
794
+ /** Why a merge's write was refused: git's message, and the paths it lists. */
795
+ export interface MergeRefusal {
796
+ message: string;
797
+ paths: string[];
798
+ }
799
+
800
+ /** Whether an untracked, not ignored file or link stands at `path`. */
801
+ async function untrackedFileAt(worktree: Worktree, path: string): Promise<boolean> {
802
+ const entry = await worktree.getEntry(path);
803
+ if (!entry || entry.isDirectory) return false;
804
+ return !(await worktree.isIgnored(path));
805
+ }
806
+
807
+ /**
808
+ * git's two-way unpack-trees (`twoway_merge`, with update) from `head` to `target` on the
809
+ * current index and worktree, as merge-ort's checkout of its result and a fast-forward
810
+ * run it: a path the two trees agree on keeps its index entry and worktree file (local
811
+ * edits survive); a path they differ on is switched when the index and the worktree still
812
+ * hold `head`'s version, else everything is refused with git's messages. Then the
813
+ * conflicts are staged at stages 1-3 (`record_conflicted_index_entries`).
814
+ *
815
+ * @returns the refusal (nothing written), or undefined once written
816
+ */
817
+ export async function switchTrees(
818
+ ctx: MergeWriteContext,
819
+ head: Map<string, CheckedEntry>,
820
+ target: Map<string, CheckedEntry>,
821
+ options: SwitchTreesOptions,
822
+ ): Promise<MergeRefusal | undefined> {
823
+ // unpack-trees with a destination index: the index written next follows a worktree update
824
+ // (post-index-change's first flag)
825
+ markIndexUpdate(ctx.staging, { workdir: true });
826
+ // in a sparse checkout: unpack-trees with its patterns (a conflict's file is written
827
+ // whatever they say where its entry was outside the checkout, as
828
+ // record_conflicted_index_entries does; one leaving the checkout is unlinked)
829
+ const conflicts = [...(options.conflicts ?? [])];
830
+ // without update nothing in the worktree is touched (unpack-trees skips the sparse
831
+ // checkout: `skip_sparse_checkout` when `!o->update`)
832
+ const sparse =
833
+ options.update === false || options.skipSparseCheckout
834
+ ? undefined
835
+ : await SparseUnpack.start(ctx);
836
+ sparse?.conflict(conflicts.map((c) => c.stagePath));
837
+ const switched = await switchTreesWith(ctx, head, target, { ...options, conflicts }, sparse);
838
+ if (switched.refused) return switched.refused;
839
+ if (sparse) {
840
+ const outcome = await sparse.finish(ctx, { written: switched.written });
841
+ pushSparseWarnings(ctx.sparseWarnings, sparse, outcome, true);
842
+ }
843
+ return undefined;
844
+ }
845
+
846
+ /** Options of {@link switchTrees}. */
847
+ export interface SwitchTreesOptions {
848
+ /** Names the operation in git's refusal messages */
849
+ action: "merge" | "checkout";
850
+ /** Names the operation in restore failures */
851
+ operation: string;
852
+ /** The conflicts to stage */
853
+ conflicts?: Iterable<OrtConflict>;
854
+ /**
855
+ * The stored tree the switch leaves in the index (`target` whole, or the tree `target`
856
+ * changes HEAD to): planned from (saves storing it again), and written as the index's
857
+ * cache-tree, as unpack-trees does
858
+ */
859
+ targetTree?: ObjectId;
860
+ /**
861
+ * Whether the worktree is updated (default true: `-u`). False (`read-tree -m H M`
862
+ * without `-u`): the entries are still checked against their files (`verify_uptodate`),
863
+ * but nothing is written or removed and untracked files are not looked for.
864
+ */
865
+ update?: boolean;
866
+ /** `--no-sparse-checkout`: the sparse-checkout patterns are not applied */
867
+ skipSparseCheckout?: boolean;
868
+ /**
869
+ * unpack-trees without porcelain messages (`read-tree`): the refusal is the first path's
870
+ * one `error:` line ("Entry 'x' would be overwritten by merge. Cannot merge.", ...)
871
+ */
872
+ plainErrors?: boolean;
873
+ /**
874
+ * unpack-trees' `initial_checkout` (`read-tree -m H M` into an unborn index,
875
+ * `isIndexUnborn`): a path of `target` missing from the index is taken as new
876
+ * (written, untracked files in the way refused), never as a staged deletion
877
+ */
878
+ initialCheckout?: boolean;
879
+ }
880
+
881
+ /** git's default unpack-trees messages (`unpack_plumbing_errors`) for one refused path. */
882
+ export function plainUnpackError(
883
+ kind: "wouldOverwrite" | "notUptodate" | "lostDir" | "untracked" | "untrackedRemoved",
884
+ path: string,
885
+ ): string {
886
+ switch (kind) {
887
+ case "wouldOverwrite":
888
+ return `error: Entry '${path}' would be overwritten by merge. Cannot merge.`;
889
+ case "notUptodate":
890
+ return `error: Entry '${path}' not uptodate. Cannot merge.`;
891
+ case "lostDir":
892
+ return `error: Updating '${path}' would lose untracked files in it`;
893
+ case "untracked":
894
+ return `error: Untracked working tree file '${path}' would be overwritten by merge.`;
895
+ case "untrackedRemoved":
896
+ return `error: Untracked working tree file '${path}' would be removed by merge.`;
897
+ }
898
+ }
899
+
900
+ async function switchTreesWith(
901
+ ctx: MergeWriteContext,
902
+ head: Map<string, CheckedEntry>,
903
+ target: Map<string, CheckedEntry>,
904
+ options: SwitchTreesOptions,
905
+ sparse: SparseUnpack | undefined,
906
+ ): Promise<{ refused?: MergeRefusal; written: Set<string> }> {
907
+ const { index, unmerged } = await readIndex(ctx.staging);
908
+ // a path staying outside the sparse checkout: neither checked nor touched in the worktree
909
+ const quiet = (path: string) => sparse?.leavesAlone(path) === true;
910
+ const update = options.update !== false;
911
+ const checked = ctx.worktree;
912
+ const worktree = update ? checked : undefined;
913
+ const trust = checked ? worktreeModeTrust(ctx.workingCopy, checked) : undefined;
914
+ const indexTime = ctx.staging.getUpdateTime();
915
+ const ignoreCase = coreBool(ctx.workingCopy, "ignorecase", false);
916
+ const staged: string[] = [];
917
+ const local: string[] = [];
918
+ const untrackedRemoved: string[] = [];
919
+ const updates: string[] = [];
920
+ const removes: string[] = [];
921
+ const paths = [...new Set([...head.keys(), ...target.keys()])].sort();
922
+ for (const path of paths) {
923
+ const h = head.get(path);
924
+ const t = target.get(path);
925
+ const i = index.get(path);
926
+ if (sameEntry(h, t) && !(options.initialCheckout && t && !i)) continue;
927
+ if (i) {
928
+ if (t && sameEntry(i, t)) continue;
929
+ if (!sameEntry(i, h)) {
930
+ // a staged change the switch would lose (reject_merge)
931
+ staged.push(path);
932
+ continue;
933
+ }
934
+ if (checked && !quiet(path) && (await entryNotUptodate(checked, path, i, trust, indexTime))) {
935
+ local.push(path);
936
+ continue;
937
+ }
938
+ (t ? updates : removes).push(path);
939
+ continue;
940
+ }
941
+ if (t) {
942
+ // a staged deletion of a path the target changes (none in an initial checkout)
943
+ if (h && !options.initialCheckout) staged.push(path);
944
+ else updates.push(path);
945
+ continue;
946
+ }
947
+ // a deletion already staged: an untracked file there would be removed
948
+ if (worktree && (await untrackedFileAt(worktree, path))) untrackedRemoved.push(path);
949
+ }
950
+ // a dirty file the target deletes is reported as a local change only (git's
951
+ // verify_clean_subdirectory counts tracked files under a replaced directory as such)
952
+ const deleting = [...removes, ...local.filter((p) => !target.has(p))];
953
+ // the files written: in a sparse checkout, those of the paths in its patterns only
954
+ const writing = new Set(updates.filter((p) => !sparse || sparse.writes(p)));
955
+ const found = worktree
956
+ ? await findInTheWay(worktree, {
957
+ writes: [...writing],
958
+ removes: new Set(deleting),
959
+ index,
960
+ ...(await restoreCaseOptions(worktree, ignoreCase)),
961
+ // verify_clean_subdirectory's read_directory folds case: removed before the writes
962
+ caseVariantsExpendable: true,
963
+ })
964
+ : { untracked: [], lostDirs: [], caseVariants: [] };
965
+ const message = options.plainErrors
966
+ ? firstPlainError({
967
+ wouldOverwrite: staged,
968
+ notUptodate: local,
969
+ lostDir: found.lostDirs,
970
+ untracked: found.untracked,
971
+ untrackedRemoved,
972
+ })
973
+ : overwriteMessage(options.action, {
974
+ staged,
975
+ local,
976
+ lostDirs: found.lostDirs,
977
+ untracked: found.untracked,
978
+ untrackedRemoved,
979
+ });
980
+ if (message) {
981
+ return {
982
+ refused: {
983
+ message,
984
+ paths: [...staged, ...local, ...found.lostDirs, ...found.untracked, ...untrackedRemoved],
985
+ },
986
+ written: new Set(),
987
+ };
988
+ }
989
+
990
+ // record_conflicted_index_entries checks out a conflict's stage-0 entry left skip-worktree
991
+ // (the switch kept it: the trees agree on it, or the index holds the target's already):
992
+ // its file is written whatever the patterns say, as checkout_entry with force writes it
993
+ let planTarget = target;
994
+ if (worktree && sparse) {
995
+ for (const conflict of options.conflicts ?? []) {
996
+ const path = conflict.stagePath;
997
+ const i = index.get(path) as IndexEntryInput | undefined;
998
+ if (!i?.skipWorktree || writing.has(path) || removes.includes(path)) continue;
999
+ const t = target.get(path);
1000
+ if (t ? !sameEntry(i, t) : head.has(path)) continue;
1001
+ if (planTarget === target) planTarget = new Map(target);
1002
+ planTarget.set(path, i);
1003
+ writing.add(path);
1004
+ }
1005
+ }
1006
+
1007
+ // the worktree: deletions first (a deleted file `d` makes way for `d/x`), then the files
1008
+ if (worktree) {
1009
+ if (sparse) {
1010
+ // apply_sparse_checkout before check_updates: the files leaving the checkout
1011
+ const removing = new Set(removes);
1012
+ const updating = new Set(updates);
1013
+ const unlink = await sparse.leaving(
1014
+ worktree,
1015
+ (p) => (removing.has(p) ? undefined : updating.has(p) ? target.get(p) : index.get(p)),
1016
+ { trust },
1017
+ );
1018
+ await sparse.unlink(worktree, unlink);
1019
+ }
1020
+ for (const path of removes) {
1021
+ if (quiet(path)) continue;
1022
+ await worktree.remove(path);
1023
+ await pruneEmptyParents(worktree, path);
1024
+ }
1025
+ // core.ignorecase: what verify_clean_subdirectory took as tracked in a directory a file
1026
+ // replaces goes with the directory (checkout_entry removes the subtree)
1027
+ await removeCaseVariants(worktree, found.caseVariants ?? []);
1028
+ if (writing.size > 0) {
1029
+ // only the written paths are planned (each validated, its blob checked)
1030
+ const plan = await planPathsRestore(
1031
+ ctx,
1032
+ planTarget,
1033
+ planTarget === target ? options.targetTree : undefined,
1034
+ writing,
1035
+ options.operation,
1036
+ );
1037
+ // the attributes of the index the switch leaves (git's o->result): only its
1038
+ // attributes files are looked up
1039
+ const result = new Map<string, ObjectId>();
1040
+ for (const [path, entry] of index) {
1041
+ if (path.endsWith(".gitattributes")) result.set(path, entry.objectId);
1042
+ }
1043
+ for (const path of removes) result.delete(path);
1044
+ for (const path of updates) result.set(path, (target.get(path) as CheckedEntry).objectId);
1045
+ await writeTreeRestorePlan(
1046
+ worktree,
1047
+ ctx.blobs,
1048
+ {
1049
+ ...plan,
1050
+ entries: plan.entries.filter((e) => writing.has(e.path)),
1051
+ gitlinks: plan.gitlinks.filter((e) => writing.has(e.path)),
1052
+ },
1053
+ options.operation,
1054
+ {
1055
+ symlinks: trust?.symlinks ?? true,
1056
+ gitlinks: true,
1057
+ replaceInTheWay: true,
1058
+ attributes: { direction: "checkout", index: attributesIndexOf(result) },
1059
+ },
1060
+ );
1061
+ }
1062
+ }
1063
+
1064
+ // the index: the switched paths take the target's entries (with the stat of the files
1065
+ // just written), the conflicts their stages
1066
+ const changes = new Map<string, IndexEntryOptions[]>();
1067
+ for (const path of removes) changes.set(path, []);
1068
+ for (const path of updates) {
1069
+ const t = target.get(path) as CheckedEntry;
1070
+ // the stat of a file written (an entry left outside the checkout has none)
1071
+ const stats = worktree && writing.has(path) ? await worktree.getEntry(path) : undefined;
1072
+ // the skip-worktree bit of the entry replaced is kept (merged_entry: "Migrate old flags
1073
+ // over"); assume-unchanged is not
1074
+ const old = index.get(path) as IndexEntryInput | undefined;
1075
+ changes.set(path, [
1076
+ {
1077
+ path,
1078
+ objectId: t.objectId,
1079
+ mode: t.mode,
1080
+ ...entryStatOf(stats && !stats.isDirectory ? stats : {}),
1081
+ ...(old?.skipWorktree ? { skipWorktree: true } : {}),
1082
+ },
1083
+ ]);
1084
+ }
1085
+ const conflicts = [...(options.conflicts ?? [])];
1086
+ for (const conflict of conflicts) {
1087
+ const stages: [CheckedEntry | undefined, 1 | 2 | 3][] = [
1088
+ [conflict.base, 1],
1089
+ [conflict.ours, 2],
1090
+ [conflict.theirs, 3],
1091
+ ];
1092
+ const list: IndexEntryOptions[] = [];
1093
+ for (const [entry, stage] of stages) {
1094
+ if (!entry) continue;
1095
+ list.push({
1096
+ path: conflict.stagePath,
1097
+ objectId: entry.objectId,
1098
+ mode: entry.mode,
1099
+ stage,
1100
+ size: 0,
1101
+ mtime: 0,
1102
+ });
1103
+ }
1104
+ // a stage-0 entry the switch wrote here gives way to the conflict's stages
1105
+ changes.set(conflict.stagePath, [
1106
+ ...(changes.get(conflict.stagePath) ?? []).filter((e) => (e.stage ?? 0) > 0),
1107
+ ...list,
1108
+ ]);
1109
+ }
1110
+ // unpack-trees writes a new index: the resolve-undo records are gone (git keeps them only
1111
+ // through commands that edit the index in place)
1112
+ ctx.staging.clearResolveUndo?.();
1113
+ let switchedCacheTree: CacheTree | undefined;
1114
+ if (options.targetTree && ctx.staging.setCacheTree) {
1115
+ // the cache-tree of the target (unpack_trees: the index's cache-tree, each path changed
1116
+ // invalidated, then cache_tree_update: only invalid directories are read), the
1117
+ // directories of the intent-to-add entries kept and of the conflicts invalid
1118
+ // (record_conflicted_index_entries)
1119
+ const previous = ctx.staging.getCacheTree?.();
1120
+ if (previous) {
1121
+ for (const path of [...updates, ...removes]) invalidateCacheTreePath(previous, path);
1122
+ }
1123
+ const cacheTree = await updateCacheTree(ctx.trees, options.targetTree, previous);
1124
+ // the index the switch leaves is the target only where it switched: an entry it kept
1125
+ // (a staged change the two trees agree on, carried over) differs from the target, and
1126
+ // its directories are invalid, as cache_tree_update computes them from the index
1127
+ const result = new Map<string, CheckedEntry>();
1128
+ for (const [path, entry] of index) {
1129
+ const e = entry as IndexEntryInput;
1130
+ if (changes.has(path) || e.intentToAdd) continue;
1131
+ result.set(path, { objectId: entry.objectId, mode: entry.mode });
1132
+ }
1133
+ for (const path of updates) {
1134
+ if (changes.get(path)?.some((e) => (e.stage ?? 0) === 0)) {
1135
+ result.set(path, target.get(path) as CheckedEntry);
1136
+ }
1137
+ }
1138
+ await invalidateCacheTreeAgainstIndex(
1139
+ ctx.trees,
1140
+ cacheTree,
1141
+ options.targetTree,
1142
+ result,
1143
+ previous,
1144
+ );
1145
+ for (const path of unmerged) invalidateCacheTreePath(cacheTree, path);
1146
+ for (const [path, entry] of index) {
1147
+ const e = entry as IndexEntryInput;
1148
+ if (e.intentToAdd && !changes.has(path)) {
1149
+ invalidateCacheTreeForIntentToAdd(cacheTree, path);
1150
+ }
1151
+ }
1152
+ for (const conflict of conflicts) {
1153
+ invalidateCacheTreePath(cacheTree, conflict.stagePath);
1154
+ }
1155
+ switchedCacheTree = cacheTree;
1156
+ }
1157
+ // set once the changes are in: it describes the index they leave
1158
+ await rewriteIndex(ctx.staging, changes, { cacheTree: switchedCacheTree });
1159
+ return { written: worktree ? writing : new Set() };
1160
+ }
1161
+
1162
+ /**
1163
+ * The refusal unpack-trees prints without porcelain messages: it stops at the first path
1164
+ * refused (in path order), with that path's one `error:` line.
1165
+ */
1166
+ export function firstPlainError(
1167
+ found: Partial<Record<Parameters<typeof plainUnpackError>[0], string[]>>,
1168
+ ): string | undefined {
1169
+ let best: { kind: Parameters<typeof plainUnpackError>[0]; path: string } | undefined;
1170
+ for (const [kind, paths] of Object.entries(found) as [
1171
+ Parameters<typeof plainUnpackError>[0],
1172
+ string[] | undefined,
1173
+ ][]) {
1174
+ for (const path of paths ?? []) {
1175
+ if (!best || compareGitPaths(path, best.path) < 0) best = { kind, path };
1176
+ }
1177
+ }
1178
+ return best ? plainUnpackError(best.kind, best.path) : undefined;
1179
+ }
1180
+
1181
+ /** git's `# Conflicts:` block appended to MERGE_MSG. */
1182
+ export function conflictsComment(paths: string[], unclean = paths.length > 0): string {
1183
+ if (!unclean) return "";
1184
+ return `\n# Conflicts:\n${paths.map((p) => `#\t${p}\n`).join("")}`;
1185
+ }
1186
+
1187
+ /** The commit's first line (git's subject, as in `<abbrev> (<subject>)`). */
1188
+ export function subjectOf(message: string): string {
1189
+ // git's format_subject after skip_blank_lines: the first paragraph's lines, each without
1190
+ // its trailing blanks (git's isspace: ASCII space, tab, LF, CR; never a no-break space),
1191
+ // joined with a space
1192
+ const lines = message.split("\n").map((l) => l.replace(/[ \t\r]+$/, ""));
1193
+ let i = 0;
1194
+ while (i < lines.length && lines[i] === "") i++;
1195
+ const subject: string[] = [];
1196
+ for (; i < lines.length && lines[i] !== ""; i++) subject.push(lines[i] as string);
1197
+ return subject.join(" ");
1198
+ }