/
git-hasher.ts
151 lines (138 loc) · 4.05 KB
/
git-hasher.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
import { spawn } from 'child_process';
import { fileExists } from '../../utilities/fileutils';
import { joinPathFragments } from '@nrwl/devkit';
export async function getGitHashForFiles(
potentialFilesToHash: string[],
path: string
): Promise<{ hashes: Map<string, string>; deleted: string[] }> {
const { filesToHash, deleted } = getActualFilesToHash(
potentialFilesToHash,
path
);
const res: Map<string, string> = new Map<string, string>();
const promises: Promise<Map<string, string>>[] = [];
if (filesToHash.length) {
// On windows the max length is limited by the length of
// the overall comand, rather than the number of individual
// arguments. Since file paths are large and rather variable,
// we use a smaller batchSize.
const batchSize = process.platform === 'win32' ? 500 : 4000;
for (
let startIndex = 0;
startIndex < filesToHash.length;
startIndex += batchSize
) {
promises.push(
getGitHashForBatch(
filesToHash.slice(startIndex, startIndex + batchSize),
path
)
);
}
}
// Merge batch results into final result set
const batchResults = await Promise.all(promises);
for (const batch of batchResults) {
batch.forEach((v, k) => res.set(k, v));
}
return { hashes: res, deleted };
}
async function getGitHashForBatch(filesToHash: string[], path) {
const res: Map<string, string> = new Map<string, string>();
const hashStdout = await spawnProcess(
'git',
['hash-object', ...filesToHash],
path
);
const hashes: string[] = hashStdout.split('\n').filter((s) => !!s);
if (hashes.length !== filesToHash.length) {
throw new Error(
`Passed ${filesToHash.length} file paths to Git to hash, but received ${hashes.length} hashes.`
);
}
for (let i = 0; i < hashes.length; i++) {
const hash: string = hashes[i];
const filePath: string = filesToHash[i];
res.set(filePath, hash);
}
return res;
}
function getActualFilesToHash(
potentialFilesToHash: string[],
path: string
): { filesToHash: string[]; deleted: string[] } {
const filesToHash = [];
const deleted = [];
for (let f of potentialFilesToHash) {
if (fileExists(joinPathFragments(path, f))) {
filesToHash.push(f);
} else {
deleted.push(f);
}
}
return { filesToHash, deleted };
}
async function spawnProcess(command: string, args: string[], cwd: string) {
const cp = spawn(command, args, {
windowsHide: true,
shell: false,
cwd,
});
let s = '';
for await (const data of cp.stdout) {
s += data;
}
return s;
}
async function getStagedFiles(path: string) {
const staged = await spawnProcess(
'git',
['ls-files', '-s', '-z', '--exclude-standard', '.'],
path
);
const res = new Map();
for (let line of staged.split('\0')) {
if (!line) continue;
const [_, hash, __, ...fileParts] = line.split(/\s/);
const fileName = fileParts.join(' ');
res.set(fileName, hash);
}
return res;
}
async function getUnstagedFiles(path: string) {
const unstaged = await spawnProcess(
'git',
['ls-files', '-m', '-z', '--exclude-standard', '.'],
path
);
const lines = unstaged.split('\0').filter((f) => !!f);
return getGitHashForFiles(lines, path);
}
async function getUntrackedFiles(path: string) {
const untracked = await spawnProcess(
'git',
['ls-files', '--other', '-z', '--exclude-standard', '.'],
path
);
const lines = untracked.split('\0').filter((f) => !!f);
return getGitHashForFiles(lines, path);
}
export async function getFileHashes(path: string): Promise<{
allFiles: Map<string, string>;
}> {
const [staged, unstaged, untracked] = await Promise.all([
getStagedFiles(path),
getUnstagedFiles(path),
getUntrackedFiles(path),
]);
unstaged.hashes.forEach((hash: string, filename: string) => {
staged.set(filename, hash);
});
unstaged.deleted.forEach((filename) => {
staged.delete(filename);
});
untracked.hashes.forEach((hash: string, filename: string) => {
staged.set(filename, hash);
});
return { allFiles: staged };
}