Skip to content

Commit 26adf1d

Browse files
ModischFabricationsasottile
authored andcommitted
add support for post-commit
1 parent 3b3b33e commit 26adf1d

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed

pre_commit/commands/hook_impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def _pre_push_ns(
150150
_EXPECTED_ARG_LENGTH_BY_HOOK = {
151151
'commit-msg': 1,
152152
'post-checkout': 3,
153+
'post-commit': 0,
153154
'pre-commit': 0,
154155
'pre-merge-commit': 0,
155156
'pre-push': 2,
@@ -186,7 +187,7 @@ def _run_ns(
186187
return _pre_push_ns(color, args, stdin)
187188
elif hook_type in {'commit-msg', 'prepare-commit-msg'}:
188189
return _ns(hook_type, color, commit_msg_filename=args[0])
189-
elif hook_type in {'pre-merge-commit', 'pre-commit'}:
190+
elif hook_type in {'post-commit', 'pre-merge-commit', 'pre-commit'}:
190191
return _ns(hook_type, color)
191192
elif hook_type == 'post-checkout':
192193
return _ns(

pre_commit/commands/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
221221

222222

223223
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
224-
if args.hook_stage == 'post-checkout': # no files for post-checkout
224+
# these hooks do not operate on files
225+
if args.hook_stage in {'post-checkout', 'post-commit'}:
225226
return ()
226227
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
227228
return (args.commit_msg_filename,)

pre_commit/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
# `manual` is not invoked by any installed git hook. See #719
1919
STAGES = (
20-
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg', 'manual',
21-
'post-checkout', 'push',
20+
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
21+
'post-commit', 'manual', 'post-checkout', 'push',
2222
)
2323

2424
DEFAULT = 'default'

pre_commit/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None:
7979
parser.add_argument(
8080
'-t', '--hook-type', choices=(
8181
'pre-commit', 'pre-merge-commit', 'pre-push',
82-
'prepare-commit-msg', 'commit-msg', 'post-checkout',
82+
'prepare-commit-msg', 'commit-msg', 'post-commit', 'post-checkout',
8383
),
8484
action=AppendReplaceDefault,
8585
default=['pre-commit'],

tests/commands/hook_impl_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def call(*_, **__):
9696
('pre-merge-commit', []),
9797
('pre-push', ['branch_name', 'remote_name']),
9898
('commit-msg', ['.git/COMMIT_EDITMSG']),
99+
('post-commit', []),
99100
('post-checkout', ['old_head', 'new_head', '1']),
100101
# multiple choices for commit-editmsg
101102
('prepare-commit-msg', ['.git/COMMIT_EDITMSG']),
@@ -149,6 +150,13 @@ def test_run_ns_commit_msg():
149150
assert ns.commit_msg_filename == '.git/COMMIT_MSG'
150151

151152

153+
def test_run_ns_post_commit():
154+
ns = hook_impl._run_ns('post-commit', True, (), b'')
155+
assert ns is not None
156+
assert ns.hook_stage == 'post-commit'
157+
assert ns.color is True
158+
159+
152160
def test_run_ns_post_checkout():
153161
ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'')
154162
assert ns is not None

tests/commands/install_uninstall_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,32 @@ def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
726726
assert second_line.startswith('Must have "Signed off by:"...')
727727

728728

729+
def test_post_commit_integration(tempdir_factory, store):
730+
path = git_dir(tempdir_factory)
731+
config = [
732+
{
733+
'repo': 'local',
734+
'hooks': [{
735+
'id': 'post-commit',
736+
'name': 'Post commit',
737+
'entry': 'touch post-commit.tmp',
738+
'language': 'system',
739+
'always_run': True,
740+
'verbose': True,
741+
'stages': ['post-commit'],
742+
}],
743+
},
744+
]
745+
write_config(path, config)
746+
with cwd(path):
747+
_get_commit_output(tempdir_factory)
748+
assert not os.path.exists('post-commit.tmp')
749+
750+
install(C.CONFIG_FILE, store, hook_types=['post-commit'])
751+
_get_commit_output(tempdir_factory)
752+
assert os.path.exists('post-commit.tmp')
753+
754+
729755
def test_post_checkout_integration(tempdir_factory, store):
730756
path = git_dir(tempdir_factory)
731757
config = [

tests/repository_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def test_manifest_hooks(tempdir_factory, store):
880880
require_serial=False,
881881
stages=(
882882
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
883-
'manual', 'post-checkout', 'push',
883+
'post-commit', 'manual', 'post-checkout', 'push',
884884
),
885885
types=['file'],
886886
verbose=False,

0 commit comments

Comments
 (0)