|
4 | 4 | import * as child_process from 'child_process'; |
5 | 5 | import { IPackageDeps } from './IPackageDeps'; |
6 | 6 |
|
| 7 | +// 8191 is the longest supported command on Windows |
| 8 | +const MAX_COMMAND_LENGTH: number = 8191; |
| 9 | + |
7 | 10 | /** |
8 | 11 | * Parses the output of the "git ls-tree" command |
9 | 12 | */ |
@@ -92,16 +95,42 @@ export function parseGitStatus(output: string, packagePath: string): Map<string, |
92 | 95 | */ |
93 | 96 | export function getGitHashForFiles(filesToHash: string[], packagePath: string): Map<string, string> { |
94 | 97 | 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 | + |
96 | 117 | 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(); |
99 | 121 |
|
100 | | - // The result of hashStdout will be a list of file hashes delimited by newlines |
101 | 122 | 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 | + } |
102 | 126 |
|
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 | + } |
104 | 132 | } |
| 133 | + |
105 | 134 | return changes; |
106 | 135 | } |
107 | 136 |
|
|
0 commit comments