@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
@@ -102,6 +102,11 @@ export interface GitmodulesParseOptions {
102
102
  export class Gitmodules {
103
103
  /** git's `warning:` lines, in order */
104
104
  readonly warnings: string[] = [];
105
+ /**
106
+ * The variables that name no submodule (`fetch.recurseSubmodules`, `submodule.fetchJobs`),
107
+ * in the file's order, as `fetch_config_from_gitmodules` reads them
108
+ */
109
+ readonly settings: { key: string; value: string | undefined }[] = [];
105
110
  private readonly names = new Map<string, SubmoduleSettings>();
106
111
  private readonly paths = new Map<string, SubmoduleSettings>();
107
112
 
@@ -164,7 +169,10 @@ export class Gitmodules {
164
169
  );
165
170
  for (const entry of config.listWithOrigin()) {
166
171
  const parts = entry.key.split(".");
167
- if (parts.length < 3 || parts[0] !== "submodule") continue;
172
+ if (parts.length < 3 || parts[0] !== "submodule") {
173
+ out.settings.push({ key: entry.key, value: entry.text });
174
+ continue;
175
+ }
168
176
  const name = parts.slice(1, -1).join(".");
169
177
  const item = parts[parts.length - 1] as string;
170
178
  const key = `submodule.${name}.${item}`;
@@ -0,0 +1,265 @@
1
+ /**
2
+ * unpack-trees' submodule support (`--recurse-submodules`, `submodule.recurse`): the checks
3
+ * `check_submodule_move_head` makes in `verify_uptodate_1`, `verify_absent_1` and
4
+ * `merged_entry`, and the moves entry.c's `checkout_entry` and `unlink_entry` make in
5
+ * `check_updates`, for the commands that switch a superproject's index and worktree
6
+ * (checkout, switch, reset, read-tree, restore).
7
+ */
8
+ import { FileMode, type ObjectId } from "@statewalker/vcs-core";
9
+ import { parseGitBool } from "@statewalker/vcs-utils";
10
+ import type { WorkingCopy, Worktree } from "@statewalker/vcs-working-tree";
11
+
12
+ import { GitApiError } from "../errors/index.js";
13
+ import { RemoteConfigStore } from "../remote-config/index.js";
14
+ import type { SubmoduleHeadMover } from "./submodule-commands.js";
15
+
16
+ /** An index or tree entry as the checks see it. */
17
+ interface Entry {
18
+ objectId: ObjectId;
19
+ mode: number;
20
+ }
21
+
22
+ /**
23
+ * Whether a command recurses into submodules: `--[no-]recurse-submodules` when given, else
24
+ * `submodule.recurse` (git's `config_update_recurse_submodules`).
25
+ */
26
+ export function recursesIntoSubmodules(
27
+ workingCopy: WorkingCopy,
28
+ given: boolean | undefined,
29
+ ): boolean {
30
+ if (given !== undefined) return given;
31
+ const value = RemoteConfigStore.from(workingCopy).value("submodule.recurse");
32
+ return value === undefined ? false : parseGitBool(value) === true;
33
+ }
34
+
35
+ /**
36
+ * One unpack-trees run's submodule handling. The checks run before anything is written and
37
+ * record what they refuse ({@link rejected}, git's `ERROR_WOULD_LOSE_SUBMODULE`) with the
38
+ * lines the submodules' own read-tree printed ({@link lines}); the moves run with the
39
+ * worktree update, removals first, then (`.gitmodules` read again) the checkouts.
40
+ */
41
+ export class SubmoduleUnpack {
42
+ /** The lines printed while checking or moving (the child read-trees' errors, git's `error:`) */
43
+ readonly lines: string[] = [];
44
+ /** The paths refused as `Cannot update submodule` (ERROR_WOULD_LOSE_SUBMODULE) */
45
+ readonly rejected: string[] = [];
46
+
47
+ private moverLoad?: Promise<SubmoduleHeadMover>;
48
+ private loadedMover?: SubmoduleHeadMover;
49
+ /** submodule-commands.ts, loaded when first needed (it imports the commands this serves) */
50
+ private commands?: typeof import("./submodule-commands.js");
51
+
52
+ private constructor(
53
+ private readonly workingCopy: WorkingCopy,
54
+ /** unpack-trees' `o->reset`: the checks pass `SUBMODULE_MOVE_HEAD_FORCE` */
55
+ readonly reset: boolean,
56
+ /** git's `--super-prefix` of the run (its plain messages name paths with it) */
57
+ readonly superPrefix: string,
58
+ ) {}
59
+
60
+ /**
61
+ * The run's submodule handling, or undefined when the command does not recurse. The
62
+ * `.gitmodules` the run starts with is read here, before anything changes it (git reads it
63
+ * before its removals: `load_gitmodules_file`).
64
+ */
65
+ static async start(
66
+ workingCopy: WorkingCopy,
67
+ options: { recurse: boolean; reset: boolean; superPrefix?: string },
68
+ ): Promise<SubmoduleUnpack | undefined> {
69
+ if (!options.recurse || !workingCopy.worktree) return undefined;
70
+ const unpack = new SubmoduleUnpack(workingCopy, options.reset, options.superPrefix ?? "");
71
+ await (await unpack.m()).isSubmodule("");
72
+ return unpack;
73
+ }
74
+
75
+ private async m(): Promise<SubmoduleHeadMover> {
76
+ if (this.loadedMover) return this.loadedMover;
77
+ this.moverLoad ??= import("./submodule-commands.js").then((commands) => {
78
+ this.commands = commands;
79
+ this.loadedMover = new commands.SubmoduleHeadMover(this.workingCopy, this.superPrefix);
80
+ return this.loadedMover;
81
+ });
82
+ return this.moverLoad;
83
+ }
84
+
85
+ /** `submodule_from_ce`: a gitlink `.gitmodules` (as read now) names. */
86
+ async isSubmodule(path: string, entry: Entry | undefined): Promise<boolean> {
87
+ if (!entry || entry.mode !== FileMode.GITLINK) return false;
88
+ return (await this.m()).isSubmodule(path);
89
+ }
90
+
91
+ /** `file_exists`. */
92
+ async exists(path: string): Promise<boolean> {
93
+ return (await this.m()).exists(path);
94
+ }
95
+
96
+ /** Whether the submodule at `path` keeps its repository in its worktree (`.git` directory). */
97
+ async hasGitDirectory(path: string): Promise<boolean> {
98
+ return (await this.m()).hasGitDirectory(path);
99
+ }
100
+
101
+ /**
102
+ * `check_submodule_move_head` (a dry run): whether the submodule at `path` could move from
103
+ * `oldHead` to `newHead`; a refusal is recorded. True when refused.
104
+ */
105
+ async refuses(path: string, oldHead: string | null, newHead: string | null): Promise<boolean> {
106
+ const ok = await this.run(async () =>
107
+ (await this.m()).moveHead(path, oldHead, newHead, { dryRun: true, force: this.reset }),
108
+ );
109
+ if (ok) return false;
110
+ if (!this.rejected.includes(path)) this.rejected.push(path);
111
+ return true;
112
+ }
113
+
114
+ /**
115
+ * `verify_uptodate_1` of the index entry `entry` at `path`: undefined when it is not a
116
+ * submodule there (the file checks apply), else whether it is refused (its HEAD cannot move
117
+ * to the recorded commit: local changes, a dirty index).
118
+ */
119
+ async notUptodate(path: string, entry: Entry): Promise<boolean | undefined> {
120
+ if (this.reset) return false;
121
+ if (!(await this.isSubmodule(path, entry)) || !(await this.exists(path))) return undefined;
122
+ return this.refuses(path, "HEAD", entry.objectId);
123
+ }
124
+
125
+ /**
126
+ * `merged_entry`'s check of the entry `next` replacing `old` (none for a new path): the
127
+ * submodule there must be able to move. True when refused.
128
+ */
129
+ async mergedRefuses(path: string, old: Entry | undefined, next: Entry): Promise<boolean> {
130
+ if (!(await this.isSubmodule(path, next)) || !(await this.exists(path))) return false;
131
+ return this.refuses(path, old?.objectId ?? null, next.objectId);
132
+ }
133
+
134
+ /**
135
+ * `verify_absent_1` of a new entry `next` at `path` (with an update, without
136
+ * `UNPACK_RESET_OVERWRITE_UNTRACKED`): undefined when not a submodule (the untracked-file
137
+ * checks apply), else whether it is refused.
138
+ */
139
+ async absentRefuses(path: string, next: Entry): Promise<boolean | undefined> {
140
+ if (!(await this.isSubmodule(path, next)) || !(await this.exists(path))) return undefined;
141
+ return this.refuses(path, next.objectId, null);
142
+ }
143
+
144
+ /** `load_gitmodules_file` after `.gitmodules` was checked out. */
145
+ reload(): void {
146
+ this.loadedMover?.reload();
147
+ }
148
+
149
+ /**
150
+ * `unlink_entry` of a gitlink `entry` leaving the index: the submodule moved to nothing
151
+ * (forced), then its directory removed when empty.
152
+ */
153
+ async unlink(worktree: Worktree, path: string, entry: Entry): Promise<void> {
154
+ if (await this.isSubmodule(path, entry)) {
155
+ await this.run(async () => (await this.m()).moveHead(path, "HEAD", null, { force: true }));
156
+ }
157
+ await worktree.removeEmptyDirectory?.(path);
158
+ }
159
+
160
+ /** `unlink_entry`'s move of a submodule leaving the index (forced), without the rmdir. */
161
+ async moveAway(path: string, entry: Entry): Promise<void> {
162
+ if (!(await this.isSubmodule(path, entry))) return;
163
+ await this.run(async () => (await this.m()).moveHead(path, "HEAD", null, { force: true }));
164
+ }
165
+
166
+ /**
167
+ * `checkout_entry` of the gitlink `entry` (check_updates' `state.force`): an existing
168
+ * submodule moved to its commit (forced), a missing or unpopulated one checked out there.
169
+ * False when the move failed.
170
+ */
171
+ async checkout(worktree: Worktree, path: string, entry: Entry): Promise<boolean> {
172
+ const kind = await (await this.m()).kind(path);
173
+ if (!(await this.isSubmodule(path, entry))) {
174
+ if (kind === undefined) await worktree.mkdir(path);
175
+ return true;
176
+ }
177
+ if (kind === "symlink") {
178
+ // a link git's stat follows into a repository: validate_submodule_path dies
179
+ await this.run(async () => (await this.m()).validateLinked(path).then(() => true));
180
+ }
181
+ if (kind === undefined) {
182
+ await worktree.mkdir(path);
183
+ return this.run(async () =>
184
+ (await this.m()).moveHead(path, null, entry.objectId, { force: true }),
185
+ );
186
+ }
187
+ if (!(await (await this.m()).populated(path))) {
188
+ if (kind !== "directory") await worktree.remove(path);
189
+ return this.run(async () => (await this.m()).moveHead(path, null, entry.objectId, {}));
190
+ }
191
+ return this.run(async () =>
192
+ (await this.m()).moveHead(path, "HEAD", entry.objectId, { force: true }),
193
+ );
194
+ }
195
+
196
+ /**
197
+ * The refusal git prints for the recorded submodules, in porcelain form (`Cannot update
198
+ * submodule:` and the list) or plain (`Submodule '<path>' cannot checkout new HEAD.`).
199
+ */
200
+ refusalLines(plain: boolean): string[] {
201
+ if (this.rejected.length === 0) return [];
202
+ if (plain) {
203
+ return this.rejected.map(
204
+ (p) => `error: Submodule '${this.superPrefix}${p}' cannot checkout new HEAD.`,
205
+ );
206
+ }
207
+ return [
208
+ ["error: Cannot update submodule:", ...this.rejected.map((p) => `\t${p}`), ""].join("\n"),
209
+ ];
210
+ }
211
+
212
+ /** The lines printed so far, as one text (each line ended). */
213
+ printed(): string {
214
+ return this.lines.join("");
215
+ }
216
+
217
+ /**
218
+ * A plain (read-tree, reset) refusal of `path`: what the submodules printed, then
219
+ * unpack-trees' own lines (`not uptodate` too when `verify_uptodate` refused it).
220
+ */
221
+ plainRefusal(path: string, notUptodate: boolean): string {
222
+ const lines = [...this.refusalLines(true)];
223
+ if (notUptodate) {
224
+ lines.push(`error: Entry '${this.superPrefix}${path}' not uptodate. Cannot merge.`);
225
+ }
226
+ return `${this.printed()}${lines.join("\n")}`;
227
+ }
228
+
229
+ /**
230
+ * A worktree file or link replacing the submodule at `path`: refused where its repository
231
+ * is a `.git` directory in it (a deliberate divergence: git's `checkout_entry` removes the
232
+ * whole directory, the submodule's only copy of its history included); else the directory
233
+ * goes, untracked files and all, as git's `remove_subtree`. True when refused.
234
+ */
235
+ async fileReplacesRefuses(path: string, old: Entry): Promise<boolean> {
236
+ if (!(await this.isSubmodule(path, old))) return false;
237
+ if (!(await this.hasGitDirectory(path))) return false;
238
+ if (!this.rejected.includes(path)) this.rejected.push(path);
239
+ return true;
240
+ }
241
+
242
+ /** git's `remove_subtree` of a submodule's directory a file replaces. */
243
+ async removeForFile(worktree: Worktree, path: string, old: Entry): Promise<void> {
244
+ if (!(await this.isSubmodule(path, old))) return;
245
+ if ((await (await this.m()).kind(path)) !== "directory") return;
246
+ if (await this.hasGitDirectory(path)) return;
247
+ await worktree.remove(path, { recursive: true, nonRepositoryGitDirs: true });
248
+ }
249
+
250
+ /** Runs a move, its printed lines kept; git's deaths propagate. */
251
+ private async run(move: () => Promise<boolean>): Promise<boolean> {
252
+ try {
253
+ return await move();
254
+ } catch (error) {
255
+ if (this.commands && error instanceof this.commands.SubmoduleCommandError) {
256
+ const text = `${(await this.m()).takeStderr()}${error.result.stderr}`;
257
+ throw new GitApiError(`${text}${error.message}`);
258
+ }
259
+ throw error;
260
+ } finally {
261
+ const text = (await this.m()).takeStderr();
262
+ if (text) this.lines.push(text);
263
+ }
264
+ }
265
+ }
@@ -12,7 +12,7 @@ import {
12
12
  type GitTransportRegistry,
13
13
  } from "@statewalker/vcs-transport";
14
14
  import type { PosixFilesApi } from "@statewalker/vcs-utils/files";
15
- import type { WorkingCopy } from "@statewalker/vcs-working-tree";
15
+ import { type WorkingCopy, wildmatch } from "@statewalker/vcs-working-tree";
16
16
 
17
17
  import { GitApiError } from "../errors/index.js";
18
18
  import { GitCommand } from "../git-command.js";
@@ -25,7 +25,7 @@ import { createDescriber } from "./git-describe.js";
25
25
  import { gitFileOf, hookRunnerOf } from "./git-hooks.js";
26
26
  import { hostEnvironmentOf } from "./git-ident.js";
27
27
  import { MergeCommand } from "./merge-command.js";
28
- import { nameRevByTags } from "./name-rev.js";
28
+ import { commitLoaderOf, nameRevByTags, withShallowParents } from "./name-rev.js";
29
29
  import { RebaseCommand } from "./rebase-command.js";
30
30
  import {
31
31
  type CredentialsForUrl,
@@ -128,7 +128,7 @@ class SubmoduleRepository extends GitCommand<never> {
128
128
  const source = {
129
129
  refs: this.refsStore,
130
130
  ...(tags ? { tags } : {}),
131
- commits: this.commits,
131
+ commits: withShallowParents(this.commits, () => this.shallowCommits()),
132
132
  };
133
133
  const describe = createDescriber({
134
134
  ...source,
@@ -142,7 +142,19 @@ class SubmoduleRepository extends GitCommand<never> {
142
142
  if (plain) return plain;
143
143
  const lightweight = await describe(id, { tags: true }).catch(() => "");
144
144
  if (lightweight) return lightweight;
145
- const contains = await nameRevByTags(source, id).catch(() => undefined);
145
+ const resolver = this.revisionResolver();
146
+ const contains = await nameRevByTags(
147
+ {
148
+ refs: this.refsStore,
149
+ objectType: (oid) => resolver.objectType(oid),
150
+ loadTag: async (oid) => tags?.load(oid),
151
+ loadCommit: commitLoaderOf(this.walkCommits, () => this.shallowCommits()),
152
+ refExists: (name) => this.refsStore.has(name).catch(() => false),
153
+ abbreviate: (oid) => this.abbreviateObjectId(oid),
154
+ wildmatch: (pattern, text) => wildmatch(pattern, text),
155
+ },
156
+ id,
157
+ ).catch(() => undefined);
146
158
  if (contains) return contains;
147
159
  return (await describe(id, { all: true, always: true }).catch(() => "")) || undefined;
148
160
  }
@@ -229,6 +241,10 @@ export class SubmoduleUpdateCommand extends SubmoduleCommandBase {
229
241
  private recommendShallow?: boolean;
230
242
  private references: string[] = [];
231
243
  private requireInit = false;
244
+ /** `--filter=<spec>` of the clones */
245
+ private filter?: string;
246
+ /** `--[no-]single-branch` of the clones */
247
+ private singleBranch?: boolean;
232
248
  private transports?: GitTransportRegistry;
233
249
  private credentials?: Credentials;
234
250
  private headers?: Record<string, string>;
@@ -312,6 +328,20 @@ export class SubmoduleUpdateCommand extends SubmoduleCommandBase {
312
328
  return this;
313
329
  }
314
330
 
331
+ /** `--filter=<spec>`: partial clones of the submodules (`CloneCommand.setFilter`). */
332
+ setFilter(spec: string | undefined): this {
333
+ this.checkCallable();
334
+ this.filter = spec;
335
+ return this;
336
+ }
337
+
338
+ /** `--[no-]single-branch`: how the submodules are cloned (`CloneCommand.setSingleBranch`). */
339
+ setSingleBranch(singleBranch: boolean | undefined): this {
340
+ this.checkCallable();
341
+ this.singleBranch = singleBranch;
342
+ return this;
343
+ }
344
+
315
345
  /** `--reference <repository>`: borrow objects in clones. */
316
346
  addReference(reference: string): this {
317
347
  this.checkCallable();
@@ -609,6 +639,8 @@ export class SubmoduleUpdateCommand extends SubmoduleCommandBase {
609
639
  ...(depth !== undefined ? { depth } : {}),
610
640
  references: this.references,
611
641
  requireInit: this.requireInit,
642
+ ...(this.filter !== undefined ? { filter: this.filter } : {}),
643
+ ...(this.singleBranch !== undefined ? { singleBranch: this.singleBranch } : {}),
612
644
  transports: await this.guardedTransports(),
613
645
  ...(this.timeout !== undefined ? { timeout: this.timeout } : {}),
614
646
  });
@@ -962,7 +994,9 @@ export class SubmoduleUpdateCommand extends SubmoduleCommandBase {
962
994
  .setNoFetch(this.noFetch)
963
995
  .setForce(this.force)
964
996
  .setMode(this.mode)
965
- .setDepth(this.depth);
997
+ .setDepth(this.depth)
998
+ .setFilter(this.filter)
999
+ .setSingleBranch(this.singleBranch);
966
1000
  if (this.recommendShallow !== undefined) child.setRecommendShallow(this.recommendShallow);
967
1001
  for (const reference of this.references) child.addReference(reference);
968
1002
  if (this.transports) child.setTransports(this.transports);
@@ -0,0 +1,215 @@
1
+ /**
2
+ * git's subtree shift (match-trees.c), which `-s subtree` and `-X subtree[=<path>]` apply to
3
+ * a merge's other side and its base before merge-ort reads them: a tree that matches a
4
+ * subdirectory of ours is shifted down into it (ours' tree with that directory replaced),
5
+ * a tree whose subdirectory matches ours is shifted up to it.
6
+ *
7
+ * ```ts
8
+ * const shifted = await shiftTree(trees, oursTree, theirsTree); // -s subtree
9
+ * const byPath = await shiftTreeBy(trees, oursTree, theirsTree, "git-gui"); // -X subtree=git-gui
10
+ * ```
11
+ *
12
+ * Trees are given by id, `undefined` for the empty tree; a shift down stores the spliced
13
+ * trees, as git writes them.
14
+ */
15
+
16
+ import type { ObjectId, TreeEntry } from "@statewalker/vcs-core";
17
+
18
+ import type { CommandTrees } from "../git-command.js";
19
+
20
+ const S_IFMT = 0o170000;
21
+ const isDir = (mode: number) => (mode & S_IFMT) === 0o040000;
22
+ const isLink = (mode: number) => (mode & S_IFMT) === 0o120000;
23
+
24
+ function scoreMissing(mode: number): number {
25
+ if (isDir(mode)) return -1000;
26
+ if (isLink(mode)) return -500;
27
+ return -50;
28
+ }
29
+
30
+ function scoreDiffers(m1: number, m2: number): number {
31
+ if (isDir(m1) !== isDir(m2)) return -100;
32
+ if (isLink(m1) !== isLink(m2)) return -50;
33
+ return -5;
34
+ }
35
+
36
+ function scoreMatches(m1: number, m2: number): number {
37
+ if (isDir(m1) !== isDir(m2)) return -100;
38
+ if (isLink(m1) !== isLink(m2)) return -50;
39
+ if (isDir(m1)) return 1000;
40
+ if (isLink(m1)) return 500;
41
+ return 250;
42
+ }
43
+
44
+ const encoder = new TextEncoder();
45
+
46
+ /** git's base_name_compare: names compared as bytes, a directory's as if `/` followed it. */
47
+ function baseNameCompare(a: TreeEntry, b: TreeEntry): number {
48
+ const x = encoder.encode(a.name);
49
+ const y = encoder.encode(b.name);
50
+ const len = Math.min(x.length, y.length);
51
+ for (let i = 0; i < len; i++) if (x[i] !== y[i]) return x[i] - y[i];
52
+ const c1 = x.length > len ? x[len] : isDir(a.mode) ? 0x2f : 0;
53
+ const c2 = y.length > len ? y[len] : isDir(b.mode) ? 0x2f : 0;
54
+ return c1 - c2;
55
+ }
56
+
57
+ async function entries(trees: CommandTrees, id: ObjectId | undefined): Promise<TreeEntry[]> {
58
+ if (id === undefined) return [];
59
+ const out: TreeEntry[] = [];
60
+ for await (const entry of trees.loadTree(id)) out.push(entry);
61
+ return out.sort(baseNameCompare);
62
+ }
63
+
64
+ /** match-trees.c's score_trees: how alike two trees are. */
65
+ async function scoreTrees(
66
+ trees: CommandTrees,
67
+ one: ObjectId | undefined,
68
+ two: ObjectId | undefined,
69
+ ): Promise<number> {
70
+ const a = await entries(trees, one);
71
+ const b = await entries(trees, two);
72
+ let score = 0;
73
+ let i = 0;
74
+ let j = 0;
75
+ while (i < a.length || j < b.length) {
76
+ const cmp = i < a.length && j < b.length ? baseNameCompare(a[i], b[j]) : i < a.length ? -1 : 1;
77
+ if (cmp < 0) score += scoreMissing(a[i++].mode);
78
+ else if (cmp > 0) score += scoreMissing(b[j++].mode);
79
+ else {
80
+ score +=
81
+ a[i].id !== b[j].id
82
+ ? scoreDiffers(a[i].mode, b[j].mode)
83
+ : scoreMatches(a[i].mode, b[j].mode);
84
+ i++;
85
+ j++;
86
+ }
87
+ }
88
+ return score;
89
+ }
90
+
91
+ /** match-trees.c's match_trees: the subtree of `one` (recursing) that best matches `two`. */
92
+ async function matchTrees(
93
+ trees: CommandTrees,
94
+ one: ObjectId | undefined,
95
+ two: ObjectId | undefined,
96
+ best: { score: number; match: string },
97
+ base: string,
98
+ recurseLimit: number,
99
+ ): Promise<void> {
100
+ for (const entry of await entries(trees, one)) {
101
+ if (!isDir(entry.mode)) continue;
102
+ const score = await scoreTrees(trees, entry.id, two);
103
+ if (best.score < score) {
104
+ best.match = `${base}${entry.name}`;
105
+ best.score = score;
106
+ }
107
+ if (recurseLimit) {
108
+ await matchTrees(trees, entry.id, two, best, `${base}${entry.name}/`, recurseLimit - 1);
109
+ }
110
+ }
111
+ }
112
+
113
+ /** get_tree_entry: the entry at `path` in tree `id`, undefined when a component is missing. */
114
+ async function treeEntryAt(
115
+ trees: CommandTrees,
116
+ id: ObjectId | undefined,
117
+ path: string,
118
+ ): Promise<{ id: ObjectId; mode: number } | undefined> {
119
+ const parts = path.split("/").filter((p) => p.length > 0);
120
+ if (parts.length === 0) return undefined;
121
+ let current: { id: ObjectId; mode: number } | undefined;
122
+ let tree = id;
123
+ for (const [k, name] of parts.entries()) {
124
+ const entry = (await entries(trees, tree)).find((e) => e.name === name);
125
+ if (!entry) return undefined;
126
+ current = { id: entry.id, mode: entry.mode };
127
+ if (k < parts.length - 1) {
128
+ if (!isDir(entry.mode)) return undefined;
129
+ tree = entry.id;
130
+ }
131
+ }
132
+ return current;
133
+ }
134
+
135
+ /** match-trees.c's splice_tree: tree `one` with its directory at `prefix` replaced by `two`. */
136
+ async function spliceTree(
137
+ trees: CommandTrees,
138
+ one: ObjectId | undefined,
139
+ prefix: string,
140
+ two: ObjectId | undefined,
141
+ emptyTree: () => Promise<ObjectId>,
142
+ ): Promise<ObjectId> {
143
+ const slash = prefix.indexOf("/");
144
+ const top = slash < 0 ? prefix : prefix.slice(0, slash);
145
+ const sub = slash < 0 ? "" : prefix.slice(slash + 1);
146
+ const list = await entries(trees, one);
147
+ const at = list.findIndex((e) => e.name === top);
148
+ if (at < 0) throw new Error(`entry ${top} not found in tree ${one ?? ""}`);
149
+ const found = list[at];
150
+ if (!isDir(found.mode)) throw new Error(`entry ${top} in tree ${one ?? ""} is not a tree`);
151
+ const replacement = sub
152
+ ? await spliceTree(trees, found.id, sub, two, emptyTree)
153
+ : (two ?? (await emptyTree()));
154
+ list[at] = { ...found, id: replacement };
155
+ return trees.storeTree(list);
156
+ }
157
+
158
+ /**
159
+ * match-trees.c's shift_tree: `two` shifted to match the shape of `one` (`-s subtree`,
160
+ * `-X subtree`), as the id of the tree merge-ort then reads.
161
+ */
162
+ export async function shiftTree(
163
+ trees: CommandTrees,
164
+ one: ObjectId | undefined,
165
+ two: ObjectId | undefined,
166
+ emptyTree: () => Promise<ObjectId>,
167
+ depthLimit = 2,
168
+ ): Promise<ObjectId | undefined> {
169
+ const initial = await scoreTrees(trees, one, two);
170
+ const add = { score: initial, match: "" };
171
+ const del = { score: initial, match: "" };
172
+ await matchTrees(trees, one, two, add, "", depthLimit);
173
+ await matchTrees(trees, two, one, del, "", depthLimit);
174
+ if (add.score < del.score) {
175
+ // a subtree of two
176
+ if (!del.match) return two;
177
+ const entry = await treeEntryAt(trees, two, del.match);
178
+ if (!entry) throw new Error(`cannot find path ${del.match} in tree ${two ?? ""}`);
179
+ return entry.id;
180
+ }
181
+ if (!add.match) return two;
182
+ return spliceTree(trees, one, add.match, two, emptyTree);
183
+ }
184
+
185
+ /**
186
+ * match-trees.c's shift_tree_by: `two` shifted by `prefix` (`-X subtree=<path>`), down when
187
+ * `one` holds a directory there (and that matches better), up when `two` does.
188
+ */
189
+ export async function shiftTreeBy(
190
+ trees: CommandTrees,
191
+ one: ObjectId | undefined,
192
+ two: ObjectId | undefined,
193
+ prefix: string,
194
+ emptyTree: () => Promise<ObjectId>,
195
+ ): Promise<ObjectId | undefined> {
196
+ const sub1 = await treeEntryAt(trees, one, prefix);
197
+ const sub2 = await treeEntryAt(trees, two, prefix);
198
+ let candidate = 0;
199
+ if (sub1 && isDir(sub1.mode)) candidate |= 1;
200
+ if (sub2 && isDir(sub2.mode)) candidate |= 2;
201
+ if (candidate === 3) {
202
+ let best = await scoreTrees(trees, one, two);
203
+ candidate = 0;
204
+ let score = await scoreTrees(trees, sub1?.id, two);
205
+ if (score > best) {
206
+ candidate = 1;
207
+ best = score;
208
+ }
209
+ score = await scoreTrees(trees, sub2?.id, one);
210
+ if (score > best) candidate = 2;
211
+ }
212
+ if (!candidate) return two;
213
+ if (candidate === 1) return spliceTree(trees, one, prefix, two, emptyTree);
214
+ return sub2?.id;
215
+ }
@@ -63,6 +63,7 @@ export class SwitchCommand extends GitCommand<CheckoutResult> {
63
63
  private ignoreOtherWorktrees = false;
64
64
  private merge = false;
65
65
  private compatibility: "2.43" | "2.56" = "2.56";
66
+ private recurseSubmodules?: boolean;
66
67
 
67
68
  /**
68
69
  * The branch to switch to; with `-c`/`-C` the start point, with `--detach` the commit.
@@ -186,6 +187,13 @@ export class SwitchCommand extends GitCommand<CheckoutResult> {
186
187
  return this;
187
188
  }
188
189
 
190
+ /** `--[no-]recurse-submodules` (default `submodule.recurse`): see `CheckoutCommand`. */
191
+ setRecurseSubmodules(recurse: boolean): this {
192
+ this.checkCallable();
193
+ this.recurseSubmodules = recurse;
194
+ return this;
195
+ }
196
+
189
197
  /** `--ignore-other-worktrees`: switch to a branch another worktree has checked out. */
190
198
  setIgnoreOtherWorktrees(ignore: boolean): this {
191
199
  this.checkCallable();
@@ -286,6 +294,7 @@ export class SwitchCommand extends GitCommand<CheckoutResult> {
286
294
  .setAdviceCommand("switch")
287
295
  .setOnlyMergeOnSwitchingBranches(true);
288
296
  if (this.guess !== undefined) checkout.setGuess(this.guess);
297
+ if (this.recurseSubmodules !== undefined) checkout.setRecurseSubmodules(this.recurseSubmodules);
289
298
  if (this.hasReflogAction()) checkout.setReflogAction(this.reflogAction("checkout"));
290
299
  if (this.orphan !== undefined) {
291
300
  checkout.setOrphan(true).setOrphanFromEmptyTree(true).setName(this.orphan);
@@ -334,7 +334,12 @@ async function writePlan(
334
334
  }
335
335
  // a submodule is an empty directory, as git checks out an unpopulated one
336
336
  if (options.gitlinks) {
337
- for (const { path } of plan.gitlinks) await worktree.mkdir(path);
337
+ for (const { path } of plan.gitlinks) {
338
+ // checkout_entry with force: a file or link standing there is unlinked first (a
339
+ // directory, its contents included, stays)
340
+ if (options.replaceInTheWay) await worktree.remove(path);
341
+ await worktree.mkdir(path);
342
+ }
338
343
  }
339
344
  }
340
345
 
@@ -414,6 +414,8 @@ export function overwriteMessage(
414
414
  lostDirs?: string[];
415
415
  untracked?: string[];
416
416
  untrackedRemoved?: string[];
417
+ /** Submodules that cannot be moved (git's ERROR_WOULD_LOSE_SUBMODULE), listed last */
418
+ submodules?: string[];
417
419
  },
418
420
  ): string | undefined {
419
421
  const list = (paths: string[]) => paths.map((p) => `\t${p}`);
@@ -463,6 +465,9 @@ export function overwriteMessage(
463
465
  ].join("\n"),
464
466
  );
465
467
  }
468
+ if (found.submodules?.length) {
469
+ parts.push(["error: Cannot update submodule:", ...list(found.submodules), ""].join("\n"));
470
+ }
466
471
  return parts.length > 0 ? [...parts, "Aborting"].join("\n") : undefined;
467
472
  }
468
473