Skip to content

Commit 8ece961

Browse files
authored
Merge pull request #1332 from chromaui/CAP-4400
Fix `getUncommittedHash` in Windows environments
2 parents 1a8c8ce + 5087fb8 commit 8ece961

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

‎node-src/git/execGit.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ export async function execGitCommand(
3232
) {
3333
try {
3434
log.debug(`execGitCommand: ${command}`);
35-
const { all } = await runCommand(command, { ...defaultOptions, ...options });
35+
const { all, stdout } = await runCommand(command, { ...defaultOptions, ...options });
36+
// If the caller sets `all: false`, then `stdout` will be the output. Otherwise, `all` will
37+
// contain interleaved stdout and stderr.
38+
const output = all ?? stdout;
3639

37-
if (all === undefined) {
40+
if (output === undefined) {
3841
throw new Error(`Unexpected missing git command output for command: '${command}'`);
3942
}
4043

41-
const result = all.toString();
44+
const result = output.toString();
4245
log.debug(`execGitCommand result: '${result}'`);
4346
return result;
4447
} catch (error) {

‎node-src/git/git.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,37 @@ export async function getUncommittedHash(deps: Pick<Deps, 'log'>) {
118118
const listStagedFiles = 'git diff --name-only --no-relative --diff-filter=d --cached';
119119
const listUnstagedFiles = 'git diff --name-only --no-relative --diff-filter=d';
120120
const listUntrackedFiles = 'git ls-files --others --exclude-standard';
121-
const listUncommittedFiles = [listStagedFiles, listUnstagedFiles, listUntrackedFiles].join(';');
122121

123-
const uncommittedHashWithPadding = await execGitCommand(
124-
deps,
125-
// Pass the combined list of filenames to hash-object to retrieve a list of hashes. Then pass
126-
// the list of hashes to hash-object again to retrieve a single hash of all hashes. We use
127-
// stdin to avoid the limit on command line arguments.
128-
`(${listUncommittedFiles}) | git hash-object --stdin-paths | git hash-object --stdin`
129-
);
122+
// Use `all: false` so only stdout is captured and we can ignore stderr output that doesn't impact
123+
// the result.
124+
const stdoutOnly = { all: false };
125+
const [stagedOutput, unstagedOutput, untrackedOutput] = await Promise.all([
126+
execGitCommand(deps, listStagedFiles, stdoutOnly),
127+
execGitCommand(deps, listUnstagedFiles, stdoutOnly),
128+
execGitCommand(deps, listUntrackedFiles, stdoutOnly),
129+
]);
130+
131+
// Combine file listings into one path per line, stripping empty lines (including trailing
132+
// newlines from each output).
133+
const files = [stagedOutput, unstagedOutput, untrackedOutput]
134+
.flatMap((output) => output?.split(newline) ?? [])
135+
.filter(Boolean)
136+
.join('\n');
137+
138+
if (!files) {
139+
return '';
140+
}
141+
142+
// Pass the combined list of filenames to hash-object to retrieve a list of hashes. Then pass
143+
// the list of hashes to hash-object again to retrieve a single hash of all hashes. We use
144+
// stdin to avoid the limit on command line arguments.
145+
const fileHashes = await execGitCommand(deps, 'git hash-object --stdin-paths', {
146+
input: files,
147+
});
148+
149+
const uncommittedHashWithPadding = await execGitCommand(deps, 'git hash-object --stdin', {
150+
input: fileHashes,
151+
});
130152
const uncommittedHash = uncommittedHashWithPadding?.trim();
131153

132154
// In case there are no uncommited changes (empty list), we always get this same hash.

0 commit comments

Comments
 (0)