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

Actually determine if we are dealing with a shallow clone instead of … #298

Merged
merged 3 commits into from
Dec 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions tartufo/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,8 @@ def chunks(self) -> Generator[types.Chunk, None, None]:
branches = [self.git_options.branch]
else:
# Everything
if not self._repo.listall_branches():
# If no local branches are found, assume that this is a
# shallow clone and examine the repo head as a single
if util.is_shallow_clone(self._repo):
# If this is a shallow clone, examine the repo head as a single
# commit to scan all files at once
branches = ["HEAD"]
else:
Expand Down
14 changes: 14 additions & 0 deletions tartufo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ def process_issues(
write_outputs(scan.issues, output_dir)
if options.output_format != types.OutputFormat.Json.value:
click.echo(f"Results have been saved in {output_dir}")


def is_shallow_clone(repo: pygit2.Repository) -> bool:
"""Determine whether a repository is a shallow clone

:param repo: The repository to check for "shallowness"

This is used to work around https://github.com/libgit2/libgit2/issues/3058
Basically, any time a git repository is a "shallow" clone (it was cloned
with `--max-depth N`), git will create a file at `.git/shallow`. So we
simply need to test whether that file exists to know whether we are
interacting with a shallow repository.
"""
return (pathlib.Path(repo.path) / "shallow").exists()
9 changes: 7 additions & 2 deletions tests/test_git_repo_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,17 @@ class ChunkGeneratorTests(ScannerTestCase):
def setUp(self) -> None:
self.diff_patcher = mock.patch("tartufo.scanner.GitScanner._iter_diff_index")
self.repo_patcher = mock.patch("pygit2.Repository")
self.shallow_patcher = mock.patch("tartufo.scanner.util.is_shallow_clone")

self.mock_iter_diff = self.diff_patcher.start()
self.mock_repo = self.repo_patcher.start()
self.mock_shallow = self.shallow_patcher.start()

self.mock_shallow.return_value = False

self.addCleanup(self.diff_patcher.stop)
self.addCleanup(self.repo_patcher.stop)
self.addCleanup(self.shallow_patcher.stop)
return super().setUp()

def test_single_branch_is_loaded_if_specified(self):
Expand Down Expand Up @@ -306,8 +311,8 @@ def test_error_is_raised_when_specified_branch_is_not_found(self):
for _ in test_scanner.chunks:
pass

def test_head_is_scanned_when_no_local_branches_are_found(self):
self.mock_repo.return_value.listall_branches.return_value = []
def test_head_is_scanned_when_shallow_clone_is_found(self):
self.mock_shallow.return_value = True
self.mock_iter_diff.return_value = []
self.mock_repo.return_value.head.target = "commit-hash"
mock_head = mock.MagicMock(spec=pygit2.Commit)
Expand Down