@statewalker/vcs-commands 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/dist/commands/add-command.d.ts +32 -0
  2. package/dist/commands/add-command.d.ts.map +1 -1
  3. package/dist/commands/add-patch/add-patch.d.ts +7 -0
  4. package/dist/commands/add-patch/add-patch.d.ts.map +1 -1
  5. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
  6. package/dist/commands/am/am-command.d.ts +47 -0
  7. package/dist/commands/am/am-command.d.ts.map +1 -1
  8. package/dist/commands/apply/apply-command.d.ts +2 -0
  9. package/dist/commands/apply/apply-command.d.ts.map +1 -1
  10. package/dist/commands/apply/apply-parse.d.ts +7 -0
  11. package/dist/commands/apply/apply-parse.d.ts.map +1 -1
  12. package/dist/commands/apply/index.d.ts +1 -1
  13. package/dist/commands/apply/index.d.ts.map +1 -1
  14. package/dist/commands/blame-command.d.ts +303 -8
  15. package/dist/commands/blame-command.d.ts.map +1 -1
  16. package/dist/commands/blame-ignore.d.ts +48 -0
  17. package/dist/commands/blame-ignore.d.ts.map +1 -0
  18. package/dist/commands/check-mailmap-command.d.ts +49 -0
  19. package/dist/commands/check-mailmap-command.d.ts.map +1 -0
  20. package/dist/commands/checkout-command.d.ts +7 -0
  21. package/dist/commands/checkout-command.d.ts.map +1 -1
  22. package/dist/commands/checkout-paths.d.ts +4 -0
  23. package/dist/commands/checkout-paths.d.ts.map +1 -1
  24. package/dist/commands/cherry-command.d.ts +61 -0
  25. package/dist/commands/cherry-command.d.ts.map +1 -0
  26. package/dist/commands/cherry-pick-command.d.ts +23 -1
  27. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  28. package/dist/commands/clean-command.d.ts +32 -0
  29. package/dist/commands/clean-command.d.ts.map +1 -1
  30. package/dist/commands/clean-interactive.d.ts +56 -0
  31. package/dist/commands/clean-interactive.d.ts.map +1 -0
  32. package/dist/commands/clone-command.d.ts +116 -2
  33. package/dist/commands/clone-command.d.ts.map +1 -1
  34. package/dist/commands/commit-command.d.ts +50 -2
  35. package/dist/commands/commit-command.d.ts.map +1 -1
  36. package/dist/commands/describe-command.d.ts +71 -64
  37. package/dist/commands/describe-command.d.ts.map +1 -1
  38. package/dist/commands/diff-command.d.ts +118 -6
  39. package/dist/commands/diff-command.d.ts.map +1 -1
  40. package/dist/commands/fetch-command.d.ts +60 -0
  41. package/dist/commands/fetch-command.d.ts.map +1 -1
  42. package/dist/commands/fetch-multiple-command.d.ts +193 -0
  43. package/dist/commands/fetch-multiple-command.d.ts.map +1 -0
  44. package/dist/commands/format-patch-command.d.ts +28 -2
  45. package/dist/commands/format-patch-command.d.ts.map +1 -1
  46. package/dist/commands/git-describe.d.ts +77 -4
  47. package/dist/commands/git-describe.d.ts.map +1 -1
  48. package/dist/commands/git-hooks.d.ts +15 -1
  49. package/dist/commands/git-hooks.d.ts.map +1 -1
  50. package/dist/commands/index.d.ts +12 -0
  51. package/dist/commands/index.d.ts.map +1 -1
  52. package/dist/commands/index.js +5 -3
  53. package/dist/commands/init-command.d.ts +89 -0
  54. package/dist/commands/init-command.d.ts.map +1 -1
  55. package/dist/commands/line-log.d.ts +161 -0
  56. package/dist/commands/line-log.d.ts.map +1 -0
  57. package/dist/commands/log-command.d.ts +235 -6
  58. package/dist/commands/log-command.d.ts.map +1 -1
  59. package/dist/commands/log-graph.d.ts +4 -2
  60. package/dist/commands/log-graph.d.ts.map +1 -1
  61. package/dist/commands/log-output.d.ts +66 -1
  62. package/dist/commands/log-output.d.ts.map +1 -1
  63. package/dist/commands/mailmap.d.ts +24 -1
  64. package/dist/commands/mailmap.d.ts.map +1 -1
  65. package/dist/commands/merge-command.d.ts +51 -1
  66. package/dist/commands/merge-command.d.ts.map +1 -1
  67. package/dist/commands/merge-drivers.d.ts +2 -0
  68. package/dist/commands/merge-drivers.d.ts.map +1 -1
  69. package/dist/commands/merge-strategy-options.d.ts +69 -0
  70. package/dist/commands/merge-strategy-options.d.ts.map +1 -0
  71. package/dist/commands/merge-support.d.ts +10 -0
  72. package/dist/commands/merge-support.d.ts.map +1 -1
  73. package/dist/commands/message-cleanup.d.ts +2 -0
  74. package/dist/commands/message-cleanup.d.ts.map +1 -1
  75. package/dist/commands/name-rev-command.d.ts +65 -0
  76. package/dist/commands/name-rev-command.d.ts.map +1 -0
  77. package/dist/commands/name-rev.d.ts +110 -22
  78. package/dist/commands/name-rev.d.ts.map +1 -1
  79. package/dist/commands/octopus-merge.d.ts +26 -1
  80. package/dist/commands/octopus-merge.d.ts.map +1 -1
  81. package/dist/commands/ort-merge.d.ts +16 -0
  82. package/dist/commands/ort-merge.d.ts.map +1 -1
  83. package/dist/commands/ort-recursive.d.ts.map +1 -1
  84. package/dist/commands/patch-id.d.ts +58 -0
  85. package/dist/commands/patch-id.d.ts.map +1 -1
  86. package/dist/commands/pull-command.d.ts +182 -38
  87. package/dist/commands/pull-command.d.ts.map +1 -1
  88. package/dist/commands/push-command.d.ts +65 -0
  89. package/dist/commands/push-command.d.ts.map +1 -1
  90. package/dist/commands/range-diff/index.d.ts +4 -0
  91. package/dist/commands/range-diff/index.d.ts.map +1 -0
  92. package/dist/commands/range-diff/linear-assignment.d.ts +27 -0
  93. package/dist/commands/range-diff/linear-assignment.d.ts.map +1 -0
  94. package/dist/commands/range-diff/range-diff-command.d.ts +121 -0
  95. package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -0
  96. package/dist/commands/range-diff/range-diff.d.ts +106 -0
  97. package/dist/commands/range-diff/range-diff.d.ts.map +1 -0
  98. package/dist/commands/read-tree-command.d.ts +14 -0
  99. package/dist/commands/read-tree-command.d.ts.map +1 -1
  100. package/dist/commands/rebase-command.d.ts +21 -2
  101. package/dist/commands/rebase-command.d.ts.map +1 -1
  102. package/dist/commands/remerge-diff.d.ts +59 -0
  103. package/dist/commands/remerge-diff.d.ts.map +1 -0
  104. package/dist/commands/remote-command.d.ts +41 -0
  105. package/dist/commands/remote-command.d.ts.map +1 -1
  106. package/dist/commands/remote-show-command.d.ts +204 -0
  107. package/dist/commands/remote-show-command.d.ts.map +1 -0
  108. package/dist/commands/reset-command.d.ts +19 -1
  109. package/dist/commands/reset-command.d.ts.map +1 -1
  110. package/dist/commands/restore-command.d.ts +7 -0
  111. package/dist/commands/restore-command.d.ts.map +1 -1
  112. package/dist/commands/rev-list-command.d.ts +75 -3
  113. package/dist/commands/rev-list-command.d.ts.map +1 -1
  114. package/dist/commands/revert-command.d.ts +22 -1
  115. package/dist/commands/revert-command.d.ts.map +1 -1
  116. package/dist/commands/revision-range.d.ts +2 -0
  117. package/dist/commands/revision-range.d.ts.map +1 -1
  118. package/dist/commands/sequencer-pick.d.ts +102 -1
  119. package/dist/commands/sequencer-pick.d.ts.map +1 -1
  120. package/dist/commands/sequencer-support.d.ts +10 -1
  121. package/dist/commands/sequencer-support.d.ts.map +1 -1
  122. package/dist/commands/shortlog-command.d.ts +109 -0
  123. package/dist/commands/shortlog-command.d.ts.map +1 -0
  124. package/dist/commands/shortlog.d.ts +151 -0
  125. package/dist/commands/shortlog.d.ts.map +1 -0
  126. package/dist/commands/status-command.d.ts +31 -2
  127. package/dist/commands/status-command.d.ts.map +1 -1
  128. package/dist/commands/submodule-commands.d.ts +90 -2
  129. package/dist/commands/submodule-commands.d.ts.map +1 -1
  130. package/dist/commands/submodule-fetch.d.ts +149 -0
  131. package/dist/commands/submodule-fetch.d.ts.map +1 -0
  132. package/dist/commands/submodule-model.d.ts +8 -0
  133. package/dist/commands/submodule-model.d.ts.map +1 -1
  134. package/dist/commands/submodule-unpack.d.ts +120 -0
  135. package/dist/commands/submodule-unpack.d.ts.map +1 -0
  136. package/dist/commands/submodule-update-command.d.ts +8 -0
  137. package/dist/commands/submodule-update-command.d.ts.map +1 -1
  138. package/dist/commands/subtree-shift.d.ts +27 -0
  139. package/dist/commands/subtree-shift.d.ts.map +1 -0
  140. package/dist/commands/switch-command.d.ts +3 -0
  141. package/dist/commands/switch-command.d.ts.map +1 -1
  142. package/dist/commands/tree-restore.d.ts.map +1 -1
  143. package/dist/commands/worktree-overwrite-checks.d.ts +2 -0
  144. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
  145. package/dist/{commands-ISM52N0n.js → commands-CNvli5bl.js} +39426 -61169
  146. package/dist/errors/command-errors.d.ts +25 -0
  147. package/dist/errors/command-errors.d.ts.map +1 -1
  148. package/dist/errors/index.js +29 -1
  149. package/dist/fsck-CPeDh9fh.js +5138 -0
  150. package/dist/git-command.d.ts +31 -1
  151. package/dist/git-command.d.ts.map +1 -1
  152. package/dist/git.d.ts +97 -1
  153. package/dist/git.d.ts.map +1 -1
  154. package/dist/guarded-transport-command.d.ts +82 -0
  155. package/dist/guarded-transport-command.d.ts.map +1 -0
  156. package/dist/index.d.ts +1 -0
  157. package/dist/index.d.ts.map +1 -1
  158. package/dist/index.js +6 -5
  159. package/dist/remote-transport-CCmBadm-.js +1606 -0
  160. package/dist/rename/detect-renames.d.ts +20 -0
  161. package/dist/rename/detect-renames.d.ts.map +1 -1
  162. package/dist/rename/diffcore-break.d.ts +51 -0
  163. package/dist/rename/diffcore-break.d.ts.map +1 -0
  164. package/dist/rename/index.d.ts +1 -0
  165. package/dist/rename/index.d.ts.map +1 -1
  166. package/dist/results/blame-format.d.ts +62 -1
  167. package/dist/results/blame-format.d.ts.map +1 -1
  168. package/dist/results/clone-result.d.ts +9 -0
  169. package/dist/results/clone-result.d.ts.map +1 -1
  170. package/dist/results/diff-dirstat.d.ts +36 -0
  171. package/dist/results/diff-dirstat.d.ts.map +1 -0
  172. package/dist/results/diff-entry.d.ts +10 -0
  173. package/dist/results/diff-entry.d.ts.map +1 -1
  174. package/dist/results/diff-formatter.d.ts.map +1 -1
  175. package/dist/results/diff-output.d.ts +18 -0
  176. package/dist/results/diff-output.d.ts.map +1 -1
  177. package/dist/results/fetch-result.d.ts +12 -1
  178. package/dist/results/fetch-result.d.ts.map +1 -1
  179. package/dist/results/index.d.ts +2 -0
  180. package/dist/results/index.d.ts.map +1 -1
  181. package/dist/results/index.js +2 -2
  182. package/dist/results/init-result.d.ts +8 -1
  183. package/dist/results/init-result.d.ts.map +1 -1
  184. package/dist/results/line-range-hunks.d.ts +27 -0
  185. package/dist/results/line-range-hunks.d.ts.map +1 -0
  186. package/dist/results/merge-result.d.ts +8 -1
  187. package/dist/results/merge-result.d.ts.map +1 -1
  188. package/dist/results/pull-result.d.ts +11 -0
  189. package/dist/results/pull-result.d.ts.map +1 -1
  190. package/dist/results/push-result.d.ts +5 -0
  191. package/dist/results/push-result.d.ts.map +1 -1
  192. package/dist/results/status-result.d.ts +1 -1
  193. package/dist/results/status-result.d.ts.map +1 -1
  194. package/dist/results-BDuEgdLU.js +46847 -0
  195. package/dist/transport-command.d.ts +14 -0
  196. package/dist/transport-command.d.ts.map +1 -1
  197. package/package.json +12 -12
  198. package/src/commands/add-command.ts +115 -0
  199. package/src/commands/add-patch/add-patch.ts +8 -0
  200. package/src/commands/add-patch/patch-backend.ts +20 -2
  201. package/src/commands/am/am-command.ts +138 -4
  202. package/src/commands/apply/apply-command.ts +52 -9
  203. package/src/commands/apply/apply-parse.ts +10 -0
  204. package/src/commands/apply/index.ts +8 -1
  205. package/src/commands/blame-command.ts +1450 -51
  206. package/src/commands/blame-ignore.ts +307 -0
  207. package/src/commands/check-mailmap-command.ts +129 -0
  208. package/src/commands/checkout-command.ts +97 -12
  209. package/src/commands/checkout-paths.ts +28 -0
  210. package/src/commands/cherry-command.ts +187 -0
  211. package/src/commands/cherry-pick-command.ts +42 -1
  212. package/src/commands/clean-command.ts +92 -12
  213. package/src/commands/clean-interactive.ts +522 -0
  214. package/src/commands/clone-command.ts +596 -44
  215. package/src/commands/commit-command.ts +180 -47
  216. package/src/commands/describe-command.ts +341 -254
  217. package/src/commands/diff-command.ts +616 -56
  218. package/src/commands/fetch-command.ts +353 -6
  219. package/src/commands/fetch-multiple-command.ts +665 -0
  220. package/src/commands/format-patch-command.ts +189 -77
  221. package/src/commands/git-describe.ts +456 -217
  222. package/src/commands/git-hooks.ts +42 -3
  223. package/src/commands/index.ts +17 -0
  224. package/src/commands/init-command.ts +603 -49
  225. package/src/commands/line-log.ts +931 -0
  226. package/src/commands/log-command.ts +1414 -177
  227. package/src/commands/log-graph.ts +6 -3
  228. package/src/commands/log-output.ts +173 -10
  229. package/src/commands/mailmap.ts +41 -5
  230. package/src/commands/merge-command.ts +274 -16
  231. package/src/commands/merge-drivers.ts +3 -0
  232. package/src/commands/merge-strategy-options.ts +224 -0
  233. package/src/commands/merge-support.ts +101 -21
  234. package/src/commands/message-cleanup.ts +1 -1
  235. package/src/commands/name-rev-command.ts +184 -0
  236. package/src/commands/name-rev.ts +700 -109
  237. package/src/commands/octopus-merge.ts +35 -11
  238. package/src/commands/ort-merge.ts +41 -7
  239. package/src/commands/ort-recursive.ts +40 -2
  240. package/src/commands/patch-id.ts +109 -0
  241. package/src/commands/pull-command.ts +718 -224
  242. package/src/commands/push-command.ts +353 -3
  243. package/src/commands/range-diff/index.ts +3 -0
  244. package/src/commands/range-diff/linear-assignment.ts +203 -0
  245. package/src/commands/range-diff/range-diff-command.ts +433 -0
  246. package/src/commands/range-diff/range-diff.ts +617 -0
  247. package/src/commands/read-tree-command.ts +74 -3
  248. package/src/commands/rebase-command.ts +141 -65
  249. package/src/commands/remerge-diff.ts +241 -0
  250. package/src/commands/remote-command.ts +98 -10
  251. package/src/commands/remote-show-command.ts +836 -0
  252. package/src/commands/reset-command.ts +78 -3
  253. package/src/commands/restore-command.ts +15 -0
  254. package/src/commands/rev-list-command.ts +187 -11
  255. package/src/commands/revert-command.ts +41 -1
  256. package/src/commands/revision-range.ts +3 -1
  257. package/src/commands/sequencer-pick.ts +323 -29
  258. package/src/commands/sequencer-support.ts +164 -2
  259. package/src/commands/shortlog-command.ts +354 -0
  260. package/src/commands/shortlog.ts +361 -0
  261. package/src/commands/show-command.ts +2 -1
  262. package/src/commands/status-command.ts +115 -25
  263. package/src/commands/status-format.ts +1 -1
  264. package/src/commands/submodule-commands.ts +321 -3
  265. package/src/commands/submodule-fetch.ts +789 -0
  266. package/src/commands/submodule-model.ts +9 -1
  267. package/src/commands/submodule-unpack.ts +265 -0
  268. package/src/commands/submodule-update-command.ts +39 -5
  269. package/src/commands/subtree-shift.ts +215 -0
  270. package/src/commands/switch-command.ts +9 -0
  271. package/src/commands/tree-restore.ts +6 -1
  272. package/src/commands/worktree-overwrite-checks.ts +5 -0
  273. package/src/errors/command-errors.ts +35 -0
  274. package/src/git-command.ts +87 -1
  275. package/src/git.ts +146 -0
  276. package/src/guarded-transport-command.ts +171 -0
  277. package/src/index.ts +1 -0
  278. package/src/rename/detect-renames.ts +42 -8
  279. package/src/rename/diffcore-break.ts +166 -0
  280. package/src/rename/index.ts +1 -0
  281. package/src/results/blame-format.ts +268 -43
  282. package/src/results/clone-result.ts +6 -0
  283. package/src/results/diff-dirstat.ts +117 -0
  284. package/src/results/diff-entry.ts +12 -0
  285. package/src/results/diff-formatter.ts +1 -0
  286. package/src/results/diff-output.ts +127 -2
  287. package/src/results/fetch-result.ts +12 -1
  288. package/src/results/index.ts +2 -0
  289. package/src/results/init-result.ts +10 -1
  290. package/src/results/line-range-hunks.ts +126 -0
  291. package/src/results/merge-result.ts +9 -0
  292. package/src/results/pull-result.ts +7 -0
  293. package/src/results/push-result.ts +5 -0
  294. package/src/results/status-result.ts +2 -2
  295. package/src/transport-command.ts +34 -3
  296. package/dist/fsck-DLRpbwTj.js +0 -13405
  297. package/dist/results-TB2IJ64h.js +0 -4700
