2
0

test_partial_clone.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. def test_load_patterns_from_blob(self):
  272. """Test loading sparse patterns from a blob object."""
  273. from dulwich.object_store import MemoryObjectStore
  274. from dulwich.objects import Blob
  275. # Create a sparse patterns blob
  276. patterns = b"*.txt\n!*.log\n/src/\n"
  277. blob = Blob.from_string(patterns)
  278. object_store = MemoryObjectStore()
  279. object_store.add_object(blob)
  280. filter_spec = SparseOidFilter(blob.id, object_store=object_store)
  281. filter_spec._load_patterns()
  282. # Verify patterns were loaded
  283. self.assertIsNotNone(filter_spec._patterns)
  284. self.assertEqual(3, len(filter_spec._patterns))
  285. def test_load_patterns_missing_blob(self):
  286. """Test error when sparse blob is not found."""
  287. from dulwich.object_store import MemoryObjectStore
  288. oid = b"1234567890abcdef1234567890abcdef12345678"
  289. object_store = MemoryObjectStore()
  290. filter_spec = SparseOidFilter(oid, object_store=object_store)
  291. with self.assertRaises(ValueError) as cm:
  292. filter_spec._load_patterns()
  293. self.assertIn("not found", str(cm.exception))
  294. def test_load_patterns_not_a_blob(self):
  295. """Test error when sparse OID points to non-blob object."""
  296. from dulwich.object_store import MemoryObjectStore
  297. from dulwich.objects import Tree
  298. tree = Tree()
  299. object_store = MemoryObjectStore()
  300. object_store.add_object(tree)
  301. filter_spec = SparseOidFilter(tree.id, object_store=object_store)
  302. with self.assertRaises(ValueError) as cm:
  303. filter_spec._load_patterns()
  304. self.assertIn("not a blob", str(cm.exception))
  305. def test_load_patterns_without_object_store(self):
  306. """Test error when trying to load patterns without object store."""
  307. oid = b"1234567890abcdef1234567890abcdef12345678"
  308. filter_spec = SparseOidFilter(oid)
  309. with self.assertRaises(ValueError) as cm:
  310. filter_spec._load_patterns()
  311. self.assertIn("without an object store", str(cm.exception))
  312. def test_should_include_path_matching(self):
  313. """Test path matching with sparse patterns."""
  314. from dulwich.object_store import MemoryObjectStore
  315. from dulwich.objects import Blob
  316. # Create a sparse patterns blob: include *.txt files
  317. patterns = b"*.txt\n"
  318. blob = Blob.from_string(patterns)
  319. object_store = MemoryObjectStore()
  320. object_store.add_object(blob)
  321. filter_spec = SparseOidFilter(blob.id, object_store=object_store)
  322. # .txt files should be included
  323. self.assertTrue(filter_spec.should_include_path("readme.txt"))
  324. self.assertTrue(filter_spec.should_include_path("docs/file.txt"))
  325. # Other files should not be included
  326. self.assertFalse(filter_spec.should_include_path("readme.md"))
  327. self.assertFalse(filter_spec.should_include_path("script.py"))
  328. def test_should_include_path_negation(self):
  329. """Test path matching with negation patterns."""
  330. from dulwich.object_store import MemoryObjectStore
  331. from dulwich.objects import Blob
  332. # Include all .txt files except logs
  333. patterns = b"*.txt\n!*.log\n"
  334. blob = Blob.from_string(patterns)
  335. object_store = MemoryObjectStore()
  336. object_store.add_object(blob)
  337. filter_spec = SparseOidFilter(blob.id, object_store=object_store)
  338. # .txt files should be included
  339. self.assertTrue(filter_spec.should_include_path("readme.txt"))
  340. # But .log files should be excluded (even though they end in .txt pattern)
  341. # Note: This depends on pattern order and sparse_patterns implementation
  342. self.assertFalse(filter_spec.should_include_path("debug.log"))
  343. class CombineFilterTests(TestCase):
  344. """Test CombineFilter class."""
  345. def test_should_include_blob_all_allow(self):
  346. """Test that blob is included when all filters allow it."""
  347. filters = [BlobLimitFilter(1024), BlobLimitFilter(2048)]
  348. filter_spec = CombineFilter(filters)
  349. self.assertTrue(filter_spec.should_include_blob(512))
  350. def test_should_include_blob_one_denies(self):
  351. """Test that blob is excluded when one filter denies it."""
  352. filters = [BlobLimitFilter(1024), BlobNoneFilter()]
  353. filter_spec = CombineFilter(filters)
  354. self.assertFalse(filter_spec.should_include_blob(512))
  355. def test_should_include_tree_all_allow(self):
  356. """Test that tree is included when all filters allow it."""
  357. filters = [TreeDepthFilter(2), TreeDepthFilter(3)]
  358. filter_spec = CombineFilter(filters)
  359. self.assertTrue(filter_spec.should_include_tree(1))
  360. def test_should_include_tree_one_denies(self):
  361. """Test that tree is excluded when one filter denies it."""
  362. filters = [TreeDepthFilter(2), TreeDepthFilter(1)]
  363. filter_spec = CombineFilter(filters)
  364. self.assertFalse(filter_spec.should_include_tree(2))
  365. def test_to_spec_string(self):
  366. """Test conversion back to spec string."""
  367. filters = [BlobNoneFilter(), TreeDepthFilter(0)]
  368. filter_spec = CombineFilter(filters)
  369. self.assertEqual("combine:blob:none+tree:0", filter_spec.to_spec_string())
  370. def test_repr(self):
  371. """Test repr output."""
  372. filters = [BlobNoneFilter()]
  373. filter_spec = CombineFilter(filters)
  374. self.assertIn("CombineFilter", repr(filter_spec))
  375. class FilterPackObjectsTests(TestCase):
  376. """Test filter_pack_objects function."""
  377. def setUp(self):
  378. super().setUp()
  379. self.store = MemoryObjectStore()
  380. # Create test objects
  381. self.small_blob = Blob.from_string(b"small")
  382. self.large_blob = Blob.from_string(b"x" * 2000)
  383. self.tree = Tree()
  384. self.commit = make_commit(tree=self.tree.id)
  385. # Add objects to store
  386. self.store.add_object(self.small_blob)
  387. self.store.add_object(self.large_blob)
  388. self.store.add_object(self.tree)
  389. self.store.add_object(self.commit)
  390. def test_filter_blob_none(self):
  391. """Test that blob:none filter excludes all blobs."""
  392. object_ids = [
  393. self.small_blob.id,
  394. self.large_blob.id,
  395. self.tree.id,
  396. self.commit.id,
  397. ]
  398. filter_spec = BlobNoneFilter()
  399. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  400. # Should exclude both blobs but keep tree and commit
  401. self.assertNotIn(self.small_blob.id, filtered)
  402. self.assertNotIn(self.large_blob.id, filtered)
  403. self.assertIn(self.tree.id, filtered)
  404. self.assertIn(self.commit.id, filtered)
  405. def test_filter_blob_limit(self):
  406. """Test that blob:limit filter excludes blobs over size limit."""
  407. object_ids = [
  408. self.small_blob.id,
  409. self.large_blob.id,
  410. self.tree.id,
  411. ]
  412. # Set limit to 100 bytes
  413. filter_spec = BlobLimitFilter(100)
  414. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  415. # Should keep small blob but exclude large blob
  416. self.assertIn(self.small_blob.id, filtered)
  417. self.assertNotIn(self.large_blob.id, filtered)
  418. self.assertIn(self.tree.id, filtered)
  419. def test_filter_no_filter_keeps_all(self):
  420. """Test that without filtering all objects are kept."""
  421. # Create a filter that includes everything
  422. filter_spec = BlobLimitFilter(10000) # Large limit
  423. object_ids = [
  424. self.small_blob.id,
  425. self.large_blob.id,
  426. self.tree.id,
  427. self.commit.id,
  428. ]
  429. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  430. # All objects should be included
  431. self.assertEqual(len(filtered), len(object_ids))
  432. for oid in object_ids:
  433. self.assertIn(oid, filtered)
  434. def test_filter_missing_object(self):
  435. """Test that missing objects are skipped without error."""
  436. from dulwich.objects import ObjectID
  437. fake_id = ObjectID(b"0" * 40)
  438. object_ids = [fake_id, self.small_blob.id]
  439. filter_spec = BlobNoneFilter()
  440. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  441. # Should skip the missing object
  442. self.assertNotIn(fake_id, filtered)
  443. def test_filter_combine(self):
  444. """Test combined filters."""
  445. object_ids = [
  446. self.small_blob.id,
  447. self.large_blob.id,
  448. self.tree.id,
  449. ]
  450. # Combine blob:limit with another filter
  451. filter_spec = CombineFilter(
  452. [
  453. BlobLimitFilter(100),
  454. BlobNoneFilter(), # This will exclude ALL blobs
  455. ]
  456. )
  457. filtered = filter_pack_objects(self.store, object_ids, filter_spec)
  458. # Should exclude all blobs due to BlobNoneFilter
  459. self.assertNotIn(self.small_blob.id, filtered)
  460. self.assertNotIn(self.large_blob.id, filtered)
  461. self.assertIn(self.tree.id, filtered)
  462. class PartialCloneIntegrationTests(TestCase):
  463. """Integration tests for partial clone with real repositories."""
  464. def setUp(self):
  465. super().setUp()
  466. self.repo_dir = tempfile.mkdtemp()
  467. self.addCleanup(self._cleanup)
  468. self.repo = Repo.init(self.repo_dir)
  469. def _cleanup(self):
  470. """Clean up test repository."""
  471. import shutil
  472. if os.path.exists(self.repo_dir):
  473. shutil.rmtree(self.repo_dir)
  474. def test_blob_none_filter_with_real_repo(self):
  475. """Test blob:none filter excludes blobs in real repository."""
  476. # Create a tree with files
  477. tree = Tree()
  478. # Add some blobs to the tree
  479. blob1 = Blob.from_string(b"file1 content")
  480. blob2 = Blob.from_string(b"file2 content")
  481. tree.add(b"file1.txt", 0o100644, blob1.id)
  482. tree.add(b"file2.txt", 0o100644, blob2.id)
  483. # Add objects to repo
  484. self.repo.object_store.add_object(blob1)
  485. self.repo.object_store.add_object(blob2)
  486. self.repo.object_store.add_object(tree)
  487. # Create commit
  488. commit = make_commit(tree=tree.id, message=b"Test commit")
  489. self.repo.object_store.add_object(commit)
  490. # Get all objects
  491. object_ids = [blob1.id, blob2.id, tree.id, commit.id]
  492. # Apply blob:none filter
  493. filter_spec = BlobNoneFilter()
  494. filtered = filter_pack_objects(self.repo.object_store, object_ids, filter_spec)
  495. # Verify blobs are excluded
  496. self.assertNotIn(blob1.id, filtered)
  497. self.assertNotIn(blob2.id, filtered)
  498. # But tree and commit are included
  499. self.assertIn(tree.id, filtered)
  500. self.assertIn(commit.id, filtered)
  501. # Verify we have only 2 objects (tree + commit)
  502. self.assertEqual(2, len(filtered))
  503. def test_blob_limit_filter_with_mixed_sizes(self):
  504. """Test blob:limit filter with mixed blob sizes."""
  505. tree = Tree()
  506. # Create blobs of different sizes
  507. small_blob = Blob.from_string(b"small") # 5 bytes
  508. medium_blob = Blob.from_string(b"x" * 50) # 50 bytes
  509. large_blob = Blob.from_string(b"y" * 500) # 500 bytes
  510. tree.add(b"small.txt", 0o100644, small_blob.id)
  511. tree.add(b"medium.txt", 0o100644, medium_blob.id)
  512. tree.add(b"large.txt", 0o100644, large_blob.id)
  513. # Add to repo
  514. self.repo.object_store.add_object(small_blob)
  515. self.repo.object_store.add_object(medium_blob)
  516. self.repo.object_store.add_object(large_blob)
  517. self.repo.object_store.add_object(tree)
  518. commit = make_commit(tree=tree.id)
  519. self.repo.object_store.add_object(commit)
  520. # Test with 100 byte limit
  521. object_ids = [
  522. small_blob.id,
  523. medium_blob.id,
  524. large_blob.id,
  525. tree.id,
  526. commit.id,
  527. ]
  528. filter_spec = BlobLimitFilter(100)
  529. filtered = filter_pack_objects(self.repo.object_store, object_ids, filter_spec)
  530. # Small and medium should be included
  531. self.assertIn(small_blob.id, filtered)
  532. self.assertIn(medium_blob.id, filtered)
  533. # Large should be excluded
  534. self.assertNotIn(large_blob.id, filtered)
  535. # Tree and commit included
  536. self.assertIn(tree.id, filtered)
  537. self.assertIn(commit.id, filtered)
  538. def test_combined_filter_integration(self):
  539. """Test combined filters in real scenario."""
  540. tree = Tree()
  541. blob1 = Blob.from_string(b"content1")
  542. blob2 = Blob.from_string(b"x" * 1000)
  543. tree.add(b"file1.txt", 0o100644, blob1.id)
  544. tree.add(b"file2.txt", 0o100644, blob2.id)
  545. self.repo.object_store.add_object(blob1)
  546. self.repo.object_store.add_object(blob2)
  547. self.repo.object_store.add_object(tree)
  548. commit = make_commit(tree=tree.id)
  549. self.repo.object_store.add_object(commit)
  550. # Combine: limit to 500 bytes, but also apply blob:none
  551. # This should exclude ALL blobs (blob:none overrides limit)
  552. filter_spec = CombineFilter(
  553. [
  554. BlobLimitFilter(500),
  555. BlobNoneFilter(),
  556. ]
  557. )
  558. object_ids = [blob1.id, blob2.id, tree.id, commit.id]
  559. filtered = filter_pack_objects(self.repo.object_store, object_ids, filter_spec)
  560. # All blobs excluded
  561. self.assertNotIn(blob1.id, filtered)
  562. self.assertNotIn(blob2.id, filtered)
  563. # Only tree and commit
  564. self.assertEqual(2, len(filtered))
  565. class FilterPackObjectsWithPathsTests(TestCase):
  566. """Test filter_pack_objects_with_paths function."""
  567. def setUp(self):
  568. super().setUp()
  569. self.object_store = MemoryObjectStore()
  570. def test_tree_depth_filtering(self):
  571. """Test filtering by tree depth."""
  572. from dulwich.objects import Blob, Tree
  573. from dulwich.partial_clone import (
  574. TreeDepthFilter,
  575. filter_pack_objects_with_paths,
  576. )
  577. from dulwich.tests.utils import make_commit
  578. # Create a nested tree structure:
  579. # root/
  580. # file1.txt (blob1)
  581. # dir1/
  582. # file2.txt (blob2)
  583. # dir2/
  584. # file3.txt (blob3)
  585. blob1 = Blob.from_string(b"file1 content")
  586. blob2 = Blob.from_string(b"file2 content")
  587. blob3 = Blob.from_string(b"file3 content")
  588. # deepest tree (dir2)
  589. tree_dir2 = Tree()
  590. tree_dir2.add(b"file3.txt", 0o100644, blob3.id)
  591. # middle tree (dir1)
  592. tree_dir1 = Tree()
  593. tree_dir1.add(b"file2.txt", 0o100644, blob2.id)
  594. tree_dir1.add(b"dir2", 0o040000, tree_dir2.id)
  595. # root tree
  596. tree_root = Tree()
  597. tree_root.add(b"file1.txt", 0o100644, blob1.id)
  598. tree_root.add(b"dir1", 0o040000, tree_dir1.id)
  599. # Add all objects to store
  600. for obj in [blob1, blob2, blob3, tree_dir2, tree_dir1, tree_root]:
  601. self.object_store.add_object(obj)
  602. commit = make_commit(tree=tree_root.id)
  603. self.object_store.add_object(commit)
  604. # Filter with depth=1 (root + 1 level deep)
  605. filter_spec = TreeDepthFilter(1)
  606. filtered = filter_pack_objects_with_paths(
  607. self.object_store, [commit.id], filter_spec
  608. )
  609. # Should include: commit, tree_root (depth 0), tree_dir1 (depth 1),
  610. # blob1 (in root), blob2 (in dir1)
  611. # Should exclude: tree_dir2 (depth 2), blob3 (in dir2)
  612. self.assertIn(commit.id, filtered)
  613. self.assertIn(tree_root.id, filtered)
  614. self.assertIn(tree_dir1.id, filtered)
  615. self.assertIn(blob1.id, filtered)
  616. self.assertIn(blob2.id, filtered)
  617. self.assertNotIn(tree_dir2.id, filtered)
  618. self.assertNotIn(blob3.id, filtered)
  619. def test_sparse_oid_path_filtering(self):
  620. """Test filtering by sparse checkout patterns."""
  621. from dulwich.objects import Blob, Tree
  622. from dulwich.partial_clone import (
  623. SparseOidFilter,
  624. filter_pack_objects_with_paths,
  625. )
  626. from dulwich.tests.utils import make_commit
  627. # Create sparse patterns blob that includes only *.txt files
  628. patterns = b"*.txt\n"
  629. patterns_blob = Blob.from_string(patterns)
  630. self.object_store.add_object(patterns_blob)
  631. # Create a tree with mixed file types:
  632. # root/
  633. # readme.txt (should be included)
  634. # script.py (should be excluded)
  635. # docs/
  636. # guide.txt (should be included)
  637. # image.png (should be excluded)
  638. blob_readme = Blob.from_string(b"readme content")
  639. blob_script = Blob.from_string(b"script content")
  640. blob_guide = Blob.from_string(b"guide content")
  641. blob_image = Blob.from_string(b"image content")
  642. tree_docs = Tree()
  643. tree_docs.add(b"guide.txt", 0o100644, blob_guide.id)
  644. tree_docs.add(b"image.png", 0o100644, blob_image.id)
  645. tree_root = Tree()
  646. tree_root.add(b"readme.txt", 0o100644, blob_readme.id)
  647. tree_root.add(b"script.py", 0o100644, blob_script.id)
  648. tree_root.add(b"docs", 0o040000, tree_docs.id)
  649. # Add all objects
  650. for obj in [
  651. blob_readme,
  652. blob_script,
  653. blob_guide,
  654. blob_image,
  655. tree_docs,
  656. tree_root,
  657. ]:
  658. self.object_store.add_object(obj)
  659. commit = make_commit(tree=tree_root.id)
  660. self.object_store.add_object(commit)
  661. # Create sparse filter
  662. filter_spec = SparseOidFilter(patterns_blob.id, object_store=self.object_store)
  663. filtered = filter_pack_objects_with_paths(
  664. self.object_store, [commit.id], filter_spec
  665. )
  666. # Should include: commit, trees, and .txt blobs
  667. self.assertIn(commit.id, filtered)
  668. self.assertIn(tree_root.id, filtered)
  669. self.assertIn(tree_docs.id, filtered)
  670. self.assertIn(blob_readme.id, filtered)
  671. self.assertIn(blob_guide.id, filtered)
  672. # Should exclude: non-.txt blobs
  673. self.assertNotIn(blob_script.id, filtered)
  674. self.assertNotIn(blob_image.id, filtered)
  675. def test_blob_size_filtering_with_paths(self):
  676. """Test that blob size filtering still works with path tracking."""
  677. from dulwich.objects import Blob, Tree
  678. from dulwich.partial_clone import (
  679. BlobLimitFilter,
  680. filter_pack_objects_with_paths,
  681. )
  682. from dulwich.tests.utils import make_commit
  683. # Create blobs of different sizes
  684. blob_small = Blob.from_string(b"small") # 5 bytes
  685. blob_large = Blob.from_string(b"x" * 1000) # 1000 bytes
  686. tree = Tree()
  687. tree.add(b"small.txt", 0o100644, blob_small.id)
  688. tree.add(b"large.txt", 0o100644, blob_large.id)
  689. for obj in [blob_small, blob_large, tree]:
  690. self.object_store.add_object(obj)
  691. commit = make_commit(tree=tree.id)
  692. self.object_store.add_object(commit)
  693. # Filter with 100 byte limit
  694. filter_spec = BlobLimitFilter(100)
  695. filtered = filter_pack_objects_with_paths(
  696. self.object_store, [commit.id], filter_spec
  697. )
  698. # Should include small blob but not large
  699. self.assertIn(commit.id, filtered)
  700. self.assertIn(tree.id, filtered)
  701. self.assertIn(blob_small.id, filtered)
  702. self.assertNotIn(blob_large.id, filtered)
  703. def test_combined_sparse_and_size_filter(self):
  704. """Test combining sparse patterns with blob size limits."""
  705. from dulwich.objects import Blob, Tree
  706. from dulwich.partial_clone import (
  707. BlobLimitFilter,
  708. CombineFilter,
  709. SparseOidFilter,
  710. filter_pack_objects_with_paths,
  711. )
  712. from dulwich.tests.utils import make_commit
  713. # Create sparse patterns: only *.txt files
  714. patterns = b"*.txt\n"
  715. patterns_blob = Blob.from_string(patterns)
  716. self.object_store.add_object(patterns_blob)
  717. # Create files:
  718. # - small.txt (5 bytes, .txt) -> should be included
  719. # - large.txt (1000 bytes, .txt) -> excluded by size
  720. # - small.py (5 bytes, .py) -> excluded by pattern
  721. # - large.py (1000 bytes, .py) -> excluded by both
  722. blob_small_txt = Blob.from_string(b"small txt")
  723. blob_large_txt = Blob.from_string(b"x" * 1000)
  724. blob_small_py = Blob.from_string(b"small py")
  725. blob_large_py = Blob.from_string(b"y" * 1000)
  726. tree = Tree()
  727. tree.add(b"small.txt", 0o100644, blob_small_txt.id)
  728. tree.add(b"large.txt", 0o100644, blob_large_txt.id)
  729. tree.add(b"small.py", 0o100644, blob_small_py.id)
  730. tree.add(b"large.py", 0o100644, blob_large_py.id)
  731. for obj in [blob_small_txt, blob_large_txt, blob_small_py, blob_large_py, tree]:
  732. self.object_store.add_object(obj)
  733. commit = make_commit(tree=tree.id)
  734. self.object_store.add_object(commit)
  735. # Combine: sparse filter + 100 byte limit
  736. filter_spec = CombineFilter(
  737. [
  738. SparseOidFilter(patterns_blob.id, object_store=self.object_store),
  739. BlobLimitFilter(100),
  740. ]
  741. )
  742. filtered = filter_pack_objects_with_paths(
  743. self.object_store, [commit.id], filter_spec
  744. )
  745. # Only small.txt should be included (matches pattern AND size limit)
  746. self.assertIn(commit.id, filtered)
  747. self.assertIn(tree.id, filtered)
  748. self.assertIn(blob_small_txt.id, filtered)
  749. self.assertNotIn(blob_large_txt.id, filtered) # Too large
  750. self.assertNotIn(blob_small_py.id, filtered) # Wrong pattern
  751. self.assertNotIn(blob_large_py.id, filtered) # Both wrong
  752. def test_blob_none_filter_with_paths(self):
  753. """Test that blob:none excludes all blobs with path tracking."""
  754. from dulwich.objects import Blob, Tree
  755. from dulwich.partial_clone import BlobNoneFilter, filter_pack_objects_with_paths
  756. from dulwich.tests.utils import make_commit
  757. blob1 = Blob.from_string(b"content1")
  758. blob2 = Blob.from_string(b"content2")
  759. tree = Tree()
  760. tree.add(b"file1.txt", 0o100644, blob1.id)
  761. tree.add(b"file2.txt", 0o100644, blob2.id)
  762. for obj in [blob1, blob2, tree]:
  763. self.object_store.add_object(obj)
  764. commit = make_commit(tree=tree.id)
  765. self.object_store.add_object(commit)
  766. filter_spec = BlobNoneFilter()
  767. filtered = filter_pack_objects_with_paths(
  768. self.object_store, [commit.id], filter_spec
  769. )
  770. # Should include commit and tree but no blobs
  771. self.assertIn(commit.id, filtered)
  772. self.assertIn(tree.id, filtered)
  773. self.assertNotIn(blob1.id, filtered)
  774. self.assertNotIn(blob2.id, filtered)
  775. def test_direct_tree_want(self):
  776. """Test filtering when a tree (not commit) is wanted."""
  777. from dulwich.objects import Blob, Tree
  778. from dulwich.partial_clone import (
  779. BlobLimitFilter,
  780. filter_pack_objects_with_paths,
  781. )
  782. blob_small = Blob.from_string(b"small")
  783. blob_large = Blob.from_string(b"x" * 1000)
  784. tree = Tree()
  785. tree.add(b"small.txt", 0o100644, blob_small.id)
  786. tree.add(b"large.txt", 0o100644, blob_large.id)
  787. for obj in [blob_small, blob_large, tree]:
  788. self.object_store.add_object(obj)
  789. # Want the tree directly (not via commit)
  790. filter_spec = BlobLimitFilter(100)
  791. filtered = filter_pack_objects_with_paths(
  792. self.object_store, [tree.id], filter_spec
  793. )
  794. # Should include tree and small blob
  795. self.assertIn(tree.id, filtered)
  796. self.assertIn(blob_small.id, filtered)
  797. self.assertNotIn(blob_large.id, filtered)