2
0

test_missing_obj_finder.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # test_missing_obj_finder.py -- tests for MissingObjectFinder
  2. # Copyright (C) 2012 syntevo GmbH
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from dulwich.object_store import (
  19. MemoryObjectStore,
  20. )
  21. from dulwich.objects import (
  22. Blob,
  23. )
  24. from dulwich.tests import TestCase
  25. from dulwich.tests.utils import (
  26. make_object,
  27. make_tag,
  28. build_commit_graph,
  29. )
  30. class MissingObjectFinderTest(TestCase):
  31. def setUp(self):
  32. super(MissingObjectFinderTest, self).setUp()
  33. self.store = MemoryObjectStore()
  34. self.commits = []
  35. def cmt(self, n):
  36. return self.commits[n-1]
  37. def assertMissingMatch(self, haves, wants, expected):
  38. for sha, path in self.store.find_missing_objects(haves, wants):
  39. self.assertTrue(sha in expected,
  40. "(%s,%s) erroneously reported as missing" % (sha, path))
  41. expected.remove(sha)
  42. self.assertEqual(len(expected), 0,
  43. "some objects are not reported as missing: %s" % (expected, ))
  44. class MOFLinearRepoTest(MissingObjectFinderTest):
  45. def setUp(self):
  46. super(MOFLinearRepoTest, self).setUp()
  47. f1_1 = make_object(Blob, data=b'f1') # present in 1, removed in 3
  48. f2_1 = make_object(Blob, data=b'f2') # present in all revisions, changed in 2 and 3
  49. f2_2 = make_object(Blob, data=b'f2-changed')
  50. f2_3 = make_object(Blob, data=b'f2-changed-again')
  51. f3_2 = make_object(Blob, data=b'f3') # added in 2, left unmodified in 3
  52. commit_spec = [[1], [2, 1], [3, 2]]
  53. trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
  54. 2: [(b'f1', f1_1), (b'f2', f2_2), (b'f3', f3_2)],
  55. 3: [(b'f2', f2_3), (b'f3', f3_2)] }
  56. # commit 1: f1 and f2
  57. # commit 2: f3 added, f2 changed. Missing shall report commit id and a
  58. # tree referenced by commit
  59. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
  60. # be reported as modified
  61. self.commits = build_commit_graph(self.store, commit_spec, trees)
  62. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  63. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  64. self.missing_1_3 = [
  65. self.cmt(2).id, self.cmt(3).id,
  66. self.cmt(2).tree, self.cmt(3).tree,
  67. f2_2.id, f3_2.id, f2_3.id]
  68. def test_1_to_2(self):
  69. self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id],
  70. self.missing_1_2)
  71. def test_2_to_3(self):
  72. self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id],
  73. self.missing_2_3)
  74. def test_1_to_3(self):
  75. self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id],
  76. self.missing_1_3)
  77. def test_bogus_haves(self):
  78. """Ensure non-existent SHA in haves are tolerated"""
  79. bogus_sha = self.cmt(2).id[::-1]
  80. haves = [self.cmt(1).id, bogus_sha]
  81. wants = [self.cmt(3).id]
  82. self.assertMissingMatch(haves, wants, self.missing_1_3)
  83. def test_bogus_wants_failure(self):
  84. """Ensure non-existent SHA in wants are not tolerated"""
  85. bogus_sha = self.cmt(2).id[::-1]
  86. haves = [self.cmt(1).id]
  87. wants = [self.cmt(3).id, bogus_sha]
  88. self.assertRaises(KeyError, self.store.find_missing_objects,
  89. haves, wants)
  90. def test_no_changes(self):
  91. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  92. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  93. # 1 --- 2 --- 4 --- 6 --- 7
  94. # \ /
  95. # 3 ---
  96. # \
  97. # 5
  98. def setUp(self):
  99. super(MOFMergeForkRepoTest, self).setUp()
  100. f1_1 = make_object(Blob, data=b'f1')
  101. f1_2 = make_object(Blob, data=b'f1-2')
  102. f1_4 = make_object(Blob, data=b'f1-4')
  103. f1_7 = make_object(Blob, data=b'f1-2') # same data as in rev 2
  104. f2_1 = make_object(Blob, data=b'f2')
  105. f2_3 = make_object(Blob, data=b'f2-3')
  106. f3_3 = make_object(Blob, data=b'f3')
  107. f3_5 = make_object(Blob, data=b'f3-5')
  108. commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
  109. trees = {1: [(b'f1', f1_1), (b'f2', f2_1)],
  110. 2: [(b'f1', f1_2), (b'f2', f2_1)], # f1 changed
  111. # f3 added, f2 changed
  112. 3: [(b'f1', f1_2), (b'f2', f2_3), (b'f3', f3_3)],
  113. 4: [(b'f1', f1_4), (b'f2', f2_1)], # f1 changed
  114. 5: [(b'f1', f1_2), (b'f3', f3_5)], # f2 removed, f3 changed
  115. 6: [(b'f1', f1_4), (b'f2', f2_3), (b'f3', f3_3)], # merged 3 and 4
  116. # f1 changed to match rev2. f3 removed
  117. 7: [(b'f1', f1_7), (b'f2', f2_3)]}
  118. self.commits = build_commit_graph(self.store, commit_spec, trees)
  119. self.f1_2_id = f1_2.id
  120. self.f1_4_id = f1_4.id
  121. self.f1_7_id = f1_7.id
  122. self.f2_3_id = f2_3.id
  123. self.f3_3_id = f3_3.id
  124. self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
  125. def test_have6_want7(self):
  126. # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
  127. # f1_2, however, to do so, MissingObjectFinder shall not record trees
  128. # of common commits only, but also all parent trees and tree items,
  129. # which is an overkill (i.e. in sha_done it records f1_4 as known, and
  130. # doesn't record f1_2 was known prior to that, hence can't detect f1_7
  131. # is in fact f1_2 and shall not be reported)
  132. self.assertMissingMatch([self.cmt(6).id], [self.cmt(7).id],
  133. [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id])
  134. def test_have4_want7(self):
  135. # have 4, want 7. Shall not include rev5 as it is not in the tree
  136. # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
  137. # commit hierarchy)
  138. self.assertMissingMatch([self.cmt(4).id], [self.cmt(7).id], [
  139. self.cmt(7).id, self.cmt(6).id, self.cmt(3).id,
  140. self.cmt(7).tree, self.cmt(6).tree, self.cmt(3).tree,
  141. self.f2_3_id, self.f3_3_id])
  142. def test_have1_want6(self):
  143. # have 1, want 6. Shall not include rev5
  144. self.assertMissingMatch([self.cmt(1).id], [self.cmt(6).id], [
  145. self.cmt(6).id, self.cmt(4).id, self.cmt(3).id, self.cmt(2).id,
  146. self.cmt(6).tree, self.cmt(4).tree, self.cmt(3).tree,
  147. self.cmt(2).tree, self.f1_2_id, self.f1_4_id, self.f2_3_id,
  148. self.f3_3_id])
  149. def test_have3_want6(self):
  150. # have 3, want 7. Shall not report rev2 and its tree, because
  151. # haves(3) means has parents, i.e. rev2, too
  152. # BUT shall report any changes descending rev2 (excluding rev3)
  153. # Shall NOT report f1_7 as it's techically == f1_2
  154. self.assertMissingMatch([self.cmt(3).id], [self.cmt(7).id], [
  155. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  156. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  157. self.f1_4_id])
  158. def test_have5_want7(self):
  159. # have 5, want 7. Common parent is rev2, hence children of rev2 from
  160. # a descent line other than rev5 shall be reported
  161. # expects f1_4 from rev6. f3_5 is known in rev5;
  162. # f1_7 shall be the same as f1_2 (known, too)
  163. self.assertMissingMatch([self.cmt(5).id], [self.cmt(7).id], [
  164. self.cmt(7).id, self.cmt(6).id, self.cmt(4).id,
  165. self.cmt(7).tree, self.cmt(6).tree, self.cmt(4).tree,
  166. self.f1_4_id])
  167. class MOFTagsTest(MissingObjectFinderTest):
  168. def setUp(self):
  169. super(MOFTagsTest, self).setUp()
  170. f1_1 = make_object(Blob, data=b'f1')
  171. commit_spec = [[1]]
  172. trees = {1: [(b'f1', f1_1)]}
  173. self.commits = build_commit_graph(self.store, commit_spec, trees)
  174. self._normal_tag = make_tag(self.cmt(1))
  175. self.store.add_object(self._normal_tag)
  176. self._tag_of_tag = make_tag(self._normal_tag)
  177. self.store.add_object(self._tag_of_tag)
  178. self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
  179. self.store.add_object(self._tag_of_tree)
  180. self._tag_of_blob = make_tag(f1_1)
  181. self.store.add_object(self._tag_of_blob)
  182. self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
  183. self.store.add_object(self._tag_of_tag_of_blob)
  184. self.f1_1_id = f1_1.id
  185. def test_tagged_commit(self):
  186. # The user already has the tagged commit, all they want is the tag,
  187. # so send them only the tag object.
  188. self.assertMissingMatch([self.cmt(1).id], [self._normal_tag.id],
  189. [self._normal_tag.id])
  190. # The remaining cases are unusual, but do happen in the wild.
  191. def test_tagged_tag(self):
  192. # User already has tagged tag, send only tag of tag
  193. self.assertMissingMatch([self._normal_tag.id], [self._tag_of_tag.id],
  194. [self._tag_of_tag.id])
  195. # User needs both tags, but already has commit
  196. self.assertMissingMatch([self.cmt(1).id], [self._tag_of_tag.id],
  197. [self._normal_tag.id, self._tag_of_tag.id])
  198. def test_tagged_tree(self):
  199. self.assertMissingMatch(
  200. [], [self._tag_of_tree.id],
  201. [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id])
  202. def test_tagged_blob(self):
  203. self.assertMissingMatch([], [self._tag_of_blob.id],
  204. [self._tag_of_blob.id, self.f1_1_id])
  205. def test_tagged_tagged_blob(self):
  206. self.assertMissingMatch([], [self._tag_of_tag_of_blob.id],
  207. [self._tag_of_tag_of_blob.id,
  208. self._tag_of_blob.id, self.f1_1_id])