Skip to content

Commit 01ab5b9

Browse files
committed
Added test for GitCmdObjectDB in order to verify the partial_to_complete_sha_hex is working as expected with different input ( it wasn't, of course ;) )
1 parent bc31651 commit 01ab5b9

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/git/db.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Module with our own gitdb implementation - it uses the git command"""
2-
from exc import GitCommandError
2+
from exc import (
3+
GitCommandError,
4+
BadObject
5+
)
36

47
from gitdb.base import (
58
OInfo,
@@ -42,7 +45,7 @@ def stream(self, sha):
4245

4346
# { Interface
4447

45-
def partial_to_complete_sha_hex(partial_hexsha):
48+
def partial_to_complete_sha_hex(self, partial_hexsha):
4649
""":return: Full binary 20 byte sha from the given partial hexsha
4750
:raise AmbiguousObjectName:
4851
:raise BadObject:
@@ -51,7 +54,7 @@ def partial_to_complete_sha_hex(partial_hexsha):
5154
try:
5255
hexsha, typename, size = self._git.get_object_header(partial_hexsha)
5356
return hex_to_bin(hexsha)
54-
except GitCommandError:
57+
except (GitCommandError, ValueError):
5558
raise BadObject(partial_hexsha)
5659
# END handle exceptions
5760

test/git/test_db.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# test_repo.py
2+
# Copyright (C) 2008, 2009 Michael Trier ([email protected]) 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+
from test.testlib import *
7+
from git.db import *
8+
from gitdb.util import bin_to_hex
9+
from git.exc import BadObject
10+
import os
11+
12+
class TestDB(TestBase):
13+
14+
def test_base(self):
15+
gdb = GitCmdObjectDB(os.path.join(self.rorepo.git_dir, 'objects'), self.rorepo.git)
16+
17+
# partial to complete - works with everything
18+
hexsha = bin_to_hex(gdb.partial_to_complete_sha_hex("0.1.6"))
19+
assert len(hexsha) == 40
20+
21+
assert bin_to_hex(gdb.partial_to_complete_sha_hex(hexsha[:20])) == hexsha
22+
23+
# fails with BadObject
24+
for invalid_rev in ("0000", "bad/ref", "super bad"):
25+
self.failUnlessRaises(BadObject, gdb.partial_to_complete_sha_hex, invalid_rev)

0 commit comments

Comments
 (0)