-
Notifications
You must be signed in to change notification settings - Fork 586
/
getRepoState.ts
515 lines (443 loc) · 15.4 KB
/
getRepoState.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type * as child_process from 'child_process';
import { once } from 'events';
import { Readable } from 'stream';
import { Executable, FileSystem, type IExecutableSpawnOptions } from '@rushstack/node-core-library';
export interface IGitVersion {
major: number;
minor: number;
patch: number;
}
const MINIMUM_GIT_VERSION: IGitVersion = {
major: 2,
minor: 20,
patch: 0
};
const STANDARD_GIT_OPTIONS: readonly string[] = [
// Don't request any optional file locks
'--no-optional-locks',
// Ensure that commands don't run automatic maintenance, since performance of the command itself is paramount
'-c',
'maintenance.auto=false'
];
interface IGitTreeState {
files: Map<string, string>; // type "blob"
submodules: Map<string, string>; // type "commit"
}
/**
* Parses the output of the "git ls-tree -r -z" command
* @internal
*/
export function parseGitLsTree(output: string): IGitTreeState {
const files: Map<string, string> = new Map();
const submodules: Map<string, string> = new Map();
// Parse the output
// With the -z modifier, paths are delimited by nulls
// A line looks like:
// <mode> <type> <newhash>\t<path>\0
// 100644 blob a300ccb0b36bd2c85ef18e3c619a2c747f95959e\ttools/prettier-git/prettier-git.js\0
let last: number = 0;
let index: number = output.indexOf('\0', last);
while (index >= 0) {
const item: string = output.slice(last, index);
const tabIndex: number = item.indexOf('\t');
const filePath: string = item.slice(tabIndex + 1);
// The newHash will be all zeros if the file is deleted, or a hash if it exists
const hash: string = item.slice(tabIndex - 40, tabIndex);
const spaceIndex: number = item.lastIndexOf(' ', tabIndex - 42);
const type: string = item.slice(spaceIndex + 1, tabIndex - 41);
switch (type) {
case 'commit': {
submodules.set(filePath, hash);
break;
}
case 'blob':
default: {
files.set(filePath, hash);
break;
}
}
last = index + 1;
index = output.indexOf('\0', last);
}
return {
files,
submodules
};
}
/**
* Parses the output of `git hash-object`
* yields [filePath, hash] pairs.
* @internal
*/
export function* parseGitHashObject(
output: string,
filePaths: ReadonlyArray<string>
): IterableIterator<[string, string]> {
const expected: number = filePaths.length;
if (expected === 0) {
return;
}
output = output.trim();
let last: number = 0;
let i: number = 0;
let index: number = output.indexOf('\n', last);
for (; i < expected && index > 0; i++) {
const hash: string = output.slice(last, index);
yield [filePaths[i], hash];
last = index + 1;
index = output.indexOf('\n', last);
}
// Handle last line. Will be non-empty to due trim() call.
if (index < 0) {
const hash: string = output.slice(last);
yield [filePaths[i], hash];
i++;
}
if (i !== expected) {
throw new Error(`Expected ${expected} hashes from "git hash-object" but received ${i}`);
}
}
/**
* Information about the changes to a file.
* @beta
*/
export interface IFileDiffStatus {
mode: string;
oldhash: string;
newhash: string;
status: 'A' | 'D' | 'M';
}
/**
* Parses the output of `git diff-index --color=never --no-renames --no-commit-id -z <REVISION> --
* Returns a map of file path to diff
* @internal
*/
export function parseGitDiffIndex(output: string): Map<string, IFileDiffStatus> {
const result: Map<string, IFileDiffStatus> = new Map();
// Parse the output
// With the -z modifier, paths are delimited by nulls
// A line looks like:
// :<oldmode> <newmode> <oldhash> <newhash> <status>\0<path>\0
// :100644 100644 a300ccb0b36bd2c85ef18e3c619a2c747f95959e 0000000000000000000000000000000000000000 M\0tools/prettier-git/prettier-git.js\0
let last: number = 0;
let index: number = output.indexOf('\0', last);
while (index >= 0) {
const header: string = output.slice(last, index);
const status: IFileDiffStatus['status'] = header.slice(-1) as IFileDiffStatus['status'];
last = index + 1;
index = output.indexOf('\0', last);
const filePath: string = output.slice(last, index);
// We passed --no-renames above, so a rename will be a delete of the old location and an add at the new.
// The newHash will be all zeros if the file is deleted, or a hash if it exists
const mode: string = header.slice(8, 14);
const oldhash: string = header.slice(-83, -43);
const newhash: string = header.slice(-42, -2);
result.set(filePath, {
mode,
oldhash,
newhash,
status
});
last = index + 1;
index = output.indexOf('\0', last);
}
return result;
}
/**
* Parses the output of `git status -z -u` to extract the set of files that have changed since HEAD.
*
* @param output - The raw output from Git
* @returns a map of file path to if it exists
* @internal
*/
export function parseGitStatus(output: string): Map<string, boolean> {
const result: Map<string, boolean> = new Map();
// Parse the output
// With the -z modifier, paths are delimited by nulls
// A line looks like:
// XY <path>\0
// M tools/prettier-git/prettier-git.js\0
let startOfLine: number = 0;
let eolIndex: number = output.indexOf('\0', startOfLine);
while (eolIndex >= 0) {
// We passed --no-renames above, so a rename will be a delete of the old location and an add at the new.
// charAt(startOfLine) is the index status, charAt(startOfLine + 1) is the working tree status
const workingTreeStatus: string = output.charAt(startOfLine + 1);
// Deleted in working tree, or not modified in working tree and deleted in index
const deleted: boolean =
workingTreeStatus === 'D' || (workingTreeStatus === ' ' && output.charAt(startOfLine) === 'D');
const filePath: string = output.slice(startOfLine + 3, eolIndex);
result.set(filePath, !deleted);
startOfLine = eolIndex + 1;
eolIndex = output.indexOf('\0', startOfLine);
}
return result;
}
const repoRootCache: Map<string, string> = new Map();
/**
* Finds the root of the current Git repository
*
* @param currentWorkingDirectory - The working directory for which to locate the repository
* @param gitPath - The path to the Git executable
*
* @returns The full path to the root directory of the Git repository
* @beta
*/
export function getRepoRoot(currentWorkingDirectory: string, gitPath?: string): string {
let cachedResult: string | undefined = repoRootCache.get(currentWorkingDirectory);
if (!cachedResult) {
const result: child_process.SpawnSyncReturns<string> = Executable.spawnSync(
gitPath || 'git',
['--no-optional-locks', 'rev-parse', '--show-toplevel'],
{
currentWorkingDirectory
}
);
if (result.status !== 0) {
ensureGitMinimumVersion(gitPath);
throw new Error(`git rev-parse exited with status ${result.status}: ${result.stderr}`);
}
cachedResult = result.stdout.trim();
repoRootCache.set(currentWorkingDirectory, cachedResult);
// To ensure that calling getRepoRoot on the result is a no-op.
repoRootCache.set(cachedResult, cachedResult);
}
return cachedResult;
}
/**
* Helper function for async process invocation with optional stdin support.
* @param gitPath - Path to the Git executable
* @param args - The process arguments
* @param currentWorkingDirectory - The working directory. Should be the repository root.
* @param stdin - An optional Readable stream to use as stdin to the process.
*/
async function spawnGitAsync(
gitPath: string | undefined,
args: string[],
currentWorkingDirectory: string,
stdin?: Readable
): Promise<string> {
const spawnOptions: IExecutableSpawnOptions = {
currentWorkingDirectory,
stdio: ['pipe', 'pipe', 'pipe']
};
let stdout: string = '';
let stderr: string = '';
const proc: child_process.ChildProcess = Executable.spawn(gitPath || 'git', args, spawnOptions);
proc.stdout!.setEncoding('utf-8');
proc.stderr!.setEncoding('utf-8');
proc.stdout!.on('data', (chunk: string) => {
stdout += chunk.toString();
});
proc.stderr!.on('data', (chunk: string) => {
stderr += chunk.toString();
});
if (stdin) {
stdin.pipe(proc.stdin!);
}
const [status] = await once(proc, 'exit');
if (status !== 0) {
throw new Error(`git ${args[0]} exited with code ${status}: ${stderr}`);
}
return stdout;
}
/**
* Gets the object hashes for all files in the Git repo, combining the current commit with working tree state.
* Uses async operations and runs all primary Git calls in parallel.
* @param rootDirectory - The root directory of the Git repository
* @param additionalRelativePathsToHash - Root-relative file paths to have Git hash and include in the results
* @param gitPath - The path to the Git executable
* @beta
*/
export async function getRepoStateAsync(
rootDirectory: string,
additionalRelativePathsToHash?: string[],
gitPath?: string
): Promise<Map<string, string>> {
const statePromise: Promise<IGitTreeState> = spawnGitAsync(
gitPath,
STANDARD_GIT_OPTIONS.concat([
'ls-tree',
// Recursively expand trees
'-r',
// Use NUL as the separator
'-z',
// Specify the full path to files relative to the root
'--full-name',
// As of last commit
'HEAD',
'--'
]),
rootDirectory
).then(parseGitLsTree);
const locallyModifiedPromise: Promise<Map<string, boolean>> = spawnGitAsync(
gitPath,
STANDARD_GIT_OPTIONS.concat([
'status',
// Use NUL as the separator
'-z',
// Include untracked files
'-u',
// Disable rename detection so that renames show up as add + delete
'--no-renames',
// Don't process submodules with this command; they'll be handled individually
'--ignore-submodules',
// Don't compare against the remote
'--no-ahead-behind',
'--'
]),
rootDirectory
).then(parseGitStatus);
const hashPaths: string[] = [];
async function* getFilesToHash(): AsyncIterableIterator<string> {
if (additionalRelativePathsToHash) {
for (const file of additionalRelativePathsToHash) {
hashPaths.push(file);
yield `${file}\n`;
}
}
const [{ files }, locallyModified] = await Promise.all([statePromise, locallyModifiedPromise]);
for (const [filePath, exists] of locallyModified) {
if (exists) {
hashPaths.push(filePath);
yield `${filePath}\n`;
} else {
files.delete(filePath);
}
}
}
const hashObjectPromise: Promise<string> = spawnGitAsync(
gitPath,
STANDARD_GIT_OPTIONS.concat(['hash-object', '--stdin-paths']),
rootDirectory,
Readable.from(getFilesToHash(), {
encoding: 'utf-8',
objectMode: false,
autoDestroy: true
})
);
const [{ files, submodules }, hashObject] = await Promise.all([
statePromise,
hashObjectPromise,
locallyModifiedPromise
]);
// The result of "git hash-object" will be a list of file hashes delimited by newlines
for (const [filePath, hash] of parseGitHashObject(hashObject, hashPaths)) {
files.set(filePath, hash);
}
// Existence check for the .gitmodules file
const hasSubmodules: boolean = submodules.size > 0 && FileSystem.exists(`${rootDirectory}/.gitmodules`);
if (hasSubmodules) {
// Submodules are not the normal critical path. Accept serial performance rather than investing in complexity.
// Can revisit if submodules become more commonly used.
for (const submodulePath of submodules.keys()) {
const submoduleState: Map<string, string> = await getRepoStateAsync(
`${rootDirectory}/${submodulePath}`,
[],
gitPath
);
for (const [filePath, hash] of submoduleState) {
files.set(`${submodulePath}/${filePath}`, hash);
}
}
}
return files;
}
/**
* Find all changed files tracked by Git, their current hashes, and the nature of the change. Only useful if all changes are staged or committed.
* @param currentWorkingDirectory - The working directory. Only used to find the repository root.
* @param revision - The Git revision specifier to detect changes relative to. Defaults to HEAD (i.e. will compare staged vs. committed)
* If comparing against a different branch, call `git merge-base` first to find the target commit.
* @param gitPath - The path to the Git executable
* @returns A map from the Git file path to the corresponding file change metadata
* @beta
*/
export function getRepoChanges(
currentWorkingDirectory: string,
revision: string = 'HEAD',
gitPath?: string
): Map<string, IFileDiffStatus> {
const rootDirectory: string = getRepoRoot(currentWorkingDirectory, gitPath);
const result: child_process.SpawnSyncReturns<string> = Executable.spawnSync(
gitPath || 'git',
STANDARD_GIT_OPTIONS.concat([
'diff-index',
'--color=never',
'--no-renames',
'--no-commit-id',
'--cached',
'-z',
revision,
'--'
]),
{
currentWorkingDirectory: rootDirectory
}
);
if (result.status !== 0) {
ensureGitMinimumVersion(gitPath);
throw new Error(`git diff-index exited with status ${result.status}: ${result.stderr}`);
}
const changes: Map<string, IFileDiffStatus> = parseGitDiffIndex(result.stdout);
return changes;
}
/**
* Checks the git version and throws an error if it is less than the minimum required version.
*
* @public
*/
export function ensureGitMinimumVersion(gitPath?: string): void {
const gitVersion: IGitVersion = getGitVersion(gitPath);
if (
gitVersion.major < MINIMUM_GIT_VERSION.major ||
(gitVersion.major === MINIMUM_GIT_VERSION.major && gitVersion.minor < MINIMUM_GIT_VERSION.minor) ||
(gitVersion.major === MINIMUM_GIT_VERSION.major &&
gitVersion.minor === MINIMUM_GIT_VERSION.minor &&
gitVersion.patch < MINIMUM_GIT_VERSION.patch)
) {
throw new Error(
`The minimum Git version required is ` +
`${MINIMUM_GIT_VERSION.major}.${MINIMUM_GIT_VERSION.minor}.${MINIMUM_GIT_VERSION.patch}. ` +
`Your version is ${gitVersion.major}.${gitVersion.minor}.${gitVersion.patch}.`
);
}
}
function getGitVersion(gitPath?: string): IGitVersion {
const result: child_process.SpawnSyncReturns<string> = Executable.spawnSync(
gitPath || 'git',
STANDARD_GIT_OPTIONS.concat(['version'])
);
if (result.status !== 0) {
throw new Error(
`While validating the Git installation, the "git version" command failed with ` +
`status ${result.status}: ${result.stderr}`
);
}
return parseGitVersion(result.stdout);
}
export function parseGitVersion(gitVersionOutput: string): IGitVersion {
// This regexp matches output of "git version" that looks like `git version <number>.<number>.<number>(+whatever)`
// Examples:
// - git version 1.2.3
// - git version 1.2.3.4.5
// - git version 1.2.3windows.1
// - git version 1.2.3.windows.1
const versionRegex: RegExp = /^git version (\d+)\.(\d+)\.(\d+)/;
const match: RegExpMatchArray | null = versionRegex.exec(gitVersionOutput);
if (!match) {
throw new Error(
`While validating the Git installation, the "git version" command produced ` +
`unexpected output: "${gitVersionOutput}"`
);
}
const major: number = parseInt(match[1], 10);
const minor: number = parseInt(match[2], 10);
const patch: number = parseInt(match[3], 10);
return {
major,
minor,
patch
};
}