diff --git a/src/serena/util/file_system.py b/src/serena/util/file_system.py index d7d9f956..318cb571 100644 --- a/src/serena/util/file_system.py +++ b/src/serena/util/file_system.py @@ -341,6 +341,9 @@ def match_path(relative_path: str, path_spec: PathSpec, root_path: str = "") -> :param root_path: the root path from which the relative path is derived :return: """ + if str(relative_path) in {"", "."}: + return False + normalized_path = str(relative_path).replace(os.path.sep, "/") # We can have patterns like /src/..., which would only match corresponding paths from the repo root diff --git a/test/serena/util/test_file_system.py b/test/serena/util/test_file_system.py index b47dd34c..95ce9e61 100644 --- a/test/serena/util/test_file_system.py +++ b/test/serena/util/test_file_system.py @@ -4,7 +4,9 @@ import tempfile from pathlib import Path # Assuming the gitignore parser code is in a module named 'gitignore_parser' -from serena.util.file_system import GitignoreParser, GitignoreSpec +from pathspec import PathSpec + +from serena.util.file_system import GitignoreParser, GitignoreSpec, match_path class TestGitignoreParser: @@ -191,6 +193,13 @@ test.log assert not parser.should_ignore("file1.txt") assert not parser.should_ignore("src/main.py") + def test_match_path_root_directory(self): + """Root directory should never be ignored by pathspec patterns.""" + spec = PathSpec.from_lines("gitwildmatch", ["/.*/"]) + + assert not match_path(".", spec, root_path=str(self.repo_path)) + assert not match_path("", spec, root_path=str(self.repo_path)) + def test_should_ignore_subdirectory_patterns(self): """Test ignoring files based on subdirectory .gitignore files.""" parser = GitignoreParser(str(self.repo_path))