summaryrefslogtreecommitdiff
path: root/git/repo/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'git/repo/base.py')
-rw-r--r--git/repo/base.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/git/repo/base.py b/git/repo/base.py
index 55682411..e7b1274b 100644
--- a/git/repo/base.py
+++ b/git/repo/base.py
@@ -3,12 +3,14 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
-
+import binascii
import logging
import os
import re
import warnings
+from gitdb.exc import BadObject
+
from git.cmd import (
Git,
handle_process_output
@@ -618,6 +620,23 @@ class Repo(object):
raise
return True
+ def is_valid_object(self, sha: str, object_type: Union['blob', 'commit', 'tree', 'tag'] = 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(f"Commit hash points to an object of type '{object_info.type.decode()}'. "
+ f"Requested were objects of type '{object_type}'")
+ return False
+ else:
+ return True
+ except BadObject as e:
+ 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)