Skip to content

Commit 2c5b49a

Browse files
authored
Merge pull request #556 from Digital-Defiance/pr/gitignore-path-resolve
Fix gitignore pathspec checks to resolve paths against repo root
2 parents e7a21bb + fcf8876 commit 2c5b49a

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

cecli/repo.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -778,37 +778,45 @@ def _get_gitignore_spec(self, dir_path):
778778
self.gitignore_spec_cache[dir_path] = spec
779779
return spec
780780

781+
def _resolve_path_in_repo(self, path):
782+
"""Resolve *path* under this repo root (not process cwd)."""
783+
file_path = Path(path)
784+
if not file_path.is_absolute():
785+
file_path = (Path(self.root) / file_path).resolve()
786+
else:
787+
file_path = file_path.resolve()
788+
return file_path
789+
781790
def _is_gitignored_by_pathspec(self, path):
782791
"""Check if a file is ignored by any .gitignore file using pathspec."""
783792
if not self.repo:
784793
return False
785794

786795
try:
787-
file_path = Path(path).resolve()
788-
if not file_path.is_relative_to(self.root):
796+
file_path = self._resolve_path_in_repo(path)
797+
root = Path(self.root).resolve()
798+
if not file_path.is_relative_to(root):
789799
return False
790800

791801
# Walk up from file's directory to root
792802
current_dir = file_path.parent
793-
relative_path = file_path.relative_to(self.root)
803+
relative_path = file_path.relative_to(root)
794804

795805
# Check each directory level
796-
while current_dir.is_relative_to(self.root):
806+
while current_dir.is_relative_to(root):
797807
spec = self._get_gitignore_spec(current_dir)
798808

799809
# Get path relative to the directory containing the .gitignore
800-
if current_dir == Path(self.root).resolve():
810+
if current_dir == root:
801811
path_to_check = str(relative_path)
802812
else:
803-
path_to_check = str(
804-
relative_path.relative_to(current_dir.relative_to(self.root))
805-
)
813+
path_to_check = str(relative_path.relative_to(current_dir.relative_to(root)))
806814

807815
if spec.match_file(path_to_check):
808816
return True
809817

810818
# Move up one directory
811-
if current_dir == Path(self.root).resolve():
819+
if current_dir == root:
812820
break
813821
current_dir = current_dir.parent
814822

0 commit comments

Comments
 (0)