def setUp(self): super(TestThinPack, self).setUp() self.store = MemoryObjectStore() self.blobs = {} for blob in (b'foo', b'bar', b'foo1234', b'bar2468'): self.blobs[blob] = make_object(Blob, data=blob) self.store.add_object(self.blobs[b'foo']) self.store.add_object(self.blobs[b'bar']) # Build a thin pack. 'foo' is as an external reference, 'bar' an # internal reference. self.pack_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.pack_dir) self.pack_prefix = os.path.join(self.pack_dir, 'pack') with open(self.pack_prefix + '.pack', 'wb') as f: build_pack(f, [(REF_DELTA, (self.blobs[b'foo'].id, b'foo1234')), (Blob.type_num, b'bar'), (REF_DELTA, (self.blobs[b'bar'].id, b'bar2468'))], store=self.store) # Index the new pack. with self.make_pack(True) as pack: with PackData(pack._data_path) as data: data.pack = pack data.create_index(self.pack_prefix + '.idx') del self.store[self.blobs[b'bar'].id]
def test_read_objects_buffered(self): f = BytesIO() build_pack(f, [ (Blob.type_num, b'blob'), (OFS_DELTA, (0, b'blob1')), ]) reader = PackStreamReader(f.read, zlib_bufsize=4) self.assertEqual(2, len(list(reader.read_objects())))
def test_bad_ext_ref_non_thin_pack(self): blob, = self.store_blobs([b'blob']) f = BytesIO() build_pack(f, [(REF_DELTA, (blob.id, b'blob1'))], store=self.store) pack_iter = self.make_pack_iter(f, thin=False) try: list(pack_iter._walk_all_chains()) self.fail() except KeyError as e: self.assertEqual(([blob.id], ), e.args)
def test_read_objects(self): f = BytesIO() entries = build_pack(f, [ (Blob.type_num, b'blob'), (OFS_DELTA, (0, b'blob1')), ]) reader = PackStreamReader(f.read) objects = list(reader.read_objects(compute_crc32=True)) self.assertEqual(2, len(objects)) unpacked_blob, unpacked_delta = objects self.assertEqual(entries[0][0], unpacked_blob.offset) self.assertEqual(Blob.type_num, unpacked_blob.pack_type_num) self.assertEqual(Blob.type_num, unpacked_blob.obj_type_num) self.assertEqual(None, unpacked_blob.delta_base) self.assertEqual(b'blob', b''.join(unpacked_blob.decomp_chunks)) self.assertEqual(entries[0][4], unpacked_blob.crc32) self.assertEqual(entries[1][0], unpacked_delta.offset) self.assertEqual(OFS_DELTA, unpacked_delta.pack_type_num) self.assertEqual(None, unpacked_delta.obj_type_num) self.assertEqual(unpacked_delta.offset - unpacked_blob.offset, unpacked_delta.delta_base) delta = create_delta(b'blob', b'blob1') self.assertEqual(delta, b''.join(unpacked_delta.decomp_chunks)) self.assertEqual(entries[1][4], unpacked_delta.crc32)
def test_ref_deltas_chain(self): f = BytesIO() entries = build_pack(f, [ (REF_DELTA, (2, b'blob1')), (Blob.type_num, (b'blob')), (REF_DELTA, (1, b'blob2')), ]) self.assertEntriesMatch([1, 2, 0], entries, self.make_pack_iter(f))
def test_ofs_deltas_chain(self): f = BytesIO() entries = build_pack(f, [ (Blob.type_num, b'blob'), (OFS_DELTA, (0, b'blob1')), (OFS_DELTA, (1, b'blob2')), ]) self.assertEntriesMatch([0, 1, 2], entries, self.make_pack_iter(f))
def test_no_deltas(self): f = BytesIO() entries = build_pack(f, [ (Commit.type_num, b'commit'), (Blob.type_num, b'blob'), (Tree.type_num, b'tree'), ]) self.assertEntriesMatch([0, 1, 2], entries, self.make_pack_iter(f))
def test_ext_ref(self): blob, = self.store_blobs([b'blob']) f = BytesIO() entries = build_pack(f, [(REF_DELTA, (blob.id, b'blob1'))], store=self.store) pack_iter = self.make_pack_iter(f) self.assertEntriesMatch([0], entries, pack_iter) self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
def test_branchy_chain(self): n = 100 objects_spec = [(Blob.type_num, b'blob')] for i in range(n): objects_spec.append( (OFS_DELTA, (0, b'blob' + str(i).encode('ascii')))) f = BytesIO() entries = build_pack(f, objects_spec) self.assertEntriesMatch(range(n + 1), entries, self.make_pack_iter(f))
def test_bad_ext_ref_thin_pack(self): b1, b2, b3 = self.store_blobs([b'foo', b'bar', b'baz']) f = BytesIO() build_pack(f, [ (REF_DELTA, (1, b'foo99')), (REF_DELTA, (b1.id, b'foo1')), (REF_DELTA, (b2.id, b'bar2')), (REF_DELTA, (b3.id, b'baz3')), ], store=self.store) del self.store[b2.id] del self.store[b3.id] pack_iter = self.make_pack_iter(f) try: list(pack_iter._walk_all_chains()) self.fail() except KeyError as e: self.assertEqual((sorted([b2.id, b3.id]), ), (sorted(e.args[0]), ))
def test_ofs_and_ref_deltas(self): # Deltas pending on this offset are popped before deltas depending on # this ref. f = BytesIO() entries = build_pack(f, [ (REF_DELTA, (1, b'blob1')), (Blob.type_num, (b'blob')), (OFS_DELTA, (1, b'blob2')), ]) self.assertEntriesMatch([1, 2, 0], entries, self.make_pack_iter(f))
def test_mixed_chain(self): f = BytesIO() entries = build_pack(f, [ (Blob.type_num, b'blob'), (REF_DELTA, (2, b'blob2')), (OFS_DELTA, (0, b'blob1')), (OFS_DELTA, (1, b'blob3')), (OFS_DELTA, (0, b'bob')), ]) self.assertEntriesMatch([0, 2, 4, 1, 3], entries, self.make_pack_iter(f))
def test_multiple_ext_refs(self): b1, b2 = self.store_blobs([b'foo', b'bar']) f = BytesIO() entries = build_pack(f, [ (REF_DELTA, (b1.id, b'foo1')), (REF_DELTA, (b2.id, b'bar2')), ], store=self.store) pack_iter = self.make_pack_iter(f) self.assertEntriesMatch([0, 1], entries, pack_iter) self.assertEqual( [hex_to_sha(b1.id), hex_to_sha(b2.id)], pack_iter.ext_refs())
def test_ext_ref_chain_degenerate(self): # Test a degenerate case where the sender is sending a REF_DELTA # object that expands to an object already in the repository. blob, = self.store_blobs([b'blob']) blob2, = self.store_blobs([b'blob2']) assert blob.id < blob2.id f = BytesIO() entries = build_pack(f, [ (REF_DELTA, (blob.id, b'blob2')), (REF_DELTA, (0, b'blob3')), ], store=self.store) pack_iter = self.make_pack_iter(f) self.assertEntriesMatch([0, 1], entries, pack_iter) self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
def test_read_objects_emtpy(self): f = BytesIO() build_pack(f, []) reader = PackStreamReader(f.read) self.assertEqual(0, len(list(reader.read_objects())))