From 4cd8b364dd871d38e6de891a1a19e52b030e51a9 Mon Sep 17 00:00:00 2001 From: Jordan Speicher Date: Wed, 1 Sep 2021 14:50:59 -0500 Subject: [PATCH] Add: post-rewrite hook support --- pre_commit/commands/hook_impl.py | 5 ++++ pre_commit/commands/run.py | 7 +++++- pre_commit/constants.py | 1 + pre_commit/main.py | 8 +++++++ testing/util.py | 2 ++ tests/commands/hook_impl_test.py | 9 ++++++++ tests/commands/install_uninstall_test.py | 29 ++++++++++++++++++++++++ tests/commands/run_test.py | 9 ++++++++ tests/repository_test.py | 1 + 9 files changed, 70 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index c544167c1..90bb33b8d 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -78,6 +78,7 @@ def _ns( commit_msg_filename: Optional[str] = None, checkout_type: Optional[str] = None, is_squash_merge: Optional[str] = None, + rewrite_command: Optional[str] = None, ) -> argparse.Namespace: return argparse.Namespace( color=color, @@ -92,6 +93,7 @@ def _ns( all_files=all_files, checkout_type=checkout_type, is_squash_merge=is_squash_merge, + rewrite_command=rewrite_command, files=(), hook=None, verbose=False, @@ -166,6 +168,7 @@ def _pre_push_ns( 'pre-commit': 0, 'pre-merge-commit': 0, 'post-merge': 1, + 'post-rewrite': 1, 'pre-push': 2, } @@ -209,6 +212,8 @@ def _run_ns( ) elif hook_type == 'post-merge': return _ns(hook_type, color, is_squash_merge=args[0]) + elif hook_type == 'post-rewrite': + return _ns(hook_type, color, rewrite_command=args[0]) else: raise AssertionError(f'unexpected hook type: {hook_type}') diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index d906d5b83..95ad5e964 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -245,7 +245,9 @@ def _compute_cols(hooks: Sequence[Hook]) -> int: def _all_filenames(args: argparse.Namespace) -> Collection[str]: # these hooks do not operate on files - if args.hook_stage in {'post-checkout', 'post-commit', 'post-merge'}: + if args.hook_stage in { + 'post-checkout', 'post-commit', 'post-merge', 'post-rewrite', + }: return () elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}: return (args.commit_msg_filename,) @@ -386,6 +388,9 @@ def run( if args.is_squash_merge: environ['PRE_COMMIT_IS_SQUASH_MERGE'] = args.is_squash_merge + if args.rewrite_command: + environ['PRE_COMMIT_REWRITE_COMMAND'] = args.rewrite_command + # Set pre_commit flag environ['PRE_COMMIT'] = '1' diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 3dcbbaca3..1a69c9041 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -19,6 +19,7 @@ STAGES = ( 'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'manual', 'post-checkout', 'push', 'post-merge', + 'post-rewrite', ) DEFAULT = 'default' diff --git a/pre_commit/main.py b/pre_commit/main.py index ad3d87370..2b50c91bb 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -69,6 +69,7 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None: '-t', '--hook-type', choices=( 'pre-commit', 'pre-merge-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'post-checkout', 'post-merge', + 'post-rewrite', ), action=AppendReplaceDefault, default=['pre-commit'], @@ -146,6 +147,13 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None: 'squash merge' ), ) + parser.add_argument( + '--rewrite-command', + help=( + 'During a post-rewrite hook, specifies the command that invoked ' + 'the rewrite' + ), + ) def _adjust_args_and_chdir(args: argparse.Namespace) -> None: diff --git a/testing/util.py b/testing/util.py index 12f22b59d..791a2b955 100644 --- a/testing/util.py +++ b/testing/util.py @@ -72,6 +72,7 @@ def run_opts( commit_msg_filename='', checkout_type='', is_squash_merge='', + rewrite_command='', ): # These are mutually exclusive assert not (all_files and files) @@ -92,6 +93,7 @@ def run_opts( commit_msg_filename=commit_msg_filename, checkout_type=checkout_type, is_squash_merge=is_squash_merge, + rewrite_command=rewrite_command, ) diff --git a/tests/commands/hook_impl_test.py b/tests/commands/hook_impl_test.py index c38b9caa1..37b78bc0d 100644 --- a/tests/commands/hook_impl_test.py +++ b/tests/commands/hook_impl_test.py @@ -99,6 +99,7 @@ def call(*_, **__): ('post-commit', []), ('post-merge', ['1']), ('post-checkout', ['old_head', 'new_head', '1']), + ('post-rewrite', ['amend']), # multiple choices for commit-editmsg ('prepare-commit-msg', ['.git/COMMIT_EDITMSG']), ('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'message']), @@ -166,6 +167,14 @@ def test_run_ns_post_merge(): assert ns.is_squash_merge == '1' +def test_run_ns_post_rewrite(): + ns = hook_impl._run_ns('post-rewrite', True, ('amend',), b'') + assert ns is not None + assert ns.hook_stage == 'post-rewrite' + assert ns.color is True + assert ns.rewrite_command == 'amend' + + def test_run_ns_post_checkout(): ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'') assert ns is not None diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 7076f8269..3c0712420 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -817,6 +817,35 @@ def test_post_merge_integration(tempdir_factory, store): assert os.path.exists('post-merge.tmp') +def test_post_rewrite_integration(tempdir_factory, store): + path = git_dir(tempdir_factory) + config = [ + { + 'repo': 'local', + 'hooks': [{ + 'id': 'post-rewrite', + 'name': 'Post rewrite', + 'entry': 'touch post-rewrite.tmp', + 'language': 'system', + 'always_run': True, + 'verbose': True, + 'stages': ['post-rewrite'], + }], + }, + ] + write_config(path, config) + with cwd(path): + open('init', 'a').close() + cmd_output('git', 'add', '.') + install(C.CONFIG_FILE, store, hook_types=['post-rewrite']) + git_commit() + + assert not os.path.exists('post-rewrite.tmp') + + git_commit('--amend', '-m', 'ammended message') + assert os.path.exists('post-rewrite.tmp') + + def test_post_checkout_integration(tempdir_factory, store): path = git_dir(tempdir_factory) config = [ diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index da7569ed0..8c153957b 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -504,6 +504,15 @@ def test_is_squash_merge(cap_out, store, repo_with_passing_hook): assert environ['PRE_COMMIT_IS_SQUASH_MERGE'] == '1' +def test_rewrite_command(cap_out, store, repo_with_passing_hook): + args = run_opts(rewrite_command='amend') + environ: MutableMapping[str, str] = {} + ret, printed = _do_run( + cap_out, store, repo_with_passing_hook, args, environ, + ) + assert environ['PRE_COMMIT_REWRITE_COMMAND'] == 'amend' + + def test_checkout_type(cap_out, store, repo_with_passing_hook): args = run_opts(from_ref='', to_ref='', checkout_type='1') environ: MutableMapping[str, str] = {} diff --git a/tests/repository_test.py b/tests/repository_test.py index e372519a5..4121fed43 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -1002,6 +1002,7 @@ def test_manifest_hooks(tempdir_factory, store): stages=( 'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'manual', 'post-checkout', 'push', 'post-merge', + 'post-rewrite', ), types=['file'], types_or=[],