diff options
-rw-r--r-- | lib/git/objects/base.py | 15 | ||||
-rw-r--r-- | lib/git/objects/utils.py | 1 | ||||
-rw-r--r-- | lib/git/refs.py | 4 | ||||
-rw-r--r-- | lib/git/repo.py | 6 | ||||
-rw-r--r-- | test/git/test_repo.py | 8 |
5 files changed, 19 insertions, 15 deletions
diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py index 07538ada..3b48e066 100644 --- a/lib/git/objects/base.py +++ b/lib/git/objects/base.py @@ -5,17 +5,32 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import os from git.utils import LazyMixin +import utils _assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r" class Object(LazyMixin): """ Implements an Object which may be Blobs, Trees, Commits and Tags + + This Object also serves as a constructor for instances of the correct type:: + + inst = Object(repo,id) """ TYPES = ("blob", "tree", "commit", "tag") __slots__ = ("repo", "id", "size", "data" ) type = None # to be set by subclass + def __new__(cls, repo, id, *args, **kwargs): + if cls is Object: + hexsha, typename, size = repo.git.get_object_header(id) + obj_type = utils.get_object_type_by_name(typename) + inst = super(Object,cls).__new__(obj_type, repo, hexsha, *args, **kwargs) + inst.size = size + return inst + else: + return super(Object,cls).__new__(cls, repo, id, *args, **kwargs) + def __init__(self, repo, id): """ Initialize an object by identifying it by its id. All keyword arguments diff --git a/lib/git/objects/utils.py b/lib/git/objects/utils.py index 9b9e0c52..367ed2b7 100644 --- a/lib/git/objects/utils.py +++ b/lib/git/objects/utils.py @@ -7,7 +7,6 @@ Module for general utility functions """ import re -import commit, tag, blob, tree from git.actor import Actor def get_object_type_by_name(object_type_name): diff --git a/lib/git/refs.py b/lib/git/refs.py index 8ed578ef..a4d7bbb1 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -71,8 +71,8 @@ class Reference(LazyMixin, Iterable): always point to the actual object as it gets re-created on each query """ # have to be dynamic here as we may be a tag which can point to anything - hexsha, typename, size = self.repo.git.get_object_header(self.path) - return get_object_type_by_name(typename)(self.repo, hexsha) + # Our path will be resolved to the hexsha which will be used accordingly + return Object(self.repo, self.path) @classmethod def iter_items(cls, repo, common_path = "refs", **kwargs): diff --git a/lib/git/repo.py b/lib/git/repo.py index 9ac6ce0c..1d24edb7 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -129,10 +129,8 @@ class Repo(object): if rev is None: rev = self.active_branch - # NOTE: currently we are not checking wheter rev really points to a commit - # If not, the system will barf on access of the object, but we don't do that - # here to safe cycles - c = Commit(self, rev) + c = Object(self, rev) + assert c.type == "commit", "Revision %s did not point to a commit, but to %s" % (rev, c) return c def tree(self, ref=None): diff --git a/test/git/test_repo.py b/test/git/test_repo.py index 5869fb6a..cefd1349 100644 --- a/test/git/test_repo.py +++ b/test/git/test_repo.py @@ -65,14 +65,6 @@ class TestRepo(object): assert_true(git.called) - @patch_object(Git, '_call_process') - def test_commit(self, git): - git.return_value = ListProcessAdapter(fixture('rev_list_single')) - - commit = self.repo.commit('4c8124ffcf4039d292442eeccabdeca5af5c5017') - - assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017", commit.id) - @patch_object(Repo, '__init__') @patch_object(Git, '_call_process') def test_init_bare(self, git, repo): |