Skip to content

Commit

Permalink
Faster gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
legau committed Apr 7, 2021
1 parent f074239 commit 927cf7e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import configparser
import fnmatch
import glob
import os
import posixpath
import re
Expand Down Expand Up @@ -214,6 +215,8 @@ class _Config:
reverse_sort: bool = False
star_first: bool = False
import_dependencies = Dict[str, str]
git_folders: Set[Path] = field(default_factory=set)
git_ignore_files: Set[Path] = field(default_factory=set)

def __post_init__(self):
py_version = self.py_version
Expand Down Expand Up @@ -521,10 +524,26 @@ def is_skipped(self, file_path: Path) -> bool:
if file_path.name == ".git": # pragma: no cover
return True

result = subprocess.run( # nosec # skipcq: PYL-W1510
["git", "-C", str(file_path.parent), "check-ignore", "--quiet", os_path]
)
if result.returncode == 0:
for folder in self.git_folders:
if folder in file_path.parents:
break
else:
topfolder_result = subprocess.run([
"git", "-C", str(file_path.parent), "rev-parse", "--show-toplevel"
], capture_output=True)
if topfolder_result.returncode == 0:
git_folder = Path(
topfolder_result.stdout.decode('utf-8').split("\n")[0]
)
files = glob.glob(str(git_folder)+"/**/*", recursive=True)
files_result = subprocess.run(
["git", "-C", git_folder, "check-ignore", *files], capture_output=True
).stdout.decode('utf-8').split("\n")
files_result = files_result[:-1] if files_result else files_result
self.git_ignore_files.update({Path(f) for f in files_result})
self.git_folders.add(git_folder)

if file_path in self.git_ignore_files:
return True

normalized_path = os_path.replace("\\", "/")
Expand All @@ -543,8 +562,8 @@ def is_skipped(self, file_path: Path) -> bool:
return True
position = os.path.split(position[0])

for glob in self.skip_globs:
if fnmatch.fnmatch(file_name, glob) or fnmatch.fnmatch("/" + file_name, glob):
for sglob in self.skip_globs:
if fnmatch.fnmatch(file_name, sglob) or fnmatch.fnmatch("/" + file_name, sglob):
return True

if not (os.path.isfile(os_path) or os.path.isdir(os_path) or os.path.islink(os_path)):
Expand Down

0 comments on commit 927cf7e

Please sign in to comment.