diff options
Diffstat (limited to 'lib/git')
-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 |
4 files changed, 19 insertions, 7 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): |