Skip to content

Commit

Permalink
fix: the --diff option implies --no-stash
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-pap committed Jun 28, 2023
1 parent cf691aa commit 66a716d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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"`.
Expand Down
8 changes: 5 additions & 3 deletions bin/lint-staged.js
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions 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}`)
50 changes: 50 additions & 0 deletions 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.')
})
)
})
2 changes: 1 addition & 1 deletion test/integration/__utils__/withGitIntegration.js
Expand Up @@ -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'])
Expand Down

0 comments on commit 66a716d

Please sign in to comment.