Skip to content

Commit 92f433c

Browse files
authored
Merge pull request pre-commit#1344 from pre-commit/post_checkout_no_files
Ensure files aren't passed to post-checkout hooks
2 parents 2d2ea15 + 53052fe commit 92f433c

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

pre_commit/commands/run.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,12 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
215215

216216

217217
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
218-
if args.from_ref and args.to_ref:
219-
return git.get_changed_files(args.from_ref, args.to_ref)
218+
if args.hook_stage == 'post-checkout': # no files for post-checkout
219+
return ()
220220
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
221221
return (args.commit_msg_filename,)
222+
elif args.from_ref and args.to_ref:
223+
return git.get_changed_files(args.from_ref, args.to_ref)
222224
elif args.files:
223225
return args.files
224226
elif args.all_files:

tests/commands/install_uninstall_test.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest import mock
55

66
import pre_commit.constants as C
7+
from pre_commit import git
78
from pre_commit.commands import install_uninstall
89
from pre_commit.commands.install_uninstall import CURRENT_HASH
910
from pre_commit.commands.install_uninstall import install
@@ -728,27 +729,39 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
728729

729730
def test_post_checkout_integration(tempdir_factory, store):
730731
path = git_dir(tempdir_factory)
731-
config = {
732-
'repo': 'local',
733-
'hooks': [{
734-
'id': 'post-checkout',
735-
'name': 'Post checkout',
736-
'entry': 'bash -c "echo ${PRE_COMMIT_ORIGIN}"',
737-
'language': 'system',
738-
'always_run': True,
739-
'verbose': True,
740-
'stages': ['post-checkout'],
741-
}],
742-
}
732+
config = [
733+
{
734+
'repo': 'local',
735+
'hooks': [{
736+
'id': 'post-checkout',
737+
'name': 'Post checkout',
738+
'entry': 'bash -c "echo ${PRE_COMMIT_TO_REF}"',
739+
'language': 'system',
740+
'always_run': True,
741+
'verbose': True,
742+
'stages': ['post-checkout'],
743+
}],
744+
},
745+
{'repo': 'meta', 'hooks': [{'id': 'identity'}]},
746+
]
743747
write_config(path, config)
744748
with cwd(path):
745749
cmd_output('git', 'add', '.')
746750
git_commit()
751+
752+
# add a file only on `feature`, it should not be passed to hooks
753+
cmd_output('git', 'checkout', '-b', 'feature')
754+
open('some_file', 'a').close()
755+
cmd_output('git', 'add', '.')
756+
git_commit()
757+
cmd_output('git', 'checkout', 'master')
758+
747759
install(C.CONFIG_FILE, store, hook_types=['post-checkout'])
748-
retc, _, stderr = cmd_output('git', 'checkout', '-b', 'feature')
760+
retc, _, stderr = cmd_output('git', 'checkout', 'feature')
761+
assert stderr is not None
749762
assert retc == 0
750-
_, head, _ = cmd_output('git', 'rev-parse', 'HEAD')
751-
assert head in str(stderr)
763+
assert git.head_rev(path) in stderr
764+
assert 'some_file' not in stderr
752765

753766

754767
def test_prepare_commit_msg_integration_failing(

0 commit comments

Comments
 (0)