@statewalker/vcs-commands 0.14.0 → 0.15.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 (265) hide show
  1. package/dist/commands/add-command.d.ts.map +1 -1
  2. package/dist/commands/add-patch/patch-backend.d.ts +1 -1
  3. package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
  4. package/dist/commands/archive/remote-archive-command.d.ts +4 -1
  5. package/dist/commands/archive/remote-archive-command.d.ts.map +1 -1
  6. package/dist/commands/branch-command.d.ts +14 -2
  7. package/dist/commands/branch-command.d.ts.map +1 -1
  8. package/dist/commands/cat-file/batch-format.d.ts +8 -12
  9. package/dist/commands/cat-file/batch-format.d.ts.map +1 -1
  10. package/dist/commands/cat-file/cat-file-command.d.ts +39 -5
  11. package/dist/commands/cat-file/cat-file-command.d.ts.map +1 -1
  12. package/dist/commands/check-ignore-command.d.ts.map +1 -1
  13. package/dist/commands/checkout-command.d.ts +4 -0
  14. package/dist/commands/checkout-command.d.ts.map +1 -1
  15. package/dist/commands/checkout-paths.d.ts +9 -0
  16. package/dist/commands/checkout-paths.d.ts.map +1 -1
  17. package/dist/commands/cherry-pick-command.d.ts +3 -0
  18. package/dist/commands/cherry-pick-command.d.ts.map +1 -1
  19. package/dist/commands/clean-interactive.d.ts +0 -9
  20. package/dist/commands/clean-interactive.d.ts.map +1 -1
  21. package/dist/commands/clone-command.d.ts +2 -0
  22. package/dist/commands/clone-command.d.ts.map +1 -1
  23. package/dist/commands/column/column-config.d.ts +23 -0
  24. package/dist/commands/column/column-config.d.ts.map +1 -0
  25. package/dist/commands/column/column-layout.d.ts +129 -0
  26. package/dist/commands/column/column-layout.d.ts.map +1 -0
  27. package/dist/commands/column/index.d.ts +3 -0
  28. package/dist/commands/column/index.d.ts.map +1 -0
  29. package/dist/commands/column-command.d.ts +59 -0
  30. package/dist/commands/column-command.d.ts.map +1 -0
  31. package/dist/commands/comment-char-config.d.ts +15 -0
  32. package/dist/commands/comment-char-config.d.ts.map +1 -1
  33. package/dist/commands/commit-command.d.ts +21 -2
  34. package/dist/commands/commit-command.d.ts.map +1 -1
  35. package/dist/commands/config-command.d.ts +12 -0
  36. package/dist/commands/config-command.d.ts.map +1 -1
  37. package/dist/commands/credential-helper.d.ts +214 -0
  38. package/dist/commands/credential-helper.d.ts.map +1 -0
  39. package/dist/commands/default-branch.d.ts +17 -1
  40. package/dist/commands/default-branch.d.ts.map +1 -1
  41. package/dist/commands/diff-color-config.d.ts +4 -0
  42. package/dist/commands/diff-color-config.d.ts.map +1 -1
  43. package/dist/commands/diff-command.d.ts +12 -0
  44. package/dist/commands/diff-command.d.ts.map +1 -1
  45. package/dist/commands/fast-export/fast-export-command.d.ts +89 -2
  46. package/dist/commands/fast-export/fast-export-command.d.ts.map +1 -1
  47. package/dist/commands/fast-export/fast-import-command.d.ts +113 -8
  48. package/dist/commands/fast-export/fast-import-command.d.ts.map +1 -1
  49. package/dist/commands/fast-export/fast-stream-support.d.ts +0 -9
  50. package/dist/commands/fast-export/fast-stream-support.d.ts.map +1 -1
  51. package/dist/commands/fetch-command.d.ts +21 -0
  52. package/dist/commands/fetch-command.d.ts.map +1 -1
  53. package/dist/commands/git-hooks.d.ts +28 -0
  54. package/dist/commands/git-hooks.d.ts.map +1 -1
  55. package/dist/commands/history-command.d.ts +173 -7
  56. package/dist/commands/history-command.d.ts.map +1 -1
  57. package/dist/commands/index-host.d.ts +19 -0
  58. package/dist/commands/index-host.d.ts.map +1 -1
  59. package/dist/commands/index-refresh.d.ts +36 -0
  60. package/dist/commands/index-refresh.d.ts.map +1 -0
  61. package/dist/commands/index.d.ts +9 -0
  62. package/dist/commands/index.d.ts.map +1 -1
  63. package/dist/commands/index.js +7 -6
  64. package/dist/commands/init-command.d.ts +20 -0
  65. package/dist/commands/init-command.d.ts.map +1 -1
  66. package/dist/commands/local-transport.d.ts +29 -1
  67. package/dist/commands/local-transport.d.ts.map +1 -1
  68. package/dist/commands/log-command.d.ts +7 -0
  69. package/dist/commands/log-command.d.ts.map +1 -1
  70. package/dist/commands/log-output.d.ts +11 -0
  71. package/dist/commands/log-output.d.ts.map +1 -1
  72. package/dist/commands/maintenance-schedule-command.d.ts +122 -0
  73. package/dist/commands/maintenance-schedule-command.d.ts.map +1 -0
  74. package/dist/commands/merge-command.d.ts +2 -0
  75. package/dist/commands/merge-command.d.ts.map +1 -1
  76. package/dist/commands/merge-index-command.d.ts +42 -0
  77. package/dist/commands/merge-index-command.d.ts.map +1 -0
  78. package/dist/commands/merge-one-file.d.ts +129 -0
  79. package/dist/commands/merge-one-file.d.ts.map +1 -0
  80. package/dist/commands/merge-support.d.ts +21 -2
  81. package/dist/commands/merge-support.d.ts.map +1 -1
  82. package/dist/commands/object-disk-info.d.ts +35 -0
  83. package/dist/commands/object-disk-info.d.ts.map +1 -0
  84. package/dist/commands/octopus-merge.d.ts +3 -3
  85. package/dist/commands/octopus-merge.d.ts.map +1 -1
  86. package/dist/commands/promisor-remote.d.ts.map +1 -1
  87. package/dist/commands/pull-command.d.ts +55 -7
  88. package/dist/commands/pull-command.d.ts.map +1 -1
  89. package/dist/commands/push-command.d.ts +18 -1
  90. package/dist/commands/push-command.d.ts.map +1 -1
  91. package/dist/commands/range-diff/range-diff-command.d.ts +37 -1
  92. package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -1
  93. package/dist/commands/rebase-command.d.ts +13 -1
  94. package/dist/commands/rebase-command.d.ts.map +1 -1
  95. package/dist/commands/ref-filter/for-each-ref-command.d.ts +3 -2
  96. package/dist/commands/ref-filter/for-each-ref-command.d.ts.map +1 -1
  97. package/dist/commands/ref-filter/ref-filter.d.ts.map +1 -1
  98. package/dist/commands/ref-filter/ref-format.d.ts +26 -1
  99. package/dist/commands/ref-filter/ref-format.d.ts.map +1 -1
  100. package/dist/commands/ref-filter/ref-listing-command.d.ts +9 -0
  101. package/dist/commands/ref-filter/ref-listing-command.d.ts.map +1 -1
  102. package/dist/commands/remote-transport.d.ts +27 -0
  103. package/dist/commands/remote-transport.d.ts.map +1 -1
  104. package/dist/commands/repack-command.d.ts +18 -2
  105. package/dist/commands/repack-command.d.ts.map +1 -1
  106. package/dist/commands/repo-command.d.ts +2 -7
  107. package/dist/commands/repo-command.d.ts.map +1 -1
  108. package/dist/commands/reset-command.d.ts +4 -1
  109. package/dist/commands/reset-command.d.ts.map +1 -1
  110. package/dist/commands/restore-command.d.ts.map +1 -1
  111. package/dist/commands/rev-list-command.d.ts +65 -0
  112. package/dist/commands/rev-list-command.d.ts.map +1 -1
  113. package/dist/commands/revert-command.d.ts +3 -0
  114. package/dist/commands/revert-command.d.ts.map +1 -1
  115. package/dist/commands/revision-filenames.d.ts.map +1 -1
  116. package/dist/commands/revision-range.d.ts +17 -0
  117. package/dist/commands/revision-range.d.ts.map +1 -1
  118. package/dist/commands/revision-resolver.d.ts +12 -0
  119. package/dist/commands/revision-resolver.d.ts.map +1 -1
  120. package/dist/commands/serve-repository.d.ts +17 -1
  121. package/dist/commands/serve-repository.d.ts.map +1 -1
  122. package/dist/commands/served-lazy-fetch.d.ts +61 -0
  123. package/dist/commands/served-lazy-fetch.d.ts.map +1 -0
  124. package/dist/commands/signing.d.ts +10 -4
  125. package/dist/commands/signing.d.ts.map +1 -1
  126. package/dist/commands/status-color-config.d.ts +33 -0
  127. package/dist/commands/status-color-config.d.ts.map +1 -0
  128. package/dist/commands/status-command.d.ts +15 -0
  129. package/dist/commands/status-command.d.ts.map +1 -1
  130. package/dist/commands/status-format.d.ts +46 -4
  131. package/dist/commands/status-format.d.ts.map +1 -1
  132. package/dist/commands/tag-command.d.ts +15 -1
  133. package/dist/commands/tag-command.d.ts.map +1 -1
  134. package/dist/commands/tree-restore.d.ts +14 -0
  135. package/dist/commands/tree-restore.d.ts.map +1 -1
  136. package/dist/commands/verify-command.d.ts +22 -2
  137. package/dist/commands/verify-command.d.ts.map +1 -1
  138. package/dist/commands/worktree-add-command.d.ts.map +1 -1
  139. package/dist/commands/worktree-overwrite-checks.d.ts +31 -6
  140. package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
  141. package/dist/{commands-C9M78Chn.js → commands-DT3VEv50.js} +14879 -16703
  142. package/dist/errors/commit-errors.d.ts +23 -3
  143. package/dist/errors/commit-errors.d.ts.map +1 -1
  144. package/dist/errors/git-api-error.d.ts +6 -0
  145. package/dist/errors/git-api-error.d.ts.map +1 -1
  146. package/dist/errors/index.js +30 -4
  147. package/dist/{fsck-C4US3l0B.js → fsck-BZ508Elg.js} +1 -1
  148. package/dist/git-command.d.ts +33 -1
  149. package/dist/git-command.d.ts.map +1 -1
  150. package/dist/git.d.ts +104 -1
  151. package/dist/git.d.ts.map +1 -1
  152. package/dist/index.js +8 -7
  153. package/dist/{remote-transport-C43eykfK.js → local-transport-D98U6p4d.js} +184 -683
  154. package/dist/{maintenance-run-command-CoJFKr2p.js → maintenance-run-command-DZFnbnbo.js} +2285 -79
  155. package/dist/remote-config/remote-config-store.d.ts +6 -0
  156. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  157. package/dist/remote-transport-zvSqwpOB.js +4865 -0
  158. package/dist/results/cherry-pick-result.d.ts +4 -3
  159. package/dist/results/cherry-pick-result.d.ts.map +1 -1
  160. package/dist/results/diff-output.d.ts +7 -0
  161. package/dist/results/diff-output.d.ts.map +1 -1
  162. package/dist/results/diff-words.d.ts +14 -3
  163. package/dist/results/diff-words.d.ts.map +1 -1
  164. package/dist/results/index.js +1 -1
  165. package/dist/results/line-prefix-sink.d.ts +50 -0
  166. package/dist/results/line-prefix-sink.d.ts.map +1 -0
  167. package/dist/results/merge-result.d.ts +4 -3
  168. package/dist/results/merge-result.d.ts.map +1 -1
  169. package/dist/results/rebase-result.d.ts +5 -0
  170. package/dist/results/rebase-result.d.ts.map +1 -1
  171. package/dist/results/revert-result.d.ts +4 -3
  172. package/dist/results/revert-result.d.ts.map +1 -1
  173. package/dist/{results-B0CWLCAp.js → results-rF81cQse.js} +1425 -202
  174. package/dist/transport-command.d.ts +10 -0
  175. package/dist/transport-command.d.ts.map +1 -1
  176. package/package.json +12 -12
  177. package/src/commands/add-command.ts +15 -2
  178. package/src/commands/add-patch/patch-backend.ts +6 -44
  179. package/src/commands/archive/remote-archive-command.ts +10 -3
  180. package/src/commands/branch-command.ts +57 -4
  181. package/src/commands/cat-file/batch-format.ts +17 -18
  182. package/src/commands/cat-file/cat-file-command.ts +181 -15
  183. package/src/commands/check-ignore-command.ts +61 -17
  184. package/src/commands/checkout-command.ts +149 -21
  185. package/src/commands/checkout-index-command.ts +2 -1
  186. package/src/commands/checkout-paths.ts +13 -0
  187. package/src/commands/cherry-pick-command.ts +9 -0
  188. package/src/commands/clean-interactive.ts +20 -74
  189. package/src/commands/clone-command.ts +13 -0
  190. package/src/commands/column/column-config.ts +63 -0
  191. package/src/commands/column/column-layout.ts +396 -0
  192. package/src/commands/column/index.ts +2 -0
  193. package/src/commands/column-command.ts +171 -0
  194. package/src/commands/comment-char-config.ts +42 -0
  195. package/src/commands/commit-command.ts +61 -21
  196. package/src/commands/config-command.ts +28 -0
  197. package/src/commands/credential-helper.ts +807 -0
  198. package/src/commands/default-branch.ts +38 -0
  199. package/src/commands/diff-color-config.ts +23 -10
  200. package/src/commands/diff-command.ts +25 -2
  201. package/src/commands/fast-export/fast-export-command.ts +661 -35
  202. package/src/commands/fast-export/fast-import-command.ts +694 -94
  203. package/src/commands/fast-export/fast-stream-support.ts +18 -1
  204. package/src/commands/fetch-command.ts +103 -14
  205. package/src/commands/git-hooks.ts +85 -0
  206. package/src/commands/history-command.ts +952 -20
  207. package/src/commands/index-host.ts +51 -0
  208. package/src/commands/index-refresh.ts +121 -0
  209. package/src/commands/index.ts +9 -0
  210. package/src/commands/init-command.ts +34 -1
  211. package/src/commands/legacy-remotes.ts +4 -20
  212. package/src/commands/local-transport.ts +130 -9
  213. package/src/commands/log-command.ts +39 -1
  214. package/src/commands/log-output.ts +31 -4
  215. package/src/commands/maintenance-schedule-command.ts +418 -0
  216. package/src/commands/merge-command.ts +10 -0
  217. package/src/commands/merge-index-command.ts +202 -0
  218. package/src/commands/merge-one-file.ts +451 -0
  219. package/src/commands/merge-support.ts +127 -11
  220. package/src/commands/object-disk-info.ts +169 -0
  221. package/src/commands/octopus-merge.ts +37 -128
  222. package/src/commands/promisor-remote.ts +14 -3
  223. package/src/commands/pull-command.ts +173 -27
  224. package/src/commands/push-command.ts +50 -5
  225. package/src/commands/range-diff/range-diff-command.ts +86 -3
  226. package/src/commands/rebase-command.ts +46 -1
  227. package/src/commands/ref-filter/for-each-ref-command.ts +4 -3
  228. package/src/commands/ref-filter/ref-filter.ts +59 -4
  229. package/src/commands/ref-filter/ref-format.ts +83 -10
  230. package/src/commands/ref-filter/ref-listing-command.ts +30 -0
  231. package/src/commands/remote-command.ts +2 -2
  232. package/src/commands/remote-transport.ts +66 -3
  233. package/src/commands/repack-command.ts +75 -9
  234. package/src/commands/repo-command.ts +6 -70
  235. package/src/commands/reset-command.ts +11 -1
  236. package/src/commands/restore-command.ts +1 -0
  237. package/src/commands/rev-list-command.ts +205 -3
  238. package/src/commands/revert-command.ts +9 -0
  239. package/src/commands/revision-filenames.ts +32 -4
  240. package/src/commands/revision-range.ts +2 -2
  241. package/src/commands/revision-resolver.ts +21 -1
  242. package/src/commands/serve-repository.ts +34 -8
  243. package/src/commands/served-lazy-fetch.ts +164 -0
  244. package/src/commands/signing.ts +18 -10
  245. package/src/commands/status-color-config.ts +92 -0
  246. package/src/commands/status-command.ts +50 -2
  247. package/src/commands/status-format.ts +198 -50
  248. package/src/commands/tag-command.ts +57 -2
  249. package/src/commands/tree-restore.ts +17 -0
  250. package/src/commands/verify-command.ts +84 -12
  251. package/src/commands/worktree-add-command.ts +2 -0
  252. package/src/commands/worktree-overwrite-checks.ts +75 -14
  253. package/src/errors/commit-errors.ts +31 -3
  254. package/src/errors/git-api-error.ts +7 -0
  255. package/src/git-command.ts +105 -4
  256. package/src/git.ts +153 -0
  257. package/src/remote-config/remote-config-store.ts +16 -0
  258. package/src/results/cherry-pick-result.ts +4 -3
  259. package/src/results/diff-output.ts +8 -0
  260. package/src/results/diff-words.ts +160 -32
  261. package/src/results/line-prefix-sink.ts +102 -0
  262. package/src/results/merge-result.ts +4 -3
  263. package/src/results/rebase-result.ts +5 -0
  264. package/src/results/revert-result.ts +4 -3
  265. package/src/transport-command.ts +48 -2
