From df864071a3b580212171a8e0740bf1980387f437 Mon Sep 17 00:00:00 2001 From: Vladimir Chebotarev Date: Sun, 28 Aug 2022 18:44:06 +0300 Subject: [PATCH] `pathspec`-based version. #24 cpburnz/python-path-specification#38 --- gitignorefile/__init__.py | 9 ++++++--- requirements.txt | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gitignorefile/__init__.py b/gitignorefile/__init__.py index 2fc08a5..42387ba 100644 --- a/gitignorefile/__init__.py +++ b/gitignorefile/__init__.py @@ -2,6 +2,8 @@ import os import re +import pathspec + def parse(path, base_path=None): if base_path is None: @@ -41,8 +43,9 @@ def __call__(self, path, is_dir=None): parent_gitignore = parent.join(".gitignore") if parent_gitignore.isfile(): - matches = parse(str(parent_gitignore), base_path=parent) - add_to_children[parent] = (matches, plain_paths) + with open(str(parent_gitignore)) as f: + matches = pathspec.PathSpec.from_lines("gitwildmatch", f) + add_to_children[parent] = ((matches, parent), plain_paths) plain_paths = [] else: @@ -74,7 +77,7 @@ def __call__(self, path, is_dir=None): for plain_path in plain_paths: self.__gitignores[plain_path.parts] = self.__gitignores[parent.parts] - return any((m(path, is_dir=is_dir) for m in self.__gitignores[parent.parts])) + return any((m[0].match_file(path.relpath(m[1])) for m in self.__gitignores[parent.parts])) class _Path: diff --git a/requirements.txt b/requirements.txt index e69de29..6486958 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +pathspec