2
0

test_bitmap.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. # test_bitmap.py -- Tests for bitmap support
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for bitmap support."""
  22. import os
  23. import shutil
  24. import tempfile
  25. import unittest
  26. from io import BytesIO
  27. from dulwich.bitmap import (
  28. BITMAP_OPT_FULL_DAG,
  29. BITMAP_OPT_HASH_CACHE,
  30. BITMAP_OPT_LOOKUP_TABLE,
  31. BITMAP_SIGNATURE,
  32. BITMAP_VERSION,
  33. BitmapEntry,
  34. EWAHBitmap,
  35. PackBitmap,
  36. _encode_ewah_words,
  37. read_bitmap_file,
  38. write_bitmap_file,
  39. )
  40. from dulwich.object_store import BitmapReachability, GraphTraversalReachability
  41. class EWAHCompressionTests(unittest.TestCase):
  42. """Tests for EWAH compression helper functions."""
  43. def test_encode_empty_words(self):
  44. """Test encoding empty word list."""
  45. result = _encode_ewah_words([])
  46. self.assertEqual([], result)
  47. def test_encode_single_literal(self):
  48. """Test encoding single literal word."""
  49. result = _encode_ewah_words([0x123])
  50. # Should be: RLW(0 run, 1 literal) + literal
  51. self.assertEqual(2, len(result))
  52. # RLW bit layout: [literal_words(31)][running_len(32)][running_bit(1)]
  53. # running_bit=0, running_len=0, literal_words=1
  54. expected_rlw = (1 << 33) | (0 << 1) | 0
  55. self.assertEqual(expected_rlw, result[0])
  56. self.assertEqual(0x123, result[1])
  57. def test_encode_zero_run(self):
  58. """Test encoding run of zeros."""
  59. result = _encode_ewah_words([0, 0, 0])
  60. # Should be: RLW(3 zeros, 0 literals)
  61. self.assertEqual(1, len(result))
  62. # RLW bit layout: [literal_words(31)][running_len(32)][running_bit(1)]
  63. # running_bit=0, running_len=3, literal_words=0
  64. expected_rlw = (0 << 33) | (3 << 1) | 0
  65. self.assertEqual(expected_rlw, result[0])
  66. def test_encode_ones_run(self):
  67. """Test encoding run of all-ones."""
  68. result = _encode_ewah_words([0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF])
  69. # Should be: RLW(2 ones, 0 literals)
  70. self.assertEqual(1, len(result))
  71. # RLW bit layout: [literal_words(31)][running_len(32)][running_bit(1)]
  72. # running_bit=1, running_len=2, literal_words=0
  73. expected_rlw = (0 << 33) | (2 << 1) | 1
  74. self.assertEqual(expected_rlw, result[0])
  75. def test_encode_run_followed_by_literals(self):
  76. """Test encoding run followed by literal words."""
  77. result = _encode_ewah_words([0, 0, 0x123, 0x456])
  78. # Should be: RLW(2 zeros, 2 literals) + literals
  79. self.assertEqual(3, len(result))
  80. # RLW bit layout: [literal_words(31)][running_len(32)][running_bit(1)]
  81. # running_bit=0, running_len=2, literal_words=2
  82. expected_rlw = (2 << 33) | (2 << 1) | 0
  83. self.assertEqual(expected_rlw, result[0])
  84. self.assertEqual(0x123, result[1])
  85. self.assertEqual(0x456, result[2])
  86. def test_encode_mixed_pattern(self):
  87. """Test encoding mixed runs and literals."""
  88. result = _encode_ewah_words(
  89. [0, 0, 0x123, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF]
  90. )
  91. # Should be: RLW(2 zeros, 1 literal) + literal + RLW(2 ones, 0 literals)
  92. self.assertEqual(3, len(result))
  93. class EWAHCompatibilityTests(unittest.TestCase):
  94. """Tests for EWAH encode/decode compatibility."""
  95. def test_encode_decode_sparse_bitmap(self):
  96. """Test encoding and decoding a sparse bitmap with runs."""
  97. # Create a bitmap with a pattern that benefits from run-length encoding
  98. # Bits: 0, 1, 128 (has runs of zeros between set bits)
  99. bitmap = EWAHBitmap()
  100. bitmap.add(0)
  101. bitmap.add(1)
  102. bitmap.add(128)
  103. # Encode
  104. encoded = bitmap.encode()
  105. # Decode
  106. bitmap2 = EWAHBitmap(encoded)
  107. # Verify all bits are preserved
  108. self.assertEqual(len(bitmap), len(bitmap2))
  109. self.assertIn(0, bitmap2)
  110. self.assertIn(1, bitmap2)
  111. self.assertIn(128, bitmap2)
  112. self.assertNotIn(2, bitmap2)
  113. self.assertNotIn(127, bitmap2)
  114. def test_encode_decode_dense_bitmap(self):
  115. """Test encoding and decoding a dense bitmap."""
  116. # Create a bitmap with many consecutive bits set
  117. bitmap = EWAHBitmap()
  118. for i in range(64):
  119. bitmap.add(i)
  120. # Encode
  121. encoded = bitmap.encode()
  122. # Decode
  123. bitmap2 = EWAHBitmap(encoded)
  124. # Verify all bits are preserved
  125. self.assertEqual(64, len(bitmap2))
  126. for i in range(64):
  127. self.assertIn(i, bitmap2)
  128. self.assertNotIn(64, bitmap2)
  129. def test_encode_decode_runs_of_zeros(self):
  130. """Test encoding and decoding bitmap with long runs of zeros."""
  131. # Create bitmap: bits 0, 200, 400 (lots of zeros in between)
  132. bitmap = EWAHBitmap()
  133. bitmap.add(0)
  134. bitmap.add(200)
  135. bitmap.add(400)
  136. # Encode
  137. encoded = bitmap.encode()
  138. # Decode
  139. bitmap2 = EWAHBitmap(encoded)
  140. # Verify
  141. self.assertEqual(3, len(bitmap2))
  142. self.assertIn(0, bitmap2)
  143. self.assertIn(200, bitmap2)
  144. self.assertIn(400, bitmap2)
  145. self.assertNotIn(1, bitmap2)
  146. self.assertNotIn(199, bitmap2)
  147. self.assertNotIn(201, bitmap2)
  148. def test_encode_decode_mixed_pattern(self):
  149. """Test encoding and decoding bitmap with mixed dense/sparse regions."""
  150. bitmap = EWAHBitmap()
  151. # Dense region: 0-63
  152. for i in range(64):
  153. bitmap.add(i)
  154. # Sparse region: 200, 300, 400
  155. bitmap.add(200)
  156. bitmap.add(300)
  157. bitmap.add(400)
  158. # Another dense region: 500-563
  159. for i in range(500, 564):
  160. bitmap.add(i)
  161. # Encode
  162. encoded = bitmap.encode()
  163. # Decode
  164. bitmap2 = EWAHBitmap(encoded)
  165. # Verify all bits preserved
  166. self.assertEqual(len(bitmap), len(bitmap2))
  167. for i in range(64):
  168. self.assertIn(i, bitmap2)
  169. self.assertIn(200, bitmap2)
  170. self.assertIn(300, bitmap2)
  171. self.assertIn(400, bitmap2)
  172. for i in range(500, 564):
  173. self.assertIn(i, bitmap2)
  174. # Check some bits that shouldn't be set
  175. self.assertNotIn(64, bitmap2)
  176. self.assertNotIn(199, bitmap2)
  177. self.assertNotIn(201, bitmap2)
  178. self.assertNotIn(499, bitmap2)
  179. def test_encode_decode_preserves_bitwise_ops(self):
  180. """Test that encode/decode doesn't break bitwise operations."""
  181. # Create two bitmaps
  182. bitmap1 = EWAHBitmap()
  183. bitmap1.add(0)
  184. bitmap1.add(5)
  185. bitmap1.add(100)
  186. bitmap2 = EWAHBitmap()
  187. bitmap2.add(5)
  188. bitmap2.add(10)
  189. bitmap2.add(100)
  190. # Encode and decode both
  191. bitmap1_decoded = EWAHBitmap(bitmap1.encode())
  192. bitmap2_decoded = EWAHBitmap(bitmap2.encode())
  193. # Perform operations on decoded bitmaps
  194. or_result = bitmap1_decoded | bitmap2_decoded
  195. and_result = bitmap1_decoded & bitmap2_decoded
  196. xor_result = bitmap1_decoded ^ bitmap2_decoded
  197. # Verify results
  198. # OR: {0, 5, 10, 100}
  199. self.assertEqual(4, len(or_result))
  200. self.assertIn(0, or_result)
  201. self.assertIn(5, or_result)
  202. self.assertIn(10, or_result)
  203. self.assertIn(100, or_result)
  204. # AND: {5, 100}
  205. self.assertEqual(2, len(and_result))
  206. self.assertIn(5, and_result)
  207. self.assertIn(100, and_result)
  208. # XOR: {0, 10}
  209. self.assertEqual(2, len(xor_result))
  210. self.assertIn(0, xor_result)
  211. self.assertIn(10, xor_result)
  212. def test_round_trip_large_bitmap(self):
  213. """Test round-trip encoding/decoding of a large bitmap."""
  214. # Create a large bitmap with various patterns
  215. bitmap = EWAHBitmap()
  216. # Add bits at various intervals
  217. for i in range(0, 10000, 7):
  218. bitmap.add(i)
  219. original_bits = set(bitmap.bits)
  220. # Encode and decode
  221. encoded = bitmap.encode()
  222. decoded = EWAHBitmap(encoded)
  223. # Verify all bits preserved
  224. self.assertEqual(original_bits, decoded.bits)
  225. self.assertEqual(len(bitmap), len(decoded))
  226. class EWAHBitmapTests(unittest.TestCase):
  227. """Tests for EWAH bitmap compression."""
  228. def test_empty_bitmap(self):
  229. """Test empty bitmap."""
  230. bitmap = EWAHBitmap()
  231. self.assertEqual(0, len(bitmap))
  232. self.assertEqual(0, bitmap.bit_count)
  233. def test_add_bit(self):
  234. """Test adding bits to bitmap."""
  235. bitmap = EWAHBitmap()
  236. bitmap.add(0)
  237. bitmap.add(5)
  238. bitmap.add(100)
  239. self.assertEqual(3, len(bitmap))
  240. self.assertEqual(101, bitmap.bit_count)
  241. self.assertIn(0, bitmap)
  242. self.assertIn(5, bitmap)
  243. self.assertIn(100, bitmap)
  244. self.assertNotIn(1, bitmap)
  245. self.assertNotIn(99, bitmap)
  246. def test_encode_decode(self):
  247. """Test encoding and decoding bitmaps."""
  248. bitmap = EWAHBitmap()
  249. bitmap.add(0)
  250. bitmap.add(1)
  251. bitmap.add(64)
  252. bitmap.add(128)
  253. # Encode
  254. data = bitmap.encode()
  255. self.assertIsInstance(data, bytes)
  256. # Decode
  257. bitmap2 = EWAHBitmap(data)
  258. self.assertEqual(len(bitmap), len(bitmap2))
  259. self.assertIn(0, bitmap2)
  260. self.assertIn(1, bitmap2)
  261. self.assertIn(64, bitmap2)
  262. self.assertIn(128, bitmap2)
  263. def test_bitwise_or(self):
  264. """Test bitwise OR operation."""
  265. bitmap1 = EWAHBitmap()
  266. bitmap1.add(0)
  267. bitmap1.add(5)
  268. bitmap2 = EWAHBitmap()
  269. bitmap2.add(5)
  270. bitmap2.add(10)
  271. result = bitmap1 | bitmap2
  272. self.assertEqual(3, len(result))
  273. self.assertIn(0, result)
  274. self.assertIn(5, result)
  275. self.assertIn(10, result)
  276. def test_bitwise_and(self):
  277. """Test bitwise AND operation."""
  278. bitmap1 = EWAHBitmap()
  279. bitmap1.add(0)
  280. bitmap1.add(5)
  281. bitmap2 = EWAHBitmap()
  282. bitmap2.add(5)
  283. bitmap2.add(10)
  284. result = bitmap1 & bitmap2
  285. self.assertEqual(1, len(result))
  286. self.assertIn(5, result)
  287. self.assertNotIn(0, result)
  288. self.assertNotIn(10, result)
  289. def test_bitwise_xor(self):
  290. """Test bitwise XOR operation."""
  291. bitmap1 = EWAHBitmap()
  292. bitmap1.add(0)
  293. bitmap1.add(5)
  294. bitmap2 = EWAHBitmap()
  295. bitmap2.add(5)
  296. bitmap2.add(10)
  297. result = bitmap1 ^ bitmap2
  298. self.assertEqual(2, len(result))
  299. self.assertIn(0, result)
  300. self.assertIn(10, result)
  301. self.assertNotIn(5, result)
  302. class BitmapEntryTests(unittest.TestCase):
  303. """Tests for bitmap entries."""
  304. def test_create_entry(self):
  305. """Test creating a bitmap entry."""
  306. bitmap = EWAHBitmap()
  307. bitmap.add(0)
  308. bitmap.add(10)
  309. entry = BitmapEntry(
  310. object_pos=100,
  311. xor_offset=0,
  312. flags=0,
  313. bitmap=bitmap,
  314. )
  315. self.assertEqual(100, entry.object_pos)
  316. self.assertEqual(0, entry.xor_offset)
  317. self.assertEqual(0, entry.flags)
  318. self.assertEqual(bitmap, entry.bitmap)
  319. class PackBitmapTests(unittest.TestCase):
  320. """Tests for pack bitmap."""
  321. def test_create_bitmap(self):
  322. """Test creating a pack bitmap."""
  323. bitmap = PackBitmap()
  324. self.assertEqual(BITMAP_VERSION, bitmap.version)
  325. self.assertEqual(BITMAP_OPT_FULL_DAG, bitmap.flags)
  326. self.assertIsNone(bitmap.pack_checksum)
  327. def test_bitmap_with_entries(self):
  328. """Test bitmap with entries."""
  329. bitmap = PackBitmap()
  330. commit_sha = b"\x00" * 20
  331. ewah_bitmap = EWAHBitmap()
  332. ewah_bitmap.add(0)
  333. ewah_bitmap.add(5)
  334. entry = BitmapEntry(
  335. object_pos=0,
  336. xor_offset=0,
  337. flags=0,
  338. bitmap=ewah_bitmap,
  339. )
  340. bitmap.entries[commit_sha] = entry
  341. self.assertTrue(bitmap.has_commit(commit_sha))
  342. self.assertFalse(bitmap.has_commit(b"\x01" * 20))
  343. def test_iter_commits(self):
  344. """Test iterating over commits with bitmaps."""
  345. bitmap = PackBitmap()
  346. commit1 = b"\x00" * 20
  347. commit2 = b"\x01" * 20
  348. ewah_bitmap = EWAHBitmap()
  349. entry = BitmapEntry(0, 0, 0, ewah_bitmap)
  350. bitmap.entries[commit1] = entry
  351. bitmap.entries[commit2] = entry
  352. commits = list(bitmap.iter_commits())
  353. self.assertEqual(2, len(commits))
  354. self.assertIn(commit1, commits)
  355. self.assertIn(commit2, commits)
  356. class BitmapFileTests(unittest.TestCase):
  357. """Tests for bitmap file I/O."""
  358. def test_write_read_empty_bitmap(self):
  359. """Test writing and reading an empty bitmap."""
  360. bitmap = PackBitmap()
  361. bitmap.pack_checksum = b"\x00" * 20
  362. # Write to bytes
  363. f = BytesIO()
  364. write_bitmap_file(f, bitmap)
  365. # Read back
  366. f.seek(0)
  367. bitmap2 = read_bitmap_file(f)
  368. self.assertEqual(bitmap.version, bitmap2.version)
  369. self.assertEqual(bitmap.flags, bitmap2.flags)
  370. self.assertEqual(bitmap.pack_checksum, bitmap2.pack_checksum)
  371. def test_write_read_with_type_bitmaps(self):
  372. """Test writing and reading bitmaps with type information."""
  373. bitmap = PackBitmap()
  374. bitmap.pack_checksum = b"\xaa" * 20
  375. # Add some type bitmap data
  376. bitmap.commit_bitmap.add(0)
  377. bitmap.commit_bitmap.add(5)
  378. bitmap.tree_bitmap.add(1)
  379. bitmap.blob_bitmap.add(2)
  380. bitmap.tag_bitmap.add(3)
  381. # Write to bytes
  382. f = BytesIO()
  383. write_bitmap_file(f, bitmap)
  384. # Read back
  385. f.seek(0)
  386. bitmap2 = read_bitmap_file(f)
  387. self.assertEqual(bitmap.version, bitmap2.version)
  388. self.assertEqual(bitmap.pack_checksum, bitmap2.pack_checksum)
  389. # Check type bitmaps
  390. self.assertIn(0, bitmap2.commit_bitmap)
  391. self.assertIn(5, bitmap2.commit_bitmap)
  392. self.assertIn(1, bitmap2.tree_bitmap)
  393. self.assertIn(2, bitmap2.blob_bitmap)
  394. self.assertIn(3, bitmap2.tag_bitmap)
  395. def test_invalid_signature(self):
  396. """Test reading file with invalid signature."""
  397. f = BytesIO(b"XXXX\x00\x01\x00\x00")
  398. with self.assertRaises(ValueError) as cm:
  399. read_bitmap_file(f)
  400. self.assertIn("Invalid bitmap signature", str(cm.exception))
  401. def test_invalid_version(self):
  402. """Test reading file with invalid version."""
  403. f = BytesIO(BITMAP_SIGNATURE + b"\x00\x02\x00\x01")
  404. with self.assertRaises(ValueError) as cm:
  405. read_bitmap_file(f)
  406. self.assertIn("Unsupported bitmap version", str(cm.exception))
  407. def test_incomplete_header(self):
  408. """Test reading file with incomplete header."""
  409. f = BytesIO(BITMAP_SIGNATURE + b"\x00")
  410. with self.assertRaises(ValueError) as cm:
  411. read_bitmap_file(f)
  412. self.assertIn("Incomplete bitmap header", str(cm.exception))
  413. class BitmapIntegrationTests(unittest.TestCase):
  414. """Integration tests for bitmap functionality."""
  415. def test_round_trip_file(self):
  416. """Test writing and reading bitmap to/from actual file."""
  417. with tempfile.TemporaryDirectory() as tmpdir:
  418. bitmap_path = os.path.join(tmpdir, "test.bitmap")
  419. # Create bitmap
  420. bitmap = PackBitmap()
  421. bitmap.pack_checksum = b"\xff" * 20
  422. bitmap.commit_bitmap.add(10)
  423. bitmap.tree_bitmap.add(20)
  424. # Write to file
  425. from dulwich.bitmap import write_bitmap
  426. write_bitmap(bitmap_path, bitmap)
  427. # Read back
  428. from dulwich.bitmap import read_bitmap
  429. bitmap2 = read_bitmap(bitmap_path)
  430. self.assertEqual(bitmap.version, bitmap2.version)
  431. self.assertEqual(bitmap.pack_checksum, bitmap2.pack_checksum)
  432. self.assertIn(10, bitmap2.commit_bitmap)
  433. self.assertIn(20, bitmap2.tree_bitmap)
  434. def test_xor_decompression(self):
  435. """Test XOR decompression of bitmap entries."""
  436. bitmap = PackBitmap()
  437. # Create base bitmap
  438. base_bitmap = EWAHBitmap()
  439. base_bitmap.add(0)
  440. base_bitmap.add(1)
  441. base_bitmap.add(2)
  442. base_entry = BitmapEntry(
  443. object_pos=0,
  444. xor_offset=0,
  445. flags=0,
  446. bitmap=base_bitmap,
  447. )
  448. # Create XOR'd bitmap
  449. # If we XOR base (bits 0,1,2) with XOR bitmap (bits 1,3),
  450. # result should be (bits 0,2,3)
  451. xor_bitmap = EWAHBitmap()
  452. xor_bitmap.add(1)
  453. xor_bitmap.add(3)
  454. xor_entry = BitmapEntry(
  455. object_pos=1,
  456. xor_offset=1, # Reference the previous entry
  457. flags=0,
  458. bitmap=xor_bitmap,
  459. )
  460. # Add entries
  461. base_sha = b"\x00" * 20
  462. xor_sha = b"\x01" * 20
  463. bitmap.entries[base_sha] = base_entry
  464. bitmap.entries_list.append((base_sha, base_entry))
  465. bitmap.entries[xor_sha] = xor_entry
  466. bitmap.entries_list.append((xor_sha, xor_entry))
  467. # Get decompressed bitmap
  468. result = bitmap.get_bitmap(xor_sha)
  469. self.assertIsNotNone(result)
  470. self.assertIn(0, result)
  471. self.assertNotIn(1, result) # XOR cancels this
  472. self.assertIn(2, result)
  473. self.assertIn(3, result)
  474. def test_lookup_table_round_trip(self):
  475. """Test reading and writing lookup tables."""
  476. with tempfile.TemporaryDirectory() as tmpdir:
  477. bitmap_path = os.path.join(tmpdir, "test.bitmap")
  478. # Create bitmap with lookup table
  479. bitmap = PackBitmap()
  480. bitmap.flags = BITMAP_OPT_FULL_DAG | BITMAP_OPT_LOOKUP_TABLE
  481. bitmap.pack_checksum = b"\xaa" * 20
  482. # Add some bitmap entries (required for lookup table to be written/read)
  483. for i in range(3):
  484. ewah = EWAHBitmap()
  485. ewah.add(i)
  486. entry = BitmapEntry(
  487. object_pos=i,
  488. xor_offset=0,
  489. flags=0,
  490. bitmap=ewah,
  491. )
  492. sha = i.to_bytes(20, byteorder="big")
  493. bitmap.entries[sha] = entry
  494. bitmap.entries_list.append((sha, entry))
  495. bitmap.lookup_table = [
  496. (0, 100, 0),
  497. (1, 200, 0),
  498. (2, 300, 1),
  499. ]
  500. # Write to file
  501. from dulwich.bitmap import write_bitmap
  502. write_bitmap(bitmap_path, bitmap)
  503. # Read back
  504. from dulwich.bitmap import read_bitmap
  505. bitmap2 = read_bitmap(bitmap_path)
  506. self.assertEqual(bitmap.flags, bitmap2.flags)
  507. self.assertIsNotNone(bitmap2.lookup_table)
  508. self.assertEqual(3, len(bitmap2.lookup_table))
  509. self.assertEqual((0, 100, 0), bitmap2.lookup_table[0])
  510. self.assertEqual((1, 200, 0), bitmap2.lookup_table[1])
  511. self.assertEqual((2, 300, 1), bitmap2.lookup_table[2])
  512. def test_name_hash_cache_round_trip(self):
  513. """Test reading and writing name-hash cache."""
  514. with tempfile.TemporaryDirectory() as tmpdir:
  515. bitmap_path = os.path.join(tmpdir, "test.bitmap")
  516. # Create bitmap with name-hash cache
  517. bitmap = PackBitmap()
  518. bitmap.flags = BITMAP_OPT_FULL_DAG | BITMAP_OPT_HASH_CACHE
  519. bitmap.pack_checksum = b"\xbb" * 20
  520. bitmap.name_hash_cache = [0x12345678, 0x9ABCDEF0, 0xFEDCBA98]
  521. # Write to file
  522. from dulwich.bitmap import write_bitmap
  523. write_bitmap(bitmap_path, bitmap)
  524. # Read back
  525. from dulwich.bitmap import read_bitmap
  526. bitmap2 = read_bitmap(bitmap_path)
  527. self.assertEqual(bitmap.flags, bitmap2.flags)
  528. self.assertIsNotNone(bitmap2.name_hash_cache)
  529. self.assertEqual(3, len(bitmap2.name_hash_cache))
  530. self.assertEqual(0x12345678, bitmap2.name_hash_cache[0])
  531. self.assertEqual(0x9ABCDEF0, bitmap2.name_hash_cache[1])
  532. self.assertEqual(0xFEDCBA98, bitmap2.name_hash_cache[2])
  533. class BitmapErrorHandlingTests(unittest.TestCase):
  534. """Tests for error handling in bitmap reading."""
  535. def test_truncated_header(self):
  536. """Test reading bitmap with truncated header."""
  537. # Only 10 bytes instead of full header
  538. data = b"BITM\x00\x01\x00\x00\x00\x00"
  539. with self.assertRaises(ValueError) as ctx:
  540. read_bitmap_file(BytesIO(data))
  541. # Should raise ValueError about missing/incomplete header data
  542. self.assertIsInstance(ctx.exception, ValueError)
  543. def test_invalid_signature(self):
  544. """Test reading bitmap with invalid signature."""
  545. data = b"JUNK\x00\x01\x00\x00" + b"\x00" * 20
  546. with self.assertRaises(ValueError) as ctx:
  547. read_bitmap_file(BytesIO(data))
  548. self.assertIn("signature", str(ctx.exception).lower())
  549. def test_unsupported_version(self):
  550. """Test reading bitmap with unsupported version."""
  551. data = BITMAP_SIGNATURE
  552. data += b"\x00\x99" # Version 153 (unsupported)
  553. data += b"\x00\x00" # Flags
  554. data += b"\x00\x00\x00\x00" # Entry count
  555. data += b"\x00" * 20 # Pack checksum
  556. with self.assertRaises(ValueError) as ctx:
  557. read_bitmap_file(BytesIO(data))
  558. self.assertIn("version", str(ctx.exception).lower())
  559. def test_truncated_type_bitmap(self):
  560. """Test reading bitmap with truncated type bitmap data."""
  561. # Valid header
  562. data = BITMAP_SIGNATURE
  563. data += BITMAP_VERSION.to_bytes(2, "big")
  564. data += b"\x00\x00" # Flags
  565. data += b"\x00\x00\x00\x00" # Entry count
  566. data += b"\x00" * 20 # Pack checksum
  567. # Truncated type bitmap (incomplete EWAH header)
  568. data += b"\x00\x00\x00\x05" # bit_count
  569. data += b"\x00\x00" # Incomplete word_count
  570. with self.assertRaises(ValueError) as ctx:
  571. read_bitmap_file(BytesIO(data))
  572. self.assertIn("type bitmap", str(ctx.exception).lower())
  573. def test_truncated_bitmap_entry(self):
  574. """Test reading bitmap with truncated entry."""
  575. # Valid header with 1 entry
  576. data = BITMAP_SIGNATURE
  577. data += BITMAP_VERSION.to_bytes(2, "big")
  578. data += b"\x00\x00" # Flags
  579. data += b"\x00\x00\x00\x01" # 1 entry
  580. data += b"\x00" * 20 # Pack checksum
  581. # Write empty type bitmaps
  582. for _ in range(4):
  583. empty_ewah = EWAHBitmap().encode()
  584. data += empty_ewah
  585. # Truncated entry (only object position, missing rest)
  586. data += b"\x00\x00\x00\x00" # Object position
  587. # Missing: XOR offset, flags, bitmap data
  588. with self.assertRaises(ValueError) as ctx:
  589. read_bitmap_file(BytesIO(data))
  590. # Should raise ValueError about missing data
  591. self.assertIsInstance(ctx.exception, ValueError)
  592. def test_empty_bitmap_file(self):
  593. """Test reading completely empty file."""
  594. with self.assertRaises(ValueError):
  595. read_bitmap_file(BytesIO(b""))
  596. def test_bitmap_with_zero_entries(self):
  597. """Test valid bitmap with zero entries."""
  598. bitmap = PackBitmap()
  599. bitmap.pack_checksum = b"\x00" * 20
  600. f = BytesIO()
  601. write_bitmap_file(f, bitmap)
  602. f.seek(0)
  603. # Should read successfully
  604. bitmap2 = read_bitmap_file(f)
  605. self.assertEqual(0, len(bitmap2.entries))
  606. self.assertIsNotNone(bitmap2.pack_checksum)
  607. class BitmapEdgeCaseTests(unittest.TestCase):
  608. """Tests for edge cases in bitmap handling."""
  609. def test_very_large_bitmap(self):
  610. """Test bitmap with many bits set."""
  611. bitmap = EWAHBitmap()
  612. # Add 100,000 bits
  613. for i in range(100000):
  614. if i % 3 == 0: # Every 3rd bit
  615. bitmap.add(i)
  616. # Should encode and decode without issues
  617. encoded = bitmap.encode()
  618. decoded = EWAHBitmap(encoded)
  619. self.assertEqual(len(bitmap), len(decoded))
  620. # Verify a sample of bits
  621. self.assertIn(0, decoded)
  622. self.assertIn(99999, decoded)
  623. self.assertNotIn(1, decoded)
  624. self.assertNotIn(99998, decoded)
  625. def test_bitmap_with_large_gaps(self):
  626. """Test bitmap with large gaps between set bits."""
  627. bitmap = EWAHBitmap()
  628. bitmap.add(0)
  629. bitmap.add(100000)
  630. bitmap.add(200000)
  631. encoded = bitmap.encode()
  632. decoded = EWAHBitmap(encoded)
  633. self.assertEqual(3, len(decoded))
  634. self.assertIn(0, decoded)
  635. self.assertIn(100000, decoded)
  636. self.assertIn(200000, decoded)
  637. def test_bitmap_all_bits_in_word(self):
  638. """Test bitmap with all 64 bits in a word set."""
  639. bitmap = EWAHBitmap()
  640. for i in range(64):
  641. bitmap.add(i)
  642. encoded = bitmap.encode()
  643. decoded = EWAHBitmap(encoded)
  644. self.assertEqual(64, len(decoded))
  645. for i in range(64):
  646. self.assertIn(i, decoded)
  647. def test_multiple_flags_combined(self):
  648. """Test bitmap with multiple flags set."""
  649. bitmap = PackBitmap(
  650. flags=BITMAP_OPT_FULL_DAG | BITMAP_OPT_HASH_CACHE | BITMAP_OPT_LOOKUP_TABLE
  651. )
  652. bitmap.pack_checksum = b"\x00" * 20
  653. bitmap.lookup_table = [(0, 0, 0), (1, 100, 0)]
  654. bitmap.name_hash_cache = [0x12345678, 0xABCDEF00]
  655. # Add an entry
  656. test_bitmap = EWAHBitmap()
  657. test_bitmap.add(0)
  658. test_bitmap.add(5)
  659. entry = BitmapEntry(object_pos=0, xor_offset=0, flags=0, bitmap=test_bitmap)
  660. bitmap.entries[b"\x00" * 20] = entry
  661. # Write and read back
  662. f = BytesIO()
  663. write_bitmap_file(f, bitmap)
  664. f.seek(0)
  665. bitmap2 = read_bitmap_file(f)
  666. self.assertEqual(bitmap.flags, bitmap2.flags)
  667. self.assertTrue(bitmap2.flags & BITMAP_OPT_FULL_DAG)
  668. self.assertTrue(bitmap2.flags & BITMAP_OPT_HASH_CACHE)
  669. self.assertTrue(bitmap2.flags & BITMAP_OPT_LOOKUP_TABLE)
  670. self.assertIsNotNone(bitmap2.lookup_table)
  671. self.assertIsNotNone(bitmap2.name_hash_cache)
  672. class BitmapConfigTests(unittest.TestCase):
  673. """Tests for bitmap-related configuration settings."""
  674. def test_pack_write_bitmaps_default(self):
  675. """Test pack.writeBitmaps defaults to false."""
  676. from dulwich.config import ConfigFile
  677. config = ConfigFile()
  678. self.assertFalse(config.get_boolean((b"pack",), b"writeBitmaps", False))
  679. def test_pack_write_bitmaps_true(self):
  680. """Test pack.writeBitmaps = true."""
  681. from dulwich.config import ConfigFile
  682. config = ConfigFile()
  683. config.set((b"pack",), b"writeBitmaps", b"true")
  684. self.assertTrue(config.get_boolean((b"pack",), b"writeBitmaps", False))
  685. def test_pack_write_bitmaps_false(self):
  686. """Test pack.writeBitmaps = false."""
  687. from dulwich.config import ConfigFile
  688. config = ConfigFile()
  689. config.set((b"pack",), b"writeBitmaps", b"false")
  690. self.assertFalse(config.get_boolean((b"pack",), b"writeBitmaps", False))
  691. def test_pack_write_bitmap_hash_cache_default(self):
  692. """Test pack.writeBitmapHashCache defaults to true."""
  693. from dulwich.config import ConfigFile
  694. config = ConfigFile()
  695. self.assertTrue(config.get_boolean((b"pack",), b"writeBitmapHashCache", True))
  696. def test_pack_write_bitmap_hash_cache_false(self):
  697. """Test pack.writeBitmapHashCache = false."""
  698. from dulwich.config import ConfigFile
  699. config = ConfigFile()
  700. config.set((b"pack",), b"writeBitmapHashCache", b"false")
  701. self.assertFalse(config.get_boolean((b"pack",), b"writeBitmapHashCache", True))
  702. def test_pack_write_bitmap_lookup_table_default(self):
  703. """Test pack.writeBitmapLookupTable defaults to true."""
  704. from dulwich.config import ConfigFile
  705. config = ConfigFile()
  706. self.assertTrue(config.get_boolean((b"pack",), b"writeBitmapLookupTable", True))
  707. def test_repack_write_bitmaps(self):
  708. """Test repack.writeBitmaps configuration."""
  709. from dulwich.config import ConfigFile
  710. config = ConfigFile()
  711. config.set((b"repack",), b"writeBitmaps", b"true")
  712. self.assertTrue(config.get_boolean((b"repack",), b"writeBitmaps", False))
  713. def test_pack_use_bitmap_index_default(self):
  714. """Test pack.useBitmapIndex defaults to true."""
  715. from dulwich.config import ConfigFile
  716. config = ConfigFile()
  717. self.assertTrue(config.get_boolean((b"pack",), b"useBitmapIndex", True))
  718. def test_pack_use_bitmap_index_false(self):
  719. """Test pack.useBitmapIndex = false."""
  720. from dulwich.config import ConfigFile
  721. config = ConfigFile()
  722. config.set((b"pack",), b"useBitmapIndex", b"false")
  723. self.assertFalse(config.get_boolean((b"pack",), b"useBitmapIndex", True))
  724. class ReachabilityProviderTests(unittest.TestCase):
  725. """Tests for ObjectReachabilityProvider implementations."""
  726. def setUp(self):
  727. """Set up test repository with commits."""
  728. from dulwich.object_store import DiskObjectStore
  729. from dulwich.objects import Blob, Commit, Tree
  730. self.test_dir = tempfile.mkdtemp()
  731. self.store = DiskObjectStore(self.test_dir)
  732. # Create a simple commit history:
  733. # commit1 -> commit2 -> commit3
  734. # \-> commit4
  735. # Create blob and tree
  736. self.blob1 = Blob.from_string(b"test content 1")
  737. self.store.add_object(self.blob1)
  738. self.blob2 = Blob.from_string(b"test content 2")
  739. self.store.add_object(self.blob2)
  740. self.tree1 = Tree()
  741. self.tree1[b"file1.txt"] = (0o100644, self.blob1.id)
  742. self.store.add_object(self.tree1)
  743. self.tree2 = Tree()
  744. self.tree2[b"file1.txt"] = (0o100644, self.blob1.id)
  745. self.tree2[b"file2.txt"] = (0o100644, self.blob2.id)
  746. self.store.add_object(self.tree2)
  747. # Create commit1 (root)
  748. self.commit1 = Commit()
  749. self.commit1.tree = self.tree1.id
  750. self.commit1.message = b"First commit"
  751. self.commit1.author = self.commit1.committer = b"Test <test@example.com>"
  752. self.commit1.author_time = self.commit1.commit_time = 1234567890
  753. self.commit1.author_timezone = self.commit1.commit_timezone = 0
  754. self.store.add_object(self.commit1)
  755. # Create commit2 (child of commit1)
  756. self.commit2 = Commit()
  757. self.commit2.tree = self.tree1.id
  758. self.commit2.parents = [self.commit1.id]
  759. self.commit2.message = b"Second commit"
  760. self.commit2.author = self.commit2.committer = b"Test <test@example.com>"
  761. self.commit2.author_time = self.commit2.commit_time = 1234567891
  762. self.commit2.author_timezone = self.commit2.commit_timezone = 0
  763. self.store.add_object(self.commit2)
  764. # Create commit3 (child of commit2)
  765. self.commit3 = Commit()
  766. self.commit3.tree = self.tree2.id
  767. self.commit3.parents = [self.commit2.id]
  768. self.commit3.message = b"Third commit"
  769. self.commit3.author = self.commit3.committer = b"Test <test@example.com>"
  770. self.commit3.author_time = self.commit3.commit_time = 1234567892
  771. self.commit3.author_timezone = self.commit3.commit_timezone = 0
  772. self.store.add_object(self.commit3)
  773. # Create commit4 (child of commit1, creates a branch)
  774. self.commit4 = Commit()
  775. self.commit4.tree = self.tree2.id
  776. self.commit4.parents = [self.commit1.id]
  777. self.commit4.message = b"Fourth commit"
  778. self.commit4.author = self.commit4.committer = b"Test <test@example.com>"
  779. self.commit4.author_time = self.commit4.commit_time = 1234567893
  780. self.commit4.author_timezone = self.commit4.commit_timezone = 0
  781. self.store.add_object(self.commit4)
  782. def tearDown(self):
  783. """Clean up test directory."""
  784. import shutil
  785. # Close store to release file handles on Windows
  786. self.store.close()
  787. shutil.rmtree(self.test_dir)
  788. def test_graph_traversal_reachability_single_commit(self):
  789. """Test GraphTraversalReachability with single commit."""
  790. from dulwich.object_store import GraphTraversalReachability
  791. provider = GraphTraversalReachability(self.store)
  792. # Get reachable commits from commit1
  793. reachable = provider.get_reachable_commits(
  794. [self.commit1.id], exclude=None, shallow=None
  795. )
  796. # Should only include commit1
  797. self.assertEqual({self.commit1.id}, reachable)
  798. def test_graph_traversal_reachability_linear_history(self):
  799. """Test GraphTraversalReachability with linear history."""
  800. from dulwich.object_store import GraphTraversalReachability
  801. provider = GraphTraversalReachability(self.store)
  802. # Get reachable commits from commit3
  803. reachable = provider.get_reachable_commits(
  804. [self.commit3.id], exclude=None, shallow=None
  805. )
  806. # Should include commit3, commit2, and commit1
  807. expected = {self.commit1.id, self.commit2.id, self.commit3.id}
  808. self.assertEqual(expected, reachable)
  809. def test_graph_traversal_reachability_with_exclusion(self):
  810. """Test GraphTraversalReachability with exclusion."""
  811. from dulwich.object_store import GraphTraversalReachability
  812. provider = GraphTraversalReachability(self.store)
  813. # Get commits reachable from commit3 but not from commit1
  814. reachable = provider.get_reachable_commits(
  815. [self.commit3.id], exclude=[self.commit1.id], shallow=None
  816. )
  817. # Should include commit3 and commit2, but not commit1
  818. expected = {self.commit2.id, self.commit3.id}
  819. self.assertEqual(expected, reachable)
  820. def test_graph_traversal_reachability_branching(self):
  821. """Test GraphTraversalReachability with branching history."""
  822. from dulwich.object_store import GraphTraversalReachability
  823. provider = GraphTraversalReachability(self.store)
  824. # Get reachable commits from both commit3 and commit4
  825. reachable = provider.get_reachable_commits(
  826. [self.commit3.id, self.commit4.id], exclude=None, shallow=None
  827. )
  828. # Should include all commits
  829. expected = {self.commit1.id, self.commit2.id, self.commit3.id, self.commit4.id}
  830. self.assertEqual(expected, reachable)
  831. def test_graph_traversal_reachable_objects(self):
  832. """Test GraphTraversalReachability.get_reachable_objects()."""
  833. from dulwich.object_store import GraphTraversalReachability
  834. provider = GraphTraversalReachability(self.store)
  835. # Get all objects reachable from commit3
  836. reachable = provider.get_reachable_objects(
  837. [self.commit3.id], exclude_commits=None
  838. )
  839. # Should include commit3, blob1, and blob2 (but not tree objects themselves)
  840. self.assertIn(self.commit3.id, reachable)
  841. self.assertIn(self.blob1.id, reachable)
  842. self.assertIn(self.blob2.id, reachable)
  843. # Verify at least 3 objects
  844. self.assertGreaterEqual(len(reachable), 3)
  845. def test_graph_traversal_reachable_objects_with_exclusion(self):
  846. """Test GraphTraversalReachability.get_reachable_objects() with exclusion."""
  847. from dulwich.object_store import GraphTraversalReachability
  848. provider = GraphTraversalReachability(self.store)
  849. # Get objects reachable from commit3 but not from commit2
  850. reachable = provider.get_reachable_objects(
  851. [self.commit3.id], exclude_commits=[self.commit2.id]
  852. )
  853. # commit2 uses tree1 (which has blob1), commit3 uses tree2 (which has blob1 + blob2)
  854. # So should include commit3 and blob2 (new in commit3)
  855. # blob1 should be excluded because it's in tree1 (reachable from commit2)
  856. self.assertIn(self.commit3.id, reachable)
  857. self.assertIn(self.blob2.id, reachable)
  858. def test_get_reachability_provider_without_bitmaps(self):
  859. """Test get_reachability_provider returns GraphTraversalReachability when no bitmaps."""
  860. from dulwich.object_store import GraphTraversalReachability
  861. provider = self.store.get_reachability_provider()
  862. # Should return GraphTraversalReachability when no bitmaps available
  863. self.assertIsInstance(provider, GraphTraversalReachability)
  864. def test_get_reachability_provider_prefer_bitmaps_false(self):
  865. """Test get_reachability_provider with prefer_bitmaps=False."""
  866. from dulwich.object_store import GraphTraversalReachability
  867. provider = self.store.get_reachability_provider(prefer_bitmaps=False)
  868. # Should return GraphTraversalReachability when prefer_bitmaps=False
  869. self.assertIsInstance(provider, GraphTraversalReachability)
  870. def test_bitmap_reachability_fallback_without_bitmaps(self):
  871. """Test BitmapReachability falls back to graph traversal without bitmaps."""
  872. provider = BitmapReachability(self.store)
  873. # Without bitmaps, should fall back to graph traversal
  874. reachable = provider.get_reachable_commits(
  875. [self.commit3.id], exclude=None, shallow=None
  876. )
  877. # Should still work via fallback
  878. expected = {self.commit1.id, self.commit2.id, self.commit3.id}
  879. self.assertEqual(expected, reachable)
  880. def test_bitmap_reachability_fallback_with_shallow(self):
  881. """Test BitmapReachability falls back for shallow clones."""
  882. provider = BitmapReachability(self.store)
  883. # With shallow boundary, should fall back to graph traversal
  884. reachable = provider.get_reachable_commits(
  885. [self.commit3.id], exclude=None, shallow={self.commit2.id}
  886. )
  887. # Should include commit3 and commit2 (shallow boundary includes boundary commit)
  888. # but not commit1 (beyond shallow boundary)
  889. self.assertEqual({self.commit2.id, self.commit3.id}, reachable)
  890. def test_reachability_provider_protocol(self):
  891. """Test that both providers implement the same interface."""
  892. graph_provider = GraphTraversalReachability(self.store)
  893. bitmap_provider = BitmapReachability(self.store)
  894. # Both should have the same methods
  895. for method in [
  896. "get_reachable_commits",
  897. "get_reachable_objects",
  898. "get_tree_objects",
  899. ]:
  900. self.assertTrue(hasattr(graph_provider, method))
  901. self.assertTrue(hasattr(bitmap_provider, method))
  902. def test_graph_traversal_vs_bitmap_consistency(self):
  903. """Test that GraphTraversalReachability and BitmapReachability produce same results."""
  904. graph_provider = GraphTraversalReachability(self.store)
  905. bitmap_provider = BitmapReachability(self.store) # Will use fallback
  906. # Test get_reachable_commits
  907. graph_commits = graph_provider.get_reachable_commits(
  908. [self.commit3.id], exclude=[self.commit1.id], shallow=None
  909. )
  910. bitmap_commits = bitmap_provider.get_reachable_commits(
  911. [self.commit3.id], exclude=[self.commit1.id], shallow=None
  912. )
  913. self.assertEqual(graph_commits, bitmap_commits)
  914. # Test get_reachable_objects
  915. graph_objects = graph_provider.get_reachable_objects(
  916. [self.commit3.id], exclude_commits=None
  917. )
  918. bitmap_objects = bitmap_provider.get_reachable_objects(
  919. [self.commit3.id], exclude_commits=None
  920. )
  921. self.assertEqual(graph_objects, bitmap_objects)
  922. class PackEnsureBitmapTests(unittest.TestCase):
  923. """Tests for Pack.ensure_bitmap() method."""
  924. def setUp(self):
  925. """Set up test repository with a pack."""
  926. from dulwich.object_store import DiskObjectStore
  927. from dulwich.objects import Blob, Commit, Tree
  928. self.temp_dir = tempfile.mkdtemp()
  929. self.addCleanup(shutil.rmtree, self.temp_dir)
  930. # Create pack directory
  931. os.makedirs(os.path.join(self.temp_dir, "pack"))
  932. self.store = DiskObjectStore(self.temp_dir)
  933. # Close store before cleanup to release file handles on Windows
  934. self.addCleanup(self.store.close)
  935. # Create test objects
  936. self.blob = Blob.from_string(b"test content")
  937. self.store.add_object(self.blob)
  938. self.tree = Tree()
  939. self.tree.add(b"file.txt", 0o100644, self.blob.id)
  940. self.store.add_object(self.tree)
  941. self.commit = Commit()
  942. self.commit.tree = self.tree.id
  943. self.commit.author = self.commit.committer = b"Test <test@example.com>"
  944. self.commit.author_time = self.commit.commit_time = 1234567890
  945. self.commit.author_timezone = self.commit.commit_timezone = 0
  946. self.commit.message = b"Test commit"
  947. self.store.add_object(self.commit)
  948. # Repack to create a pack
  949. self.store.repack()
  950. self.pack = self.store.packs[0]
  951. def test_ensure_bitmap_creates_bitmap(self):
  952. """Test that ensure_bitmap creates a bitmap file."""
  953. # Initially no bitmap
  954. self.assertFalse(os.path.exists(self.pack._bitmap_path))
  955. # Ensure bitmap with commit_interval=1 to ensure our single commit is selected
  956. refs = {b"refs/heads/master": self.commit.id}
  957. bitmap = self.pack.ensure_bitmap(self.store, refs, commit_interval=1)
  958. # Bitmap should now exist
  959. self.assertIsNotNone(bitmap)
  960. self.assertTrue(os.path.exists(self.pack._bitmap_path))
  961. # Verify it's a PackBitmap instance
  962. from dulwich.bitmap import PackBitmap
  963. self.assertIsInstance(bitmap, PackBitmap)
  964. def test_ensure_bitmap_returns_existing(self):
  965. """Test that ensure_bitmap returns existing bitmap without regenerating."""
  966. refs = {b"refs/heads/master": self.commit.id}
  967. # Create bitmap with commit_interval=1
  968. self.pack.ensure_bitmap(self.store, refs, commit_interval=1)
  969. mtime1 = os.path.getmtime(self.pack._bitmap_path)
  970. # Ensure again - should return existing
  971. import time
  972. time.sleep(0.01) # Ensure time difference
  973. self.pack.ensure_bitmap(self.store, refs, commit_interval=1)
  974. mtime2 = os.path.getmtime(self.pack._bitmap_path)
  975. # File should not have been regenerated
  976. self.assertEqual(mtime1, mtime2)
  977. def test_ensure_bitmap_with_custom_interval(self):
  978. """Test ensure_bitmap with custom commit_interval."""
  979. refs = {b"refs/heads/master": self.commit.id}
  980. bitmap = self.pack.ensure_bitmap(self.store, refs, commit_interval=50)
  981. self.assertIsNotNone(bitmap)
  982. class GeneratePackBitmapsTests(unittest.TestCase):
  983. """Tests for PackBasedObjectStore.generate_pack_bitmaps()."""
  984. def setUp(self):
  985. """Set up test repository."""
  986. from dulwich.object_store import DiskObjectStore
  987. from dulwich.objects import Blob, Commit, Tree
  988. self.temp_dir = tempfile.mkdtemp()
  989. self.addCleanup(shutil.rmtree, self.temp_dir)
  990. # Create pack directory
  991. os.makedirs(os.path.join(self.temp_dir, "pack"))
  992. self.store = DiskObjectStore(self.temp_dir)
  993. # Close store before cleanup to release file handles on Windows
  994. self.addCleanup(self.store.close)
  995. # Create multiple commits
  996. self.commits = []
  997. for i in range(3):
  998. blob = Blob.from_string(f"content {i}".encode())
  999. self.store.add_object(blob)
  1000. tree = Tree()
  1001. tree.add(f"file{i}.txt".encode(), 0o100644, blob.id)
  1002. self.store.add_object(tree)
  1003. commit = Commit()
  1004. commit.tree = tree.id
  1005. if i > 0:
  1006. commit.parents = [self.commits[-1].id]
  1007. commit.author = commit.committer = b"Test <test@example.com>"
  1008. commit.author_time = commit.commit_time = 1234567890 + i
  1009. commit.author_timezone = commit.commit_timezone = 0
  1010. commit.message = f"Commit {i}".encode()
  1011. self.store.add_object(commit)
  1012. self.commits.append(commit)
  1013. # Repack to create pack
  1014. self.store.repack()
  1015. def test_generate_pack_bitmaps(self):
  1016. """Test generating bitmaps for all packs."""
  1017. refs = {b"refs/heads/master": self.commits[-1].id}
  1018. # Initially no bitmaps
  1019. for pack in self.store.packs:
  1020. self.assertFalse(os.path.exists(pack._bitmap_path))
  1021. # Generate bitmaps
  1022. count = self.store.generate_pack_bitmaps(refs)
  1023. # Should have generated bitmaps
  1024. self.assertEqual(count, len(self.store.packs))
  1025. for pack in self.store.packs:
  1026. self.assertTrue(os.path.exists(pack._bitmap_path))
  1027. def test_generate_pack_bitmaps_multiple_calls(self):
  1028. """Test that calling generate_pack_bitmaps multiple times is safe."""
  1029. refs = {b"refs/heads/master": self.commits[-1].id}
  1030. # Generate once
  1031. self.store.generate_pack_bitmaps(refs)
  1032. mtimes1 = [os.path.getmtime(p._bitmap_path) for p in self.store.packs]
  1033. # Generate again
  1034. import time
  1035. time.sleep(0.01)
  1036. self.store.generate_pack_bitmaps(refs)
  1037. mtimes2 = [os.path.getmtime(p._bitmap_path) for p in self.store.packs]
  1038. # Should not regenerate existing bitmaps
  1039. self.assertEqual(mtimes1, mtimes2)
  1040. def test_generate_pack_bitmaps_with_progress(self):
  1041. """Test generate_pack_bitmaps with progress callback."""
  1042. refs = {b"refs/heads/master": self.commits[-1].id}
  1043. messages = []
  1044. def progress(msg):
  1045. messages.append(msg)
  1046. self.store.generate_pack_bitmaps(refs, progress=progress)
  1047. # Should have received progress messages
  1048. self.assertGreater(len(messages), 0)