From 385a8c6c1a72dc34f69c5273c1b4c1285cc1d3c5 Mon Sep 17 00:00:00 2001 From: Robert Westman Date: Sat, 5 Jun 2021 12:15:38 +0200 Subject: [PATCH 1/5] Adds repo.is_valid_object check --- git/repo/base.py | 21 ++++++++++++++++++++- test/test_repo.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index 55682411a..e7b1274b1 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 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool: 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) diff --git a/test/test_repo.py b/test/test_repo.py index 04102b013..8aced94d4 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -989,6 +989,34 @@ def test_is_ancestor(self): 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 From ac4fe6efbccc2ad5c2044bf36e34019363018630 Mon Sep 17 00:00:00 2001 From: Robert Westman Date: Sat, 5 Jun 2021 12:22:24 +0200 Subject: [PATCH 2/5] Fixes type check for is_valid_object --- git/repo/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index e7b1274b1..b19503ee2 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -620,7 +620,7 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool: raise return True - def is_valid_object(self, sha: str, object_type: Union['blob', 'commit', 'tree', 'tag'] = None) -> bool: + 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) From 4832aa6bf82e4853f8f426fc06350540e2c8a9e7 Mon Sep 17 00:00:00 2001 From: Robert Westman Date: Sat, 5 Jun 2021 12:34:24 +0200 Subject: [PATCH 3/5] Removes local variable 'e' that is assigned to but never used --- git/repo/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index b19503ee2..d38cf756d 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -633,7 +633,7 @@ def is_valid_object(self, sha: str, object_type: str = None) -> bool: return False else: return True - except BadObject as e: + except BadObject: log.debug("Commit hash is invalid.") return False From fb2461d84f97a72641ef1e878450aeab7cd17241 Mon Sep 17 00:00:00 2001 From: Robert Westman Date: Sat, 5 Jun 2021 12:35:41 +0200 Subject: [PATCH 4/5] Removes unused import --- git/repo/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/git/repo/base.py b/git/repo/base.py index d38cf756d..0db0bd0cd 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -3,7 +3,6 @@ # # 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 From 464504ce0069758fdb88b348e4a626a265fb3fe3 Mon Sep 17 00:00:00 2001 From: Robert Westman Date: Sat, 5 Jun 2021 12:39:44 +0200 Subject: [PATCH 5/5] Removes f-string syntax for p35 compatibility --- git/repo/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git/repo/base.py b/git/repo/base.py index 0db0bd0cd..6cc560310 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -627,8 +627,8 @@ def is_valid_object(self, sha: str, object_type: str = None) -> bool: 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}'") + 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