Skip to content

Commit bc31651

Browse files
committed
Adjusted previous object creators to use the rev_parse method directly. rev_parse could be adjusted not to return Objects anymore, providing better performance for those who just want a sha only. On the other hand, the method is high-level and should be convenient to use as well, its a starting point for more usually, hence its unlikely to call it in tight loops
1 parent f068cdc commit bc31651

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

lib/git/index/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ def diff(self, other=diff.Diffable.Index, paths=None, create_patch=False, **kwar
11221122
# item. Handle existing -R flags properly. Transform strings to the object
11231123
# so that we can call diff on it
11241124
if isinstance(other, basestring):
1125-
other = Object.new(self.repo, other)
1125+
other = self.repo.rev_parse(other)
11261126
# END object conversion
11271127

11281128
if isinstance(other, Object):

lib/git/objects/base.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def new(cls, repo, id):
4949
5050
:note: This cannot be a __new__ method as it would always call __init__
5151
with the input id which is not necessarily a binsha."""
52-
hexsha, typename, size = repo.git.get_object_header(id)
53-
inst = get_object_type_by_name(typename)(repo, hex_to_bin(hexsha))
54-
inst.size = size
55-
return inst
52+
return repo.rev_parse(str(id))
5653

5754
@classmethod
5855
def new_from_sha(cls, repo, sha1):

lib/git/refs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def _create(cls, repo, path, resolve, reference, force):
345345
# figure out target data
346346
target = reference
347347
if resolve:
348-
target = Object.new(repo, reference)
348+
target = repo.rev_parse(str(reference))
349349

350350
if not force and isfile(abs_ref_path):
351351
target_data = str(target)

lib/git/remote.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def _from_line(cls, repo, line, fetch_line):
391391
split_token = '...'
392392
if control_character == ' ':
393393
split_token = split_token[:-1]
394-
old_commit = Commit.new(repo, operation.split(split_token)[0])
394+
old_commit = repo.rev_parse(operation.split(split_token)[0])
395395
# END handle refspec
396396
# END reference flag handling
397397

lib/git/repo/base.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,9 @@ def commit(self, rev=None):
323323
:param rev: revision specifier, see git-rev-parse for viable options.
324324
:return: ``git.Commit``"""
325325
if rev is None:
326-
rev = self.active_branch
327-
328-
c = Object.new(self, rev)
329-
assert c.type == "commit", "Revision %s did not point to a commit, but to %s" % (rev, c)
330-
return c
326+
return self.active_branch.commit
327+
else:
328+
return self.rev_parse(str(rev)+"^0")
331329

332330
def iter_trees(self, *args, **kwargs):
333331
""":return: Iterator yielding Tree objects
@@ -348,14 +346,9 @@ def tree(self, rev=None):
348346
it cannot know about its path relative to the repository root and subsequent
349347
operations might have unexpected results."""
350348
if rev is None:
351-
rev = self.active_branch
352-
353-
c = Object.new(self, rev)
354-
if c.type == "commit":
355-
return c.tree
356-
elif c.type == "tree":
357-
return c
358-
raise ValueError( "Revision %s did not point to a treeish, but to %s" % (rev, c))
349+
return self.active_branch.commit.tree
350+
else:
351+
return self.rev_parse(str(rev)+"^{tree}")
359352

360353
def iter_commits(self, rev=None, paths='', **kwargs):
361354
"""A list of Commit objects representing the history of a given ref/commit

test/git/test_repo.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from git import *
88
from git.util import join_path_native
99
from git.exc import BadObject
10-
from gitdb.util import hex_to_bin
10+
from gitdb.util import hex_to_bin, bin_to_hex
1111

1212
import os, sys
1313
import tempfile
@@ -57,7 +57,12 @@ def test_tree_from_revision(self):
5757
assert self.rorepo.tree(tree) == tree
5858

5959
# try from invalid revision that does not exist
60-
self.failUnlessRaises(ValueError, self.rorepo.tree, 'hello world')
60+
self.failUnlessRaises(BadObject, self.rorepo.tree, 'hello world')
61+
62+
def test_commit_from_revision(self):
63+
commit = self.rorepo.commit('0.1.4')
64+
assert commit.type == 'commit'
65+
assert self.rorepo.commit(commit) == commit
6166

6267
def test_commits(self):
6368
mc = 10
@@ -445,7 +450,7 @@ def test_rev_parse(self):
445450
rev_parse = self.rorepo.rev_parse
446451

447452
# try special case: This one failed beforehand
448-
assert self.rorepo.odb.partial_to_complete_sha_hex("33ebe") == hex_to_bin("33ebe7acec14b25c5f84f35a664803fcab2f7781")
453+
assert rev_parse("33ebe").hexsha == "33ebe7acec14b25c5f84f35a664803fcab2f7781"
449454

450455
# start from reference
451456
num_resolved = 0
@@ -507,6 +512,16 @@ def test_rev_parse(self):
507512
assert tag.object == rev_parse('0.1.4%s' % token)
508513
# END handle multiple tokens
509514

515+
# try partial parsing
516+
max_items = 40
517+
for i, binsha in enumerate(self.rorepo.odb.sha_iter()):
518+
assert rev_parse(bin_to_hex(binsha)[:8-(i%2)]).binsha == binsha
519+
if i > max_items:
520+
# this is rather slow currently, as rev_parse returns an object
521+
# which requires accessing packs, it has some additional overhead
522+
break
523+
# END for each binsha in repo
524+
510525
# missing closing brace commit^{tree
511526
self.failUnlessRaises(ValueError, rev_parse, '0.1.4^{tree')
512527

0 commit comments

Comments
 (0)