From 3317ddfb3a2272792ee83e2750bcca3a90faa8ea Mon Sep 17 00:00:00 2001 From: Ezri Mudde Date: Mon, 25 Mar 2024 17:33:01 +0100 Subject: [PATCH] feat: Add support for verbose git messages --- conventional_pre_commit/format.py | 10 +++++ tests/test_format.py | 67 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/conventional_pre_commit/format.py b/conventional_pre_commit/format.py index aa25fdc..e79306f 100644 --- a/conventional_pre_commit/format.py +++ b/conventional_pre_commit/format.py @@ -54,6 +54,15 @@ def r_autosquash_prefixes(): return "|".join(AUTOSQUASH_PREFIXES) +def r_verbose_diff(): + """Regex str for verbose diff""" + return r"(?P(^# -* >8 -*$\r?\n)(^# .*$\r?\n)+(diff ){1}(.*\r?\n)*)" + + +def strip_verbose_diff(input): + return re.sub(r_verbose_diff(), "", input, flags=re.MULTILINE) + + def r_comment(): """Regex str for comment""" return r"^#.*\r?\n?" @@ -77,6 +86,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True): Optionally provide a list of additional custom types. """ + input = strip_verbose_diff(input) input = strip_comments(input) types = conventional_types(types) pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}" diff --git a/tests/test_format.py b/tests/test_format.py index 532b3a6..6b9507b 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -151,6 +151,73 @@ def test_strip_comments__spaced(): assert result.strip() == "feat(scope): message" +def test_r_verbose_diff__has_diff(): + regex = re.compile(format.r_verbose_diff(), re.MULTILINE) + input = """# ----------- >8 ----------- +# Some comment +# Some comment +diff --git a/file b/file +""" + + assert regex.match(input) + + +def test_r_verbose_diff__no_diff(): + regex = re.compile(format.r_verbose_diff(), re.MULTILINE) + input = """# ----------- >8 ----------- +# Some comment +# Some comment +""" + + assert not regex.match(input) + + +def test_r_verbose_diff__no_extra_comments(): + regex = re.compile(format.r_verbose_diff(), re.MULTILINE) + input = """# ----------- >8 ----------- +diff --git a/file b/file +""" + + assert not regex.match(input) + + +def test_strip_verbose_diff__has_diff(): + input = """feat(scope): message +# Please enter the commit message for your changes. + +# These are comments usually added by editors, f.ex. with export EDITOR=vim +# ----------- >8 ----------- +# Some comment +# Some comment +diff --git a/file b/file +""" + + result = format.strip_verbose_diff(input) + assert result.count("\n") == 4 + assert ( + result + == """feat(scope): message +# Please enter the commit message for your changes. + +# These are comments usually added by editors, f.ex. with export EDITOR=vim +""" + ) + + +def test_strip_verbose_diff__no_diff(): + input = """feat(scope): message +# Please enter the commit message for your changes. + +# These are comments usually added by editors, f.ex. with export EDITOR=vim +# ----------- >8 ----------- +# Some comment +# Some comment +""" + + result = format.strip_verbose_diff(input) + assert result == input + + @pytest.mark.parametrize("type", format.DEFAULT_TYPES) def test_is_conventional__default_type(type): input = f"{type}: message"