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

File tree

6 files changed

+132
-40
lines changed

6 files changed

+132
-40
lines changed

lib/git/base.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# lazy.py
1+
# base.py
22
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
33
#
44
# This module is part of GitPython and is released under
@@ -30,3 +30,70 @@ def __prebake__(self):
3030

3131
def __bake_it__(self):
3232
self.__baked__ = True
33+
34+
35+
class Object(LazyMixin):
36+
"""
37+
Implements an Object which may be Blobs, Trees, Commits and Tags
38+
"""
39+
TYPES = ("blob", "tree", "commit", "tag")
40+
__slots__ = ("repo", "id", "size")
41+
type = None # to be set by subclass
42+
43+
def __init__(self, repo, id, size=None):
44+
"""
45+
Initialize an object by identifying it by its id. All keyword arguments
46+
will be set on demand if None.
47+
48+
``repo``
49+
repository this object is located in
50+
``id``
51+
SHA1 or ref suitable for git-rev-parse
52+
``size``
53+
Size of the object's data in bytes
54+
"""
55+
super(Object,self).__init__()
56+
self.repo = repo
57+
self.id = id
58+
self.size = size
59+
60+
def __bake__(self):
61+
"""
62+
Retrieve object information
63+
"""
64+
self.size = int(self.repo.git.cat_file(self.id, s=True).rstrip())
65+
66+
def __eq__(self, other):
67+
"""
68+
Returns
69+
True if the objects have the same SHA1
70+
"""
71+
return self.id == other.id
72+
73+
def __ne__(self, other):
74+
"""
75+
Returns
76+
True if the objects do not have the same SHA1
77+
"""
78+
return self.id != other.id
79+
80+
def __hash__(self):
81+
"""
82+
Returns
83+
Hash of our id allowing objects to be used in dicts and sets
84+
"""
85+
return hash(self.id)
86+
87+
def __str__(self):
88+
"""
89+
Returns
90+
string of our SHA1 as understood by all git commands
91+
"""
92+
return self.id
93+
94+
def __repr__(self):
95+
"""
96+
Returns
97+
string with pythonic representation of our object
98+
"""
99+
return '<git.%s "%s">' % (self.__class__.__name__, self.id)

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# test_base.py
2+
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
3+
#
4+
# This module is part of GitPython and is released under
5+
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
7+
import time
8+
from test.testlib import *
9+
from git import *
10+
11+
class TestBase(object):
12+
13+
type_tuples = ( ("blob", "8741fc1d09d61f02ffd8cded15ff603eff1ec070"),
14+
("tree", "3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79"),
15+
("commit", "4251bd59fb8e11e40c40548cba38180a9536118c") )
16+
17+
def setup(self):
18+
self.repo = Repo(GIT_REPO)
19+
20+
def test_base(self):
21+
# test interface of base classes
22+
fcreators = (self.repo.blob, self.repo.tree, self.repo.commit )
23+
assert len(fcreators) == len(self.type_tuples)
24+
for fcreator, (typename, hexsha) in zip(fcreators, self.type_tuples):
25+
item = fcreator(hexsha)
26+
assert item.id == hexsha
27+
assert item.type == typename
28+
assert item.size
29+
# END for each object type to create
30+
31+
assert False,"TODO: Test for all types"
32+
33+
def test_tags(self):
34+
# tag refs can point to tag objects or to commits
35+
assert False, "TODO: Tag handling"
36+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy