|
| 1 | +/** |
| 2 | + * What every tracked path looked like, so a battery can prove it changed none. |
| 3 | + * |
| 4 | + * A successful battery leaves tracked files byte-for-byte and mode-for-mode |
| 5 | + * unchanged (#279). Proving that needs a record per path and a comparison of |
| 6 | + * two records — not a "is the tree clean" question, which a worktree that was |
| 7 | + * already dirty when verify started would answer wrongly. |
| 8 | + * |
| 9 | + * ## Reading the index |
| 10 | + * |
| 11 | + * `git ls-files --stage -z` emits `<mode> <sha> <stage>\t<path>\0`. Two details |
| 12 | + * decide the parser, both measured: |
| 13 | + * |
| 14 | + * - **Only the first tab delimits.** A tracked filename may contain a tab — |
| 15 | + * `tab\tname.txt` arrives as `100644 <sha> 0\ttab\tname.txt` — so splitting |
| 16 | + * on every tab truncates the path. NUL delimits records, so a filename |
| 17 | + * containing a newline needs nothing special. |
| 18 | + * - **Mode `160000` is rejected before anything touches the filesystem.** A |
| 19 | + * gitlink whose working copy was emptied `lstat`s as a *directory*; one whose |
| 20 | + * directory was removed raises `ENOENT` and would be recorded as an absent |
| 21 | + * file, so an uninitialized submodule would compare equal to a deleted one. |
| 22 | + * Refusing on the recorded mode is what makes that impossible. |
| 23 | + * |
| 24 | + * A stage other than 0 is an unmerged path. A conflicted worktree is not a |
| 25 | + * state in which "nothing moved" means anything, so it is reported rather than |
| 26 | + * fingerprinted. |
| 27 | + */ |
| 28 | + |
| 29 | +export class UnsupportedEntryError extends Error {} |
| 30 | + |
| 31 | +export const GITLINK_MODE = "160000"; |
| 32 | + |
| 33 | +export interface TrackedRecord { |
| 34 | + mode: string; |
| 35 | + oid: string; |
| 36 | + path: string; |
| 37 | +} |
| 38 | + |
| 39 | +export type TrackedEntry = |
| 40 | + | { kind: "file"; digest: string; executable: boolean } |
| 41 | + | { kind: "symlink"; target: string } |
| 42 | + | { kind: "absent" }; |
| 43 | + |
| 44 | +export type TrackedState = ReadonlyMap<string, TrackedEntry>; |
| 45 | + |
| 46 | +/** |
| 47 | + * Records from `git ls-files --stage -z`, with the entries no fingerprint can |
| 48 | + * describe refused rather than skipped. |
| 49 | + */ |
| 50 | +export function parseStageRecords(output: string): TrackedRecord[] { |
| 51 | + const records: TrackedRecord[] = []; |
| 52 | + for (const field of output.split("\0")) { |
| 53 | + if (field.length === 0) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + const tab = field.indexOf("\t"); |
| 57 | + if (tab < 0) { |
| 58 | + throw new UnsupportedEntryError(`git ls-files emitted a record with no path: ${field}`); |
| 59 | + } |
| 60 | + const [mode, oid, stage] = field.slice(0, tab).split(" "); |
| 61 | + const path = field.slice(tab + 1); |
| 62 | + if (mode === undefined || oid === undefined || stage === undefined) { |
| 63 | + throw new UnsupportedEntryError(`git ls-files emitted incomplete metadata for ${path}`); |
| 64 | + } |
| 65 | + if (stage !== "0") { |
| 66 | + throw new UnsupportedEntryError( |
| 67 | + `${path} is unmerged (stage ${stage}); resolve the conflict before verifying`, |
| 68 | + ); |
| 69 | + } |
| 70 | + if (mode === GITLINK_MODE) { |
| 71 | + throw new UnsupportedEntryError( |
| 72 | + `${path} is a submodule, which this fingerprint cannot describe`, |
| 73 | + ); |
| 74 | + } |
| 75 | + records.push({ mode, oid, path }); |
| 76 | + } |
| 77 | + return records; |
| 78 | +} |
| 79 | + |
| 80 | +function describe(entry: TrackedEntry): string { |
| 81 | + if (entry.kind === "file") { |
| 82 | + return `${entry.digest.slice(0, 12)}${entry.executable ? " +x" : ""}`; |
| 83 | + } |
| 84 | + if (entry.kind === "symlink") { |
| 85 | + return `-> ${entry.target}`; |
| 86 | + } |
| 87 | + return "absent"; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Every tracked path whose content, mode, symlink target, or presence moved. |
| 92 | + * |
| 93 | + * Both states cover the same paths by construction — the enumeration is the |
| 94 | + * index, not the filesystem — so a path missing from either is itself a change |
| 95 | + * worth naming. |
| 96 | + */ |
| 97 | +export function compareTracked(before: TrackedState, after: TrackedState): string[] { |
| 98 | + const moved: string[] = []; |
| 99 | + for (const [path, entry] of [...before].sort(([left], [right]) => left.localeCompare(right))) { |
| 100 | + const found = after.get(path); |
| 101 | + if (!found) { |
| 102 | + moved.push(`${path}: no longer tracked`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + if (!same(entry, found)) { |
| 106 | + moved.push(`${path}: ${describe(entry)} -> ${describe(found)}`); |
| 107 | + } |
| 108 | + } |
| 109 | + for (const path of [...after.keys()].sort()) { |
| 110 | + if (!before.has(path)) { |
| 111 | + moved.push(`${path}: newly tracked`); |
| 112 | + } |
| 113 | + } |
| 114 | + return moved; |
| 115 | +} |
| 116 | + |
| 117 | +function same(left: TrackedEntry, right: TrackedEntry): boolean { |
| 118 | + if (left.kind === "file" && right.kind === "file") { |
| 119 | + return left.digest === right.digest && left.executable === right.executable; |
| 120 | + } |
| 121 | + if (left.kind === "symlink" && right.kind === "symlink") { |
| 122 | + return left.target === right.target; |
| 123 | + } |
| 124 | + return left.kind === "absent" && right.kind === "absent"; |
| 125 | +} |
0 commit comments