Skip to content

Commit

Permalink
gitignore_parser.py: Handle non-subpaths in rule file check (#644)
Browse files Browse the repository at this point in the history
In IgnoreRule.match(), simply return False if the given absolute
path is not a subpath of the base path instead of not handling
the exception and breaking the overall build.

Signed-off-by: Michael Kubacki <[email protected]>
  • Loading branch information
makubacki authored Oct 1, 2024
1 parent 2b0527b commit 78b103e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion edk2toollib/gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ def match(self, abs_path: str) -> bool:
"""Returns True or False if the path matches the rule."""
matched = False
if self.base_path:
rel_path = _normalize_path(abs_path).relative_to(self.base_path).as_posix()
try:
rel_path = _normalize_path(abs_path).relative_to(self.base_path).as_posix()
except ValueError as e:
if "is not in the subpath of" in str(e):
return False
else:
raise
else:
rel_path = _normalize_path(abs_path).as_posix()
# Path() strips the trailing following symbols on windows, so we need to
Expand Down
15 changes: 15 additions & 0 deletions tests.unit/parsers/test_gitingore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ def test_incomplete_filename(tmp_path):
assert rule_tester("/home/tmp/dir/o.pyc") is False



def test_unrelated_path(tmp_path):
"""Tests that a path that is completely unrelated to another path works."""
root = tmp_path.resolve()
gitignore_path = root / ".gitignore"

with open(gitignore_path, 'w') as f:
f.write('*foo*\n')

rule_tester = gitignore_parser.parse_gitignore_file(gitignore_path, base_dir='/home/tmp')

assert rule_tester('/home/tmp/foo') is True
assert rule_tester('/some/other/dir') is False


def test_double_asterisks(tmp_path):
"""Test that double asterisk match any number of directories."""
root = tmp_path.resolve()
Expand Down

0 comments on commit 78b103e

Please sign in to comment.