mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 16:24:40 +04:00
Add: post-rewrite hook support
This commit is contained in:
parent
3bab1514c3
commit
4cd8b364dd
9 changed files with 70 additions and 1 deletions
|
|
@ -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 @@ _EXPECTED_ARG_LENGTH_BY_HOOK = {
|
|||
'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}')
|
||||
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ VERSION = importlib_metadata.version('pre_commit')
|
|||
STAGES = (
|
||||
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
|
||||
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
|
||||
'post-rewrite',
|
||||
)
|
||||
|
||||
DEFAULT = 'default'
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue