Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "--diff implies --no-stash" feature #1303

Merged
merged 16 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 })
iiroj marked this conversation as resolved.
Show resolved Hide resolved
)

cli.option(
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/__utils__/getLintStagedExecutor.js
iiroj marked this conversation as resolved.
Show resolved Hide resolved
@@ -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
iiroj marked this conversation as resolved.
Show resolved Hide resolved
@@ -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.')
})
)
})
3 changes: 3 additions & 0 deletions test/integration/__utils__/withGitIntegration.js
Expand Up @@ -81,6 +81,9 @@ export const withGitIntegration =
// Init repository with initial commit
await utils.execGit('init')
iiroj marked this conversation as resolved.
Show resolved Hide resolved

// Rename default main branch to master for tests to pass on local environment
await utils.execGit(['branch', '-m', 'master'])

iiroj marked this conversation as resolved.
Show resolved Hide resolved
if (isWindowsActions()) {
await utils.execGit(['config', 'core.autocrlf', 'input'])
}
Expand Down