From dd78ecff8e3b219fecd7d31f70a16268e3fcfc9c Mon Sep 17 00:00:00 2001 From: Jeremy Paige Date: Tue, 20 Jul 2021 16:44:26 -0700 Subject: [PATCH 1/2] Check gitignore more intelligently --- isort/settings.py | 7 +++---- tests/unit/test_isort.py | 4 ++-- tests/unit/test_main.py | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/isort/settings.py b/isort/settings.py index 7cec61048..764760210 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -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 konw 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): - 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 diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 526a7b8a0..f6f905fda 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -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 @@ -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" diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 9039787cc..ea7dbaa7b 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -1170,3 +1170,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) From c674a7dd163a0726dc6277241fdffe3fccf4b38f Mon Sep 17 00:00:00 2001 From: Timothy Edmund Crosley Date: Tue, 20 Jul 2021 22:46:06 -0700 Subject: [PATCH 2/2] Fix bad merge --- isort/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isort/settings.py b/isort/settings.py index 73f12d16d..ee7297442 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -555,7 +555,7 @@ def _check_folder_gitignore(self, folder: str) -> Optional[Path]: if ".git" in _dirs: _dirs.remove(".git") for git_file in git_files: - files.append(os.path.join(root, git_file))\ + 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