Skip to content

Adds repo.is_valid_object() check. #1267

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

Merged
merged 5 commits into from
Jun 5, 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
20 changes: 19 additions & 1 deletion git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

import logging
import os
import re
import warnings

from gitdb.exc import BadObject

from git.cmd import (
Git,
handle_process_output
Expand Down Expand Up @@ -618,6 +619,23 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool:
raise
return True

def is_valid_object(self, sha: str, object_type: str = None) -> bool:
try:
complete_sha = self.odb.partial_to_complete_sha_hex(sha)
object_info = self.odb.info(complete_sha)
if object_type:
if object_info.type == object_type.encode():
return True
else:
log.debug("Commit hash points to an object of type '%s'. Requested were objects of type '%s'",
object_info.type.decode(), object_type)
return False
else:
return True
except BadObject:
log.debug("Commit hash is invalid.")
return False

def _get_daemon_export(self) -> bool:
if self.git_dir:
filename = osp.join(self.git_dir, self.DAEMON_EXPORT_FILE)
Expand Down
28 changes: 28 additions & 0 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,34 @@ def test_is_ancestor(self):
for i, j in itertools.permutations([c1, 'ffffff', ''], r=2):
self.assertRaises(GitCommandError, repo.is_ancestor, i, j)

def test_is_valid_object(self):
repo = self.rorepo
commit_sha = 'f6aa8d1'
blob_sha = '1fbe3e4375'
tree_sha = '960b40fe36'
tag_sha = '42c2f60c43'

# Check for valid objects
self.assertTrue(repo.is_valid_object(commit_sha))
self.assertTrue(repo.is_valid_object(blob_sha))
self.assertTrue(repo.is_valid_object(tree_sha))
self.assertTrue(repo.is_valid_object(tag_sha))

# Check for valid objects of specific type
self.assertTrue(repo.is_valid_object(commit_sha, 'commit'))
self.assertTrue(repo.is_valid_object(blob_sha, 'blob'))
self.assertTrue(repo.is_valid_object(tree_sha, 'tree'))
self.assertTrue(repo.is_valid_object(tag_sha, 'tag'))

# Check for invalid objects
self.assertFalse(repo.is_valid_object(b'1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a', 'blob'))

# Check for invalid objects of specific type
self.assertFalse(repo.is_valid_object(commit_sha, 'blob'))
self.assertFalse(repo.is_valid_object(blob_sha, 'commit'))
self.assertFalse(repo.is_valid_object(tree_sha, 'commit'))
self.assertFalse(repo.is_valid_object(tag_sha, 'commit'))

@with_rw_directory
def test_git_work_tree_dotgit(self, rw_dir):
"""Check that we find .git as a worktree file and find the worktree
Expand Down