Skip to content

Lenient linting #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Testing pipeline

on:
push:
branches:
Expand Down Expand Up @@ -41,6 +42,7 @@ jobs:
- run: mutmut run --paths-to-mutate "./hooks/" --use-coverage --no-progress

combos:
if: github.ref == 'refs/heads/main'
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} / ${{ matrix.env }}
needs: main
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.mutmut-cache
.tox
dist
venv

### JetBrains ###
.idea/**
Expand Down
13 changes: 10 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ default_install_hook_types: [ commit-msg, pre-commit, prepare-commit-msg ]
default_stages: [ pre-commit ]
repos:
- repo: https://github.com/ShellMagick/shellmagick-commit-hooks
rev: v24.03
rev: v24.03.1
hooks:
- id: commiticketing
stages: [ prepare-commit-msg ]
args: [ -t= ]
- id: no-boms
- id: no-todos
args: [ -e=.pre-commit-hooks.yaml, -e=test_no_todos.py, -e=no_todos.py, -e=README.md ]
- id: lint-commit-message
stages: [ commit-msg ]
- repo: https://github.com/sirosen/texthooks
rev: 0.6.6
hooks:
- id: alphabetize-codeowners
- id: fix-smartquotes
- id: fix-spaces
- id: fix-ligatures
- id: forbid-bidi-controls
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down
39 changes: 11 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Add this to your `.pre-commit-config.yaml`:

```yaml
- repo: https://github.com/ShellMagick/commit-hooks
rev: v24.03 # Use the ref you want to point at
rev: v24.06 # Use the ref you want to point at
hooks:
- id: no-todos
- id: no-boms
# - id: ...
```

Expand All @@ -37,34 +37,13 @@ default_install_hook_types: [ commit-msg, pre-commit, prepare-commit-msg ]
default_stages: [ pre-commit ]
repos:
- repo: https://github.com/ShellMagick/commit-hooks
rev: v24.03
rev: v24.06
hooks:
- id: commiticketing
stages: [ prepare-commit-msg ]
- id: no-boms
- id: no-todos
- id: lint-commit-message
stages: [ commit-msg ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
args: [ --maxkb=128 ]
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
args: [ --assume-in-merge ]
- id: check-toml
- id: check-xml
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
- id: forbid-submodules
- id: mixed-line-ending
- id: no-commit-to-branch
args: [ --branch=develop, --branch=release, --branch=master ]
- id: pretty-format-json
- id: trailing-whitespace
...
```

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

This hook has two optional arguments:

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

#### Side effects

Expand Down
9 changes: 8 additions & 1 deletion hooks/lint_commit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def main(argv: Sequence[str] | None = None) -> int:
default='120',
help='Maximum length of lines in the body (default: 120)',
)
parser.add_argument(
'-e',
'--ending',
default='[.,;?!\\-]',
help='Regex, defining forbidden characters for ending the subject line'
' (default: [.,;?!\\-])',
)
args = parser.parse_args(argv)

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

Expand Down
30 changes: 28 additions & 2 deletions tests/test_lint_commit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,24 @@ def test_subject_line_relaxed_too_long(tmpdir):
assert lint_commit_message.main(('-sl', '73', str(f))) == 2


@pytest.mark.parametrize('commit_msg', ['Hello!', 'Bye?', 'Oy.'])
def test_subject_line_ends_with_punctuation(tmpdir, commit_msg):
@pytest.mark.parametrize(
'commit_msg',
['Hello!', 'Bye?', 'Oy.', 'Some (thing)', 'Hey /ho/'],
)
def test_subject_line_ends_with_punctuation_and_regex(tmpdir, commit_msg):
with unittest.mock.patch('builtins.print') as mocked_print:
f = tmpdir.join('pseudo_commit_msg.txt')
f.write_text(commit_msg, encoding='utf-8')
assert lint_commit_message.main(('-e', '\\W', str(f))) == 4
assert call('The subject line must not end with punctuation.') \
in mocked_print.mock_calls


@pytest.mark.parametrize(
'commit_msg',
['Hello!', 'Bye?', 'Oy.'],
)
def test_subject_line_ends_forbidden_default(tmpdir, commit_msg):
with unittest.mock.patch('builtins.print') as mocked_print:
f = tmpdir.join('pseudo_commit_msg.txt')
f.write_text(commit_msg, encoding='utf-8')
Expand All @@ -79,6 +95,16 @@ def test_subject_line_ends_with_punctuation(tmpdir, commit_msg):
in mocked_print.mock_calls


@pytest.mark.parametrize(
'commit_msg',
['Some (thing)', 'Hey /ho/'],
)
def test_subject_line_ends_enabled_default(tmpdir, commit_msg):
f = tmpdir.join('pseudo_commit_msg.txt')
f.write_text(commit_msg, encoding='utf-8')
assert lint_commit_message.main((str(f),)) == 0


def test_body_line_too_long(tmpdir):
with unittest.mock.patch('builtins.print') as mocked_print:
f = tmpdir.join('pseudo_commit_msg.txt')
Expand Down
Loading