mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +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,
|
commit_msg_filename: Optional[str] = None,
|
||||||
checkout_type: Optional[str] = None,
|
checkout_type: Optional[str] = None,
|
||||||
is_squash_merge: Optional[str] = None,
|
is_squash_merge: Optional[str] = None,
|
||||||
|
rewrite_command: Optional[str] = None,
|
||||||
) -> argparse.Namespace:
|
) -> argparse.Namespace:
|
||||||
return argparse.Namespace(
|
return argparse.Namespace(
|
||||||
color=color,
|
color=color,
|
||||||
|
|
@ -92,6 +93,7 @@ def _ns(
|
||||||
all_files=all_files,
|
all_files=all_files,
|
||||||
checkout_type=checkout_type,
|
checkout_type=checkout_type,
|
||||||
is_squash_merge=is_squash_merge,
|
is_squash_merge=is_squash_merge,
|
||||||
|
rewrite_command=rewrite_command,
|
||||||
files=(),
|
files=(),
|
||||||
hook=None,
|
hook=None,
|
||||||
verbose=False,
|
verbose=False,
|
||||||
|
|
@ -166,6 +168,7 @@ _EXPECTED_ARG_LENGTH_BY_HOOK = {
|
||||||
'pre-commit': 0,
|
'pre-commit': 0,
|
||||||
'pre-merge-commit': 0,
|
'pre-merge-commit': 0,
|
||||||
'post-merge': 1,
|
'post-merge': 1,
|
||||||
|
'post-rewrite': 1,
|
||||||
'pre-push': 2,
|
'pre-push': 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -209,6 +212,8 @@ def _run_ns(
|
||||||
)
|
)
|
||||||
elif hook_type == 'post-merge':
|
elif hook_type == 'post-merge':
|
||||||
return _ns(hook_type, color, is_squash_merge=args[0])
|
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:
|
else:
|
||||||
raise AssertionError(f'unexpected hook type: {hook_type}')
|
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]:
|
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
|
||||||
# these hooks do not operate on files
|
# 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 ()
|
return ()
|
||||||
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
|
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
|
||||||
return (args.commit_msg_filename,)
|
return (args.commit_msg_filename,)
|
||||||
|
|
@ -386,6 +388,9 @@ def run(
|
||||||
if args.is_squash_merge:
|
if args.is_squash_merge:
|
||||||
environ['PRE_COMMIT_IS_SQUASH_MERGE'] = 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
|
# Set pre_commit flag
|
||||||
environ['PRE_COMMIT'] = '1'
|
environ['PRE_COMMIT'] = '1'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ VERSION = importlib_metadata.version('pre_commit')
|
||||||
STAGES = (
|
STAGES = (
|
||||||
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
|
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
|
||||||
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
|
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
|
||||||
|
'post-rewrite',
|
||||||
)
|
)
|
||||||
|
|
||||||
DEFAULT = 'default'
|
DEFAULT = 'default'
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None:
|
||||||
'-t', '--hook-type', choices=(
|
'-t', '--hook-type', choices=(
|
||||||
'pre-commit', 'pre-merge-commit', 'pre-push', 'prepare-commit-msg',
|
'pre-commit', 'pre-merge-commit', 'pre-push', 'prepare-commit-msg',
|
||||||
'commit-msg', 'post-commit', 'post-checkout', 'post-merge',
|
'commit-msg', 'post-commit', 'post-checkout', 'post-merge',
|
||||||
|
'post-rewrite',
|
||||||
),
|
),
|
||||||
action=AppendReplaceDefault,
|
action=AppendReplaceDefault,
|
||||||
default=['pre-commit'],
|
default=['pre-commit'],
|
||||||
|
|
@ -146,6 +147,13 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
|
||||||
'squash merge'
|
'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:
|
def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@ def run_opts(
|
||||||
commit_msg_filename='',
|
commit_msg_filename='',
|
||||||
checkout_type='',
|
checkout_type='',
|
||||||
is_squash_merge='',
|
is_squash_merge='',
|
||||||
|
rewrite_command='',
|
||||||
):
|
):
|
||||||
# These are mutually exclusive
|
# These are mutually exclusive
|
||||||
assert not (all_files and files)
|
assert not (all_files and files)
|
||||||
|
|
@ -92,6 +93,7 @@ def run_opts(
|
||||||
commit_msg_filename=commit_msg_filename,
|
commit_msg_filename=commit_msg_filename,
|
||||||
checkout_type=checkout_type,
|
checkout_type=checkout_type,
|
||||||
is_squash_merge=is_squash_merge,
|
is_squash_merge=is_squash_merge,
|
||||||
|
rewrite_command=rewrite_command,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,7 @@ def test_run_legacy_recursive(tmpdir):
|
||||||
('post-commit', []),
|
('post-commit', []),
|
||||||
('post-merge', ['1']),
|
('post-merge', ['1']),
|
||||||
('post-checkout', ['old_head', 'new_head', '1']),
|
('post-checkout', ['old_head', 'new_head', '1']),
|
||||||
|
('post-rewrite', ['amend']),
|
||||||
# multiple choices for commit-editmsg
|
# multiple choices for commit-editmsg
|
||||||
('prepare-commit-msg', ['.git/COMMIT_EDITMSG']),
|
('prepare-commit-msg', ['.git/COMMIT_EDITMSG']),
|
||||||
('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'message']),
|
('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'message']),
|
||||||
|
|
@ -166,6 +167,14 @@ def test_run_ns_post_merge():
|
||||||
assert ns.is_squash_merge == '1'
|
assert ns.is_squash_merge == '1'
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_ns_post_rewrite():
|
||||||
|
ns = hook_impl._run_ns('post-rewrite', True, ('amend',), b'')
|
||||||
|
assert ns is not None
|
||||||
|
assert ns.hook_stage == 'post-rewrite'
|
||||||
|
assert ns.color is True
|
||||||
|
assert ns.rewrite_command == 'amend'
|
||||||
|
|
||||||
|
|
||||||
def test_run_ns_post_checkout():
|
def test_run_ns_post_checkout():
|
||||||
ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'')
|
ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'')
|
||||||
assert ns is not None
|
assert ns is not None
|
||||||
|
|
|
||||||
|
|
@ -817,6 +817,35 @@ def test_post_merge_integration(tempdir_factory, store):
|
||||||
assert os.path.exists('post-merge.tmp')
|
assert os.path.exists('post-merge.tmp')
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_rewrite_integration(tempdir_factory, store):
|
||||||
|
path = git_dir(tempdir_factory)
|
||||||
|
config = [
|
||||||
|
{
|
||||||
|
'repo': 'local',
|
||||||
|
'hooks': [{
|
||||||
|
'id': 'post-rewrite',
|
||||||
|
'name': 'Post rewrite',
|
||||||
|
'entry': 'touch post-rewrite.tmp',
|
||||||
|
'language': 'system',
|
||||||
|
'always_run': True,
|
||||||
|
'verbose': True,
|
||||||
|
'stages': ['post-rewrite'],
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
write_config(path, config)
|
||||||
|
with cwd(path):
|
||||||
|
open('init', 'a').close()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
install(C.CONFIG_FILE, store, hook_types=['post-rewrite'])
|
||||||
|
git_commit()
|
||||||
|
|
||||||
|
assert not os.path.exists('post-rewrite.tmp')
|
||||||
|
|
||||||
|
git_commit('--amend', '-m', 'ammended message')
|
||||||
|
assert os.path.exists('post-rewrite.tmp')
|
||||||
|
|
||||||
|
|
||||||
def test_post_checkout_integration(tempdir_factory, store):
|
def test_post_checkout_integration(tempdir_factory, store):
|
||||||
path = git_dir(tempdir_factory)
|
path = git_dir(tempdir_factory)
|
||||||
config = [
|
config = [
|
||||||
|
|
|
||||||
|
|
@ -504,6 +504,15 @@ def test_is_squash_merge(cap_out, store, repo_with_passing_hook):
|
||||||
assert environ['PRE_COMMIT_IS_SQUASH_MERGE'] == '1'
|
assert environ['PRE_COMMIT_IS_SQUASH_MERGE'] == '1'
|
||||||
|
|
||||||
|
|
||||||
|
def test_rewrite_command(cap_out, store, repo_with_passing_hook):
|
||||||
|
args = run_opts(rewrite_command='amend')
|
||||||
|
environ: MutableMapping[str, str] = {}
|
||||||
|
ret, printed = _do_run(
|
||||||
|
cap_out, store, repo_with_passing_hook, args, environ,
|
||||||
|
)
|
||||||
|
assert environ['PRE_COMMIT_REWRITE_COMMAND'] == 'amend'
|
||||||
|
|
||||||
|
|
||||||
def test_checkout_type(cap_out, store, repo_with_passing_hook):
|
def test_checkout_type(cap_out, store, repo_with_passing_hook):
|
||||||
args = run_opts(from_ref='', to_ref='', checkout_type='1')
|
args = run_opts(from_ref='', to_ref='', checkout_type='1')
|
||||||
environ: MutableMapping[str, str] = {}
|
environ: MutableMapping[str, str] = {}
|
||||||
|
|
|
||||||
|
|
@ -1002,6 +1002,7 @@ def test_manifest_hooks(tempdir_factory, store):
|
||||||
stages=(
|
stages=(
|
||||||
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
|
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
|
||||||
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
|
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
|
||||||
|
'post-rewrite',
|
||||||
),
|
),
|
||||||
types=['file'],
|
types=['file'],
|
||||||
types_or=[],
|
types_or=[],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue