Repository navigation
Expand file tree
/
Copy pathcommitted-source-tree.ts
More file actions
184 lines (170 loc) · 7.71 KB
/
Copy pathcommitted-source-tree.ts
File metadata and controls
184 lines (170 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// A committed git tree as the closure walker's source of truth: the merge-base with origin/main,
// read without checking it out, so a ratchet compares against what actually landed.
import { execFileSync } from 'node:child_process';
import path from 'node:path';
import type { SourceTreeReader } from '../../src/__tests__/eager-import-closure.fixtures.ts';
import { isProductionSourceFile } from '../layering/tracked-sources.ts';
const WALKED_SOURCE = /^(?:src|packages\/[^/]+\/src)\/.*\.ts$/;
const WALKED_MANIFEST = /^packages\/[^/]+\/package\.json$/;
function git(repoRoot: string, args: readonly string[], input = ''): Buffer {
return execFileSync('git', [...args], {
cwd: repoRoot,
input,
maxBuffer: 256 * 1024 * 1024,
stdio: ['pipe', 'pipe', 'pipe'],
});
}
/** The commit a branch's closures ratchet against: `git merge-base origin/main HEAD`. */
export function mergeBaseWithMain(repoRoot: string): string {
try {
return git(repoRoot, ['merge-base', 'origin/main', 'HEAD']).toString('utf8').trim();
} catch (error) {
const stderr = (error as { stderr?: Buffer }).stderr?.toString('utf8').trim() ?? '';
throw new Error(
'The eager-closure ratchet needs origin/main to find its merge-base (git merge-base ' +
`origin/main HEAD failed: ${stderr}). Fetch origin/main; the gate does not skip.`,
{ cause: error },
);
}
}
/**
* The commit under measurement. Compared against `mergeBaseWithMain`, this answers whether the
* head carries work of its own: they are equal exactly when `main` already carries this commit --
* a push to `main`, or a branch that has not committed anything yet -- and then no entry is
* first-introduced, whatever the tree contains.
*/
export function headCommit(repoRoot: string): string {
return git(repoRoot, ['rev-parse', 'HEAD']).toString('utf8').trim();
}
/** Files renamed since `base`, current path -> path at `base`, so a rename is not a new entry. */
export function renamedSince(repoRoot: string, base: string): ReadonlyMap<string, string> {
const renamed = new Map<string, string>();
const status = git(repoRoot, ['diff', '--name-status', '-M', '--diff-filter=R', '-z', base]);
const fields = status.toString('utf8').split('\0');
for (let index = 0; index + 2 < fields.length; index += 3) {
const [from, to] = [fields[index + 1], fields[index + 2]];
if (from && to) renamed.set(to, from);
}
return renamed;
}
/** A carried entry must have been a source the base closure walker could actually read. */
export function baseProductionPathOf(
repoRoot: string,
entryFile: string,
baseTree: SourceTreeReader,
renamedFrom: ReadonlyMap<string, string>,
): string | null {
const baseFile = renamedFrom.get(entryFile) ?? entryFile;
return isProductionSourceFile(baseFile) && baseTree.isFile(path.resolve(repoRoot, baseFile))
? baseFile
: null;
}
/** Every path tracked at `treeish`, repo-root-relative, from ONE `git ls-tree`. */
function listCommittedTree(repoRoot: string, treeish: string): string[] {
const listing = git(repoRoot, ['ls-tree', '-r', '--name-only', '-z', treeish]).toString('utf8');
return listing.split('\0').filter(Boolean);
}
/**
* The sources this reader serves, split by kind: production TypeScript under `src/` and
* `packages/<pkg>/src/`, and workspace package manifests. One definition, so a consumer reading
* a committed tree cannot classify it differently from the walker.
*/
function committedSourceSet(tracked: readonly string[]): {
sources: string[];
manifests: string[];
} {
return {
sources: tracked.filter((file) => WALKED_SOURCE.test(file) && isProductionSourceFile(file)),
manifests: tracked.filter((file) => WALKED_MANIFEST.test(file)),
};
}
/** Contents of `files` at `treeish`, through ONE long-lived `git cat-file --batch`. */
function readCommittedBlobs(
repoRoot: string,
treeish: string,
files: readonly string[],
): Map<string, string> {
if (files.length === 0) return new Map();
const requests = files.map((file) => `${treeish}:${file}\n`).join('');
return parseCatFileBatch(git(repoRoot, ['cat-file', '--batch'], requests), files);
}
/**
* The same enumeration and blob read as `createCommittedSourceTree`, handed over as text: the
* production sources and workspace manifests committed at `treeish`. A whole-tree measurement
* (the layering ratchets) needs the corpus rather than a reader, and taking it from here is what
* keeps its file set identical to the closure walker's.
*/
// fallow-ignore-next-line unused-export -- consumed by scripts/layering, outside fallow's scope
export function readCommittedSources(
repoRoot: string,
treeish: string,
): { sources: Map<string, string>; manifests: Map<string, string> } {
const { sources, manifests } = committedSourceSet(listCommittedTree(repoRoot, treeish));
const blobs = readCommittedBlobs(repoRoot, treeish, [...sources, ...manifests]);
const only = (files: readonly string[]) =>
new Map(files.flatMap((file) => (blobs.has(file) ? [[file, blobs.get(file)!] as const] : [])));
return { sources: only(sources), manifests: only(manifests) };
}
/**
* `<sha> blob <size>\n<size bytes>\n` per hit and `<request> missing\n` per miss, in request
* order. Sizes are bytes, so this walks the raw buffer rather than a string offset.
*/
function parseCatFileBatch(output: Buffer, files: readonly string[]): Map<string, string> {
const contents = new Map<string, string>();
let offset = 0;
for (const file of files) {
const headerEnd = output.indexOf(0x0a, offset);
const blob = /^\S+ blob (\d+)$/.exec(output.toString('utf8', offset, headerEnd));
offset = headerEnd + 1;
if (!blob) continue;
const size = Number(blob[1]);
contents.set(file, output.toString('utf8', offset, offset + size));
offset += size + 1;
}
return contents;
}
/** Every ancestor directory of the tracked paths, so `exists` answers for directories too. */
function directoriesOf(files: ReadonlySet<string>): Set<string> {
const directories = new Set<string>();
for (const file of files) {
for (let dir = path.posix.dirname(file); dir !== '.'; dir = path.posix.dirname(dir)) {
if (directories.has(dir)) break;
directories.add(dir);
}
}
return directories;
}
/**
* The walker's view of `treeish`: tracked paths from one `git ls-tree`, and every source the
* walker can reach (production TypeScript under `src/` and `packages/<pkg>/src/`, package
* manifests) from ONE `git cat-file --batch` fed those paths up front -- two processes for the
* whole tree, never one per file. A read outside that set is a widening request, not a fallback.
*/
export function createCommittedSourceTree(repoRoot: string, treeish: string): SourceTreeReader {
const listing = listCommittedTree(repoRoot, treeish);
const tracked = new Set(listing);
const directories = directoriesOf(tracked);
const { sources, manifests } = committedSourceSet(listing);
const contents = readCommittedBlobs(repoRoot, treeish, [...sources, ...manifests]);
const relative = (file: string) => path.relative(repoRoot, file).split(path.sep).join('/');
return {
exists: (file) => tracked.has(relative(file)) || directories.has(relative(file)),
isFile: (file) => tracked.has(relative(file)),
readdir: (dir) => {
const prefix = `${relative(dir)}/`;
const names = new Set<string>();
for (const entry of [...tracked, ...directories]) {
if (entry.startsWith(prefix)) names.add(entry.slice(prefix.length).split('/')[0] ?? '');
}
names.delete('');
return [...names].sort();
},
readFile: (file) => {
const source = contents.get(relative(file));
if (source === undefined) {
throw new Error(`${relative(file)} is not a source the closure walker reads at ${treeish}`);
}
return source;
},
};
}