Skip to content

Commit e8cc8c0

Browse files
zlodesthekaveman
authored andcommitted
feat: option to skip merge commits format check
1 parent fab6a95 commit e8cc8c0

File tree

7 files changed

+42
-0
lines changed

7 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ options:
152152
--no-color Disable color in output.
153153
--force-scope Force commit to have scope defined.
154154
--scopes SCOPES Optional list of scopes to support. Scopes should be separated by commas with no spaces (e.g. api,client)
155+
--skip-merges Do not check format for merge commits.
155156
--strict Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.
156157
--verbose Print more verbose error output.
157158
```

conventional_pre_commit/format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,13 @@ def has_autosquash_prefix(input):
159159
regex = re.compile(pattern, re.DOTALL)
160160

161161
return bool(regex.match(input))
162+
163+
def is_merge_commit(input):
164+
"""
165+
Returns True if input starts with 'Merge branch '.
166+
167+
It doesn't check whether the rest of the input matches Conventional Commits
168+
formatting.
169+
"""
170+
171+
return input.startswith("Merge branch ")

conventional_pre_commit/hook.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def main(argv=[]):
2323
default=None,
2424
help="Optional list of scopes to support. Scopes should be separated by commas with no spaces (e.g. api,client)",
2525
)
26+
parser.add_argument(
27+
"--skip-merges",
28+
action="store_true",
29+
default=False,
30+
dest="skip_merges",
31+
help="Do not check format for merge commits.",
32+
)
2633
parser.add_argument(
2734
"--strict",
2835
action="store_true",
@@ -55,6 +62,10 @@ def main(argv=[]):
5562
else:
5663
scopes = args.scopes
5764

65+
if args.skip_merges:
66+
if format.is_merge_commit(commit_msg):
67+
return RESULT_SUCCESS
68+
5869
if not args.strict:
5970
if format.has_autosquash_prefix(commit_msg):
6071
return RESULT_SUCCESS

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def fixup_commit_path():
4444
return get_message_path("fixup_commit")
4545

4646

47+
@pytest.fixture
48+
def merge_commit_path():
49+
return get_message_path("merge_commit")
50+
51+
4752
@pytest.fixture
4853
def conventional_commit_bad_multi_line_path():
4954
return get_message_path("conventional_commit_bad_multi_line")

tests/messages/merge_commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Merge branch '2.x.x' into '1.x.x'

tests/test_format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def test_r_autosquash_prefixes():
124124
for prefix in format.AUTOSQUASH_PREFIXES:
125125
assert regex.match(prefix)
126126

127+
def test_merge_commit():
128+
assert format.is_merge_commit("Merge branch '2.x.x' into '1.x.x'")
127129

128130
def test_conventional_types__default():
129131
result = format.conventional_types()

tests/test_hook.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def test_main_fail__fixup_commit(fixup_commit_path):
8484
assert result == RESULT_FAIL
8585

8686

87+
def test_main_fail__merge_commit(merge_commit_path):
88+
result = main([merge_commit_path])
89+
90+
assert result == RESULT_FAIL
91+
92+
93+
def test_main_success__merge_commit(merge_commit_path):
94+
result = main(["--skip-merges", merge_commit_path])
95+
96+
assert result == RESULT_SUCCESS
97+
98+
8799
def test_main_success__conventional_commit_multi_line(conventional_commit_multi_line_path):
88100
result = main([conventional_commit_multi_line_path])
89101

0 commit comments

Comments
 (0)