Skip to content

Commit

Permalink
feat: Add support for verbose git messages (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Mar 29, 2024
2 parents a5fb76c + 3317ddf commit 772d7ff
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions conventional_pre_commit/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def r_autosquash_prefixes():
return "|".join(AUTOSQUASH_PREFIXES)


def r_verbose_diff():
"""Regex str for verbose diff"""
return r"(?P<diff>(^# -* >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?"
Expand All @@ -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()}"
Expand Down
67 changes: 67 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 772d7ff

Please sign in to comment.