Skip to content

Commit c699e25

Browse files
committed
support pre-merge-commit
1 parent bf68512 commit c699e25

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

pre_commit/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
VERSION = importlib_metadata.version('pre_commit')
2727

2828
# `manual` is not invoked by any installed git hook. See #719
29-
STAGES = ('commit', 'prepare-commit-msg', 'commit-msg', 'manual', 'push')
29+
STAGES = (
30+
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg', 'manual',
31+
'push',
32+
)
3033

3134
DEFAULT = 'default'

pre_commit/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __call__(self, parser, namespace, values, option_string=None):
7070
def _add_hook_type_option(parser):
7171
parser.add_argument(
7272
'-t', '--hook-type', choices=(
73-
'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
73+
'pre-commit', 'pre-merge-commit', 'pre-push',
74+
'prepare-commit-msg', 'commit-msg',
7475
),
7576
action=AppendReplaceDefault,
7677
default=['pre-commit'],

pre_commit/resources/hook-tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def _opts(stdin):
163163
fns = {
164164
'prepare-commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
165165
'commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
166+
'pre-merge-commit': lambda _: (),
166167
'pre-commit': lambda _: (),
167168
'pre-push': _pre_push,
168169
}

testing/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ def cmd_output_mocked_pre_commit_home(*args, **kwargs):
3636
os.name == 'nt' or not docker_is_running(),
3737
reason="Docker isn't running or can't be accessed",
3838
)
39-
4039
skipif_cant_run_swift = pytest.mark.skipif(
4140
parse_shebang.find_executable('swift') is None,
42-
reason='swift isn\'t installed or can\'t be found',
41+
reason="swift isn't installed or can't be found",
4342
)
44-
4543
xfailif_windows_no_ruby = pytest.mark.xfail(
4644
os.name == 'nt',
4745
reason='Ruby support not yet implemented on windows.',
4846
)
47+
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')
4948

5049

5150
def broken_deep_listdir(): # pragma: no cover (platform specific)

tests/commands/install_uninstall_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from testing.util import cwd
3030
from testing.util import git_commit
3131
from testing.util import xfailif_no_symlink
32+
from testing.util import xfailif_windows
3233

3334

3435
def test_is_not_script():
@@ -742,6 +743,33 @@ def test_prepare_commit_msg_legacy(
742743
assert 'Signed off by: ' in f.read()
743744

744745

746+
@xfailif_windows # pragma: windows no cover (once AP has git 2.24)
747+
def test_pre_merge_commit_integration(tempdir_factory, store):
748+
expected = re.compile(
749+
r'^\[INFO\] Initializing environment for .+\n'
750+
r'Bash hook\.+Passed\n'
751+
r"Merge made by the 'recursive' strategy.\n"
752+
r' foo \| 0\n'
753+
r' 1 file changed, 0 insertions\(\+\), 0 deletions\(-\)\n'
754+
r' create mode 100644 foo\n$',
755+
)
756+
757+
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
758+
with cwd(path):
759+
ret = install(C.CONFIG_FILE, store, hook_types=['pre-merge-commit'])
760+
assert ret == 0
761+
762+
cmd_output('git', 'checkout', 'master', '-b', 'feature')
763+
_get_commit_output(tempdir_factory)
764+
cmd_output('git', 'checkout', 'master')
765+
ret, output, _ = cmd_output_mocked_pre_commit_home(
766+
'git', 'merge', '--no-ff', '--no-edit', 'feature',
767+
tempdir_factory=tempdir_factory,
768+
)
769+
assert ret == 0
770+
assert expected.match(output)
771+
772+
745773
def test_install_disallow_missing_config(tempdir_factory, store):
746774
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
747775
with cwd(path):

tests/repository_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,8 @@ def test_manifest_hooks(tempdir_factory, store):
895895
pass_filenames=True,
896896
require_serial=False,
897897
stages=(
898-
'commit', 'prepare-commit-msg', 'commit-msg', 'manual', 'push',
898+
'commit', 'merge-commit', 'prepare-commit-msg', 'commit-msg',
899+
'manual', 'push',
899900
),
900901
types=['file'],
901902
verbose=False,

0 commit comments

Comments
 (0)