Skip to content

Commit f986b2f

Browse files
committed
feat(format): improve verbose commit message stripping
1 parent 23e7888 commit f986b2f

File tree

3 files changed

+236
-56
lines changed

3 files changed

+236
-56
lines changed

conventional_pre_commit/format.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ def r_autosquash_prefixes():
6767
return "|".join(AUTOSQUASH_PREFIXES)
6868

6969

70-
def r_verbose_diff():
70+
def r_verbose_commit_ignored():
7171
"""Regex str for verbose diff"""
72-
return r"(?P<diff>(^# -* >8 -*$\r?\n)(^# .*$\r?\n)+(diff ){1}(.*\r?\n)*)"
72+
return r"^# -{24} >8 -{24}\r?\n(?:.*\r?\n)*.*"
7373

7474

75-
def strip_verbose_diff(input):
76-
return re.sub(r_verbose_diff(), "", input, flags=re.MULTILINE)
75+
def strip_verbose_commit_ignored(input):
76+
return re.sub(r_verbose_commit_ignored(), "", input, flags=re.MULTILINE)
7777

7878

7979
def r_comment():
@@ -99,7 +99,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True, scopes: Opt
9999
100100
Optionally provide a list of additional custom types.
101101
"""
102-
input = strip_verbose_diff(input)
102+
input = strip_verbose_commit_ignored(input)
103103
input = strip_comments(input)
104104
types = conventional_types(types)
105105
pattern = f"^({r_types(types)}){r_scope(optional_scope, scopes=scopes)}{r_delim()}{r_subject()}{r_body()}"

tests/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ coverage run -m pytest
77
# clean out old coverage results
88
rm -rf ./tests/coverage
99

10-
# regenerate coverate report
10+
# regenerate coverage report
1111
coverage html --directory ./tests/coverage

tests/test_format.py

Lines changed: 230 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -165,71 +165,251 @@ def test_strip_comments__spaced():
165165
assert result.strip() == "feat(scope): message"
166166

167167

168-
def test_r_verbose_diff__has_diff():
169-
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
170-
input = """# ----------- >8 -----------
171-
# Some comment
172-
# Some comment
173-
diff --git a/file b/file
168+
def test_r_verbose_commit_ignored__does_not_match_no_verbose():
169+
regex = re.compile(format.r_verbose_commit_ignored(), re.MULTILINE)
170+
input = """feat: some commit message
171+
# Please enter the commit message for your changes. Lines starting
172+
# with '#' will be ignored, and an empty message aborts the commit.
173+
#
174+
# On branch main
175+
# Your branch is up to date with 'origin/main'.
176+
#
177+
# Changes to be committed:
178+
# modified: README.md
179+
#
180+
# Changes not staged for commit:
181+
# modified: README.md
182+
#
174183
"""
175184

176-
assert regex.match(input)
177-
178-
179-
def test_r_verbose_diff__no_diff():
180-
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
181-
input = """# ----------- >8 -----------
182-
# Some comment
183-
# Some comment
185+
assert not regex.search(input)
186+
187+
188+
def test_r_verbose_commit_ignored__matches_single_verbose_ignored():
189+
regex = re.compile(format.r_verbose_commit_ignored(), re.MULTILINE)
190+
input = """feat: some commit message
191+
# Please enter the commit message for your changes. Lines starting
192+
# with '#' will be ignored, and an empty message aborts the commit.
193+
#
194+
# On branch main
195+
# Your branch is up to date with 'origin/main'.
196+
#
197+
# Changes to be committed:
198+
# modified: README.md
199+
#
200+
# Changes not staged for commit:
201+
# modified: README.md
202+
#
203+
# ------------------------ >8 ------------------------
204+
# Do not modify or remove the line above.
205+
# Everything below it will be ignored.
206+
diff --git c/README.md i/README.md
207+
index ea80a93..fe8a527 100644
208+
--- c/README.md
209+
+++ i/README.md
210+
@@ -20,3 +20,4 @@ Some hunk header
211+
Context 1
212+
213+
Context 2
214+
+Added line
184215
"""
185216

186-
assert not regex.match(input)
187-
188-
189-
def test_r_verbose_diff__no_extra_comments():
190-
regex = re.compile(format.r_verbose_diff(), re.MULTILINE)
191-
input = """# ----------- >8 -----------
192-
diff --git a/file b/file
217+
assert regex.search(input)
218+
219+
220+
def test_r_verbose_commit_ignored__matches_double_verbose_ignored():
221+
regex = re.compile(format.r_verbose_commit_ignored(), re.MULTILINE)
222+
input = """feat: some commit message
223+
# Please enter the commit message for your changes. Lines starting
224+
# with '#' will be ignored, and an empty message aborts the commit.
225+
#
226+
# On branch main
227+
# Your branch is up to date with 'origin/main'.
228+
#
229+
# Changes to be committed:
230+
# modified: README.md
231+
#
232+
# Changes not staged for commit:
233+
# modified: README.md
234+
#
235+
# ------------------------ >8 ------------------------
236+
# Do not modify or remove the line above.
237+
# Everything below it will be ignored.
238+
#
239+
# Changes to be committed:
240+
diff --git c/README.md i/README.md
241+
index ea80a93..fe8a527 100644
242+
--- c/README.md
243+
+++ i/README.md
244+
@@ -20,3 +20,4 @@ Some staged hunk header
245+
Staged Context 1
246+
247+
Staged Context 2
248+
+Staged added line
249+
# --------------------------------------------------
250+
# Changes not staged for commit:
251+
diff --git i/README.md w/README.md
252+
index fe8a527..1c00c14 100644
253+
--- i/README.md
254+
+++ w/README.md
255+
@@ -10,6 +10,7 @@ Some unstaged hunk header
256+
Context 1
257+
Context 2
258+
Context 3
259+
-Removed line
260+
+Added line
261+
262+
Context 4
263+
193264
"""
194265

