diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-06-05 22:17:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-05 22:17:59 +0800 |
commit | 617c09e70bfd54af1c88b4d2c892b8d287747542 (patch) | |
tree | 52042cecc4cab7d4e9426749397b392530703dd7 | |
parent | 01a96b92f7d873cbd531d142813c2be7ab88d5a5 (diff) | |
parent | 464504ce0069758fdb88b348e4a626a265fb3fe3 (diff) | |
download | gitpython-617c09e70bfd54af1c88b4d2c892b8d287747542.tar.gz |
Merge pull request #1267 from bytefluxio/repo.is_valid_object
Adds repo.is_valid_object() check.
-rw-r--r-- | git/repo/base.py | 20 | ||||
-rw-r--r-- | test/test_repo.py | 28 |
2 files changed, 47 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) diff --git a/test/test_repo.py b/test/test_repo.py index 04102b01..8aced94d 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -989,6 +989,34 @@ class TestRepo(TestBase): for i, j in itertools.permutations([c1, 'ffffff', ''], r=2): self.assertRaises(GitCommandError, repo.is_ancestor, i, j) + def test_is_valid_object(self): + repo = self.rorepo + commit_sha = 'f6aa8d1' + blob_sha = '1fbe3e4375' + tree_sha = '960b40fe36' + tag_sha = '42c2f60c43' + + # Check for valid objects + self.assertTrue(repo.is_valid_object(commit_sha)) + self.assertTrue(repo.is_valid_object(blob_sha)) + self.assertTrue(repo.is_valid_object(tree_sha)) + self.assertTrue(repo.is_valid_object(tag_sha)) + + # Check for valid objects of specific type + self.assertTrue(repo.is_valid_object(commit_sha, 'commit')) + self.assertTrue(repo.is_valid_object(blob_sha, 'blob')) + self.assertTrue(repo.is_valid_object(tree_sha, 'tree')) + self.assertTrue(repo.is_valid_object(tag_sha, 'tag')) + + # Check for invalid objects + self.assertFalse(repo.is_valid_object(b'1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a', 'blob')) + + # Check for invalid objects of specific type + self.assertFalse(repo.is_valid_object(commit_sha, 'blob')) + self.assertFalse(repo.is_valid_object(blob_sha, 'commit')) + self.assertFalse(repo.is_valid_object(tree_sha, 'commit')) + self.assertFalse(repo.is_valid_object(tag_sha, 'commit')) + @with_rw_directory def test_git_work_tree_dotgit(self, rw_dir): """Check that we find .git as a worktree file and find the worktree |