From 7ada83a4cf83c1178159c250a11811012506058c Mon Sep 17 00:00:00 2001 From: Axel Aguado Date: Sat, 21 Jan 2023 14:06:25 -0600 Subject: [PATCH 1/3] Add test_ignored_items_reported --- test/test_repo.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_repo.py b/test/test_repo.py index d5474353f..c21f3270f 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1384,3 +1384,19 @@ def test_clone_from_command_injection(self, rw_repo): rw_repo.clone_from(payload, temp_repo.common_dir) assert not unexpected_file.exists() + + def test_ignored_items_reported(self): + with tempfile.TemporaryDirectory() as tdir: + tmp_dir = pathlib.Path(tdir) + temp_repo = Repo.init(tmp_dir / "repo") + + gi = tmp_dir / "repo" / ".gitignore" + + with open(gi, 'w') as file: + file.write('ignored_file.txt\n') + file.write('ignored_dir/\n') + + assert temp_repo.ignored(['included_file.txt', 'included_dir/file.txt']) == [] + assert temp_repo.ignored(['ignored_file.txt']) == ['ignored_file.txt'] + assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt']) == ['ignored_file.txt'] + assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt'] \ No newline at end of file From 2ddb604ae8a7fd1069857c8194155d109565e6bb Mon Sep 17 00:00:00 2001 From: Axel Aguado Date: Sat, 21 Jan 2023 15:05:33 -0600 Subject: [PATCH 2/3] Add test to verify GitCommandError is raised when check-ignore is run against a file behind a symlink --- test/test_repo.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/test_repo.py b/test/test_repo.py index c21f3270f..07c1e9adf 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1399,4 +1399,15 @@ def test_ignored_items_reported(self): assert temp_repo.ignored(['included_file.txt', 'included_dir/file.txt']) == [] assert temp_repo.ignored(['ignored_file.txt']) == ['ignored_file.txt'] assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt']) == ['ignored_file.txt'] - assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt'] \ No newline at end of file + assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt'] + + def test_ignored_raises_error_w_symlink(self): + with tempfile.TemporaryDirectory() as tdir: + tmp_dir = pathlib.Path(tdir) + temp_repo = Repo.init(tmp_dir / "repo") + + os.mkdir(tmp_dir / "target") + os.symlink(tmp_dir / "target", tmp_dir / "symlink") + + with pytest.raises(GitCommandError): + temp_repo.ignored(tmp_dir / "symlink/file.txt") \ No newline at end of file From df4dabb17c4e83c580d515894dbf7d57912ee554 Mon Sep 17 00:00:00 2001 From: Axel Aguado Date: Sat, 21 Jan 2023 15:38:46 -0600 Subject: [PATCH 3/3] Raise exception if return code from check-ignore is not 1 --- git/repo/base.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/git/repo/base.py b/git/repo/base.py index 30f71b0c8..9cdf673e6 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -873,8 +873,15 @@ def ignored(self, *paths: PathLike) -> List[str]: """ try: proc: str = self.git.check_ignore(*paths) - except GitCommandError: - return [] + except GitCommandError as err: + # If return code is 1, this means none of the items in *paths + # are ignored by Git, so return an empty list. Raise the + # exception on all other return codes. + if err.status == 1: + return [] + else: + raise + return proc.replace("\\\\", "\\").replace('"', "").split("\n") @property