Skip to content

Commit e774c09

Browse files
committed
Add pass_filenames hook option
This option controls whether filenames are passed along as arguments to the hook program.
1 parent 6025475 commit e774c09

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

pre_commit/clientlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def _make_argparser(filenames_help):
4646
),
4747
schema.Optional('args', schema.check_array(schema.check_string), []),
4848
schema.Optional('always_run', schema.check_bool, False),
49+
schema.Optional('pass_filenames', schema.check_bool, True),
4950
schema.Optional('description', schema.check_string, ''),
5051
schema.Optional(
5152
'exclude',

pre_commit/commands/run.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def _run_single_hook(hook, repo, args, skips, cols):
8686
sys.stdout.flush()
8787

8888
diff_before = cmd_output('git', 'diff', retcode=None, encoding=None)
89-
retcode, stdout, stderr = repo.run_hook(hook, tuple(filenames))
89+
retcode, stdout, stderr = repo.run_hook(
90+
hook,
91+
tuple(filenames) if hook['pass_filenames'] else (),
92+
)
9093
diff_after = cmd_output('git', 'diff', retcode=None, encoding=None)
9194

9295
file_modifications = diff_before != diff_after

tests/commands/run_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,29 @@ def test_files_running_subdir(
731731
tempdir_factory=tempdir_factory,
732732
)
733733
assert 'subdir/foo.py'.replace('/', os.sep) in stdout
734+
735+
736+
@pytest.mark.parametrize(
737+
('pass_filenames', 'hook_args', 'expected_out'),
738+
(
739+
(True, [], b'foo.py'),
740+
(False, [], b''),
741+
(True, ['some', 'args'], b'some args foo.py'),
742+
(False, ['some', 'args'], b'some args'),
743+
)
744+
)
745+
def test_pass_filenames(
746+
cap_out, repo_with_passing_hook, mock_out_store_directory,
747+
pass_filenames,
748+
hook_args,
749+
expected_out,
750+
):
751+
with modify_config() as config:
752+
config[0]['hooks'][0]['pass_filenames'] = pass_filenames
753+
config[0]['hooks'][0]['args'] = hook_args
754+
stage_a_file()
755+
ret, printed = _do_run(
756+
cap_out, repo_with_passing_hook, _get_opts(verbose=True),
757+
)
758+
assert expected_out + b'\nHello World' in printed
759+
assert ('foo.py' in printed) == pass_filenames

tests/manifest_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_manifest_contents(manifest):
3232
'log_file': '',
3333
'minimum_pre_commit_version': '0',
3434
'name': 'Bash hook',
35+
'pass_filenames': True,
3536
'stages': [],
3637
}]
3738

@@ -51,6 +52,7 @@ def test_hooks(manifest):
5152
'log_file': '',
5253
'minimum_pre_commit_version': '0',
5354
'name': 'Bash hook',
55+
'pass_filenames': True,
5456
'stages': [],
5557
}
5658

0 commit comments

Comments
 (0)