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 27, 2022
1 parent fb0ccf3 commit 24558c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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
12 changes: 11 additions & 1 deletion pre_commit/commands/run.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 24558c2

Please sign in to comment.