Skip to content

Commit

Permalink
fix: restore original state when preventing an empty commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Oct 2, 2021
1 parent ba62b22 commit f7ef8ef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
36 changes: 18 additions & 18 deletions lib/gitWorkflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class GitWorkflow {
debug('Backing up original state...')

const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
await this.execGit(['diff', ...GIT_DIFF_ARGS, '--output', unstagedPatch, '--', '.'])
await this.execGit(['diff', ...GIT_DIFF_ARGS, '--output', unstagedPatch])

// Get a list of files with bot staged and unstaged changes.
// Unstaged changes to these files should be hidden before the tasks run.
Expand Down Expand Up @@ -162,23 +162,6 @@ class GitWorkflow {
}
}

/**
* Restore original HEAD state in case of errors
*/
async restoreOriginalState(ctx) {
try {
debug('Restoring original state...')
await this.execGit(['checkout', '--force', '--', '.'])

const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])

debug('Done restoring original state!')
} catch (error) {
handleError(error, ctx, RestoreOriginalStateError)
}
}

/** Add all task modifications to index for files that were staged before running. */
async applyModifications(ctx) {
debug('Adding task modifications to index...')
Expand All @@ -203,6 +186,23 @@ class GitWorkflow {
debug('Done adding task modifications to index!')
}

/**
* Restore original HEAD state in case of errors
*/
async restoreOriginalState(ctx) {
try {
debug('Restoring original state...')
await this.execGit(['checkout', '--force', '--', '.'])

const unstagedPatch = this.getHiddenFilepath(PATCH_UNSTAGED)
await this.execGit(['apply', ...GIT_APPLY_ARGS, unstagedPatch])

debug('Done restoring original state!')
} catch (error) {
handleError(error, ctx, RestoreOriginalStateError)
}
}

/**
* Restore unstaged changes to partially changed files. If it at first fails,
* this is probably because of conflicts between new task modifications.
Expand Down
10 changes: 9 additions & 1 deletion lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ const restorePartialChangesSkipped = (ctx) => {
}
}

const restoreOriginalStateEnabled = (ctx) => ctx.shouldBackup && ctx.errors.has(TaskError)
const restoreOriginalStateEnabled = (ctx) => {
if (ctx.shouldBackup && ctx.errors.has(TaskError)) {
return TASK_ERROR
}

if (ctx.errors.has(ApplyEmptyCommitError)) {
return true
}
}

const restoreOriginalStateSkipped = (ctx) => {
// Should be skipped in case of unknown git errors
Expand Down
22 changes: 16 additions & 6 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ describe('lint-staged', () => {
expect(await readFile('test2.js')).toEqual(testJsFilePretty)
})

it('Should warn when task contains deprecated `git add`', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
await execGit(['add', 'test.js'])

// Run lint-staged with `prettier --write` and commit pretty file
await gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })

expect(console.printHistory()).toMatch(
'Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.'
)
})

it('Should fail to commit entire staged file when errors from linter', async () => {
// Stage ugly file
await appendFile('test.js', testJsFileUgly)
Expand Down Expand Up @@ -664,27 +677,24 @@ describe('lint-staged', () => {

// Run lint-staged with prettier --write to automatically fix the file
// Since prettier reverts all changes, the commit should fail
// use the old syntax with manual `git add` to provide a warning message
await expect(
gitCommit({ config: { '*.js': ['prettier --write', 'git add'] } })
gitCommit({ config: { '*.js': ['prettier --write'] } })
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)

expect(console.printHistory()).toMatchInlineSnapshot(`
"
WARN ⚠ Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.
LOG [STARTED] Preparing...
LOG [SUCCESS] Preparing...
LOG [STARTED] Running tasks...
LOG [STARTED] Running tasks for *.js
LOG [STARTED] prettier --write
LOG [SUCCESS] prettier --write
LOG [STARTED] git add
LOG [SUCCESS] git add
LOG [SUCCESS] Running tasks for *.js
LOG [SUCCESS] Running tasks...
LOG [STARTED] Applying modifications...
ERROR [FAILED] Prevented an empty git commit!
LOG [STARTED] Reverting because of errors...
ERROR [FAILED] error: unrecognized input
LOG [STARTED] Cleaning up...
LOG [SUCCESS] Cleaning up...
WARN
Expand Down

0 comments on commit f7ef8ef

Please sign in to comment.