@@ -0,0 +1,396 @@
1
+ /**
2
+ * column.c: git's column layouts (`--column[=<options>]`, `column.ui`, `column.<command>`,
3
+ * `git column`). The options are git's `colopts` bits: a layout (`column`, `row`, `plain`),
4
+ * whether the layout is used (`always`, `never`, `auto`), `dense`, and whether `--column`
5
+ * came from the command line.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * let colopts = parseColumnMode(0, "column,dense");
10
+ * colopts = finalizeColopts(colopts); // auto never lays out: no terminal
11
+ * printColumns(["a", "b", "c"], colopts, { width: 19 });
12
+ * // "a b c\n"
13
+ * ```
14
+ *
15
+ * @module
16
+ */
17
+
18
+ import { encodeGitPath, gitCharWidth } from "@statewalker/vcs-utils/encoding";
19
+
20
+ import { GitApiError } from "../../errors/index.js";
21
+
22
+ /** The layout bits (`COL_LAYOUT_MASK`) */
23
+ export const COL_LAYOUT_MASK = 0x000f;
24
+ /** always, never or auto (`COL_ENABLE_MASK`) */
25
+ export const COL_ENABLE_MASK = 0x0030;
26
+ /** `--column` was given on the command line (`COL_PARSEOPT`) */
27
+ export const COL_PARSEOPT = 0x0040;
28
+ /** Shrink columns when possible, making space for more columns (`COL_DENSE`) */
29
+ export const COL_DENSE = 0x0080;
30
+ /** `never` */
31
+ export const COL_DISABLED = 0x0000;
32
+ /** `always` */
33
+ export const COL_ENABLED = 0x0010;
34
+ /** `auto` */
35
+ export const COL_AUTO = 0x0020;
36
+ /** Fill columns before rows */
37
+ export const COL_COLUMN = 0;
38
+ /** Fill rows before columns */
39
+ export const COL_ROW = 1;
40
+ /** One item per line */
41
+ export const COL_PLAIN = 15;
42
+
43
+ /**
44
+ * A column option git refuses (`error: unsupported option '<word>'` from `--column=<word>`,
45
+ * exit 129) or a combination it dies on (`fatal: options '--column' and '--verbose' cannot be
46
+ * used together`, exit 128). `message` holds git's stderr lines.
47
+ */
48
+ export class ColumnOptionError extends GitApiError {
49
+ constructor(
50
+ message: string,
51
+ /** git's exit status */
52
+ readonly exitCode: number,
53
+ ) {
54
+ super(message);
55
+ this.name = "ColumnOptionError";
56
+ }
57
+ }
58
+
59
+ /** A word of a column mode git does not know (`parse_option`'s error). */
60
+ class UnsupportedColumnOption extends Error {}
61
+
62
+ const LAYOUT_SET = 1;
63
+ const ENABLE_SET = 2;
64
+
65
+ const OPTIONS: readonly { name: string; value: number; mask: number }[] = [
66
+ { name: "always", value: COL_ENABLED, mask: COL_ENABLE_MASK },
67
+ { name: "never", value: COL_DISABLED, mask: COL_ENABLE_MASK },
68
+ { name: "auto", value: COL_AUTO, mask: COL_ENABLE_MASK },
69
+ { name: "plain", value: COL_PLAIN, mask: COL_LAYOUT_MASK },
70
+ { name: "column", value: COL_COLUMN, mask: COL_LAYOUT_MASK },
71
+ { name: "row", value: COL_ROW, mask: COL_LAYOUT_MASK },
72
+ { name: "dense", value: COL_DENSE, mask: 0 },
73
+ ];
74
+
75
+ /** parse_config: the words of `value` (separated by spaces and commas) applied to `colopts`. */
76
+ function parseConfig(colopts: number, value: string): number {
77
+ let group = 0;
78
+ let out = colopts;
79
+ for (const word of value.split(/[ ,]+/)) {
80
+ if (word === "") continue;
81
+ let found = false;
82
+ for (const opt of OPTIONS) {
83
+ let name = word;
84
+ let set = true;
85
+ // only `dense` takes a `no` (case matters: git compares with strncmp)
86
+ if (!opt.mask && name.length > 2 && name.startsWith("no")) {
87
+ name = name.slice(2);
88
+ set = false;
89
+ }
90
+ if (name !== opt.name) continue;
91
+ if (opt.mask === COL_ENABLE_MASK) group |= ENABLE_SET;
92
+ else if (opt.mask === COL_LAYOUT_MASK) group |= LAYOUT_SET;
93
+ if (opt.mask) out = (out & ~opt.mask) | opt.value;
94
+ else out = set ? out | opt.value : out & ~opt.value;
95
+ found = true;
96
+ break;
97
+ }
98
+ if (!found) throw new UnsupportedColumnOption(word);
99
+ }
100
+ // a layout without always, never or auto implies always (the current one is disregarded)
101
+ if (group & LAYOUT_SET && !(group & ENABLE_SET)) {
102
+ out = (out & ~COL_ENABLE_MASK) | COL_ENABLED;
103
+ }
104
+ return out;
105
+ }
106
+
107
+ /**
108
+ * The words of a column mode (`column.ui`'s value, `--column=<options>`) applied to
109
+ * `colopts`, as column.c's `parse_config`.
110
+ *
111
+ * @throws ColumnOptionError for a word git does not know (`error: unsupported option 'x'`,
112
+ * exit 129 as parse-options gives it)
113
+ */
114
+ export function parseColumnMode(colopts: number, value: string): number {
115
+ try {
116
+ return parseConfig(colopts, value);
117
+ } catch (error) {
118
+ if (!(error instanceof UnsupportedColumnOption)) throw error;
119
+ throw new ColumnOptionError(`error: unsupported option '${error.message}'`, 129);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * parseopt_column_callback: `--column` (`true`), `--column=<options>` (a string) or
125
+ * `--no-column` (`false`) applied to `colopts`. `--column` alone is `always`; the layout
126
+ * read from the configuration stays.
127
+ *
128
+ * @throws ColumnOptionError for an option word git does not know (exit 129)
129
+ */
130
+ export function applyColumnOption(colopts: number, option: string | boolean): number {
131
+ let out = (colopts | COL_PARSEOPT) & ~COL_ENABLE_MASK;
132
+ if (option === false) return out;
133
+ out |= COL_ENABLED;
134
+ return typeof option === "string" ? parseColumnMode(out, option) : out;
135
+ }
136
+
137
+ /** A configuration variable as git's config callbacks see it. */
138
+ export interface ColumnConfigEntry {
139
+ /** The canonical (lower-case) key */
140
+ key: string;
141
+ /** Its text; undefined for a variable without `=` */
142
+ value: string | undefined;
143
+ /** Where it was read; none for the command line */
144
+ file?: string;
145
+ line?: number;
146
+ }
147
+
148
+ /**
149
+ * git_column_config over the configuration in its order: `column.ui` and
150
+ * `column.<command>` applied to `colopts` (0 to start).
151
+ *
152
+ * @returns The options, or git's death on a bad value (`error: unsupported option 'x'`,
153
+ * `error: invalid column.ui mode x`, then `fatal: bad config variable ...`; exit 128)
154
+ */
155
+ export function readColumnConfig(
156
+ entries: readonly ColumnConfigEntry[],
157
+ command?: string,
158
+ colopts = 0,
159
+ ): { colopts: number } | { key: string; message: string } {
160
+ let out = colopts;
161
+ for (const { key, value, file, line } of entries) {
162
+ if (!key.startsWith("column.")) continue;
163
+ const it = key.slice(7);
164
+ if (it !== "ui" && (command === undefined || it !== command)) continue;
165
+ const fatal =
166
+ file !== undefined && line !== undefined
167
+ ? `fatal: bad config variable '${key}' in file '${file}' at line ${line}`
168
+ : `fatal: unable to parse '${key}' from command-line config`;
169
+ if (value === undefined) {
170
+ return { key, message: `error: missing value for '${key}'\n${fatal}` };
171
+ }
172
+ try {
173
+ out = parseConfig(out, value);
174
+ } catch (error) {
175
+ if (!(error instanceof UnsupportedColumnOption)) throw error;
176
+ return {
177
+ key,
178
+ message: `error: unsupported option '${error.message}'\nerror: invalid column.${it} mode ${value}\n${fatal}`,
179
+ };
180
+ }
181
+ }
182
+ return { colopts: out };
183
+ }
184
+
185
+ /** finalize_colopts: `auto` lays out only on a terminal (or a pager). */
186
+ export function finalizeColopts(colopts: number, terminal = false): number {
187
+ if ((colopts & COL_ENABLE_MASK) !== COL_AUTO) return colopts;
188
+ return (colopts & ~COL_ENABLE_MASK) | (terminal ? COL_ENABLED : COL_DISABLED);
189
+ }
190
+
191
+ /** column_active: the layout is used. */
192
+ export function columnActive(colopts: number): boolean {
193
+ return (colopts & COL_ENABLE_MASK) === COL_ENABLED;
194
+ }
195
+
196
+ /** explicitly_enable_column: `--column` given on the command line and active. */
197
+ export function explicitlyEnableColumn(colopts: number): boolean {
198
+ return (colopts & COL_PARSEOPT) !== 0 && columnActive(colopts);
199
+ }
200
+
201
+ /**
202
+ * term_columns: `$COLUMNS` read as `atoi` when positive, else 80 (no terminal is asked).
203
+ *
204
+ * @param columns `$COLUMNS` as the environment gives it
205
+ */
206
+ export function termColumns(columns: string | undefined): number {
207
+ if (columns === undefined) return 80;
208
+ const n = Number.parseInt(columns.trimStart(), 10);
209
+ return Number.isFinite(n) && n > 0 && n <= 0x7fffffff ? n : 80;
210
+ }
211
+
212
+ /** The byte length of a string as git holds it (`strlen`). */
213
+ const byteLength = (s: string) => encodeGitPath(s).length;
214
+
215
+ /** display_mode_esc_sequence_len: `ESC [ <digits and ;> m`, else 0. */
216
+ function escapeLength(text: string, at: number): number {
217
+ if (text.charCodeAt(at) !== 0x1b || text[at + 1] !== "[") return 0;
218
+ let i = at + 2;
219
+ while (i < text.length && /[0-9;]/.test(text[i] as string)) i++;
220
+ return text[i] === "m" ? i + 1 - at : 0;
221
+ }
222
+
223
+ /**
224
+ * item_length: utf8_strnwidth with ANSI escapes skipped (wide characters 2, combining ones
225
+ * 0, control characters none); a string that is no UTF-8 (bytes kept as `decodeGitPath`'s
226
+ * U+DC80..U+DCFF) counts its bytes.
227
+ */
228
+ export function columnItemWidth(text: string): number {
229
+ let width = 0;
230
+ for (let i = 0; i < text.length; ) {
231
+ const esc = escapeLength(text, i);
232
+ if (esc) {
233
+ i += esc;
234
+ continue;
235
+ }
236
+ const cp = text.codePointAt(i) as number;
237
+ if (cp >= 0xd800 && cp <= 0xdfff) return byteLength(text);
238
+ const w = gitCharWidth(cp);
239
+ if (w > 0) width += w;
240
+ i += cp > 0xffff ? 2 : 1;
241
+ }
242
+ return width;
243
+ }
244
+
245
+ /**
246
+ * `(size_t)a / (size_t)b` stored in an `int`, as column.c computes its columns and rows
247
+ * (`width - strlen(indent)` and `DIV_ROUND_UP(list->nr, cols)` are unsigned: a negative
248
+ * width wraps around).
249
+ */
250
+ function udiv(a: number, b: number): number {
251
+ const d = BigInt.asUintN(64, BigInt(b));
252
+ if (d === 0n) return 0;
253
+ return Number(BigInt.asIntN(32, BigInt.asUintN(64, BigInt(a)) / d));
254
+ }
255
+
256
+ /** `(int)x`: C's wrap of int arithmetic. */
257
+ const int32 = (x: number) => Number(BigInt.asIntN(32, BigInt(x)));
258
+
259
+ /** The options of {@link printColumns} (column.h's `struct column_options`). */
260
+ export interface ColumnPrintOptions {
261
+ /** The width to fill; 0 or none: the terminal's columns less one */
262
+ width?: number;
263
+ /** Spaces between cells (default 1) */
264
+ padding?: number;
265
+ /** Before each line (default none) */
266
+ indent?: string;
267
+ /** After each line (default `\n`) */
268
+ nl?: string;
269
+ /** term_columns (default 80) */
270
+ termColumns?: number;
271
+ }
272
+
273
+ /**
274
+ * print_columns: `list` laid out as `colopts` says (inactive or `plain`: one item per line;
275
+ * `column`/`row`: a table filled down or across, `dense` shrinking the columns), as the
276
+ * text git prints. `colopts` must be finalized (no `auto`).
277
+ *
278
+ * @throws ColumnOptionError where git dies (a dense layout of a negative width wrapping to a
279
+ * negative column count: `fatal: size_t overflow`)
280
+ */
281
+ export function printColumns(
282
+ list: readonly string[],
283
+ colopts: number,
284
+ options: ColumnPrintOptions = {},
285
+ ): string {
286
+ if (list.length === 0) return "";
287
+ const indent = options.indent ?? "";
288
+ const nl = options.nl ?? "\n";
289
+ const padding = options.padding ?? 1;
290
+ const width = options.width ? options.width : (options.termColumns ?? 80) - 1;
291
+ if (!columnActive(colopts)) return list.map((s) => `${s}\n`).join("");
292
+ const layout = colopts & COL_LAYOUT_MASK;
293
+ if (layout === COL_PLAIN) return list.map((s) => `${indent}${s}${nl}`).join("");
294
+ if (layout !== COL_COLUMN && layout !== COL_ROW) {
295
+ throw new Error(`BUG: invalid layout mode ${layout}`);
296
+ }
297
+ return displayTable(list, layout === COL_COLUMN, (colopts & COL_DENSE) !== 0, {
298
+ indent,
299
+ nl,
300
+ padding,
301
+ width,
302
+ });
303
+ }
304
+
305
+ /** display_table: COL_COLUMN or COL_ROW. */
306
+ function displayTable(
307
+ list: readonly string[],
308
+ column: boolean,
309
+ dense: boolean,
310
+ opts: { indent: string; nl: string; padding: number; width: number },
311
+ ): string {
312
+ const nr = list.length;
313
+ const len = list.map(columnItemWidth);
314
+ const indentLength = byteLength(opts.indent);
315
+ const lenAt = (i: number) => len[i] as number;
316
+ let rows = 0;
317
+ let cols = 0;
318
+ const linear = (x: number, y: number) =>
319
+ int32(column ? Math.imul(x, rows) + y : Math.imul(y, cols) + x);
320
+ // `i < list->nr` compares unsigned: a negative index is past the end
321
+ const past = (i: number) => i < 0 || i >= nr;
322
+
323
+ // layout: equal cells as wide as the widest item plus the padding
324
+ let initialWidth = 0;
325
+ for (const l of len) if (initialWidth < l) initialWidth = l;
326
+ initialWidth += opts.padding;
327
+ cols = udiv(opts.width - indentLength, initialWidth);
328
+ if (cols === 0) cols = 1;
329
+ rows = udiv(nr + cols - 1, cols);
330
+
331
+ // compute_column_width: the index of each column's widest item
332
+ const computeColumnWidth = (): number[] => {
333
+ const w: number[] = [];
334
+ // columns past the items' count are never shown (git computes them anyway)
335
+ const shown = Math.min(cols, nr);
336
+ for (let x = 0; x < shown; x++) {
337
+ let widest = linear(x, 0);
338
+ for (let y = 0; y < rows; y++) {
339
+ const i = linear(x, y);
340
+ if (!past(i) && lenAt(widest) < lenAt(i)) widest = i;
341
+ }
342
+ w[x] = widest;
343
+ }
344
+ return w;
345
+ };
346
+
347
+ // shrink_columns: one row less (and more columns) while the whole still fits
348
+ let widths: number[] | undefined;
349
+ if (dense) {
350
+ // REALLOC_ARRAY(width, cols): a negative count is a huge size_t
351
+ if (cols < 0) {
352
+ throw new ColumnOptionError(
353
+ `fatal: size_t overflow: 4 * ${BigInt.asUintN(64, BigInt(cols))}`,
354
+ 128,
355
+ );
356
+ }
357
+ while (rows > 1) {
358
+ const oldRows = rows;
359
+ const oldCols = cols;
360
+ rows--;
361
+ cols = udiv(nr + rows - 1, rows);
362
+ const w = computeColumnWidth();
363
+ let total = indentLength;
364
+ for (let x = 0; x < cols; x++) total += lenAt(w[x] as number) + opts.padding;
365
+ if (total > opts.width) {
366
+ rows = oldRows;
367
+ cols = oldCols;
368
+ break;
369
+ }
370
+ }
371
+ widths = computeColumnWidth();
372
+ }
373
+
374
+ let out = "";
375
+ for (let y = 0; y < rows; y++) {
376
+ for (let x = 0; x < cols; x++) {
377
+ // display_cell
378
+ const i = linear(x, y);
379
+ if (past(i)) break;
380
+ let l = lenAt(i);
381
+ if (widths) {
382
+ const widest = lenAt(widths[x] as number);
383
+ if (widest < initialWidth) {
384
+ // the column is narrower than the cell: fill less
385
+ l += initialWidth - widest;
386
+ l -= opts.padding;
387
+ }
388
+ }
389
+ const newline = column ? past(int32(i + rows)) : x === cols - 1 || i === nr - 1;
390
+ out += x === 0 ? opts.indent : "";
391
+ out += list[i];
392
+ out += newline ? opts.nl : " ".repeat(Math.max(0, initialWidth - Math.max(0, l)));
393
+ }
394
+ }
395
+ return out;
396
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./column-config.js";
2
+ export * from "./column-layout.js";
@@ -0,0 +1,171 @@
1
+ /**
2
+ * `git column` (builtin/column.c): lines of input laid out in columns.
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * await git.column().setMode("column").setWidth(20).setInput("one\ntwo\nthree\n").call();
7
+ * // { exitCode: 0, stdout: "one two three\n", stderr: "" }
8
+ * ```
9
+ *
10
+ * @module
11
+ */
12
+
13
+ import { decodeGitPath } from "@statewalker/vcs-utils/encoding";
14
+
15
+ import { InvalidConfigError } from "../errors/index.js";
16
+ import { GitCommand } from "../git-command.js";
17
+ import type { PlumbingTextOutput } from "./check-ref-format-command.js";
18
+ import {
19
+ applyColumnOption,
20
+ ColumnOptionError,
21
+ columnConfigOf,
22
+ finalizeColopts,
23
+ printColumns,
24
+ terminalColumnsOf,
25
+ } from "./column/index.js";
26
+
27
+ /**
28
+ * `git column [--command=<name>] [--mode=<style> | --raw-mode=<n>] [--width=<width>]
29
+ * [--indent=<string>] [--nl=<string>] [--padding=<n>]` over {@link setInput} (git's stdin).
30
+ *
31
+ * The layout starts from `column.ui` (and `column.<name>` with {@link setCommand}), then
32
+ * {@link setMode} and {@link setRawMode} apply in the order they are called, as git parses
33
+ * its options. `auto` never lays out (no terminal). The width is {@link setWidth}, else
34
+ * {@link setColumns} or the host's `$COLUMNS` less one, else 79. A configured value git
35
+ * refuses dies as git (exit 128, its lines on stderr); so does `--mode=<bad>` (exit 129) and,
36
+ * from git 2.44, a negative padding.
37
+ */
38
+ export class ColumnCommand extends GitCommand<PlumbingTextOutput> {
39
+ private input = "";
40
+ private command?: string;
41
+ private modes: Array<{ mode: string | boolean } | { raw: number }> = [];
42
+ private width = 0;
43
+ private padding = 1;
44
+ private indent?: string;
45
+ private nl?: string;
46
+ private columns?: number;
47
+ private compatibility: "2.43" | "2.56" = "2.56";
48
+
49
+ /** The lines to lay out (git's stdin, read as `strbuf_getline`: a CR before the LF goes). */
50
+ setInput(input: string | Uint8Array): this {
51
+ this.checkCallable();
52
+ this.input = typeof input === "string" ? input : decodeGitPath(input);
53
+ return this;
54
+ }
55
+
56
+ /** `--command=<name>` (as the first argument): `column.<name>` is read after `column.ui`. */
57
+ setCommand(name: string): this {
58
+ this.checkCallable();
59
+ this.command = name;
60
+ return this;
61
+ }
62
+
63
+ /** `--mode=<style>` (a string), `--mode` (`true`: `always`) or `--no-mode` (`false`). */
64
+ setMode(mode: string | boolean = true): this {
65
+ this.checkCallable();
66
+ this.modes.push({ mode });
67
+ return this;
68
+ }
69
+
70
+ /** `--raw-mode=<n>`: the layout bits (`colopts`) themselves, replacing what came before. */
71
+ setRawMode(colopts: number): this {
72
+ this.checkCallable();
73
+ this.modes.push({ raw: colopts });
74
+ return this;
75
+ }
76
+
77
+ /** `--width=<n>`: the width to fill (0: the terminal's less one). */
78
+ setWidth(width: number): this {
79
+ this.checkCallable();
80
+ this.width = width;
81
+ return this;
82
+ }
83
+
84
+ /** `--padding=<n>`: spaces between cells (default 1). */
85
+ setPadding(padding: number): this {
86
+ this.checkCallable();
87
+ this.padding = padding;
88
+ return this;
89
+ }
90
+
91
+ /** `--indent=<string>`: before each line of a table or `plain` layout. */
92
+ setIndent(indent: string): this {
93
+ this.checkCallable();
94
+ this.indent = indent;
95
+ return this;
96
+ }
97
+
98
+ /** `--nl=<string>`: after each line of a table or `plain` layout (default a newline). */
99
+ setNl(nl: string): this {
100
+ this.checkCallable();
101
+ this.nl = nl;
102
+ return this;
103
+ }
104
+
105
+ /** The terminal's width (`$COLUMNS`; default the host environment's, else 80). */
106
+ setColumns(columns: number): this {
107
+ this.checkCallable();
108
+ this.columns = columns;
109
+ return this;
110
+ }
111
+
112
+ /** The git version whose behaviour to follow (default 2.56). */
113
+ setCompatibility(version: "2.43" | "2.56"): this {
114
+ this.checkCallable();
115
+ this.compatibility = version;
116
+ return this;
117
+ }
118
+
119
+ async call(): Promise<PlumbingTextOutput> {
120
+ this.checkCallable();
121
+ this.setCallable(false);
122
+ let colopts: number;
123
+ try {
124
+ colopts = columnConfigOf(this._workingCopy, this.command);
125
+ for (const step of this.modes) {
126
+ if ("raw" in step) {
127
+ if (step.raw < 0 && this.compatibility !== "2.43") {
128
+ throw new ColumnOptionError(
129
+ "error: option `raw-mode' expects a non-negative integer value with an optional k/m/g suffix",
130
+ 129,
131
+ );
132
+ }
133
+ colopts = step.raw >>> 0;
134
+ } else colopts = applyColumnOption(colopts, step.mode);
135
+ }
136
+ } catch (error) {
137
+ if (error instanceof ColumnOptionError) {
138
+ return { exitCode: error.exitCode, stdout: "", stderr: `${error.message}\n` };
139
+ }
140
+ if (error instanceof InvalidConfigError) {
141
+ return { exitCode: 128, stdout: "", stderr: `${error.message}\n` };
142
+ }
143
+ throw error;
144
+ }
145
+ if (this.padding < 0 && this.compatibility !== "2.43") {
146
+ return { exitCode: 128, stdout: "", stderr: "fatal: --padding must be non-negative\n" };
147
+ }
148
+ colopts = finalizeColopts(colopts);
149
+ try {
150
+ const stdout = printColumns(readLines(this.input), colopts, {
151
+ width: this.width,
152
+ padding: this.padding,
153
+ ...(this.indent !== undefined ? { indent: this.indent } : {}),
154
+ ...(this.nl !== undefined ? { nl: this.nl } : {}),
155
+ termColumns: terminalColumnsOf(this._workingCopy, this.columns),
156
+ });
157
+ return { exitCode: 0, stdout, stderr: "" };
158
+ } catch (error) {
159
+ if (!(error instanceof ColumnOptionError)) throw error;
160
+ return { exitCode: error.exitCode, stdout: "", stderr: `${error.message}\n` };
161
+ }
162
+ }
163
+ }
164
+
165
+ /** strbuf_getline over the whole input: lines without their LF (or CR LF); a last partial one kept. */
166
+ export function readLines(input: string): string[] {
167
+ if (input === "") return [];
168
+ const lines = input.split("\n");
169
+ if (lines[lines.length - 1] === "") lines.pop();
170
+ return lines.map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line));
171
+ }
@@ -105,6 +105,48 @@ export function checkAutoCommentChar(workingCopy: WorkingCopy): void {
105
105
  if (lines) throw new AutoCommentCharRemovedError(lines);
106
106
  }
107
107
 
108
+ /**
109
+ * The working copies inside a call of one of the five commands that warn (commit, merge,
110
+ * rebase, revert, cherry-pick), with how deep: git's `GIT_AUTO_COMMENT_CHAR_CONFIG_WARNING_GIVEN`,
111
+ * set once the warning was given so that the commands a command runs (`merge --continue`'s
112
+ * `git commit`, a rebase step's) do not repeat it.
113
+ */
114
+ const warningScopes = new WeakMap<WorkingCopy, number>();
115
+
116
+ /**
117
+ * Enters a call of a command that warns of a deprecated `auto` comment character (git
118
+ * 2.5x's `warn_on_auto_comment_char`): the warning's lines when this call is the outermost
119
+ * one on `workingCopy`, git 2.43 compatibility is off and no git 3.0 is asked for (it dies
120
+ * instead, {@link checkAutoCommentChar}); `leave` ends the call.
121
+ *
122
+ * ```typescript
123
+ * const { lines, leave } = enterAutoCommentCharWarning(workingCopy, "2.56");
124
+ * // lines: ["warning: Support for 'core.commentChar=auto' is deprecated ...", "hint:", ...]
125
+ * ```
126
+ */
127
+ export function enterAutoCommentCharWarning(
128
+ workingCopy: WorkingCopy,
129
+ compatibility: "2.43" | "2.56",
130
+ ): { lines: string[] | undefined; leave: () => void } {
131
+ const depth = warningScopes.get(workingCopy) ?? 0;
132
+ warningScopes.set(workingCopy, depth + 1);
133
+ const leave = () => {
134
+ const now = (warningScopes.get(workingCopy) ?? 1) - 1;
135
+ if (now <= 0) warningScopes.delete(workingCopy);
136
+ else warningScopes.set(workingCopy, now);
137
+ };
138
+ if (depth > 0 || compatibility === "2.43") return { lines: undefined, leave };
139
+ if (hostEnvironmentOf(workingCopy)?.breakingChanges) return { lines: undefined, leave };
140
+ let lines: string[] | undefined;
141
+ try {
142
+ lines = autoCommentCharLines(workingCopy, "deprecated");
143
+ } catch {
144
+ // a configuration that cannot be read fails the command itself, with git's lines
145
+ lines = undefined;
146
+ }
147
+ return { lines, leave };
148
+ }
149
+
108
150
  function headline(kind: "removed" | "deprecated", key: string): string {
109
151
  return kind === "removed"
110
152
  ? `fatal: Support for '${key}=auto' has been removed in Git 3.0`