Skip to content

Commit

Permalink
Add extra args to prepare-commit-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Whitaker committed May 28, 2022
1 parent fb0ccf3 commit 772c6b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pre_commit/commands/hook_impl.py
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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':
Expand Down
7 changes: 7 additions & 0 deletions pre_commit/commands/run.py
Expand Up @@ -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
Expand Down

0 comments on commit 772c6b0

Please sign in to comment.