diff options
author | Bendik Samseth <b.samseth@gmail.com> | 2019-02-19 16:03:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-19 16:03:50 +0100 |
commit | b3a9232e2c5bea2798c2b133aeb0773d22b0df0d (patch) | |
tree | a093385a14053d36e19829f404787d7eacd5b1ea | |
parent | 003bea4f6bf2cf13accd1900d7f887a664121410 (diff) | |
download | isort-b3a9232e2c5bea2798c2b133aeb0773d22b0df0d.tar.gz |
Add modify option to git hook
Redoing the sorting if it fails and the modify flag is set, but then allowing for modifications to be made to the sources.
-rw-r--r-- | isort/hooks.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/isort/hooks.py b/isort/hooks.py index 15b6d408..102c8973 100644 --- a/isort/hooks.py +++ b/isort/hooks.py @@ -48,13 +48,16 @@ def get_lines(command): return [line.strip().decode('utf-8') for line in stdout.splitlines()] -def git_hook(strict=False): +def git_hook(strict=False, modify=False): """ Git pre-commit hook to check staged files for isort errors :param bool strict - if True, return number of errors on exit, causing the hook to fail. If False, return zero so it will just act as a warning. + :param bool modify - if True, fix the sources if they are not + sorted properly. If False, only report result without + modifying anything. :return number of errors if in strict mode, 0 otherwise. """ @@ -78,5 +81,11 @@ def git_hook(strict=False): if sort.incorrectly_sorted: errors += 1 + if modify: + SortImports( + file_path=filename, + file_contents=staged_contents.decode(), + check=False + ) return errors if strict else 0 |