summaryrefslogtreecommitdiff
path: root/lib/git/base.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-11 11:01:12 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-11 11:01:12 +0200
commit9ee31065abea645cbc2cf3e54b691d5983a228b2 (patch)
tree21e38d54e5a69d2983906f6ac30e6322ed9a7ef1 /lib/git/base.py
parent8430529e1a9fb28d8586d24ee507a8195c370fa5 (diff)
downloadgitpython-9ee31065abea645cbc2cf3e54b691d5983a228b2.tar.gz
Intermediate commit: commit,tree and blob objects now derive from object - test is in place which still fails on purpose. Need to integrate tags which can be objects or just a special form of a ref
Diffstat (limited to 'lib/git/base.py')
-rw-r--r--lib/git/base.py69
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)