Skip to content

Commit e8ef1dc

Browse files
committed
Add git-diff argument to directly staged changes
1 parent 52a765e commit e8ef1dc

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

.pre-commit-hooks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
name: relint
33
description: 'Write your own linting rules using regular expressions.'
44
entry: relint
5+
args: [--git-diff]
56
language: python
67
types: [file]

README.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,40 @@ expressions can match multiple lines and include newlines.
3232
You can narrow down the file types your linter should be working with, by
3333
providing the optional ``filePattern`` attribute. The default is ``.*``.
3434

35-
The optional `error` attribute allows you to only show a warning but not exit
36-
with a bad (non-zero) exit code. The default is `true`.
35+
The optional ``error`` attribute allows you to only show a warning but not exit
36+
with a bad (non-zero) exit code. The default is ``true``.
3737

3838
The following command will lint all files in the current directory:
3939

4040
.. code-block:: bash
4141
4242
relint -c .relint.yml **
4343
44-
The default configuration file name is `.relint.yml` within your working
44+
The default configuration file name is ``.relint.yml`` within your working
4545
directory, but you can provide any YAML or JSON file.
4646

4747
If you prefer linting changed files (cached on git) you can use the option
48-
`--diff [-d]`:
48+
``--diff [-d]`` or ``--git-diff [-g]``:
4949

5050
.. code-block:: bash
5151
5252
git diff --unified=0 | relint my_file.py --diff
5353
54-
This option is useful for pre-commit purposes. Here an example of how to use it
54+
55+
pre-commit
56+
^^^^^^^^^^
57+
58+
You can automate the linting process by adding a **pre-commit** hook to your
59+
project. Add the following entry to your ``.pre-commit-config.yaml``:
5560
with `pre-commit`_ framework:
5661

5762
.. code-block:: yaml
5863
5964
- repo: https://github.com/codingjoe/relint
60-
rev: 1.2.0
65+
rev: 1.4.0
6166
hooks:
6267
- id: relint
63-
64-
You can find an example of `relint-pre-commit.sh`_ in this repository.
68+
args: [-W] # optional, if you want to fail on warnings during commit
6569
6670
Samples
6771
-------
@@ -80,7 +84,7 @@ Samples
8084
8185
- name: the database is lava
8286
pattern: '@pytest.fixture.*\n[ ]*def [^(]+\([^)]*(db|transactional_db)(, |\))'
83-
hint: Please do not create db fixtures but model_bakery recipies instead.
87+
hint: Please do not create db fixtures but model_bakery recipes instead.
8488
filePattern: .*\.py
8589
8690
- name: No logger in management commands

relint-pre-commit.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

relint.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fnmatch
33
import glob
44
import re
5+
import subprocess
56
import sys
67
import warnings
78
from collections import namedtuple
@@ -58,7 +59,12 @@ def parse_args(args):
5859
'-d',
5960
'--diff',
6061
action='store_true',
61-
help='Analyze content from git diff.'
62+
help='Analyze content from a diff.'
63+
)
64+
parser.add_argument(
65+
'--git-diff',
66+
action='store_true',
67+
help='Analyze content from git diff directly by calling `git diff --staged`.'
6268
)
6369
parser.add_argument(
6470
'-W',
@@ -249,6 +255,12 @@ def main(args=sys.argv[1:]):
249255

250256
if args.diff:
251257
output = sys.stdin.read()
258+
elif args.git_diff:
259+
output = subprocess.check_output(
260+
['git', 'diff', '--staged', '--unified=0', '--no-color'],
261+
universal_newlines=True,
262+
)
263+
if args.diff or args.git_diff:
252264
changed_content = parse_diff(output)
253265
matches = match_with_diff_changes(changed_content, matches)
254266

test_relint.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import io
2+
import os
3+
import subprocess
24
import sys
35
import warnings
46

@@ -196,3 +198,14 @@ def test_corrupt_config_file(self, tmpdir):
196198
main(['**'])
197199

198200
assert 'Error parsing your relint config file.' in str(exc_info.value)
201+
202+
def test_git_diff(self, capsys, tmpdir):
203+
tmpdir.join('.relint.yml').write(open('.relint.yml').read())
204+
tmpdir.join('dummy.py').write("# TODO do something")
205+
subprocess.check_call(['git', 'init'], cwd=tmpdir.strpath) # nosec
206+
207+
with tmpdir.as_cwd():
208+
with pytest.raises(SystemExit) as exc_info:
209+
main(['relint.py', 'dummy.py', '--git-diff'])
210+
211+
assert '0' in str(exc_info.value)

0 commit comments

Comments
 (0)