From b317fdbd334f958e26aba7397cc483ed92c7b29f Mon Sep 17 00:00:00 2001 From: Zlatoslav Desyatnikov Date: Sat, 13 Jul 2024 21:01:47 +0400 Subject: [PATCH 1/2] feat: option to skip merge commits format check --- README.md | 15 ++++++++------- conventional_pre_commit/format.py | 10 ++++++++++ conventional_pre_commit/hook.py | 10 ++++++++++ tests/conftest.py | 3 +++ tests/messages/merge_commit | 1 + tests/test_format.py | 2 ++ tests/test_hook.py | 9 +++++++++ 7 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tests/messages/merge_commit 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..2788100 100644 --- a/conventional_pre_commit/format.py +++ b/conventional_pre_commit/format.py @@ -125,3 +125,13 @@ 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..24fc8b2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,6 +43,9 @@ def conventional_gbk_commit_path(): 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(): diff --git a/tests/messages/merge_commit b/tests/messages/merge_commit new file mode 100644 index 0000000..6f038da --- /dev/null +++ b/tests/messages/merge_commit @@ -0,0 +1 @@ +Merge branch '2.x.x' into '1.x.x' \ No newline at end of file diff --git a/tests/test_format.py b/tests/test_format.py index 6940a21..152285d 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -124,6 +124,8 @@ def test_r_autosquash_prefixes(): for prefix in format.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..28e344e 100644 --- a/tests/test_hook.py +++ b/tests/test_hook.py @@ -81,6 +81,15 @@ 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]) From 726d9cff7a74328cf000026bae3646f4d55b2250 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:15:23 +0000 Subject: [PATCH 2/2] chore(pre-commit): autofix run --- conventional_pre_commit/format.py | 1 + tests/conftest.py | 2 ++ tests/messages/merge_commit | 2 +- tests/test_format.py | 2 ++ tests/test_hook.py | 3 +++ 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/conventional_pre_commit/format.py b/conventional_pre_commit/format.py index 2788100..4b4e7ce 100644 --- a/conventional_pre_commit/format.py +++ b/conventional_pre_commit/format.py @@ -126,6 +126,7 @@ def has_autosquash_prefix(input): return bool(regex.match(input)) + def is_merge_commit(input): """ Returns True if input starts with 'Merge branch '. diff --git a/tests/conftest.py b/tests/conftest.py index 24fc8b2..bab7488 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,10 +43,12 @@ def conventional_gbk_commit_path(): 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 index 6f038da..3cfe63b 100644 --- a/tests/messages/merge_commit +++ b/tests/messages/merge_commit @@ -1 +1 @@ -Merge branch '2.x.x' into '1.x.x' \ No newline at end of file +Merge branch '2.x.x' into '1.x.x' diff --git a/tests/test_format.py b/tests/test_format.py index 152285d..44a044f 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -124,9 +124,11 @@ def test_r_autosquash_prefixes(): for prefix in format.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 28e344e..bc75653 100644 --- a/tests/test_hook.py +++ b/tests/test_hook.py @@ -81,16 +81,19 @@ 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])