Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ usage: conventional-pre-commit [-h] [--force-scope] [--scopes SCOPES] [--strict]
Check a git commit message for Conventional Commits formatting.

positional arguments:
types Optional list of types to support
input A file containing a git commit message
types Optional list of types to support
input A file containing a git commit message

options:
-h, --help show this help message and exit
--force-scope Force commit to have scope defined.
--scopes SCOPES Optional list of scopes to support. Scopes should be separated by commas with no spaces (e.g. api,client)
--strict Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.
-h, --help show this help message and exit
--force-scope Force commit to have scope defined.
--scopes SCOPES Optional list of scopes to support. Scopes should be separated by commas with no spaces (e.g. api,client)
--strict Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.
--skip-merge-commits Do not check format for merge commits.
```

Supply arguments on the command-line, or via the pre-commit `hooks.args` property:
Expand All @@ -153,7 +154,7 @@ repos:
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: [--strict, --force-scope, feat, fix, chore, test, custom]
args: [--strict, --skip-merge-commits, --force-scope, feat, fix, chore, test, custom]
```

**NOTE:** when using as a pre-commit hook, `input` is supplied automatically (with the current commit's message).
Expand Down
11 changes: 11 additions & 0 deletions conventional_pre_commit/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ def has_autosquash_prefix(input):
regex = re.compile(pattern, re.DOTALL)

return bool(regex.match(input))


def is_merge_commit(input):
"""
Returns True if input starts with 'Merge branch '.

It doesn't check whether the rest of the input matches Conventional Commits
formatting.
"""

return input.startswith("Merge branch ")
10 changes: 10 additions & 0 deletions conventional_pre_commit/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def main(argv=[]):
action="store_true",
help="Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.",
)
parser.add_argument(
"--skip-merge-commits",
action="store_true",
dest="skip_merge_commits",
help="Do not check format for merge commits.",
)

if len(argv) < 1:
argv = sys.argv[1:]
Expand Down Expand Up @@ -62,6 +68,10 @@ def main(argv=[]):
else:
scopes = args.scopes

if args.skip_merge_commits:
if format.is_merge_commit(message):
return RESULT_SUCCESS

if not args.strict:
if format.has_autosquash_prefix(message):
return RESULT_SUCCESS
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def fixup_commit_path():
return get_message_path("fixup_commit")


@pytest.fixture
def merge_commit_path():
return get_message_path("merge_commit")


@pytest.fixture
def conventional_commit_bad_multi_line_path():
return get_message_path("conventional_commit_bad_multi_line")
Expand Down
1 change: 1 addition & 0 deletions tests/messages/merge_commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Merge branch '2.x.x' into '1.x.x'
4 changes: 4 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def test_r_autosquash_prefixes():
assert regex.match(prefix)


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


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

Expand Down
12 changes: 12 additions & 0 deletions tests/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def test_main_fail__fixup_commit(fixup_commit_path):
assert result == RESULT_FAIL


def test_main_fail__merge_commit(merge_commit_path):
result = main([merge_commit_path])

assert result == RESULT_FAIL


def test_main_success__merge_commit(merge_commit_path):
result = main(["--skip-merge-commits", merge_commit_path])

assert result == RESULT_SUCCESS


def test_main_success__conventional_commit_multi_line(conventional_commit_multi_line_path):
result = main([conventional_commit_multi_line_path])

Expand Down