66import os
77import subprocess # nosec - Needed for hook
88from pathlib import Path
9- from typing import List
9+ from typing import List , Optional
1010
1111from isort import Config , api , exceptions
1212
@@ -32,7 +32,8 @@ def get_lines(command: List[str]) -> List[str]:
3232
3333
3434def 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