8000 Added stub for pack writing implementation which should work for pack… · gitpython-developers/gitdb@810d1e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 810d1e3

Browse files
committed
Added stub for pack writing implementation which should work for pack streaming over a transport as well
1 parent 3bcb30f commit 810d1e3

File tree

3 files changed

+50
-19
lines changed

3 file changed

+50
-19
lines changed

gitdb/fun.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,7 @@ def pack_object_header_info(data):
411411
size += (c & 0x7f) << s
412412
s += 7
413413
# END character loop
414-
415-
try:
416-
return (type_id, size, i)
417-
except KeyError:
418-
# invalid object type - we could try to be smart now and decode part
419-
# of the stream to get the info, problem is that we had trouble finding
420-
# the exact start of the content stream
421-
raise BadObjectType(type_id)
422-
# END handle exceptions
414+
return (type_id, size, i)
423415

424416
def msb_size(data, offset=0):
425417
"""

gitdb/pack.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def pack_object_at(data, offset, as_stream):
9898
# REF DELTA
9999
elif type_id == REF_DELTA:
100100
total_rela_offset = data_rela_offset+20
101-
ref_sha = data[data_rela_offset:total_rela_offset]
102-
delta_info = ref_sha
101+
delta_info = data[data_rela_offset:total_rela_offset]
103102
# BASE OBJECT
104103
else:
105104
# assume its a base object
@@ -561,11 +560,10 @@ def _sha_to_index(self, sha):
561560

562561
def _iter_objects(self, as_stream):
563562
"""Iterate over all objects in our index and yield their OInfo or OStream instences"""
564-
indexfile = self._index
563+
_sha = self._index.sha
565564
_object = self._object
566-
for index in xrange(indexfile.size()):
567-
sha = indexfile.sha(index)
568-
yield _object(sha, as_stream, index)
565+
for index in xrange(self._index.size()):
566+
yield _object(_sha(index), as_stream, index)
569567
# END for each index
570568

571569
def _object(self, sha, as_stream, index=-1):
@@ -760,5 +758,20 @@ def collect_streams(self, sha):
760758
return self.collect_streams_at_offset(self._index.offset(self._sha_to_index(sha)))
761759

762760

761+
@classmethod
762+
def create(cls, object_iter, pack_write, index_write=None):
763+
"""
764+
Create a new pack by putting all objects obtained by the object_iterator
765+
into a pack which is written using the pack_write method.
766+
The respective index is produced as well if index_write is not Non.
767+
768+
:param object_iter: iterator yielding odb output objects
769+
:param pack_write: function to receive strings to write into the pack stream
770+
:param indx_write: if not None, the function writes the index file corresponding
771+
to the pack.
772+
:note: The destination of the write functions is up to the user. It could
773+
be a socket, or a file for instance"""
774+
775+
763776

764777
#} END interface

gitdb/test/test_pack.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
from gitdb.fun import delta_types
2626
from gitdb.exc import UnsupportedOperation
2727
from gitdb.util import to_bin_sha
28-
from itertools import izip
28+
from itertools import izip, chain
29+
from nose import SkipTest
30+
2931
import os
32+
import sys
33+
import tempfile
3034

3135

3236
#{ Utilities
@@ -134,7 +138,9 @@ def test_pack(self):
134138
self._assert_pack_file(pack, version, size)
135139
# END for each pack to test
136140

137-
def test_pack_entity(self):
141+
@with_rw_directory
142+
def test_pack_entity(self, rw_dir):
143+
pack_iterators = list();
138144
for packinfo, indexinfo in ( (self.packfile_v2_1, self.packindexfile_v1),
139145
(self.packfile_v2_2, self.packindexfile_v2),
140146
(self.packfile_v2_3_ascii, self.packindexfile_v2_3_ascii)):
@@ -143,6 +149,7 @@ def test_pack_entity(self):
143149
entity = PackEntity(packfile)
144150
assert entity.pack().path() == packfile
145151
assert entity.index().path() == indexfile
152+
pack_iterators.append(entity.stream_iter())
146153

147154
count = 0
148155
for info, stream in izip(entity.info_iter(), entity.stream_iter()):
@@ -174,9 +181,28 @@ def test_pack_entity(self):
174181
# END for each info, stream tuple
175182
assert count == size
176183

177-
# END for each entity
184+
# END for each entity
185+
186+
# pack writing - write all packs into one
187+
# index path can be None
188+
pack_path = tempfile.mktemp('', "pack", rw_dir)
189+
index_path = tempfile.mktemp('', 'index', rw_dir)
190+
for pp, ip in ((pack_path, )*2, (index_path, None)):
191+
pfile = open(pp, 'wb')
192+
ifile = None
193+
if ip:
194+
ifile = open(ip, 'wb')
195+
#END handle ip
196+
197+
PackEntity.create(chain(*pack_iterators), pf 764F ile, ifile)
198+
assert os.path.getsize(pp) > 100
199+
if ip is not None:
200+
assert os.path.getsize(ip) > 100
201+
#END verify files exist
202+
#END for each packpath, indexpath pair
203+
178204

179205
def test_pack_64(self):
180206
# TODO: hex-edit a pack helping us to verify that we can handle 64 byte offsets
181207
# of course without really needing such a huge pack
182-
pass
208+
raise SkipTest()

0 commit comments

Comments
 (0)
0