mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1344 from pre-commit/post_checkout_no_files
Ensure files aren't passed to post-checkout hooks
This commit is contained in:
commit
92f433c3cf
2 changed files with 32 additions and 17 deletions
|
|
@ -215,10 +215,12 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
|
||||||
|
|
||||||
|
|
||||||
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
|
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
|
||||||
if args.from_ref and args.to_ref:
|
if args.hook_stage == 'post-checkout': # no files for post-checkout
|
||||||
return git.get_changed_files(args.from_ref, args.to_ref)
|
return ()
|
||||||
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
|
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
|
||||||
return (args.commit_msg_filename,)
|
return (args.commit_msg_filename,)
|
||||||
|
elif args.from_ref and args.to_ref:
|
||||||
|
return git.get_changed_files(args.from_ref, args.to_ref)
|
||||||
elif args.files:
|
elif args.files:
|
||||||
return args.files
|
return args.files
|
||||||
elif args.all_files:
|
elif args.all_files:
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import sys
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
|
from pre_commit import git
|
||||||
from pre_commit.commands import install_uninstall
|
from pre_commit.commands import install_uninstall
|
||||||
from pre_commit.commands.install_uninstall import CURRENT_HASH
|
from pre_commit.commands.install_uninstall import CURRENT_HASH
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
|
|
@ -728,27 +729,39 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
|
||||||
|
|
||||||
def test_post_checkout_integration(tempdir_factory, store):
|
def test_post_checkout_integration(tempdir_factory, store):
|
||||||
path = git_dir(tempdir_factory)
|
path = git_dir(tempdir_factory)
|
||||||
config = {
|
config = [
|
||||||
'repo': 'local',
|
{
|
||||||
'hooks': [{
|
'repo': 'local',
|
||||||
'id': 'post-checkout',
|
'hooks': [{
|
||||||
'name': 'Post checkout',
|
'id': 'post-checkout',
|
||||||
'entry': 'bash -c "echo ${PRE_COMMIT_ORIGIN}"',
|
'name': 'Post checkout',
|
||||||
'language': 'system',
|
'entry': 'bash -c "echo ${PRE_COMMIT_TO_REF}"',
|
||||||
'always_run': True,
|
'language': 'system',
|
||||||
'verbose': True,
|
'always_run': True,
|
||||||
'stages': ['post-checkout'],
|
'verbose': True,
|
||||||
}],
|
'stages': ['post-checkout'],
|
||||||
}
|
}],
|
||||||
|
},
|
||||||
|
{'repo': 'meta', 'hooks': [{'id': 'identity'}]},
|
||||||
|
]
|
||||||
write_config(path, config)
|
write_config(path, config)
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
cmd_output('git', 'add', '.')
|
cmd_output('git', 'add', '.')
|
||||||
git_commit()
|
git_commit()
|
||||||
|
|
||||||
|
# add a file only on `feature`, it should not be passed to hooks
|
||||||
|
cmd_output('git', 'checkout', '-b', 'feature')
|
||||||
|
open('some_file', 'a').close()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
git_commit()
|
||||||
|
cmd_output('git', 'checkout', 'master')
|
||||||
|
|
||||||
install(C.CONFIG_FILE, store, hook_types=['post-checkout'])
|
install(C.CONFIG_FILE, store, hook_types=['post-checkout'])
|
||||||
retc, _, stderr = cmd_output('git', 'checkout', '-b', 'feature')
|
retc, _, stderr = cmd_output('git', 'checkout', 'feature')
|
||||||
|
assert stderr is not None
|
||||||
assert retc == 0
|
assert retc == 0
|
||||||
_, head, _ = cmd_output('git', 'rev-parse', 'HEAD')
|
assert git.head_rev(path) in stderr
|
||||||
assert head in str(stderr)
|
assert 'some_file' not in stderr
|
||||||
|
|
||||||
|
|
||||||
def test_prepare_commit_msg_integration_failing(
|
def test_prepare_commit_msg_integration_failing(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue