From 47d0e05d09208db6d5c38bfaaff0c9f3f610951a Mon Sep 17 00:00:00 2001 From: Joshua Cannon <3956745+thejcannon@users.noreply.github.com> Date: Wed, 3 Jul 2024 21:08:52 -0500 Subject: [PATCH] use_filesnames_file --- pre_commit/clientlib.py | 1 + pre_commit/commands/run.py | 6 ++++++ pre_commit/hook.py | 1 + tests/commands/run_test.py | 25 +++++++++++++++++++++++++ tests/repository_test.py | 1 + 5 files changed, 34 insertions(+) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index a49465e8..e94e3663 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -128,6 +128,7 @@ MANIFEST_HOOK_DICT = cfgv.Map( cfgv.Optional('always_run', cfgv.check_bool, False), cfgv.Optional('fail_fast', cfgv.check_bool, False), cfgv.Optional('pass_filenames', cfgv.check_bool, True), + cfgv.Optional('use_filesnames_file', cfgv.check_bool, False), cfgv.Optional('description', cfgv.check_string, ''), cfgv.Optional('language_version', cfgv.check_string, C.DEFAULT), cfgv.Optional('log_file', cfgv.check_string, ''), diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 2a08dff0..8d99f3aa 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -7,6 +7,7 @@ import logging import os import re import subprocess +import tempfile import time import unicodedata from collections.abc import Generator @@ -187,6 +188,11 @@ def _run_single_hook( if not hook.pass_filenames: filenames = () + elif hook.use_filesnames_file: + filenames_file = tempfile.NamedTemporaryFile("w+") + filenames_file.write("\n".join(filenames)) + filenames = (f"@{filenames_file}",) + time_before = time.monotonic() language = languages[hook.language] with language.in_env(hook.prefix, hook.language_version): diff --git a/pre_commit/hook.py b/pre_commit/hook.py index 309cd5be..0ef86b72 100644 --- a/pre_commit/hook.py +++ b/pre_commit/hook.py @@ -28,6 +28,7 @@ class Hook(NamedTuple): always_run: bool fail_fast: bool pass_filenames: bool + use_filesnames_file: bool description: str language_version: str log_file: str diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 50a20f37..676d5fb5 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -1064,6 +1064,31 @@ def test_pass_filenames( assert (b'foo.py' in printed) == pass_filenames +@pytest.mark.parametrize( + ('use_filesnames_file', 'hook_args'), + ( + (True, []), + (False, []), + (True, ['some', 'args']), + (False, ['some', 'args']), + ), +) +def test_use_filesnames_file( + cap_out, store, repo_with_passing_hook, + use_filesnames_file, hook_args, +): + with modify_config() as config: + config['repos'][0]['hooks'][0]['use_filesnames_file'] = use_filesnames_file + config['repos'][0]['hooks'][0]['args'] = hook_args + stage_a_file() + ret, printed = _do_run( + cap_out, store, repo_with_passing_hook, run_opts(verbose=True), + ) + out_lines = printed.splitlines() + out_lines[-1] == b"Hello World" + assert out_lines[-2].startswith(b"@") == use_filesnames_file + + def test_fail_fast(cap_out, store, repo_with_failing_hook): with modify_config() as config: # More than one hook diff --git a/tests/repository_test.py b/tests/repository_test.py index ac065ec4..0033b84f 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -455,6 +455,7 @@ def test_manifest_hooks(tempdir_factory, store): minimum_pre_commit_version='0', name='Bash hook', pass_filenames=True, + use_filesnames_file=False, require_serial=False, stages=[ 'commit-msg',