Skip to content

Commit

Permalink
Add git-diff argument to directly staged changes
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Sep 3, 2022
1 parent 52a765e commit e8ef1dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
name: relint
description: 'Write your own linting rules using regular expressions.'
entry: relint
args: [--git-diff]
language: python
types: [file]
22 changes: 13 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,40 @@ expressions can match multiple lines and include newlines.
You can narrow down the file types your linter should be working with, by
providing the optional ``filePattern`` attribute. The default is ``.*``.

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

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

.. code-block:: bash
relint -c .relint.yml **
The default configuration file name is `.relint.yml` within your working
The default configuration file name is ``.relint.yml`` within your working
directory, but you can provide any YAML or JSON file.

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

.. code-block:: bash
git diff --unified=0 | relint my_file.py --diff
This option is useful for pre-commit purposes. Here an example of how to use it
pre-commit
^^^^^^^^^^

You can automate the linting process by adding a **pre-commit** hook to your
project. Add the following entry to your ``.pre-commit-config.yaml``:
with `pre-commit`_ framework:

.. code-block:: yaml
- repo: https://github.com/codingjoe/relint
rev: 1.2.0
rev: 1.4.0
hooks:
- id: relint
You can find an example of `relint-pre-commit.sh`_ in this repository.
args: [-W] # optional, if you want to fail on warnings during commit
Samples
-------
Expand All @@ -80,7 +84,7 @@ Samples
- name: the database is lava
pattern: '@pytest.fixture.*\n[ ]*def [^(]+\([^)]*(db|transactional_db)(, |\))'
hint: Please do not create db fixtures but model_bakery recipies instead.
hint: Please do not create db fixtures but model_bakery recipes instead.
filePattern: .*\.py
- name: No logger in management commands
Expand Down
4 changes: 0 additions & 4 deletions relint-pre-commit.sh

This file was deleted.

14 changes: 13 additions & 1 deletion relint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fnmatch
import glob
import re
import subprocess
import sys
import warnings
from collections import namedtuple
Expand Down Expand Up @@ -58,7 +59,12 @@ def parse_args(args):
'-d',
'--diff',
action='store_true',
help='Analyze content from git diff.'
help='Analyze content from a diff.'
)
parser.add_argument(
'--git-diff',
action='store_true',
help='Analyze content from git diff directly by calling `git diff --staged`.'
)
parser.add_argument(
'-W',
Expand Down Expand Up @@ -249,6 +255,12 @@ def main(args=sys.argv[1:]):

if args.diff:
output = sys.stdin.read()
elif args.git_diff:
output = subprocess.check_output(
['git', 'diff', '--staged', '--unified=0', '--no-color'],
universal_newlines=True,
)
if args.diff or args.git_diff:
changed_content = parse_diff(output)
matches = match_with_diff_changes(changed_content, matches)

Expand Down
13 changes: 13 additions & 0 deletions test_relint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import io
import os
import subprocess
import sys
import warnings

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

assert 'Error parsing your relint config file.' in str(exc_info.value)

def test_git_diff(self, capsys, tmpdir):
tmpdir.join('.relint.yml').write(open('.relint.yml').read())
tmpdir.join('dummy.py').write("# TODO do something")
subprocess.check_call(['git', 'init'], cwd=tmpdir.strpath) # nosec

with tmpdir.as_cwd():
with pytest.raises(SystemExit) as exc_info:
main(['relint.py', 'dummy.py', '--git-diff'])

assert '0' in str(exc_info.value)

0 comments on commit e8ef1dc

Please sign in to comment.