Skip to content

Commit

Permalink
fix: skip backup stash when using the --diff option
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed May 31, 2022
1 parent 1f06dd0 commit d4da24d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/lint-staged.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 18 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}

Expand Down
7 changes: 4 additions & 3 deletions lib/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
11 changes: 11 additions & 0 deletions test/runAll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('runAll', () => {

beforeAll(() => {
console = makeConsoleMock()
jest.clearAllMocks()
})

afterEach(() => {
Expand Down Expand Up @@ -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')
})
})

0 comments on commit d4da24d

Please sign in to comment.