diff --git a/README.md b/README.md index 6e7f8c3..e73217f 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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). diff --git a/conventional_pre_commit/format.py b/conventional_pre_commit/format.py index 672cb34..4b4e7ce 100644 --- a/conventional_pre_commit/format.py +++ b/conventional_pre_commit/format.py @@ -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 ") diff --git a/conventional_pre_commit/hook.py b/conventional_pre_commit/hook.py index 25d6090..a8f2ece 100644 --- a/conventional_pre_commit/hook.py +++ b/conventional_pre_commit/hook.py @@ -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:] @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 83034ee..bab7488 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/messages/merge_commit b/tests/messages/merge_commit new file mode 100644 index 0000000..3cfe63b --- /dev/null +++ b/tests/messages/merge_commit @@ -0,0 +1 @@ +Merge branch '2.x.x' into '1.x.x' diff --git a/tests/test_format.py b/tests/test_format.py index 6940a21..44a044f 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -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() diff --git a/tests/test_hook.py b/tests/test_hook.py index f2d1ce6..bc75653 100644 --- a/tests/test_hook.py +++ b/tests/test_hook.py @@ -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])