test_object_store.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. # test_object_store.py -- tests for object_store.py
  2. # Copyright (C) 2008 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 the object store interface."""
  22. from collections.abc import Callable, Iterator, Sequence
  23. from typing import TYPE_CHECKING, Any
  24. from unittest import TestCase
  25. from unittest.mock import patch
  26. from dulwich.index import commit_tree
  27. from dulwich.object_store import (
  28. MemoryObjectStore,
  29. PackBasedObjectStore,
  30. find_shallow,
  31. iter_commit_contents,
  32. iter_tree_contents,
  33. peel_sha,
  34. )
  35. from dulwich.objects import (
  36. Blob,
  37. Commit,
  38. ObjectID,
  39. ShaFile,
  40. Tag,
  41. Tree,
  42. TreeEntry,
  43. )
  44. from dulwich.protocol import DEPTH_INFINITE
  45. from dulwich.refs import Ref
  46. from .utils import make_commit, make_object, make_tag
  47. if TYPE_CHECKING:
  48. from dulwich.object_store import BaseObjectStore
  49. testobject = make_object(Blob, data=b"yummy data")
  50. class ObjectStoreTests:
  51. """Base class for testing object store implementations."""
  52. store: "BaseObjectStore"
  53. assertEqual: Callable[[object, object], None]
  54. # For type checker purposes - actual implementation supports both styles
  55. assertRaises: Callable[..., Any]
  56. assertNotIn: Callable[[object, object], None]
  57. assertNotEqual: Callable[[object, object], None]
  58. assertIn: Callable[[object, object], None]
  59. assertTrue: Callable[[bool], None]
  60. assertFalse: Callable[[bool], None]
  61. def test_determine_wants_all(self) -> None:
  62. """Test determine_wants_all with valid ref."""
  63. self.assertEqual(
  64. [ObjectID(b"1" * 40)],
  65. self.store.determine_wants_all(
  66. {Ref(b"refs/heads/foo"): ObjectID(b"1" * 40)}
  67. ),
  68. )
  69. def test_determine_wants_all_zero(self) -> None:
  70. """Test determine_wants_all with zero ref."""
  71. self.assertEqual(
  72. [],
  73. self.store.determine_wants_all(
  74. {Ref(b"refs/heads/foo"): ObjectID(b"0" * 40)}
  75. ),
  76. )
  77. def test_determine_wants_all_depth(self) -> None:
  78. """Test determine_wants_all with depth parameter."""
  79. self.store.add_object(testobject)
  80. refs = {Ref(b"refs/heads/foo"): testobject.id}
  81. with patch.object(self.store, "_get_depth", return_value=1) as m:
  82. self.assertEqual([], self.store.determine_wants_all(refs, depth=0))
  83. self.assertEqual(
  84. [testobject.id],
  85. self.store.determine_wants_all(refs, depth=DEPTH_INFINITE),
  86. )
  87. m.assert_not_called()
  88. self.assertEqual([], self.store.determine_wants_all(refs, depth=1))
  89. m.assert_called_with(testobject.id)
  90. self.assertEqual(
  91. [testobject.id], self.store.determine_wants_all(refs, depth=2)
  92. )
  93. def test_get_depth(self) -> None:
  94. """Test getting object depth."""
  95. self.assertEqual(0, self.store._get_depth(testobject.id))
  96. self.store.add_object(testobject)
  97. self.assertEqual(
  98. 1, self.store._get_depth(testobject.id, get_parents=lambda x: [])
  99. )
  100. parent = make_object(Blob, data=b"parent data")
  101. self.store.add_object(parent)
  102. self.assertEqual(
  103. 2,
  104. self.store._get_depth(
  105. testobject.id,
  106. get_parents=lambda x: [parent.id] if x == testobject else [],
  107. ),
  108. )
  109. def test_iter(self) -> None:
  110. """Test iterating over empty store."""
  111. self.assertEqual([], list(self.store))
  112. def test_get_nonexistant(self) -> None:
  113. """Test getting non-existent object raises KeyError."""
  114. self.assertRaises(KeyError, lambda: self.store[ObjectID(b"a" * 40)])
  115. def test_contains_nonexistant(self) -> None:
  116. """Test checking for non-existent object."""
  117. self.assertNotIn(b"a" * 40, self.store)
  118. def test_add_objects_empty(self) -> None:
  119. """Test adding empty list of objects."""
  120. self.store.add_objects([])
  121. def test_add_commit(self) -> None:
  122. """Test adding commit objects."""
  123. # TODO: Argh, no way to construct Git commit objects without
  124. # access to a serialized form.
  125. self.store.add_objects([])
  126. def test_store_resilience(self) -> None:
  127. """Test if updating an existing stored object doesn't erase the object from the store."""
  128. test_object = make_object(Blob, data=b"data")
  129. self.store.add_object(test_object)
  130. test_object_id = test_object.id
  131. test_object.data = test_object.data + b"update"
  132. stored_test_object = self.store[test_object_id]
  133. self.assertNotEqual(test_object.id, stored_test_object.id)
  134. self.assertEqual(stored_test_object.id, test_object_id)
  135. def test_add_object(self) -> None:
  136. """Test adding a single object to store."""
  137. self.store.add_object(testobject)
  138. self.assertEqual({testobject.id}, set(self.store))
  139. self.assertIn(testobject.id, self.store)
  140. r = self.store[testobject.id]
  141. self.assertEqual(r, testobject)
  142. def test_add_objects(self) -> None:
  143. """Test adding multiple objects to store."""
  144. data = [(testobject, "mypath")]
  145. self.store.add_objects(data)
  146. self.assertEqual({testobject.id}, set(self.store))
  147. self.assertIn(testobject.id, self.store)
  148. r = self.store[testobject.id]
  149. self.assertEqual(r, testobject)
  150. def test_tree_changes(self) -> None:
  151. """Test detecting changes between trees."""
  152. blob_a1 = make_object(Blob, data=b"a1")
  153. blob_a2 = make_object(Blob, data=b"a2")
  154. blob_b = make_object(Blob, data=b"b")
  155. for blob in [blob_a1, blob_a2, blob_b]:
  156. self.store.add_object(blob)
  157. blobs_1 = [(b"a", blob_a1.id, 0o100644), (b"b", blob_b.id, 0o100644)]
  158. tree1_id = commit_tree(self.store, blobs_1)
  159. blobs_2 = [(b"a", blob_a2.id, 0o100644), (b"b", blob_b.id, 0o100644)]
  160. tree2_id = commit_tree(self.store, blobs_2)
  161. change_a = (
  162. (b"a", b"a"),
  163. (0o100644, 0o100644),
  164. (blob_a1.id, blob_a2.id),
  165. )
  166. self.assertEqual([change_a], list(self.store.tree_changes(tree1_id, tree2_id)))
  167. self.assertEqual(
  168. [
  169. change_a,
  170. ((b"b", b"b"), (0o100644, 0o100644), (blob_b.id, blob_b.id)),
  171. ],
  172. list(self.store.tree_changes(tree1_id, tree2_id, want_unchanged=True)),
  173. )
  174. def test_iter_tree_contents(self) -> None:
  175. """Test iterating over tree contents."""
  176. blob_a = make_object(Blob, data=b"a")
  177. blob_b = make_object(Blob, data=b"b")
  178. blob_c = make_object(Blob, data=b"c")
  179. for blob in [blob_a, blob_b, blob_c]:
  180. self.store.add_object(blob)
  181. blobs = [
  182. (b"a", blob_a.id, 0o100644),
  183. (b"ad/b", blob_b.id, 0o100644),
  184. (b"ad/bd/c", blob_c.id, 0o100755),
  185. (b"ad/c", blob_c.id, 0o100644),
  186. (b"c", blob_c.id, 0o100644),
  187. ]
  188. tree_id = commit_tree(self.store, blobs)
  189. self.assertEqual(
  190. [TreeEntry(p, m, h) for (p, h, m) in blobs],
  191. list(iter_tree_contents(self.store, tree_id)),
  192. )
  193. self.assertEqual([], list(iter_tree_contents(self.store, None)))
  194. def test_iter_tree_contents_include_trees(self) -> None:
  195. """Test iterating tree contents including tree objects."""
  196. blob_a = make_object(Blob, data=b"a")
  197. blob_b = make_object(Blob, data=b"b")
  198. blob_c = make_object(Blob, data=b"c")
  199. for blob in [blob_a, blob_b, blob_c]:
  200. self.store.add_object(blob)
  201. blobs = [
  202. (b"a", blob_a.id, 0o100644),
  203. (b"ad/b", blob_b.id, 0o100644),
  204. (b"ad/bd/c", blob_c.id, 0o100755),
  205. ]
  206. tree_id = commit_tree(self.store, blobs)
  207. tree = self.store[tree_id]
  208. assert isinstance(tree, Tree)
  209. tree_ad = self.store[tree[b"ad"][1]]
  210. assert isinstance(tree_ad, Tree)
  211. tree_bd = self.store[tree_ad[b"bd"][1]]
  212. expected = [
  213. TreeEntry(b"", 0o040000, tree_id),
  214. TreeEntry(b"a", 0o100644, blob_a.id),
  215. TreeEntry(b"ad", 0o040000, tree_ad.id),
  216. TreeEntry(b"ad/b", 0o100644, blob_b.id),
  217. TreeEntry(b"ad/bd", 0o040000, tree_bd.id),
  218. TreeEntry(b"ad/bd/c", 0o100755, blob_c.id),
  219. ]
  220. actual = iter_tree_contents(self.store, tree_id, include_trees=True)
  221. self.assertEqual(expected, list(actual))
  222. def make_tag(self, name: bytes, obj: ShaFile) -> Tag:
  223. """Helper to create and add a tag object."""
  224. tag = make_tag(obj, name=name)
  225. self.store.add_object(tag)
  226. return tag
  227. def test_peel_sha(self) -> None:
  228. """Test peeling SHA to get underlying object."""
  229. self.store.add_object(testobject)
  230. tag1 = self.make_tag(b"1", testobject)
  231. tag2 = self.make_tag(b"2", testobject)
  232. tag3 = self.make_tag(b"3", testobject)
  233. for obj in [testobject, tag1, tag2, tag3]:
  234. self.assertEqual((obj, testobject), peel_sha(self.store, obj.id))
  235. def test_get_raw(self) -> None:
  236. """Test getting raw object data."""
  237. self.store.add_object(testobject)
  238. self.assertEqual(
  239. (Blob.type_num, b"yummy data"), self.store.get_raw(testobject.id)
  240. )
  241. def test_close(self) -> None:
  242. """Test closing the object store."""
  243. # For now, just check that close doesn't barf.
  244. self.store.add_object(testobject)
  245. self.store.close()
  246. def test_iter_prefix(self) -> None:
  247. """Test iterating objects by prefix."""
  248. self.store.add_object(testobject)
  249. self.assertEqual([testobject.id], list(self.store.iter_prefix(testobject.id)))
  250. self.assertEqual(
  251. [testobject.id], list(self.store.iter_prefix(testobject.id[:10]))
  252. )
  253. def test_iterobjects_subset_all_present(self) -> None:
  254. """Test iterating over a subset of objects that all exist."""
  255. blob1 = make_object(Blob, data=b"blob 1 data")
  256. blob2 = make_object(Blob, data=b"blob 2 data")
  257. self.store.add_object(blob1)
  258. self.store.add_object(blob2)
  259. objects = list(self.store.iterobjects_subset([blob1.id, blob2.id]))
  260. self.assertEqual(2, len(objects))
  261. object_ids = set(o.id for o in objects)
  262. self.assertEqual(set([blob1.id, blob2.id]), object_ids)
  263. def test_iterobjects_subset_missing_not_allowed(self) -> None:
  264. """Test iterating with missing objects when not allowed."""
  265. blob1 = make_object(Blob, data=b"blob 1 data")
  266. self.store.add_object(blob1)
  267. missing_sha = ObjectID(b"1" * 40)
  268. self.assertRaises(
  269. KeyError,
  270. lambda: list(self.store.iterobjects_subset([blob1.id, missing_sha])),
  271. )
  272. def test_iterobjects_subset_missing_allowed(self) -> None:
  273. """Test iterating with missing objects when allowed."""
  274. blob1 = make_object(Blob, data=b"blob 1 data")
  275. self.store.add_object(blob1)
  276. missing_sha = ObjectID(b"1" * 40)
  277. objects = list(
  278. self.store.iterobjects_subset([blob1.id, missing_sha], allow_missing=True)
  279. )
  280. self.assertEqual(1, len(objects))
  281. self.assertEqual(blob1.id, objects[0].id)
  282. def test_iter_prefix_not_found(self) -> None:
  283. """Test iterating with prefix that doesn't match any objects."""
  284. self.assertEqual([], list(self.store.iter_prefix(b"1" * 40)))
  285. class PackBasedObjectStoreTests(ObjectStoreTests):
  286. """Tests for pack-based object stores."""
  287. store: PackBasedObjectStore
  288. def tearDown(self) -> None:
  289. """Clean up by closing all packs."""
  290. for pack in self.store.packs:
  291. pack.close()
  292. def test_empty_packs(self) -> None:
  293. """Test that new store has no packs."""
  294. self.assertEqual([], list(self.store.packs))
  295. def test_pack_loose_objects(self) -> None:
  296. """Test packing loose objects into packs."""
  297. b1 = make_object(Blob, data=b"yummy data")
  298. self.store.add_object(b1)
  299. b2 = make_object(Blob, data=b"more yummy data")
  300. self.store.add_object(b2)
  301. b3 = make_object(Blob, data=b"even more yummy data")
  302. b4 = make_object(Blob, data=b"and more yummy data")
  303. self.store.add_objects([(b3, None), (b4, None)])
  304. self.assertEqual({b1.id, b2.id, b3.id, b4.id}, set(self.store))
  305. self.assertEqual(1, len(self.store.packs))
  306. self.assertEqual(2, self.store.pack_loose_objects())
  307. self.assertNotEqual([], list(self.store.packs))
  308. self.assertEqual(0, self.store.pack_loose_objects())
  309. def test_repack(self) -> None:
  310. """Test repacking multiple packs into one."""
  311. b1 = make_object(Blob, data=b"yummy data")
  312. self.store.add_object(b1)
  313. b2 = make_object(Blob, data=b"more yummy data")
  314. self.store.add_object(b2)
  315. b3 = make_object(Blob, data=b"even more yummy data")
  316. b4 = make_object(Blob, data=b"and more yummy data")
  317. self.store.add_objects([(b3, None), (b4, None)])
  318. b5 = make_object(Blob, data=b"and more data")
  319. b6 = make_object(Blob, data=b"and some more data")
  320. self.store.add_objects([(b5, None), (b6, None)])
  321. self.assertEqual({b1.id, b2.id, b3.id, b4.id, b5.id, b6.id}, set(self.store))
  322. self.assertEqual(2, len(self.store.packs))
  323. self.assertEqual(6, self.store.repack())
  324. self.assertEqual(1, len(self.store.packs))
  325. self.assertEqual(0, self.store.pack_loose_objects())
  326. def test_repack_existing(self) -> None:
  327. """Test repacking with existing objects."""
  328. b1 = make_object(Blob, data=b"yummy data")
  329. self.store.add_object(b1)
  330. b2 = make_object(Blob, data=b"more yummy data")
  331. self.store.add_object(b2)
  332. self.store.add_objects([(b1, None), (b2, None)])
  333. self.store.add_objects([(b2, None)])
  334. self.assertEqual({b1.id, b2.id}, set(self.store))
  335. self.assertEqual(2, len(self.store.packs))
  336. self.assertEqual(2, self.store.repack())
  337. self.assertEqual(1, len(self.store.packs))
  338. self.assertEqual(0, self.store.pack_loose_objects())
  339. self.assertEqual({b1.id, b2.id}, set(self.store))
  340. self.assertEqual(1, len(self.store.packs))
  341. self.assertEqual(2, self.store.repack())
  342. self.assertEqual(1, len(self.store.packs))
  343. self.assertEqual(0, self.store.pack_loose_objects())
  344. def test_repack_with_exclude(self) -> None:
  345. """Test repacking while excluding specific objects."""
  346. b1 = make_object(Blob, data=b"yummy data")
  347. self.store.add_object(b1)
  348. b2 = make_object(Blob, data=b"more yummy data")
  349. self.store.add_object(b2)
  350. b3 = make_object(Blob, data=b"even more yummy data")
  351. b4 = make_object(Blob, data=b"and more yummy data")
  352. self.store.add_objects([(b3, None), (b4, None)])
  353. self.assertEqual({b1.id, b2.id, b3.id, b4.id}, set(self.store))
  354. self.assertEqual(1, len(self.store.packs))
  355. # Repack, excluding b2 and b3
  356. excluded = {b2.id, b3.id}
  357. self.assertEqual(2, self.store.repack(exclude=excluded))
  358. # Should have repacked only b1 and b4
  359. self.assertEqual(1, len(self.store.packs))
  360. self.assertIn(b1.id, self.store)
  361. self.assertNotIn(b2.id, self.store)
  362. self.assertNotIn(b3.id, self.store)
  363. self.assertIn(b4.id, self.store)
  364. def test_delete_loose_object(self) -> None:
  365. """Test deleting loose objects."""
  366. b1 = make_object(Blob, data=b"test data")
  367. self.store.add_object(b1)
  368. # Verify it's loose
  369. self.assertTrue(self.store.contains_loose(b1.id))
  370. self.assertIn(b1.id, self.store)
  371. # Delete it
  372. self.store.delete_loose_object(b1.id)
  373. # Verify it's gone
  374. self.assertFalse(self.store.contains_loose(b1.id))
  375. self.assertNotIn(b1.id, self.store)
  376. class CommitTestHelper:
  377. """Helper for tests which iterate over commits."""
  378. def setUp(self) -> None:
  379. """Set up test fixture."""
  380. super().setUp() # type: ignore[misc]
  381. self._store = MemoryObjectStore()
  382. def make_commit(self, **attrs: Any) -> Commit: # noqa: ANN401
  383. """Helper to create and store a commit."""
  384. commit = make_commit(**attrs)
  385. self._store.add_object(commit)
  386. return commit
  387. class IterCommitContentsTests(CommitTestHelper, TestCase):
  388. """Tests for iter_commit_contents."""
  389. def make_commits_with_contents(self) -> Commit:
  390. """Helper to prepare test commits."""
  391. files = [
  392. # (path, contents)
  393. (b"foo", b"foo"),
  394. (b"bar", b"bar"),
  395. (b"dir/baz", b"baz"),
  396. (b"dir/subdir/foo", b"subfoo"),
  397. (b"dir/subdir/bar", b"subbar"),
  398. (b"dir/subdir/baz", b"subbaz"),
  399. ]
  400. blobs = {contents: make_object(Blob, data=contents) for path, contents in files}
  401. for blob in blobs.values():
  402. self._store.add_object(blob)
  403. commit = self.make_commit(
  404. tree=commit_tree(
  405. self._store,
  406. [(path, blobs[contents].id, 0o100644) for path, contents in files],
  407. )
  408. )
  409. return commit
  410. def assertCommitEntries(
  411. self, results: Iterator[TreeEntry], expected: list[tuple[bytes, bytes]]
  412. ) -> None:
  413. """Assert that iter_commit_contents results are equal to expected."""
  414. actual = []
  415. for entry in results:
  416. assert entry.sha is not None
  417. obj = self._store[entry.sha]
  418. assert isinstance(obj, Blob)
  419. actual.append((entry.path, obj.data))
  420. self.assertEqual(actual, expected)
  421. def test_iter_commit_contents_no_includes(self) -> None:
  422. """Test iterating commit contents without includes."""
  423. commit = self.make_commits_with_contents()
  424. # this is the same list as used by make_commits_with_contents,
  425. # but ordered to match the actual iter_tree_contents iteration
  426. # order
  427. all_files = [
  428. (b"bar", b"bar"),
  429. (b"dir/baz", b"baz"),
  430. (b"dir/subdir/bar", b"subbar"),
  431. (b"dir/subdir/baz", b"subbaz"),
  432. (b"dir/subdir/foo", b"subfoo"),
  433. (b"foo", b"foo"),
  434. ]
  435. # No includes
  436. self.assertCommitEntries(iter_commit_contents(self._store, commit), all_files)
  437. # Explicit include=None
  438. self.assertCommitEntries(
  439. iter_commit_contents(self._store, commit, include=None), all_files
  440. )
  441. # include=[] is not the same as None
  442. self.assertCommitEntries(
  443. iter_commit_contents(self._store, commit, include=[]), []
  444. )
  445. def test_iter_commit_contents_with_includes(self) -> None:
  446. """Test iterating commit contents with includes."""
  447. commit = self.make_commits_with_contents()
  448. include1 = ["foo", "bar"]
  449. expected1 = [
  450. # Note: iter_tree_contents iterates in name order, but we
  451. # listed two separate paths, so they'll keep their order
  452. # as specified
  453. (b"foo", b"foo"),
  454. (b"bar", b"bar"),
  455. ]
  456. include2 = ["foo", "dir/subdir"]
  457. expected2 = [
  458. # foo
  459. (b"foo", b"foo"),
  460. # dir/subdir
  461. (b"dir/subdir/bar", b"subbar"),
  462. (b"dir/subdir/baz", b"subbaz"),
  463. (b"dir/subdir/foo", b"subfoo"),
  464. ]
  465. include3 = ["dir"]
  466. expected3 = [
  467. (b"dir/baz", b"baz"),
  468. (b"dir/subdir/bar", b"subbar"),
  469. (b"dir/subdir/baz", b"subbaz"),
  470. (b"dir/subdir/foo", b"subfoo"),
  471. ]
  472. for include, expected in [
  473. (include1, expected1),
  474. (include2, expected2),
  475. (include3, expected3),
  476. ]:
  477. self.assertCommitEntries(
  478. iter_commit_contents(self._store, commit, include=include), expected
  479. )
  480. def test_iter_commit_contents_overlapping_includes(self) -> None:
  481. """Test iterating commit contents with overlaps in includes."""
  482. commit = self.make_commits_with_contents()
  483. include1 = ["dir", "dir/baz"]
  484. expected1 = [
  485. # dir
  486. (b"dir/baz", b"baz"),
  487. (b"dir/subdir/bar", b"subbar"),
  488. (b"dir/subdir/baz", b"subbaz"),
  489. (b"dir/subdir/foo", b"subfoo"),
  490. # dir/baz
  491. (b"dir/baz", b"baz"),
  492. ]
  493. include2 = ["dir", "dir/subdir", "dir/subdir/baz"]
  494. expected2 = [
  495. # dir
  496. (b"dir/baz", b"baz"),
  497. (b"dir/subdir/bar", b"subbar"),
  498. (b"dir/subdir/baz", b"subbaz"),
  499. (b"dir/subdir/foo", b"subfoo"),
  500. # dir/subdir
  501. (b"dir/subdir/bar", b"subbar"),
  502. (b"dir/subdir/baz", b"subbaz"),
  503. (b"dir/subdir/foo", b"subfoo"),
  504. # dir/subdir/baz
  505. (b"dir/subdir/baz", b"subbaz"),
  506. ]
  507. for include, expected in [
  508. (include1, expected1),
  509. (include2, expected2),
  510. ]:
  511. self.assertCommitEntries(
  512. iter_commit_contents(self._store, commit, include=include), expected
  513. )
  514. class FindShallowTests(CommitTestHelper, TestCase):
  515. """Tests for finding shallow commits."""
  516. def make_linear_commits(self, n: int, message: bytes = b"") -> list[Commit]:
  517. """Create a linear chain of commits."""
  518. commits = []
  519. parents: list[bytes] = []
  520. for _ in range(n):
  521. commits.append(self.make_commit(parents=parents, message=message))
  522. parents = [commits[-1].id]
  523. return commits
  524. def assertSameElements(
  525. self, expected: Sequence[object], actual: Sequence[object]
  526. ) -> None:
  527. """Assert that two sequences contain the same elements."""
  528. self.assertEqual(set(expected), set(actual))
  529. def test_linear(self) -> None:
  530. """Test finding shallow commits in a linear history."""
  531. c1, c2, c3 = self.make_linear_commits(3)
  532. self.assertEqual((set([c3.id]), set([])), find_shallow(self._store, [c3.id], 1))
  533. self.assertEqual(
  534. (set([c2.id]), set([c3.id])),
  535. find_shallow(self._store, [c3.id], 2),
  536. )
  537. self.assertEqual(
  538. (set([c1.id]), set([c2.id, c3.id])),
  539. find_shallow(self._store, [c3.id], 3),
  540. )
  541. self.assertEqual(
  542. (set([]), set([c1.id, c2.id, c3.id])),
  543. find_shallow(self._store, [c3.id], 4),
  544. )
  545. def test_multiple_independent(self) -> None:
  546. """Test finding shallow commits with multiple independent branches."""
  547. a = self.make_linear_commits(2, message=b"a")
  548. b = self.make_linear_commits(2, message=b"b")
  549. c = self.make_linear_commits(2, message=b"c")
  550. heads = [a[1].id, b[1].id, c[1].id]
  551. self.assertEqual(
  552. (set([a[0].id, b[0].id, c[0].id]), set(heads)),
  553. find_shallow(self._store, heads, 2),
  554. )
  555. def test_multiple_overlapping(self) -> None:
  556. """Test finding shallow commits with overlapping branches."""
  557. # Create the following commit tree:
  558. # 1--2
  559. # \
  560. # 3--4
  561. c1, c2 = self.make_linear_commits(2)
  562. c3 = self.make_commit(parents=[c1.id])
  563. c4 = self.make_commit(parents=[c3.id])
  564. # 1 is shallow along the path from 4, but not along the path from 2.
  565. self.assertEqual(
  566. (set([c1.id]), set([c1.id, c2.id, c3.id, c4.id])),
  567. find_shallow(self._store, [c2.id, c4.id], 3),
  568. )
  569. def test_merge(self) -> None:
  570. """Test finding shallow commits with merge commits."""
  571. c1 = self.make_commit()
  572. c2 = self.make_commit()
  573. c3 = self.make_commit(parents=[c1.id, c2.id])
  574. self.assertEqual(
  575. (set([c1.id, c2.id]), set([c3.id])),
  576. find_shallow(self._store, [c3.id], 2),
  577. )
  578. def test_tag(self) -> None:
  579. """Test finding shallow commits with tags."""
  580. c1, c2 = self.make_linear_commits(2)
  581. tag = make_tag(c2, name=b"tag")
  582. self._store.add_object(tag)
  583. self.assertEqual(
  584. (set([c1.id]), set([c2.id])),
  585. find_shallow(self._store, [tag.id], 2),
  586. )