Skip to content

Commit

Permalink
Objects: Constructor now manually checks and sets the input arguments…
Browse files Browse the repository at this point in the history
… to the local cache - previously a procedural approach was used, which was less code, but slower too. Especially in case of CommitObjects unrolling the loop manually makes a difference.

Submodule: Implemented query methods and did a bit of testing. More is to come, but the test works for now. As special addition, the submodule implementation uses the section name as submodule ID even though it seems to be just the path. This allows to make renames easier
  • Loading branch information
Byron committed Nov 15, 2010
1 parent 4d36f8f commit 00ce31a
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 52 deletions.
16 changes: 4 additions & 12 deletions lib/git/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ def new_from_sha(cls, repo, sha1):
inst.size = oinfo.size
return inst

def _set_self_from_args_(self, args_dict):
"""Initialize attributes on self from the given dict that was retrieved
from locals() in the calling method.
Will only set an attribute on self if the corresponding value in args_dict
is not None"""
for attr, val in args_dict.items():
if attr != "self" and val is not None:
setattr( self, attr, val )
# END set all non-None attributes

def _set_cache_(self, attr):
"""Retrieve object information"""
if attr == "size":
Expand Down Expand Up @@ -140,7 +129,10 @@ def __init__(self, repo, binsha, mode=None, path=None):
Path may not be set of the index object has been created directly as it cannot
be retrieved without knowing the parent tree."""
super(IndexObject, self).__init__(repo, binsha)
self._set_self_from_args_(locals())
if mode is not None:
self.mode = mode
if path is not None:
self.path = path

def __hash__(self):
""":return:
Expand Down
25 changes: 22 additions & 3 deletions lib/git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,26 @@ def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, aut
super(Commit,self).__init__(repo, binsha)
if tree is not None:
assert isinstance(tree, Tree), "Tree needs to be a Tree instance, was %s" % type(tree)
self._set_self_from_args_(locals())
if tree is not None:
self.tree = tree
if author is not None:
self.author = author
if authored_date is not None:
self.authored_date = authored_date
if author_tz_offset is not None:
self.author_tz_offset = author_tz_offset
if committer is not None:
self.committer = committer
if committed_date is not None:
self.committed_date = committed_date
if committer_tz_offset is not None:
self.committer_tz_offset = committer_tz_offset
if message is not None:
self.message = message
if parents is not None:
self.parents = parents
if encoding is not None:
self.encoding = encoding

@classmethod
def _get_intermediate_items(cls, commit):
Expand Down Expand Up @@ -434,7 +453,7 @@ def _deserialize(self, stream):
try:
self.author.name = self.author.name.decode(self.encoding)
except UnicodeDecodeError:
print >> sys.stderr, "Failed to decode author name: %s" % self.author.name
print >> sys.stderr, "Failed to decode author name '%s' using encoding %s" % (self.author.name, self.encoding)
# END handle author's encoding

# a stream from our data simply gives us the plain message
Expand All @@ -443,7 +462,7 @@ def _deserialize(self, stream):
try:
self.message = self.message.decode(self.encoding)
except UnicodeDecodeError:
print >> sys.stderr, "Failed to decode message: %s" % self.message
print >> sys.stderr, "Failed to decode message '%s' using encoding %s" % (self.message, self.encoding)
# END exception handling
return self

Expand Down
Loading

0 comments on commit 00ce31a

Please sign in to comment.