From 34710a538e0904791e66421d46b7908870c21988 Mon Sep 17 00:00:00 2001 From: Tasos Bekos Date: Mon, 15 Mar 2021 17:00:12 +0200 Subject: [PATCH] fix(core): git hasher identifies deleted files Closes #4201 --- .../workspace/src/core/hasher/git-hasher.spec.ts | 4 ++++ packages/workspace/src/core/hasher/git-hasher.ts | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/workspace/src/core/hasher/git-hasher.spec.ts b/packages/workspace/src/core/hasher/git-hasher.spec.ts index 5d2be5307c493..57ef84bc3f1b5 100644 --- a/packages/workspace/src/core/hasher/git-hasher.spec.ts +++ b/packages/workspace/src/core/hasher/git-hasher.spec.ts @@ -5,15 +5,19 @@ import { getFileHashes } from './git-hasher'; describe('git-hasher', () => { let dir; + const warnSpy = jest.spyOn(console, 'warn'); beforeEach(() => { dir = dirSync().name; run(`git init`); run(`git config user.email "test@test.com"`); run(`git config user.name "test"`); + + warnSpy.mockClear(); }); afterEach(() => { + expect(console.warn).not.toHaveBeenCalled(); removeSync(dir); }); diff --git a/packages/workspace/src/core/hasher/git-hasher.ts b/packages/workspace/src/core/hasher/git-hasher.ts index ed5b8a85f1b26..8fe3a9c6293b6 100644 --- a/packages/workspace/src/core/hasher/git-hasher.ts +++ b/packages/workspace/src/core/hasher/git-hasher.ts @@ -32,11 +32,16 @@ function parseGitStatus(output: string): Map { for (let i = 0; i < chunks.length; i++) { const chunk = chunks[i]; if (chunk.length) { - const change = chunk[0]; - if (change === 'R') { + // The XY is the two-letter status code. + // See: https://git-scm.com/docs/git-status#_short_format + const X = chunk[0].trim(); + const Y = chunk[1].trim(); + const filename = chunk.substring(3); + if (X === 'R') { changes.set(chunks[++i], 'D'); } - changes.set(chunk.substring(2).trim(), change); + // If both present, Y shows the status of the working tree + changes.set(filename, Y || X); } } return changes; @@ -49,7 +54,7 @@ function spawnProcess(command: string, args: string[], cwd: string): string { `Failed to run ${command} ${args.join(' ')}.\n${r.stdout}\n${r.stderr}` ); } - return r.stdout.toString().trim(); + return r.stdout.toString().trimEnd(); } function getGitHashForFiles(