Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: lint-staged/lint-staged
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9b4fff70cd5c428b12afe04a56f4dc81ea3f94ba
Choose a base ref
...
head repository: lint-staged/lint-staged
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 29be5d705e06b9f8105d285970e9f445175b9271
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Oct 2, 2021

  1. fix: do not apply empty patch

    iiroj committed Oct 2, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    29be5d7 View commit details
Showing with 22 additions and 2 deletions.
  1. +8 −1 lib/gitWorkflow.js
  2. +13 −0 test/gitWorkflow.spec.js
  3. +1 −1 test/integration.test.js
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 {
@@ -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'
@@ -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) {
13 changes: 13 additions & 0 deletions test/gitWorkflow.spec.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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