Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignored #1545

Merged
merged 3 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,30 @@ 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']

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")