From e03b5eb2180fb5355bb765121b0b917511dbb1e9 Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Thu, 17 Feb 2022 18:52:18 -0800 Subject: [PATCH] Add PRE_COMMIT_ALL_FILES environment variable Add a `PRE_COMMIT_ALL_FILES` environment variable to allow hooks to determine if pre-commit was called with `--all-files`. This can be useful if different behavior is desired when running pre-commit over all files vs only staged files. --- pre_commit/commands/run.py | 3 +++ tests/commands/run_test.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 37f989b5..004449ed 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -387,6 +387,9 @@ def run( if args.rewrite_command: environ['PRE_COMMIT_REWRITE_COMMAND'] = args.rewrite_command + + if args.all_files: + environ['PRE_COMMIT_ALL_FILES'] = '1' if args.all_files else '' # Set pre_commit flag environ['PRE_COMMIT'] = '1' diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 085b063f..1512b1a5 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -524,6 +524,15 @@ def test_checkout_type(cap_out, store, repo_with_passing_hook): assert environ['PRE_COMMIT_CHECKOUT_TYPE'] == '1' +def test_all_files(cap_out, store, repo_with_passing_hook): + args = run_opts(all_files=True) + environ: MutableMapping[str, str] = {} + ret, printed = _do_run( + cap_out, store, repo_with_passing_hook, args, environ, + ) + assert environ['PRE_COMMIT_ALL_FILES'] == '1' + + def test_has_unmerged_paths(in_merge_conflict): assert _has_unmerged_paths() is True cmd_output('git', 'add', '.')