From a7812e382b2dd5c5e9e51b69a56dc9ace382dd69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20Ja=CC=88ppinen?= Date: Mon, 27 Jan 2020 07:50:25 +0200 Subject: [PATCH] fix: generate untracked diff using same arguments as unstaged --- lib/gitWorkflow.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index d0f740985..d43216af7 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -12,7 +12,8 @@ const MERGE_MSG = 'MERGE_MSG' const STASH = 'lint-staged automatic backup' -const gitApplyArgs = ['apply', '-v', '--whitespace=nowarn', '--recount', '--unidiff-zero'] +const GIT_APPLY_ARGS = ['apply', '-v', '--whitespace=nowarn', '--recount', '--unidiff-zero'] +const GIT_DIFF_ARGS = ['--binary', '--unified=0', '--no-color', '--no-ext-diff', '--patch'] /** * Delete untracked files using `git clean` @@ -123,11 +124,7 @@ class GitWorkflow { // Get a diff of unstaged changes by diffing the saved stash against what's left on disk. this.unstagedDiff = await this.execGit([ 'diff', - '--binary', - '--unified=0', - '--no-color', - '--no-ext-diff', - '--patch', + ...GIT_DIFF_ARGS, await this.getBackupStash(ctx), '-R' // Show diff in reverse ]) @@ -173,7 +170,7 @@ class GitWorkflow { if (this.unstagedDiff) { debug('Restoring unstaged changes...') try { - await this.execGit(gitApplyArgs, { input: `${this.unstagedDiff}\n` }) + await this.execGit(GIT_APPLY_ARGS, { input: `${this.unstagedDiff}\n` }) } catch (error) { debug('Error while restoring changes:') debug(error) @@ -181,7 +178,7 @@ class GitWorkflow { try { // Retry with `--3way` if normal apply fails - await this.execGit([...gitApplyArgs, '--3way'], { input: `${this.unstagedDiff}\n` }) + await this.execGit([...GIT_APPLY_ARGS, '--3way'], { input: `${this.unstagedDiff}\n` }) } catch (error2) { debug('Error while restoring unstaged changes using 3-way merge:') debug(error2) @@ -196,21 +193,20 @@ class GitWorkflow { } // Restore untracked files by reading from the third commit associated with the backup stash - // Git will return with error code if the commit doesn't exist // See https://stackoverflow.com/a/52357762 try { const backupStash = await this.getBackupStash(ctx) - const output = await this.execGit(['show', '--no-color', '--format=%b', `${backupStash}^3`]) + const output = await this.execGit([ + 'show', + ...GIT_DIFF_ARGS, + '--format=%b', + `${backupStash}^3` + ]) const untrackedDiff = output.trim() - if (untrackedDiff) await this.execGit([...gitApplyArgs], { input: `${untrackedDiff}\n` }) + if (untrackedDiff) await this.execGit([...GIT_APPLY_ARGS], { input: `${untrackedDiff}\n` }) } catch (error) { - if ( - !error.message || - !error.message.includes('unknown revision or path not in the working tree') - ) { - ctx.gitRestoreUntrackedError = true - handleError(error, ctx) - } + ctx.gitRestoreUntrackedError = true + handleError(error, ctx) } }