@tobi-or-not/quorum 0.5.0 → 0.6.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/README.md +2 -0
- package/dist/bench/fingerprint.d.ts +6 -5
- package/dist/bench/fingerprint.js +11 -5
- package/dist/bench/records.d.ts +100 -2
- package/dist/bench/records.js +80 -5
- package/dist/bench/runner.js +1 -1
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/main.d.ts +36 -13
- package/dist/cli/main.js +194 -44
- package/dist/cli/print-config.d.ts +5 -5
- package/dist/cli/print-config.js +10 -5
- package/dist/cli/render.d.ts +7 -1
- package/dist/cli/render.js +25 -3
- package/dist/config/claude-code.d.ts +2 -6
- package/dist/config/claude-code.js +6 -44
- package/dist/config/command.d.ts +10 -0
- package/dist/config/command.js +47 -0
- package/dist/config/from-config.js +8 -2
- package/dist/config/resolve.d.ts +4 -0
- package/dist/config/resolve.js +10 -0
- package/dist/config/schema.d.ts +13 -2
- package/dist/config/schema.js +36 -5
- package/dist/core/coverage.d.ts +38 -13
- package/dist/core/coverage.js +105 -28
- package/dist/core/diff.d.ts +77 -0
- package/dist/core/diff.js +122 -24
- package/dist/core/estimate.d.ts +45 -0
- package/dist/core/estimate.js +188 -0
- package/dist/core/flags.d.ts +14 -1
- package/dist/core/flags.js +56 -0
- package/dist/core/inputs.d.ts +38 -0
- package/dist/core/inputs.js +228 -0
- package/dist/core/limits.d.ts +5 -4
- package/dist/core/limits.js +11 -5
- package/dist/core/parts.d.ts +146 -0
- package/dist/core/parts.js +388 -0
- package/dist/core/ports.d.ts +10 -0
- package/dist/core/prompts.d.ts +22 -3
- package/dist/core/prompts.js +43 -7
- package/dist/core/quorum.d.ts +7 -5
- package/dist/core/quorum.js +164 -58
- package/dist/core/secrets.d.ts +98 -0
- package/dist/core/secrets.js +507 -0
- package/dist/core/stages/context.d.ts +16 -3
- package/dist/core/stages/context.js +44 -17
- package/dist/core/stages/panel.d.ts +8 -2
- package/dist/core/stages/panel.js +50 -9
- package/dist/core/stages/research.d.ts +107 -31
- package/dist/core/stages/research.js +290 -53
- package/dist/core/types.d.ts +217 -29
- package/dist/core/verify.d.ts +6 -0
- package/dist/core/verify.js +6 -3
- package/dist/git/run.d.ts +223 -0
- package/dist/git/run.js +608 -0
- package/dist/git/tree.d.ts +43 -0
- package/dist/git/tree.js +171 -0
- package/dist/index.d.ts +26 -2
- package/dist/index.js +34 -2
- package/dist/providers/anthropic.d.ts +2 -1
- package/dist/providers/anthropic.js +4 -2
- package/dist/providers/claude-code.d.ts +5 -0
- package/dist/providers/claude-code.js +11 -1
- package/dist/providers/gemini.d.ts +2 -1
- package/dist/providers/gemini.js +4 -2
- package/dist/providers/ollama.d.ts +2 -1
- package/dist/providers/ollama.js +4 -2
- package/dist/providers/openai-compatible.d.ts +2 -1
- package/dist/providers/openai-compatible.js +4 -2
- package/dist/providers/size.d.ts +9 -1
- package/dist/providers/size.js +18 -4
- package/dist/sources/added.d.ts +49 -0
- package/dist/sources/added.js +134 -0
- package/dist/sources/diff.d.ts +13 -0
- package/dist/sources/diff.js +63 -0
- package/dist/sources/local.d.ts +41 -0
- package/dist/sources/local.js +251 -0
- package/dist/sources/paths.d.ts +45 -0
- package/dist/sources/paths.js +230 -0
- package/dist/sources/range.d.ts +21 -0
- package/dist/sources/range.js +75 -0
- package/dist/sources/read.d.ts +69 -0
- package/dist/sources/read.js +37 -0
- package/dist/sources/repository.d.ts +9 -0
- package/dist/sources/repository.js +28 -0
- package/dist/sources/skips.d.ts +49 -0
- package/dist/sources/skips.js +126 -0
- package/package.json +1 -1
- package/schema/quorum.schema.json +15 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { decodeQuoted } from "../core/diff.js";
|
|
2
|
+
import { utf8Length } from "../core/tokens.js";
|
|
3
|
+
/**
|
|
4
|
+
* The all-added writer: files that have no diff of their own (a path review's, the untracked files of a
|
|
5
|
+
* local review) written as git writes a new file, so the core reads them as it reads any diff. The form
|
|
6
|
+
* is git's new-file form less its `index` line, with each name quoted as git quotes it, and a file cut
|
|
7
|
+
* into hunks of at most `HUNK_BYTES` at line ends, so research in parts spreads a large file across
|
|
8
|
+
* parts. It is a reading form and not a patch: `git apply` refuses a new file cut in several hunks.
|
|
9
|
+
*
|
|
10
|
+
* @see ../../docs/95-reference/40-architecture.md for where it sits in the layers.
|
|
11
|
+
*/
|
|
12
|
+
/** The most bytes of rows a hunk holds, line feeds included, a longer line aside, which goes alone in its hunk. */
|
|
13
|
+
export const HUNK_BYTES = 4096;
|
|
14
|
+
/** How many leading bytes git looks at for a NUL to call a file binary. */
|
|
15
|
+
export const BINARY_PROBE_BYTES = 8000;
|
|
16
|
+
/** Reads a name git writes as it is, which holds printable ASCII only. */
|
|
17
|
+
const ASCII_DECODER = new TextDecoder();
|
|
18
|
+
const NAMED = {
|
|
19
|
+
0x07: 'a',
|
|
20
|
+
0x08: 'b',
|
|
21
|
+
0x09: 't',
|
|
22
|
+
0x0A: 'n',
|
|
23
|
+
0x0B: 'v',
|
|
24
|
+
0x0C: 'f',
|
|
25
|
+
0x0D: 'r',
|
|
26
|
+
0x22: '"',
|
|
27
|
+
0x5C: '\\',
|
|
28
|
+
};
|
|
29
|
+
// A byte's escape under core.quotePath=true, as git's cq_lookup writes it, or undefined when git writes it as it is.
|
|
30
|
+
function escapeOf(byte) {
|
|
31
|
+
return byte in NAMED
|
|
32
|
+
? `\\${NAMED[byte]}`
|
|
33
|
+
: byte < 0x20 || byte >= 0x7F
|
|
34
|
+
? `\\${byte.toString(8).padStart(3, '0')}`
|
|
35
|
+
: undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The inside of git's quoting of a name, as `core.quotePath=true` writes it: `\a \b \t \n \v \f \r \" \\`
|
|
39
|
+
* for those nine bytes and three octal digits for every other byte below 0x20 and from 0x7F up.
|
|
40
|
+
* `undefined` when git writes the name as it is.
|
|
41
|
+
*/
|
|
42
|
+
export function quotedPath(raw) {
|
|
43
|
+
const escapes = Array.from(raw, escapeOf);
|
|
44
|
+
return escapes.some(escape => escape !== undefined)
|
|
45
|
+
? escapes.map((escape, at) => escape ?? String.fromCharCode(raw[at])).join('')
|
|
46
|
+
: undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The name `diffSections` reads back from the header this writer writes for `raw`: the name itself when
|
|
50
|
+
* git writes it as it is, else `decodeQuoted`, the core's own reading of git's quoted names, of its
|
|
51
|
+
* quoting, so writer and reader cannot drift.
|
|
52
|
+
*/
|
|
53
|
+
export function pathName(raw) {
|
|
54
|
+
const quoted = quotedPath(raw);
|
|
55
|
+
return quoted === undefined
|
|
56
|
+
? ASCII_DECODER.decode(raw)
|
|
57
|
+
: decodeQuoted(quoted);
|
|
58
|
+
}
|
|
59
|
+
/** Whether git would call `content` binary: a NUL among its first `BINARY_PROBE_BYTES` bytes. */
|
|
60
|
+
export function isBinary(content) {
|
|
61
|
+
return content.subarray(0, BINARY_PROBE_BYTES).includes(0);
|
|
62
|
+
}
|
|
63
|
+
/** A label of a header: the name behind `a/` or `b/`, the whole label quoted when any byte of the name needs it. */
|
|
64
|
+
function labelOf(prefix, raw) {
|
|
65
|
+
const quoted = quotedPath(raw);
|
|
66
|
+
return quoted === undefined
|
|
67
|
+
? `${prefix}${ASCII_DECODER.decode(raw)}`
|
|
68
|
+
: `"${prefix}${quoted}"`;
|
|
69
|
+
}
|
|
70
|
+
const BLOB_DECODER = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
71
|
+
/** The text of a blob: a byte that is not UTF-8 becomes U+FFFD, as `decodeDiff` makes it of git's own output, and a leading byte-order mark stays. */
|
|
72
|
+
export function blobText(content) {
|
|
73
|
+
return BLOB_DECODER.decode(content);
|
|
74
|
+
}
|
|
75
|
+
/** The lines of a text, split at line feeds, with no empty piece after a last one. */
|
|
76
|
+
function linesOf(text) {
|
|
77
|
+
const lines = text.split('\n');
|
|
78
|
+
if (lines.at(-1) === '')
|
|
79
|
+
lines.pop();
|
|
80
|
+
return lines;
|
|
81
|
+
}
|
|
82
|
+
/** A hunk's header: no old lines, and `count` new ones from line `first`, with no count for a single line, as git writes it. */
|
|
83
|
+
function hunkHeader(first, count) {
|
|
84
|
+
return count === 1
|
|
85
|
+
? `@@ -0,0 +${first} @@`
|
|
86
|
+
: `@@ -0,0 +${first},${count} @@`;
|
|
87
|
+
}
|
|
88
|
+
/** The hunks of `lines`: whole lines while their UTF-8 bytes, line feeds included, stay within `HUNK_BYTES`, a longer line alone. */
|
|
89
|
+
function hunksOf(lines) {
|
|
90
|
+
const hunks = [];
|
|
91
|
+
for (let start = 0; start < lines.length;) {
|
|
92
|
+
let end = start;
|
|
93
|
+
for (let bytes = 0; end < lines.length; end++) {
|
|
94
|
+
const size = utf8Length(lines[end]) + 1;
|
|
95
|
+
if (end > start && bytes + size > HUNK_BYTES)
|
|
96
|
+
break;
|
|
97
|
+
bytes += size;
|
|
98
|
+
}
|
|
99
|
+
hunks.push([
|
|
100
|
+
hunkHeader(start + 1, end - start),
|
|
101
|
+
...lines.slice(start, end).map(line => `+${line}`),
|
|
102
|
+
].join('\n'));
|
|
103
|
+
start = end;
|
|
104
|
+
}
|
|
105
|
+
return hunks;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* One file as git's new-file diff less its `index` line, with no final line feed: the header, the mode,
|
|
109
|
+
* then `--- /dev/null`, `+++` and the hunks, or git's binary line for a file with a NUL among its first
|
|
110
|
+
* 8000 bytes, or nothing more for an empty file and a gitlink. The `+++` line ends with a tab when its
|
|
111
|
+
* label holds a space, as git writes it, and a last line with no line feed is followed by git's
|
|
112
|
+
* ``.
|
|
113
|
+
*/
|
|
114
|
+
export function addedSection(file) {
|
|
115
|
+
const oldLabel = labelOf('a/', file.path);
|
|
116
|
+
const newLabel = labelOf('b/', file.path);
|
|
117
|
+
const lines = [`diff --git ${oldLabel} ${newLabel}`, `new file mode ${file.mode}`];
|
|
118
|
+
if (file.content.length === 0)
|
|
119
|
+
return lines.join('\n');
|
|
120
|
+
if (isBinary(file.content))
|
|
121
|
+
return [...lines, `Binary files /dev/null and ${newLabel} differ`].join('\n');
|
|
122
|
+
const text = blobText(file.content);
|
|
123
|
+
const tab = newLabel.includes(' ')
|
|
124
|
+
? '\t'
|
|
125
|
+
: '';
|
|
126
|
+
lines.push('--- /dev/null', `+++ ${newLabel}${tab}`, ...hunksOf(linesOf(text)));
|
|
127
|
+
if (!text.endsWith('\n'))
|
|
128
|
+
lines.push('\');
|
|
129
|
+
return lines.join('\n');
|
|
130
|
+
}
|
|
131
|
+
/** The sections of `files`, joined by line feeds and ending with one: `''` for no file. */
|
|
132
|
+
export function addedDiff(files) {
|
|
133
|
+
return files.map(file => `${addedSection(file)}\n`).join('');
|
|
134
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SourceRead } from './read.ts';
|
|
2
|
+
/** The diff source: a diff's text as the diff a review reads, nothing left out. A text over 16 MiB of UTF-8 is a `ConfigError`. */
|
|
3
|
+
export declare function diffSource(text: string): SourceRead;
|
|
4
|
+
/**
|
|
5
|
+
* Reads the diff from the file `file` names, resolved against `cwd`, or from `stdin` when none is named.
|
|
6
|
+
* Both are read as `decodeDiff` reads them, counted in bytes: a diff past 16 MiB is refused, and reading
|
|
7
|
+
* stops at the chunk that crosses the cap. A terminal with no file is refused, not waited on. Each failure
|
|
8
|
+
* is a `ConfigError` with an empty path, in a text that says what failed in this code's words, never the
|
|
9
|
+
* path: no file, a directory, a file that cannot be read, no diff given, a diff over the cap.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readDiffInput(cwd: string, file: string | undefined, stdin: AsyncIterable<Uint8Array | string> & {
|
|
12
|
+
readonly isTTY?: boolean;
|
|
13
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { decodeDiff, MAX_DIFF_BYTES } from "../core/diff.js";
|
|
4
|
+
import { ConfigError } from "../core/errors.js";
|
|
5
|
+
import { utf8Length } from "../core/tokens.js";
|
|
6
|
+
const OVER = 'the diff is over 16 MiB, the most quorum reads';
|
|
7
|
+
/** A usage error of the diff source: it names no flag, and its message holds no part of what the user typed. */
|
|
8
|
+
function problem(message) {
|
|
9
|
+
return new ConfigError([
|
|
10
|
+
{ path: '', message },
|
|
11
|
+
]);
|
|
12
|
+
}
|
|
13
|
+
/** The diff source: a diff's text as the diff a review reads, nothing left out. A text over 16 MiB of UTF-8 is a `ConfigError`. */
|
|
14
|
+
export function diffSource(text) {
|
|
15
|
+
if (utf8Length(text) > MAX_DIFF_BYTES)
|
|
16
|
+
throw problem(OVER);
|
|
17
|
+
return {
|
|
18
|
+
diff: text,
|
|
19
|
+
source: { kind: 'diff' },
|
|
20
|
+
leftOut: [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** What went wrong reading the diff file, in words that repeat no path: the system's own message holds it. */
|
|
24
|
+
function fileProblem(error) {
|
|
25
|
+
const code = typeof error === 'object' && error !== null && 'code' in error
|
|
26
|
+
? error.code
|
|
27
|
+
: undefined;
|
|
28
|
+
switch (code) {
|
|
29
|
+
case 'ENOENT':
|
|
30
|
+
case 'ENOTDIR':
|
|
31
|
+
return problem('the diff file does not exist');
|
|
32
|
+
case 'EISDIR':
|
|
33
|
+
return problem('the diff file is a directory');
|
|
34
|
+
default:
|
|
35
|
+
return problem('the diff file cannot be read');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Reads the diff from the file `file` names, resolved against `cwd`, or from `stdin` when none is named.
|
|
40
|
+
* Both are read as `decodeDiff` reads them, counted in bytes: a diff past 16 MiB is refused, and reading
|
|
41
|
+
* stops at the chunk that crosses the cap. A terminal with no file is refused, not waited on. Each failure
|
|
42
|
+
* is a `ConfigError` with an empty path, in a text that says what failed in this code's words, never the
|
|
43
|
+
* path: no file, a directory, a file that cannot be read, no diff given, a diff over the cap.
|
|
44
|
+
*/
|
|
45
|
+
export async function readDiffInput(cwd, file, stdin) {
|
|
46
|
+
if (!file && stdin.isTTY)
|
|
47
|
+
throw problem('no diff given');
|
|
48
|
+
let text;
|
|
49
|
+
if (file) {
|
|
50
|
+
try {
|
|
51
|
+
text = await decodeDiff(createReadStream(resolve(cwd, file)));
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw fileProblem(error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
text = await decodeDiff(stdin);
|
|
59
|
+
}
|
|
60
|
+
if (text === undefined)
|
|
61
|
+
throw problem(OVER);
|
|
62
|
+
return text;
|
|
63
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { FilterKey } from '../git/run.ts';
|
|
2
|
+
import type { SourceContext, SourceRead } from './read.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Whether the filter keys of a repository's config are all the user's own: each key's scope is `system` or
|
|
5
|
+
* `global`, the config of the machine and of the user, which run as they do with the user's own git. A key of
|
|
6
|
+
* the repository (`local`), of its work tree (`worktree`), of the command line or the environment
|
|
7
|
+
* (`command`), or of any scope git may add later is the repository's, whatever its value: a repository
|
|
8
|
+
* someone else made can name a program there. An empty value runs nothing, but refusing it keeps the rule
|
|
9
|
+
* one line.
|
|
10
|
+
*/
|
|
11
|
+
export declare function trustsConfig(keys: readonly FilterKey[]): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether two paths are the same one: equal after Unicode normalization to NFC. Git lists a name as NFC on
|
|
14
|
+
* macOS (`core.precomposeunicode`), where the file system may store it as NFD and `realpath` answers with
|
|
15
|
+
* what is stored, so the two spellings of one directory must not read as two directories. A link still
|
|
16
|
+
* resolves to another path, which no normalization makes equal.
|
|
17
|
+
*/
|
|
18
|
+
export declare function samePath(first: string, second: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The local sources: `local`, what `git add -A` would commit (the working tree against HEAD, and the
|
|
21
|
+
* untracked files), and `staged`, what the next commit holds (the index against HEAD). Both diff against
|
|
22
|
+
* HEAD, or against the empty tree before a first commit, which also stands for HEAD as the attributes'
|
|
23
|
+
* source, and `meta.source.head` is then null. The attributes are HEAD's, never the working tree's.
|
|
24
|
+
*
|
|
25
|
+
* A working-tree diff runs the clean and process filters a `filter=` attribute names, whatever the diff's
|
|
26
|
+
* flags say, so a repository whose own config names one is refused before any diff, unless
|
|
27
|
+
* `trustRepoConfig` says it is the user's ({@link trustsConfig}). The trust check runs for `staged` too,
|
|
28
|
+
* as the flag is one for both. A `ConfigError` also refuses a diff over 16 MiB, git's own bytes, the
|
|
29
|
+
* untracked files' sizes and what is written counted.
|
|
30
|
+
*
|
|
31
|
+
* The untracked files of `local` are read by quorum, never through git and never through a link. Git
|
|
32
|
+
* lists a link as one entry and a nested repository as `name/`; a link is written as mode 120000 and its
|
|
33
|
+
* target, a nested repository as a gitlink (the review lists it as a `submodule`), as `git add -A` would
|
|
34
|
+
* commit them. A name that is not UTF-8, one that leaves the top level, one under a link, an entry that is
|
|
35
|
+
* neither a regular file nor a link (a FIFO, a socket, a device) and a file that cannot be read are refused,
|
|
36
|
+
* and so is one that is gone or has changed since the listing. An untracked file the config's `exclude` matches is listed `excluded` and
|
|
37
|
+
* never read.
|
|
38
|
+
*
|
|
39
|
+
* @see ../../docs/20-using/15-sources.md#the-local-changes for what each source reviews.
|
|
40
|
+
*/
|
|
41
|
+
export declare function localSource(kind: 'local' | 'staged', trustRepoConfig: boolean, context: SourceContext): Promise<SourceRead>;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { lstat, readlink, realpath } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { readBounded } from "../config/load.js";
|
|
4
|
+
import { decodeDiff, MAX_DIFF_BYTES } from "../core/diff.js";
|
|
5
|
+
import { ConfigError } from "../core/errors.js";
|
|
6
|
+
import { excludeMatcher } from "../core/exclude.js";
|
|
7
|
+
import { utf8Length } from "../core/tokens.js";
|
|
8
|
+
import { addedDiff } from "./added.js";
|
|
9
|
+
import { openSourceRepository } from "./repository.js";
|
|
10
|
+
const UNTRACKED_FLAG = '--local';
|
|
11
|
+
const FILTER = 'the repository\'s own config names a filter program, which a working-tree diff runs: pass --trust-repo-config if the repository is yours, or review a range';
|
|
12
|
+
const OVER = 'its diff is over 16 MiB, the most quorum reads: review fewer changes at once';
|
|
13
|
+
const NOT_UTF8 = 'an untracked file\'s name is not UTF-8, which quorum cannot open: rename it, or name it in .gitignore';
|
|
14
|
+
const LEAVES = 'an untracked file\'s name leaves the top level';
|
|
15
|
+
const neither = (name) => `the untracked ${JSON.stringify(name)} is neither a regular file nor a link: remove it, or name it in .gitignore`;
|
|
16
|
+
const underLink = (name) => `the untracked ${JSON.stringify(name)} lies under a symbolic link`;
|
|
17
|
+
const unreadable = (name) => `the untracked ${JSON.stringify(name)} cannot be read: check its permissions, or name it in .gitignore`;
|
|
18
|
+
const changed = (name) => `the untracked ${JSON.stringify(name)} changed while quorum read it: run quorum again`;
|
|
19
|
+
function refusal(flag, message) {
|
|
20
|
+
return new ConfigError([
|
|
21
|
+
{ path: flag, message },
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether the filter keys of a repository's config are all the user's own: each key's scope is `system` or
|
|
26
|
+
* `global`, the config of the machine and of the user, which run as they do with the user's own git. A key of
|
|
27
|
+
* the repository (`local`), of its work tree (`worktree`), of the command line or the environment
|
|
28
|
+
* (`command`), or of any scope git may add later is the repository's, whatever its value: a repository
|
|
29
|
+
* someone else made can name a program there. An empty value runs nothing, but refusing it keeps the rule
|
|
30
|
+
* one line.
|
|
31
|
+
*/
|
|
32
|
+
export function trustsConfig(keys) {
|
|
33
|
+
return keys.every(({ scope }) => scope === 'system' || scope === 'global');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Whether two paths are the same one: equal after Unicode normalization to NFC. Git lists a name as NFC on
|
|
37
|
+
* macOS (`core.precomposeunicode`), where the file system may store it as NFD and `realpath` answers with
|
|
38
|
+
* what is stored, so the two spellings of one directory must not read as two directories. A link still
|
|
39
|
+
* resolves to another path, which no normalization makes equal.
|
|
40
|
+
*/
|
|
41
|
+
export function samePath(first, second) {
|
|
42
|
+
return first.normalize('NFC') === second.normalize('NFC');
|
|
43
|
+
}
|
|
44
|
+
/** Whether `error` says that what a path named is no longer there as it was: gone, no longer a directory, or no longer a link. */
|
|
45
|
+
function isChanged(error) {
|
|
46
|
+
const code = error.code;
|
|
47
|
+
return code === 'ENOENT' || code === 'ENOTDIR' || code === 'EINVAL';
|
|
48
|
+
}
|
|
49
|
+
/** Whether a failed `readBounded` says the file is no longer the regular file `lstat` saw: it is gone, is a link now, or holds more than its size. */
|
|
50
|
+
function vanished(read) {
|
|
51
|
+
return read.counted !== undefined || read.problem === 'does not exist' || read.problem === 'is a symbolic link';
|
|
52
|
+
}
|
|
53
|
+
/** What a failed `lstat`, `realpath` or `readlink` of the untracked `name` says: `changed` for a path that is gone or no longer what it was, `unreadable` for a permission, `neither` for any other failure. */
|
|
54
|
+
function failedCall(name, error) {
|
|
55
|
+
if (isChanged(error))
|
|
56
|
+
return changed(name);
|
|
57
|
+
return error.code === 'EACCES'
|
|
58
|
+
? unreadable(name)
|
|
59
|
+
: neither(name);
|
|
60
|
+
}
|
|
61
|
+
/** What `call` resolves to, or a refusal naming `name`, as `failedCall` says. */
|
|
62
|
+
async function inspected(name, call) {
|
|
63
|
+
try {
|
|
64
|
+
return await call();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw refusal(UNTRACKED_FLAG, failedCall(name, error));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* What a `readBounded` that failed on the untracked `name` says: `changed` when the file is gone, a link now
|
|
72
|
+
* or larger than its size; `unreadable` when it cannot be read and is still a regular file, which is a
|
|
73
|
+
* permission; `neither` for anything else, a FIFO, a socket or a device swapped in included.
|
|
74
|
+
*/
|
|
75
|
+
async function failedRead(name, absolute, read) {
|
|
76
|
+
if (vanished(read))
|
|
77
|
+
return changed(name);
|
|
78
|
+
if (read.problem !== 'cannot be read')
|
|
79
|
+
return neither(name);
|
|
80
|
+
try {
|
|
81
|
+
return (await lstat(absolute)).isFile()
|
|
82
|
+
? unreadable(name)
|
|
83
|
+
: neither(name);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return failedCall(name, error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The checks of the untracked names, each in listing order, that need no read of a file's bytes. A name is
|
|
91
|
+
* refused when it is not UTF-8 (quorum opens files by their text name), holds a `..` segment or starts with
|
|
92
|
+
* `/`. A name the config's `exclude` matches is listed `excluded`, and left unread and unsized. A name with a
|
|
93
|
+
* trailing `/` is a nested repository, which git adds as a gitlink and quorum never enters. Any other is
|
|
94
|
+
* `lstat`ed once its parent directory's real path is known to be the real top level joined with that parent
|
|
95
|
+
* (`samePath`), which no link in the chain would give, and is refused unless it is a regular file or a link;
|
|
96
|
+
* the sizes sum, and a sum over `budget` is refused before any file is read. An entry or a parent that is
|
|
97
|
+
* gone by then is refused as changed.
|
|
98
|
+
*/
|
|
99
|
+
async function planUntracked(top, names, excluded, budget) {
|
|
100
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
101
|
+
const realParents = new Map();
|
|
102
|
+
let realTop;
|
|
103
|
+
let total = 0;
|
|
104
|
+
const planned = [];
|
|
105
|
+
const leftOut = [];
|
|
106
|
+
for (const raw of names) {
|
|
107
|
+
let listed;
|
|
108
|
+
try {
|
|
109
|
+
listed = decoder.decode(raw);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
throw refusal(UNTRACKED_FLAG, NOT_UTF8);
|
|
113
|
+
}
|
|
114
|
+
if (listed.startsWith('/') || listed.split('/').includes('..'))
|
|
115
|
+
throw refusal(UNTRACKED_FLAG, LEAVES);
|
|
116
|
+
const gitlink = listed.endsWith('/');
|
|
117
|
+
const name = gitlink
|
|
118
|
+
? listed.slice(0, -1)
|
|
119
|
+
: listed;
|
|
120
|
+
if (excluded(name)) {
|
|
121
|
+
leftOut.push({ file: name, reason: 'excluded' });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (gitlink) {
|
|
125
|
+
planned.push({
|
|
126
|
+
name,
|
|
127
|
+
path: raw.subarray(0, raw.length - 1),
|
|
128
|
+
absolute: '',
|
|
129
|
+
kind: 'gitlink',
|
|
130
|
+
size: 0,
|
|
131
|
+
mode: '160000',
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
realTop ??= await realpath(top);
|
|
136
|
+
const parent = name.slice(0, Math.max(name.lastIndexOf('/'), 0));
|
|
137
|
+
let realParent = realParents.get(parent);
|
|
138
|
+
if (realParent === undefined) {
|
|
139
|
+
realParent = await inspected(name, () => realpath(join(top, parent)));
|
|
140
|
+
realParents.set(parent, realParent);
|
|
141
|
+
}
|
|
142
|
+
if (!samePath(realParent, join(realTop, parent)))
|
|
143
|
+
throw refusal(UNTRACKED_FLAG, underLink(name));
|
|
144
|
+
const absolute = join(top, name);
|
|
145
|
+
const stats = await inspected(name, () => lstat(absolute));
|
|
146
|
+
const link = stats.isSymbolicLink();
|
|
147
|
+
if (!(link || stats.isFile()))
|
|
148
|
+
throw refusal(UNTRACKED_FLAG, neither(name));
|
|
149
|
+
total += stats.size;
|
|
150
|
+
if (total > budget)
|
|
151
|
+
throw refusal(UNTRACKED_FLAG, OVER);
|
|
152
|
+
const mode = link
|
|
153
|
+
? '120000'
|
|
154
|
+
: (stats.mode & 0o100) === 0
|
|
155
|
+
? '100644'
|
|
156
|
+
: '100755';
|
|
157
|
+
planned.push({
|
|
158
|
+
name,
|
|
159
|
+
path: raw,
|
|
160
|
+
absolute,
|
|
161
|
+
kind: link ? 'link' : 'file',
|
|
162
|
+
size: stats.size,
|
|
163
|
+
mode,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return { planned, leftOut };
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The files of `planned`, read in order: a link as the bytes of its target, read with `readlink` and never
|
|
170
|
+
* followed; a regular file with `readBounded` without following a link, at most the size `lstat` gave it
|
|
171
|
+
* (or what is left of `budget`, the whole 16 MiB less git's own diff, less what was read, when that is
|
|
172
|
+
* less), so a buffer is a file's own size and a file that grew is caught; a gitlink as an empty file, which
|
|
173
|
+
* the writer writes as its header and mode alone. An entry that is gone, or is no longer what it was, or has
|
|
174
|
+
* outgrown its size, is refused as changed; a FIFO, a socket or a device swapped in as neither a file nor a
|
|
175
|
+
* link; a file that cannot be read, for its permissions, as unreadable.
|
|
176
|
+
*/
|
|
177
|
+
async function readPlanned(planned, budget) {
|
|
178
|
+
let left = budget;
|
|
179
|
+
const files = [];
|
|
180
|
+
for (const { name, path, absolute, kind, size, mode } of planned) {
|
|
181
|
+
let content = new Uint8Array(0);
|
|
182
|
+
if (kind === 'link') {
|
|
183
|
+
content = await inspected(name, () => readlink(absolute, { encoding: 'buffer' }));
|
|
184
|
+
}
|
|
185
|
+
else if (kind === 'file') {
|
|
186
|
+
const read = await readBounded(absolute, Math.min(left, size), '16 MiB', { noFollow: true });
|
|
187
|
+
if ('problem' in read)
|
|
188
|
+
throw refusal(UNTRACKED_FLAG, await failedRead(name, absolute, read));
|
|
189
|
+
content = read.value;
|
|
190
|
+
}
|
|
191
|
+
left -= content.length;
|
|
192
|
+
files.push({ path, mode, content });
|
|
193
|
+
}
|
|
194
|
+
return files;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The local sources: `local`, what `git add -A` would commit (the working tree against HEAD, and the
|
|
198
|
+
* untracked files), and `staged`, what the next commit holds (the index against HEAD). Both diff against
|
|
199
|
+
* HEAD, or against the empty tree before a first commit, which also stands for HEAD as the attributes'
|
|
200
|
+
* source, and `meta.source.head` is then null. The attributes are HEAD's, never the working tree's.
|
|
201
|
+
*
|
|
202
|
+
* A working-tree diff runs the clean and process filters a `filter=` attribute names, whatever the diff's
|
|
203
|
+
* flags say, so a repository whose own config names one is refused before any diff, unless
|
|
204
|
+
* `trustRepoConfig` says it is the user's ({@link trustsConfig}). The trust check runs for `staged` too,
|
|
205
|
+
* as the flag is one for both. A `ConfigError` also refuses a diff over 16 MiB, git's own bytes, the
|
|
206
|
+
* untracked files' sizes and what is written counted.
|
|
207
|
+
*
|
|
208
|
+
* The untracked files of `local` are read by quorum, never through git and never through a link. Git
|
|
209
|
+
* lists a link as one entry and a nested repository as `name/`; a link is written as mode 120000 and its
|
|
210
|
+
* target, a nested repository as a gitlink (the review lists it as a `submodule`), as `git add -A` would
|
|
211
|
+
* commit them. A name that is not UTF-8, one that leaves the top level, one under a link, an entry that is
|
|
212
|
+
* neither a regular file nor a link (a FIFO, a socket, a device) and a file that cannot be read are refused,
|
|
213
|
+
* and so is one that is gone or has changed since the listing. An untracked file the config's `exclude` matches is listed `excluded` and
|
|
214
|
+
* never read.
|
|
215
|
+
*
|
|
216
|
+
* @see ../../docs/20-using/15-sources.md#the-local-changes for what each source reviews.
|
|
217
|
+
*/
|
|
218
|
+
export async function localSource(kind, trustRepoConfig, context) {
|
|
219
|
+
const flag = `--${kind}`;
|
|
220
|
+
const repository = await openSourceRepository(context, flag);
|
|
221
|
+
if (!trustRepoConfig && !trustsConfig(await repository.filterConfig()))
|
|
222
|
+
throw refusal(flag, FILTER);
|
|
223
|
+
const head = await repository.resolveCommit('HEAD');
|
|
224
|
+
const base = head ?? await repository.emptyTree();
|
|
225
|
+
const bytes = kind === 'staged'
|
|
226
|
+
? await repository.diffIndex(base, base)
|
|
227
|
+
: await repository.diffWorktree(base, base);
|
|
228
|
+
const tracked = bytes === undefined
|
|
229
|
+
? undefined
|
|
230
|
+
: await decodeDiff([bytes]);
|
|
231
|
+
if (bytes === undefined || tracked === undefined)
|
|
232
|
+
throw refusal(flag, OVER);
|
|
233
|
+
let diff = tracked;
|
|
234
|
+
let leftOut = [];
|
|
235
|
+
if (kind === 'local') {
|
|
236
|
+
const names = await repository.untracked();
|
|
237
|
+
if (names === undefined)
|
|
238
|
+
throw refusal(flag, OVER);
|
|
239
|
+
const budget = MAX_DIFF_BYTES - bytes.length;
|
|
240
|
+
const plan = await planUntracked(repository.top, names, excludeMatcher(context.exclude ?? []), budget);
|
|
241
|
+
diff += addedDiff(await readPlanned(plan.planned, budget));
|
|
242
|
+
leftOut = plan.leftOut;
|
|
243
|
+
}
|
|
244
|
+
if (utf8Length(diff) > MAX_DIFF_BYTES)
|
|
245
|
+
throw refusal(flag, OVER);
|
|
246
|
+
return {
|
|
247
|
+
diff,
|
|
248
|
+
source: { kind, head: head ?? null, objectFormat: repository.objectFormat },
|
|
249
|
+
leftOut,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { SourceContext, SourceRead } from './read.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The problem with a `--path` value that can be told from the text alone, worded to follow `--path: `: it
|
|
4
|
+
* names no path when empty, and is absolute. The CLI refuses a value so before any config and any git, and
|
|
5
|
+
* `pathsSource` for a library caller. A problem never repeats the value.
|
|
6
|
+
*/
|
|
7
|
+
export declare function pathProblem(value: string): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The problem with an `--at` value that can be told from the text alone, worded to follow `--at: `: a
|
|
10
|
+
* revision that starts with `-` is an option to git, so it is refused before any git runs. The CLI refuses
|
|
11
|
+
* it before any config, and `pathsSource` before it opens the repository, for a library caller. A problem
|
|
12
|
+
* never repeats the value.
|
|
13
|
+
*/
|
|
14
|
+
export declare function atProblem(value: string | undefined): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* The path source: the files under one or more paths at a commit, written as an all-added diff, read from
|
|
17
|
+
* the commit's tree by id and never from the working tree (uncommitted edits are `--local`'s). The tree is
|
|
18
|
+
* listed once and filtered in code, so no user path reaches git: a value is checked, normalized against
|
|
19
|
+
* where the working directory sits in the repository, and matched against each name as the diff reads it
|
|
20
|
+
* back (`pathName`), with no pathspec magic and no link to follow. Git is handed one thing the user wrote,
|
|
21
|
+
* the revision of `--at`, resolved once and then known as a full id.
|
|
22
|
+
*
|
|
23
|
+
* Each selected entry, in git's order, is decided in this order: a gitlink is skipped as `submodule`,
|
|
24
|
+
* always; a name `exclude` matches is listed `excluded`; a name the core takes as a secret's is skipped as
|
|
25
|
+
* `secret`, whatever the skips, unless the context's `send` names the file; and with the skips on a name is
|
|
26
|
+
* skipped by the first other rule it fits, `vendored`, `lockfile` or `minified` (`nameRule`). The sizes of
|
|
27
|
+
* what is left must then be within 16 MiB before any blob is read, and the blobs are read through one
|
|
28
|
+
* `cat-file`. Once read, a Git LFS pointer is skipped as `lfs`, always; with the skips on a binary file is
|
|
29
|
+
* skipped as `binary`; a file whose text holds a credential is skipped as `secret`, whatever the skips,
|
|
30
|
+
* unless `send` names it; and with the skips on one whose lines are long is skipped as `minified`. With the
|
|
31
|
+
* skips off a binary file is written as git's binary line, a gap of every profile. So only `send` sends a
|
|
32
|
+
* secret, by its name and by its content, and `--no-default-skips` turns off the vendored, lockfile,
|
|
33
|
+
* minified and binary rules alone. `send` holds exact paths, as the diff writes them and `leftOut` lists
|
|
34
|
+
* them. A template (`.env.example`, a Helm chart's `secrets.yaml`) is no secret by name, so it is read and
|
|
35
|
+
* written unless it holds a credential. Nothing is read from a `.gitattributes`: a tree can still shape a
|
|
36
|
+
* file to match a rule, which is why each skip is listed in `leftOut` with its rule, and no skip is a gap.
|
|
37
|
+
*
|
|
38
|
+
* A `ConfigError` at `--path` or `--at` refuses a `--path` value that is empty or absolute and an `--at`
|
|
39
|
+
* that starts with `-` (before any git), a `--path` that leaves the top level or names no tracked file, a
|
|
40
|
+
* commit that does not exist or an unborn HEAD, a selection whose every file is left out (an empty review
|
|
41
|
+
* never passes), and a selection or a diff over 16 MiB.
|
|
42
|
+
*
|
|
43
|
+
* @see ../../docs/20-using/15-sources.md#the-files-at-a-commit for what a path review reads and skips.
|
|
44
|
+
*/
|
|
45
|
+
export declare function pathsSource(paths: readonly string[], at: string | undefined, skips: boolean, context: SourceContext): Promise<SourceRead>;
|