Skip to content

Commit e5213a7

Browse files
committed
ISSUE-18: Enable lenient ending for commit messages (non-punctuation)
1 parent 319e1f7 commit e5213a7

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.mutmut-cache
77
.tox
88
dist
9+
venv
910

1011
### JetBrains ###
1112
.idea/**

.pre-commit-config.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ default_install_hook_types: [ commit-msg, pre-commit, prepare-commit-msg ]
22
default_stages: [ pre-commit ]
33
repos:
44
- repo: https://github.com/ShellMagick/shellmagick-commit-hooks
5-
rev: v24.03
5+
rev: v24.03.1
66
hooks:
77
- id: commiticketing
8-
stages: [ prepare-commit-msg ]
8+
args: [ -t= ]
99
- id: no-boms
1010
- id: no-todos
1111
args: [ -e=.pre-commit-hooks.yaml, -e=test_no_todos.py, -e=no_todos.py, -e=README.md ]
1212
- id: lint-commit-message
13-
stages: [ commit-msg ]
13+
- repo: https://github.com/sirosen/texthooks
14+
rev: 0.6.6
15+
hooks:
16+
- id: alphabetize-codeowners
17+
- id: fix-smartquotes
18+
- id: fix-spaces
19+
- id: fix-ligatures
20+
- id: forbid-bidi-controls
1421
- repo: https://github.com/pre-commit/pre-commit-hooks
1522
rev: v4.5.0
1623
hooks:

README.md

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Add this to your `.pre-commit-config.yaml`:
2222

2323
```yaml
2424
- repo: https://github.com/ShellMagick/commit-hooks
25-
rev: v24.03 # Use the ref you want to point at
25+
rev: v24.06 # Use the ref you want to point at
2626
hooks:
27-
- id: no-todos
27+
- id: no-boms
2828
# - id: ...
2929
```
3030

@@ -37,34 +37,13 @@ default_install_hook_types: [ commit-msg, pre-commit, prepare-commit-msg ]
3737
default_stages: [ pre-commit ]
3838
repos:
3939
- repo: https://github.com/ShellMagick/commit-hooks
40-
rev: v24.03
40+
rev: v24.06
4141
hooks:
4242
- id: commiticketing
43-
stages: [ prepare-commit-msg ]
4443
- id: no-boms
4544
- id: no-todos
4645
- id: lint-commit-message
47-
stages: [ commit-msg ]
48-
- repo: https://github.com/pre-commit/pre-commit-hooks
49-
rev: v4.5.0
50-
hooks:
51-
- id: check-added-large-files
52-
args: [ --maxkb=128 ]
53-
- id: check-case-conflict
54-
- id: check-json
55-
- id: check-merge-conflict
56-
args: [ --assume-in-merge ]
57-
- id: check-toml
58-
- id: check-xml
59-
- id: check-yaml
60-
- id: detect-private-key
61-
- id: end-of-file-fixer
62-
- id: forbid-submodules
63-
- id: mixed-line-ending
64-
- id: no-commit-to-branch
65-
args: [ --branch=develop, --branch=release, --branch=master ]
66-
- id: pretty-format-json
67-
- id: trailing-whitespace
46+
...
6847
```
6948

7049
## Available hooks
@@ -154,7 +133,8 @@ The linter checks the following (corresponding the rules from the linked article
154133
</ul>
155134
</li>
156135
<li>❌ Is not checked (would be "Capitilize the subject line"). Use <code>commiticketing</code> instead.</li>
157-
<li>✅ The subject line does not end with non-word character (i.e., non-alphanumeric character).
136+
<li>✅ The subject line does not end with a punctuation character (i.e., matching the regex
137+
<code>[.,;?!\\-]$</code>).
158138
<ul>
159139
<li>Note: This is stricter than the proposed rule "do not end the subject line with a period" by the
160140
article.</li>
@@ -175,8 +155,11 @@ The linter checks the following (corresponding the rules from the linked article
175155

176156
This hook has two optional arguments:
177157

178-
* `-sl/--subject-line-length:` adjust the parameter of rule 2 (default value: 72).
179-
* `-bl/--body-line-length:` adjust the parameter of rule 6 (default value: 120).
158+
* `-sl/--subject-line-length`: adjust the parameter of rule 2 (default value: `72`).
159+
* `-bl/--body-line-length`: adjust the parameter of rule 6 (default value: `120`).
160+
* `-e/--ending`: adjust the parameter of rule 4 (default: `[.,;?!\\-]`)
161+
* Example: In case you want to forbid any non-alphanumeric-character, you could use `\\W` as parameter (note that this
162+
forbids parentheses too).
180163

181164
#### Side effects
182165

hooks/lint_commit_message.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def main(argv: Sequence[str] | None = None) -> int:
2020
default='120',
2121
help='Maximum length of lines in the body (default: 120)',
2222
)
23+
parser.add_argument(
24+
'-e',
25+
'--ending',
26+
default='[.,;?!\\-]',
27+
help='Regex, defining forbidden characters for ending the subject line'
28+
' (default: [.,;?!\\-])',
29+
)
2330
args = parser.parse_args(argv)
2431

2532
with open(args.filename, encoding='utf-8') as msg:
@@ -56,7 +63,7 @@ def main(argv: Sequence[str] | None = None) -> int:
5663
# Rule 4: Do not end the subject line with a period
5764
# -> We restrict this more,
5865
# it should not end with any non-word character
59-
if match(r'^.*\W$', lines[0].rstrip()):
66+
if match(r'^.*%s$' % args.ending, lines[0].rstrip()):
6067
print('The subject line must not end with punctuation.')
6168
return 4
6269

tests/test_lint_commit_message.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,24 @@ def test_subject_line_relaxed_too_long(tmpdir):
6969
assert lint_commit_message.main(('-sl', '73', str(f))) == 2
7070

7171

72-
@pytest.mark.parametrize('commit_msg', ['Hello!', 'Bye?', 'Oy.'])
73-
def test_subject_line_ends_with_punctuation(tmpdir, commit_msg):
72+
@pytest.mark.parametrize(
73+
'commit_msg',
74+
['Hello!', 'Bye?', 'Oy.', 'Some (thing)', 'Hey /ho/'],
75+
)
76+
def test_subject_line_ends_with_punctuation_and_regex(tmpdir, commit_msg):
77+
with unittest.mock.patch('builtins.print') as mocked_print:
78+
f = tmpdir.join('pseudo_commit_msg.txt')
79+
f.write_text(commit_msg, encoding='utf-8')
80+
assert lint_commit_message.main(('-e', '\\W', str(f))) == 4
81+
assert call('The subject line must not end with punctuation.') \
82+
in mocked_print.mock_calls
83+
84+
85+
@pytest.mark.parametrize(
86+
'commit_msg',
87+
['Hello!', 'Bye?', 'Oy.'],
88+
)
89+
def test_subject_line_ends_forbidden_default(tmpdir, commit_msg):
7490
with unittest.mock.patch('builtins.print') as mocked_print:
7591
f = tmpdir.join('pseudo_commit_msg.txt')
7692
f.write_text(commit_msg, encoding='utf-8')
@@ -79,6 +95,16 @@ def test_subject_line_ends_with_punctuation(tmpdir, commit_msg):
7995
in mocked_print.mock_calls
8096

8197

98+
@pytest.mark.parametrize(
99+
'commit_msg',
100+
['Some (thing)', 'Hey /ho/'],
101+
)
102+
def test_subject_line_ends_enabled_default(tmpdir, commit_msg):
103+
f = tmpdir.join('pseudo_commit_msg.txt')
104+
f.write_text(commit_msg, encoding='utf-8')
105+
assert lint_commit_message.main((str(f),)) == 0
106+
107+
82108
def test_body_line_too_long(tmpdir):
83109
with unittest.mock.patch('builtins.print') as mocked_print:
84110
f = tmpdir.join('pseudo_commit_msg.txt')

0 commit comments

Comments
 (0)