summaryrefslogtreecommitdiff
path: root/git/repo/base.py
diff options
context:
space:
mode:
authorRobert Westman <robert@byteflux.io>2021-06-05 12:15:38 +0200
committerRobert Westman <robert@byteflux.io>2021-06-05 12:15:38 +0200
commit385a8c6c1a72dc34f69c5273c1b4c1285cc1d3c5 (patch)
tree570d920c2e14fff195fca4529f17c8a598f466c5 /git/repo/base.py
parent01a96b92f7d873cbd531d142813c2be7ab88d5a5 (diff)
downloadgitpython-385a8c6c1a72dc34f69c5273c1b4c1285cc1d3c5.tar.gz
Adds repo.is_valid_object check
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)