Skip to content

Commit

Permalink
Adds lazy option to git hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
sztamas committed Jul 25, 2020
1 parent 2d76984 commit 045c084
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,18 @@ include the following in `.git/hooks/pre-commit`:
import sys
from isort.hooks import git_hook

sys.exit(git_hook(strict=True, modify=True))
sys.exit(git_hook(strict=True, modify=True, lazy=True))
```

If you just want to display warnings, but allow the commit to happen
anyway, call `git_hook` without the strict parameter. If you want to
display warnings, but not also fix the code, call `git_hook` without the
modify parameter.
The `lazy` argument is to support users who are "lazy" to add files
individually to the index and tend to use `git commit -a` instead.
Set it to `True` to ensure all tracked files are properly isorted,
leave it out or set it to `False` to check only files added to your
index.

Setuptools integration
----------------------
Expand Down
8 changes: 7 additions & 1 deletion isort/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_lines(command: List[str]) -> List[str]:
return [line.strip() for line in stdout.splitlines()]


def git_hook(strict: bool = False, modify: bool = False) -> int:
def git_hook(strict: bool = False, modify: bool = False, lazy: bool = False) -> int:
"""
Git pre-commit hook to check staged files for isort errors
Expand All @@ -43,12 +43,18 @@ def git_hook(strict: bool = False, modify: bool = False) -> int:
:param bool modify - if True, fix the sources if they are not
sorted properly. If False, only report result without
modifying anything.
:param bool lazy - if True, also check/fix unstaged files.
This is useful if you frequently use ``git commit -a`` for example.
If False, ony check/fix the staged files for isort errors.
:return number of errors if in strict mode, 0 otherwise.
"""

# Get list of files modified and staged
diff_cmd = ["git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD"]
if lazy:
diff_cmd.remove("--cached")

files_modified = get_lines(diff_cmd)
if not files_modified:
return 0
Expand Down
22 changes: 19 additions & 3 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ def test_git_hook(src_dir):
# Ensure correct subprocess command is called
with patch("subprocess.run", MagicMock()) as run_mock:
hooks.git_hook()
assert run_mock.called_with(
["git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB HEAD"]
)
assert run_mock.called_once()
assert run_mock.call_args[0][0] == [
"git",
"diff-index",
"--cached",
"--name-only",
"--diff-filter=ACMRTUXB",
"HEAD",
]

hooks.git_hook(lazy=True)
assert run_mock.called_once()
assert run_mock.call_args[0][0] == [
"git",
"diff-index",
"--name-only",
"--diff-filter=ACMRTUXB",
"HEAD",
]

# Test with incorrectly sorted file returned from git
with patch(
Expand Down

0 comments on commit 045c084

Please sign in to comment.