| 1 |
# Git hook |
| 2 |
|
| 3 |
isort provides a hook function that can be integrated into your Git |
| 4 |
pre-commit script to check Python code before committing. |
| 5 |
|
| 6 |
To cause the commit to fail if there are isort errors (strict mode), |
| 7 |
include the following in `.git/hooks/pre-commit`: |
| 8 |
|
| 9 |
```python |
| 10 |
#!/usr/bin/env python |
| 11 |
import sys |
| 12 |
from isort.hooks import git_hook |
| 13 |
|
| 14 |
sys.exit(git_hook(strict=True, modify=True, lazy=True, settings_file="")) |
| 15 |
``` |
| 16 |
|
| 17 |
If you just want to display warnings, but allow the commit to happen |
| 18 |
anyway, call `git_hook` without the strict parameter. If you want to |
| 19 |
display warnings, but not also fix the code, call `git_hook` without the |
| 20 |
modify parameter. |
| 21 |
The `lazy` argument is to support users who are "lazy" to add files |
| 22 |
individually to the index and tend to use `git commit -a` instead. |
| 23 |
Set it to `True` to ensure all tracked files are properly isorted, |
| 24 |
leave it out or set it to `False` to check only files added to your |
| 25 |
index. |
| 26 |
|
| 27 |
If you want to use a specific configuration file for the hook, you can pass its |
| 28 |
path to settings_file. If no path is specifically requested, `git_hook` will |
| 29 |
search for the configuration file starting at the directory containing the first |
| 30 |
staged file, as per `git diff-index` ordering, and going upward in the directory |
| 31 |
structure until a valid configuration file is found or |
| 32 |
`MAX_CONFIG_SEARCH_DEPTH` directories are checked. Check the source code for the |
| 33 |
current value of this variable. |
| 34 |
The settings_file parameter is used to support users who keep their configuration |
| 35 |
file in a directory that might not be a parent of all the other files. |