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

Check gitignore more intelligently #1789

Merged
merged 3 commits into from Jul 21, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions isort/settings.py
Expand Up @@ -552,11 +552,10 @@ def _check_folder_gitignore(self, folder: str) -> Optional[Path]:
# don't check symlinks; either part of the repo and would be checked
# twice, or is external to the repo and git won't know anything about it
for root, _dirs, git_files in os.walk(git_folder, followlinks=False):
if ".git" in _dirs:
_dirs.remove(".git")
for git_file in git_files:
git_path = os.path.join(root, git_file)
# followlinks only disables walking into linked dirs
if not os.path.islink(git_path): # pragma: no cover
files.append(git_path)
files.append(os.path.join(root, git_file))
git_options = ["-C", str(git_folder), "-c", "core.quotePath="]
try:
ignored = subprocess.check_output( # nosec # skipcq: PYL-W1510
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_isort.py
Expand Up @@ -2708,7 +2708,7 @@ def test_import_by_paren_issue_375() -> None:


def test_import_by_paren_issue_460() -> None:
"""Test to ensure isort can doesnt move comments around """
"""Test to ensure isort can doesnt move comments around"""
test_input = """
# First comment
# Second comment
Expand Down Expand Up @@ -5184,7 +5184,7 @@ def test_only_sections() -> None:


def test_combine_straight_imports() -> None:
""" Tests to ensure that combine_straight_imports works correctly """
"""Tests to ensure that combine_straight_imports works correctly"""

test_input = (
"import os\n" "import sys\n" "# this is a comment\n" "import math # inline comment\n"
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_main.py
Expand Up @@ -1188,3 +1188,26 @@ def main_check(args):
out, error = main_check([str(tmpdir), "--skip-gitignore", "--filter-files"])

assert all(f"{str(tmpdir)}{file}" in out for file in should_check)

# Should work when git project contains symlinks

if os.name != "nt":
git_project0.join("has_imports_ignored.py").write(import_content)
git_project0.join("has_imports.py").write(import_content)
tmpdir.join("has_imports.py").write(import_content)
tmpdir.join("nested_dir").join("has_imports.py").write(import_content)
git_project0.join("ignore_link.py").mksymlinkto(tmpdir.join("has_imports.py"))
git_project0.join("ignore_link").mksymlinkto(tmpdir.join("nested_dir"))
git_project0.join(".gitignore").write("ignore_link.py\nignore_link", mode="a")

out, error = main_check(
[str(git_project0), "--skip-gitignore", "--filter-files", "--check"]
)

should_check = ["/git_project0/has_imports.py"]

assert all(f"{str(tmpdir)}{file}" in error for file in should_check)

out, error = main_check([str(git_project0), "--skip-gitignore", "--filter-files"])

assert all(f"{str(tmpdir)}{file}" in out for file in should_check)