From cb15498d83cd2c5b3b740df9081869fc37feb6fa Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Thu, 10 Sep 2020 14:02:06 -0400 Subject: [PATCH] fix(core): git hasher should handle unstaged files with spaces --- .../src/core/hasher/git-hasher.spec.ts | 23 ++++++++++++++++++- .../workspace/src/core/hasher/git-hasher.ts | 7 +++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/workspace/src/core/hasher/git-hasher.spec.ts b/packages/workspace/src/core/hasher/git-hasher.spec.ts index 44362940d6..b3193cf534 100644 --- a/packages/workspace/src/core/hasher/git-hasher.spec.ts +++ b/packages/workspace/src/core/hasher/git-hasher.spec.ts @@ -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', () => { diff --git a/packages/workspace/src/core/hasher/git-hasher.ts b/packages/workspace/src/core/hasher/git-hasher.ts index 90e71eae4a..ea45abb1d2 100644 --- a/packages/workspace/src/core/hasher/git-hasher.ts +++ b/packages/workspace/src/core/hasher/git-hasher.ts @@ -36,6 +36,7 @@ function parseGitStatus(output: string): Map { .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 { // 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); } - changes.set(filenames[filenames.length - 1], changeType); } }); return changes;