Skip to content

Commit 772d7ff

Browse files
authored
feat: Add support for verbose git messages (#96)
2 parents a5fb76c + 3317ddf commit 772d7ff

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

conventional_pre_commit/format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def r_autosquash_prefixes():
5454
return "|".join(AUTOSQUASH_PREFIXES)
5555

5656

57+
def r_verbose_diff():
58+
"""Regex str for verbose diff"""
59+
return r"(?P<diff>(^# -* >8 -*$\r?\n)(^# .*$\r?\n)+(diff ){1}(.*\r?\n)*)"
60+
61+
62+
def strip_verbose_diff(input):
63+
return re.sub(r_verbose_diff(), "", input, flags=re.MULTILINE)
64+
65+
5766
def r_comment():
5867
"""Regex str for comment"""
5968
return r"^#.*\r?\n?"
@@ -77,6 +86,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
7786
7887
Optionally provide a list of additional custom types.
7988
"""
89+
input = strip_verbose_diff(input)
8090
input = strip_comments(input)
8191
types = conventional_types(types)
8292
pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}"

tests/test_format.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,73 @@ def test_strip_comments__spaced():
151151
assert result.strip() == "feat(scope): message"
152152

153153

154+
def test_r_verbose_diff__has_diff():
155+
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
156+
input = """# ----------- >8 -----------
157+
# Some comment
158+
# Some comment
159+
diff --git a/file b/file
160+
"""
161+
162+
assert regex.match(input)
163+
164+
165+
def test_r_verbose_diff__no_diff():
166+
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
167+
input = """# ----------- >8 -----------
168+
# Some comment
169+
# Some comment
170+
"""
171+
172+
assert not regex.match(input)
173+
174+
175+
def test_r_verbose_diff__no_extra_comments():
176+
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
177+
input = """# ----------- >8 -----------
178+
diff --git a/file b/file
179+
"""
180+
181+
assert not regex.match(input)
182+
183+
184+
def test_strip_verbose_diff__has_diff():
185+
input = """feat(scope): message
186+
# Please enter the commit message for your changes.
187+
188+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
189+
# ----------- >8 -----------
190+
# Some comment
191+
# Some comment
192+
diff --git a/file b/file
193+
"""
194+
195+
result = format.strip_verbose_diff(input)
196+
assert result.count("\n") == 4
197+
assert (
198+
result
199+
== """feat(scope): message
200+
# Please enter the commit message for your changes.
201+
202+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
203+
"""
204+
)
205+
206+
207+
def test_strip_verbose_diff__no_diff():
208+
input = """feat(scope): message
209+
# Please enter the commit message for your changes.
210+
211+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
212+
# ----------- >8 -----------
213+
# Some comment
214+
# Some comment
215+
"""
216+
217+
result = format.strip_verbose_diff(input)
218+
assert result == input
219+
220+
154221
@pytest.mark.parametrize("type", format.DEFAULT_TYPES)
155222
def test_is_conventional__default_type(type):
156223
input = f"{type}: message"

0 commit comments

Comments
 (0)