test_pack.py 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. # test_pack.py -- Tests for the handling of git packs.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # of the License, or (at your option) any later version of the license.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Tests for Dulwich packs."""
  20. from cStringIO import StringIO
  21. import os
  22. import shutil
  23. import tempfile
  24. import zlib
  25. from dulwich._compat import (
  26. make_sha,
  27. )
  28. from dulwich.errors import (
  29. ChecksumMismatch,
  30. )
  31. from dulwich.file import (
  32. GitFile,
  33. )
  34. from dulwich.object_store import (
  35. MemoryObjectStore,
  36. )
  37. from dulwich.objects import (
  38. Blob,
  39. hex_to_sha,
  40. sha_to_hex,
  41. Commit,
  42. Tree,
  43. Blob,
  44. )
  45. from dulwich.pack import (
  46. OFS_DELTA,
  47. REF_DELTA,
  48. DELTA_TYPES,
  49. MemoryPackIndex,
  50. Pack,
  51. PackData,
  52. apply_delta,
  53. create_delta,
  54. deltify_pack_objects,
  55. load_pack_index,
  56. UnpackedObject,
  57. read_zlib_chunks,
  58. write_pack_header,
  59. write_pack_index_v1,
  60. write_pack_index_v2,
  61. SHA1Writer,
  62. write_pack_object,
  63. write_pack,
  64. unpack_object,
  65. compute_file_sha,
  66. PackStreamReader,
  67. DeltaChainIterator,
  68. )
  69. from dulwich.tests import (
  70. TestCase,
  71. )
  72. from utils import (
  73. make_object,
  74. build_pack,
  75. )
  76. pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
  77. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  78. tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
  79. commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
  80. class PackTests(TestCase):
  81. """Base class for testing packs"""
  82. def setUp(self):
  83. super(PackTests, self).setUp()
  84. self.tempdir = tempfile.mkdtemp()
  85. self.addCleanup(shutil.rmtree, self.tempdir)
  86. datadir = os.path.abspath(os.path.join(os.path.dirname(__file__),
  87. 'data/packs'))
  88. def get_pack_index(self, sha):
  89. """Returns a PackIndex from the datadir with the given sha"""
  90. return load_pack_index(os.path.join(self.datadir, 'pack-%s.idx' % sha))
  91. def get_pack_data(self, sha):
  92. """Returns a PackData object from the datadir with the given sha"""
  93. return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
  94. def get_pack(self, sha):
  95. return Pack(os.path.join(self.datadir, 'pack-%s' % sha))
  96. def assertSucceeds(self, func, *args, **kwargs):
  97. try:
  98. func(*args, **kwargs)
  99. except ChecksumMismatch, e:
  100. self.fail(e)
  101. class PackIndexTests(PackTests):
  102. """Class that tests the index of packfiles"""
  103. def test_object_index(self):
  104. """Tests that the correct object offset is returned from the index."""
  105. p = self.get_pack_index(pack1_sha)
  106. self.assertRaises(KeyError, p.object_index, pack1_sha)
  107. self.assertEqual(p.object_index(a_sha), 178)
  108. self.assertEqual(p.object_index(tree_sha), 138)
  109. self.assertEqual(p.object_index(commit_sha), 12)
  110. def test_index_len(self):
  111. p = self.get_pack_index(pack1_sha)
  112. self.assertEqual(3, len(p))
  113. def test_get_stored_checksum(self):
  114. p = self.get_pack_index(pack1_sha)
  115. self.assertEqual('f2848e2ad16f329ae1c92e3b95e91888daa5bd01',
  116. sha_to_hex(p.get_stored_checksum()))
  117. self.assertEqual('721980e866af9a5f93ad674144e1459b8ba3e7b7',
  118. sha_to_hex(p.get_pack_checksum()))
  119. def test_index_check(self):
  120. p = self.get_pack_index(pack1_sha)
  121. self.assertSucceeds(p.check)
  122. def test_iterentries(self):
  123. p = self.get_pack_index(pack1_sha)
  124. entries = [(sha_to_hex(s), o, c) for s, o, c in p.iterentries()]
  125. self.assertEqual([
  126. ('6f670c0fb53f9463760b7295fbb814e965fb20c8', 178, None),
  127. ('b2a2766a2879c209ab1176e7e778b81ae422eeaa', 138, None),
  128. ('f18faa16531ac570a3fdc8c7ca16682548dafd12', 12, None)
  129. ], entries)
  130. def test_iter(self):
  131. p = self.get_pack_index(pack1_sha)
  132. self.assertEqual(set([tree_sha, commit_sha, a_sha]), set(p))
  133. class TestPackDeltas(TestCase):
  134. test_string1 = 'The answer was flailing in the wind'
  135. test_string2 = 'The answer was falling down the pipe'
  136. test_string3 = 'zzzzz'
  137. test_string_empty = ''
  138. test_string_big = 'Z' * 8192
  139. test_string_huge = 'Z' * 100000
  140. def _test_roundtrip(self, base, target):
  141. self.assertEqual(target,
  142. ''.join(apply_delta(base, create_delta(base, target))))
  143. def test_nochange(self):
  144. self._test_roundtrip(self.test_string1, self.test_string1)
  145. def test_change(self):
  146. self._test_roundtrip(self.test_string1, self.test_string2)
  147. def test_rewrite(self):
  148. self._test_roundtrip(self.test_string1, self.test_string3)
  149. def test_overflow(self):
  150. self._test_roundtrip(self.test_string_empty, self.test_string_big)
  151. def test_overflow_64k(self):
  152. self.skipTest("big strings don't work yet")
  153. self._test_roundtrip(self.test_string_huge, self.test_string_huge)
  154. class TestPackData(PackTests):
  155. """Tests getting the data from the packfile."""
  156. def test_create_pack(self):
  157. p = self.get_pack_data(pack1_sha)
  158. def test_from_file(self):
  159. path = os.path.join(self.datadir, 'pack-%s.pack' % pack1_sha)
  160. PackData.from_file(open(path), os.path.getsize(path))
  161. def test_pack_len(self):
  162. p = self.get_pack_data(pack1_sha)
  163. self.assertEqual(3, len(p))
  164. def test_index_check(self):
  165. p = self.get_pack_data(pack1_sha)
  166. self.assertSucceeds(p.check)
  167. def test_iterobjects(self):
  168. p = self.get_pack_data(pack1_sha)
  169. commit_data = ('tree b2a2766a2879c209ab1176e7e778b81ae422eeaa\n'
  170. 'author James Westby <jw+debian@jameswestby.net> '
  171. '1174945067 +0100\n'
  172. 'committer James Westby <jw+debian@jameswestby.net> '
  173. '1174945067 +0100\n'
  174. '\n'
  175. 'Test commit\n')
  176. blob_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  177. tree_data = '100644 a\0%s' % hex_to_sha(blob_sha)
  178. actual = []
  179. for offset, type_num, chunks, crc32 in p.iterobjects():
  180. actual.append((offset, type_num, ''.join(chunks), crc32))
  181. self.assertEqual([
  182. (12, 1, commit_data, 3775879613L),
  183. (138, 2, tree_data, 912998690L),
  184. (178, 3, 'test 1\n', 1373561701L)
  185. ], actual)
  186. def test_iterentries(self):
  187. p = self.get_pack_data(pack1_sha)
  188. entries = set((sha_to_hex(s), o, c) for s, o, c in p.iterentries())
  189. self.assertEqual(set([
  190. ('6f670c0fb53f9463760b7295fbb814e965fb20c8', 178, 1373561701L),
  191. ('b2a2766a2879c209ab1176e7e778b81ae422eeaa', 138, 912998690L),
  192. ('f18faa16531ac570a3fdc8c7ca16682548dafd12', 12, 3775879613L),
  193. ]), entries)
  194. def test_create_index_v1(self):
  195. p = self.get_pack_data(pack1_sha)
  196. filename = os.path.join(self.tempdir, 'v1test.idx')
  197. p.create_index_v1(filename)
  198. idx1 = load_pack_index(filename)
  199. idx2 = self.get_pack_index(pack1_sha)
  200. self.assertEqual(idx1, idx2)
  201. def test_create_index_v2(self):
  202. p = self.get_pack_data(pack1_sha)
  203. filename = os.path.join(self.tempdir, 'v2test.idx')
  204. p.create_index_v2(filename)
  205. idx1 = load_pack_index(filename)
  206. idx2 = self.get_pack_index(pack1_sha)
  207. self.assertEqual(idx1, idx2)
  208. def test_compute_file_sha(self):
  209. f = StringIO('abcd1234wxyz')
  210. self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
  211. compute_file_sha(f).hexdigest())
  212. self.assertEqual(make_sha('abcd1234wxyz').hexdigest(),
  213. compute_file_sha(f, buffer_size=5).hexdigest())
  214. self.assertEqual(make_sha('abcd1234').hexdigest(),
  215. compute_file_sha(f, end_ofs=-4).hexdigest())
  216. self.assertEqual(make_sha('1234wxyz').hexdigest(),
  217. compute_file_sha(f, start_ofs=4).hexdigest())
  218. self.assertEqual(
  219. make_sha('1234').hexdigest(),
  220. compute_file_sha(f, start_ofs=4, end_ofs=-4).hexdigest())
  221. class TestPack(PackTests):
  222. def test_len(self):
  223. p = self.get_pack(pack1_sha)
  224. self.assertEqual(3, len(p))
  225. def test_contains(self):
  226. p = self.get_pack(pack1_sha)
  227. self.assertTrue(tree_sha in p)
  228. def test_get(self):
  229. p = self.get_pack(pack1_sha)
  230. self.assertEqual(type(p[tree_sha]), Tree)
  231. def test_iter(self):
  232. p = self.get_pack(pack1_sha)
  233. self.assertEqual(set([tree_sha, commit_sha, a_sha]), set(p))
  234. def test_iterobjects(self):
  235. p = self.get_pack(pack1_sha)
  236. expected = set([p[s] for s in [commit_sha, tree_sha, a_sha]])
  237. self.assertEqual(expected, set(list(p.iterobjects())))
  238. def test_pack_tuples(self):
  239. p = self.get_pack(pack1_sha)
  240. tuples = p.pack_tuples()
  241. expected = set([(p[s], None) for s in [commit_sha, tree_sha, a_sha]])
  242. self.assertEqual(expected, set(list(tuples)))
  243. self.assertEqual(expected, set(list(tuples)))
  244. self.assertEqual(3, len(tuples))
  245. def test_get_object_at(self):
  246. """Tests random access for non-delta objects"""
  247. p = self.get_pack(pack1_sha)
  248. obj = p[a_sha]
  249. self.assertEqual(obj.type_name, 'blob')
  250. self.assertEqual(obj.sha().hexdigest(), a_sha)
  251. obj = p[tree_sha]
  252. self.assertEqual(obj.type_name, 'tree')
  253. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  254. obj = p[commit_sha]
  255. self.assertEqual(obj.type_name, 'commit')
  256. self.assertEqual(obj.sha().hexdigest(), commit_sha)
  257. def test_copy(self):
  258. origpack = self.get_pack(pack1_sha)
  259. try:
  260. self.assertSucceeds(origpack.index.check)
  261. basename = os.path.join(self.tempdir, 'Elch')
  262. write_pack(basename, origpack.pack_tuples())
  263. newpack = Pack(basename)
  264. try:
  265. self.assertEqual(origpack, newpack)
  266. self.assertSucceeds(newpack.index.check)
  267. self.assertEqual(origpack.name(), newpack.name())
  268. self.assertEqual(origpack.index.get_pack_checksum(),
  269. newpack.index.get_pack_checksum())
  270. wrong_version = origpack.index.version != newpack.index.version
  271. orig_checksum = origpack.index.get_stored_checksum()
  272. new_checksum = newpack.index.get_stored_checksum()
  273. self.assertTrue(wrong_version or orig_checksum == new_checksum)
  274. finally:
  275. newpack.close()
  276. finally:
  277. origpack.close()
  278. def test_commit_obj(self):
  279. p = self.get_pack(pack1_sha)
  280. commit = p[commit_sha]
  281. self.assertEqual('James Westby <jw+debian@jameswestby.net>',
  282. commit.author)
  283. self.assertEqual([], commit.parents)
  284. def _copy_pack(self, origpack):
  285. basename = os.path.join(self.tempdir, 'somepack')
  286. write_pack(basename, origpack.pack_tuples())
  287. return Pack(basename)
  288. def test_keep_no_message(self):
  289. p = self.get_pack(pack1_sha)
  290. p = self._copy_pack(p)
  291. keepfile_name = p.keep()
  292. # file should exist
  293. self.assertTrue(os.path.exists(keepfile_name))
  294. f = open(keepfile_name, 'r')
  295. try:
  296. buf = f.read()
  297. self.assertEqual('', buf)
  298. finally:
  299. f.close()
  300. def test_keep_message(self):
  301. p = self.get_pack(pack1_sha)
  302. p = self._copy_pack(p)
  303. msg = 'some message'
  304. keepfile_name = p.keep(msg)
  305. # file should exist
  306. self.assertTrue(os.path.exists(keepfile_name))
  307. # and contain the right message, with a linefeed
  308. f = open(keepfile_name, 'r')
  309. try:
  310. buf = f.read()
  311. self.assertEqual(msg + '\n', buf)
  312. finally:
  313. f.close()
  314. def test_name(self):
  315. p = self.get_pack(pack1_sha)
  316. self.assertEqual(pack1_sha, p.name())
  317. def test_length_mismatch(self):
  318. data = self.get_pack_data(pack1_sha)
  319. index = self.get_pack_index(pack1_sha)
  320. Pack.from_objects(data, index).check_length_and_checksum()
  321. data._file.seek(12)
  322. bad_file = StringIO()
  323. write_pack_header(bad_file, 9999)
  324. bad_file.write(data._file.read())
  325. bad_file = StringIO(bad_file.getvalue())
  326. bad_data = PackData('', file=bad_file)
  327. bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
  328. self.assertRaises(AssertionError, lambda: bad_pack.data)
  329. self.assertRaises(AssertionError,
  330. lambda: bad_pack.check_length_and_checksum())
  331. def test_checksum_mismatch(self):
  332. data = self.get_pack_data(pack1_sha)
  333. index = self.get_pack_index(pack1_sha)
  334. Pack.from_objects(data, index).check_length_and_checksum()
  335. data._file.seek(0)
  336. bad_file = StringIO(data._file.read()[:-20] + ('\xff' * 20))
  337. bad_data = PackData('', file=bad_file)
  338. bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
  339. self.assertRaises(ChecksumMismatch, lambda: bad_pack.data)
  340. self.assertRaises(ChecksumMismatch, lambda:
  341. bad_pack.check_length_and_checksum())
  342. def test_iterobjects(self):
  343. p = self.get_pack(pack1_sha)
  344. objs = dict((o.id, o) for o in p.iterobjects())
  345. self.assertEqual(3, len(objs))
  346. self.assertEqual(sorted(objs), sorted(p.index))
  347. self.assertTrue(isinstance(objs[a_sha], Blob))
  348. self.assertTrue(isinstance(objs[tree_sha], Tree))
  349. self.assertTrue(isinstance(objs[commit_sha], Commit))
  350. class TestThinPack(PackTests):
  351. def setUp(self):
  352. super(TestThinPack, self).setUp()
  353. self.store = MemoryObjectStore()
  354. self.blobs = {}
  355. for blob in ('foo', 'bar', 'foo1234', 'bar2468'):
  356. self.blobs[blob] = make_object(Blob, data=blob)
  357. self.store.add_object(self.blobs['foo'])
  358. self.store.add_object(self.blobs['bar'])
  359. # Build a thin pack. 'foo' is as an external reference, 'bar' an
  360. # internal reference.
  361. self.pack_dir = tempfile.mkdtemp()
  362. self.addCleanup(shutil.rmtree, self.pack_dir)
  363. self.pack_prefix = os.path.join(self.pack_dir, 'pack')
  364. f = open(self.pack_prefix + '.pack', 'wb')
  365. try:
  366. build_pack(f, [
  367. (REF_DELTA, (self.blobs['foo'].id, 'foo1234')),
  368. (Blob.type_num, 'bar'),
  369. (REF_DELTA, (self.blobs['bar'].id, 'bar2468'))],
  370. store=self.store)
  371. finally:
  372. f.close()
  373. # Index the new pack.
  374. pack = self.make_pack(True)
  375. data = PackData(pack._data_path)
  376. data.pack = pack
  377. data.create_index(self.pack_prefix + '.idx')
  378. del self.store[self.blobs['bar'].id]
  379. def make_pack(self, resolve_ext_ref):
  380. return Pack(
  381. self.pack_prefix,
  382. resolve_ext_ref=self.store.get_raw if resolve_ext_ref else None)
  383. def test_get_raw(self):
  384. self.assertRaises(
  385. KeyError, self.make_pack(False).get_raw, self.blobs['foo1234'].id)
  386. self.assertEqual(
  387. (3, 'foo1234'),
  388. self.make_pack(True).get_raw(self.blobs['foo1234'].id))
  389. def test_iterobjects(self):
  390. self.assertRaises(KeyError, list, self.make_pack(False).iterobjects())
  391. self.assertEqual(
  392. sorted([self.blobs['foo1234'].id, self.blobs['bar'].id,
  393. self.blobs['bar2468'].id]),
  394. sorted(o.id for o in self.make_pack(True).iterobjects()))
  395. class WritePackTests(TestCase):
  396. def test_write_pack_header(self):
  397. f = StringIO()
  398. write_pack_header(f, 42)
  399. self.assertEqual('PACK\x00\x00\x00\x02\x00\x00\x00*',
  400. f.getvalue())
  401. def test_write_pack_object(self):
  402. f = StringIO()
  403. f.write('header')
  404. offset = f.tell()
  405. crc32 = write_pack_object(f, Blob.type_num, 'blob')
  406. self.assertEqual(crc32, zlib.crc32(f.getvalue()[6:]) & 0xffffffff)
  407. f.write('x') # unpack_object needs extra trailing data.
  408. f.seek(offset)
  409. comp_len = len(f.getvalue()) - offset - 1
  410. unpacked, unused = unpack_object(f.read, compute_crc32=True)
  411. self.assertEqual(Blob.type_num, unpacked.pack_type_num)
  412. self.assertEqual(Blob.type_num, unpacked.obj_type_num)
  413. self.assertEqual(['blob'], unpacked.decomp_chunks)
  414. self.assertEqual(crc32, unpacked.crc32)
  415. self.assertEqual('x', unused)
  416. def test_write_pack_object_sha(self):
  417. f = StringIO()
  418. f.write('header')
  419. offset = f.tell()
  420. sha_a = make_sha('foo')
  421. sha_b = sha_a.copy()
  422. write_pack_object(f, Blob.type_num, 'blob', sha=sha_a)
  423. self.assertNotEqual(sha_a.digest(), sha_b.digest())
  424. sha_b.update(f.getvalue()[offset:])
  425. self.assertEqual(sha_a.digest(), sha_b.digest())
  426. pack_checksum = hex_to_sha('721980e866af9a5f93ad674144e1459b8ba3e7b7')
  427. class BaseTestPackIndexWriting(object):
  428. def assertSucceeds(self, func, *args, **kwargs):
  429. try:
  430. func(*args, **kwargs)
  431. except ChecksumMismatch, e:
  432. self.fail(e)
  433. def index(self, filename, entries, pack_checksum):
  434. raise NotImplementedError(self.index)
  435. def test_empty(self):
  436. idx = self.index('empty.idx', [], pack_checksum)
  437. self.assertEqual(idx.get_pack_checksum(), pack_checksum)
  438. self.assertEqual(0, len(idx))
  439. def test_large(self):
  440. entry1_sha = hex_to_sha('4e6388232ec39792661e2e75db8fb117fc869ce6')
  441. entry2_sha = hex_to_sha('e98f071751bd77f59967bfa671cd2caebdccc9a2')
  442. entries = [(entry1_sha, 0xf2972d0830529b87, 24),
  443. (entry2_sha, (~0xf2972d0830529b87)&(2**64-1), 92)]
  444. if not self._supports_large:
  445. self.assertRaises(TypeError, self.index, 'single.idx',
  446. entries, pack_checksum)
  447. return
  448. idx = self.index('single.idx', entries, pack_checksum)
  449. self.assertEqual(idx.get_pack_checksum(), pack_checksum)
  450. self.assertEqual(2, len(idx))
  451. actual_entries = list(idx.iterentries())
  452. self.assertEqual(len(entries), len(actual_entries))
  453. for mine, actual in zip(entries, actual_entries):
  454. my_sha, my_offset, my_crc = mine
  455. actual_sha, actual_offset, actual_crc = actual
  456. self.assertEqual(my_sha, actual_sha)
  457. self.assertEqual(my_offset, actual_offset)
  458. if self._has_crc32_checksum:
  459. self.assertEqual(my_crc, actual_crc)
  460. else:
  461. self.assertTrue(actual_crc is None)
  462. def test_single(self):
  463. entry_sha = hex_to_sha('6f670c0fb53f9463760b7295fbb814e965fb20c8')
  464. my_entries = [(entry_sha, 178, 42)]
  465. idx = self.index('single.idx', my_entries, pack_checksum)
  466. self.assertEqual(idx.get_pack_checksum(), pack_checksum)
  467. self.assertEqual(1, len(idx))
  468. actual_entries = list(idx.iterentries())
  469. self.assertEqual(len(my_entries), len(actual_entries))
  470. for mine, actual in zip(my_entries, actual_entries):
  471. my_sha, my_offset, my_crc = mine
  472. actual_sha, actual_offset, actual_crc = actual
  473. self.assertEqual(my_sha, actual_sha)
  474. self.assertEqual(my_offset, actual_offset)
  475. if self._has_crc32_checksum:
  476. self.assertEqual(my_crc, actual_crc)
  477. else:
  478. self.assertTrue(actual_crc is None)
  479. class BaseTestFilePackIndexWriting(BaseTestPackIndexWriting):
  480. def setUp(self):
  481. self.tempdir = tempfile.mkdtemp()
  482. def tearDown(self):
  483. shutil.rmtree(self.tempdir)
  484. def index(self, filename, entries, pack_checksum):
  485. path = os.path.join(self.tempdir, filename)
  486. self.writeIndex(path, entries, pack_checksum)
  487. idx = load_pack_index(path)
  488. self.assertSucceeds(idx.check)
  489. self.assertEqual(idx.version, self._expected_version)
  490. return idx
  491. def writeIndex(self, filename, entries, pack_checksum):
  492. # FIXME: Write to StringIO instead rather than hitting disk ?
  493. f = GitFile(filename, "wb")
  494. try:
  495. self._write_fn(f, entries, pack_checksum)
  496. finally:
  497. f.close()
  498. class TestMemoryIndexWriting(TestCase, BaseTestPackIndexWriting):
  499. def setUp(self):
  500. TestCase.setUp(self)
  501. self._has_crc32_checksum = True
  502. self._supports_large = True
  503. def index(self, filename, entries, pack_checksum):
  504. return MemoryPackIndex(entries, pack_checksum)
  505. def tearDown(self):
  506. TestCase.tearDown(self)
  507. class TestPackIndexWritingv1(TestCase, BaseTestFilePackIndexWriting):
  508. def setUp(self):
  509. TestCase.setUp(self)
  510. BaseTestFilePackIndexWriting.setUp(self)
  511. self._has_crc32_checksum = False
  512. self._expected_version = 1
  513. self._supports_large = False
  514. self._write_fn = write_pack_index_v1
  515. def tearDown(self):
  516. TestCase.tearDown(self)
  517. BaseTestFilePackIndexWriting.tearDown(self)
  518. class TestPackIndexWritingv2(TestCase, BaseTestFilePackIndexWriting):
  519. def setUp(self):
  520. TestCase.setUp(self)
  521. BaseTestFilePackIndexWriting.setUp(self)
  522. self._has_crc32_checksum = True
  523. self._supports_large = True
  524. self._expected_version = 2
  525. self._write_fn = write_pack_index_v2
  526. def tearDown(self):
  527. TestCase.tearDown(self)
  528. BaseTestFilePackIndexWriting.tearDown(self)
  529. class ReadZlibTests(TestCase):
  530. decomp = (
  531. 'tree 4ada885c9196b6b6fa08744b5862bf92896fc002\n'
  532. 'parent None\n'
  533. 'author Jelmer Vernooij <jelmer@samba.org> 1228980214 +0000\n'
  534. 'committer Jelmer Vernooij <jelmer@samba.org> 1228980214 +0000\n'
  535. '\n'
  536. "Provide replacement for mmap()'s offset argument.")
  537. comp = zlib.compress(decomp)
  538. extra = 'nextobject'
  539. def setUp(self):
  540. super(ReadZlibTests, self).setUp()
  541. self.read = StringIO(self.comp + self.extra).read
  542. self.unpacked = UnpackedObject(Tree.type_num, None, len(self.decomp), 0)
  543. def test_decompress_size(self):
  544. good_decomp_len = len(self.decomp)
  545. self.unpacked.decomp_len = -1
  546. self.assertRaises(ValueError, read_zlib_chunks, self.read,
  547. self.unpacked)
  548. self.unpacked.decomp_len = good_decomp_len - 1
  549. self.assertRaises(zlib.error, read_zlib_chunks, self.read,
  550. self.unpacked)
  551. self.unpacked.decomp_len = good_decomp_len + 1
  552. self.assertRaises(zlib.error, read_zlib_chunks, self.read,
  553. self.unpacked)
  554. def test_decompress_truncated(self):
  555. read = StringIO(self.comp[:10]).read
  556. self.assertRaises(zlib.error, read_zlib_chunks, read, self.unpacked)
  557. read = StringIO(self.comp).read
  558. self.assertRaises(zlib.error, read_zlib_chunks, read, self.unpacked)
  559. def test_decompress_empty(self):
  560. unpacked = UnpackedObject(Tree.type_num, None, 0, None)
  561. comp = zlib.compress('')
  562. read = StringIO(comp + self.extra).read
  563. unused = read_zlib_chunks(read, unpacked)
  564. self.assertEqual('', ''.join(unpacked.decomp_chunks))
  565. self.assertNotEquals('', unused)
  566. self.assertEqual(self.extra, unused + read())
  567. def test_decompress_no_crc32(self):
  568. self.unpacked.crc32 = None
  569. read_zlib_chunks(self.read, self.unpacked)
  570. self.assertEqual(None, self.unpacked.crc32)
  571. def _do_decompress_test(self, buffer_size, **kwargs):
  572. unused = read_zlib_chunks(self.read, self.unpacked,
  573. buffer_size=buffer_size, **kwargs)
  574. self.assertEqual(self.decomp, ''.join(self.unpacked.decomp_chunks))
  575. self.assertEqual(zlib.crc32(self.comp), self.unpacked.crc32)
  576. self.assertNotEquals('', unused)
  577. self.assertEqual(self.extra, unused + self.read())
  578. def test_simple_decompress(self):
  579. self._do_decompress_test(4096)
  580. self.assertEqual(None, self.unpacked.comp_chunks)
  581. # These buffer sizes are not intended to be realistic, but rather simulate
  582. # larger buffer sizes that may end at various places.
  583. def test_decompress_buffer_size_1(self):
  584. self._do_decompress_test(1)
  585. def test_decompress_buffer_size_2(self):
  586. self._do_decompress_test(2)
  587. def test_decompress_buffer_size_3(self):
  588. self._do_decompress_test(3)
  589. def test_decompress_buffer_size_4(self):
  590. self._do_decompress_test(4)
  591. def test_decompress_include_comp(self):
  592. self._do_decompress_test(4096, include_comp=True)
  593. self.assertEqual(self.comp, ''.join(self.unpacked.comp_chunks))
  594. class DeltifyTests(TestCase):
  595. def test_empty(self):
  596. self.assertEqual([], list(deltify_pack_objects([])))
  597. def test_single(self):
  598. b = Blob.from_string("foo")
  599. self.assertEqual(
  600. [(b.type_num, b.sha().digest(), None, b.as_raw_string())],
  601. list(deltify_pack_objects([(b, "")])))
  602. def test_simple_delta(self):
  603. b1 = Blob.from_string("a" * 101)
  604. b2 = Blob.from_string("a" * 100)
  605. delta = create_delta(b1.as_raw_string(), b2.as_raw_string())
  606. self.assertEqual([
  607. (b1.type_num, b1.sha().digest(), None, b1.as_raw_string()),
  608. (b2.type_num, b2.sha().digest(), b1.sha().digest(), delta)
  609. ],
  610. list(deltify_pack_objects([(b1, ""), (b2, "")])))
  611. class TestPackStreamReader(TestCase):
  612. def test_read_objects_emtpy(self):
  613. f = StringIO()
  614. build_pack(f, [])
  615. reader = PackStreamReader(f.read)
  616. self.assertEqual(0, len(list(reader.read_objects())))
  617. def test_read_objects(self):
  618. f = StringIO()
  619. entries = build_pack(f, [
  620. (Blob.type_num, 'blob'),
  621. (OFS_DELTA, (0, 'blob1')),
  622. ])
  623. reader = PackStreamReader(f.read)
  624. objects = list(reader.read_objects(compute_crc32=True))
  625. self.assertEqual(2, len(objects))
  626. unpacked_blob, unpacked_delta = objects
  627. self.assertEqual(entries[0][0], unpacked_blob.offset)
  628. self.assertEqual(Blob.type_num, unpacked_blob.pack_type_num)
  629. self.assertEqual(Blob.type_num, unpacked_blob.obj_type_num)
  630. self.assertEqual(None, unpacked_blob.delta_base)
  631. self.assertEqual('blob', ''.join(unpacked_blob.decomp_chunks))
  632. self.assertEqual(entries[0][4], unpacked_blob.crc32)
  633. self.assertEqual(entries[1][0], unpacked_delta.offset)
  634. self.assertEqual(OFS_DELTA, unpacked_delta.pack_type_num)
  635. self.assertEqual(None, unpacked_delta.obj_type_num)
  636. self.assertEqual(unpacked_delta.offset - unpacked_blob.offset,
  637. unpacked_delta.delta_base)
  638. delta = create_delta('blob', 'blob1')
  639. self.assertEqual(delta, ''.join(unpacked_delta.decomp_chunks))
  640. self.assertEqual(entries[1][4], unpacked_delta.crc32)
  641. def test_read_objects_buffered(self):
  642. f = StringIO()
  643. build_pack(f, [
  644. (Blob.type_num, 'blob'),
  645. (OFS_DELTA, (0, 'blob1')),
  646. ])
  647. reader = PackStreamReader(f.read, zlib_bufsize=4)
  648. self.assertEqual(2, len(list(reader.read_objects())))
  649. def test_read_objects_empty(self):
  650. reader = PackStreamReader(StringIO().read)
  651. self.assertEqual([], list(reader.read_objects()))
  652. class TestPackIterator(DeltaChainIterator):
  653. _compute_crc32 = True
  654. def __init__(self, *args, **kwargs):
  655. super(TestPackIterator, self).__init__(*args, **kwargs)
  656. self._unpacked_offsets = set()
  657. def _result(self, unpacked):
  658. """Return entries in the same format as build_pack."""
  659. return (unpacked.offset, unpacked.obj_type_num,
  660. ''.join(unpacked.obj_chunks), unpacked.sha(), unpacked.crc32)
  661. def _resolve_object(self, offset, pack_type_num, base_chunks):
  662. assert offset not in self._unpacked_offsets, (
  663. 'Attempted to re-inflate offset %i' % offset)
  664. self._unpacked_offsets.add(offset)
  665. return super(TestPackIterator, self)._resolve_object(
  666. offset, pack_type_num, base_chunks)
  667. class DeltaChainIteratorTests(TestCase):
  668. def setUp(self):
  669. super(DeltaChainIteratorTests, self).setUp()
  670. self.store = MemoryObjectStore()
  671. self.fetched = set()
  672. def store_blobs(self, blobs_data):
  673. blobs = []
  674. for data in blobs_data:
  675. blob = make_object(Blob, data=data)
  676. blobs.append(blob)
  677. self.store.add_object(blob)
  678. return blobs
  679. def get_raw_no_repeat(self, bin_sha):
  680. """Wrapper around store.get_raw that doesn't allow repeat lookups."""
  681. hex_sha = sha_to_hex(bin_sha)
  682. self.assertFalse(hex_sha in self.fetched,
  683. 'Attempted to re-fetch object %s' % hex_sha)
  684. self.fetched.add(hex_sha)
  685. return self.store.get_raw(hex_sha)
  686. def make_pack_iter(self, f, thin=None):
  687. if thin is None:
  688. thin = bool(list(self.store))
  689. resolve_ext_ref = thin and self.get_raw_no_repeat or None
  690. data = PackData('test.pack', file=f)
  691. return TestPackIterator.for_pack_data(
  692. data, resolve_ext_ref=resolve_ext_ref)
  693. def assertEntriesMatch(self, expected_indexes, entries, pack_iter):
  694. expected = [entries[i] for i in expected_indexes]
  695. self.assertEqual(expected, list(pack_iter._walk_all_chains()))
  696. def test_no_deltas(self):
  697. f = StringIO()
  698. entries = build_pack(f, [
  699. (Commit.type_num, 'commit'),
  700. (Blob.type_num, 'blob'),
  701. (Tree.type_num, 'tree'),
  702. ])
  703. self.assertEntriesMatch([0, 1, 2], entries, self.make_pack_iter(f))
  704. def test_ofs_deltas(self):
  705. f = StringIO()
  706. entries = build_pack(f, [
  707. (Blob.type_num, 'blob'),
  708. (OFS_DELTA, (0, 'blob1')),
  709. (OFS_DELTA, (0, 'blob2')),
  710. ])
  711. self.assertEntriesMatch([0, 1, 2], entries, self.make_pack_iter(f))
  712. def test_ofs_deltas_chain(self):
  713. f = StringIO()
  714. entries = build_pack(f, [
  715. (Blob.type_num, 'blob'),
  716. (OFS_DELTA, (0, 'blob1')),
  717. (OFS_DELTA, (1, 'blob2')),
  718. ])
  719. self.assertEntriesMatch([0, 1, 2], entries, self.make_pack_iter(f))
  720. def test_ref_deltas(self):
  721. f = StringIO()
  722. entries = build_pack(f, [
  723. (REF_DELTA, (1, 'blob1')),
  724. (Blob.type_num, ('blob')),
  725. (REF_DELTA, (1, 'blob2')),
  726. ])
  727. self.assertEntriesMatch([1, 0, 2], entries, self.make_pack_iter(f))
  728. def test_ref_deltas_chain(self):
  729. f = StringIO()
  730. entries = build_pack(f, [
  731. (REF_DELTA, (2, 'blob1')),
  732. (Blob.type_num, ('blob')),
  733. (REF_DELTA, (1, 'blob2')),
  734. ])
  735. self.assertEntriesMatch([1, 2, 0], entries, self.make_pack_iter(f))
  736. def test_ofs_and_ref_deltas(self):
  737. # Deltas pending on this offset are popped before deltas depending on
  738. # this ref.
  739. f = StringIO()
  740. entries = build_pack(f, [
  741. (REF_DELTA, (1, 'blob1')),
  742. (Blob.type_num, ('blob')),
  743. (OFS_DELTA, (1, 'blob2')),
  744. ])
  745. self.assertEntriesMatch([1, 2, 0], entries, self.make_pack_iter(f))
  746. def test_mixed_chain(self):
  747. f = StringIO()
  748. entries = build_pack(f, [
  749. (Blob.type_num, 'blob'),
  750. (REF_DELTA, (2, 'blob2')),
  751. (OFS_DELTA, (0, 'blob1')),
  752. (OFS_DELTA, (1, 'blob3')),
  753. (OFS_DELTA, (0, 'bob')),
  754. ])
  755. self.assertEntriesMatch([0, 2, 1, 3, 4], entries,
  756. self.make_pack_iter(f))
  757. def test_long_chain(self):
  758. n = 100
  759. objects_spec = [(Blob.type_num, 'blob')]
  760. for i in xrange(n):
  761. objects_spec.append((OFS_DELTA, (i, 'blob%i' % i)))
  762. f = StringIO()
  763. entries = build_pack(f, objects_spec)
  764. self.assertEntriesMatch(xrange(n + 1), entries, self.make_pack_iter(f))
  765. def test_branchy_chain(self):
  766. n = 100
  767. objects_spec = [(Blob.type_num, 'blob')]
  768. for i in xrange(n):
  769. objects_spec.append((OFS_DELTA, (0, 'blob%i' % i)))
  770. f = StringIO()
  771. entries = build_pack(f, objects_spec)
  772. self.assertEntriesMatch(xrange(n + 1), entries, self.make_pack_iter(f))
  773. def test_ext_ref(self):
  774. blob, = self.store_blobs(['blob'])
  775. f = StringIO()
  776. entries = build_pack(f, [(REF_DELTA, (blob.id, 'blob1'))],
  777. store=self.store)
  778. pack_iter = self.make_pack_iter(f)
  779. self.assertEntriesMatch([0], entries, pack_iter)
  780. self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
  781. def test_ext_ref_chain(self):
  782. blob, = self.store_blobs(['blob'])
  783. f = StringIO()
  784. entries = build_pack(f, [
  785. (REF_DELTA, (1, 'blob2')),
  786. (REF_DELTA, (blob.id, 'blob1')),
  787. ], store=self.store)
  788. pack_iter = self.make_pack_iter(f)
  789. self.assertEntriesMatch([1, 0], entries, pack_iter)
  790. self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
  791. def test_ext_ref_multiple_times(self):
  792. blob, = self.store_blobs(['blob'])
  793. f = StringIO()
  794. entries = build_pack(f, [
  795. (REF_DELTA, (blob.id, 'blob1')),
  796. (REF_DELTA, (blob.id, 'blob2')),
  797. ], store=self.store)
  798. pack_iter = self.make_pack_iter(f)
  799. self.assertEntriesMatch([0, 1], entries, pack_iter)
  800. self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
  801. def test_multiple_ext_refs(self):
  802. b1, b2 = self.store_blobs(['foo', 'bar'])
  803. f = StringIO()
  804. entries = build_pack(f, [
  805. (REF_DELTA, (b1.id, 'foo1')),
  806. (REF_DELTA, (b2.id, 'bar2')),
  807. ], store=self.store)
  808. pack_iter = self.make_pack_iter(f)
  809. self.assertEntriesMatch([0, 1], entries, pack_iter)
  810. self.assertEqual([hex_to_sha(b1.id), hex_to_sha(b2.id)],
  811. pack_iter.ext_refs())
  812. def test_bad_ext_ref_non_thin_pack(self):
  813. blob, = self.store_blobs(['blob'])
  814. f = StringIO()
  815. entries = build_pack(f, [(REF_DELTA, (blob.id, 'blob1'))],
  816. store=self.store)
  817. pack_iter = self.make_pack_iter(f, thin=False)
  818. try:
  819. list(pack_iter._walk_all_chains())
  820. self.fail()
  821. except KeyError, e:
  822. self.assertEqual(([blob.id],), e.args)
  823. def test_bad_ext_ref_thin_pack(self):
  824. b1, b2, b3 = self.store_blobs(['foo', 'bar', 'baz'])
  825. f = StringIO()
  826. entries = build_pack(f, [
  827. (REF_DELTA, (1, 'foo99')),
  828. (REF_DELTA, (b1.id, 'foo1')),
  829. (REF_DELTA, (b2.id, 'bar2')),
  830. (REF_DELTA, (b3.id, 'baz3')),
  831. ], store=self.store)
  832. del self.store[b2.id]
  833. del self.store[b3.id]
  834. pack_iter = self.make_pack_iter(f)
  835. try:
  836. list(pack_iter._walk_all_chains())
  837. self.fail()
  838. except KeyError, e:
  839. self.assertEqual((sorted([b2.id, b3.id]),), e.args)