Skip to content

Commit 406f0d1

Browse files
authored
feat(format): improve verbose commit message stripping (#105)
2 parents 23e7888 + 800b147 commit 406f0d1

File tree

3 files changed

+261
-56
lines changed

3 files changed

+261
-56
lines changed

conventional_pre_commit/format.py

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

6969

70-
def r_verbose_diff():
71-
"""Regex str for verbose diff"""
72-
return r"(?P<diff>(^# -* >8 -*$\r?\n)(^# .*$\r?\n)+(diff ){1}(.*\r?\n)*)"
70+
def r_verbose_commit_ignored():
71+
"""Regex str for the ignored part of verbose commit message templates"""
72+
return r"^# -{24} >8 -{24}\r?\n.*\Z"
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+
"""Strip the ignored part of verbose commit message templates."""
77+
return re.sub(r_verbose_commit_ignored(), "", input, flags=re.DOTALL | re.MULTILINE)
7778

7879

7980
def r_comment():
@@ -99,7 +100,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True, scopes: Opt
99100
100101
Optionally provide a list of additional custom types.
101102
"""
102-
input = strip_verbose_diff(input)
103+
input = strip_verbose_commit_ignored(input)
103104
input = strip_comments(input)
104105
types = conventional_types(types)
105106
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: 253 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -165,71 +165,275 @@ 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.DOTALL | 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.DOTALL | re.MULTILINE)
190+
input = (
191+
"""feat: some commit message
192+
# Please enter the commit message for your changes. Lines starting
193+
# with '#' will be ignored, and an empty message aborts the commit.
194+
#
195+
# On branch main
196+
# Your branch is up to date with 'origin/main'.
197+
#
198+
# Changes to be committed:
199+
# modified: README.md
200+
#
201+
# Changes not staged for commit:
202+
# modified: README.md
203+
#
204+
# ------------------------ >8 ------------------------
205+
# Do not modify or remove the line above.
206+
# Everything below it will be ignored.
207+
diff --git c/README.md i/README.md
208+
index ea80a93..fe8a527 100644
209+
--- c/README.md
210+
+++ i/README.md
211+
@@ -20,3 +20,4 @@ Some hunk header
212+
Context 1
184213
"""
185-
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
214+
+ " " # This is on purpose to preserve the space from overly eager stripping.
215+
+ """
216+
Context 2
217+
+Added line
193218
"""
219+
)
194220

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.
221+
assert regex.search(input)
222+
223+
224+
def test_r_verbose_commit_ignored__matches_double_verbose_ignored():
225+
regex = re.compile(format.r_verbose_commit_ignored(), re.DOTALL | re.MULTILINE)
226+
input = (
227+
"""feat: some commit message
228+
# Please enter the commit message for your changes. Lines starting
229+
# with '#' will be ignored, and an empty message aborts the commit.
230+
#
231+
# On branch main
232+
# Your branch is up to date with 'origin/main'.
233+
#
234+
# Changes to be committed:
235+
# modified: README.md
236+
#
237+
# Changes not staged for commit:
238+
# modified: README.md
239+
#
240+
# ------------------------ >8 ------------------------
241+
# Do not modify or remove the line above.
242+
# Everything below it will be ignored.
243+
#
244+
# Changes to be committed:
245+
diff --git c/README.md i/README.md
246+
index ea80a93..fe8a527 100644
247+
--- c/README.md
248+
+++ i/README.md
249+
@@ -20,3 +20,4 @@ Some staged hunk header
250+
Staged Context 1
251+
"""
252+
+ " " # This is on purpose to preserve the space from overly eager stripping.
253+
+ """
254+
Staged Context 2
255+
+Staged added line
256+
# --------------------------------------------------
257+
# Changes not staged for commit:
258+
diff --git i/README.md w/README.md
259+
index fe8a527..1c00c14 100644
260+
--- i/README.md
261+
+++ w/README.md
262+
@@ -10,6 +10,7 @@ Some unstaged hunk header
263+
Context 1
264+
Context 2
265+
Context 3
266+
-Removed line
267+
+Added line
268+
"""
269+
+ " " # This is on purpose to preserve the space from overly eager stripping.
270+
+ """
271+
Context 4
272+
"""
273+
+ " " # This is on purpose to preserve the space from overly eager stripping.
274+
+ """
275+
"""
276+
)
201277

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
278+
assert regex.search(input)
279+
280+
281+
def test_strip_verbose_commit_ignored__does_not_strip_no_verbose():
282+
input = """feat: some commit message
283+
# Please enter the commit message for your changes. Lines starting
284+
# with '#' will be ignored, and an empty message aborts the commit.
285+
#
286+
# On branch main
287+
# Your branch is up to date with 'origin/main'.
288+
#
289+
# Changes to be committed:
290+
# modified: README.md
291+
#
292+
# Changes not staged for commit:
293+
# modified: README.md
294+
#
207295
"""
208296

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.
297+
expected = """feat: some commit message
298+
# Please enter the commit message for your changes. Lines starting
299+
# with '#' will be ignored, and an empty message aborts the commit.
300+
#
301+
# On branch main
302+
# Your branch is up to date with 'origin/main'.
303+
#
304+
# Changes to be committed:
305+
# modified: README.md
306+
#
307+
# Changes not staged for commit:
308+
# modified: README.md
309+
#
310+
"""
215311

216-
# These are comments usually added by editors, f.ex. with export EDITOR=vim
312+
result = format.strip_verbose_commit_ignored(input)
313+
assert result == expected
314+
315+
316+
def test_strip_verbose_commit_ignored__strips_single_verbose_ignored():
317+
input = (
318+
"""feat: some commit message
319+
# Please enter the commit message for your changes. Lines starting
320+
# with '#' will be ignored, and an empty message aborts the commit.
321+
#
322+
# On branch main
323+
# Your branch is up to date with 'origin/main'.
324+
#
325+
# Changes to be committed:
326+
# modified: README.md
327+
#
328+
# Changes not staged for commit:
329+
# modified: README.md
330+
#
331+
# ------------------------ >8 ------------------------
332+
# Do not modify or remove the line above.
333+
# Everything below it will be ignored.
334+
diff --git c/README.md i/README.md
335+
index ea80a93..fe8a527 100644
336+
--- c/README.md
337+
+++ i/README.md
338+
@@ -20,3 +20,4 @@ Some hunk header
339+
Context 1
340+
"""
341+
+ " " # This is on purpose to preserve the space from overly eager stripping.
342+
+ """
343+
Context 2
344+
+Added line
217345
"""
218346
)
219347

348+
expected = """feat: some commit message
349+
# Please enter the commit message for your changes. Lines starting
350+
# with '#' will be ignored, and an empty message aborts the commit.
351+
#
352+
# On branch main
353+
# Your branch is up to date with 'origin/main'.
354+
#
355+
# Changes to be committed:
356+
# modified: README.md
357+
#
358+
# Changes not staged for commit:
359+
# modified: README.md
360+
#
361+
"""
220362

221-
def test_strip_verbose_diff__no_diff():
222-
input = """feat(scope): message
223-
# Please enter the commit message for your changes.
363+
result = format.strip_verbose_commit_ignored(input)
364+
assert result == expected
365+
366+
367+
def test_strip_verbose_commit_ignored__strips_double_verbose_ignored():
368+
input = (
369+
"""feat: some commit message
370+
# Please enter the commit message for your changes. Lines starting
371+
# with '#' will be ignored, and an empty message aborts the commit.
372+
#
373+
# On branch main
374+
# Your branch is up to date with 'origin/main'.
375+
#
376+
# Changes to be committed:
377+
# modified: README.md
378+
#
379+
# Changes not staged for commit:
380+
# modified: README.md
381+
#
382+
# ------------------------ >8 ------------------------
383+
# Do not modify or remove the line above.
384+
# Everything below it will be ignored.
385+
#
386+
# Changes to be committed:
387+
diff --git c/README.md i/README.md
388+
index ea80a93..fe8a527 100644
389+
--- c/README.md
390+
+++ i/README.md
391+
@@ -20,3 +20,4 @@ Some staged hunk header
392+
Staged Context 1
393+
"""
394+
+ " " # This is on purpose to preserve the space from overly eager stripping.
395+
+ """
396+
Staged Context 2
397+
+Staged added line
398+
# --------------------------------------------------
399+
# Changes not staged for commit:
400+
diff --git i/README.md w/README.md
401+
index fe8a527..1c00c14 100644
402+
--- i/README.md
403+
+++ w/README.md
404+
@@ -10,6 +10,7 @@ Some unstaged hunk header
405+
Context 1
406+
Context 2
407+
Context 3
408+
-Removed line
409+
+Added line
410+
"""
411+
+ " " # This is on purpose to preserve the space from overly eager stripping.
412+
+ """
413+
Context 4
414+
"""
415+
+ " " # This is on purpose to preserve the space from overly eager stripping.
416+
+ """
417+
"""
418+
)
224419

225-
# These are comments usually added by editors, f.ex. with export EDITOR=vim
226-
# ----------- >8 -----------
227-
# Some comment
228-
# Some comment
420+
expected = """feat: some commit message
421+
# Please enter the commit message for your changes. Lines starting
422+
# with '#' will be ignored, and an empty message aborts the commit.
423+
#
424+
# On branch main
425+
# Your branch is up to date with 'origin/main'.
426+
#
427+
# Changes to be committed:
428+
# modified: README.md
429+
#
430+
# Changes not staged for commit:
431+
# modified: README.md
432+
#
229433
"""
230434

231-
result = format.strip_verbose_diff(input)
232-
assert result == input
435+
result = format.strip_verbose_commit_ignored(input)
436+
assert result == expected
233437

234438

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

0 commit comments

Comments
 (0)