Skip to content

Commit

Permalink
Merge pull request #1789 from ucodery/better-gitignore
Browse files Browse the repository at this point in the history
Check gitignore more intelligently
  • Loading branch information
timothycrosley committed Jul 21, 2021
2 parents 3dc4d89 + c674a7d commit f311627
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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)

0 comments on commit f311627

Please sign in to comment.