195-
assert not regex.match(input)
196-
197-
198-
def test_strip_verbose_diff__has_diff():
199-
input = """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-
# ----------- >8 -----------
204-
# Some comment
205-
# Some comment
206-
diff --git a/file b/file
266+
assert regex.search(input)
267+
268+
269+
def test_strip_verbose_commit_ignored__does_not_strip_no_verbose():
270+
input = """feat: some commit message
271+
# Please enter the commit message for your changes. Lines starting
272+
# with '#' will be ignored, and an empty message aborts the commit.
273+
#
274+
# On branch main
275+
# Your branch is up to date with 'origin/main'.
276+
#
277+
# Changes to be committed:
278+
# modified: README.md
279+
#
280+
# Changes not staged for commit:
281+
# modified: README.md
282+
#
207283
"""
208284

209-
result = format.strip_verbose_diff(input)
210-
assert result.count("\n") == 4
211-
assert (
212-
result
213-
== """feat(scope): message
214-
# Please enter the commit message for your changes.
285+
expected = """feat: some commit message
286+
# Please enter the commit message for your changes. Lines starting
287+
# with '#' will be ignored, and an empty message aborts the commit.
288+
#
289+
# On branch main
290+
# Your branch is up to date with 'origin/main'.
291+
#
292+
# Changes to be committed:
293+
# modified: README.md
294+
#
295+
# Changes not staged for commit:
296+
# modified: README.md
297+
#
298+
"""
215299

216-
# These are comments usually added by editors, f.ex. with export EDITOR=vim
300+
result = format.strip_verbose_commit_ignored(input)
301+
assert result == expected
302+
303+
304+
def test_strip_verbose_commit_ignored__strips_single_verbose_ignored():
305+
input = """feat: some commit message
306+
# Please enter the commit message for your changes. Lines starting
307+
# with '#' will be ignored, and an empty message aborts the commit.
308+
#
309+
# On branch main
310+
# Your branch is up to date with 'origin/main'.
311+
#
312+
# Changes to be committed:
313+
# modified: README.md
314+
#
315+
# Changes not staged for commit:
316+
# modified: README.md
317+
#
318+
# ------------------------ >8 ------------------------
319+
# Do not modify or remove the line above.
320+
# Everything below it will be ignored.
321+
diff --git c/README.md i/README.md
322+
index ea80a93..fe8a527 100644
323+
--- c/README.md
324+
+++ i/README.md
325+
@@ -20,3 +20,4 @@ Some hunk header
326+
Context 1
327+
328+
Context 2
329+
+Added line
217330
"""
218-
)
219331

332+
expected = """feat: some commit message
333+
# Please enter the commit message for your changes. Lines starting
334+
# with '#' will be ignored, and an empty message aborts the commit.
335+
#
336+
# On branch main
337+
# Your branch is up to date with 'origin/main'.
338+
#
339+
# Changes to be committed:
340+
# modified: README.md
341+
#
342+
# Changes not staged for commit:
343+
# modified: README.md
344+
#
345+
"""
220346

221-
def test_strip_verbose_diff__no_diff():
222-
input = """feat(scope): message
223-
# Please enter the commit message for your changes.
347+
result = format.strip_verbose_commit_ignored(input)
348+
assert result == expected
349+
350+
351+
def test_strip_verbose_commit_ignored__strips_double_verbose_ignored():
352+
input = """feat: some commit message
353+
# Please enter the commit message for your changes. Lines starting
354+
# with '#' will be ignored, and an empty message aborts the commit.
355+
#
356+
# On branch main
357+
# Your branch is up to date with 'origin/main'.
358+
#
359+
# Changes to be committed:
360+
# modified: README.md
361+
#
362+
# Changes not staged for commit:
363+
# modified: README.md
364+
#
365+
# ------------------------ >8 ------------------------
366+
# Do not modify or remove the line above.
367+
# Everything below it will be ignored.
368+
#
369+
# Changes to be committed:
370+
diff --git c/README.md i/README.md
371+
index ea80a93..fe8a527 100644
372+
--- c/README.md
373+
+++ i/README.md
374+
@@ -20,3 +20,4 @@ Some staged hunk header
375+
Staged Context 1
376+
377+
Staged Context 2
378+
+Staged added line
379+
# --------------------------------------------------
380+
# Changes not staged for commit:
381+
diff --git i/README.md w/README.md
382+
index fe8a527..1c00c14 100644
383+
--- i/README.md
384+
+++ w/README.md
385+
@@ -10,6 +10,7 @@ Some unstaged hunk header
386+
Context 1
387+
Context 2
388+
Context 3
389+
-Removed line
390+
+Added line
391+
392+
Context 4
393+
394+
"""
224395

225-
# These are comments usually added by editors, f.ex. with export EDITOR=vim
226-
# ----------- >8 -----------
227-
# Some comment
228-
# Some comment
396+
expected = """feat: some commit message
397+
# Please enter the commit message for your changes. Lines starting
398+
# with '#' will be ignored, and an empty message aborts the commit.
399+
#
400+
# On branch main
401+
# Your branch is up to date with 'origin/main'.
402+
#
403+
# Changes to be committed:
404+
# modified: README.md
405+
#
406+
# Changes not staged for commit:
407+
# modified: README.md
408+
#
229409
"""
230410

231-
result = format.strip_verbose_diff(input)
232-
assert result == input
411+
result = format.strip_verbose_commit_ignored(input)
412+
assert result == expected
233413

234414

235415
@pytest.mark.parametrize("type", format.DEFAULT_TYPES)

0 commit comments

Comments
 (0)