123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- from dulwich.object_store import MemoryObjectStore, MissingObjectFinder
- from dulwich.objects import Blob
- from dulwich.tests.utils import build_commit_graph, make_object, make_tag
- from . import TestCase
- class MissingObjectFinderTest(TestCase):
- def setUp(self) -> None:
- super().setUp()
- self.store = MemoryObjectStore()
- self.commits = []
- def cmt(self, n):
- return self.commits[n - 1]
- def assertMissingMatch(self, haves, wants, expected) -> None:
- for sha, path in MissingObjectFinder(self.store, haves, wants, shallow=set()):
- self.assertIn(
- sha, expected, f"({sha},{path}) erroneously reported as missing"
- )
- expected.remove(sha)
- self.assertEqual(
- len(expected),
- 0,
- f"some objects are not reported as missing: {expected}",
- )
- class MOFLinearRepoTest(MissingObjectFinderTest):
- def setUp(self) -> None:
- super().setUp()
-
- f1_1 = make_object(Blob, data=b"f1")
-
- f2_1 = make_object(Blob, data=b"f2")
- f2_2 = make_object(Blob, data=b"f2-changed")
- f2_3 = make_object(Blob, data=b"f2-changed-again")
-
- f3_2 = make_object(Blob, data=b"f3")
- commit_spec = [[1], [2, 1], [3, 2]]
- trees = {
- 1: [(b"f1", f1_1), (b"f2", f2_1)],
- 2: [(b"f1", f1_1), (b"f2", f2_2), (b"f3", f3_2)],
- 3: [(b"f2", f2_3), (b"f3", f3_2)],
- }
-
-
-
-
-
- self.commits = build_commit_graph(self.store, commit_spec, trees)
- self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
- self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
- self.missing_1_3 = [
- self.cmt(2).id,
- self.cmt(3).id,
- self.cmt(2).tree,
- self.cmt(3).tree,
- f2_2.id,
- f3_2.id,
- f2_3.id,
- ]
- def test_1_to_2(self) -> None:
- self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id], self.missing_1_2)
- def test_2_to_3(self) -> None:
- self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id], self.missing_2_3)
- def test_1_to_3(self) -> None:
- self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id], self.missing_1_3)
- def test_bogus_haves(self) -> None:
- """Ensure non-existent SHA in haves are tolerated."""
- bogus_sha = self.cmt(2).id[::-1]
- haves = [self.cmt(1).id, bogus_sha]
- wants = [self.cmt(3).id]
- self.assertMissingMatch(haves, wants, self.missing_1_3)
- def test_bogus_wants_failure(self) -> None:
- """Ensure non-existent SHA in wants are not tolerated."""
- bogus_sha = self.cmt(2).id[::-1]
- haves = [self.cmt(1).id]
- wants = [self.cmt(3).id, bogus_sha]
- self.assertRaises(
- KeyError, MissingObjectFinder, self.store, haves, wants, shallow=set()
- )
- def test_no_changes(self) -> None:
- self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
- class MOFMergeForkRepoTest(MissingObjectFinderTest):
-
-
-
-
-
- def setUp(self) -> None:
- super().setUp()
- f1_1 = make_object(Blob, data=b"f1")
- f1_2 = make_object(Blob, data=b"f1-2")
- f1_4 = make_object(Blob, data=b"f1-4")
- f1_7 = make_object(Blob, data=b"f1-2")
- f2_1 = make_object(Blob, data=b"f2")
- f2_3 = make_object(Blob, data=b"f2-3")
- f3_3 = make_object(Blob, data=b"f3")
- f3_5 = make_object(Blob, data=b"f3-5")
- commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
- trees = {
- 1: [(b"f1", f1_1), (b"f2", f2_1)],
- 2: [(b"f1", f1_2), (b"f2", f2_1)],
-
- 3: [(b"f1", f1_2), (b"f2", f2_3), (b"f3", f3_3)],
- 4: [(b"f1", f1_4), (b"f2", f2_1)],
- 5: [(b"f1", f1_2), (b"f3", f3_5)],
-
- 6: [(b"f1", f1_4), (b"f2", f2_3), (b"f3", f3_3)],
-
- 7: [(b"f1", f1_7), (b"f2", f2_3)],
- }
- self.commits = build_commit_graph(self.store, commit_spec, trees)
- self.f1_2_id = f1_2.id
- self.f1_4_id = f1_4.id
- self.f1_7_id = f1_7.id
- self.f2_3_id = f2_3.id
- self.f3_3_id = f3_3.id
- self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
- def test_have6_want7(self) -> None:
-
-
-
-
-
-
- self.assertMissingMatch(
- [self.cmt(6).id],
- [self.cmt(7).id],
- [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id],
- )
- def test_have4_want7(self) -> None:
-
-
-
- self.assertMissingMatch(
- [self.cmt(4).id],
- [self.cmt(7).id],
- [
- self.cmt(7).id,
- self.cmt(6).id,
- self.cmt(3).id,
- self.cmt(7).tree,
- self.cmt(6).tree,
- self.cmt(3).tree,
- self.f2_3_id,
- self.f3_3_id,
- ],
- )
- def test_have1_want6(self) -> None:
-
- self.assertMissingMatch(
- [self.cmt(1).id],
- [self.cmt(6).id],
- [
- self.cmt(6).id,
- self.cmt(4).id,
- self.cmt(3).id,
- self.cmt(2).id,
- self.cmt(6).tree,
- self.cmt(4).tree,
- self.cmt(3).tree,
- self.cmt(2).tree,
- self.f1_2_id,
- self.f1_4_id,
- self.f2_3_id,
- self.f3_3_id,
- ],
- )
- def test_have3_want6(self) -> None:
-
-
-
-
- self.assertMissingMatch(
- [self.cmt(3).id],
- [self.cmt(7).id],
- [
- self.cmt(7).id,
- self.cmt(6).id,
- self.cmt(4).id,
- self.cmt(7).tree,
- self.cmt(6).tree,
- self.cmt(4).tree,
- self.f1_4_id,
- ],
- )
- def test_have5_want7(self) -> None:
-
-
-
-
- self.assertMissingMatch(
- [self.cmt(5).id],
- [self.cmt(7).id],
- [
- self.cmt(7).id,
- self.cmt(6).id,
- self.cmt(4).id,
- self.cmt(7).tree,
- self.cmt(6).tree,
- self.cmt(4).tree,
- self.f1_4_id,
- ],
- )
- class MOFTagsTest(MissingObjectFinderTest):
- def setUp(self) -> None:
- super().setUp()
- f1_1 = make_object(Blob, data=b"f1")
- commit_spec = [[1]]
- trees = {1: [(b"f1", f1_1)]}
- self.commits = build_commit_graph(self.store, commit_spec, trees)
- self._normal_tag = make_tag(self.cmt(1))
- self.store.add_object(self._normal_tag)
- self._tag_of_tag = make_tag(self._normal_tag)
- self.store.add_object(self._tag_of_tag)
- self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
- self.store.add_object(self._tag_of_tree)
- self._tag_of_blob = make_tag(f1_1)
- self.store.add_object(self._tag_of_blob)
- self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
- self.store.add_object(self._tag_of_tag_of_blob)
- self.f1_1_id = f1_1.id
- def test_tagged_commit(self) -> None:
-
-
- self.assertMissingMatch(
- [self.cmt(1).id], [self._normal_tag.id], [self._normal_tag.id]
- )
-
- def test_tagged_tag(self) -> None:
-
- self.assertMissingMatch(
- [self._normal_tag.id], [self._tag_of_tag.id], [self._tag_of_tag.id]
- )
-
- self.assertMissingMatch(
- [self.cmt(1).id],
- [self._tag_of_tag.id],
- [self._normal_tag.id, self._tag_of_tag.id],
- )
- def test_tagged_tree(self) -> None:
- self.assertMissingMatch(
- [],
- [self._tag_of_tree.id],
- [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id],
- )
- def test_tagged_blob(self) -> None:
- self.assertMissingMatch(
- [], [self._tag_of_blob.id], [self._tag_of_blob.id, self.f1_1_id]
- )
- def test_tagged_tagged_blob(self) -> None:
- self.assertMissingMatch(
- [],
- [self._tag_of_tag_of_blob.id],
- [self._tag_of_tag_of_blob.id, self._tag_of_blob.id, self.f1_1_id],
- )
|