Skip to content

Commit

Permalink
Perfect processing, 35% faster than pathspec. #24 cpburnz/python-pa…
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Aug 28, 2022
1 parent 14f1214 commit 7b9266a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 14 additions & 5 deletions gitignorefile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ def __call__(self, path, is_dir=None):

parent_gitignore = parent.join(".gitignore")
if parent_gitignore.isfile():
p = parse(str(parent_gitignore), base_path=parent)
add_to_children[parent] = (p, plain_paths)
matches = parse(str(parent_gitignore), base_path=parent)
add_to_children[parent] = (matches, plain_paths)
plain_paths = []

else:
plain_paths.append(parent)

else:
for plain_path in plain_paths:
self.__gitignores[plain_path] = []
assert plain_path.parts not in self.__gitignores
self.__gitignores[plain_path.parts] = []

if not add_to_children:
if add_to_children:
plain_paths.clear()

else:
return False

for parent, (_, parent_plain_paths) in reversed(list(add_to_children.items())):
Expand All @@ -64,8 +68,13 @@ def __call__(self, path, is_dir=None):

self.__gitignores[parent.parts].reverse()
for plain_path in parent_plain_paths:
assert plain_path.parts not in self.__gitignores
self.__gitignores[plain_path.parts] = self.__gitignores[parent.parts]

for plain_path in plain_paths:
assert plain_path.parts not in self.__gitignores
self.__gitignores[plain_path.parts] = self.__gitignores[parent.parts]

return any(
(m(path, is_dir=is_dir) for m in self.__gitignores[parent.parts])
) # This parent comes either from first or second loop.
Expand Down Expand Up @@ -114,7 +123,7 @@ def isdir(self):

def __str__(self):
if self.__joined is None:
self.__joined = "/".join(self.__parts)
self.__joined = "/".join(self.__parts) if self.__parts != ("",) else "/"
return self.__joined


Expand Down
7 changes: 6 additions & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def __call__(self, path):
"/",
],
[
"/home/vladimir/project/directory/subdirectory/subdirectory/file.txt",
"/home/vladimir/project/directory/subdirectory/subdirectory/file2.txt",
"/home/vladimir/project/directory/subdirectory/file.txt",
"/home/vladimir/project/directory/subdirectory/file2.txt",
"/home/vladimir/project/directory/.gitignore",
Expand Down Expand Up @@ -75,12 +77,15 @@ def mock_stat(path):
matches = gitignorefile.Cache()
self.assertTrue(matches("/home/vladimir/project/directory/subdirectory/file.txt"))
self.assertTrue(matches("/home/vladimir/project/directory/subdirectory/file2.txt"))
self.assertTrue(matches("/home/vladimir/project/directory/subdirectory/subdirectory/file.txt"))
self.assertTrue(matches("/home/vladimir/project/directory/subdirectory/subdirectory/file2.txt"))
self.assertFalse(matches("/home/vladimir/project/directory/subdirectory/subdirectory/file3.txt"))
self.assertTrue(matches("/home/vladimir/project/directory/file.txt"))
self.assertTrue(matches("/home/vladimir/project/directory/file2.txt"))
self.assertFalse(matches("/home/vladimir/project/file.txt"))

self.assertEqual(statistics["open"], 2)
self.assertEqual(statistics["stat"], 6 + 5)
self.assertEqual(statistics["stat"], 7 + 8)

def test_wrong_symlink(self):
with tempfile.TemporaryDirectory() as d:
Expand Down

0 comments on commit 7b9266a

Please sign in to comment.