From 313026627fcd5a54f5f3fd1821d27de91fc9e877 Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Mon, 30 Sep 2024 07:28:58 -0400 Subject: [PATCH 1/2] gitignore_parser.py: Handle non-subpaths in rule file check 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 --- edk2toollib/gitignore_parser.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/edk2toollib/gitignore_parser.py b/edk2toollib/gitignore_parser.py index 2cbc8b50..513e4079 100644 --- a/edk2toollib/gitignore_parser.py +++ b/edk2toollib/gitignore_parser.py @@ -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 From 53346c24d9f229bf3af0a4bafb1a14809e75dd09 Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Mon, 30 Sep 2024 21:26:15 -0400 Subject: [PATCH 2/2] PR Update: Add unit test for non-subpaths --- tests.unit/parsers/test_gitingore_parser.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests.unit/parsers/test_gitingore_parser.py b/tests.unit/parsers/test_gitingore_parser.py index 56e77050..73e4b304 100644 --- a/tests.unit/parsers/test_gitingore_parser.py +++ b/tests.unit/parsers/test_gitingore_parser.py @@ -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()