From 24558c2f40583632dfa741079e9cd61fbaab1a54 Mon Sep 17 00:00:00 2001 From: Matt Whitaker Date: Fri, 27 May 2022 17:03:21 +0100 Subject: [PATCH] Add extra args to prepare-commit-msg --- pre_commit/commands/hook_impl.py | 18 +++++++++++++++++- pre_commit/commands/run.py | 12 +++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index f315c04de..71b884905 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -76,6 +76,8 @@ def _ns( remote_name: str | None = None, remote_url: str | None = None, commit_msg_filename: str | None = None, + commit_msg: str | None = None, + commit_sha: str | None = None, checkout_type: str | None = None, is_squash_merge: str | None = None, rewrite_command: str | None = None, @@ -90,6 +92,8 @@ def _ns( remote_name=remote_name, remote_url=remote_url, commit_msg_filename=commit_msg_filename, + commit_msg=commit_msg, + commit_sha=commit_sha, all_files=all_files, checkout_type=checkout_type, is_squash_merge=is_squash_merge, @@ -202,8 +206,20 @@ def _run_ns( _check_args_length(hook_type, args) if hook_type == 'pre-push': return _pre_push_ns(color, args, stdin) - elif hook_type in {'commit-msg', 'prepare-commit-msg'}: + elif hook_type in 'commit-msg' or hook_type == 'prepare-commit-msg' and \ + len(args) == 1: return _ns(hook_type, color, commit_msg_filename=args[0]) + elif hook_type == 'prepare-commit-msg' and len(args) > 1: + if (len(args) == 2): + return _ns( + hook_type, color, commit_msg_filename=args[0], + commit_msg=args[1], + ) + else: + return _ns( + hook_type, color, commit_msg_filename=args[0], + commit_msg=args[1], commit_sha=args[2], + ) elif hook_type in {'post-commit', 'pre-merge-commit', 'pre-commit'}: return _ns(hook_type, color) elif hook_type == 'post-checkout': diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 37f989b57..9109a7603 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -147,6 +147,7 @@ def _run_single_hook( diff_before: bytes, verbose: bool, use_color: bool, + extra_hook_args: tuple[str, ...] = (), ) -> tuple[bool, bytes]: filenames = classifier.filenames_for_hook(hook) @@ -189,7 +190,10 @@ def _run_single_hook( filenames = () time_before = time.time() language = languages[hook.language] - retcode, out = language.run_hook(hook, filenames, use_color) + retcode, out = language.run_hook( + hook, filenames + extra_hook_args, + use_color, + ) duration = round(time.time() - time_before, 2) or 0 diff_after = _get_diff() @@ -282,9 +286,15 @@ def _run_hooks( retval = 0 prior_diff = _get_diff() for hook in hooks: + extra_args: tuple[str, ...] = () + if args.commit_msg: + extra_args = (args.commit_msg,) + elif args.commit_msg and args.commit_sha: + extra_args = (args.commit_msg, args.commit_sha) current_retval, prior_diff = _run_single_hook( classifier, hook, skips, cols, prior_diff, verbose=args.verbose, use_color=args.color, + extra_hook_args=extra_args, ) retval |= current_retval if retval and (config['fail_fast'] or hook.fail_fast):