Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit e9e0496

Browse files
committed
Moved partial_to_complete_sha_hex from gitdb to compounddb, its fitting there better
1 parent e750ce0 commit e9e0496

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

db/base.py

+59-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
from gitdb.util import (
33
pool,
44
join,
5-
LazyMixin
5+
LazyMixin,
6+
hex_to_bin
67
)
78

8-
from gitdb.exc import BadObject
9+
from gitdb.exc import (
10+
BadObject,
11+
AmbiguousObjectName
12+
)
913

1014
from async import (
1115
ChannelThreadTask
@@ -186,6 +190,22 @@ def update_cache(self, force=False):
186190
# END interface
187191

188192

193+
194+
195+
def _databases_recursive(database, output):
196+
"""Fill output list with database from db, in order. Deals with Loose, Packed
197+
and compound databases."""
198+
if isinstance(database, CompoundDB):
199+
compounds = list()
200+
dbs = database.databases()
201+
output.extend(db for db in dbs if not isinstance(db, CompoundDB))
202+
for cdb in (db for db in dbs if isinstance(db, CompoundDB)):
203+
_databases_recursive(cdb, output)
204+
else:
205+
output.append(database)
206+
# END handle database type
207+
208+
189209
class CompoundDB(ObjectDBR, LazyMixin, CachingDB):
190210
"""A database which delegates calls to sub-databases.
191211
@@ -258,6 +278,43 @@ def update_cache(self, force=False):
258278
# END if is caching db
259279
# END for each database to update
260280
return stat
281+
282+
def partial_to_complete_sha_hex(self, partial_hexsha):
283+
"""
284+
:return: 20 byte binary sha1 from the given less-than-40 byte hexsha
285+
:param partial_hexsha: hexsha with less than 40 byte
286+
:raise AmbiguousObjectName: """
287+
databases = list()
288+
_databases_recursive(self, databases)
289+
290+
if len(partial_hexsha) % 2 != 0:
291+
partial_binsha = hex_to_bin(partial_hexsha + "0")
292+
else:
293+
partial_binsha = hex_to_bin(partial_hexsha)
294+
# END assure successful binary conversion
295+
296+
candidate = None
297+
for db in databases:
298+
full_bin_sha = None
299+
try:
300+
if hasattr(db, 'partial_to_complete_sha_hex'):
301+
full_bin_sha = db.partial_to_complete_sha_hex(partial_hexsha)
302+
else:
303+
full_bin_sha = db.partial_to_complete_sha(partial_binsha)
304+
# END handle database type
305+
except BadObject:
306+
continue
307+
# END ignore bad objects
308+
if full_bin_sha:
309+
if candidate and candidate != full_bin_sha:
310+
raise AmbiguousObjectName(partial_hexsha)
311+
candidate = full_bin_sha
312+
# END handle candidate
313+
# END for each db
314+
if not candidate:
315+
raise BadObject(partial_binsha)
316+
return candidate
317+
261318
#} END interface
262319

263320

db/git.py

-56
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,9 @@
1616
)
1717
import os
1818

19-
from gitdb.util import hex_to_bin
20-
2119
__all__ = ('GitDB', )
2220

2321

24-
def _databases_recursive(database, output):
25-
"""Fill output list with database from db, in order. Deals with Loose, Packed
26-
and compound databases."""
27-
if isinstance(database, CompoundDB):
28-
compounds = list()
29-
dbs = database.databases()
30-
output.extend(db for db in dbs if not isinstance(db, CompoundDB))
31-
for cdb in (db for db in dbs if isinstance(db, CompoundDB)):
32-
_databases_recursive(cdb, output)
33-
else:
34-
output.append(database)
35-
# END handle database type
36-
37-
38-
3922
class GitDB(FileDBBase, ObjectDBW, CompoundDB):
4023
"""A git-style object database, which contains all objects in the 'objects'
4124
subdirectory"""
@@ -96,42 +79,3 @@ def set_ostream(self, ostream):
9679

9780
#} END objectdbw interface
9881

99-
#{ Interface
100-
101-
def partial_to_complete_sha_hex(self, partial_hexsha):
102-
"""
103-
:return: 20 byte binary sha1 from the given less-than-40 byte hexsha
104-
:param partial_hexsha: hexsha with less than 40 byte
105-
:raise AmbiguousObjectName: """
106-
databases = list()
107-
_databases_recursive(self, databases)
108-
109-
if len(partial_hexsha) % 2 != 0:
110-
partial_binsha = hex_to_bin(partial_hexsha + "0")
111-
else:
112-
partial_binsha = hex_to_bin(partial_hexsha)
113-
# END assure successful binary conversion
114-
115-
candidate = None
116-
for db in databases:
117-
full_bin_sha = None
118-
try:
119-
if isinstance(db, LooseObjectDB):
120-
full_bin_sha = db.partial_to_complete_sha_hex(partial_hexsha)
121-
else:
122-
full_bin_sha = db.partial_to_complete_sha(partial_binsha)
123-
# END handle database type
124-
except BadObject:
125-
continue
126-
# END ignore bad objects
127-
if full_bin_sha:
128-
if candidate and candidate != full_bin_sha:
129-
raise AmbiguousObjectName(partial_hexsha)
130-
candidate = full_bin_sha
131-
# END handle candidate
132-
# END for each db
133-
if not candidate:
134-
raise BadObject(partial_binsha)
135-
return candidate
136-
137-
#} END interface

test/db/test_git.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ def test_reading(self):
2020
sha_list = list(gdb.sha_iter())
2121
assert len(sha_list) == gdb.size()
2222

23+
24+
# This is actually a test for compound functionality, but it doesn't
25+
# have a separate test module
2326
# test partial shas
24-
for binsha in sha_list:
25-
assert gdb.partial_to_complete_sha_hex(bin_to_hex(binsha)[:8]) == binsha
27+
# this one as uneven and quite short
28+
assert gdb.partial_to_complete_sha_hex('155b6') == hex_to_bin("155b62a9af0aa7677078331e111d0f7aa6eb4afc")
29+
30+
# mix even/uneven hexshas
31+
for i, binsha in enumerate(sha_list):
32+
assert gdb.partial_to_complete_sha_hex(bin_to_hex(binsha)[:8-(i%2)]) == binsha
2633
# END for each sha
2734

2835
self.failUnlessRaises(BadObject, gdb.partial_to_complete_sha_hex, "0000")

0 commit comments

Comments
 (0)