Skip to content

Commit d4da24d

Browse files
committed
fix: skip backup stash when using the --diff option
1 parent 1f06dd0 commit d4da24d

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ Options:
9292
-c, --config [path] path to configuration file, or - to read from stdin
9393
--cwd [path] run all tasks in specific directory, instead of the current
9494
-d, --debug print additional debug information (default: false)
95-
--diff [string] override the default "--staged" flag of "git diff" to get list of files
95+
--diff [string] override the default "--staged" flag of "git diff" to get list of files. Implies
96+
"--no-stash".
9697
--diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of files
9798
--max-arg-length [number] maximum length of the command-line argument string (default: 0)
9899
--no-stash disable the backup stash, and do not revert in case of errors

bin/lint-staged.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cli.option('-d, --debug', 'print additional debug information', false)
4444

4545
cli.option(
4646
'--diff [string]',
47-
'override the default "--staged" flag of "git diff" to get list of files'
47+
'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
4848
)
4949

5050
cli.option(

lib/index.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,25 @@ const lintStaged = async (
8686
debugLog('Unset GIT_LITERAL_PATHSPECS (was `%s`)', process.env.GIT_LITERAL_PATHSPECS)
8787
delete process.env.GIT_LITERAL_PATHSPECS
8888

89+
const options = {
90+
allowEmpty,
91+
concurrent,
92+
configObject,
93+
configPath,
94+
cwd,
95+
debug,
96+
diff,
97+
diffFilter,
98+
maxArgLength,
99+
quiet,
100+
relative,
101+
shell,
102+
stash,
103+
verbose,
104+
}
105+
89106
try {
90-
const ctx = await runAll(
91-
{
92-
allowEmpty,
93-
concurrent,
94-
configObject,
95-
configPath,
96-
cwd,
97-
debug,
98-
diff,
99-
diffFilter,
100-
maxArgLength,
101-
quiet,
102-
relative,
103-
shell,
104-
stash,
105-
verbose,
106-
},
107-
logger
108-
)
107+
const ctx = await runAll(options, logger)
109108
debugLog('Tasks were executed successfully!')
110109
printTaskOutput(ctx, logger)
111110
return true

lib/messages.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ export const NO_STAGED_FILES = `${info} No staged files found.`
2828

2929
export const NO_TASKS = `${info} No staged files match any configured task.`
3030

31-
export const skippingBackup = (hasInitialCommit) => {
32-
const reason = hasInitialCommit ? '`--no-stash` was used' : 'there’s no initial commit yet'
31+
export const skippingBackup = (hasInitialCommit, diff) => {
32+
const reason =
33+
diff !== undefined
34+
? '`--diff` was used'
35+
: hasInitialCommit
36+
? '`--no-stash` was used'
37+
: 'there’s no initial commit yet'
38+
3339
return yellow(`${warning} Skipping backup because ${reason}.\n`)
3440
}
3541

lib/runAll.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ export const runAll = async (
104104
.then(() => true)
105105
.catch(() => false)
106106

107-
// Lint-staged should create a backup stash only when there's an initial commit
108-
ctx.shouldBackup = hasInitialCommit && stash
107+
// Lint-staged should create a backup stash only when there's an initial commit,
108+
// and when using the default list of staged files
109+
ctx.shouldBackup = hasInitialCommit && stash && diff === undefined
109110
if (!ctx.shouldBackup) {
110-
logger.warn(skippingBackup(hasInitialCommit))
111+
logger.warn(skippingBackup(hasInitialCommit, diff))
111112
}
112113

113114
const files = await getStagedFiles({ cwd: gitDir, diff, diffFilter })

test/runAll.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('runAll', () => {
4545

4646
beforeAll(() => {
4747
console = makeConsoleMock()
48+
jest.clearAllMocks()
4849
})
4950

5051
afterEach(() => {
@@ -372,4 +373,14 @@ describe('runAll', () => {
372373
expect(ctx.errors.has(ConfigNotFoundError)).toBe(true)
373374
}
374375
})
376+
377+
it('should warn when --no-stash was used', async () => {
378+
await runAll({ configObject: { '*.js': ['echo "sample"'] }, stash: false })
379+
expect(console.printHistory()).toMatch('Skipping backup because `--no-stash` was used')
380+
})
381+
382+
it('should warn when --diff was used', async () => {
383+
await runAll({ configObject: { '*.js': ['echo "sample"'] }, diff: 'branch1...branch2' })
384+
expect(console.printHistory()).toMatch('Skipping backup because `--diff` was used')
385+
})
375386
})

0 commit comments

Comments
 (0)