test_partial_clone.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. # test_partial_clone.py -- Tests for partial clone filter specifications
  2. # Copyright (C) 2024 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 partial clone filter specifications."""
  22. from dulwich.object_store import MemoryObjectStore
  23. from dulwich.objects import Blob, Tree
  24. from dulwich.partial_clone import (
  25. BlobLimitFilter,
  26. BlobNoneFilter,
  27. CombineFilter,
  28. SparseOidFilter,
  29. TreeDepthFilter,
  30. filter_pack_objects,
  31. parse_filter_spec,
  32. )
  33. from dulwich.tests.utils import make_commit
  34. from . import TestCase
  35. class ParseFilterSpecTests(TestCase):
  36. """Test parse_filter_spec function."""
  37. def test_parse_blob_none(self):
  38. """Test parsing 'blob:none' filter."""
  39. filter_spec = parse_filter_spec("blob:none")
  40. self.assertIsInstance(filter_spec, BlobNoneFilter)
  41. self.assertEqual("blob:none", filter_spec.to_spec_string())
  42. def test_parse_blob_none_bytes(self):
  43. """Test parsing 'blob:none' as bytes."""
  44. filter_spec = parse_filter_spec(b"blob:none")
  45. self.assertIsInstance(filter_spec, BlobNoneFilter)
  46. def test_parse_blob_limit_bytes(self):
  47. """Test parsing 'blob:limit=100' in bytes."""
  48. filter_spec = parse_filter_spec("blob:limit=100")
  49. self.assertIsInstance(filter_spec, BlobLimitFilter)
  50. self.assertEqual(100, filter_spec.limit)
  51. def test_parse_blob_limit_kb(self):
  52. """Test parsing 'blob:limit=10k'."""
  53. filter_spec = parse_filter_spec("blob:limit=10k")
  54. self.assertIsInstance(filter_spec, BlobLimitFilter)
  55. self.assertEqual(10 * 1024, filter_spec.limit)
  56. def test_parse_blob_limit_mb(self):
  57. """Test parsing 'blob:limit=5m'."""
  58. filter_spec = parse_filter_spec("blob:limit=5m")
  59. self.assertIsInstance(filter_spec, BlobLimitFilter)
  60. self.assertEqual(5 * 1024 * 1024, filter_spec.limit)
  61. def test_parse_blob_limit_gb(self):
  62. """Test parsing 'blob:limit=1g'."""
  63. filter_spec = parse_filter_spec("blob:limit=1g")
  64. self.assertIsInstance(filter_spec, BlobLimitFilter)
  65. self.assertEqual(1024 * 1024 * 1024, filter_spec.limit)
  66. def test_parse_tree_depth(self):
  67. """Test parsing 'tree:0' filter."""
  68. filter_spec = parse_filter_spec("tree:0")
  69. self.assertIsInstance(filter_spec, TreeDepthFilter)
  70. self.assertEqual(0, filter_spec.max_depth)
  71. def test_parse_tree_depth_nonzero(self):
  72. """Test parsing 'tree:3' filter."""
  73. filter_spec = parse_filter_spec("tree:3")
  74. self.assertIsInstance(filter_spec, TreeDepthFilter)
  75. self.assertEqual(3, filter_spec.max_depth)
  76. def test_parse_sparse_oid(self):
  77. """Test parsing 'sparse:oid=<oid>' filter."""
  78. oid = b"1234567890abcdef1234567890abcdef12345678"
  79. filter_spec = parse_filter_spec(f"sparse:oid={oid.decode('ascii')}")
  80. self.assertIsInstance(filter_spec, SparseOidFilter)
  81. self.assertEqual(oid, filter_spec.oid)
  82. def test_parse_combine(self):
  83. """Test parsing 'combine:blob:none+tree:0' filter."""
  84. filter_spec = parse_filter_spec("combine:blob:none+tree:0")
  85. self.assertIsInstance(filter_spec, CombineFilter)
  86. self.assertEqual(2, len(filter_spec.filters))
  87. self.assertIsInstance(filter_spec.filters[0], BlobNoneFilter)
  88. self.assertIsInstance(filter_spec.filters[1], TreeDepthFilter)
  89. def test_parse_combine_multiple(self):
  90. """Test parsing combine filter with 3+ filters."""
  91. filter_spec = parse_filter_spec("combine:blob:none+tree:0+blob:limit=1m")
  92. self.assertIsInstance(filter_spec, CombineFilter)
  93. self.assertEqual(3, len(filter_spec.filters))
  94. def test_parse_unknown_spec(self):
  95. """Test that unknown filter specs raise ValueError."""
  96. with self.assertRaises(ValueError) as cm:
  97. parse_filter_spec("unknown:spec")
  98. self.assertIn("Unknown filter specification", str(cm.exception))
  99. def test_parse_invalid_tree_depth(self):
  100. """Test that invalid tree depth raises ValueError."""
  101. with self.assertRaises(ValueError) as cm:
  102. parse_filter_spec("tree:invalid")
  103. self.assertIn("Invalid tree depth", str(cm.exception))
  104. def test_parse_invalid_blob_limit(self):
  105. """Test that invalid blob limit raises ValueError."""
  106. with self.assertRaises(ValueError) as cm:
  107. parse_filter_spec("blob:limit=invalid")
  108. self.assertIn("Invalid size specification", str(cm.exception))
  109. class BlobNoneFilterTests(TestCase):
  110. """Test BlobNoneFilter class."""
  111. def test_should_include_blob(self):
  112. """Test that BlobNoneFilter excludes all blobs."""
  113. filter_spec = BlobNoneFilter()
  114. self.assertFalse(filter_spec.should_include_blob(0))
  115. self.assertFalse(filter_spec.should_include_blob(100))
  116. self.assertFalse(filter_spec.should_include_blob(1024 * 1024))
  117. def test_should_include_tree(self):
  118. """Test that BlobNoneFilter includes all trees."""
  119. filter_spec = BlobNoneFilter()
  120. self.assertTrue(filter_spec.should_include_tree(0))
  121. self.assertTrue(filter_spec.should_include_tree(1))
  122. self.assertTrue(filter_spec.should_include_tree(100))
  123. def test_to_spec_string(self):
  124. """Test conversion back to spec string."""
  125. filter_spec = BlobNoneFilter()
  126. self.assertEqual("blob:none", filter_spec.to_spec_string())
  127. def test_repr(self):
  128. """Test repr output."""
  129. filter_spec = BlobNoneFilter()
  130. self.assertEqual("BlobNoneFilter()", repr(filter_spec))
  131. class BlobLimitFilterTests(TestCase):
  132. """Test BlobLimitFilter class."""
  133. def test_should_include_blob_under_limit(self):
  134. """Test that blobs under limit are included."""
  135. filter_spec = BlobLimitFilter(1024)
  136. self.assertTrue(filter_spec.should_include_blob(0))
  137. self.assertTrue(filter_spec.should_include_blob(512))
  138. self.assertTrue(filter_spec.should_include_blob(1024))
  139. def test_should_include_blob_over_limit(self):
  140. """Test that blobs over limit are excluded."""
  141. filter_spec = BlobLimitFilter(1024)
  142. self.assertFalse(filter_spec.should_include_blob(1025))
  143. self.assertFalse(filter_spec.should_include_blob(2048))
  144. def test_should_include_tree(self):
  145. """Test that BlobLimitFilter includes all trees."""
  146. filter_spec = BlobLimitFilter(1024)
  147. self.assertTrue(filter_spec.should_include_tree(0))
  148. self.assertTrue(filter_spec.should_include_tree(100))
  149. def test_to_spec_string_bytes(self):
  150. """Test conversion to spec string with bytes."""
  151. filter_spec = BlobLimitFilter(100)
  152. self.assertEqual("blob:limit=100", filter_spec.to_spec_string())
  153. def test_to_spec_string_kb(self):
  154. """Test conversion to spec string with KB."""
  155. filter_spec = BlobLimitFilter(10 * 1024)
  156. self.assertEqual("blob:limit=10k", filter_spec.to_spec_string())
  157. def test_to_spec_string_mb(self):
  158. """Test conversion to spec string with MB."""
  159. filter_spec = BlobLimitFilter(5 * 1024 * 1024)
  160. self.assertEqual("blob:limit=5m", filter_spec.to_spec_string())
  161. def test_to_spec_string_gb(self):
  162. """Test conversion to spec string with GB."""
  163. filter_spec = BlobLimitFilter(2 * 1024 * 1024 * 1024)
  164. self.assertEqual("blob:limit=2g", filter_spec.to_spec_string())
  165. def test_to_spec_string_not_round(self):
  166. """Test conversion to spec string with non-round size."""
  167. filter_spec = BlobLimitFilter(1500)
  168. self.assertEqual("blob:limit=1500", filter_spec.to_spec_string())
  169. def test_repr(self):
  170. """Test repr output."""
  171. filter_spec = BlobLimitFilter(1024)
  172. self.assertEqual("BlobLimitFilter(limit=1024)", repr(filter_spec))
  173. class TreeDepthFilterTests(TestCase):
  174. """Test TreeDepthFilter class."""
  175. def test_should_include_blob(self):
  176. """Test that TreeDepthFilter includes all blobs."""
  177. filter_spec = TreeDepthFilter(0)
  178. self.assertTrue(filter_spec.should_include_blob(0))
  179. self.assertTrue(filter_spec.should_include_blob(1024))
  180. def test_should_include_tree_at_depth(self):
  181. """Test that trees at or below max_depth are included."""
  182. filter_spec = TreeDepthFilter(2)
  183. self.assertTrue(filter_spec.should_include_tree(0))
  184. self.assertTrue(filter_spec.should_include_tree(1))
  185. self.assertTrue(filter_spec.should_include_tree(2))
  186. def test_should_include_tree_beyond_depth(self):
  187. """Test that trees beyond max_depth are excluded."""
  188. filter_spec = TreeDepthFilter(2)
  189. self.assertFalse(filter_spec.should_include_tree(3))
  190. self.assertFalse(filter_spec.should_include_tree(10))
  191. def test_to_spec_string(self):
  192. """Test conversion back to spec string."""
  193. filter_spec = TreeDepthFilter(3)
  194. self.assertEqual("tree:3", filter_spec.to_spec_string())
  195. def test_repr(self):
  196. """Test repr output."""
  197. filter_spec = TreeDepthFilter(2)
  198. self.assertEqual("TreeDepthFilter(max_depth=2)", repr(filter_spec))
  199. class SparseOidFilterTests(TestCase):
  200. """Test SparseOidFilter class."""
  201. def test_should_include_blob(self):
  202. """Test that SparseOidFilter includes all blobs."""
  203. oid = b"1234567890abcdef1234567890abcdef12345678"
  204. filter_spec = SparseOidFilter(oid)
  205. self.assertTrue(filter_spec.should_include_blob(0))
  206. self.assertTrue(filter_spec.should_include_blob(1024))
  207. def test_should_include_tree(self):
  208. """Test that SparseOidFilter includes all trees."""
  209. oid = b"1234567890abcdef1234567890abcdef12345678"
  210. filter_spec = SparseOidFilter(oid)
  211. self.assertTrue(filter_spec.should_include_tree(0))
  212. self.assertTrue(filter_spec.should_include_tree(10))
  213. def test_to_spec_string(self):
  214. """Test conversion back to spec string."""
  215. oid = b"1234567890abcdef1234567890abcdef12345678"
  216. filter_spec = SparseOidFilter(oid)
  217. expected = "sparse:oid=1234567890abcdef1234567890abcdef12345678"
  218. self.assertEqual(expected, filter_spec.to_spec_string())
  219. def test_repr(self):
  220. """Test repr output."""
  221. oid = b"1234567890abcdef1234567890abcdef12345678"
  222. filter_spec = SparseOidFilter(oid)
  223. self.assertIn("SparseOidFilter", repr(filter_spec))
  224. self.assertIn("1234567890abcdef1234567890abcdef12345678", repr(filter_spec))
  225. class CombineFilterTests(TestCase):
  226. """Test CombineFilter class."""
  227. def test_should_include_blob_all_allow(self):
  228. """Test that blob is included when all filters allow it."""
  229. filters = [BlobLimitFilter(1024), BlobLimitFilter(2048)]
  230. filter_spec = CombineFilter(filters)
  231. self.assertTrue(filter_spec.should_include_blob(512))
  232. def test_should_include_blob_one_denies(self):
  233. """Test that blob is excluded when one filter denies it."""
  234. filters = [BlobLimitFilter(1024), BlobNoneFilter()]
  235. filter_spec = CombineFilter(filters)
  236. self.assertFalse(filter_spec.should_include_blob(512))
  237. def test_should_include_tree_all_allow(self):
  238. """Test that tree is included when all filters allow it."""
  239. filters = [TreeDepthFilter(2), TreeDepthFilter(3)]
  240. filter_spec = CombineFilter(filters)
  241. self.assertTrue(filter_spec.should_include_tree(1))
  242. def test_should_include_tree_one_denies(self):
  243. """Test that tree is excluded when one filter denies it."""
  244. filters = [TreeDepthFilter(2), TreeDepthFilter(1)]
  245. filter_spec = CombineFilter(filters)
  246. self.assertFalse(filter_spec.should_include_tree(2))
  247. def test_to_spec_string(self):
  248. """Test conversion back to spec string."""
  249. filters = [BlobNoneFilter(), TreeDepthFilter(0)]
  250. filter_spec = CombineFilter(filters)
  251. self.assertEqual("combine:blob:none+tree:0", filter_spec.to_spec_string())
  252. def test_repr(self):
  253. """Test repr output."""
  254. filters = [BlobNoneFilter()]
  255. filter_spec = CombineFilter(filters)
  256. self.assertIn("CombineFilter", repr(filter_spec))
  257. class FilterPackObjectsTests(TestCase):
  258. """Test filter_pack_objects function."""
  259. def setUp(self):
  260. super().setUp()
  261. self.store = MemoryObjectStore()
  262. # Create test objects
  263. self.small_blob = Blob.from_string(b"small")
  264. self.large_blob = Blob.from_string(b"x" * 2000)
  265. self.tree = Tree()
  266. self.commit = make_commit(tree=self.tree.id)
  267. # Add objects to store
  268. self.store.add_object(self.small_blob)
  269. self.store.add_object(self.large_blob)
  270. self.store.add_object(self.tree)
  271. self.store.add_object(self.commit)
  272. def test_filter_blob_none(self):
  273. """Test that blob:none filter excludes all blobs."""
  274. object_ids = [
  275. self.small_blob.id,
  276. self.large_blob.id,
  277. self.tree.id,
  278. self.commit.id,
  279. ]
  280. filter_spec = BlobNoneFilter()
  281. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  282. # Should exclude both blobs but keep tree and commit
  283. self.assertNotIn(self.small_blob.id, filtered)
  284. self.assertNotIn(self.large_blob.id, filtered)
  285. self.assertIn(self.tree.id, filtered)
  286. self.assertIn(self.commit.id, filtered)
  287. def test_filter_blob_limit(self):
  288. """Test that blob:limit filter excludes blobs over size limit."""
  289. object_ids = [
  290. self.small_blob.id,
  291. self.large_blob.id,
  292. self.tree.id,
  293. ]
  294. # Set limit to 100 bytes
  295. filter_spec = BlobLimitFilter(100)
  296. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  297. # Should keep small blob but exclude large blob
  298. self.assertIn(self.small_blob.id, filtered)
  299. self.assertNotIn(self.large_blob.id, filtered)
  300. self.assertIn(self.tree.id, filtered)
  301. def test_filter_no_filter_keeps_all(self):
  302. """Test that without filtering all objects are kept."""
  303. # Create a filter that includes everything
  304. filter_spec = BlobLimitFilter(10000) # Large limit
  305. object_ids = [
  306. self.small_blob.id,
  307. self.large_blob.id,
  308. self.tree.id,
  309. self.commit.id,
  310. ]
  311. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  312. # All objects should be included
  313. self.assertEqual(len(filtered), len(object_ids))
  314. for oid in object_ids:
  315. self.assertIn(oid, filtered)
  316. def test_filter_missing_object(self):
  317. """Test that missing objects are skipped without error."""
  318. from dulwich.objects import ObjectID
  319. fake_id = ObjectID(b"0" * 40)
  320. object_ids = [fake_id, self.small_blob.id]
  321. filter_spec = BlobNoneFilter()
  322. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  323. # Should skip the missing object
  324. self.assertNotIn(fake_id, filtered)
  325. def test_filter_combine(self):
  326. """Test combined filters."""
  327. object_ids = [
  328. self.small_blob.id,
  329. self.large_blob.id,
  330. self.tree.id,
  331. ]
  332. # Combine blob:limit with another filter
  333. filter_spec = CombineFilter([
  334. BlobLimitFilter(100),
  335. BlobNoneFilter(), # This will exclude ALL blobs
  336. ])
  337. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  338. # Should exclude all blobs due to BlobNoneFilter
  339. self.assertNotIn(self.small_blob.id, filtered)
  340. self.assertNotIn(self.large_blob.id, filtered)
  341. self.assertIn(self.tree.id, filtered)