Skip to content

Commit 385a8c6

Browse files
committed
Adds repo.is_valid_object check
1 parent 01a96b9 commit 385a8c6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

git/repo/base.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6-
6+
import binascii
77
import logging
88
import os
99
import re
1010
import warnings
1111

12+
from gitdb.exc import BadObject
13+
1214
from git.cmd import (
1315
Git,
1416
handle_process_output
@@ -618,6 +620,23 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool:
618620
raise
619621
return True
620622

623+
def is_valid_object(self, sha: str, object_type: Union['blob', 'commit', 'tree', 'tag'] = None) -> bool:
624+
try:
625+
complete_sha = self.odb.partial_to_complete_sha_hex(sha)
626+
object_info = self.odb.info(complete_sha)
627+
if object_type:
628+
if object_info.type == object_type.encode():
629+
return True
630+
else:
631+
log.debug(f"Commit hash points to an object of type '{object_info.type.decode()}'. "
632+
f"Requested were objects of type '{object_type}'")
633+
return False
634+
else:
635+
return True
636+
except BadObject as e:
637+
log.debug("Commit hash is invalid.")
638+
return False
639+
621640
def _get_daemon_export(self) -> bool:
622641
if self.git_dir:
623642
filename = osp.join(self.git_dir, self.DAEMON_EXPORT_FILE)

test/test_repo.py

+28
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,34 @@ def test_is_ancestor(self):
989989
for i, j in itertools.permutations([c1, 'ffffff', ''], r=2):
990990
self.assertRaises(GitCommandError, repo.is_ancestor, i, j)
991991

992+
def test_is_valid_object(self):
993+
repo = self.rorepo
994+
commit_sha = 'f6aa8d1'
995+
blob_sha = '1fbe3e4375'
996+
tree_sha = '960b40fe36'
997+
tag_sha = '42c2f60c43'
998+
999+
# Check for valid objects
1000+
self.assertTrue(repo.is_valid_object(commit_sha))
1001+
self.assertTrue(repo.is_valid_object(blob_sha))
1002+
self.assertTrue(repo.is_valid_object(tree_sha))
1003+
self.assertTrue(repo.is_valid_object(tag_sha))
1004+
1005+
# Check for valid objects of specific type
1006+
self.assertTrue(repo.is_valid_object(commit_sha, 'commit'))
1007+
self.assertTrue(repo.is_valid_object(blob_sha, 'blob'))
1008+
self.assertTrue(repo.is_valid_object(tree_sha, 'tree'))
1009+
self.assertTrue(repo.is_valid_object(tag_sha, 'tag'))
1010+
1011+
# Check for invalid objects
1012+
self.assertFalse(repo.is_valid_object(b'1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a', 'blob'))
1013+
1014+
# Check for invalid objects of specific type
1015+
self.assertFalse(repo.is_valid_object(commit_sha, 'blob'))
1016+
self.assertFalse(repo.is_valid_object(blob_sha, 'commit'))
1017+
self.assertFalse(repo.is_valid_object(tree_sha, 'commit'))
1018+
self.assertFalse(repo.is_valid_object(tag_sha, 'commit'))
1019+
9921020
@with_rw_directory
9931021
def test_git_work_tree_dotgit(self, rw_dir):
9941022
"""Check that we find .git as a worktree file and find the worktree

0 commit comments

Comments
 (0)