Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to restrict which directories isort works against #1967

Merged
merged 1 commit into from Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions isort/hooks.py
Expand Up @@ -6,7 +6,7 @@
import os
import subprocess # nosec - Needed for hook
from pathlib import Path
from typing import List
from typing import List, Optional

from isort import Config, api, exceptions

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


def git_hook(
strict: bool = False, modify: bool = False, lazy: bool = False, settings_file: str = ""
strict: bool = False, modify: bool = False, lazy: bool = False, settings_file: str = "",
directories: Optional[List[str]] = None,
) -> int:
"""Git pre-commit hook to check staged files for isort errors

Expand All @@ -50,13 +51,16 @@ def git_hook(
When settings_file is the empty string, the configuration file
will be searched starting at the directory containing the first
staged file, if any, and going upward in the directory structure.
:param list[str] directories - A list of directories to restrict the hook to.

:return number of errors if in strict mode, 0 otherwise.
"""
# Get list of files modified and staged
diff_cmd = ["git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD"]
if lazy:
diff_cmd.remove("--cached")
if directories:
diff_cmd.extend(directories)

files_modified = get_lines(diff_cmd)
if not files_modified:
Expand Down