Skip to content

Commit 829723d

Browse files
committed
Run getGitHashForFiles in batches.
1 parent 56a37a9 commit 829723d

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

‎libraries/package-deps-hash/src/getPackageDeps.ts‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import * as child_process from 'child_process';
55
import { IPackageDeps } from './IPackageDeps';
66

7+
// 8191 is the longest supported command on Windows
8+
const MAX_COMMAND_LENGTH: number = 8191;
9+
710
/**
811
* Parses the output of the "git ls-tree" command
912
*/
@@ -92,16 +95,42 @@ export function parseGitStatus(output: string, packagePath: string): Map<string,
9295
*/
9396
export function getGitHashForFiles(filesToHash: string[], packagePath: string): Map<string, string> {
9497
const changes: Map<string, string> = new Map<string, string>();
95-
if (filesToHash.length) {
98+
99+
let index: number = 0;
100+
while (index < filesToHash.length) {
101+
const HASH_OBJECT_COMMAND: string = 'git hash-object';
102+
const filesInBatch: string[] = [];
103+
let commandPartsTextLength: number = HASH_OBJECT_COMMAND.length;
104+
105+
for (; index < filesToHash.length; index++) {
106+
const filePath: string = filesToHash[index];
107+
// The "1" accounts for the space between commands
108+
const newLength: number = commandPartsTextLength + 1 + filePath.length;
109+
if (newLength > MAX_COMMAND_LENGTH) {
110+
break;
111+
}
112+
113+
filesInBatch.push(filePath);
114+
commandPartsTextLength = newLength;
115+
}
116+
96117
const hashStdout: string = child_process.execSync(
97-
'git hash-object ' + filesToHash.join(' '),
98-
{ cwd: packagePath }).toString();
118+
[HASH_OBJECT_COMMAND, ...filesInBatch].join(' '),
119+
{ cwd: packagePath }
120+
).toString().trim();
99121

100-
// The result of hashStdout will be a list of file hashes delimited by newlines
101122
const hashes: string[] = hashStdout.split('\n');
123+
if (hashes.length !== filesInBatch.length) {
124+
throw new Error(`Passed ${filesInBatch.length} file paths to Git to hash, but received ${hashes.length} hashes.`);
125+
}
102126

103-
filesToHash.forEach((filename, i) => changes.set(filename, hashes[i]));
127+
for (let i: number = 0; i < hashes.length; i++) {
128+
const hash: string = hashes[i];
129+
const filePath: string = filesInBatch[i];
130+
changes.set(filePath, hash);
131+
}
104132
}
133+
105134
return changes;
106135
}
107136

0 commit comments

Comments
 (0)