Skip to content

Commit 66a716d

Browse files
authoredJun 28, 2023
fix: the --diff option implies --no-stash
1 parent cf691aa commit 66a716d

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Options:
131131
- **`--diff`**: By default linters are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`.
132132
- **`--diff-filter`**: By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203).
133133
- **`--max-arg-length`**: long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
134-
- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash`
134+
- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash`.
135135
- **`--quiet`**: Supress all CLI output, except from tasks.
136136
- **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
137137
- **`--shell`**: By default linter commands will be parsed for speed and security. This has the side-effect that regular shell scripts might not work as expected. You can skip parsing of commands with this option. To use a specific shell, use a path like `--shell "/bin/bash"`.

‎bin/lint-staged.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ cli.option('--cwd [path]', 'run all tasks in specific directory, instead of the
4242

4343
cli.option('-d, --debug', 'print additional debug information', false)
4444

45-
cli.option(
46-
'--diff [string]',
47-
'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
45+
cli.addOption(
46+
new Option(
47+
'--diff [string]',
48+
'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
49+
).implies({ stash: false })
4850
)
4951

5052
cli.option(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { resolve } from 'node:path'
2+
3+
import { execaCommand } from 'execa'
4+
5+
let lintStagedBin = resolve(__dirname, '../../../bin/lint-staged.js')
6+
7+
/**
8+
* @param {string} cwd
9+
* @return {Function}
10+
*/
11+
export const getLintStagedExecutor =
12+
(cwd) =>
13+
async (params = '') =>
14+
await execaCommand(`${lintStagedBin} --cwd=${cwd} ${params}`)

‎test/e2e/no-stash.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import '../integration/__mocks__/resolveConfig.js'
2+
3+
import { jest } from '@jest/globals'
4+
5+
import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js'
6+
import * as fileFixtures from '../integration/__fixtures__/files.js'
7+
import * as configFixtures from '../integration/__fixtures__/configs.js'
8+
9+
import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js'
10+
11+
jest.setTimeout(20000)
12+
jest.retryTimes(2)
13+
14+
describe('lint-staged', () => {
15+
test(
16+
'--diff implies --no-stash',
17+
withGitIntegration(async ({ execGit, writeFile, cwd }) => {
18+
const lintStaged = getLintStagedExecutor(cwd)
19+
20+
await execGit(['checkout', '-b', 'my-branch'])
21+
await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierListDifferent))
22+
await writeFile('test.js', fileFixtures.prettyJS)
23+
await execGit(['add', '.lintstagedrc.json'])
24+
await execGit(['add', 'test.js'])
25+
await execGit(['commit', '-m', 'test'])
26+
27+
let res = await lintStaged()
28+
expect(res.stdout).toMatch('No staged files found.')
29+
30+
res = await lintStaged('--stash')
31+
expect(res.stdout).toMatch('No staged files found.')
32+
33+
res = await lintStaged('--no-stash')
34+
expect(res.stdout).toMatch('No staged files found.')
35+
expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.')
36+
37+
res = await lintStaged('--diff=master...my-branch')
38+
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')
39+
40+
try {
41+
await lintStaged('--diff=master...my-branch --stash')
42+
} catch (err) {
43+
expect(err.stderr).toMatch('lint-staged failed due to a git error.')
44+
}
45+
46+
res = await lintStaged('--diff=master...my-branch --no-stash')
47+
expect(res.stderr).toMatch('Skipping backup because `--diff` was used.')
48+
})
49+
)
50+
})

‎test/integration/__utils__/withGitIntegration.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const withGitIntegration =
7979
const utils = getGitUtils(cwd)
8080

8181
// Init repository with initial commit
82-
await utils.execGit('init')
82+
await utils.execGit(['init', '--initial-branch', 'master'])
8383

8484
if (isWindowsActions()) {
8585
await utils.execGit(['config', 'core.autocrlf', 'input'])

0 commit comments

Comments
 (0)
Please sign in to comment.