@williamthorsen/nmr-core 0.13.4 โ 0.13.6
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/.meta/changelog.json +56 -0
- package/CHANGELOG.md +26 -0
- package/dist/esm/collectChangedPaths.d.ts +18 -0
- package/dist/esm/collectChangedPaths.js +58 -0
- package/dist/esm/gitObjects.d.ts +24 -0
- package/dist/esm/gitObjects.js +40 -0
- package/dist/esm/hashWorkingTree.d.ts +14 -13
- package/dist/esm/hashWorkingTree.js +206 -97
- package/dist/esm/runGit.d.ts +18 -0
- package/dist/esm/runGit.js +22 -0
- package/package.json +7 -7
package/.meta/changelog.json
CHANGED
|
@@ -1,4 +1,38 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "0.13.6",
|
|
4
|
+
"date": "2026-10-24",
|
|
5
|
+
"sections": [
|
|
6
|
+
{
|
|
7
|
+
"title": "๐งช Tests",
|
|
8
|
+
"audience": "dev",
|
|
9
|
+
"items": [
|
|
10
|
+
{
|
|
11
|
+
"description": "Captures the output that passing tests leaked into the run, asserting on each error, hint, and `TAG_RESOLVE_FAILED` YAML warning emitted by the code under test, and pipes the stdio of the child processes run by tool tests so that their output does not reach the worker's streams. (#1097)",
|
|
12
|
+
"hash": "ed6e4187d7f30fa27eb59b495c5f51cf07eacdbc",
|
|
13
|
+
"entry": 2
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"version": "0.13.5",
|
|
21
|
+
"date": "2026-10-21",
|
|
22
|
+
"sections": [
|
|
23
|
+
{
|
|
24
|
+
"title": "๐ Bug fixes",
|
|
25
|
+
"audience": "all",
|
|
26
|
+
"items": [
|
|
27
|
+
{
|
|
28
|
+
"description": "Key the check cache on the working tree's git tree id (#1078)",
|
|
29
|
+
"body": "Keys `nmr`'s check-result cache on the git tree id that `git add --all` would record for the working tree, so that committing a tree that has already passed a check no longer makes the next run repeat it. Invalidates every pass recorded by earlier versions of `nmr`, and the first run after the upgrade repeats each of those checks. Also fixes the issue that the snapshot sent to the host by `nmr --delegate` could omit a file edited in the same second as git's last write of the index.",
|
|
30
|
+
"hash": "42cd3fb52a022010eccbf904a7d88358dd4e5759"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
},
|
|
2
36
|
{
|
|
3
37
|
"version": "0.13.4",
|
|
4
38
|
"date": "2026-10-17",
|
|
@@ -37,6 +71,17 @@
|
|
|
37
71
|
"version": "0.13.2",
|
|
38
72
|
"date": "2026-10-08",
|
|
39
73
|
"sections": [
|
|
74
|
+
{
|
|
75
|
+
"title": "๐ Features",
|
|
76
|
+
"audience": "all",
|
|
77
|
+
"items": [
|
|
78
|
+
{
|
|
79
|
+
"description": "Archive retired labels rather than deleting them in sync-labels (#955)",
|
|
80
|
+
"body": "Lets a repository retire a GitHub label without deleting it: `sync-labels generate` writes an `archived: true` mark from a `repoLabels.labels` entry to `.github/labels.yaml`, and the reusable sync workflow archives the marked label. An archived label stays on the issues and pull requests that have it, but it cannot be added to new ones.",
|
|
81
|
+
"hash": "23c0e0eeaf3e59cd921632850c7771ccdd6effe1"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
},
|
|
40
85
|
{
|
|
41
86
|
"title": "๐๏ธ Internal features",
|
|
42
87
|
"audience": "dev",
|
|
@@ -81,6 +126,17 @@
|
|
|
81
126
|
"version": "0.13.1",
|
|
82
127
|
"date": "2026-10-01",
|
|
83
128
|
"sections": [
|
|
129
|
+
{
|
|
130
|
+
"title": "๐ Features",
|
|
131
|
+
"audience": "all",
|
|
132
|
+
"items": [
|
|
133
|
+
{
|
|
134
|
+
"description": "Add update-templates to bring scaffolded workflows up to date (#952)",
|
|
135
|
+
"body": "Adds an `update-templates` command to `release-kit` and `v11y-check` that brings the GitHub Actions workflows scaffolded by `init` up to date with the installed version's templates and prints what changed in each file. Unlike `init --force`, the command never reads or writes a config file and keeps a private repo's omission of `provenance: true` from `publish.yaml`. The readyup kits now recommend it for a workflow that differs from its template.",
|
|
136
|
+
"hash": "9fac96675e9fb4dd70407cbfb0e3e0ffa7e83ba4"
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
},
|
|
84
140
|
{
|
|
85
141
|
"title": "โป๏ธ Refactoring",
|
|
86
142
|
"audience": "dev",
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## 0.13.6 โ 2026-10-24
|
|
6
|
+
|
|
7
|
+
### ๐งช Tests
|
|
8
|
+
|
|
9
|
+
- Captures the output that passing tests leaked into the run, asserting on each error, hint, and `TAG_RESOLVE_FAILED` YAML warning emitted by the code under test, and pipes the stdio of the child processes run by tool tests so that their output does not reach the worker's streams. (#1097)
|
|
10
|
+
|
|
11
|
+
## 0.13.5 โ 2026-10-21
|
|
12
|
+
|
|
13
|
+
### ๐ Bug fixes
|
|
14
|
+
|
|
15
|
+
- Key the check cache on the working tree's git tree id (#1078)
|
|
16
|
+
|
|
17
|
+
Keys `nmr`'s check-result cache on the git tree id that `git add --all` would record for the working tree, so that committing a tree that has already passed a check no longer makes the next run repeat it. Invalidates every pass recorded by earlier versions of `nmr`, and the first run after the upgrade repeats each of those checks. Also fixes the issue that the snapshot sent to the host by `nmr --delegate` could omit a file edited in the same second as git's last write of the index.
|
|
18
|
+
|
|
5
19
|
## 0.13.4 โ 2026-10-17
|
|
6
20
|
|
|
7
21
|
### ๐ฆ Dependencies
|
|
@@ -16,6 +30,12 @@ All notable changes to this project will be documented in this file.
|
|
|
16
30
|
|
|
17
31
|
## 0.13.2 โ 2026-10-08
|
|
18
32
|
|
|
33
|
+
### ๐ Features
|
|
34
|
+
|
|
35
|
+
- Archive retired labels rather than deleting them in sync-labels (#955)
|
|
36
|
+
|
|
37
|
+
Lets a repository retire a GitHub label without deleting it: `sync-labels generate` writes an `archived: true` mark from a `repoLabels.labels` entry to `.github/labels.yaml`, and the reusable sync workflow archives the marked label. An archived label stays on the issues and pull requests that have it, but it cannot be added to new ones.
|
|
38
|
+
|
|
19
39
|
### ๐๏ธ Internal features
|
|
20
40
|
|
|
21
41
|
- Adds an optional `stopAtDir` to `CacheDirRef`, which limits the upward search by `resolveCacheDir` for a `node_modules` to that directory, inclusive, and makes resolution throw when it is not the scope directory or one of its ancestors. (#969)
|
|
@@ -31,6 +51,12 @@ All notable changes to this project will be documented in this file.
|
|
|
31
51
|
|
|
32
52
|
## 0.13.1 โ 2026-10-01
|
|
33
53
|
|
|
54
|
+
### ๐ Features
|
|
55
|
+
|
|
56
|
+
- Add update-templates to bring scaffolded workflows up to date (#952)
|
|
57
|
+
|
|
58
|
+
Adds an `update-templates` command to `release-kit` and `v11y-check` that brings the GitHub Actions workflows scaffolded by `init` up to date with the installed version's templates and prints what changed in each file. Unlike `init --force`, the command never reads or writes a config file and keeps a private repo's omission of `provenance: true` from `publish.yaml`. The readyup kits now recommend it for a workflow that differs from its template.
|
|
59
|
+
|
|
34
60
|
### โป๏ธ Refactoring
|
|
35
61
|
|
|
36
62
|
- Replaces every `.sort()` call that suppressed `unicorn/no-array-sort` with `toSorted()` and drops the spread copy wherever the source was already an array. (#925)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The paths named by a `git status --porcelain=v2 -z` output, or the reason that they could not be read. */
|
|
2
|
+
export type ChangedPathsResult = {
|
|
3
|
+
ok: true;
|
|
4
|
+
paths: Set<string>;
|
|
5
|
+
} | {
|
|
6
|
+
ok: false;
|
|
7
|
+
reason: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Extracts every path named by a `--porcelain=v2 -z` status. An unrecognized record type fails closed: A record
|
|
11
|
+
* that this parser cannot read may name a path whose content would then go unhashed, which is the one failure that
|
|
12
|
+
* a content identity may never have.
|
|
13
|
+
*
|
|
14
|
+
* Record shapes, each a NUL-terminated field: An ordinary change (`1`) and an unmerged path (`u`) have their
|
|
15
|
+
* path last, after a fixed count of space-separated fields; a rename or copy (`2`) has its new path last and
|
|
16
|
+
* its original path in the field that follows; an untracked path (`?`) has its path alone.
|
|
17
|
+
*/
|
|
18
|
+
export declare function collectChangedPaths(statusOutput: string): ChangedPathsResult;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const PRECEDING_FIELD_COUNTS = {
|
|
2
|
+
1: 8,
|
|
3
|
+
2: 9,
|
|
4
|
+
'?': 1,
|
|
5
|
+
u: 10,
|
|
6
|
+
};
|
|
7
|
+
const SUBMODULE_BEARING_TYPES = new Set(['1', '2', 'u']);
|
|
8
|
+
export function collectChangedPaths(statusOutput) {
|
|
9
|
+
const records = statusOutput.split('\0');
|
|
10
|
+
const paths = new Set();
|
|
11
|
+
for (let index = 0; index < records.length; index++) {
|
|
12
|
+
const record = records[index];
|
|
13
|
+
if (record === undefined || record === '') {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const type = record.slice(0, 1);
|
|
17
|
+
if (type === '#') {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (SUBMODULE_BEARING_TYPES.has(type) && extractField(record, 2)?.startsWith('S') === true) {
|
|
21
|
+
return { ok: false, reason: 'the working tree contains a submodule, whose content this hash does not cover' };
|
|
22
|
+
}
|
|
23
|
+
const precedingFieldCount = PRECEDING_FIELD_COUNTS[type];
|
|
24
|
+
if (precedingFieldCount === undefined) {
|
|
25
|
+
return { ok: false, reason: `unrecognized git status record: ${JSON.stringify(record.slice(0, 40))}` };
|
|
26
|
+
}
|
|
27
|
+
const changedPath = extractPath(record, precedingFieldCount);
|
|
28
|
+
if (changedPath === undefined) {
|
|
29
|
+
return { ok: false, reason: `malformed git status record: ${JSON.stringify(record.slice(0, 40))}` };
|
|
30
|
+
}
|
|
31
|
+
paths.add(changedPath);
|
|
32
|
+
if (type !== '2') {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
index++;
|
|
36
|
+
const originalPath = records[index];
|
|
37
|
+
if (originalPath === undefined || originalPath === '') {
|
|
38
|
+
return { ok: false, reason: 'git status reported a rename without an original path' };
|
|
39
|
+
}
|
|
40
|
+
paths.add(originalPath);
|
|
41
|
+
}
|
|
42
|
+
return { ok: true, paths };
|
|
43
|
+
}
|
|
44
|
+
function extractField(record, fieldIndex) {
|
|
45
|
+
return record.split(' ')[fieldIndex];
|
|
46
|
+
}
|
|
47
|
+
function extractPath(record, precedingFieldCount) {
|
|
48
|
+
let index = 0;
|
|
49
|
+
for (let field = 0; field < precedingFieldCount; field++) {
|
|
50
|
+
const separator = record.indexOf(' ', index);
|
|
51
|
+
if (separator === -1) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
index = separator + 1;
|
|
55
|
+
}
|
|
56
|
+
const extracted = record.slice(index);
|
|
57
|
+
return extracted === '' ? undefined : extracted;
|
|
58
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** The hash functions that git uses for object ids, by the name that `git rev-parse --show-object-format` prints. */
|
|
2
|
+
export type GitObjectFormat = 'sha1' | 'sha256';
|
|
3
|
+
/**
|
|
4
|
+
* One entry of a tree object. `name` holds the entry's name as bytes, one character per byte (`latin1`), so that a
|
|
5
|
+
* name that is not valid UTF-8 survives a round trip and comparing two names compares their bytes.
|
|
6
|
+
*/
|
|
7
|
+
export interface GitTreeEntry {
|
|
8
|
+
id: Buffer;
|
|
9
|
+
mode: string;
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
/** The mode that a tree object records for a subtree. */
|
|
13
|
+
export declare const TREE_MODE = "40000";
|
|
14
|
+
/** Returns the raw id of a git object: the hash of `"<type> <byte length>\0"` followed by the content. */
|
|
15
|
+
export declare function hashGitObject(format: GitObjectFormat, type: 'blob' | 'tree', content: Buffer): Buffer;
|
|
16
|
+
/** Returns `value` as a `GitObjectFormat`, or `undefined` when git reported a format that this module cannot hash. */
|
|
17
|
+
export declare function parseObjectFormat(value: string): GitObjectFormat | undefined;
|
|
18
|
+
/** Splits a tree object's content into its entries, or returns `undefined` when the content is malformed. */
|
|
19
|
+
export declare function parseTreeObject(format: GitObjectFormat, content: Buffer): GitTreeEntry[] | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Serializes entries into a tree object's content in git's canonical order: by name bytes, with a subtree's name
|
|
22
|
+
* compared as though it ended in `/`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function serializeTreeObject(entries: Iterable<GitTreeEntry>): Buffer;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
export const TREE_MODE = '40000';
|
|
3
|
+
export function hashGitObject(format, type, content) {
|
|
4
|
+
return createHash(format).update(`${type} ${content.length}\0`).update(content).digest();
|
|
5
|
+
}
|
|
6
|
+
export function parseObjectFormat(value) {
|
|
7
|
+
return value === 'sha1' || value === 'sha256' ? value : undefined;
|
|
8
|
+
}
|
|
9
|
+
export function parseTreeObject(format, content) {
|
|
10
|
+
const idLength = format === 'sha1' ? 20 : 32;
|
|
11
|
+
const entries = [];
|
|
12
|
+
let offset = 0;
|
|
13
|
+
while (offset < content.length) {
|
|
14
|
+
const space = content.indexOf(0x20, offset);
|
|
15
|
+
const nul = content.indexOf(0, space + 1);
|
|
16
|
+
if (space === -1 || nul === -1 || nul + 1 + idLength > content.length) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
entries.push({
|
|
20
|
+
id: content.subarray(nul + 1, nul + 1 + idLength),
|
|
21
|
+
mode: content.toString('latin1', offset, space),
|
|
22
|
+
name: content.toString('latin1', space + 1, nul),
|
|
23
|
+
});
|
|
24
|
+
offset = nul + 1 + idLength;
|
|
25
|
+
}
|
|
26
|
+
return entries;
|
|
27
|
+
}
|
|
28
|
+
export function serializeTreeObject(entries) {
|
|
29
|
+
const sorted = [...entries].toSorted((left, right) => compareStrings(sortKeyOf(left), sortKeyOf(right)));
|
|
30
|
+
return Buffer.concat(sorted.flatMap((entry) => [Buffer.from(`${entry.mode} ${entry.name}\0`, 'latin1'), entry.id]));
|
|
31
|
+
}
|
|
32
|
+
function compareStrings(left, right) {
|
|
33
|
+
if (left === right) {
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
return left < right ? -1 : 1;
|
|
37
|
+
}
|
|
38
|
+
function sortKeyOf(entry) {
|
|
39
|
+
return entry.mode === TREE_MODE ? `${entry.name}/` : entry.name;
|
|
40
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* no commit, a git failure, a tree that this
|
|
4
|
-
* which a caller might act.
|
|
2
|
+
* The git tree id of a working tree, or the reason one could not be produced. For every degraded condition (no
|
|
3
|
+
* repository, no commit, a git failure, a tree that this id cannot describe), the result is `ok: false` rather than
|
|
4
|
+
* an id on which a caller might act.
|
|
5
5
|
*/
|
|
6
6
|
export type WorkingTreeHashResult = {
|
|
7
7
|
ok: true;
|
|
8
|
-
|
|
8
|
+
treeId: string;
|
|
9
9
|
headSha: string;
|
|
10
10
|
toplevel: string;
|
|
11
11
|
} | {
|
|
@@ -13,17 +13,18 @@ export type WorkingTreeHashResult = {
|
|
|
13
13
|
reason: string;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Identifies the entire working tree at `cwd` by its git tree id: the id that `git add --all` into a fresh copy of
|
|
17
|
+
* the index followed by `git write-tree` would record. Two trees with the same content share an id, so a `touch`
|
|
18
|
+
* that does not change any bytes leaves it unchanged, while any edit, addition, deletion, or mode change moves it. A
|
|
19
|
+
* clean tree's id is `HEAD^{tree}`, and a commit that records the tree as it is leaves the id unchanged.
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* The id is computed in memory rather than with `write-tree`: A hash is an observation, and a repository must look
|
|
22
|
+
* exactly the same after taking one, so nothing here writes to the object database or the index. Reading git's own
|
|
23
|
+
* status keeps the running time proportional to what has changed rather than to the size of the repository: Only
|
|
24
|
+
* the trees on the path from a changed entry to the root are read from `HEAD` and hashed again.
|
|
23
25
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* that preserves content leaves the hash alone, as does checking out a branch whose tree is identical.
|
|
26
|
+
* Content passes through git's clean filters as `git add` applies them. Configuration that makes `git add` record
|
|
27
|
+
* something other than what is on disk, such as `core.fileMode=false` or `core.symlinks=false`, is not modeled.
|
|
27
28
|
*/
|
|
28
29
|
export declare function hashWorkingTree(cwd: string): WorkingTreeHashResult;
|
|
29
30
|
/**
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createHash } from 'node:crypto';
|
|
3
|
-
import { existsSync, lstatSync, readFileSync, readlinkSync } from 'node:fs';
|
|
1
|
+
import { existsSync, lstatSync, readlinkSync } from 'node:fs';
|
|
4
2
|
import path from 'node:path';
|
|
5
3
|
import { describeError } from '@williamthorsen/toolbelt.errors';
|
|
6
|
-
import {
|
|
4
|
+
import { collectChangedPaths } from "./collectChangedPaths.js";
|
|
5
|
+
import { hashGitObject, parseObjectFormat, parseTreeObject, serializeTreeObject, TREE_MODE, } from "./gitObjects.js";
|
|
7
6
|
import { hasErrnoCode } from "./hasErrnoCode.js";
|
|
8
|
-
|
|
9
|
-
const PRECEDING_FIELD_COUNTS = {
|
|
10
|
-
1: 8,
|
|
11
|
-
2: 9,
|
|
12
|
-
'?': 1,
|
|
13
|
-
u: 10,
|
|
14
|
-
};
|
|
15
|
-
const SUBMODULE_BEARING_TYPES = new Set(['1', '2', 'u']);
|
|
7
|
+
import { runGit, runGitForBytes } from "./runGit.js";
|
|
16
8
|
export function hashWorkingTree(cwd) {
|
|
17
9
|
const toplevelResult = runGit(['rev-parse', '--show-toplevel'], cwd);
|
|
18
10
|
if (!toplevelResult.ok) {
|
|
@@ -25,13 +17,17 @@ export function hashWorkingTree(cwd) {
|
|
|
25
17
|
if (existsSync(path.join(toplevel, '.gitmodules'))) {
|
|
26
18
|
return { ok: false, reason: 'the repository declares submodules, whose content this hash does not cover' };
|
|
27
19
|
}
|
|
28
|
-
const revisionResult = runGit(['rev-parse', 'HEAD', 'HEAD^{tree}'], cwd);
|
|
20
|
+
const revisionResult = runGit(['rev-parse', '--show-object-format', 'HEAD', 'HEAD^{tree}'], cwd);
|
|
29
21
|
if (!revisionResult.ok) {
|
|
30
22
|
return { ok: false, reason: `no commit at HEAD: ${revisionResult.error}` };
|
|
31
23
|
}
|
|
32
|
-
const [headSha,
|
|
33
|
-
if (headSha === undefined ||
|
|
34
|
-
return { ok: false, reason: 'git did not report a HEAD commit and tree' };
|
|
24
|
+
const [formatName, headSha, headTreeId] = revisionResult.stdout.trim().split('\n', 3);
|
|
25
|
+
if (formatName === undefined || headSha === undefined || headTreeId === undefined) {
|
|
26
|
+
return { ok: false, reason: 'git did not report an object format, a HEAD commit, and a tree' };
|
|
27
|
+
}
|
|
28
|
+
const format = parseObjectFormat(formatName);
|
|
29
|
+
if (format === undefined) {
|
|
30
|
+
return { ok: false, reason: `unsupported object format: ${formatName}` };
|
|
35
31
|
}
|
|
36
32
|
const statusResult = runGit(['status', '--porcelain=v2', '-z', '--untracked-files=all'], cwd);
|
|
37
33
|
if (!statusResult.ok) {
|
|
@@ -41,24 +37,18 @@ export function hashWorkingTree(cwd) {
|
|
|
41
37
|
if (!pathsResult.ok) {
|
|
42
38
|
return pathsResult;
|
|
43
39
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
hash.update(content.kind);
|
|
57
|
-
hash.update('\0');
|
|
58
|
-
hash.update(content.digest);
|
|
59
|
-
hash.update('\0');
|
|
60
|
-
}
|
|
61
|
-
return { ok: true, hash: hash.digest('hex'), headSha, toplevel };
|
|
40
|
+
if (pathsResult.paths.size === 0) {
|
|
41
|
+
return { ok: true, treeId: headTreeId, headSha, toplevel };
|
|
42
|
+
}
|
|
43
|
+
const changesResult = readChangedEntries(toplevel, pathsResult.paths, format);
|
|
44
|
+
if (!changesResult.ok) {
|
|
45
|
+
return changesResult;
|
|
46
|
+
}
|
|
47
|
+
const treeResult = buildTreeId({ changes: changesResult.changes, format, headSha, headTreeId, toplevel });
|
|
48
|
+
if (!treeResult.ok) {
|
|
49
|
+
return treeResult;
|
|
50
|
+
}
|
|
51
|
+
return { ok: true, treeId: treeResult.treeId, headSha, toplevel };
|
|
62
52
|
}
|
|
63
53
|
export function readHeadSha(cwd) {
|
|
64
54
|
const result = runGit(['rev-parse', 'HEAD'], cwd);
|
|
@@ -68,93 +58,212 @@ export function readHeadSha(cwd) {
|
|
|
68
58
|
const headSha = result.stdout.trim();
|
|
69
59
|
return headSha === '' ? undefined : headSha;
|
|
70
60
|
}
|
|
71
|
-
function
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
for (
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
function buildTreeId(options) {
|
|
62
|
+
const { changes, format, headSha, headTreeId, toplevel } = options;
|
|
63
|
+
const directories = new Set();
|
|
64
|
+
for (const changedPath of changes.keys()) {
|
|
65
|
+
for (let directory = splitPath(changedPath).parent;; directory = splitPath(directory).parent) {
|
|
66
|
+
directories.add(directory);
|
|
67
|
+
if (directory === '') {
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const treesResult = readHeadTrees({ directories, format, headSha, headTreeId, toplevel });
|
|
73
|
+
if (!treesResult.ok) {
|
|
74
|
+
return treesResult;
|
|
75
|
+
}
|
|
76
|
+
const { trees } = treesResult;
|
|
77
|
+
for (const [changedPath, entry] of changes) {
|
|
78
|
+
const { name, parent } = splitPath(changedPath);
|
|
79
|
+
const tree = entriesAt(trees, parent);
|
|
80
|
+
if (entry === undefined) {
|
|
81
|
+
tree.delete(name);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
tree.set(name, { ...entry, name });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const deepestFirst = [...directories].toSorted((left, right) => depthOf(right) - depthOf(left));
|
|
88
|
+
for (const directory of deepestFirst) {
|
|
89
|
+
if (directory === '') {
|
|
77
90
|
continue;
|
|
78
91
|
}
|
|
79
|
-
const
|
|
80
|
-
|
|
92
|
+
const { name, parent } = splitPath(directory);
|
|
93
|
+
const parentTree = entriesAt(trees, parent);
|
|
94
|
+
const tree = entriesAt(trees, directory);
|
|
95
|
+
if (tree.size === 0) {
|
|
96
|
+
if (parentTree.get(name)?.mode === TREE_MODE) {
|
|
97
|
+
parentTree.delete(name);
|
|
98
|
+
}
|
|
81
99
|
continue;
|
|
82
100
|
}
|
|
83
|
-
|
|
84
|
-
|
|
101
|
+
parentTree.set(name, {
|
|
102
|
+
id: hashGitObject(format, 'tree', serializeTreeObject(tree.values())),
|
|
103
|
+
mode: TREE_MODE,
|
|
104
|
+
name,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const rootId = hashGitObject(format, 'tree', serializeTreeObject(entriesAt(trees, '').values()));
|
|
108
|
+
return { ok: true, treeId: rootId.toString('hex') };
|
|
109
|
+
}
|
|
110
|
+
function depthOf(directory) {
|
|
111
|
+
return directory === '' ? 0 : directory.split('/').length;
|
|
112
|
+
}
|
|
113
|
+
function entriesAt(trees, directory) {
|
|
114
|
+
let entries = trees.get(directory);
|
|
115
|
+
if (entries === undefined) {
|
|
116
|
+
entries = new Map();
|
|
117
|
+
trees.set(directory, entries);
|
|
118
|
+
}
|
|
119
|
+
return entries;
|
|
120
|
+
}
|
|
121
|
+
function hasSymlinkLeadingPath(toplevel, relativePath) {
|
|
122
|
+
const segments = relativePath.split('/').slice(0, -1);
|
|
123
|
+
return segments.some((_segment, index) => {
|
|
124
|
+
try {
|
|
125
|
+
return lstatSync(path.join(toplevel, ...segments.slice(0, index + 1))).isSymbolicLink();
|
|
85
126
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return { ok: false, reason: `unrecognized git status record: ${JSON.stringify(record.slice(0, 40))}` };
|
|
127
|
+
catch {
|
|
128
|
+
return false;
|
|
89
129
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function quotePath(relativePath) {
|
|
133
|
+
let quoted = '"';
|
|
134
|
+
for (const character of relativePath) {
|
|
135
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
136
|
+
if (character === '"' || character === '\\') {
|
|
137
|
+
quoted += `\\${character}`;
|
|
93
138
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
continue;
|
|
139
|
+
else if (codePoint < 0x20 || codePoint === 0x7f) {
|
|
140
|
+
quoted += `\\${codePoint.toString(8).padStart(3, '0')}`;
|
|
97
141
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (originalPath === undefined || originalPath === '') {
|
|
101
|
-
return { ok: false, reason: 'git status reported a rename without an original path' };
|
|
142
|
+
else {
|
|
143
|
+
quoted += character;
|
|
102
144
|
}
|
|
103
|
-
paths.add(originalPath);
|
|
104
145
|
}
|
|
105
|
-
return {
|
|
146
|
+
return `${quoted}"`;
|
|
106
147
|
}
|
|
107
|
-
function
|
|
108
|
-
|
|
148
|
+
function readChangedEntries(toplevel, paths, format) {
|
|
149
|
+
const changes = new Map();
|
|
150
|
+
const regularFiles = [];
|
|
151
|
+
for (const relativePath of paths) {
|
|
152
|
+
if (relativePath.includes('\n') || relativePath.includes('\u{FFFD}')) {
|
|
153
|
+
return {
|
|
154
|
+
ok: false,
|
|
155
|
+
reason: `${JSON.stringify(relativePath)} contains a newline or bytes that are not UTF-8, so it is not hashable`,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const key = Buffer.from(relativePath, 'utf8').toString('latin1');
|
|
159
|
+
const state = readPathState(toplevel, relativePath, format);
|
|
160
|
+
if (!state.ok) {
|
|
161
|
+
return state;
|
|
162
|
+
}
|
|
163
|
+
switch (state.kind) {
|
|
164
|
+
case 'absent':
|
|
165
|
+
changes.set(key, undefined);
|
|
166
|
+
break;
|
|
167
|
+
case 'file':
|
|
168
|
+
regularFiles.push({ key, mode: state.mode, relativePath });
|
|
169
|
+
break;
|
|
170
|
+
case 'link':
|
|
171
|
+
changes.set(key, { id: state.id, mode: '120000' });
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (regularFiles.length === 0) {
|
|
176
|
+
return { ok: true, changes };
|
|
177
|
+
}
|
|
178
|
+
const input = regularFiles.map((file) => `${quotePath(file.relativePath)}\n`).join('');
|
|
179
|
+
const hashResult = runGit(['hash-object', '--stdin-paths'], toplevel, input);
|
|
180
|
+
if (!hashResult.ok) {
|
|
181
|
+
return { ok: false, reason: `git hash-object failed: ${hashResult.error}` };
|
|
182
|
+
}
|
|
183
|
+
const ids = hashResult.stdout.split('\n').filter((line) => line !== '');
|
|
184
|
+
if (ids.length !== regularFiles.length) {
|
|
185
|
+
return { ok: false, reason: `git hash-object reported ${ids.length} ids for ${regularFiles.length} paths` };
|
|
186
|
+
}
|
|
187
|
+
for (const [index, file] of regularFiles.entries()) {
|
|
188
|
+
changes.set(file.key, { id: Buffer.from(ids[index] ?? '', 'hex'), mode: file.mode });
|
|
189
|
+
}
|
|
190
|
+
return { ok: true, changes };
|
|
191
|
+
}
|
|
192
|
+
function readHeadTrees(options) {
|
|
193
|
+
const { directories, format, headSha, headTreeId, toplevel } = options;
|
|
194
|
+
const ordered = [...directories];
|
|
195
|
+
const input = ordered
|
|
196
|
+
.map((directory) => (directory === '' ? `${headTreeId}\0` : `${headSha}:${directory}\0`))
|
|
197
|
+
.join('');
|
|
198
|
+
const batchResult = runGitForBytes(['cat-file', '--batch', '-z'], toplevel, Buffer.from(input, 'latin1'));
|
|
199
|
+
if (!batchResult.ok) {
|
|
200
|
+
return { ok: false, reason: `git cat-file failed: ${batchResult.error}` };
|
|
201
|
+
}
|
|
202
|
+
const output = batchResult.stdout;
|
|
203
|
+
const malformed = { ok: false, reason: 'git cat-file reported a malformed object' };
|
|
204
|
+
const trees = new Map();
|
|
205
|
+
let offset = 0;
|
|
206
|
+
for (const directory of ordered) {
|
|
207
|
+
const lineEnd = output.indexOf(0x0a, offset);
|
|
208
|
+
if (lineEnd === -1) {
|
|
209
|
+
return malformed;
|
|
210
|
+
}
|
|
211
|
+
const header = output.toString('latin1', offset, lineEnd);
|
|
212
|
+
offset = lineEnd + 1;
|
|
213
|
+
if (header.endsWith(' missing')) {
|
|
214
|
+
trees.set(directory, new Map());
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const [, type, sizeText] = header.split(' ', 3);
|
|
218
|
+
if (sizeText === undefined) {
|
|
219
|
+
return malformed;
|
|
220
|
+
}
|
|
221
|
+
const size = Number(sizeText);
|
|
222
|
+
if (!Number.isSafeInteger(size) || offset + size > output.length) {
|
|
223
|
+
return malformed;
|
|
224
|
+
}
|
|
225
|
+
const content = output.subarray(offset, offset + size);
|
|
226
|
+
offset += size + 1;
|
|
227
|
+
const entries = type === 'tree' ? parseTreeObject(format, content) : [];
|
|
228
|
+
if (entries === undefined) {
|
|
229
|
+
return malformed;
|
|
230
|
+
}
|
|
231
|
+
trees.set(directory, new Map(entries.map((entry) => [entry.name, entry])));
|
|
232
|
+
}
|
|
233
|
+
return { ok: true, trees };
|
|
109
234
|
}
|
|
110
|
-
function
|
|
235
|
+
function readPathState(toplevel, relativePath, format) {
|
|
111
236
|
const absolutePath = path.join(toplevel, relativePath);
|
|
237
|
+
if (hasSymlinkLeadingPath(toplevel, relativePath)) {
|
|
238
|
+
return { ok: true, kind: 'absent' };
|
|
239
|
+
}
|
|
112
240
|
let stats;
|
|
113
241
|
try {
|
|
114
242
|
stats = lstatSync(absolutePath);
|
|
115
243
|
}
|
|
116
244
|
catch (error) {
|
|
117
|
-
if (hasErrnoCode(error, 'ENOENT')) {
|
|
118
|
-
return { ok: true, kind: 'absent'
|
|
245
|
+
if (hasErrnoCode(error, 'ENOENT') || hasErrnoCode(error, 'ENOTDIR')) {
|
|
246
|
+
return { ok: true, kind: 'absent' };
|
|
119
247
|
}
|
|
120
248
|
return { ok: false, reason: `could not stat ${relativePath}: ${describeError(error)}` };
|
|
121
249
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
250
|
+
if (stats.isFile()) {
|
|
251
|
+
return { ok: true, kind: 'file', mode: (stats.mode & 0o100) === 0 ? '100644' : '100755' };
|
|
252
|
+
}
|
|
253
|
+
if (stats.isSymbolicLink()) {
|
|
254
|
+
try {
|
|
255
|
+
const target = readlinkSync(absolutePath, { encoding: 'buffer' });
|
|
256
|
+
return { ok: true, kind: 'link', id: hashGitObject(format, 'blob', target) };
|
|
125
257
|
}
|
|
126
|
-
|
|
127
|
-
return { ok:
|
|
258
|
+
catch (error) {
|
|
259
|
+
return { ok: false, reason: `could not read ${relativePath}: ${describeError(error)}` };
|
|
128
260
|
}
|
|
129
261
|
}
|
|
130
|
-
catch (error) {
|
|
131
|
-
return { ok: false, reason: `could not read ${relativePath}: ${describeError(error)}` };
|
|
132
|
-
}
|
|
133
262
|
return { ok: false, reason: `${relativePath} is neither a file nor a symlink, so its content is not hashable` };
|
|
134
263
|
}
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
for (let field = 0; field < precedingFieldCount; field++) {
|
|
141
|
-
const separator = record.indexOf(' ', index);
|
|
142
|
-
if (separator === -1) {
|
|
143
|
-
return undefined;
|
|
144
|
-
}
|
|
145
|
-
index = separator + 1;
|
|
146
|
-
}
|
|
147
|
-
const extracted = record.slice(index);
|
|
148
|
-
return extracted === '' ? undefined : extracted;
|
|
149
|
-
}
|
|
150
|
-
function runGit(args, cwd) {
|
|
151
|
-
const result = spawnSync('git', args, { cwd, encoding: 'utf8', maxBuffer: GIT_OUTPUT_LIMIT });
|
|
152
|
-
if (result.error) {
|
|
153
|
-
return { ok: false, error: result.error.message };
|
|
154
|
-
}
|
|
155
|
-
if (result.status !== 0) {
|
|
156
|
-
const stderr = result.stderr.trim();
|
|
157
|
-
return { ok: false, error: stderr || `\`git ${args.join(' ')}\` failed with exit code ${result.status}.` };
|
|
158
|
-
}
|
|
159
|
-
return { ok: true, stdout: result.stdout };
|
|
264
|
+
function splitPath(fullPath) {
|
|
265
|
+
const separator = fullPath.lastIndexOf('/');
|
|
266
|
+
return separator === -1
|
|
267
|
+
? { name: fullPath, parent: '' }
|
|
268
|
+
: { name: fullPath.slice(separator + 1), parent: fullPath.slice(0, separator) };
|
|
160
269
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Git's stdout, or the reason that git failed. */
|
|
2
|
+
export type GitResult<T> = {
|
|
3
|
+
ok: true;
|
|
4
|
+
stdout: T;
|
|
5
|
+
} | {
|
|
6
|
+
ok: false;
|
|
7
|
+
error: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Runs git and returns its stdout as text, or the reason it failed. A failure must never read as empty output: An
|
|
11
|
+
* unreported change would leave a caller certifying content that is no longer there.
|
|
12
|
+
*/
|
|
13
|
+
export declare function runGit(args: string[], cwd: string, input?: string): GitResult<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Runs git without a shell, so that a shell never interprets any part of a path or revision, and returns its raw
|
|
16
|
+
* stdout. `GIT_OPTIONAL_LOCKS=0` stops `git status` from writing its refreshed stat data back to the index.
|
|
17
|
+
*/
|
|
18
|
+
export declare function runGitForBytes(args: string[], cwd: string, input?: Buffer): GitResult<Buffer>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { GIT_OUTPUT_LIMIT } from "./gitOutputLimit.js";
|
|
3
|
+
export function runGit(args, cwd, input) {
|
|
4
|
+
const result = runGitForBytes(args, cwd, input === undefined ? undefined : Buffer.from(input, 'utf8'));
|
|
5
|
+
return result.ok ? { ok: true, stdout: result.stdout.toString('utf8') } : result;
|
|
6
|
+
}
|
|
7
|
+
export function runGitForBytes(args, cwd, input) {
|
|
8
|
+
const result = spawnSync('git', args, {
|
|
9
|
+
cwd,
|
|
10
|
+
env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' },
|
|
11
|
+
input,
|
|
12
|
+
maxBuffer: GIT_OUTPUT_LIMIT,
|
|
13
|
+
});
|
|
14
|
+
if (result.error) {
|
|
15
|
+
return { ok: false, error: result.error.message };
|
|
16
|
+
}
|
|
17
|
+
if (result.status !== 0) {
|
|
18
|
+
const stderr = result.stderr.toString('utf8').trim();
|
|
19
|
+
return { ok: false, error: stderr || `\`git ${args.join(' ')}\` failed with exit code ${result.status}.` };
|
|
20
|
+
}
|
|
21
|
+
return { ok: true, stdout: result.stdout };
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@williamthorsen/nmr-core",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Shared utilities for node-monorepo-tools packages",
|
|
6
6
|
"keywords": [
|
|
@@ -39,17 +39,17 @@
|
|
|
39
39
|
"dist"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@williamthorsen/toolbelt.errors": "0.8.
|
|
43
|
-
"@williamthorsen/toolbelt.filesystem": "0.9.
|
|
44
|
-
"@williamthorsen/toolbelt.packaging": "0.6.
|
|
42
|
+
"@williamthorsen/toolbelt.errors": "0.8.4",
|
|
43
|
+
"@williamthorsen/toolbelt.filesystem": "0.9.4",
|
|
44
|
+
"@williamthorsen/toolbelt.packaging": "0.6.4",
|
|
45
45
|
"@williamthorsen/toolbelt.terminal": "0.2.1",
|
|
46
46
|
"diff": "9.0.0",
|
|
47
47
|
"yaml": "2.9.1"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@williamthorsen/toolbelt.testing": "0.
|
|
51
|
-
"@williamthorsen/toolbelt.vitest": "0.
|
|
52
|
-
"vitest": "
|
|
50
|
+
"@williamthorsen/toolbelt.testing": "0.7.0",
|
|
51
|
+
"@williamthorsen/toolbelt.vitest": "0.12.0",
|
|
52
|
+
"vitest": "5.0.5"
|
|
53
53
|
},
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=24.16.0"
|