diff --git a/README.md b/README.md index 7a0077286..aa07571c5 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Options: - **`--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`. - **`--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). - **`--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. -- **`--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` +- **`--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`. - **`--quiet`**: Supress all CLI output, except from tasks. - **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`. - **`--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"`. diff --git a/bin/lint-staged.js b/bin/lint-staged.js index cf74b5389..6a4a4e21a 100755 --- a/bin/lint-staged.js +++ b/bin/lint-staged.js @@ -42,9 +42,11 @@ cli.option('--cwd [path]', 'run all tasks in specific directory, instead of the 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. Implies "--no-stash".' +cli.addOption( + new Option( + '--diff [string]', + 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".' + ).implies({ stash: false }) ) cli.option( diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js new file mode 100644 index 000000000..d1e94afe3 --- /dev/null +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -0,0 +1,14 @@ +import { resolve } from 'node:path' + +import { execaCommand } from 'execa' + +let lintStagedBin = resolve(__dirname, '../../../bin/lint-staged.js') + +/** + * @param {string} cwd + * @return {Function} + */ +export const getLintStagedExecutor = + (cwd) => + async (params = '') => + await execaCommand(`${lintStagedBin} --cwd=${cwd} ${params}`) diff --git a/test/e2e/no-stash.test.js b/test/e2e/no-stash.test.js new file mode 100644 index 000000000..e454835f9 --- /dev/null +++ b/test/e2e/no-stash.test.js @@ -0,0 +1,50 @@ +import '../integration/__mocks__/resolveConfig.js' + +import { jest } from '@jest/globals' + +import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js' +import * as fileFixtures from '../integration/__fixtures__/files.js' +import * as configFixtures from '../integration/__fixtures__/configs.js' + +import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js' + +jest.setTimeout(20000) +jest.retryTimes(2) + +describe('lint-staged', () => { + test( + '--diff implies --no-stash', + withGitIntegration(async ({ execGit, writeFile, cwd }) => { + const lintStaged = getLintStagedExecutor(cwd) + + await execGit(['checkout', '-b', 'my-branch']) + await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierListDifferent)) + await writeFile('test.js', fileFixtures.prettyJS) + await execGit(['add', '.lintstagedrc.json']) + await execGit(['add', 'test.js']) + await execGit(['commit', '-m', 'test']) + + let res = await lintStaged() + expect(res.stdout).toMatch('No staged files found.') + + res = await lintStaged('--stash') + expect(res.stdout).toMatch('No staged files found.') + + res = await lintStaged('--no-stash') + expect(res.stdout).toMatch('No staged files found.') + expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.') + + res = await lintStaged('--diff=master...my-branch') + expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') + + try { + await lintStaged('--diff=master...my-branch --stash') + } catch (err) { + expect(err.stderr).toMatch('lint-staged failed due to a git error.') + } + + res = await lintStaged('--diff=master...my-branch --no-stash') + expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') + }) + ) +}) diff --git a/test/integration/__utils__/withGitIntegration.js b/test/integration/__utils__/withGitIntegration.js index 670940fc4..ff05e1a9f 100644 --- a/test/integration/__utils__/withGitIntegration.js +++ b/test/integration/__utils__/withGitIntegration.js @@ -79,7 +79,7 @@ export const withGitIntegration = const utils = getGitUtils(cwd) // Init repository with initial commit - await utils.execGit('init') + await utils.execGit(['init', '--initial-branch', 'master']) if (isWindowsActions()) { await utils.execGit(['config', 'core.autocrlf', 'input'])