@osdlc/typed-git 0.17.0 → 0.21.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.
package/dist/verbs.js CHANGED
@@ -233,7 +233,7 @@ function pushRevAndPaths(argv, rev, paths) {
233
233
  * The `git diff` flags that pin patch text against the user's git settings:
234
234
  * no colour codes, no external diff tool, no textconv conversion, the `a/`
235
235
  * and `b/` prefixes, paths that are not cut to the cwd, a submodule change as
236
- * a `Subproject commit` patch, and three lines of context.
236
+ * a `Subproject commit` patch. The context is {@link unifiedArg}'s.
237
237
  */
238
238
  const PLAIN_PATCH = [
239
239
  '--no-color',
@@ -243,8 +243,21 @@ const PLAIN_PATCH = [
243
243
  '--dst-prefix=b/',
244
244
  '--no-relative',
245
245
  '--submodule=short',
246
- '--unified=3',
247
246
  ];
247
+ /** The lines of context {@link diffText} and {@link diffAddedFile} print by default. */
248
+ const DEFAULT_CONTEXT = 3;
249
+ /**
250
+ * `--unified=<context>`, or the default when `context` is undefined. Throws
251
+ * `TypeError` when `context` is not a non-negative safe integer; `verb` names
252
+ * the caller in the message.
253
+ */
254
+ function unifiedArg(verb, context) {
255
+ const n = context ?? DEFAULT_CONTEXT;
256
+ if (!Number.isSafeInteger(n) || n < 0) {
257
+ throw new TypeError(`typed-git: ${verb}: context must be a non-negative integer (got ${JSON.stringify(n)})`);
258
+ }
259
+ return `--unified=${n}`;
260
+ }
248
261
  /** The child env that makes git print English, whatever the caller's locale. */
249
262
  const C_LOCALE = Object.freeze({ LC_ALL: 'C' });
250
263
  // ── queries ─────────────────────────────────────────────────────────────────
@@ -813,6 +826,8 @@ export function forEachRef(pattern, opts = {}) {
813
826
  argv.push(`--sort=${opts.sort}`);
814
827
  if (opts.count !== undefined)
815
828
  argv.push(`--count=${opts.count}`);
829
+ if (opts.contains !== undefined)
830
+ argv.push(`--contains=${opts.contains}`);
816
831
  for (const p of typeof pattern === 'string' ? [pattern] : pattern) {
817
832
  argv.push(positional('forEachRef', 'pattern', p));
818
833
  }
@@ -1028,24 +1043,24 @@ export function lsTree(rev, opts = {}) {
1028
1043
  /**
1029
1044
  * The unified diff text (`git diff --no-color --no-ext-diff --no-textconv
1030
1045
  * --src-prefix=a/ --dst-prefix=b/ --no-relative --submodule=short
1031
- * --unified=3 [--cached] [--binary] [rev] [--] [paths]`), for the same rev
1046
+ * --unified=<context> [--cached] [--binary] [-M] [rev] [--] [paths]`), for the same rev
1032
1047
  * and staged shape {@link diffNameStatus} lists paths for. `--` is passed
1033
1048
  * once, when there is a rev or a path. Empty when nothing differs.
1034
1049
  * {@link diffNameOnly} selects the same paths except under `diff.relative`,
1035
1050
  * which it still follows.
1036
1051
  *
1037
- * The text is a plain unified diff with `a/` and `b/` prefixes and three
1038
- * lines of context. The flags that are always passed turn off colour codes
1052
+ * The text is a plain unified diff with `a/` and `b/` prefixes. The flags that are always passed turn off colour codes
1039
1053
  * (`color.ui`, `color.diff`), an external diff tool (`diff.external`, a
1040
1054
  * `diff.<driver>.command`), textconv conversion, other prefixes
1041
1055
  * (`diff.noprefix`, `diff.mnemonicPrefix`, `diff.srcPrefix`,
1042
1056
  * `diff.dstPrefix`) and `diff.relative`, which with a cwd below the
1043
1057
  * repository root would drop changes outside it. They also keep a submodule
1044
1058
  * change as a `Subproject commit` patch, whatever `diff.submodule` says, and
1045
- * the context at three lines, whatever `diff.context` says. Other settings
1046
- * still apply: rename detection (`diff.renames`), `diff.ignoreSubmodules` and
1047
- * `diff.algorithm`; so does the `GIT_DIFF_OPTS` environment variable, where
1048
- * `-u0` overrides the three lines of context. The rev is followed by `--`, so git never reads it as a
1059
+ * the context at `context` lines (default three), whatever `diff.context`
1060
+ * says. Other settings still apply: rename detection (`diff.renames`, unless
1061
+ * `findRenames` pins it on), `diff.ignoreSubmodules` and `diff.algorithm`; so
1062
+ * does the `GIT_DIFF_OPTS` environment variable, where `-u0` overrides the
1063
+ * lines of context. The rev is followed by `--`, so git never reads it as a
1049
1064
  * path: a rev that names a file but no revision throws, and a branch that
1050
1065
  * shares its name with a file works.
1051
1066
  *
@@ -1059,18 +1074,20 @@ export function lsTree(rev, opts = {}) {
1059
1074
  * "no diff". `git.try.diffText` reports them as `{ ok: false }`.
1060
1075
  */
1061
1076
  export function diffText(rev, opts = {}) {
1062
- const argv = ['diff', ...PLAIN_PATCH];
1077
+ const argv = ['diff', ...PLAIN_PATCH, unifiedArg('diffText', opts.context)];
1063
1078
  if (opts.staged === true)
1064
1079
  argv.push('--cached');
1065
1080
  if (opts.binary === true)
1066
1081
  argv.push('--binary');
1082
+ if (opts.findRenames === true)
1083
+ argv.push('-M');
1067
1084
  pushRevAndPaths(argv, optionalPositional('diffText', 'rev', rev), opts.paths);
1068
1085
  return { argv, absent: NONE, parse: (r) => r.stdout };
1069
1086
  }
1070
1087
  /**
1071
1088
  * One untracked file rendered as an all-added diff (`diff --no-color
1072
1089
  * --no-ext-diff --no-textconv --src-prefix=a/ --dst-prefix=b/ --no-relative
1073
- * --submodule=short --unified=3 --no-index -- /dev/null <path>`), so a new
1090
+ * --submodule=short --unified=<context> --no-index -- /dev/null <path>`), so a new
1074
1091
  * file reaches a consumer in the same shape as a modified one: the flags are
1075
1092
  * the ones {@link diffText} passes, and the text is as plain. An empty
1076
1093
  * existing file still gives a diff header.
@@ -1080,9 +1097,17 @@ export function diffText(rev, opts = {}) {
1080
1097
  * for a path it cannot read; that throws the classified failure. Exit 2 (a usage or
1081
1098
  * read error) throws too.
1082
1099
  */
1083
- export function diffAddedFile(relPath) {
1100
+ export function diffAddedFile(relPath, opts = {}) {
1084
1101
  return {
1085
- argv: ['diff', ...PLAIN_PATCH, '--no-index', '--', '/dev/null', relPath],
1102
+ argv: [
1103
+ 'diff',
1104
+ ...PLAIN_PATCH,
1105
+ unifiedArg('diffAddedFile', opts.context),
1106
+ '--no-index',
1107
+ '--',
1108
+ '/dev/null',
1109
+ relPath,
1110
+ ],
1086
1111
  absent: [1],
1087
1112
  parse(r, ranArgv) {
1088
1113
  if (r.exitCode !== 0 && r.stdout === '')
@@ -1256,6 +1281,25 @@ export function commitTree(tree, opts) {
1256
1281
  argv.push('-m', opts.message);
1257
1282
  return { argv, absent: NONE, parse: shaReply };
1258
1283
  }
1284
+ /**
1285
+ * Load `rev`'s tree into the index (`git read-tree <rev>`), replacing it.
1286
+ *
1287
+ * It leaves the working tree alone. Run it with `env: { GIT_INDEX_FILE }` on
1288
+ * the call to seed a temporary index, so the repository's own index is not
1289
+ * touched. `absent: []`.
1290
+ */
1291
+ export function readTree(rev) {
1292
+ return { argv: ['read-tree', positional('readTree', 'rev', rev)], absent: NONE, parse: nothing };
1293
+ }
1294
+ /**
1295
+ * Write the index as a tree object and print its sha (`git write-tree`).
1296
+ *
1297
+ * It writes objects and no ref. A conflicted index makes git exit 128 and
1298
+ * this throws; a reply that is not a sha throws. `absent: []`.
1299
+ */
1300
+ export function writeTree() {
1301
+ return { argv: ['write-tree'], absent: NONE, parse: shaReply };
1302
+ }
1259
1303
  /** A full object id in either hash format: 40 hex digits (SHA-1) or 64 (SHA-256). */
1260
1304
  const FULL_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
1261
1305
  /** The one sha a plumbing verb prints. Anything else throws rather than reading as an id. */
@@ -1541,6 +1585,34 @@ export function fetch(remote, refspec, opts = {}) {
1541
1585
  }
1542
1586
  return { argv, absent: NONE, parse: nothing };
1543
1587
  }
1588
+ /**
1589
+ * `git fetch --multiple <remote>…`: every remote in one invocation, in
1590
+ * parallel with `jobs`. Git fetches each remote it can and exits non-zero
1591
+ * when any failed, naming it on stderr; the classified failure carries that
1592
+ * text. Throws a `TypeError` for an empty list.
1593
+ */
1594
+ export function fetchMany(remotes, opts = {}) {
1595
+ if (remotes.length === 0)
1596
+ throw new TypeError('typed-git: fetchMany: no remote given');
1597
+ const argv = ['fetch', '--multiple'];
1598
+ if (opts.quiet)
1599
+ argv.push('--quiet');
1600
+ if (opts.prune)
1601
+ argv.push('--prune');
1602
+ if (opts.noWriteFetchHead)
1603
+ argv.push('--no-write-fetch-head');
1604
+ if (opts.noTags)
1605
+ argv.push('--no-tags');
1606
+ if (opts.jobs !== undefined) {
1607
+ if (!Number.isSafeInteger(opts.jobs) || opts.jobs < 1) {
1608
+ throw new TypeError(`typed-git: fetchMany: jobs must be a positive integer (got ${JSON.stringify(opts.jobs)})`);
1609
+ }
1610
+ argv.push(`--jobs=${opts.jobs}`);
1611
+ }
1612
+ for (const remote of remotes)
1613
+ argv.push(positional('fetchMany', 'remote', remote));
1614
+ return { argv, absent: NONE, parse: nothing };
1615
+ }
1544
1616
  /** Stage paths. `--` precedes them, so a path starting with `-` is a path, not an option; flags go in `opts`. */
1545
1617
  export function add(paths, opts = {}) {
1546
1618
  const argv = ['add'];
@@ -1901,10 +1973,12 @@ export function resetHard(rev) {
1901
1973
  }
1902
1974
  /**
1903
1975
  * `git clean -f`: delete untracked files, and untracked directories with
1904
- * `{directories: true}`. Ignored files are kept.
1976
+ * `{directories: true}`; `{nested: true}` also deletes untracked nested repositories.
1977
+ * Ignored files are kept.
1905
1978
  */
1906
1979
  export function clean(opts = {}) {
1907
- return { argv: ['clean', opts.directories ? '-fd' : '-f'], absent: NONE, parse: nothing };
1980
+ const flags = `${opts.nested ? '-ff' : '-f'}${opts.directories ? 'd' : ''}`;
1981
+ return { argv: ['clean', flags], absent: NONE, parse: nothing };
1908
1982
  }
1909
1983
  /** Delete a local branch (`-d`, or `-D` with `{force: true}`). */
1910
1984
  export function deleteBranch(name, opts = {}) {
@@ -2022,6 +2096,33 @@ export function worktreeUnlock(path) {
2022
2096
  export function worktreePrune() {
2023
2097
  return { argv: ['worktree', 'prune'], absent: NONE, parse: nothing };
2024
2098
  }
2099
+ /**
2100
+ * `git submodule update --init --recursive --checkout`, with the `file` and `ext` transports
2101
+ * refused: check out every submodule, nested ones included, at the commit the checked-out tree pins, cloning those not yet initialised. A repository
2102
+ * with no submodules is exit 0 and no work.
2103
+ *
2104
+ * The URLs come from the tree's own `.gitmodules`, which a pull request's author controls, so
2105
+ * the `file` and `ext` transports are refused (a local path or a command as a "URL"). A
2106
+ * `GIT_ALLOW_PROTOCOL` in the environment still takes precedence over these. `--checkout` ignores a
2107
+ * `submodule.<name>.update` the tree's `.gitmodules` sets (`none`, `rebase`, `merge`, `!command`).
2108
+ */
2109
+ export function submoduleUpdate() {
2110
+ return {
2111
+ argv: [
2112
+ '-c',
2113
+ 'protocol.file.allow=never',
2114
+ '-c',
2115
+ 'protocol.ext.allow=never',
2116
+ 'submodule',
2117
+ 'update',
2118
+ '--init',
2119
+ '--recursive',
2120
+ '--checkout',
2121
+ ],
2122
+ absent: NONE,
2123
+ parse: nothing,
2124
+ };
2125
+ }
2025
2126
  /** `git remote add <name> <url>`. */
2026
2127
  export function remoteAdd(name, url) {
2027
2128
  return {
@@ -2181,8 +2282,11 @@ export function rebase(onto, opts = {}) {
2181
2282
  const argv = ['rebase'];
2182
2283
  if (opts.autostash)
2183
2284
  argv.push('--autostash');
2184
- if (opts.upstream === undefined)
2285
+ if (opts.upstream === undefined) {
2286
+ if (opts.forkPoint)
2287
+ argv.push('--fork-point');
2185
2288
  argv.push(positional('rebase', 'onto', onto));
2289
+ }
2186
2290
  else {
2187
2291
  argv.push('--onto', positional('rebase', 'onto', onto), positional('rebase', 'upstream', opts.upstream));
2188
2292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osdlc/typed-git",
3
- "version": "0.17.0",
3
+ "version": "0.21.0",
4
4
  "description": "A typed git client for TypeScript, sync and awaitable. Each verb declares what absence means for it; every other failure throws. git.try.* inspects, raw() bypasses — over an injectable backend that drives the git CLI or any other implementation.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": "sksizer",
@@ -52,7 +52,7 @@
52
52
  ],
53
53
  "dependencies": {
54
54
  "hosted-git-info": "^10.1.1",
55
- "@osdlc/command-seam": "^0.15.0",
55
+ "@osdlc/command-seam": "^0.15.1",
56
56
  "@osdlc/result": "^0.1.0"
57
57
  },
58
58
  "devDependencies": {