@@ -0,0 +1,522 @@
1
+ /**
2
+ * `git clean -i` (builtin/clean.c's `interactive_main_loop`): the items to delete listed in
3
+ * columns, the "*** Commands ***" menu (clean, filter by pattern, select by numbers, ask
4
+ * each, quit, help) and its actions, each read of standard input answered by a
5
+ * `PatchSelector` (the menus as `choose` requests, the patterns as a `line`, each "Remove
6
+ * ...?" as a `yesno`), and what git prints gathered as its standard output.
7
+ */
8
+
9
+ import { wildmatch } from "@statewalker/vcs-working-tree";
10
+
11
+ import { quotePath } from "../results/quote-path.js";
12
+ import type { PatchRequest, PatchSelector } from "./add-patch/index.js";
13
+
14
+ /** What `git clean -i` printed. */
15
+ export interface CleanInteractiveResult {
16
+ /** Standard output, as git prints it: listings, menus, prompts, help */
17
+ output: string;
18
+ }
19
+
20
+ /** column.c's layouts (`column.ui`, `column.clean`). */
21
+ export interface ColumnLayout {
22
+ layout: "column" | "row" | "plain";
23
+ dense: boolean;
24
+ }
25
+
26
+ /** Options of {@link runCleanInteractive}. */
27
+ export interface CleanInteractiveOptions {
28
+ selector: PatchSelector;
29
+ /** The items to delete, as git lists them (directories with their `/`), in order */
30
+ items: string[];
31
+ /** Whether an item is a directory (`lstat`'s answer for the pattern filter) */
32
+ isDirectory: (item: string) => boolean;
33
+ /** `term_columns()` (default 80: `$COLUMNS`, else the terminal's width) */
34
+ columns?: number;
35
+ /** The layout of the item listing (default `column`, not dense) */
36
+ layout?: ColumnLayout;
37
+ /** `core.quotePath` (default true) */
38
+ quotePath?: boolean;
39
+ /** `core.ignoreCase`: patterns match ignoring case */
40
+ ignoreCase?: boolean;
41
+ /** git 2.43 prints the whole input of a pattern filter that matched nothing */
42
+ compatibility?: "2.43" | "2.56";
43
+ }
44
+
45
+ /** Reads `column.ui` / `column.clean` values (the later one wins for each part). */
46
+ export function parseColumnLayout(values: readonly string[]): ColumnLayout {
47
+ const out: ColumnLayout = { layout: "column", dense: false };
48
+ for (const value of values) {
49
+ for (const word of value.split(/[ ,]+/)) {
50
+ const w = word.toLowerCase();
51
+ if (w === "column" || w === "row" || w === "plain") out.layout = w;
52
+ else if (w === "dense") out.dense = true;
53
+ else if (w === "nodense") out.dense = false;
54
+ }
55
+ }
56
+ return out;
57
+ }
58
+
59
+ /** utf8_strnwidth: the display width (one column per character here). */
60
+ const itemLength = (s: string) => [...s].length;
61
+
62
+ /**
63
+ * column.c's `print_columns` with the layout always enabled: `indent` before each line,
64
+ * `padding` spaces between cells, `width` the terminal's columns less one.
65
+ */
66
+ export function printColumns(
67
+ list: readonly string[],
68
+ layout: ColumnLayout,
69
+ options: { indent: string; padding: number; width: number },
70
+ ): string {
71
+ if (list.length === 0) return "";
72
+ const { indent, padding } = options;
73
+ if (layout.layout === "plain") return list.map((s) => `${indent}${s}\n`).join("");
74
+ const n = list.length;
75
+ const len = list.map(itemLength);
76
+ const column = layout.layout === "column";
77
+ let initialWidth = Math.max(...len) + padding;
78
+ let cols = Math.floor((options.width - indent.length) / initialWidth);
79
+ if (cols === 0) cols = 1;
80
+ let rows = Math.ceil(n / cols);
81
+ const linear = (x: number, y: number) => (column ? x * rows + y : y * cols + x);
82
+ let width: number[] | undefined;
83
+ const computeWidths = () => {
84
+ const w: number[] = [];
85
+ for (let x = 0; x < cols; x++) {
86
+ w[x] = linear(x, 0);
87
+ for (let y = 0; y < rows; y++) {
88
+ const i = linear(x, y);
89
+ if (i < n && len[w[x] as number] < len[i]) w[x] = i;
90
+ }
91
+ }
92
+ return w;
93
+ };
94
+ if (layout.dense) {
95
+ while (rows > 1) {
96
+ const oldRows = rows;
97
+ const oldCols = cols;
98
+ rows--;
99
+ cols = Math.ceil(n / rows);
100
+ const w = computeWidths();
101
+ let total = indent.length;
102
+ for (let x = 0; x < cols; x++) total += len[w[x] as number] + padding;
103
+ if (total > options.width) {
104
+ rows = oldRows;
105
+ cols = oldCols;
106
+ break;
107
+ }
108
+ }
109
+ width = computeWidths();
110
+ }
111
+ initialWidth = Math.max(...len) + padding;
112
+ let out = "";
113
+ for (let y = 0; y < rows; y++) {
114
+ for (let x = 0; x < cols; x++) {
115
+ const i = linear(x, y);
116
+ if (i >= n) break;
117
+ let l = len[i] as number;
118
+ if (width && len[width[x] as number] < initialWidth) {
119
+ l += initialWidth - len[width[x] as number];
120
+ l -= padding;
121
+ }
122
+ const newline = column ? i + rows >= n : x === cols - 1 || i === n - 1;
123
+ out += `${x === 0 ? indent : ""}${list[i]}${newline ? "\n" : " ".repeat(Math.max(0, initialWidth - l))}`;
124
+ }
125
+ }
126
+ return out;
127
+ }
128
+
129
+ interface MenuItem {
130
+ hotkey: string;
131
+ title: string;
132
+ }
133
+
134
+ const MENU: readonly MenuItem[] = [
135
+ { hotkey: "c", title: "clean" },
136
+ { hotkey: "f", title: "filter by pattern" },
137
+ { hotkey: "s", title: "select by numbers" },
138
+ { hotkey: "a", title: "ask each" },
139
+ { hotkey: "q", title: "quit" },
140
+ { hotkey: "h", title: "help" },
141
+ ];
142
+
143
+ const isDigit = (c: string) => c >= "0" && c <= "9";
144
+
145
+ /** atoi: the leading digits' value (0 for none). */
146
+ const atoi = (s: string) => {
147
+ const m = /^\d+/.exec(s);
148
+ return m ? Number(m[0]) : 0;
149
+ };
150
+
151
+ const lower = (s: string) => s.replace(/[A-Z]/g, (c) => c.toLowerCase());
152
+
153
+ /** A pattern of `add_pattern` (parse_path_pattern's flags). */
154
+ interface Pattern {
155
+ pattern: string;
156
+ negative: boolean;
157
+ mustBeDir: boolean;
158
+ noDir: boolean;
159
+ }
160
+
161
+ function parsePattern(text: string): Pattern {
162
+ let p = text;
163
+ let negative = false;
164
+ if (p.startsWith("!")) {
165
+ negative = true;
166
+ p = p.slice(1);
167
+ }
168
+ let mustBeDir = false;
169
+ if (p.length > 0 && p.endsWith("/")) {
170
+ mustBeDir = true;
171
+ p = p.slice(0, -1);
172
+ }
173
+ return { pattern: p, negative, mustBeDir, noDir: !p.includes("/") };
174
+ }
175
+
176
+ /** The input up to the end of its first space-separated piece, its trailing blanks cut. */
177
+ function firstPieceOf(input: string): string {
178
+ let p = 0;
179
+ while (p < input.length && /\s/.test(input[p] as string)) p++;
180
+ let end = input.indexOf(" ", p);
181
+ if (end < 0) end = input.length;
182
+ while (end > p && /\s/.test(input[end - 1] as string)) end--;
183
+ return input.slice(0, end);
184
+ }
185
+
186
+ /** A `git clean -i` session. */
187
+ class CleanInteractiveSession {
188
+ private out = "";
189
+ private del: string[];
190
+ private readonly width: number;
191
+
192
+ constructor(private readonly options: CleanInteractiveOptions) {
193
+ this.del = [...options.items];
194
+ this.width = (options.columns ?? 80) - 1;
195
+ }
196
+
197
+ private quote(item: string): string {
198
+ return quotePath(item, this.options.quotePath ?? true);
199
+ }
200
+
201
+ private prettyPrintDels(): void {
202
+ this.out += printColumns(
203
+ this.del.map((item) => this.quote(item)),
204
+ this.options.layout ?? { layout: "column", dense: false },
205
+ { indent: " ", padding: 2, width: this.width },
206
+ );
207
+ }
208
+
209
+ private async read(request: PatchRequest): Promise<string | undefined> {
210
+ const reply = await this.options.selector(request);
211
+ // git_read_line_interactively: the line without its line end
212
+ return reply?.replace(/\r?\n$/, "");
213
+ }
214
+
215
+ async run(): Promise<{ items: string[]; output: string }> {
216
+ while (this.del.length > 0) {
217
+ this.out += `Would remove the following item${this.del.length === 1 ? "" : "s"}:\n`;
218
+ this.prettyPrintDels();
219
+ const chosen = await this.listAndChoose(
220
+ MENU.map((m) => m.title),
221
+ {
222
+ header: "*** Commands ***",
223
+ prompt: "What now",
224
+ singleton: true,
225
+ },
226
+ (i, selected) => {
227
+ const m = MENU[i] as MenuItem;
228
+ return `${selected ? "*" : " "}${String(i + 1).padStart(2)}: ${m.title}`;
229
+ },
230
+ );
231
+ if (chosen === undefined) {
232
+ this.quit();
233
+ break;
234
+ }
235
+ const title = MENU[chosen[0] as number]?.title;
236
+ let loop = true;
237
+ if (title === "clean") loop = false;
238
+ else if (title === "filter by pattern") await this.filterByPatterns();
239
+ else if (title === "select by numbers") await this.selectByNumbers();
240
+ else if (title === "ask each") {
241
+ await this.askEach();
242
+ loop = false;
243
+ } else if (title === "quit") {
244
+ this.quit();
245
+ loop = false;
246
+ } else if (title === "help") this.help();
247
+ if (!loop) break;
248
+ if (this.del.length === 0) {
249
+ this.out += "No more files to clean, exiting.\n";
250
+ break;
251
+ }
252
+ }
253
+ return { items: this.del, output: this.out };
254
+ }
255
+
256
+ private quit(): void {
257
+ this.del = [];
258
+ this.out += "Bye.\n";
259
+ }
260
+
261
+ private help(): void {
262
+ this.out +=
263
+ "clean - start cleaning\n" +
264
+ "filter by pattern - exclude items from deletion\n" +
265
+ "select by numbers - select items to be deleted by numbers\n" +
266
+ 'ask each - confirm each deletion (like "rm -i")\n' +
267
+ "quit - stop cleaning\n" +
268
+ "help - this screen\n" +
269
+ "? - help for prompt selection\n";
270
+ }
271
+
272
+ /** `find_unique`: the 1-based item `choice` names, 0 for none or several. */
273
+ private findUnique(choice: string, titles: readonly string[], menu: boolean): number {
274
+ const len = choice.length;
275
+ let found = 0;
276
+ const c = lower(choice);
277
+ for (let i = 0; i < titles.length; i++) {
278
+ const title = titles[i] as string;
279
+ if (menu && len === 1 && choice === MENU[i]?.hotkey) return i + 1;
280
+ if (lower(title.slice(0, len)) === c && title.length >= len) {
281
+ if (found) {
282
+ if (menu && len === 1) {
283
+ // continue for hotkey matching
284
+ found = -1;
285
+ } else return 0;
286
+ } else found = i + 1;
287
+ }
288
+ }
289
+ return found;
290
+ }
291
+
292
+ /** `parse_choice`: marks `chosen`; the number of items chosen. */
293
+ private parseChoice(
294
+ titles: readonly string[],
295
+ singleton: boolean,
296
+ input: string,
297
+ chosen: number[],
298
+ menu: boolean,
299
+ ): number {
300
+ const tokens = (singleton ? [input] : input.split(/[, ]/)).map((t) => t.trim());
301
+ const nr = titles.length;
302
+ for (const token of tokens) {
303
+ let string = token;
304
+ if (!string) continue;
305
+ let choose = 1;
306
+ if (string.startsWith("-")) {
307
+ choose = 0;
308
+ string = string.slice(1);
309
+ }
310
+ let isRange = false;
311
+ let isNumber = true;
312
+ for (const ch of string) {
313
+ if (ch === "-") {
314
+ if (!isRange) {
315
+ isRange = true;
316
+ isNumber = false;
317
+ } else {
318
+ isNumber = false;
319
+ isRange = false;
320
+ break;
321
+ }
322
+ } else if (!isDigit(ch)) {
323
+ isNumber = false;
324
+ isRange = false;
325
+ break;
326
+ }
327
+ }
328
+ let bottom: number;
329
+ let top: number;
330
+ if (isNumber) {
331
+ bottom = atoi(string);
332
+ top = bottom;
333
+ } else if (isRange) {
334
+ bottom = atoi(string);
335
+ const rest = string.slice(string.indexOf("-") + 1);
336
+ top = rest ? atoi(rest) : nr;
337
+ } else if (string === "*") {
338
+ bottom = 1;
339
+ top = nr;
340
+ } else {
341
+ bottom = this.findUnique(string, titles, menu);
342
+ top = bottom;
343
+ }
344
+ if (top <= 0 || bottom <= 0 || top > nr || bottom > top || (singleton && bottom !== top)) {
345
+ this.out += `Huh (${string})?\n`;
346
+ continue;
347
+ }
348
+ for (let i = bottom; i <= top; i++) chosen[i - 1] = choose;
349
+ }
350
+ return chosen.reduce((a, b) => a + Math.max(0, b), 0);
351
+ }
352
+
353
+ /**
354
+ * `list_and_choose`: the indexes chosen, or undefined at the end of input.
355
+ */
356
+ private async listAndChoose(
357
+ titles: readonly string[],
358
+ opts: { header?: string; prompt: string; singleton: boolean },
359
+ label: (i: number, selected: boolean) => string,
360
+ ): Promise<number[] | undefined> {
361
+ const menu = opts.singleton;
362
+ const chosen = titles.map(() => 0);
363
+ let nr = 0;
364
+ for (;;) {
365
+ if (opts.header !== undefined) this.out += `${opts.header}\n`;
366
+ this.out += printColumns(
367
+ titles.map((_, i) => label(i, chosen[i] === 1)),
368
+ // pretty_print_menus: always in rows
369
+ { layout: "row", dense: false },
370
+ { indent: " ", padding: 2, width: this.width },
371
+ );
372
+ const prompt = `${opts.prompt}${opts.singleton ? "> " : ">> "}`;
373
+ this.out += prompt;
374
+ const input = await this.read({
375
+ type: "choose",
376
+ prompt,
377
+ singleton: opts.singleton,
378
+ items: [...titles],
379
+ output: this.out,
380
+ });
381
+ if (input === undefined) return undefined;
382
+ if (input === "?") {
383
+ this.promptHelp(opts.singleton);
384
+ continue;
385
+ }
386
+ if (!opts.singleton && !input.length) break;
387
+ nr = this.parseChoice(titles, opts.singleton, input, chosen, menu);
388
+ if (opts.singleton && nr) break;
389
+ }
390
+ const result: number[] = [];
391
+ for (let i = 0; i < chosen.length; i++) if (chosen[i]) result.push(i);
392
+ return result;
393
+ }
394
+
395
+ private promptHelp(singleton: boolean): void {
396
+ this.out += singleton
397
+ ? "Prompt help:\n" +
398
+ "1 - select a numbered item\n" +
399
+ "foo - select item based on unique prefix\n" +
400
+ " - (empty) select nothing\n"
401
+ : "Prompt help:\n" +
402
+ "1 - select a single item\n" +
403
+ "3-5 - select a range of items\n" +
404
+ "2-3,6-9 - select multiple ranges\n" +
405
+ "foo - select item based on unique prefix\n" +
406
+ "-... - unselect specified items\n" +
407
+ "* - choose all items\n" +
408
+ " - (empty) finish selecting\n";
409
+ }
410
+
411
+ /** `filter_by_patterns_cmd`. */
412
+ private async filterByPatterns(): Promise<void> {
413
+ let changed = -1;
414
+ for (;;) {
415
+ if (this.del.length === 0) break;
416
+ if (changed) this.prettyPrintDels();
417
+ const prompt = "Input ignore patterns>> ";
418
+ this.out += prompt;
419
+ let confirm = await this.read({ type: "line", prompt, output: this.out });
420
+ if (confirm === undefined) {
421
+ this.out += "\n";
422
+ confirm = "";
423
+ }
424
+ if (!confirm.length) break;
425
+ const pieces = confirm.split(" ").map((p) => p.trim());
426
+ const patterns = pieces.filter((p) => p.length > 0).map(parsePattern);
427
+ changed = 0;
428
+ const kept: string[] = [];
429
+ for (const item of this.del) {
430
+ if (this.excluded(item, patterns)) changed++;
431
+ else kept.push(item);
432
+ }
433
+ if (changed) this.del = kept;
434
+ else {
435
+ // 2.5x splits the input in place (string_list_split_in_place_f, trimming): what is
436
+ // left of it ends where its first piece, right-trimmed, ends
437
+ const shown = this.options.compatibility === "2.43" ? confirm : firstPieceOf(confirm);
438
+ this.out += `WARNING: Cannot find items matched by: ${shown}\n`;
439
+ }
440
+ }
441
+ }
442
+
443
+ /**
444
+ * `is_excluded` with the patterns alone: `prep_exclude` first asks each leading
445
+ * directory (an excluded one excludes everything below it), then the item itself
446
+ * (`last_matching_pattern_from_list`, its basename what follows its last `/`).
447
+ */
448
+ private excluded(item: string, patterns: Pattern[]): boolean {
449
+ const slash = item.lastIndexOf("/");
450
+ for (let at = item.indexOf("/"); at >= 0 && at <= slash; at = item.indexOf("/", at + 1)) {
451
+ if (at === 0) continue;
452
+ const dir = item.slice(0, at);
453
+ const match = this.lastMatching(dir, true, patterns);
454
+ if (match && !match.negative) return true;
455
+ }
456
+ const match = this.lastMatching(item, this.options.isDirectory(item), patterns);
457
+ return match !== undefined && !match.negative;
458
+ }
459
+
460
+ /** `last_matching_pattern_from_list`: the last pattern matching `path`. */
461
+ private lastMatching(path: string, directory: boolean, patterns: Pattern[]): Pattern | undefined {
462
+ const slash = path.lastIndexOf("/");
463
+ const basename = slash < 0 ? path : path.slice(slash + 1);
464
+ const casefold = this.options.ignoreCase === true;
465
+ for (let i = patterns.length - 1; i >= 0; i--) {
466
+ const p = patterns[i] as Pattern;
467
+ if (p.mustBeDir && !directory) continue;
468
+ const matched = p.noDir
469
+ ? wildmatch(p.pattern, basename, { casefold })
470
+ : wildmatch(p.pattern.startsWith("/") ? p.pattern.slice(1) : p.pattern, path, {
471
+ pathname: true,
472
+ casefold,
473
+ });
474
+ if (matched) return p;
475
+ }
476
+ return undefined;
477
+ }
478
+
479
+ /** `select_by_numbers_cmd`. */
480
+ private async selectByNumbers(): Promise<void> {
481
+ const chosen = await this.listAndChoose(
482
+ this.del,
483
+ { prompt: "Select items to delete", singleton: false },
484
+ (i, selected) => `${selected ? "*" : " "}${String(i + 1).padStart(2)}: ${this.del[i]}`,
485
+ );
486
+ const keep = new Set(chosen ?? []);
487
+ this.del = this.del.filter((_, i) => keep.has(i));
488
+ }
489
+
490
+ /** `ask_each_cmd`. */
491
+ private async askEach(): Promise<void> {
492
+ let eof = false;
493
+ let confirm = "";
494
+ const kept: string[] = [];
495
+ for (const item of this.del) {
496
+ if (!eof) {
497
+ const prompt = `Remove ${this.quote(item)} [y/N]? `;
498
+ this.out += prompt;
499
+ const reply = await this.read({ type: "yesno", prompt });
500
+ if (reply === undefined) {
501
+ this.out += "\n";
502
+ eof = true;
503
+ confirm = "";
504
+ } else confirm = reply;
505
+ }
506
+ if (confirm.length && lower("yes".slice(0, confirm.length)) === lower(confirm)) {
507
+ kept.push(item);
508
+ }
509
+ }
510
+ this.del = kept;
511
+ }
512
+ }
513
+
514
+ /**
515
+ * Runs `git clean -i` over the items to delete: the ones left to delete come back with
516
+ * the transcript.
517
+ */
518
+ export async function runCleanInteractive(
519
+ options: CleanInteractiveOptions,
520
+ ): Promise<{ items: string[]; output: string }> {
521
+ return new CleanInteractiveSession(options).run();
522
+ }