summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-11 18:06:18 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-11 18:06:18 +0200
commit7a7eedde7f5d5082f7f207ef76acccd24a6113b1 (patch)
treef17c4534f0af534499503d2111e746a961bbb370
parent101fb1df36f29469ee8f4e0b9e7846d856b87daa (diff)
downloadgitpython-7a7eedde7f5d5082f7f207ef76acccd24a6113b1.tar.gz
put Tree and Blob onto a new base class suitable to deal with IndexObjects
-rw-r--r--lib/git/base.py56
-rw-r--r--lib/git/blob.py51
-rw-r--r--lib/git/tree.py12
3 files changed, 62 insertions, 57 deletions
diff --git a/lib/git/base.py b/lib/git/base.py
index 1f8e085d..22c73491 100644
--- a/lib/git/base.py
+++ b/lib/git/base.py
@@ -40,7 +40,7 @@ class Object(LazyMixin):
Implements an Object which may be Blobs, Trees, Commits and Tags
"""
TYPES = ("blob", "tree", "commit", "tag")
- __slots__ = ("repo", "id", "size")
+ __slots__ = ("repo", "id", "size", "_data_cached" )
type = None # to be set by subclass
def __init__(self, repo, id, size=None):
@@ -61,6 +61,7 @@ class Object(LazyMixin):
self.repo = repo
self.id = id
self.size = size
+ self._data_cached = type(None)
def __bake__(self):
"""
@@ -103,6 +104,20 @@ class Object(LazyMixin):
"""
return '<git.%s "%s">' % (self.__class__.__name__, self.id)
+ @property
+ def data(self):
+ """
+ The binary contents of this object.
+
+ Returns
+ str
+
+ NOTE
+ The data will be cached after the first access.
+ """
+ self._data_cached = ( self._data_cached is not type(None) and self._data_cached ) or self.repo.git.cat_file(self.id, p=True, with_raw_output=True)
+ return self._data_cached
+
@classmethod
def get_type_by_name(cls, object_type_name):
"""
@@ -132,6 +147,45 @@ class Object(LazyMixin):
raise ValueError("Cannot handle unknown object type: %s" % object_type_name)
+class IndexObject(Object):
+ """
+ Base for all objects that can be part of the index file , namely Tree, Blob and
+ SubModule objects
+ """
+ __slots__ = ("path", "mode")
+
+ def __init__(self, repo, id, mode=None, path=None, size = None):
+ """
+ Initialize a newly instanced IndexObject
+ ``repo``
+ is the Repo we are located in
+
+ ``id`` : string
+ is the git object id as hex sha
+
+ ``mode`` : int
+ is the file mode as int, use the stat module to evaluate the infomration
+
+ ``path`` : str
+ is the path to the file in the file system, relative to the git repository root, i.e.
+ file.ext or folder/other.ext
+
+ ``size`` : int
+ size of the object data in bytes
+ """
+ super(IndexObject, self).__init__(repo, id, size)
+ self.mode = mode
+ self.path = path
+
+ @property
+ def basename(self):
+ """
+ Returns
+ The basename of the IndexObject's file path
+ """
+ return os.path.basename(self.path)
+
+
class Ref(object):
"""
Represents a named reference to any object
diff --git a/lib/git/blob.py b/lib/git/blob.py
index c6fb9c1d..b0e47a3c 100644
--- a/lib/git/blob.py
+++ b/lib/git/blob.py
@@ -12,11 +12,12 @@ from actor import Actor
from commit import Commit
import base
-class Blob(base.Object):
+class Blob(base.IndexObject):
"""A Blob encapsulates a git blob object"""
DEFAULT_MIME_TYPE = "text/plain"
type = "blob"
- __slots__ = ("mode", "path", "_data_stored")
+
+ __slots__ = tuple()
# precompiled regex
re_whitespace = re.compile(r'\s+')
@@ -24,44 +25,6 @@ class Blob(base.Object):
re_author_committer_start = re.compile(r'^(author|committer)')
re_tab_full_line = re.compile(r'^\t(.*)$')
- def __init__(self, repo, id, mode=None, path=None):
- """
- Create an unbaked Blob containing just the specified attributes
-
- ``repo``
- is the Repo
-
- ``id``
- is the git object id
-
- ``mode``
- is the file mode
-
- ``path``
- is the path to the file
-
- Returns
- git.Blob
- """
- super(Blob,self).__init__(repo, id)
- self.mode = mode
- self.path = path
- self._data_stored = type(None) # serves as marker to prevent baking in this case
-
- @property
- def data(self):
- """
- The binary contents of this blob.
-
- Returns
- str
-
- NOTE
- The data will be cached after the first access.
- """
- self._data_stored = ( self._data_stored is not type(None) and self._data_stored ) or self.repo.git.cat_file(self.id, p=True, with_raw_output=True)
- return self._data_stored
-
@property
def mime_type(self):
"""
@@ -78,14 +41,6 @@ class Blob(base.Object):
guesses = mimetypes.guess_type(self.path)
return guesses and guesses[0] or self.DEFAULT_MIME_TYPE
- @property
- def basename(self):
- """
- Returns
- The basename of the Blobs file path
- """
- return os.path.basename(self.path)
-
@classmethod
def blame(cls, repo, commit, file):
"""
diff --git a/lib/git/tree.py b/lib/git/tree.py
index 3d4deb16..90f1b72d 100644
--- a/lib/git/tree.py
+++ b/lib/git/tree.py
@@ -8,14 +8,13 @@ import os
import blob
import base
-class Tree(base.Object):
+class Tree(base.IndexObject):
type = "tree"
+ __slots__ = "_contents"
- def __init__(self, repo, id, mode=None, path=None):
- super(Tree, self).__init__(repo, id)
- self.mode = mode
- self.path = path
+ def __init__(self, repo, id, mode=None, path=None, size=None):
+ super(Tree, self).__init__(repo, id, mode, path, size)
self._contents = None
def __bake__(self):
@@ -71,9 +70,6 @@ class Tree(base.Object):
"""
return self.get(file)
- @property
- def basename(self):
- os.path.basename(self.path)
def __repr__(self):
return '<git.Tree "%s">' % self.id