@@ -89,7 +89,8 @@ def _is_ignored_dirname(self, dirname: str) -> bool:
89
89
90
90
def _is_ignored_relative_path (self , relative_path : str | Path , ignore_non_source_files : bool = True ) -> bool :
91
91
"""
92
- Determine whether a path should be ignored based on file type and ignore patterns.
92
+ Determine whether an existing path should be ignored based on file type and ignore patterns.
93
+ Raises `FileNotFoundError` if the path does not exist.
93
94
94
95
:param relative_path: Relative path to check
95
96
:param ignore_non_source_files: whether files that are not source files (according to the file masks
@@ -156,10 +157,22 @@ def is_path_in_project(self, path: str | Path) -> bool:
156
157
path = path .resolve ()
157
158
return path .is_relative_to (_proj_root )
158
159
160
+ def relative_path_exists (self , relative_path : str ) -> bool :
161
+ """
162
+ Checks if the given relative path exists in the project directory.
163
+
164
+ :param relative_path: the path to check, relative to the project root
165
+ :return: True if the path exists, False otherwise
166
+ """
167
+ abs_path = Path (self .project_root ) / relative_path
168
+ return abs_path .exists ()
169
+
159
170
def validate_relative_path (self , relative_path : str ) -> None :
160
171
"""
161
- Validates that the given relative path is safe to read or edit,
172
+ Validates that the given relative path to an existing file/dir is safe to read or edit,
162
173
meaning it's inside the project directory and is not ignored by git.
174
+
175
+ Passing a path to a non-existing file will lead to a `FileNotFoundError`.
163
176
"""
164
177
if not self .is_path_in_project (relative_path ):
165
178
raise ValueError (f"{ relative_path = } points to path outside of the repository root; cannot access for safety reasons" )
0 commit comments