Skip to content

Commit

Permalink
fix: allow re-enabling --stash when using the --diff option
Browse files Browse the repository at this point in the history
By default creating a backup stash is disabled when using `--diff`,
because the typical use-case is to run _lint-staged_ in a
CI environment with a clean working tree. It's now possible to
re-enable this behavior by using the `--stash` option.
  • Loading branch information
saiichihashimoto committed Feb 7, 2023
1 parent 421234c commit 99390c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -127,10 +127,10 @@ Options:
- **`--debug`**: Run in debug mode. When set, it does the following:
- uses [debug](https://github.com/visionmedia/debug) internally to log additional information about staged files, commands being executed, location of binaries, etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
- uses [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`; this causes serial, uncoloured output to the terminal, instead of the default (beautified, dynamic) output.
- **`--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).
- **`--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.
- **`--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
9 changes: 5 additions & 4 deletions lib/runAll.js
Expand Up @@ -77,7 +77,8 @@ export const runAll = async (
quiet = false,
relative = false,
shell = false,
stash = true,
// Stashing should be disabled by default when the `diff` option is used
stash = diff === undefined,
verbose = false,
},
logger = console
Expand All @@ -104,9 +105,9 @@ export const runAll = async (
.then(() => true)
.catch(() => false)

// 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
// Lint-staged will create a backup stash only when there's an initial commit,
// and when using the default list of staged files by default
ctx.shouldBackup = hasInitialCommit && stash
if (!ctx.shouldBackup) {
logger.warn(skippingBackup(hasInitialCommit, diff))
}
Expand Down

0 comments on commit 99390c3

Please sign in to comment.