Skip to content

Commit c64c36b

Browse files
authored
Merge pull request #2036 from uSpike/add-post-rewrite-hook
Add: post-rewrite hook support
2 parents 3bab151 + 4cd8b36 commit c64c36b

File tree

9 files changed

+70
-1
lines changed

9 files changed

+70
-1
lines changed

pre_commit/commands/hook_impl.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def _ns(
7878
commit_msg_filename: Optional[str] = None,
7979
checkout_type: Optional[str] = None,
8080
is_squash_merge: Optional[str] = None,
81+
rewrite_command: Optional[str] = None,
8182
) -> argparse.Namespace:
8283
return argparse.Namespace(
8384
color=color,
@@ -92,6 +93,7 @@ def _ns(
9293
all_files=all_files,
9394
checkout_type=checkout_type,
9495
is_squash_merge=is_squash_merge,
96+
rewrite_command=rewrite_command,
9597
files=(),
9698
hook=None,
9799
verbose=False,
@@ -166,6 +168,7 @@ def _pre_push_ns(
166168
'pre-commit': 0,
167169
'pre-merge-commit': 0,
168170
'post-merge': 1,
171+
'post-rewrite': 1,
169172
'pre-push': 2,
170173
}
171174

@@ -209,6 +212,8 @@ def _run_ns(
209212
)
210213
elif hook_type == 'post-merge':
211214
return _ns(hook_type, color, is_squash_merge=args[0])
215+
elif hook_type == 'post-rewrite':
216+
return _ns(hook_type, color, rewrite_command=args[0])
212217
else:
213218
raise AssertionError(f'unexpected hook type: {hook_type}')
214219

pre_commit/commands/run.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
245245

246246
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
247247
# these hooks do not operate on files
248-
if args.hook_stage in {'post-checkout', 'post-commit', 'post-merge'}:
248+
if args.hook_stage in {
249+
'post-checkout', 'post-commit', 'post-merge', 'post-rewrite',
250+
}:
249251
return ()
250252
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
251253
return (args.commit_msg_filename,)
@@ -386,6 +388,9 @@ def run(
386388
if args.is_squash_merge:
387389
environ['PRE_COMMIT_IS_SQUASH_MERGE'] = args.is_squash_merge
388390

391+
if args.rewrite_command:
392+
environ['PRE_COMMIT_REWRITE_COMMAND'] = args.rewrite_command
393+
389394
# Set pre_commit flag
390395
environ['PRE_COMMIT'] = '1'
391396

pre_commit/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
STAGES = (
2020
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
2121
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
22+
'post-rewrite',
2223
)
2324

2425
DEFAULT = 'default'

pre_commit/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None:
6969
'-t', '--hook-type', choices=(
7070
'pre-commit', 'pre-merge-commit', 'pre-push', 'prepare-commit-msg',
7171
'commit-msg', 'post-commit', 'post-checkout', 'post-merge',
72+
'post-rewrite',
7273
),
7374
action=AppendReplaceDefault,
7475
default=['pre-commit'],
@@ -146,6 +147,13 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
146147
'squash merge'
147148
),
148149
)
150+
parser.add_argument(
151+
'--rewrite-command',
152+
help=(
153+
'During a post-rewrite hook, specifies the command that invoked '
154+
'the rewrite'
155+
),
156+
)
149157

150158

151159
def _adjust_args_and_chdir(args: argparse.Namespace) -> None:

testing/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def run_opts(
7272
commit_msg_filename='',
7373
checkout_type='',
7474
is_squash_merge='',
75+
rewrite_command='',
7576
):
7677
# These are mutually exclusive
7778
assert not (all_files and files)
@@ -92,6 +93,7 @@ def run_opts(
9293
commit_msg_filename=commit_msg_filename,
9394
checkout_type=checkout_type,
9495
is_squash_merge=is_squash_merge,
96+
rewrite_command=rewrite_command,
9597
)
9698

9799

tests/commands/hook_impl_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def call(*_, **__):
9999
('post-commit', []),
100100
('post-merge', ['1']),
101101
('post-checkout', ['old_head', 'new_head', '1']),
102+
('post-rewrite', ['amend']),
102103
# multiple choices for commit-editmsg
103104
('prepare-commit-msg', ['.git/COMMIT_EDITMSG']),
104105
('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'message']),
@@ -166,6 +167,14 @@ def test_run_ns_post_merge():
166167
assert ns.is_squash_merge == '1'
167168

168169

170+
def test_run_ns_post_rewrite():
171+
ns = hook_impl._run_ns('post-rewrite', True, ('amend',), b'')
172+
assert ns is not None
173+
assert ns.hook_stage == 'post-rewrite'
174+
assert ns.color is True
175+
assert ns.rewrite_command == 'amend'
176+
177+
169178
def test_run_ns_post_checkout():
170179
ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'')
171180
assert ns is not None

tests/commands/install_uninstall_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,35 @@ def test_post_merge_integration(tempdir_factory, store):
817817
assert os.path.exists('post-merge.tmp')
818818

819819

820+
def test_post_rewrite_integration(tempdir_factory, store):
821+
path = git_dir(tempdir_factory)
822+
config = [
823+
{
824+
'repo': 'local',
825+
'hooks': [{
826+
'id': 'post-rewrite',
827+
'name': 'Post rewrite',
828+
'entry': 'touch post-rewrite.tmp',
829+
'language': 'system',
830+
'always_run': True,
831+
'verbose': True,
832+
'stages': ['post-rewrite'],
833+
}],
834+
},
835+
]
836+
write_config(path, config)
837+
with cwd(path):
838+
open('init', 'a').close()
839+
cmd_output('git', 'add', '.')
840+
install(C.CONFIG_FILE, store, hook_types=['post-rewrite'])
841+
git_commit()
842+
843+
assert not os.path.exists('post-rewrite.tmp')
844+
845+
git_commit('--amend', '-m', 'ammended message')
846+
assert os.path.exists('post-rewrite.tmp')
847+
848+
820849
def test_post_checkout_integration(tempdir_factory, store):
821850
path = git_dir(tempdir_factory)
822851
config = [

tests/commands/run_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,15 @@ def test_is_squash_merge(cap_out, store, repo_with_passing_hook):
504504
assert environ['PRE_COMMIT_IS_SQUASH_MERGE'] == '1'
505505

506506

507+
def test_rewrite_command(cap_out, store, repo_with_passing_hook):
508+
args = run_opts(rewrite_command='amend')
509+
environ: MutableMapping[str, str] = {}
510+
ret, printed = _do_run(
511+
cap_out, store, repo_with_passing_hook, args, environ,
512+
)
513+
assert environ['PRE_COMMIT_REWRITE_COMMAND'] == 'amend'
514+
515+
507516
def test_checkout_type(cap_out, store, repo_with_passing_hook):
508517
args = run_opts(from_ref='', to_ref='', checkout_type='1')
509518
environ: MutableMapping[str, str] = {}

tests/repository_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ def test_manifest_hooks(tempdir_factory, store):
10021002
stages=(
10031003
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
10041004
'post-commit', 'manual', 'post-checkout', 'push', 'post-merge',
1005+
'post-rewrite',
10051006
),
10061007
types=['file'],
10071008
types_or=[],

0 commit comments

Comments
 (0)