From 99390c31a856154e380f04d5c3603d2e6428f1e5 Mon Sep 17 00:00:00 2001 From: Saiichi Shayan Hashimoto Date: Mon, 6 Feb 2023 23:53:29 -0600 Subject: [PATCH] fix: allow re-enabling `--stash` when using the `--diff` option 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. --- README.md | 4 ++-- lib/runAll.js | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7693be396..9bc0433f9 100644 --- a/README.md +++ b/README.md @@ -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"`. diff --git a/lib/runAll.js b/lib/runAll.js index f0ff20342..f74774226 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -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 @@ -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)) }