Git Product home page Git Product logo

Comments (2)

bram-tv avatar bram-tv commented on June 8, 2024

Looking at the is_skipped code in settings.py, it ends with:

and not file_path.is_dir()

            ...

            # git_ls_files are good files you should parse. If you're not in the allow list, skip.

            if (
                git_folder
                and not file_path.is_dir()
                and str(file_path.resolve()) not in self.git_ls_files[git_folder]
            ):
                return True

        return False

When the file_path is a directory it will return False which will cause it to traverse the directory.

A possible fix: when file_path is a directory then check if there git_ls_files contains a file inside that directory.

Something like:

diff --git a/isort/settings.py b/isort/settings.py
index a3658fff..67acdbf5 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -641,11 +641,14 @@ class Config(_Config):

             # git_ls_files are good files you should parse. If you're not in the allow list, skip.

-            if (
-                git_folder
-                and not file_path.is_dir()
-                and str(file_path.resolve()) not in self.git_ls_files[git_folder]
-            ):
+            if git_folder:
+                if file_path.is_dir():
+                    dir = file_path.resolve()
+                    if not any(file.startswith(f"{dir}/") for file in self.git_ls_files[git_folder]):
+                        return True
+
+                elif str(file_path.resolve()) not in self.git_ls_files[git_folder]:
+                    return True
                 return True

         return False

With the example repository listed in the description the output is:

...
sub3 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
sub2 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
sub1 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled....
...

which is good.

However it's not yet perfect...
When the repository is changed into:

tree -a  -I '.git'
.
├── bar.py
├── dir1
│   ├── sub1
│   │   ├── dir1_sub1_file1.py
│   │   └── dir1_sub1_file2.py
│   ├── sub2
│   │   ├── dir1_sub2_file1.py
│   │   └── dir1_sub2_file2.py
│   └── sub3
│       ├── dir1_sub3_file1.py
│       └── dir1_sub3_file2.py
├── foo.py
└── .gitignore

4 directories, 9 files

where the .gitignore file now contains:

dir1/sub1/
dir1/sub2/
dir1/sub3/

then the output is:

...
dir1 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
...

which is a bit confusing: in the .gitignore file it's specified to ignore dir1/sub1/, dir1/sub2 and dir1/sub3 but the isort output makes it appear as if the entire directory was ignored..
In a way it was because there were no other files in dir1/ but still the message doesn't "feel" right..

from isort.

bram-tv avatar bram-tv commented on June 8, 2024

Also: note that this solution is still suboptimal is some cases: it can still cause too many files/directories to be traversed.

Assume the layout:

$ tree -a  -I '.git'
.
├── bar.py
├── dir1
│   ├── sub1
│   │   ├── dir1_sub1_file1.py
│   │   ├── dir1_sub1_file2.py
│   │   ├── subsub1
│   │   │   └── dir1_sub1_subsub1_file_1.py
│   │   ├── subsub2
│   │   │   └── dir1_sub1_subsub2_file_1.py
│   │   ├── subsub3
│   │   │   └── dir1_sub1_subsub3_file_1.py
│   │   ├── subsub4
│   │   │   └── dir1_sub1_subsub4_file_1.py
│   │   ├── subsub5
│   │   │   └── dir1_sub1_subsub5_file_1.py
│   │   ├── subsub6
│   │   │   └── dir1_sub1_subsub6_file_1.py
│   │   ├── subsub7
│   │   │   └── dir1_sub1_subsub7_file_1.py
│   │   ├── subsub8
│   │   │   └── dir1_sub1_subsub8_file_1.py
│   │   └── subsub9
│   │       └── dir1_sub1_subsub9_file_1.py
│   ├── sub2
│   │   ├── dir1_sub2_file1.py
│   │   └── dir1_sub2_file2.py
│   └── sub3
│       ├── dir1_sub3_file1.py
│       └── dir1_sub3_file2.py
├── foo.py
└── .gitignore

13 directories, 18 files

And assume the file dir1/sub1/subsub5/dir1_sub1_subsub5_file_1.py was committed (despite being ignored, i.e. it was added using git add -f)

And .gitignore contains:

dir1/sub1/
dir1/sub2/
dir1/sub3/

The git ls-files output for reference:

$ git ls-files
.gitignore
bar.py
dir1/sub1/subsub5/dir1_sub1_subsub5_file_1.py
foo.py
$ git ls-files --others --exclude-standard
$

then isort output is:

...
sub3 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
sub2 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub1 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub7 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub2 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub4 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub9 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub3 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub6 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
subsub8 was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
dir1_sub1_file2.py was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
dir1_sub1_file1.py was skipped as it's listed in 'skip' setting, matches a glob in 'skip_glob' setting, or is in a .gitignore file with --skip-gitignore enabled.
...

so it still did some unneeded directory traversal.

I'm not sure if the structure of the code allows this to be (easily) fixed tho..
We know the files that may need to be checked (i.e. the files in self.git_ls_files) but that's not how the code loops..

from isort.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.