Intermediate commit: commit,tree and blob objects now derive from obj… · gitpython-developers/GitPython@9ee3106 · GitHub
Skip to content

Commit 9ee3106

Browse files
committed
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
1 parent 8430529 commit 9ee3106

6 files changed

Lines changed: 132 additions & 40 deletions

File tree

lib/git/base.py

Lines changed: 68 additions & 1 deletion

lib/git/blob.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import time
1111
from actor import Actor
1212
from commit import Commit
13+
import base
1314

14-
class Blob(object):
15+
class Blob(base.Object):
1516
"""A Blob encapsulates a git blob object"""
1617
DEFAULT_MIME_TYPE = "text/plain"
18+
type = "blob"
19+
__slots__ = ("mode", "path", "_data_stored")
1720

1821
# precompiled regex
1922
re_whitespace = re.compile(r'\s+')
@@ -40,28 +43,10 @@ def __init__(self, repo, id, mode=None, path=None):
4043
Returns
4144
git.Blob
4245
"""
43-
self.repo = repo
44-
self.id = id
46+
super(Blob,self).__init__(repo, id, "blob")
4547
self.mode = mode
4648
self.path = path
47-
48-
self._size = None
49-
self.data_stored = None
50-
51-
@property
52-
def size(self):
53-
"""
54-
The size of this blob in bytes
55-
56-
Returns
57-
int
58-
59-
NOTE
60-
The size will be cached after the first access
61-
"""
62-
if self._size is None:
63-
self._size = int(self.repo.git.cat_file(self.id, s=True).rstrip())
64-
return self._size
49+
self._data_stored = None
6550

6651
@property
6752
def data(self):
@@ -74,8 +59,8 @@ def data(self):
7459
NOTE
7560
The data will be cached after the first access.
7661
"""
77-
self.data_stored = self.data_stored or self.repo.git.cat_file(self.id, p=True, with_raw_output=True)
78-
return self.data_stored
62+
self._data_stored = self._data_stored or self.repo.git.cat_file(self.id, p=True, with_raw_output=True)
63+
return self._data_stored
7964

8065
@property
8166
def mime_type(self):

lib/git/commit.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import time
99

1010
from actor import Actor
11-
from base import LazyMixin
1211
from tree import Tree
1312
import diff
1413
import stats
14+
import base
1515

16-
class Commit(LazyMixin):
16+
class Commit(base.Object):
1717
"""
1818
Wraps a git Commit object.
1919
@@ -23,6 +23,9 @@ class Commit(LazyMixin):
2323
# precompiled regex
2424
re_actor_epoch = re.compile(r'^.+? (.*) (\d+) .*$')
2525

26+
# object configuration
27+
type = "commit"
28+
2629
def __init__(self, repo, id, tree=None, author=None, authored_date=None,
2730
committer=None, committed_date=None, message=None, parents=None):
2831
"""
@@ -58,10 +61,7 @@ def __init__(self, repo, id, tree=None, author=None, authored_date=None,
5861
Returns
5962
git.Commit
6063
"""
61-
LazyMixin.__init__(self)
62-
63-
self.repo = repo
64-
self.id = id
64+
super(Commit,self).__init__(repo, id, "commit")
6565
self.parents = None
6666
self.tree = None
6767
self.author = author
@@ -87,6 +87,7 @@ def __bake__(self):
8787
Called by LazyMixin superclass when the first uninitialized member needs
8888
to be set as it is queried.
8989
"""
90+
super(Commit, self).__bake__()
9091
temp = Commit.find_all(self.repo, self.id, max_count=1)[0]
9192
self.parents = temp.parents
9293
self.tree = temp.tree

lib/git/tag.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
from commit import Commit
88

99
class Tag(object):
10+
"""
11+
Class representing a tag reference which either points to a commit
12+
or to a tag object. In the latter case additional information, like the signature
13+
or the tag-creator, is available.
14+
"""
15+
1016
def __init__(self, name, commit):
1117
"""
1218
Initialize a newly instantiated Tag

lib/git/tree.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

77
import os
8-
from base import LazyMixin
98
import blob
9+
import base
1010

11-
class Tree(LazyMixin):
11+
class Tree(base.Object):
12+
13+
type = "tree"
14+
1215
def __init__(self, repo, id, mode=None, path=None):
13-
LazyMixin.__init__(self)
14-
self.repo = repo
15-
self.id = id
16+
super(Tree, self).__init__(repo, id)
1617
self.mode = mode
1718
self.path = path
1819
self._contents = None
1920

2021
def __bake__(self):
21-
# Ensure the treeish references directly a tree
22-
treeish = self.id
23-
if not treeish.endswith(':'):
24-
treeish = treeish + ':'
25-
2622
# Read the tree contents.
23+
super(Tree, self).__bake__()
2724
self._contents = {}
2825
for line in self.repo.git.ls_tree(self.id).splitlines():
2926
obj = self.content_from_string(self.repo, line)

test/git/test_base.py

Lines changed: 36 additions & 0 deletions

0 commit comments

Comments
 (0)