Skip to content

Commit 920e9ea

Browse files
committed
Add the ability to restrict which directories isort works against
This is useful if certain parts of the code are `isort` compatible, while others might not be. This happened with us when resolving `merges`, which caused hooks to inadvertently sort imports of parts of the code that aren't (yet) compatible with `isort`, leading to unclean diffs/PRs.
1 parent 12cc5fb commit 920e9ea

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

isort/hooks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import subprocess # nosec - Needed for hook
88
from pathlib import Path
9-
from typing import List
9+
from typing import List, Optional
1010

1111
from isort import Config, api, exceptions
1212

@@ -32,7 +32,8 @@ def get_lines(command: List[str]) -> List[str]:
3232

3333

3434
def git_hook(
35-
strict: bool = False, modify: bool = False, lazy: bool = False, settings_file: str = ""
35+
strict: bool = False, modify: bool = False, lazy: bool = False, settings_file: str = "",
36+
directories: Optional[List[str]] = None,
3637
) -> int:
3738
"""Git pre-commit hook to check staged files for isort errors
3839
@@ -50,13 +51,16 @@ def git_hook(
5051
When settings_file is the empty string, the configuration file
5152
will be searched starting at the directory containing the first
5253
staged file, if any, and going upward in the directory structure.
54+
:param list[str] directories - A list of directories to restrict the hook to.
5355
5456
:return number of errors if in strict mode, 0 otherwise.
5557
"""
5658
# Get list of files modified and staged
5759
diff_cmd = ["git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD"]
5860
if lazy:
5961
diff_cmd.remove("--cached")
62+
if directories:
63+
diff_cmd.extend(directories)
6064

6165
files_modified = get_lines(diff_cmd)
6266
if not files_modified:

0 commit comments

Comments
 (0)