Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for verbose git messages #96

Merged
merged 1 commit into from
Mar 29, 2024
Merged
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
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
Loading