Skip to content

Commit

Permalink
fix: generate untracked diff using same arguments as unstaged
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 27, 2020
1 parent f5d7e7c commit ee92929
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/gitWorkflow.js
Expand Up @@ -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`
Expand Down Expand Up @@ -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
])
Expand Down Expand Up @@ -173,15 +170,15 @@ 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)
debug('Retrying with 3-way merge')

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)
Expand All @@ -200,17 +197,17 @@ class GitWorkflow {
// 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)
}
}

Expand Down

0 comments on commit ee92929

Please sign in to comment.