diff options
Diffstat (limited to 'lib/git/base.py')
-rw-r--r-- | lib/git/base.py | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/lib/git/base.py b/lib/git/base.py index 5e470181..687fb50a 100644 --- a/lib/git/base.py +++ b/lib/git/base.py @@ -1,4 +1,4 @@ -# lazy.py +# base.py # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors # # This module is part of GitPython and is released under @@ -30,3 +30,70 @@ class LazyMixin(object): def __bake_it__(self): self.__baked__ = True + + +class Object(LazyMixin): + """ + Implements an Object which may be Blobs, Trees, Commits and Tags + """ + TYPES = ("blob", "tree", "commit", "tag") + __slots__ = ("repo", "id", "size") + type = None # to be set by subclass + + def __init__(self, repo, id, size=None): + """ + Initialize an object by identifying it by its id. All keyword arguments + will be set on demand if None. + + ``repo`` + repository this object is located in + ``id`` + SHA1 or ref suitable for git-rev-parse + ``size`` + Size of the object's data in bytes + """ + super(Object,self).__init__() + self.repo = repo + self.id = id + self.size = size + + def __bake__(self): + """ + Retrieve object information + """ + self.size = int(self.repo.git.cat_file(self.id, s=True).rstrip()) + + def __eq__(self, other): + """ + Returns + True if the objects have the same SHA1 + """ + return self.id == other.id + + def __ne__(self, other): + """ + Returns + True if the objects do not have the same SHA1 + """ + return self.id != other.id + + def __hash__(self): + """ + Returns + Hash of our id allowing objects to be used in dicts and sets + """ + return hash(self.id) + + def __str__(self): + """ + Returns + string of our SHA1 as understood by all git commands + """ + return self.id + + def __repr__(self): + """ + Returns + string with pythonic representation of our object + """ + return '<git.%s "%s">' % (self.__class__.__name__, self.id) |