test_partial_clone.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. import os
  23. import tempfile
  24. from dulwich.object_store import MemoryObjectStore
  25. from dulwich.objects import Blob, Tree
  26. from dulwich.partial_clone import (
  27. BlobLimitFilter,
  28. BlobNoneFilter,
  29. CombineFilter,
  30. SparseOidFilter,
  31. TreeDepthFilter,
  32. filter_pack_objects,
  33. parse_filter_spec,
  34. )
  35. from dulwich.repo import Repo
  36. from dulwich.tests.utils import make_commit
  37. from . import TestCase
  38. class ParseFilterSpecTests(TestCase):
  39. """Test parse_filter_spec function."""
  40. def test_parse_blob_none(self):
  41. """Test parsing 'blob:none' filter."""
  42. filter_spec = parse_filter_spec("blob:none")
  43. self.assertIsInstance(filter_spec, BlobNoneFilter)
  44. self.assertEqual("blob:none", filter_spec.to_spec_string())
  45. def test_parse_blob_none_bytes(self):
  46. """Test parsing 'blob:none' as bytes."""
  47. filter_spec = parse_filter_spec(b"blob:none")
  48. self.assertIsInstance(filter_spec, BlobNoneFilter)
  49. def test_parse_blob_limit_bytes(self):
  50. """Test parsing 'blob:limit=100' in bytes."""
  51. filter_spec = parse_filter_spec("blob:limit=100")
  52. self.assertIsInstance(filter_spec, BlobLimitFilter)
  53. self.assertEqual(100, filter_spec.limit)
  54. def test_parse_blob_limit_kb(self):
  55. """Test parsing 'blob:limit=10k'."""
  56. filter_spec = parse_filter_spec("blob:limit=10k")
  57. self.assertIsInstance(filter_spec, BlobLimitFilter)
  58. self.assertEqual(10 * 1024, filter_spec.limit)
  59. def test_parse_blob_limit_mb(self):
  60. """Test parsing 'blob:limit=5m'."""
  61. filter_spec = parse_filter_spec("blob:limit=5m")
  62. self.assertIsInstance(filter_spec, BlobLimitFilter)
  63. self.assertEqual(5 * 1024 * 1024, filter_spec.limit)
  64. def test_parse_blob_limit_gb(self):
  65. """Test parsing 'blob:limit=1g'."""
  66. filter_spec = parse_filter_spec("blob:limit=1g")
  67. self.assertIsInstance(filter_spec, BlobLimitFilter)
  68. self.assertEqual(1024 * 1024 * 1024, filter_spec.limit)
  69. def test_parse_tree_depth(self):
  70. """Test parsing 'tree:0' filter."""
  71. filter_spec = parse_filter_spec("tree:0")
  72. self.assertIsInstance(filter_spec, TreeDepthFilter)
  73. self.assertEqual(0, filter_spec.max_depth)
  74. def test_parse_tree_depth_nonzero(self):
  75. """Test parsing 'tree:3' filter."""
  76. filter_spec = parse_filter_spec("tree:3")
  77. self.assertIsInstance(filter_spec, TreeDepthFilter)
  78. self.assertEqual(3, filter_spec.max_depth)
  79. def test_parse_sparse_oid(self):
  80. """Test parsing 'sparse:oid=<oid>' filter."""
  81. oid = b"1234567890abcdef1234567890abcdef12345678"
  82. filter_spec = parse_filter_spec(f"sparse:oid={oid.decode('ascii')}")
  83. self.assertIsInstance(filter_spec, SparseOidFilter)
  84. self.assertEqual(oid, filter_spec.oid)
  85. def test_parse_combine(self):
  86. """Test parsing 'combine:blob:none+tree:0' filter."""
  87. filter_spec = parse_filter_spec("combine:blob:none+tree:0")
  88. self.assertIsInstance(filter_spec, CombineFilter)
  89. self.assertEqual(2, len(filter_spec.filters))
  90. self.assertIsInstance(filter_spec.filters[0], BlobNoneFilter)
  91. self.assertIsInstance(filter_spec.filters[1], TreeDepthFilter)
  92. def test_parse_combine_multiple(self):
  93. """Test parsing combine filter with 3+ filters."""
  94. filter_spec = parse_filter_spec("combine:blob:none+tree:0+blob:limit=1m")
  95. self.assertIsInstance(filter_spec, CombineFilter)
  96. self.assertEqual(3, len(filter_spec.filters))
  97. def test_parse_unknown_spec(self):
  98. """Test that unknown filter specs raise ValueError."""
  99. with self.assertRaises(ValueError) as cm:
  100. parse_filter_spec("unknown:spec")
  101. self.assertIn("Unknown filter specification", str(cm.exception))
  102. def test_parse_invalid_tree_depth(self):
  103. """Test that invalid tree depth raises ValueError."""
  104. with self.assertRaises(ValueError) as cm:
  105. parse_filter_spec("tree:invalid")
  106. self.assertIn("Invalid tree filter", str(cm.exception))
  107. def test_parse_invalid_blob_limit(self):
  108. """Test that invalid blob limit raises ValueError."""
  109. with self.assertRaises(ValueError) as cm:
  110. parse_filter_spec("blob:limit=invalid")
  111. self.assertIn("Invalid", str(cm.exception))
  112. def test_parse_empty_spec(self):
  113. """Test that empty filter spec raises ValueError."""
  114. with self.assertRaises(ValueError) as cm:
  115. parse_filter_spec("")
  116. self.assertIn("cannot be empty", str(cm.exception))
  117. def test_parse_blob_limit_no_value(self):
  118. """Test that blob:limit without value raises ValueError."""
  119. with self.assertRaises(ValueError) as cm:
  120. parse_filter_spec("blob:limit=")
  121. self.assertIn("requires a size value", str(cm.exception))
  122. def test_parse_tree_no_value(self):
  123. """Test that tree: without depth raises ValueError."""
  124. with self.assertRaises(ValueError) as cm:
  125. parse_filter_spec("tree:")
  126. self.assertIn("requires a depth value", str(cm.exception))
  127. def test_parse_tree_negative_depth(self):
  128. """Test that negative tree depth raises ValueError."""
  129. with self.assertRaises(ValueError) as cm:
  130. parse_filter_spec("tree:-1")
  131. self.assertIn("non-negative", str(cm.exception))
  132. def test_parse_sparse_oid_invalid_length(self):
  133. """Test that invalid OID length raises ValueError."""
  134. with self.assertRaises(ValueError) as cm:
  135. parse_filter_spec("sparse:oid=abc123")
  136. self.assertIn("40 or 64 hex chars", str(cm.exception))
  137. def test_parse_sparse_oid_invalid_hex(self):
  138. """Test that non-hex OID raises ValueError."""
  139. with self.assertRaises(ValueError) as cm:
  140. parse_filter_spec("sparse:oid=" + "x" * 40)
  141. self.assertIn("hexadecimal", str(cm.exception))
  142. def test_parse_combine_single_filter(self):
  143. """Test that combine with single filter raises ValueError."""
  144. with self.assertRaises(ValueError) as cm:
  145. parse_filter_spec("combine:blob:none")
  146. self.assertIn("at least two filters", str(cm.exception))
  147. def test_parse_unknown_with_helpful_message(self):
  148. """Test that unknown spec gives helpful error message."""
  149. with self.assertRaises(ValueError) as cm:
  150. parse_filter_spec("unknown:spec")
  151. error_msg = str(cm.exception)
  152. self.assertIn("Unknown filter specification", error_msg)
  153. self.assertIn("Supported formats", error_msg)
  154. self.assertIn("blob:none", error_msg)
  155. class BlobNoneFilterTests(TestCase):
  156. """Test BlobNoneFilter class."""
  157. def test_should_include_blob(self):
  158. """Test that BlobNoneFilter excludes all blobs."""
  159. filter_spec = BlobNoneFilter()
  160. self.assertFalse(filter_spec.should_include_blob(0))
  161. self.assertFalse(filter_spec.should_include_blob(100))
  162. self.assertFalse(filter_spec.should_include_blob(1024 * 1024))
  163. def test_should_include_tree(self):
  164. """Test that BlobNoneFilter includes all trees."""
  165. filter_spec = BlobNoneFilter()
  166. self.assertTrue(filter_spec.should_include_tree(0))
  167. self.assertTrue(filter_spec.should_include_tree(1))
  168. self.assertTrue(filter_spec.should_include_tree(100))
  169. def test_to_spec_string(self):
  170. """Test conversion back to spec string."""
  171. filter_spec = BlobNoneFilter()
  172. self.assertEqual("blob:none", filter_spec.to_spec_string())
  173. def test_repr(self):
  174. """Test repr output."""
  175. filter_spec = BlobNoneFilter()
  176. self.assertEqual("BlobNoneFilter()", repr(filter_spec))
  177. class BlobLimitFilterTests(TestCase):
  178. """Test BlobLimitFilter class."""
  179. def test_should_include_blob_under_limit(self):
  180. """Test that blobs under limit are included."""
  181. filter_spec = BlobLimitFilter(1024)
  182. self.assertTrue(filter_spec.should_include_blob(0))
  183. self.assertTrue(filter_spec.should_include_blob(512))
  184. self.assertTrue(filter_spec.should_include_blob(1024))
  185. def test_should_include_blob_over_limit(self):
  186. """Test that blobs over limit are excluded."""
  187. filter_spec = BlobLimitFilter(1024)
  188. self.assertFalse(filter_spec.should_include_blob(1025))
  189. self.assertFalse(filter_spec.should_include_blob(2048))
  190. def test_should_include_tree(self):
  191. """Test that BlobLimitFilter includes all trees."""
  192. filter_spec = BlobLimitFilter(1024)
  193. self.assertTrue(filter_spec.should_include_tree(0))
  194. self.assertTrue(filter_spec.should_include_tree(100))
  195. def test_to_spec_string_bytes(self):
  196. """Test conversion to spec string with bytes."""
  197. filter_spec = BlobLimitFilter(100)
  198. self.assertEqual("blob:limit=100", filter_spec.to_spec_string())
  199. def test_to_spec_string_kb(self):
  200. """Test conversion to spec string with KB."""
  201. filter_spec = BlobLimitFilter(10 * 1024)
  202. self.assertEqual("blob:limit=10k", filter_spec.to_spec_string())
  203. def test_to_spec_string_mb(self):
  204. """Test conversion to spec string with MB."""
  205. filter_spec = BlobLimitFilter(5 * 1024 * 1024)
  206. self.assertEqual("blob:limit=5m", filter_spec.to_spec_string())
  207. def test_to_spec_string_gb(self):
  208. """Test conversion to spec string with GB."""
  209. filter_spec = BlobLimitFilter(2 * 1024 * 1024 * 1024)
  210. self.assertEqual("blob:limit=2g", filter_spec.to_spec_string())
  211. def test_to_spec_string_not_round(self):
  212. """Test conversion to spec string with non-round size."""
  213. filter_spec = BlobLimitFilter(1500)
  214. self.assertEqual("blob:limit=1500", filter_spec.to_spec_string())
  215. def test_repr(self):
  216. """Test repr output."""
  217. filter_spec = BlobLimitFilter(1024)
  218. self.assertEqual("BlobLimitFilter(limit=1024)", repr(filter_spec))
  219. class TreeDepthFilterTests(TestCase):
  220. """Test TreeDepthFilter class."""
  221. def test_should_include_blob(self):
  222. """Test that TreeDepthFilter includes all blobs."""
  223. filter_spec = TreeDepthFilter(0)
  224. self.assertTrue(filter_spec.should_include_blob(0))
  225. self.assertTrue(filter_spec.should_include_blob(1024))
  226. def test_should_include_tree_at_depth(self):
  227. """Test that trees at or below max_depth are included."""
  228. filter_spec = TreeDepthFilter(2)
  229. self.assertTrue(filter_spec.should_include_tree(0))
  230. self.assertTrue(filter_spec.should_include_tree(1))
  231. self.assertTrue(filter_spec.should_include_tree(2))
  232. def test_should_include_tree_beyond_depth(self):
  233. """Test that trees beyond max_depth are excluded."""
  234. filter_spec = TreeDepthFilter(2)
  235. self.assertFalse(filter_spec.should_include_tree(3))
  236. self.assertFalse(filter_spec.should_include_tree(10))
  237. def test_to_spec_string(self):
  238. """Test conversion back to spec string."""
  239. filter_spec = TreeDepthFilter(3)
  240. self.assertEqual("tree:3", filter_spec.to_spec_string())
  241. def test_repr(self):
  242. """Test repr output."""
  243. filter_spec = TreeDepthFilter(2)
  244. self.assertEqual("TreeDepthFilter(max_depth=2)", repr(filter_spec))
  245. class SparseOidFilterTests(TestCase):
  246. """Test SparseOidFilter class."""
  247. def test_should_include_blob(self):
  248. """Test that SparseOidFilter includes all blobs."""
  249. oid = b"1234567890abcdef1234567890abcdef12345678"
  250. filter_spec = SparseOidFilter(oid)
  251. self.assertTrue(filter_spec.should_include_blob(0))
  252. self.assertTrue(filter_spec.should_include_blob(1024))
  253. def test_should_include_tree(self):
  254. """Test that SparseOidFilter includes all trees."""
  255. oid = b"1234567890abcdef1234567890abcdef12345678"
  256. filter_spec = SparseOidFilter(oid)
  257. self.assertTrue(filter_spec.should_include_tree(0))
  258. self.assertTrue(filter_spec.should_include_tree(10))
  259. def test_to_spec_string(self):
  260. """Test conversion back to spec string."""
  261. oid = b"1234567890abcdef1234567890abcdef12345678"
  262. filter_spec = SparseOidFilter(oid)
  263. expected = "sparse:oid=1234567890abcdef1234567890abcdef12345678"
  264. self.assertEqual(expected, filter_spec.to_spec_string())
  265. def test_repr(self):
  266. """Test repr output."""
  267. oid = b"1234567890abcdef1234567890abcdef12345678"
  268. filter_spec = SparseOidFilter(oid)
  269. self.assertIn("SparseOidFilter", repr(filter_spec))
  270. self.assertIn("1234567890abcdef1234567890abcdef12345678", repr(filter_spec))
  271. class CombineFilterTests(TestCase):
  272. """Test CombineFilter class."""
  273. def test_should_include_blob_all_allow(self):
  274. """Test that blob is included when all filters allow it."""
  275. filters = [BlobLimitFilter(1024), BlobLimitFilter(2048)]
  276. filter_spec = CombineFilter(filters)
  277. self.assertTrue(filter_spec.should_include_blob(512))
  278. def test_should_include_blob_one_denies(self):
  279. """Test that blob is excluded when one filter denies it."""
  280. filters = [BlobLimitFilter(1024), BlobNoneFilter()]
  281. filter_spec = CombineFilter(filters)
  282. self.assertFalse(filter_spec.should_include_blob(512))
  283. def test_should_include_tree_all_allow(self):
  284. """Test that tree is included when all filters allow it."""
  285. filters = [TreeDepthFilter(2), TreeDepthFilter(3)]
  286. filter_spec = CombineFilter(filters)
  287. self.assertTrue(filter_spec.should_include_tree(1))
  288. def test_should_include_tree_one_denies(self):
  289. """Test that tree is excluded when one filter denies it."""
  290. filters = [TreeDepthFilter(2), TreeDepthFilter(1)]
  291. filter_spec = CombineFilter(filters)
  292. self.assertFalse(filter_spec.should_include_tree(2))
  293. def test_to_spec_string(self):
  294. """Test conversion back to spec string."""
  295. filters = [BlobNoneFilter(), TreeDepthFilter(0)]
  296. filter_spec = CombineFilter(filters)
  297. self.assertEqual("combine:blob:none+tree:0", filter_spec.to_spec_string())
  298. def test_repr(self):
  299. """Test repr output."""
  300. filters = [BlobNoneFilter()]
  301. filter_spec = CombineFilter(filters)
  302. self.assertIn("CombineFilter", repr(filter_spec))
  303. class FilterPackObjectsTests(TestCase):
  304. """Test filter_pack_objects function."""
  305. def setUp(self):
  306. super().setUp()
  307. self.store = MemoryObjectStore()
  308. # Create test objects
  309. self.small_blob = Blob.from_string(b"small")
  310. self.large_blob = Blob.from_string(b"x" * 2000)
  311. self.tree = Tree()
  312. self.commit = make_commit(tree=self.tree.id)
  313. # Add objects to store
  314. self.store.add_object(self.small_blob)
  315. self.store.add_object(self.large_blob)
  316. self.store.add_object(self.tree)
  317. self.store.add_object(self.commit)
  318. def test_filter_blob_none(self):
  319. """Test that blob:none filter excludes all blobs."""
  320. object_ids = [
  321. self.small_blob.id,
  322. self.large_blob.id,
  323. self.tree.id,
  324. self.commit.id,
  325. ]
  326. filter_spec = BlobNoneFilter()
  327. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  328. # Should exclude both blobs but keep tree and commit
  329. self.assertNotIn(self.small_blob.id, filtered)
  330. self.assertNotIn(self.large_blob.id, filtered)
  331. self.assertIn(self.tree.id, filtered)
  332. self.assertIn(self.commit.id, filtered)
  333. def test_filter_blob_limit(self):
  334. """Test that blob:limit filter excludes blobs over size limit."""
  335. object_ids = [
  336. self.small_blob.id,
  337. self.large_blob.id,
  338. self.tree.id,
  339. ]
  340. # Set limit to 100 bytes
  341. filter_spec = BlobLimitFilter(100)
  342. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  343. # Should keep small blob but exclude large blob
  344. self.assertIn(self.small_blob.id, filtered)
  345. self.assertNotIn(self.large_blob.id, filtered)
  346. self.assertIn(self.tree.id, filtered)
  347. def test_filter_no_filter_keeps_all(self):
  348. """Test that without filtering all objects are kept."""
  349. # Create a filter that includes everything
  350. filter_spec = BlobLimitFilter(10000) # Large limit
  351. object_ids = [
  352. self.small_blob.id,
  353. self.large_blob.id,
  354. self.tree.id,
  355. self.commit.id,
  356. ]
  357. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  358. # All objects should be included
  359. self.assertEqual(len(filtered), len(object_ids))
  360. for oid in object_ids:
  361. self.assertIn(oid, filtered)
  362. def test_filter_missing_object(self):
  363. """Test that missing objects are skipped without error."""
  364. from dulwich.objects import ObjectID
  365. fake_id = ObjectID(b"0" * 40)
  366. object_ids = [fake_id, self.small_blob.id]
  367. filter_spec = BlobNoneFilter()
  368. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  369. # Should skip the missing object
  370. self.assertNotIn(fake_id, filtered)
  371. def test_filter_combine(self):
  372. """Test combined filters."""
  373. object_ids = [
  374. self.small_blob.id,
  375. self.large_blob.id,
  376. self.tree.id,
  377. ]
  378. # Combine blob:limit with another filter
  379. filter_spec = CombineFilter([
  380. BlobLimitFilter(100),
  381. BlobNoneFilter(), # This will exclude ALL blobs
  382. ])
  383. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  384. # Should exclude all blobs due to BlobNoneFilter
  385. self.assertNotIn(self.small_blob.id, filtered)
  386. self.assertNotIn(self.large_blob.id, filtered)
  387. self.assertIn(self.tree.id, filtered)
  388. class PartialCloneIntegrationTests(TestCase):
  389. """Integration tests for partial clone with real repositories."""
  390. def setUp(self):
  391. super().setUp()
  392. self.repo_dir = tempfile.mkdtemp()
  393. self.addCleanup(self._cleanup)
  394. self.repo = Repo.init(self.repo_dir)
  395. def _cleanup(self):
  396. """Clean up test repository."""
  397. import shutil
  398. if os.path.exists(self.repo_dir):
  399. shutil.rmtree(self.repo_dir)
  400. def test_blob_none_filter_with_real_repo(self):
  401. """Test blob:none filter excludes blobs in real repository."""
  402. # Create a tree with files
  403. tree = Tree()
  404. # Add some blobs to the tree
  405. blob1 = Blob.from_string(b"file1 content")
  406. blob2 = Blob.from_string(b"file2 content")
  407. tree.add(b"file1.txt", 0o100644, blob1.id)
  408. tree.add(b"file2.txt", 0o100644, blob2.id)
  409. # Add objects to repo
  410. self.repo.object_store.add_object(blob1)
  411. self.repo.object_store.add_object(blob2)
  412. self.repo.object_store.add_object(tree)
  413. # Create commit
  414. commit = make_commit(tree=tree.id, message=b"Test commit")
  415. self.repo.object_store.add_object(commit)
  416. # Get all objects
  417. object_ids = [blob1.id, blob2.id, tree.id, commit.id]
  418. # Apply blob:none filter
  419. filter_spec = BlobNoneFilter()
  420. filtered = filter_pack_objects(
  421. self.repo.object_store, object_ids, filter_spec
  422. )
  423. # Verify blobs are excluded
  424. self.assertNotIn(blob1.id, filtered)
  425. self.assertNotIn(blob2.id, filtered)
  426. # But tree and commit are included
  427. self.assertIn(tree.id, filtered)
  428. self.assertIn(commit.id, filtered)
  429. # Verify we have only 2 objects (tree + commit)
  430. self.assertEqual(2, len(filtered))
  431. def test_blob_limit_filter_with_mixed_sizes(self):
  432. """Test blob:limit filter with mixed blob sizes."""
  433. tree = Tree()
  434. # Create blobs of different sizes
  435. small_blob = Blob.from_string(b"small") # 5 bytes
  436. medium_blob = Blob.from_string(b"x" * 50) # 50 bytes
  437. large_blob = Blob.from_string(b"y" * 500) # 500 bytes
  438. tree.add(b"small.txt", 0o100644, small_blob.id)
  439. tree.add(b"medium.txt", 0o100644, medium_blob.id)
  440. tree.add(b"large.txt", 0o100644, large_blob.id)
  441. # Add to repo
  442. self.repo.object_store.add_object(small_blob)
  443. self.repo.object_store.add_object(medium_blob)
  444. self.repo.object_store.add_object(large_blob)
  445. self.repo.object_store.add_object(tree)
  446. commit = make_commit(tree=tree.id)
  447. self.repo.object_store.add_object(commit)
  448. # Test with 100 byte limit
  449. object_ids = [
  450. small_blob.id,
  451. medium_blob.id,
  452. large_blob.id,
  453. tree.id,
  454. commit.id,
  455. ]
  456. filter_spec = BlobLimitFilter(100)
  457. filtered = filter_pack_objects(
  458. self.repo.object_store, object_ids, filter_spec
  459. )
  460. # Small and medium should be included
  461. self.assertIn(small_blob.id, filtered)
  462. self.assertIn(medium_blob.id, filtered)
  463. # Large should be excluded
  464. self.assertNotIn(large_blob.id, filtered)
  465. # Tree and commit included
  466. self.assertIn(tree.id, filtered)
  467. self.assertIn(commit.id, filtered)
  468. def test_combined_filter_integration(self):
  469. """Test combined filters in real scenario."""
  470. tree = Tree()
  471. blob1 = Blob.from_string(b"content1")
  472. blob2 = Blob.from_string(b"x" * 1000)
  473. tree.add(b"file1.txt", 0o100644, blob1.id)
  474. tree.add(b"file2.txt", 0o100644, blob2.id)
  475. self.repo.object_store.add_object(blob1)
  476. self.repo.object_store.add_object(blob2)
  477. self.repo.object_store.add_object(tree)
  478. commit = make_commit(tree=tree.id)
  479. self.repo.object_store.add_object(commit)
  480. # Combine: limit to 500 bytes, but also apply blob:none
  481. # This should exclude ALL blobs (blob:none overrides limit)
  482. filter_spec = CombineFilter([
  483. BlobLimitFilter(500),
  484. BlobNoneFilter(),
  485. ])
  486. object_ids = [blob1.id, blob2.id, tree.id, commit.id]
  487. filtered = filter_pack_objects(
  488. self.repo.object_store, object_ids, filter_spec
  489. )
  490. # All blobs excluded
  491. self.assertNotIn(blob1.id, filtered)
  492. self.assertNotIn(blob2.id, filtered)
  493. # Only tree and commit
  494. self.assertEqual(2, len(filtered))