Skip to content

Commit ae0cc1c

Browse files
Merge pull request #1967 from vkomarov-r7/feature/directory-hooks
Add the ability to restrict which directories isort works against
2 parents 06d8ef5 + 920e9ea commit ae0cc1c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Diff for: 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)