test_missing_obj_finder.py 10 KB

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