fix(core): git hasher should handle unstaged files with spaces

This commit is contained in:
Victor Savkin 2020-09-10 14:02:06 -04:00 committed by Victor Savkin
parent e9393805c1
commit cb15498d83
2 changed files with 28 additions and 2 deletions

View File

@ -78,7 +78,28 @@ describe('git-hasher', () => {
run(`echo AAA > "a b".txt`);
run(`git add .`);
run(`git commit -am init`);
expect([...getFileHashes(dir).keys()]).toEqual([`${dir}/a b.txt`]);
run(`touch "x y z.txt"`); // unstaged
expect([...getFileHashes(dir).keys()]).toEqual([
`${dir}/a b.txt`,
`${dir}/x y z.txt`,
]);
run(`git add .`);
expect([...getFileHashes(dir).keys()]).toEqual([
`${dir}/a b.txt`,
`${dir}/x y z.txt`,
]);
run(`mv "a b.txt" "a b moved.txt"`);
expect([...getFileHashes(dir).keys()]).toEqual([
`${dir}/x y z.txt`,
`${dir}/a b moved.txt`,
]);
run(`git add .`);
expect([...getFileHashes(dir).keys()]).toEqual([
`${dir}/a b moved.txt`,
`${dir}/x y z.txt`,
]);
run(`rm "x y z.txt"`);
expect([...getFileHashes(dir).keys()]).toEqual([`${dir}/a b moved.txt`]);
});
it('should handle renames and modifications', () => {

View File

@ -36,6 +36,7 @@ function parseGitStatus(output: string): Map<string, string> {
.match(/(?:[^\s"]+|"[^"]*")+/g)
.map((r) => (r.startsWith('"') ? r.substring(1, r.length - 1) : r))
.filter((r) => !!r);
if (changeType && filenames && filenames.length > 0) {
// the before filename we mark as deleted, so we remove it from the map
// changeType can be A/D/R/RM etc
@ -43,8 +44,12 @@ function parseGitStatus(output: string): Map<string, string> {
// the before part gets marked as deleted
if (changeType[0] === 'R') {
changes.set(filenames[0], 'D');
}
changes.set(filenames[filenames.length - 1], changeType);
} else if (changeType === '??') {
changes.set(filenames.join(' '), changeType);
} else {
changes.set(filenames[filenames.length - 1], changeType);
}
}
});
return changes;