test_bitmap.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # test_bitmap.py -- Compatibility tests for git pack bitmaps.
  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. """Compatibility tests for git pack bitmaps."""
  22. import os
  23. import tempfile
  24. from dulwich.bitmap import (
  25. BITMAP_OPT_FULL_DAG,
  26. BITMAP_OPT_HASH_CACHE,
  27. BITMAP_OPT_LOOKUP_TABLE,
  28. BitmapEntry,
  29. EWAHBitmap,
  30. PackBitmap,
  31. write_bitmap,
  32. )
  33. from dulwich.pack import Pack
  34. from dulwich.repo import Repo
  35. from .. import TestCase
  36. from .utils import remove_ro, require_git_version, rmtree_ro, run_git_or_fail
  37. class BitmapCompatTests(TestCase):
  38. """Compatibility tests for reading git-generated bitmaps."""
  39. def setUp(self):
  40. super().setUp()
  41. # Git bitmap support was added in 2.0.0
  42. require_git_version((2, 0, 0))
  43. self._tempdir = tempfile.mkdtemp()
  44. self.addCleanup(rmtree_ro, self._tempdir)
  45. def _init_repo_with_bitmap(self):
  46. """Create a repo and generate a bitmap using git."""
  47. repo_path = os.path.join(self._tempdir, "test-repo")
  48. os.mkdir(repo_path)
  49. # Initialize repo
  50. run_git_or_fail(["init"], cwd=repo_path)
  51. # Create some commits
  52. test_file = os.path.join(repo_path, "test.txt")
  53. for i in range(5):
  54. with open(test_file, "w") as f:
  55. f.write(f"Content {i}\n")
  56. run_git_or_fail(["add", "test.txt"], cwd=repo_path)
  57. run_git_or_fail(
  58. ["commit", "-m", f"Commit {i}"],
  59. cwd=repo_path,
  60. env={"GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "test@example.com"},
  61. )
  62. # Enable bitmap writing and repack
  63. run_git_or_fail(
  64. ["config", "pack.writeBitmaps", "true"],
  65. cwd=repo_path,
  66. )
  67. run_git_or_fail(["repack", "-a", "-d", "-b"], cwd=repo_path)
  68. return repo_path
  69. def test_read_git_generated_bitmap(self):
  70. """Test that Dulwich can read a bitmap generated by git."""
  71. repo_path = self._init_repo_with_bitmap()
  72. # Find the pack file with bitmap
  73. pack_dir = os.path.join(repo_path, ".git", "objects", "pack")
  74. bitmap_files = [f for f in os.listdir(pack_dir) if f.endswith(".bitmap")]
  75. # Get the pack file (basename without extension)
  76. bitmap_name = bitmap_files[0]
  77. pack_basename = bitmap_name.replace(".bitmap", "")
  78. pack_path = os.path.join(pack_dir, pack_basename)
  79. # Verify bitmap file exists at expected location
  80. bitmap_path = pack_path + ".bitmap"
  81. self.assertTrue(
  82. os.path.exists(bitmap_path), f"Bitmap file not found at {bitmap_path}"
  83. )
  84. # Try to load the bitmap using Dulwich
  85. with Pack(pack_path) as pack:
  86. bitmap = pack.bitmap
  87. # Basic checks
  88. self.assertIsNotNone(bitmap, f"Failed to load bitmap from {pack_path}")
  89. self.assertIsNotNone(bitmap.pack_checksum, "Bitmap missing pack checksum")
  90. # Check that we have some type bitmaps
  91. # At minimum, we should have some commits
  92. self.assertGreater(
  93. len(bitmap.commit_bitmap.bits),
  94. 0,
  95. "Commit bitmap should not be empty",
  96. )
  97. def test_git_can_use_dulwich_repo_with_bitmap(self):
  98. """Test that git can work with a repo that has Dulwich-created objects."""
  99. repo_path = os.path.join(self._tempdir, "dulwich-repo")
  100. # Create a repo with Dulwich and add commits to ensure git creates bitmaps
  101. repo = Repo.init(repo_path, mkdir=True)
  102. self.addCleanup(repo.close)
  103. # Create actual commits, not just loose objects - git needs commits for bitmaps
  104. test_file = os.path.join(repo_path, "test.txt")
  105. for i in range(5):
  106. with open(test_file, "w") as f:
  107. f.write(f"Content {i}\n")
  108. run_git_or_fail(["add", "test.txt"], cwd=repo_path)
  109. run_git_or_fail(
  110. ["commit", "-m", f"Commit {i}"],
  111. cwd=repo_path,
  112. env={"GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "test@example.com"},
  113. )
  114. # Configure git to write bitmaps
  115. run_git_or_fail(
  116. ["config", "pack.writeBitmaps", "true"],
  117. cwd=repo_path,
  118. )
  119. # Git should be able to repack with bitmaps
  120. run_git_or_fail(["repack", "-a", "-d", "-b"], cwd=repo_path)
  121. # Verify git created a bitmap
  122. pack_dir = os.path.join(repo_path, ".git", "objects", "pack")
  123. self.assertTrue(os.path.exists(pack_dir), "Pack directory should exist")
  124. bitmap_files = [f for f in os.listdir(pack_dir) if f.endswith(".bitmap")]
  125. self.assertGreater(
  126. len(bitmap_files), 0, "Git should have created a bitmap file after repack"
  127. )
  128. def test_git_can_read_dulwich_bitmap(self):
  129. """Test that git can read a bitmap file written by Dulwich."""
  130. repo_path = os.path.join(self._tempdir, "dulwich-bitmap-repo")
  131. # Create a repo with git and add commits
  132. run_git_or_fail(["init"], cwd=None, env={"GIT_DIR": repo_path})
  133. test_file = os.path.join(repo_path, "..", "test.txt")
  134. os.makedirs(os.path.dirname(test_file), exist_ok=True)
  135. for i in range(5):
  136. with open(test_file, "w") as f:
  137. f.write(f"Content {i}\n")
  138. run_git_or_fail(
  139. ["add", test_file],
  140. cwd=os.path.dirname(repo_path),
  141. env={
  142. "GIT_DIR": repo_path,
  143. "GIT_WORK_TREE": os.path.dirname(repo_path),
  144. },
  145. )
  146. run_git_or_fail(
  147. ["commit", "-m", f"Commit {i}"],
  148. cwd=os.path.dirname(repo_path),
  149. env={
  150. "GIT_DIR": repo_path,
  151. "GIT_WORK_TREE": os.path.dirname(repo_path),
  152. "GIT_AUTHOR_NAME": "Test",
  153. "GIT_AUTHOR_EMAIL": "test@example.com",
  154. },
  155. )
  156. # Create a pack with git first
  157. run_git_or_fail(["repack", "-a", "-d"], cwd=None, env={"GIT_DIR": repo_path})
  158. # Now use Dulwich to write a bitmap for the pack
  159. pack_dir = os.path.join(repo_path, "objects", "pack")
  160. pack_files = [f for f in os.listdir(pack_dir) if f.endswith(".pack")]
  161. self.assertGreater(len(pack_files), 0, "Should have at least one pack file")
  162. pack_basename = pack_files[0].replace(".pack", "")
  163. pack_path = os.path.join(pack_dir, pack_basename)
  164. # Load the pack and create bitmap data, then close before writing
  165. with Pack(pack_path) as pack:
  166. # Create a simple bitmap for testing
  167. # Git requires BITMAP_OPT_FULL_DAG flag
  168. bitmap = PackBitmap(
  169. flags=BITMAP_OPT_FULL_DAG
  170. | BITMAP_OPT_HASH_CACHE
  171. | BITMAP_OPT_LOOKUP_TABLE
  172. )
  173. bitmap.pack_checksum = pack.get_stored_checksum()
  174. # Add bitmap entries for the first few commits in the pack
  175. for i, (sha, offset, crc) in enumerate(pack.index.iterentries()):
  176. if i >= 3: # Just add 3 entries
  177. break
  178. ewah = EWAHBitmap()
  179. # Mark this object and a couple others as reachable
  180. for j in range(i + 1):
  181. ewah.add(j)
  182. entry = BitmapEntry(object_pos=i, xor_offset=0, flags=0, bitmap=ewah)
  183. bitmap.entries[sha] = entry
  184. bitmap.entries_list.append((sha, entry))
  185. # Add name hash cache
  186. bitmap.name_hash_cache = [0x12345678, 0xABCDEF00, 0xFEDCBA98]
  187. # Write the bitmap after pack is closed to avoid file locking on Windows
  188. bitmap_path = pack_path + ".bitmap"
  189. remove_ro(bitmap_path)
  190. write_bitmap(bitmap_path, bitmap)
  191. # Verify git can use the repository with our bitmap
  192. # This should succeed if git can read our bitmap
  193. run_git_or_fail(
  194. ["rev-list", "--count", "--use-bitmap-index", "HEAD"],
  195. cwd=None,
  196. env={"GIT_DIR": repo_path},
  197. )
  198. # Verify git count-objects works with our bitmap
  199. run_git_or_fail(["count-objects", "-v"], cwd=None, env={"GIT_DIR": repo_path})
  200. def test_bitmap_file_format_structure(self):
  201. """Test that git-generated bitmap has expected structure."""
  202. repo_path = self._init_repo_with_bitmap()
  203. # Find bitmap
  204. pack_dir = os.path.join(repo_path, ".git", "objects", "pack")
  205. bitmap_files = [f for f in os.listdir(pack_dir) if f.endswith(".bitmap")]
  206. bitmap_path = os.path.join(pack_dir, bitmap_files[0])
  207. # Read the raw file to verify header
  208. with open(bitmap_path, "rb") as f:
  209. signature = f.read(4)
  210. self.assertEqual(b"BITM", signature, "Invalid bitmap signature")
  211. version = int.from_bytes(f.read(2), byteorder="big")
  212. self.assertGreaterEqual(version, 1, "Bitmap version should be >= 1")
  213. # Load with Dulwich and verify structure
  214. bitmap_name = bitmap_files[0]
  215. pack_basename = bitmap_name.replace(".bitmap", "")
  216. pack_path = os.path.join(pack_dir, pack_basename)
  217. with Pack(pack_path) as pack:
  218. bitmap = pack.bitmap
  219. self.assertIsNotNone(bitmap)
  220. self.assertEqual(bitmap.version, version)