Skip to content

Commit 617c09e

Browse files
authored
Merge pull request #1267 from bytefluxio/repo.is_valid_object
Adds repo.is_valid_object() check.
2 parents 01a96b9 + 464504c commit 617c09e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

git/repo/base.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
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-
76
import logging
87
import os
98
import re
109
import warnings
1110

11+
from gitdb.exc import BadObject
12+
1213
from git.cmd import (
1314
Git,
1415
handle_process_output
@@ -618,6 +619,23 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool:
618619
raise
619620
return True
620621

622+
def is_valid_object(self, sha: str, object_type: str = None) -> bool:
623+
try:
624+
complete_sha = self.odb.partial_to_complete_sha_hex(sha)
625+
object_info = self.odb.info(complete_sha)
626+
if object_type:
627+
if object_info.type == object_type.encode():
628+
return True
629+
else:
630+
log.debug("Commit hash points to an object of type '%s'. Requested were objects of type '%s'",
631+
object_info.type.decode(), object_type)
632+
return False
633+
else:
634+
return True
635+
except BadObject:
636+
log.debug("Commit hash is invalid.")
637+
return False
638+
621639
def _get_daemon_export(self) -> bool:
622640
if self.git_dir:
623641
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)