summaryrefslogtreecommitdiff
path: root/git/repo/base.py
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2021-06-05 22:17:59 +0800
committerGitHub <noreply@github.com>2021-06-05 22:17:59 +0800
commit617c09e70bfd54af1c88b4d2c892b8d287747542 (patch)
tree52042cecc4cab7d4e9426749397b392530703dd7 /git/repo/base.py
parent01a96b92f7d873cbd531d142813c2be7ab88d5a5 (diff)
parent464504ce0069758fdb88b348e4a626a265fb3fe3 (diff)
downloadgitpython-617c09e70bfd54af1c88b4d2c892b8d287747542.tar.gz
Merge pull request #1267 from bytefluxio/repo.is_valid_object
Adds repo.is_valid_object() check.
Diffstat (limited to 'git/repo/base.py')
-rw-r--r--git/repo/base.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/git/repo/base.py b/git/repo/base.py
index 55682411..6cc56031 100644
--- a/git/repo/base.py
+++ b/git/repo/base.py
@@ -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
@@ -618,6 +619,23 @@ class Repo(object):
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)