@osdlc/typed-git 0.14.0 → 0.16.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/CHANGELOG.md +21 -0
- package/README.md +97 -15
- package/dist/async.d.ts +35 -7
- package/dist/async.js +44 -7
- package/dist/backend.d.ts +37 -11
- package/dist/backend.js +33 -18
- package/dist/client.d.ts +4 -4
- package/dist/client.js +3 -3
- package/dist/failure.d.ts +9 -0
- package/dist/failure.js +15 -0
- package/dist/outcome.d.ts +7 -6
- package/dist/sync.d.ts +54 -9
- package/dist/sync.js +68 -9
- package/dist/verbs.d.ts +139 -8
- package/dist/verbs.js +249 -20
- package/package.json +1 -1
package/dist/verbs.js
CHANGED
|
@@ -51,6 +51,30 @@ export function gate(verb, result, argv) {
|
|
|
51
51
|
throw gitFailureError(error);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Gate for a bytes runner's result: returns it, or throws a `FailureError`
|
|
56
|
+
* with the classified `GitFailure` on a non-zero exit, `neverExited` or
|
|
57
|
+
* `outputIncomplete`. The `CommandFailed` cause holds stdout decoded as UTF-8.
|
|
58
|
+
*/
|
|
59
|
+
export function gateBytes(result, argv) {
|
|
60
|
+
try {
|
|
61
|
+
if (runFailed(result))
|
|
62
|
+
throw new CommandFailed(argv, result);
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw gitFailureError(error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The bytes runner a client runs `rawBytes` on: the `bytesRunner` passed,
|
|
71
|
+
* else the default spawn, but only when no `runner` was passed either.
|
|
72
|
+
* `undefined` means the client throws `UnsupportedRequest` from its bytes
|
|
73
|
+
* calls. See the `Git` constructor.
|
|
74
|
+
*/
|
|
75
|
+
export function bytesRunnerFor(opts, spawnDefault) {
|
|
76
|
+
return opts.bytesRunner ?? (opts.runner === undefined ? spawnDefault() : undefined);
|
|
77
|
+
}
|
|
54
78
|
/**
|
|
55
79
|
* A client-level environment bag under a per-call one, or `undefined` when
|
|
56
80
|
* neither is set (so the runner inherits the parent environment untouched
|
|
@@ -371,14 +395,20 @@ export function statusPorcelain(opts = {}) {
|
|
|
371
395
|
}
|
|
372
396
|
/**
|
|
373
397
|
* Every changed or untracked file as a {@link StatusEntry}
|
|
374
|
-
* (`git status --porcelain -z --untracked-files=all`
|
|
375
|
-
* are expanded to their files. Paths are
|
|
376
|
-
* or copy yields one entry (its source
|
|
377
|
-
* tree; throws outside a repository.
|
|
378
|
-
|
|
379
|
-
|
|
398
|
+
* (`git status --porcelain -z --untracked-files=all`, plus `--ignored` with
|
|
399
|
+
* `ignored`). Untracked directories are expanded to their files. Paths are
|
|
400
|
+
* verbatim, never quoted, and a rename or copy yields one entry (its source
|
|
401
|
+
* path is dropped). Empty on a clean tree; throws outside a repository.
|
|
402
|
+
*
|
|
403
|
+
* Paths are decoded as UTF-8, so a name that is not valid UTF-8 comes back
|
|
404
|
+
* with U+FFFD in it. Read such names through `rawBytesChecked`.
|
|
405
|
+
*/
|
|
406
|
+
export function statusEntries(opts = {}) {
|
|
407
|
+
const argv = ['status', '--porcelain', '-z', '--untracked-files=all'];
|
|
408
|
+
if (opts.ignored === true)
|
|
409
|
+
argv.push('--ignored');
|
|
380
410
|
return {
|
|
381
|
-
argv
|
|
411
|
+
argv,
|
|
382
412
|
absent: NONE,
|
|
383
413
|
parse(r) {
|
|
384
414
|
const fields = nulFields(r);
|
|
@@ -963,9 +993,14 @@ export function lsTree(rev, opts = {}) {
|
|
|
963
993
|
};
|
|
964
994
|
}
|
|
965
995
|
/**
|
|
966
|
-
* The unified diff text (`git diff [--cached] [rev] [-- paths]`),
|
|
967
|
-
* rev and staged shape {@link diffNameOnly} selects paths for.
|
|
968
|
-
* nothing differs.
|
|
996
|
+
* The unified diff text (`git diff [--cached] [--binary] [rev] [-- paths]`),
|
|
997
|
+
* for the same rev and staged shape {@link diffNameOnly} selects paths for.
|
|
998
|
+
* Empty when nothing differs.
|
|
999
|
+
*
|
|
1000
|
+
* The text is decoded as UTF-8. A text file whose lines are not valid UTF-8
|
|
1001
|
+
* comes back with U+FFFD in them, and the patch no longer applies. For a
|
|
1002
|
+
* patch that must replay byte for byte, run the same argv through
|
|
1003
|
+
* `rawBytesChecked`.
|
|
969
1004
|
*
|
|
970
1005
|
* `absent: []`. A rev that does not resolve exits 128, and outside a
|
|
971
1006
|
* repository `git diff` exits 129. Both throw, so "git failed" never reads as
|
|
@@ -975,6 +1010,8 @@ export function diffText(rev, opts = {}) {
|
|
|
975
1010
|
const argv = ['diff'];
|
|
976
1011
|
if (opts.staged === true)
|
|
977
1012
|
argv.push('--cached');
|
|
1013
|
+
if (opts.binary === true)
|
|
1014
|
+
argv.push('--binary');
|
|
978
1015
|
const from = optionalPositional('diffText', 'rev', rev);
|
|
979
1016
|
if (from !== undefined)
|
|
980
1017
|
argv.push(from);
|
|
@@ -1167,16 +1204,166 @@ export function commitTree(tree, opts) {
|
|
|
1167
1204
|
argv.push('-p', positional('commitTree', 'parent', parent));
|
|
1168
1205
|
}
|
|
1169
1206
|
argv.push('-m', opts.message);
|
|
1170
|
-
return {
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1207
|
+
return { argv, absent: NONE, parse: shaReply };
|
|
1208
|
+
}
|
|
1209
|
+
/** A full object id in either hash format: 40 hex digits (SHA-1) or 64 (SHA-256). */
|
|
1210
|
+
const FULL_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
1211
|
+
/** The one sha a plumbing verb prints. Anything else throws rather than reading as an id. */
|
|
1212
|
+
function shaReply(r, ranArgv) {
|
|
1213
|
+
const sha = r.stdout.trim();
|
|
1214
|
+
if (!FULL_SHA.test(sha))
|
|
1215
|
+
throw new CommandFailed(ranArgv, r);
|
|
1216
|
+
return sha;
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* The blob sha of `content`, and with `{ write: true }` the blob itself
|
|
1220
|
+
* written to the object store (`git hash-object [-w] --stdin`).
|
|
1221
|
+
*
|
|
1222
|
+
* `content` goes to git on stdin as UTF-8, so the blob holds exactly those
|
|
1223
|
+
* bytes: input from stdin skips the clean filters and line-ending
|
|
1224
|
+
* conversion that `.gitattributes` would apply to a file. A string cannot
|
|
1225
|
+
* carry arbitrary bytes, so binary content is not supported.
|
|
1226
|
+
*
|
|
1227
|
+
* `absent: []`, and a reply that is not a sha throws.
|
|
1228
|
+
*/
|
|
1229
|
+
export function hashObject(content, opts = {}) {
|
|
1230
|
+
const argv = ['hash-object'];
|
|
1231
|
+
if (opts.write)
|
|
1232
|
+
argv.push('-w');
|
|
1233
|
+
argv.push('--stdin');
|
|
1234
|
+
return { argv, input: content, absent: NONE, parse: shaReply };
|
|
1235
|
+
}
|
|
1236
|
+
/** The object type each tree mode git writes stands for. */
|
|
1237
|
+
const TREE_MODE_TYPES = {
|
|
1238
|
+
'100644': 'blob',
|
|
1239
|
+
'100755': 'blob',
|
|
1240
|
+
'120000': 'blob',
|
|
1241
|
+
'040000': 'tree',
|
|
1242
|
+
'160000': 'commit',
|
|
1243
|
+
};
|
|
1244
|
+
/** Code points HFS+ ignores in a name (git's `next_hfs_char`). */
|
|
1245
|
+
const HFS_IGNORED = /[\u200C-\u200F\u202A-\u202E\u206A-\u206F\uFEFF]/gu;
|
|
1246
|
+
/** Lower-case the ASCII letters only, as git does against its ASCII needles. */
|
|
1247
|
+
const asciiLower = (s) => s.replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
1248
|
+
/**
|
|
1249
|
+
* HFS+ reads `name` as `.<needle>` (git's `is_hfs_dot_generic`): equal to it
|
|
1250
|
+
* in ASCII letter case once the ignorable code points are dropped.
|
|
1251
|
+
*/
|
|
1252
|
+
function hfsReadsAs(name, needle) {
|
|
1253
|
+
return asciiLower(name.replace(HFS_IGNORED, '')) === `.${needle}`;
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* NTFS drops trailing dots and spaces, and a `:` starts an alternate data
|
|
1257
|
+
* stream. True when `rest` holds only dots and spaces before its end or a `:`.
|
|
1258
|
+
*/
|
|
1259
|
+
function ntfsTailIsEmpty(rest) {
|
|
1260
|
+
return /^[. ]*(?:$|:)/.test(rest);
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* A name git's fsck reports as `hasDotgit` (git's `is_hfs_dotgit` and
|
|
1264
|
+
* `is_ntfs_dotgit`, which checks each part between backslashes), and checkout refuses on every platform.
|
|
1265
|
+
*/
|
|
1266
|
+
function readsAsDotGit(name) {
|
|
1267
|
+
if (hfsReadsAs(name, 'git'))
|
|
1268
|
+
return true;
|
|
1269
|
+
for (const part of name.split('\\')) {
|
|
1270
|
+
const lower = asciiLower(part);
|
|
1271
|
+
if (lower.startsWith('.git') && ntfsTailIsEmpty(part.slice(4)))
|
|
1272
|
+
return true;
|
|
1273
|
+
if (lower.startsWith('git~1') && ntfsTailIsEmpty(part.slice(5)))
|
|
1274
|
+
return true;
|
|
1275
|
+
if (part.includes(':'))
|
|
1276
|
+
return false;
|
|
1277
|
+
}
|
|
1278
|
+
return false;
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* A name git's fsck treats as `.gitmodules` (git's `is_hfs_dotgitmodules`
|
|
1282
|
+
* and `is_ntfs_dotgitmodules`): the HFS+ and NTFS spellings, the short names
|
|
1283
|
+
* `gitmod~1` to `~4`, and the 8.3 fallback short name, whose prefix
|
|
1284
|
+
* `gi7eba` git took from a hash of the long name.
|
|
1285
|
+
*/
|
|
1286
|
+
function readsAsDotGitmodules(name) {
|
|
1287
|
+
if (hfsReadsAs(name, 'gitmodules'))
|
|
1288
|
+
return true;
|
|
1289
|
+
const lower = asciiLower(name);
|
|
1290
|
+
if (lower.startsWith('.gitmodules'))
|
|
1291
|
+
return ntfsTailIsEmpty(name.slice(11));
|
|
1292
|
+
if (/^gitmod~[1-4]/.test(lower))
|
|
1293
|
+
return ntfsTailIsEmpty(name.slice(8));
|
|
1294
|
+
// The fallback: a leading part of `gi7eba` (at most six characters), `~`,
|
|
1295
|
+
// a digit 1-9, then digits up to eight characters. This follows git's loop
|
|
1296
|
+
// step for step, so a `~` at index 7 reads one character past eight.
|
|
1297
|
+
const prefix = 'gi7eba';
|
|
1298
|
+
let sawTilde = false;
|
|
1299
|
+
let i = 0;
|
|
1300
|
+
for (; i < 8; i++) {
|
|
1301
|
+
const c = lower[i];
|
|
1302
|
+
if (c === undefined)
|
|
1303
|
+
return false;
|
|
1304
|
+
if (sawTilde) {
|
|
1305
|
+
if (c < '0' || c > '9')
|
|
1306
|
+
return false;
|
|
1307
|
+
}
|
|
1308
|
+
else if (c === '~') {
|
|
1309
|
+
i++;
|
|
1310
|
+
const d = lower[i];
|
|
1311
|
+
if (d === undefined || d < '1' || d > '9')
|
|
1312
|
+
return false;
|
|
1313
|
+
sawTilde = true;
|
|
1314
|
+
}
|
|
1315
|
+
else if (i >= 6 || c !== prefix[i]) {
|
|
1316
|
+
return false;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
return ntfsTailIsEmpty(name.slice(i));
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Write a tree object holding `entries` and return its sha (`git mktree -z`).
|
|
1323
|
+
* No entries writes the empty tree.
|
|
1324
|
+
*
|
|
1325
|
+
* The entries go to git on stdin, NUL-terminated, so a name may hold spaces,
|
|
1326
|
+
* tabs or newlines; git sorts them into tree order itself.
|
|
1327
|
+
*
|
|
1328
|
+
* Throws a `TypeError` before git runs for a duplicate, empty, `.`, `..`,
|
|
1329
|
+
* `/`- or NUL-bearing name, a name git reads as `.git`, a `.gitmodules` that
|
|
1330
|
+
* is not a file, an unknown mode, a sha that is not a full sha, and a type
|
|
1331
|
+
* that does not match the mode. `git mktree` writes most of these, but
|
|
1332
|
+
* `git fsck --strict` reports them. D-03 lists the NTFS and HFS+ spellings.
|
|
1333
|
+
*
|
|
1334
|
+
* Every sha must name an object already in the store, of the entry's type,
|
|
1335
|
+
* except a submodule (`160000`) commit, which git does not check; otherwise
|
|
1336
|
+
* git exits 128 and this throws. `absent: []`, and a reply that is not a
|
|
1337
|
+
* sha throws.
|
|
1338
|
+
*/
|
|
1339
|
+
export function mktree(entries) {
|
|
1340
|
+
const names = new Set();
|
|
1341
|
+
const records = [];
|
|
1342
|
+
for (const { mode, type: given, sha, path } of entries) {
|
|
1343
|
+
const bad = (why) => new TypeError(`typed-git: mktree: entry ${JSON.stringify(path)} ${why}`);
|
|
1344
|
+
const type = TREE_MODE_TYPES[mode];
|
|
1345
|
+
if (type === undefined)
|
|
1346
|
+
throw bad(`has mode ${JSON.stringify(mode)}, which git does not write`);
|
|
1347
|
+
if (given !== undefined && given !== type) {
|
|
1348
|
+
throw bad(`has type ${JSON.stringify(given)}, but mode ${mode} is a ${type}`);
|
|
1349
|
+
}
|
|
1350
|
+
if (!FULL_SHA.test(sha))
|
|
1351
|
+
throw bad(`needs a full sha (got ${JSON.stringify(sha)})`);
|
|
1352
|
+
if (path === '' || path === '.' || path === '..' || readsAsDotGit(path)) {
|
|
1353
|
+
throw bad('is not a name git allows in a tree');
|
|
1354
|
+
}
|
|
1355
|
+
if (path.includes('/') || path.includes('\x00')) {
|
|
1356
|
+
throw bad('must be one name, without "/" or NUL');
|
|
1357
|
+
}
|
|
1358
|
+
if (mode !== '100644' && mode !== '100755' && readsAsDotGitmodules(path)) {
|
|
1359
|
+
throw bad(`is read by git as .gitmodules, which must be a file (got mode ${mode})`);
|
|
1360
|
+
}
|
|
1361
|
+
if (names.has(path))
|
|
1362
|
+
throw bad('appears twice');
|
|
1363
|
+
names.add(path);
|
|
1364
|
+
records.push(`${mode} ${type} ${sha}\t${path}\x00`);
|
|
1365
|
+
}
|
|
1366
|
+
return { argv: ['mktree', '-z'], input: records.join(''), absent: NONE, parse: shaReply };
|
|
1180
1367
|
}
|
|
1181
1368
|
/**
|
|
1182
1369
|
* How far `tip` has diverged from `base`
|
|
@@ -1696,6 +1883,48 @@ export function deleteRef(ref, opts) {
|
|
|
1696
1883
|
parse: nothing,
|
|
1697
1884
|
};
|
|
1698
1885
|
}
|
|
1886
|
+
/**
|
|
1887
|
+
* `git update-ref [-m <reason>] <ref> <sha> <oldSha>`: point `ref` at `sha`,
|
|
1888
|
+
* only while `ref` still holds `oldSha` (`null`: only while it does not
|
|
1889
|
+
* exist). It writes no object and leaves the index and working tree alone.
|
|
1890
|
+
*
|
|
1891
|
+
* `ref` must be `HEAD` or a full name under `refs/`. git does not expand a
|
|
1892
|
+
* short name here: `update-ref main` would write a new top-level ref named
|
|
1893
|
+
* `main` beside `refs/heads/main`, so a name that is neither throws a
|
|
1894
|
+
* `TypeError` before git runs. `HEAD` on a branch moves that branch.
|
|
1895
|
+
*
|
|
1896
|
+
* When `ref` is not where `oldSha` says, git exits 128 and this throws,
|
|
1897
|
+
* like any other failure: `reference already exists`, `is at <sha> but
|
|
1898
|
+
* expected <oldSha>`, or `unable to resolve reference`. Re-read the ref with
|
|
1899
|
+
* `resolveRef` to decide whether to retry. git also refuses a `sha` that
|
|
1900
|
+
* names no object, a non-commit on a branch, and a malformed ref name.
|
|
1901
|
+
*/
|
|
1902
|
+
export function updateRef(ref, sha, opts) {
|
|
1903
|
+
const name = positional('updateRef', 'ref', ref);
|
|
1904
|
+
if (name !== 'HEAD' && !name.startsWith('refs/')) {
|
|
1905
|
+
throw new TypeError(`typed-git: updateRef: ref must be HEAD or start with "refs/" (got ${JSON.stringify(ref)})`);
|
|
1906
|
+
}
|
|
1907
|
+
const argv = ['update-ref'];
|
|
1908
|
+
if (opts.reason !== undefined)
|
|
1909
|
+
argv.push('-m', opts.reason);
|
|
1910
|
+
argv.push(name, positional('updateRef', 'sha', sha));
|
|
1911
|
+
if (opts.oldSha === null) {
|
|
1912
|
+
// An empty old value is git's "must not exist", in either hash format.
|
|
1913
|
+
argv.push('');
|
|
1914
|
+
}
|
|
1915
|
+
else if (opts.oldSha === undefined) {
|
|
1916
|
+
// Only reachable without types: report the missing option, not an invalid sha.
|
|
1917
|
+
throw new TypeError('typed-git: updateRef: oldSha is required: the sha the ref holds now, or null for "must not exist"');
|
|
1918
|
+
}
|
|
1919
|
+
else {
|
|
1920
|
+
const old = positional('updateRef', 'oldSha', opts.oldSha);
|
|
1921
|
+
if (!FULL_SHA.test(old)) {
|
|
1922
|
+
throw new TypeError(`typed-git: updateRef: oldSha must be a full sha, or null for "must not exist" (got ${JSON.stringify(old)})`);
|
|
1923
|
+
}
|
|
1924
|
+
argv.push(old);
|
|
1925
|
+
}
|
|
1926
|
+
return { argv, absent: NONE, parse: nothing };
|
|
1927
|
+
}
|
|
1699
1928
|
/** `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`. */
|
|
1700
1929
|
export function worktreeAdd(path, opts = {}) {
|
|
1701
1930
|
if (opts.resetBranch && opts.branch === undefined) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@osdlc/typed-git",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.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",
|