Skip to content

Commit 1b9773f

Browse files
committed
fix: Handle comments by stripping
1 parent 5f44f3a commit 1b9773f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-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_comment():
58+
"""Regex str for comment"""
59+
return r"^#.*\r?\n?"
60+
61+
62+
def strip_comments(input):
63+
return re.sub(r_comment(), "", input, flags=re.MULTILINE)
64+
65+
5766
def conventional_types(types=[]):
5867
"""Return a list of Conventional Commits types merged with the given types."""
5968
if set(types) & set(CONVENTIONAL_TYPES) == set():
@@ -68,6 +77,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
6877
6978
Optionally provide a list of additional custom types.
7079
"""
80+
input = strip_comments(input)
7181
types = conventional_types(types)
7282
pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}"
7383
regex = re.compile(pattern, re.MULTILINE)

tests/test_format.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ def test_conventional_types__custom():
122122
assert set(["custom", *format.CONVENTIONAL_TYPES]) == set(result)
123123

124124

125+
def test_r_comment_single():
126+
regex = re.compile(format.r_comment())
127+
assert regex.match("# Some comment")
128+
assert not regex.match("Some comment")
129+
assert not regex.match(" # Some comment")
130+
131+
132+
def test_strip_comments__consecutive():
133+
input = """feat(scope): message
134+
# Please enter the commit message for your changes.
135+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
136+
"""
137+
result = format.strip_comments(input)
138+
assert result.count("\n") == 1
139+
140+
141+
def test_strip_comments__spaced():
142+
input = """feat(scope): message
143+
# Please enter the commit message for your changes.
144+
145+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
146+
"""
147+
result = format.strip_comments(input)
148+
assert result.count("\n") == 2
149+
150+
125151
@pytest.mark.parametrize("type", format.DEFAULT_TYPES)
126152
def test_is_conventional__default_type(type):
127153
input = f"{type}: message"
@@ -198,6 +224,14 @@ def test_is_conventional__bad_body_multiline_paragraphs():
198224
assert not format.is_conventional(input)
199225

200226

227+
def test_is_conventional__comment():
228+
input = """feat(scope): message
229+
# Please enter the commit message for your changes.
230+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
231+
"""
232+
assert format.is_conventional(input)
233+
234+
201235
@pytest.mark.parametrize("char", ['"', "'", "`", "#", "&"])
202236
def test_is_conventional__body_special_char(char):
203237
input = f"feat: message with {char}"

0 commit comments

Comments
 (0)