Skip to content

Commit dd78ecf

Browse files
committed
Check gitignore more intelligently
1 parent 2bbf393 commit dd78ecf

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

isort/settings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,10 @@ def _check_folder_gitignore(self, folder: str) -> Optional[Path]:
552552
# don't check symlinks; either part of the repo and would be checked
553553
# twice, or is external to the repo and git won't konw anything about it
554554
for root, _dirs, git_files in os.walk(git_folder, followlinks=False):
555+
if ".git" in _dirs:
556+
_dirs.remove(".git")
555557
for git_file in git_files:
556-
git_path = os.path.join(root, git_file)
557-
# followlinks only disables walking into linked dirs
558-
if not os.path.islink(git_path):
559-
files.append(git_path)
558+
files.append(os.path.join(root, git_file))
560559
git_options = ["-C", str(git_folder), "-c", "core.quotePath="]
561560
try:
562561
ignored = subprocess.check_output( # nosec # skipcq: PYL-W1510

tests/unit/test_isort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ def test_import_by_paren_issue_375() -> None:
27082708

27092709

27102710
def test_import_by_paren_issue_460() -> None:
2711-
"""Test to ensure isort can doesnt move comments around """
2711+
"""Test to ensure isort can doesnt move comments around"""
27122712
test_input = """
27132713
# First comment
27142714
# Second comment
@@ -5184,7 +5184,7 @@ def test_only_sections() -> None:
51845184

51855185

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

51895189
test_input = (
51905190
"import os\n" "import sys\n" "# this is a comment\n" "import math # inline comment\n"

tests/unit/test_main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,3 +1170,26 @@ def main_check(args):
11701170
out, error = main_check([str(tmpdir), "--skip-gitignore", "--filter-files"])
11711171

11721172
assert all(f"{str(tmpdir)}{file}" in out for file in should_check)
1173+
1174+
# Should work when git project contains symlinks
1175+
1176+
if os.name != "nt":
1177+
git_project0.join("has_imports_ignored.py").write(import_content)
1178+
git_project0.join("has_imports.py").write(import_content)
1179+
tmpdir.join("has_imports.py").write(import_content)
1180+
tmpdir.join("nested_dir").join("has_imports.py").write(import_content)
1181+
git_project0.join("ignore_link.py").mksymlinkto(tmpdir.join("has_imports.py"))
1182+
git_project0.join("ignore_link").mksymlinkto(tmpdir.join("nested_dir"))
1183+
git_project0.join(".gitignore").write("ignore_link.py\nignore_link", mode="a")
1184+
1185+
out, error = main_check(
1186+
[str(git_project0), "--skip-gitignore", "--filter-files", "--check"]
1187+
)
1188+
1189+
should_check = ["/git_project0/has_imports.py"]
1190+
1191+
assert all(f"{str(tmpdir)}{file}" in error for file in should_check)
1192+
1193+
out, error = main_check([str(git_project0), "--skip-gitignore", "--filter-files"])
1194+
1195+
assert all(f"{str(tmpdir)}{file}" in out for file in should_check)

0 commit comments

Comments
 (0)