Skip to content

Commit

Permalink
fix: do not apply empty patch
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Oct 2, 2021
1 parent c99a6a1 commit a7c1c0b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/gitWorkflow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const debug = require('debug')('lint-staged:git')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

const execGit = require('./execGit')
const {
Expand All @@ -13,6 +15,8 @@ const {
} = require('./symbols')
const unlink = require('./unlink')

const fsReadFile = promisify(fs.readFile)

const MERGE_HEAD = 'MERGE_HEAD'
const MERGE_MODE = 'MERGE_MODE'
const MERGE_MSG = 'MERGE_MSG'
Expand Down Expand Up @@ -195,7 +199,10 @@ class GitWorkflow {
await this.execGit(['checkout', '--force', '--', '.'])

const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])
const hasContents = !!(await fsReadFile(unstagedPatch)).toString()
if (hasContents) {
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])
}

debug('Done restoring original state!')
} catch (error) {
Expand Down
13 changes: 13 additions & 0 deletions test/gitWorkflow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ describe('gitWorkflow', () => {
expect(await readFile('TEST.md')).toStrictEqual(origContent)
})
})

describe('restoreOriginalState', () => {
it('should handle errors', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git'),
})
const totallyRandom = `totally_random_file-${Date.now().toString()}`
gitWorkflow.partiallyStagedFiles = [totallyRandom]
const ctx = getInitialState()
await expect(gitWorkflow.restoreOriginalState(ctx)).rejects.toThrowError('ENOENT')
})
})
})
2 changes: 1 addition & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ describe('lint-staged', () => {
LOG [STARTED] Applying modifications...
ERROR [FAILED] Prevented an empty git commit!
LOG [STARTED] Reverting because of errors...
ERROR [FAILED] error: unrecognized input
LOG [SUCCESS] Reverting because of errors...
LOG [STARTED] Cleaning up...
LOG [SUCCESS] Cleaning up...
WARN
Expand Down

0 comments on commit a7c1c0b

Please sign in to comment.