diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-10-15 15:42:00 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-10-15 15:42:00 +0200 |
commit | 39229db70e71a6be275dafb3dd9468930e40ae48 (patch) | |
tree | c631f2b69c7f5874834214cf32970c48f05b5613 /lib/git/objects/base.py | |
parent | 9f51eeb2f9a1efe22e45f7a1f7b963100f2f8e6b (diff) | |
download | gitpython-39229db70e71a6be275dafb3dd9468930e40ae48.tar.gz |
Object can now create objects of the proper type in case one attempts to create an object directly - this feature is used in several places now, allowing for additional type-checking
Diffstat (limited to 'lib/git/objects/base.py')
-rw-r--r-- | lib/git/objects/base.py | 15 |
1 files changed, 15 insertions, 0 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 |