test_missing_obj_finder.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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, set()):
  41. self.assertIn(
  42. sha,
  43. expected,
  44. "(%s,%s) erroneously reported as missing" % (sha, path)
  45. )
  46. expected.remove(sha)
  47. self.assertEqual(
  48. len(expected),
  49. 0,
  50. "some objects are not reported as missing: %s" % (expected,),
  51. )
  52. class MOFLinearRepoTest(MissingObjectFinderTest):
  53. def setUp(self):
  54. super(MOFLinearRepoTest, self).setUp()
  55. # present in 1, removed in 3
  56. f1_1 = make_object(Blob, data=b"f1")
  57. # present in all revisions, changed in 2 and 3
  58. f2_1 = make_object(Blob, data=b"f2")
  59. f2_2 = make_object(Blob, data=b"f2-changed")
  60. f2_3 = make_object(Blob, data=b"f2-changed-again")
  61. # added in 2, left unmodified in 3
  62. f3_2 = make_object(Blob, data=b"f3")
  63. commit_spec = [[1], [2, 1], [3, 2]]
  64. trees = {
  65. 1: [(b"f1", f1_1), (b"f2", f2_1)],
  66. 2: [(b"f1", f1_1), (b"f2", f2_2), (b"f3", f3_2)],
  67. 3: [(b"f2", f2_3), (b"f3", f3_2)],
  68. }
  69. # commit 1: f1 and f2
  70. # commit 2: f3 added, f2 changed. Missing shall report commit id and a
  71. # tree referenced by commit
  72. # commit 3: f1 removed, f2 changed. Commit sha and root tree sha shall
  73. # be reported as modified
  74. self.commits = build_commit_graph(self.store, commit_spec, trees)
  75. self.missing_1_2 = [self.cmt(2).id, self.cmt(2).tree, f2_2.id, f3_2.id]
  76. self.missing_2_3 = [self.cmt(3).id, self.cmt(3).tree, f2_3.id]
  77. self.missing_1_3 = [
  78. self.cmt(2).id,
  79. self.cmt(3).id,
  80. self.cmt(2).tree,
  81. self.cmt(3).tree,
  82. f2_2.id,
  83. f3_2.id,
  84. f2_3.id,
  85. ]
  86. def test_1_to_2(self):
  87. self.assertMissingMatch([self.cmt(1).id], [self.cmt(2).id], self.missing_1_2)
  88. def test_2_to_3(self):
  89. self.assertMissingMatch([self.cmt(2).id], [self.cmt(3).id], self.missing_2_3)
  90. def test_1_to_3(self):
  91. self.assertMissingMatch([self.cmt(1).id], [self.cmt(3).id], self.missing_1_3)
  92. def test_bogus_haves(self):
  93. """Ensure non-existent SHA in haves are tolerated"""
  94. bogus_sha = self.cmt(2).id[::-1]
  95. haves = [self.cmt(1).id, bogus_sha]
  96. wants = [self.cmt(3).id]
  97. self.assertMissingMatch(haves, wants, self.missing_1_3)
  98. def test_bogus_wants_failure(self):
  99. """Ensure non-existent SHA in wants are not tolerated"""
  100. bogus_sha = self.cmt(2).id[::-1]
  101. haves = [self.cmt(1).id]
  102. wants = [self.cmt(3).id, bogus_sha]
  103. self.assertRaises(
  104. KeyError, self.store.find_missing_objects, haves, wants, set()
  105. )
  106. def test_no_changes(self):
  107. self.assertMissingMatch([self.cmt(3).id], [self.cmt(3).id], [])
  108. class MOFMergeForkRepoTest(MissingObjectFinderTest):
  109. # 1 --- 2 --- 4 --- 6 --- 7
  110. # \ /
  111. # 3 ---
  112. # \
  113. # 5
  114. def setUp(self):
  115. super(MOFMergeForkRepoTest, self).setUp()
  116. f1_1 = make_object(Blob, data=b"f1")
  117. f1_2 = make_object(Blob, data=b"f1-2")
  118. f1_4 = make_object(Blob, data=b"f1-4")
  119. f1_7 = make_object(Blob, data=b"f1-2") # same data as in rev 2
  120. f2_1 = make_object(Blob, data=b"f2")
  121. f2_3 = make_object(Blob, data=b"f2-3")
  122. f3_3 = make_object(Blob, data=b"f3")
  123. f3_5 = make_object(Blob, data=b"f3-5")
  124. commit_spec = [[1], [2, 1], [3, 2], [4, 2], [5, 3], [6, 3, 4], [7, 6]]
  125. trees = {
  126. 1: [(b"f1", f1_1), (b"f2", f2_1)],
  127. 2: [(b"f1", f1_2), (b"f2", f2_1)], # f1 changed
  128. # f3 added, f2 changed
  129. 3: [(b"f1", f1_2), (b"f2", f2_3), (b"f3", f3_3)],
  130. 4: [(b"f1", f1_4), (b"f2", f2_1)], # f1 changed
  131. 5: [(b"f1", f1_2), (b"f3", f3_5)], # f2 removed, f3 changed
  132. # merged 3 and 4
  133. 6: [(b"f1", f1_4), (b"f2", f2_3), (b"f3", f3_3)],
  134. # f1 changed to match rev2. f3 removed
  135. 7: [(b"f1", f1_7), (b"f2", f2_3)],
  136. }
  137. self.commits = build_commit_graph(self.store, commit_spec, trees)
  138. self.f1_2_id = f1_2.id
  139. self.f1_4_id = f1_4.id
  140. self.f1_7_id = f1_7.id
  141. self.f2_3_id = f2_3.id
  142. self.f3_3_id = f3_3.id
  143. self.assertEqual(f1_2.id, f1_7.id, "[sanity]")
  144. def test_have6_want7(self):
  145. # have 6, want 7. Ideally, shall not report f1_7 as it's the same as
  146. # f1_2, however, to do so, MissingObjectFinder shall not record trees
  147. # of common commits only, but also all parent trees and tree items,
  148. # which is an overkill (i.e. in sha_done it records f1_4 as known, and
  149. # doesn't record f1_2 was known prior to that, hence can't detect f1_7
  150. # is in fact f1_2 and shall not be reported)
  151. self.assertMissingMatch(
  152. [self.cmt(6).id],
  153. [self.cmt(7).id],
  154. [self.cmt(7).id, self.cmt(7).tree, self.f1_7_id],
  155. )
  156. def test_have4_want7(self):
  157. # have 4, want 7. Shall not include rev5 as it is not in the tree
  158. # between 4 and 7 (well, it is, but its SHA's are irrelevant for 4..7
  159. # commit hierarchy)
  160. self.assertMissingMatch(
  161. [self.cmt(4).id],
  162. [self.cmt(7).id],
  163. [
  164. self.cmt(7).id,
  165. self.cmt(6).id,
  166. self.cmt(3).id,
  167. self.cmt(7).tree,
  168. self.cmt(6).tree,
  169. self.cmt(3).tree,
  170. self.f2_3_id,
  171. self.f3_3_id,
  172. ],
  173. )
  174. def test_have1_want6(self):
  175. # have 1, want 6. Shall not include rev5
  176. self.assertMissingMatch(
  177. [self.cmt(1).id],
  178. [self.cmt(6).id],
  179. [
  180. self.cmt(6).id,
  181. self.cmt(4).id,
  182. self.cmt(3).id,
  183. self.cmt(2).id,
  184. self.cmt(6).tree,
  185. self.cmt(4).tree,
  186. self.cmt(3).tree,
  187. self.cmt(2).tree,
  188. self.f1_2_id,
  189. self.f1_4_id,
  190. self.f2_3_id,
  191. self.f3_3_id,
  192. ],
  193. )
  194. def test_have3_want6(self):
  195. # have 3, want 7. Shall not report rev2 and its tree, because
  196. # haves(3) means has parents, i.e. rev2, too
  197. # BUT shall report any changes descending rev2 (excluding rev3)
  198. # Shall NOT report f1_7 as it's technically == f1_2
  199. self.assertMissingMatch(
  200. [self.cmt(3).id],
  201. [self.cmt(7).id],
  202. [
  203. self.cmt(7).id,
  204. self.cmt(6).id,
  205. self.cmt(4).id,
  206. self.cmt(7).tree,
  207. self.cmt(6).tree,
  208. self.cmt(4).tree,
  209. self.f1_4_id,
  210. ],
  211. )
  212. def test_have5_want7(self):
  213. # have 5, want 7. Common parent is rev2, hence children of rev2 from
  214. # a descent line other than rev5 shall be reported
  215. # expects f1_4 from rev6. f3_5 is known in rev5;
  216. # f1_7 shall be the same as f1_2 (known, too)
  217. self.assertMissingMatch(
  218. [self.cmt(5).id],
  219. [self.cmt(7).id],
  220. [
  221. self.cmt(7).id,
  222. self.cmt(6).id,
  223. self.cmt(4).id,
  224. self.cmt(7).tree,
  225. self.cmt(6).tree,
  226. self.cmt(4).tree,
  227. self.f1_4_id,
  228. ],
  229. )
  230. class MOFTagsTest(MissingObjectFinderTest):
  231. def setUp(self):
  232. super(MOFTagsTest, self).setUp()
  233. f1_1 = make_object(Blob, data=b"f1")
  234. commit_spec = [[1]]
  235. trees = {1: [(b"f1", f1_1)]}
  236. self.commits = build_commit_graph(self.store, commit_spec, trees)
  237. self._normal_tag = make_tag(self.cmt(1))
  238. self.store.add_object(self._normal_tag)
  239. self._tag_of_tag = make_tag(self._normal_tag)
  240. self.store.add_object(self._tag_of_tag)
  241. self._tag_of_tree = make_tag(self.store[self.cmt(1).tree])
  242. self.store.add_object(self._tag_of_tree)
  243. self._tag_of_blob = make_tag(f1_1)
  244. self.store.add_object(self._tag_of_blob)
  245. self._tag_of_tag_of_blob = make_tag(self._tag_of_blob)
  246. self.store.add_object(self._tag_of_tag_of_blob)
  247. self.f1_1_id = f1_1.id
  248. def test_tagged_commit(self):
  249. # The user already has the tagged commit, all they want is the tag,
  250. # so send them only the tag object.
  251. self.assertMissingMatch(
  252. [self.cmt(1).id], [self._normal_tag.id], [self._normal_tag.id]
  253. )
  254. # The remaining cases are unusual, but do happen in the wild.
  255. def test_tagged_tag(self):
  256. # User already has tagged tag, send only tag of tag
  257. self.assertMissingMatch(
  258. [self._normal_tag.id], [self._tag_of_tag.id], [self._tag_of_tag.id]
  259. )
  260. # User needs both tags, but already has commit
  261. self.assertMissingMatch(
  262. [self.cmt(1).id],
  263. [self._tag_of_tag.id],
  264. [self._normal_tag.id, self._tag_of_tag.id],
  265. )
  266. def test_tagged_tree(self):
  267. self.assertMissingMatch(
  268. [],
  269. [self._tag_of_tree.id],
  270. [self._tag_of_tree.id, self.cmt(1).tree, self.f1_1_id],
  271. )
  272. def test_tagged_blob(self):
  273. self.assertMissingMatch(
  274. [], [self._tag_of_blob.id], [self._tag_of_blob.id, self.f1_1_id]
  275. )
  276. def test_tagged_tagged_blob(self):
  277. self.assertMissingMatch(
  278. [],
  279. [self._tag_of_tag_of_blob.id],
  280. [self._tag_of_tag_of_blob.id, self._tag_of_blob.id, self.f1_1_id],
  281. )