GeezerVison is showing github.com — fix: do not drop backup stash when errors and `--fail-on-changes` was… · lint-staged/lint-staged@53bb27b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/gitWorkflow.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,7 +9,7 @@ import { getDiffCommand } from './getDiffCommand.js' | 9 | 9 | import { parseGitZOutput } from './parseGitZOutput.js' | 10 | 10 | import { | 11 | 11 | ApplyEmptyCommitError, | 12 | | - ExitCodeError, | | 12 | + FailOnChangesError, | 13 | 13 | GetBackupStashError, | 14 | 14 | GitError, | 15 | 15 | HideUnstagedChangesError, | @@ -317,7 +317,7 @@ export class GitWorkflow { | 317 | 317 | const diffSha256 = calculateSha256(diff) | 318 | 318 | debugLog('SHA-256 hash of changes after tasks is %S', this.diffSha256) | 319 | 319 | if (this.unstagedDiffSha256 !== diffSha256) { | 320 | | - ctx.errors.add(ExitCodeError) | | 320 | + ctx.errors.add(FailOnChangesError) | 321 | 321 | throw new Error('Tasks modified files and --fail-on-changes was used!') | 322 | 322 | } | 323 | 323 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/index.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,7 @@ import { cleanupSkipped } from './state.js' | 15 | 15 | import { | 16 | 16 | ApplyEmptyCommitError, | 17 | 17 | ConfigNotFoundError, | 18 | | - ExitCodeError, | | 18 | + FailOnChangesError, | 19 | 19 | GetBackupStashError, | 20 | 20 | GitError, | 21 | 21 | RestoreUnstagedChangesError, | @@ -155,7 +155,7 @@ const lintStaged = async ( | 155 | 155 | logger.error(NO_CONFIGURATION) | 156 | 156 | } else if (ctx.errors.has(ApplyEmptyCommitError)) { | 157 | 157 | logger.warn(PREVENTED_EMPTY_COMMIT) | 158 | | - } else if (ctx.errors.has(ExitCodeError)) { | | 158 | + } else if (ctx.errors.has(FailOnChangesError)) { | 159 | 159 | logger.warn(PREVENTED_TASK_MODIFICATIONS) | 160 | 160 | } else if (ctx.errors.has(RestoreUnstagedChangesError)) { | 161 | 161 | logger.warn(UNSTAGED_CHANGES_BACKUP_STASH_LOCATION) | |
Collapse file Copy file name to clipboardExpand all lines: lib/state.js +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import EventEmitter from 'events' | 2 | 2 |
| 3 | 3 | import { GIT_ERROR, TASK_ERROR } from './messages.js' | 4 | 4 | import { | 5 | | - ExitCodeError, | | 5 | + FailOnChangesError, | 6 | 6 | GitError, | 7 | 7 | RestoreOriginalStateError, | 8 | 8 | RestoreUnstagedChangesError, | @@ -18,6 +18,7 @@ export const getInitialState = ({ | 18 | 18 | backupHash: null, | 19 | 19 | errors: new Set([]), | 20 | 20 | events: new EventEmitter(), | | 21 | + shouldFailOnChanges: null, | 21 | 22 | hasFilesToHide: null, | 22 | 23 | output: [], | 23 | 24 | quiet, | @@ -62,24 +63,25 @@ export const restoreUnstagedChangesSkipped = (ctx) => { | 62 | 63 | export const restoreOriginalStateEnabled = (ctx) => | 63 | 64 | !!ctx.shouldRevert && | 64 | 65 | !!ctx.shouldBackup && | 65 | | - (ctx.errors.has(ExitCodeError) || | | 66 | + (ctx.errors.has(FailOnChangesError) || | 66 | 67 | ctx.errors.has(TaskError) || | 67 | 68 | ctx.errors.has(RestoreUnstagedChangesError)) | 68 | 69 |
| 69 | 70 | export const restoreOriginalStateSkipped = (ctx) => { | 70 | 71 | // Should be skipped in case of unknown git errors | 71 | | - if ( | 72 | | - ctx.errors.has(GitError) && | 73 | | - !ctx.errors.has(RestoreUnstagedChangesError) && | 74 | | - !ctx.errors.has(ExitCodeError) | 75 | | - ) { | | 72 | + if (ctx.errors.has(GitError) && !ctx.errors.has(RestoreUnstagedChangesError)) { | 76 | 73 | return GIT_ERROR | 77 | 74 | } | 78 | 75 | } | 79 | 76 |
| 80 | 77 | export const cleanupEnabled = (ctx) => ctx.shouldBackup | 81 | 78 |
| 82 | 79 | export const cleanupSkipped = (ctx) => { | | 80 | + // "--fail-on-changes" was used, so we shouldn't drop the backup stash | | 81 | + if (ctx.errors.has(FailOnChangesError) && !ctx.shouldRevert) { | | 82 | + return true | | 83 | + } | | 84 | + | 83 | 85 | // Should be skipped in case of unknown git errors | 84 | 86 | if (restoreOriginalStateSkipped(ctx)) { | 85 | 87 | return GIT_ERROR | |
Collapse file Copy file name to clipboardExpand all lines: lib/symbols.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,4 +26,4 @@ export const RestoreUnstagedChangesError = Symbol('RestoreUnstagedChangesError') | 26 | 26 |
| 27 | 27 | export const TaskError = Symbol('TaskError') | 28 | 28 |
| 29 | | -export const ExitCodeError = Symbol('ExitCodeError') | | 29 | +export const FailOnChangesError = Symbol('FailOnChangesError') |
Collapse file Copy file name to clipboardExpand all lines: test/integration/fail-on-changes.test.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,7 +30,7 @@ describe('lint-staged', () => { | 30 | 30 | ) | 31 | 31 |
| 32 | 32 | test( | 33 | | - 'should not fail --fail-on-changes is used but tasks do not modify files', | | 33 | + 'should not fail when --fail-on-changes is used but tasks do not modify files', | 34 | 34 | withGitIntegration(async ({ execGit, expect, gitCommit, readFile, writeFile }) => { | 35 | 35 | await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierWrite)) | 36 | 36 |
| @@ -66,7 +66,6 @@ describe('lint-staged', () => { | 66 | 66 | lintStaged: { | 67 | 67 | failOnChanges: true, | 68 | 68 | revert: true, | 69 | | - quiet: true, | 70 | 69 | }, | 71 | 70 | }) | 72 | 71 | ).rejects.toThrow('lint-staged failed because `--fail-on-changes` was used') | |
|