6
6
import os
7
7
import subprocess # nosec - Needed for hook
8
8
from pathlib import Path
9
- from typing import List
9
+ from typing import List , Optional
10
10
11
11
from isort import Config , api , exceptions
12
12
@@ -32,7 +32,8 @@ def get_lines(command: List[str]) -> List[str]:
32
32
33
33
34
34
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 ,
36
37
) -> int :
37
38
"""Git pre-commit hook to check staged files for isort errors
38
39
@@ -50,13 +51,16 @@ def git_hook(
50
51
When settings_file is the empty string, the configuration file
51
52
will be searched starting at the directory containing the first
52
53
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.
53
55
54
56
:return number of errors if in strict mode, 0 otherwise.
55
57
"""
56
58
# Get list of files modified and staged
57
59
diff_cmd = ["git" , "diff-index" , "--cached" , "--name-only" , "--diff-filter=ACMRTUXB" , "HEAD" ]
58
60
if lazy :
59
61
diff_cmd .remove ("--cached" )
62
+ if directories :
63
+ diff_cmd .extend (directories )
60
64
61
65
files_modified = get_lines (diff_cmd )
62
66
if not files_modified :
0 commit comments