Skip to content

Commit 39229db

Browse files
committed
Object can now create objects of the proper type in case one attempts to create an object directly - this feature is used in several places now, allowing for additional type-checking
1 parent 9f51eeb commit 39229db

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

lib/git/objects/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,32 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
import os
77
from git.utils import LazyMixin
8+
import utils
89

910
_assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r"
1011

1112
class Object(LazyMixin):
1213
"""
1314
Implements an Object which may be Blobs, Trees, Commits and Tags
15+
16+
This Object also serves as a constructor for instances of the correct type::
17+
18+
inst = Object(repo,id)
1419
"""
1520
TYPES = ("blob", "tree", "commit", "tag")
1621
__slots__ = ("repo", "id", "size", "data" )
1722
type = None # to be set by subclass
1823

24+
def __new__(cls, repo, id, *args, **kwargs):
25+
if cls is Object:
26+
hexsha, typename, size = repo.git.get_object_header(id)
27+
obj_type = utils.get_object_type_by_name(typename)
28+
inst = super(Object,cls).__new__(obj_type, repo, hexsha, *args, **kwargs)
29+
inst.size = size
30+
return inst
31+
else:
32+
return super(Object,cls).__new__(cls, repo, id, *args, **kwargs)
33+
1934
def __init__(self, repo, id):
2035
"""
2136
Initialize an object by identifying it by its id. All keyword arguments

lib/git/objects/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Module for general utility functions
88
"""
99
import re
10-
import commit, tag, blob, tree
1110
from git.actor import Actor
1211

1312
def get_object_type_by_name(object_type_name):

lib/git/refs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def object(self):
7171
always point to the actual object as it gets re-created on each query
7272
"""
7373
# have to be dynamic here as we may be a tag which can point to anything
74-
hexsha, typename, size = self.repo.git.get_object_header(self.path)
75-
return get_object_type_by_name(typename)(self.repo, hexsha)
74+
# Our path will be resolved to the hexsha which will be used accordingly
75+
return Object(self.repo, self.path)
7676

7777
@classmethod
7878
def iter_items(cls, repo, common_path = "refs", **kwargs):

lib/git/repo.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ def commit(self, rev=None):
129129
if rev is None:
130130
rev = self.active_branch
131131

132-
# NOTE: currently we are not checking wheter rev really points to a commit
133-
# If not, the system will barf on access of the object, but we don't do that
134-
# here to safe cycles
135-
c = Commit(self, rev)
132+
c = Object(self, rev)
133+
assert c.type == "commit", "Revision %s did not point to a commit, but to %s" % (rev, c)
136134
return c
137135

138136
def tree(self, ref=None):

test/git/test_repo.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ def test_commits(self, git):
6565

6666
assert_true(git.called)
6767

68-
@patch_object(Git, '_call_process')
69-
def test_commit(self, git):
70-
git.return_value = ListProcessAdapter(fixture('rev_list_single'))
71-
72-
commit = self.repo.commit('4c8124ffcf4039d292442eeccabdeca5af5c5017')
73-
74-
assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017", commit.id)
75-
7668
@patch_object(Repo, '__init__')
7769
@patch_object(Git, '_call_process')
7870
def test_init_bare(self, git, repo):

0 commit comments

Comments
 (0)