From 772c6b08187a8b7f379ce778bc1ab0ffc4e35006 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 | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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..c5612dfd4 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -361,6 +361,13 @@ def run( ): return 0 + # Expose commit_msg / commit_sha as environment variables for the hooks + if args.commit_msg: + environ['PRE_COMMIT_COMMIT_MSG'] = args.commit_msg + + if args.commit_sha: + environ['PRE_COMMIT_COMMIT_SHA'] = args.commit_sha + # Expose from-ref / to-ref as environment variables for hooks to consume if args.from_ref and args.to_ref: # legacy names