From d4da24d90cfa85ef8589a5f8c6ba5f51c3b45275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Mon, 30 May 2022 15:23:32 +0300 Subject: [PATCH] fix: skip backup stash when using the `--diff` option --- README.md | 3 ++- bin/lint-staged.js | 2 +- lib/index.js | 37 ++++++++++++++++++------------------- lib/messages.js | 10 ++++++++-- lib/runAll.js | 7 ++++--- test/runAll.spec.js | 11 +++++++++++ 6 files changed, 44 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index cdd994aba..42b6938e2 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ Options: -c, --config [path] path to configuration file, or - to read from stdin --cwd [path] run all tasks in specific directory, instead of the current -d, --debug print additional debug information (default: false) - --diff [string] override the default "--staged" flag of "git diff" to get list of files + --diff [string] override the default "--staged" flag of "git diff" to get list of files. Implies + "--no-stash". --diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of files --max-arg-length [number] maximum length of the command-line argument string (default: 0) --no-stash disable the backup stash, and do not revert in case of errors diff --git a/bin/lint-staged.js b/bin/lint-staged.js index 21018ddbe..115ce1a96 100755 --- a/bin/lint-staged.js +++ b/bin/lint-staged.js @@ -44,7 +44,7 @@ cli.option('-d, --debug', 'print additional debug information', false) cli.option( '--diff [string]', - 'override the default "--staged" flag of "git diff" to get list of files' + 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".' ) cli.option( diff --git a/lib/index.js b/lib/index.js index 636e3ae59..2e250fecf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -86,26 +86,25 @@ const lintStaged = async ( debugLog('Unset GIT_LITERAL_PATHSPECS (was `%s`)', process.env.GIT_LITERAL_PATHSPECS) delete process.env.GIT_LITERAL_PATHSPECS + const options = { + allowEmpty, + concurrent, + configObject, + configPath, + cwd, + debug, + diff, + diffFilter, + maxArgLength, + quiet, + relative, + shell, + stash, + verbose, + } + try { - const ctx = await runAll( - { - allowEmpty, - concurrent, - configObject, - configPath, - cwd, - debug, - diff, - diffFilter, - maxArgLength, - quiet, - relative, - shell, - stash, - verbose, - }, - logger - ) + const ctx = await runAll(options, logger) debugLog('Tasks were executed successfully!') printTaskOutput(ctx, logger) return true diff --git a/lib/messages.js b/lib/messages.js index 6083f77cf..2be1d5b87 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -28,8 +28,14 @@ export const NO_STAGED_FILES = `${info} No staged files found.` export const NO_TASKS = `${info} No staged files match any configured task.` -export const skippingBackup = (hasInitialCommit) => { - const reason = hasInitialCommit ? '`--no-stash` was used' : 'there’s no initial commit yet' +export const skippingBackup = (hasInitialCommit, diff) => { + const reason = + diff !== undefined + ? '`--diff` was used' + : hasInitialCommit + ? '`--no-stash` was used' + : 'there’s no initial commit yet' + return yellow(`${warning} Skipping backup because ${reason}.\n`) } diff --git a/lib/runAll.js b/lib/runAll.js index 7bbd91146..08f06d190 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -104,10 +104,11 @@ export const runAll = async ( .then(() => true) .catch(() => false) - // Lint-staged should create a backup stash only when there's an initial commit - ctx.shouldBackup = hasInitialCommit && stash + // Lint-staged should create a backup stash only when there's an initial commit, + // and when using the default list of staged files + ctx.shouldBackup = hasInitialCommit && stash && diff === undefined if (!ctx.shouldBackup) { - logger.warn(skippingBackup(hasInitialCommit)) + logger.warn(skippingBackup(hasInitialCommit, diff)) } const files = await getStagedFiles({ cwd: gitDir, diff, diffFilter }) diff --git a/test/runAll.spec.js b/test/runAll.spec.js index f890383aa..58a6cefe0 100644 --- a/test/runAll.spec.js +++ b/test/runAll.spec.js @@ -45,6 +45,7 @@ describe('runAll', () => { beforeAll(() => { console = makeConsoleMock() + jest.clearAllMocks() }) afterEach(() => { @@ -372,4 +373,14 @@ describe('runAll', () => { expect(ctx.errors.has(ConfigNotFoundError)).toBe(true) } }) + + it('should warn when --no-stash was used', async () => { + await runAll({ configObject: { '*.js': ['echo "sample"'] }, stash: false }) + expect(console.printHistory()).toMatch('Skipping backup because `--no-stash` was used') + }) + + it('should warn when --diff was used', async () => { + await runAll({ configObject: { '*.js': ['echo "sample"'] }, diff: 'branch1...branch2' }) + expect(console.printHistory()).toMatch('Skipping backup because `--diff` was used') + }) })