test_diff_tree.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. # test_diff_tree.py -- Tests for file and tree diff utilities.
  2. # Copyright (C) 2010 Google, Inc.
  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; either version 2
  7. # or (at your option) a 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. """Tests for file and tree diff utilities."""
  19. from dulwich.diff_tree import (
  20. CHANGE_MODIFY,
  21. CHANGE_RENAME,
  22. CHANGE_COPY,
  23. CHANGE_UNCHANGED,
  24. TreeChange,
  25. _merge_entries,
  26. _merge_entries_py,
  27. tree_changes,
  28. tree_changes_for_merge,
  29. _count_blocks,
  30. _count_blocks_py,
  31. _similarity_score,
  32. _tree_change_key,
  33. RenameDetector,
  34. _is_tree,
  35. _is_tree_py
  36. )
  37. from dulwich.index import (
  38. commit_tree,
  39. )
  40. from dulwich._compat import (
  41. permutations,
  42. )
  43. from dulwich.object_store import (
  44. MemoryObjectStore,
  45. )
  46. from dulwich.objects import (
  47. ShaFile,
  48. Blob,
  49. TreeEntry,
  50. Tree,
  51. )
  52. from dulwich.tests import (
  53. TestCase,
  54. )
  55. from dulwich.tests.utils import (
  56. F,
  57. make_object,
  58. functest_builder,
  59. ext_functest_builder,
  60. )
  61. class DiffTestCase(TestCase):
  62. def setUp(self):
  63. super(DiffTestCase, self).setUp()
  64. self.store = MemoryObjectStore()
  65. self.empty_tree = self.commit_tree([])
  66. def commit_tree(self, entries):
  67. commit_blobs = []
  68. for entry in entries:
  69. if len(entry) == 2:
  70. path, obj = entry
  71. mode = F
  72. else:
  73. path, obj, mode = entry
  74. if isinstance(obj, Blob):
  75. self.store.add_object(obj)
  76. sha = obj.id
  77. else:
  78. sha = obj
  79. commit_blobs.append((path, sha, mode))
  80. return self.store[commit_tree(self.store, commit_blobs)]
  81. class TreeChangesTest(DiffTestCase):
  82. def assertMergeFails(self, merge_entries, name, mode, sha):
  83. t = Tree()
  84. t[name] = (mode, sha)
  85. self.assertRaises(TypeError, merge_entries, '', t, t)
  86. def _do_test_merge_entries(self, merge_entries):
  87. blob_a1 = make_object(Blob, data='a1')
  88. blob_a2 = make_object(Blob, data='a2')
  89. blob_b1 = make_object(Blob, data='b1')
  90. blob_c2 = make_object(Blob, data='c2')
  91. tree1 = self.commit_tree([('a', blob_a1, 0100644),
  92. ('b', blob_b1, 0100755)])
  93. tree2 = self.commit_tree([('a', blob_a2, 0100644),
  94. ('c', blob_c2, 0100755)])
  95. self.assertEqual([], merge_entries('', self.empty_tree,
  96. self.empty_tree))
  97. self.assertEqual([
  98. ((None, None, None), ('a', 0100644, blob_a1.id)),
  99. ((None, None, None), ('b', 0100755, blob_b1.id)),
  100. ], merge_entries('', self.empty_tree, tree1))
  101. self.assertEqual([
  102. ((None, None, None), ('x/a', 0100644, blob_a1.id)),
  103. ((None, None, None), ('x/b', 0100755, blob_b1.id)),
  104. ], merge_entries('x', self.empty_tree, tree1))
  105. self.assertEqual([
  106. (('a', 0100644, blob_a2.id), (None, None, None)),
  107. (('c', 0100755, blob_c2.id), (None, None, None)),
  108. ], merge_entries('', tree2, self.empty_tree))
  109. self.assertEqual([
  110. (('a', 0100644, blob_a1.id), ('a', 0100644, blob_a2.id)),
  111. (('b', 0100755, blob_b1.id), (None, None, None)),
  112. ((None, None, None), ('c', 0100755, blob_c2.id)),
  113. ], merge_entries('', tree1, tree2))
  114. self.assertEqual([
  115. (('a', 0100644, blob_a2.id), ('a', 0100644, blob_a1.id)),
  116. ((None, None, None), ('b', 0100755, blob_b1.id)),
  117. (('c', 0100755, blob_c2.id), (None, None, None)),
  118. ], merge_entries('', tree2, tree1))
  119. self.assertMergeFails(merge_entries, 0xdeadbeef, 0100644, '1' * 40)
  120. self.assertMergeFails(merge_entries, 'a', 'deadbeef', '1' * 40)
  121. self.assertMergeFails(merge_entries, 'a', 0100644, 0xdeadbeef)
  122. test_merge_entries = functest_builder(_do_test_merge_entries,
  123. _merge_entries_py)
  124. test_merge_entries_extension = ext_functest_builder(_do_test_merge_entries,
  125. _merge_entries)
  126. def _do_test_is_tree(self, is_tree):
  127. self.assertFalse(is_tree(TreeEntry(None, None, None)))
  128. self.assertFalse(is_tree(TreeEntry('a', 0100644, 'a' * 40)))
  129. self.assertFalse(is_tree(TreeEntry('a', 0100755, 'a' * 40)))
  130. self.assertFalse(is_tree(TreeEntry('a', 0120000, 'a' * 40)))
  131. self.assertTrue(is_tree(TreeEntry('a', 0040000, 'a' * 40)))
  132. self.assertRaises(TypeError, is_tree, TreeEntry('a', 'x', 'a' * 40))
  133. self.assertRaises(AttributeError, is_tree, 1234)
  134. test_is_tree = functest_builder(_do_test_is_tree, _is_tree_py)
  135. test_is_tree_extension = ext_functest_builder(_do_test_is_tree, _is_tree)
  136. def assertChangesEqual(self, expected, tree1, tree2, **kwargs):
  137. actual = list(tree_changes(self.store, tree1.id, tree2.id, **kwargs))
  138. self.assertEqual(expected, actual)
  139. # For brevity, the following tests use tuples instead of TreeEntry objects.
  140. def test_tree_changes_empty(self):
  141. self.assertChangesEqual([], self.empty_tree, self.empty_tree)
  142. def test_tree_changes_no_changes(self):
  143. blob = make_object(Blob, data='blob')
  144. tree = self.commit_tree([('a', blob), ('b/c', blob)])
  145. self.assertChangesEqual([], self.empty_tree, self.empty_tree)
  146. self.assertChangesEqual([], tree, tree)
  147. self.assertChangesEqual(
  148. [TreeChange(CHANGE_UNCHANGED, ('a', F, blob.id), ('a', F, blob.id)),
  149. TreeChange(CHANGE_UNCHANGED, ('b/c', F, blob.id),
  150. ('b/c', F, blob.id))],
  151. tree, tree, want_unchanged=True)
  152. def test_tree_changes_add_delete(self):
  153. blob_a = make_object(Blob, data='a')
  154. blob_b = make_object(Blob, data='b')
  155. tree = self.commit_tree([('a', blob_a, 0100644),
  156. ('x/b', blob_b, 0100755)])
  157. self.assertChangesEqual(
  158. [TreeChange.add(('a', 0100644, blob_a.id)),
  159. TreeChange.add(('x/b', 0100755, blob_b.id))],
  160. self.empty_tree, tree)
  161. self.assertChangesEqual(
  162. [TreeChange.delete(('a', 0100644, blob_a.id)),
  163. TreeChange.delete(('x/b', 0100755, blob_b.id))],
  164. tree, self.empty_tree)
  165. def test_tree_changes_modify_contents(self):
  166. blob_a1 = make_object(Blob, data='a1')
  167. blob_a2 = make_object(Blob, data='a2')
  168. tree1 = self.commit_tree([('a', blob_a1)])
  169. tree2 = self.commit_tree([('a', blob_a2)])
  170. self.assertChangesEqual(
  171. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  172. ('a', F, blob_a2.id))], tree1, tree2)
  173. def test_tree_changes_modify_mode(self):
  174. blob_a = make_object(Blob, data='a')
  175. tree1 = self.commit_tree([('a', blob_a, 0100644)])
  176. tree2 = self.commit_tree([('a', blob_a, 0100755)])
  177. self.assertChangesEqual(
  178. [TreeChange(CHANGE_MODIFY, ('a', 0100644, blob_a.id),
  179. ('a', 0100755, blob_a.id))], tree1, tree2)
  180. def test_tree_changes_change_type(self):
  181. blob_a1 = make_object(Blob, data='a')
  182. blob_a2 = make_object(Blob, data='/foo/bar')
  183. tree1 = self.commit_tree([('a', blob_a1, 0100644)])
  184. tree2 = self.commit_tree([('a', blob_a2, 0120000)])
  185. self.assertChangesEqual(
  186. [TreeChange.delete(('a', 0100644, blob_a1.id)),
  187. TreeChange.add(('a', 0120000, blob_a2.id))],
  188. tree1, tree2)
  189. def test_tree_changes_to_tree(self):
  190. blob_a = make_object(Blob, data='a')
  191. blob_x = make_object(Blob, data='x')
  192. tree1 = self.commit_tree([('a', blob_a)])
  193. tree2 = self.commit_tree([('a/x', blob_x)])
  194. self.assertChangesEqual(
  195. [TreeChange.delete(('a', F, blob_a.id)),
  196. TreeChange.add(('a/x', F, blob_x.id))],
  197. tree1, tree2)
  198. def test_tree_changes_complex(self):
  199. blob_a_1 = make_object(Blob, data='a1_1')
  200. blob_bx1_1 = make_object(Blob, data='bx1_1')
  201. blob_bx2_1 = make_object(Blob, data='bx2_1')
  202. blob_by1_1 = make_object(Blob, data='by1_1')
  203. blob_by2_1 = make_object(Blob, data='by2_1')
  204. tree1 = self.commit_tree([
  205. ('a', blob_a_1),
  206. ('b/x/1', blob_bx1_1),
  207. ('b/x/2', blob_bx2_1),
  208. ('b/y/1', blob_by1_1),
  209. ('b/y/2', blob_by2_1),
  210. ])
  211. blob_a_2 = make_object(Blob, data='a1_2')
  212. blob_bx1_2 = blob_bx1_1
  213. blob_by_2 = make_object(Blob, data='by_2')
  214. blob_c_2 = make_object(Blob, data='c_2')
  215. tree2 = self.commit_tree([
  216. ('a', blob_a_2),
  217. ('b/x/1', blob_bx1_2),
  218. ('b/y', blob_by_2),
  219. ('c', blob_c_2),
  220. ])
  221. self.assertChangesEqual(
  222. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a_1.id),
  223. ('a', F, blob_a_2.id)),
  224. TreeChange.delete(('b/x/2', F, blob_bx2_1.id)),
  225. TreeChange.add(('b/y', F, blob_by_2.id)),
  226. TreeChange.delete(('b/y/1', F, blob_by1_1.id)),
  227. TreeChange.delete(('b/y/2', F, blob_by2_1.id)),
  228. TreeChange.add(('c', F, blob_c_2.id))],
  229. tree1, tree2)
  230. def test_tree_changes_name_order(self):
  231. blob = make_object(Blob, data='a')
  232. tree1 = self.commit_tree([('a', blob), ('a.', blob), ('a..', blob)])
  233. # Tree order is the reverse of this, so if we used tree order, 'a..'
  234. # would not be merged.
  235. tree2 = self.commit_tree([('a/x', blob), ('a./x', blob), ('a..', blob)])
  236. self.assertChangesEqual(
  237. [TreeChange.delete(('a', F, blob.id)),
  238. TreeChange.add(('a/x', F, blob.id)),
  239. TreeChange.delete(('a.', F, blob.id)),
  240. TreeChange.add(('a./x', F, blob.id))],
  241. tree1, tree2)
  242. def test_tree_changes_prune(self):
  243. blob_a1 = make_object(Blob, data='a1')
  244. blob_a2 = make_object(Blob, data='a2')
  245. blob_x = make_object(Blob, data='x')
  246. tree1 = self.commit_tree([('a', blob_a1), ('b/x', blob_x)])
  247. tree2 = self.commit_tree([('a', blob_a2), ('b/x', blob_x)])
  248. # Remove identical items so lookups will fail unless we prune.
  249. subtree = self.store[tree1['b'][1]]
  250. for entry in subtree.iteritems():
  251. del self.store[entry.sha]
  252. del self.store[subtree.id]
  253. self.assertChangesEqual(
  254. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  255. ('a', F, blob_a2.id))],
  256. tree1, tree2)
  257. def assertChangesForMergeEqual(self, expected, parent_trees, merge_tree,
  258. **kwargs):
  259. parent_tree_ids = [t.id for t in parent_trees]
  260. actual = list(tree_changes_for_merge(
  261. self.store, parent_tree_ids, merge_tree.id, **kwargs))
  262. self.assertEqual(expected, actual)
  263. parent_tree_ids.reverse()
  264. expected = [list(reversed(cs)) for cs in expected]
  265. actual = list(tree_changes_for_merge(
  266. self.store, parent_tree_ids, merge_tree.id, **kwargs))
  267. self.assertEqual(expected, actual)
  268. def test_tree_changes_for_merge_add_no_conflict(self):
  269. blob = make_object(Blob, data='blob')
  270. parent1 = self.commit_tree([])
  271. parent2 = merge = self.commit_tree([('a', blob)])
  272. self.assertChangesForMergeEqual([], [parent1, parent2], merge)
  273. self.assertChangesForMergeEqual([], [parent2, parent2], merge)
  274. def test_tree_changes_for_merge_add_modify_conflict(self):
  275. blob1 = make_object(Blob, data='1')
  276. blob2 = make_object(Blob, data='2')
  277. parent1 = self.commit_tree([])
  278. parent2 = self.commit_tree([('a', blob1)])
  279. merge = self.commit_tree([('a', blob2)])
  280. self.assertChangesForMergeEqual(
  281. [[TreeChange.add(('a', F, blob2.id)),
  282. TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob2.id))]],
  283. [parent1, parent2], merge)
  284. def test_tree_changes_for_merge_modify_modify_conflict(self):
  285. blob1 = make_object(Blob, data='1')
  286. blob2 = make_object(Blob, data='2')
  287. blob3 = make_object(Blob, data='3')
  288. parent1 = self.commit_tree([('a', blob1)])
  289. parent2 = self.commit_tree([('a', blob2)])
  290. merge = self.commit_tree([('a', blob3)])
  291. self.assertChangesForMergeEqual(
  292. [[TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob3.id)),
  293. TreeChange(CHANGE_MODIFY, ('a', F, blob2.id), ('a', F, blob3.id))]],
  294. [parent1, parent2], merge)
  295. def test_tree_changes_for_merge_modify_no_conflict(self):
  296. blob1 = make_object(Blob, data='1')
  297. blob2 = make_object(Blob, data='2')
  298. parent1 = self.commit_tree([('a', blob1)])
  299. parent2 = merge = self.commit_tree([('a', blob2)])
  300. self.assertChangesForMergeEqual([], [parent1, parent2], merge)
  301. def test_tree_changes_for_merge_delete_delete_conflict(self):
  302. blob1 = make_object(Blob, data='1')
  303. blob2 = make_object(Blob, data='2')
  304. parent1 = self.commit_tree([('a', blob1)])
  305. parent2 = self.commit_tree([('a', blob2)])
  306. merge = self.commit_tree([])
  307. self.assertChangesForMergeEqual(
  308. [[TreeChange.delete(('a', F, blob1.id)),
  309. TreeChange.delete(('a', F, blob2.id))]],
  310. [parent1, parent2], merge)
  311. def test_tree_changes_for_merge_delete_no_conflict(self):
  312. blob = make_object(Blob, data='blob')
  313. has = self.commit_tree([('a', blob)])
  314. doesnt_have = self.commit_tree([])
  315. self.assertChangesForMergeEqual([], [has, has], doesnt_have)
  316. self.assertChangesForMergeEqual([], [has, doesnt_have], doesnt_have)
  317. def test_tree_changes_for_merge_octopus_no_conflict(self):
  318. r = range(5)
  319. blobs = [make_object(Blob, data=str(i)) for i in r]
  320. parents = [self.commit_tree([('a', blobs[i])]) for i in r]
  321. for i in r:
  322. # Take the SHA from each of the parents.
  323. self.assertChangesForMergeEqual([], parents, parents[i])
  324. def test_tree_changes_for_merge_octopus_modify_conflict(self):
  325. # Because the octopus merge strategy is limited, I doubt it's possible
  326. # to create this with the git command line. But the output is well-
  327. # defined, so test it anyway.
  328. r = range(5)
  329. parent_blobs = [make_object(Blob, data=str(i)) for i in r]
  330. merge_blob = make_object(Blob, data='merge')
  331. parents = [self.commit_tree([('a', parent_blobs[i])]) for i in r]
  332. merge = self.commit_tree([('a', merge_blob)])
  333. expected = [[TreeChange(CHANGE_MODIFY, ('a', F, parent_blobs[i].id),
  334. ('a', F, merge_blob.id)) for i in r]]
  335. self.assertChangesForMergeEqual(expected, parents, merge)
  336. def test_tree_changes_for_merge_octopus_delete(self):
  337. blob1 = make_object(Blob, data='1')
  338. blob2 = make_object(Blob, data='3')
  339. parent1 = self.commit_tree([('a', blob1)])
  340. parent2 = self.commit_tree([('a', blob2)])
  341. parent3 = merge = self.commit_tree([])
  342. self.assertChangesForMergeEqual([], [parent1, parent1, parent1], merge)
  343. self.assertChangesForMergeEqual([], [parent1, parent1, parent3], merge)
  344. self.assertChangesForMergeEqual([], [parent1, parent3, parent3], merge)
  345. self.assertChangesForMergeEqual(
  346. [[TreeChange.delete(('a', F, blob1.id)),
  347. TreeChange.delete(('a', F, blob2.id)),
  348. None]],
  349. [parent1, parent2, parent3], merge)
  350. class RenameDetectionTest(DiffTestCase):
  351. def _do_test_count_blocks(self, count_blocks):
  352. blob = make_object(Blob, data='a\nb\na\n')
  353. self.assertEqual({hash('a\n'): 4, hash('b\n'): 2}, count_blocks(blob))
  354. test_count_blocks = functest_builder(_do_test_count_blocks,
  355. _count_blocks_py)
  356. test_count_blocks_extension = ext_functest_builder(_do_test_count_blocks,
  357. _count_blocks)
  358. def _do_test_count_blocks_no_newline(self, count_blocks):
  359. blob = make_object(Blob, data='a\na')
  360. self.assertEqual({hash('a\n'): 2, hash('a'): 1}, _count_blocks(blob))
  361. test_count_blocks_no_newline = functest_builder(
  362. _do_test_count_blocks_no_newline, _count_blocks_py)
  363. test_count_blocks_no_newline_extension = ext_functest_builder(
  364. _do_test_count_blocks_no_newline, _count_blocks)
  365. def _do_test_count_blocks_chunks(self, count_blocks):
  366. blob = ShaFile.from_raw_chunks(Blob.type_num, ['a\nb', '\na\n'])
  367. self.assertEqual({hash('a\n'): 4, hash('b\n'): 2}, _count_blocks(blob))
  368. test_count_blocks_chunks = functest_builder(_do_test_count_blocks_chunks,
  369. _count_blocks_py)
  370. test_count_blocks_chunks_extension = ext_functest_builder(
  371. _do_test_count_blocks_chunks, _count_blocks)
  372. def _do_test_count_blocks_long_lines(self, count_blocks):
  373. a = 'a' * 64
  374. data = a + 'xxx\ny\n' + a + 'zzz\n'
  375. blob = make_object(Blob, data=data)
  376. self.assertEqual({hash('a' * 64): 128, hash('xxx\n'): 4, hash('y\n'): 2,
  377. hash('zzz\n'): 4},
  378. _count_blocks(blob))
  379. test_count_blocks_long_lines = functest_builder(
  380. _do_test_count_blocks_long_lines, _count_blocks_py)
  381. test_count_blocks_long_lines_extension = ext_functest_builder(
  382. _do_test_count_blocks_long_lines, _count_blocks)
  383. def assertSimilar(self, expected_score, blob1, blob2):
  384. self.assertEqual(expected_score, _similarity_score(blob1, blob2))
  385. self.assertEqual(expected_score, _similarity_score(blob2, blob1))
  386. def test_similarity_score(self):
  387. blob0 = make_object(Blob, data='')
  388. blob1 = make_object(Blob, data='ab\ncd\ncd\n')
  389. blob2 = make_object(Blob, data='ab\n')
  390. blob3 = make_object(Blob, data='cd\n')
  391. blob4 = make_object(Blob, data='cd\ncd\n')
  392. self.assertSimilar(100, blob0, blob0)
  393. self.assertSimilar(0, blob0, blob1)
  394. self.assertSimilar(33, blob1, blob2)
  395. self.assertSimilar(33, blob1, blob3)
  396. self.assertSimilar(66, blob1, blob4)
  397. self.assertSimilar(0, blob2, blob3)
  398. self.assertSimilar(50, blob3, blob4)
  399. def test_similarity_score_cache(self):
  400. blob1 = make_object(Blob, data='ab\ncd\n')
  401. blob2 = make_object(Blob, data='ab\n')
  402. block_cache = {}
  403. self.assertEqual(
  404. 50, _similarity_score(blob1, blob2, block_cache=block_cache))
  405. self.assertEqual(set([blob1.id, blob2.id]), set(block_cache))
  406. def fail_chunks():
  407. self.fail('Unexpected call to as_raw_chunks()')
  408. blob1.as_raw_chunks = blob2.as_raw_chunks = fail_chunks
  409. blob1.raw_length = lambda: 6
  410. blob2.raw_length = lambda: 3
  411. self.assertEqual(
  412. 50, _similarity_score(blob1, blob2, block_cache=block_cache))
  413. def test_tree_entry_sort(self):
  414. sha = 'abcd' * 10
  415. expected_entries = [
  416. TreeChange.add(TreeEntry('aaa', F, sha)),
  417. TreeChange(CHANGE_COPY, TreeEntry('bbb', F, sha),
  418. TreeEntry('aab', F, sha)),
  419. TreeChange(CHANGE_MODIFY, TreeEntry('bbb', F, sha),
  420. TreeEntry('bbb', F, 'dabc' * 10)),
  421. TreeChange(CHANGE_RENAME, TreeEntry('bbc', F, sha),
  422. TreeEntry('ddd', F, sha)),
  423. TreeChange.delete(TreeEntry('ccc', F, sha)),
  424. ]
  425. for perm in permutations(expected_entries):
  426. self.assertEqual(expected_entries,
  427. sorted(perm, key=_tree_change_key))
  428. def detect_renames(self, tree1, tree2, **kwargs):
  429. detector = RenameDetector(self.store, tree1.id, tree2.id, **kwargs)
  430. return detector.changes_with_renames()
  431. def test_no_renames(self):
  432. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  433. blob2 = make_object(Blob, data='a\nb\ne\nf\n')
  434. blob3 = make_object(Blob, data='a\nb\ng\nh\n')
  435. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  436. tree2 = self.commit_tree([('a', blob1), ('b', blob3)])
  437. self.assertEqual(
  438. [TreeChange(CHANGE_MODIFY, ('b', F, blob2.id), ('b', F, blob3.id))],
  439. self.detect_renames(tree1, tree2))
  440. def test_exact_rename_one_to_one(self):
  441. blob1 = make_object(Blob, data='1')
  442. blob2 = make_object(Blob, data='2')
  443. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  444. tree2 = self.commit_tree([('c', blob1), ('d', blob2)])
  445. self.assertEqual(
  446. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob1.id)),
  447. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('d', F, blob2.id))],
  448. self.detect_renames(tree1, tree2))
  449. def test_exact_rename_split_different_type(self):
  450. blob = make_object(Blob, data='/foo')
  451. tree1 = self.commit_tree([('a', blob, 0100644)])
  452. tree2 = self.commit_tree([('a', blob, 0120000)])
  453. self.assertEqual(
  454. [TreeChange.add(('a', 0120000, blob.id)),
  455. TreeChange.delete(('a', 0100644, blob.id))],
  456. self.detect_renames(tree1, tree2))
  457. def test_exact_rename_and_different_type(self):
  458. blob1 = make_object(Blob, data='1')
  459. blob2 = make_object(Blob, data='2')
  460. tree1 = self.commit_tree([('a', blob1)])
  461. tree2 = self.commit_tree([('a', blob2, 0120000), ('b', blob1)])
  462. self.assertEqual(
  463. [TreeChange.add(('a', 0120000, blob2.id)),
  464. TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob1.id))],
  465. self.detect_renames(tree1, tree2))
  466. def test_exact_rename_one_to_many(self):
  467. blob = make_object(Blob, data='1')
  468. tree1 = self.commit_tree([('a', blob)])
  469. tree2 = self.commit_tree([('b', blob), ('c', blob)])
  470. self.assertEqual(
  471. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('b', F, blob.id)),
  472. TreeChange(CHANGE_COPY, ('a', F, blob.id), ('c', F, blob.id))],
  473. self.detect_renames(tree1, tree2))
  474. def test_exact_rename_many_to_one(self):
  475. blob = make_object(Blob, data='1')
  476. tree1 = self.commit_tree([('a', blob), ('b', blob)])
  477. tree2 = self.commit_tree([('c', blob)])
  478. self.assertEqual(
  479. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('c', F, blob.id)),
  480. TreeChange.delete(('b', F, blob.id))],
  481. self.detect_renames(tree1, tree2))
  482. def test_exact_rename_many_to_many(self):
  483. blob = make_object(Blob, data='1')
  484. tree1 = self.commit_tree([('a', blob), ('b', blob)])
  485. tree2 = self.commit_tree([('c', blob), ('d', blob), ('e', blob)])
  486. self.assertEqual(
  487. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('c', F, blob.id)),
  488. TreeChange(CHANGE_COPY, ('a', F, blob.id), ('e', F, blob.id)),
  489. TreeChange(CHANGE_RENAME, ('b', F, blob.id), ('d', F, blob.id))],
  490. self.detect_renames(tree1, tree2))
  491. def test_rename_threshold(self):
  492. blob1 = make_object(Blob, data='a\nb\nc\n')
  493. blob2 = make_object(Blob, data='a\nb\nd\n')
  494. tree1 = self.commit_tree([('a', blob1)])
  495. tree2 = self.commit_tree([('b', blob2)])
  496. self.assertEqual(
  497. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob2.id))],
  498. self.detect_renames(tree1, tree2, rename_threshold=50))
  499. self.assertEqual(
  500. [TreeChange.delete(('a', F, blob1.id)),
  501. TreeChange.add(('b', F, blob2.id))],
  502. self.detect_renames(tree1, tree2, rename_threshold=75))
  503. def test_content_rename_max_files(self):
  504. blob1 = make_object(Blob, data='a\nb\nc\nd')
  505. blob4 = make_object(Blob, data='a\nb\nc\ne\n')
  506. blob2 = make_object(Blob, data='e\nf\ng\nh\n')
  507. blob3 = make_object(Blob, data='e\nf\ng\ni\n')
  508. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  509. tree2 = self.commit_tree([('c', blob3), ('d', blob4)])
  510. self.assertEqual(
  511. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('d', F, blob4.id)),
  512. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('c', F, blob3.id))],
  513. self.detect_renames(tree1, tree2))
  514. self.assertEqual(
  515. [TreeChange.delete(('a', F, blob1.id)),
  516. TreeChange.delete(('b', F, blob2.id)),
  517. TreeChange.add(('c', F, blob3.id)),
  518. TreeChange.add(('d', F, blob4.id))],
  519. self.detect_renames(tree1, tree2, max_files=1))
  520. def test_content_rename_one_to_one(self):
  521. b11 = make_object(Blob, data='a\nb\nc\nd\n')
  522. b12 = make_object(Blob, data='a\nb\nc\ne\n')
  523. b21 = make_object(Blob, data='e\nf\ng\n\h')
  524. b22 = make_object(Blob, data='e\nf\ng\n\i')
  525. tree1 = self.commit_tree([('a', b11), ('b', b21)])
  526. tree2 = self.commit_tree([('c', b12), ('d', b22)])
  527. self.assertEqual(
  528. [TreeChange(CHANGE_RENAME, ('a', F, b11.id), ('c', F, b12.id)),
  529. TreeChange(CHANGE_RENAME, ('b', F, b21.id), ('d', F, b22.id))],
  530. self.detect_renames(tree1, tree2))
  531. def test_content_rename_one_to_one_ordering(self):
  532. blob1 = make_object(Blob, data='a\nb\nc\nd\ne\nf\n')
  533. blob2 = make_object(Blob, data='a\nb\nc\nd\ng\nh\n')
  534. # 6/10 match to blob1, 8/10 match to blob2
  535. blob3 = make_object(Blob, data='a\nb\nc\nd\ng\ni\n')
  536. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  537. tree2 = self.commit_tree([('c', blob3)])
  538. self.assertEqual(
  539. [TreeChange.delete(('a', F, blob1.id)),
  540. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('c', F, blob3.id))],
  541. self.detect_renames(tree1, tree2))
  542. tree3 = self.commit_tree([('a', blob2), ('b', blob1)])
  543. tree4 = self.commit_tree([('c', blob3)])
  544. self.assertEqual(
  545. [TreeChange(CHANGE_RENAME, ('a', F, blob2.id), ('c', F, blob3.id)),
  546. TreeChange.delete(('b', F, blob1.id))],
  547. self.detect_renames(tree3, tree4))
  548. def test_content_rename_one_to_many(self):
  549. blob1 = make_object(Blob, data='aa\nb\nc\nd\ne\n')
  550. blob2 = make_object(Blob, data='ab\nb\nc\nd\ne\n') # 8/11 match
  551. blob3 = make_object(Blob, data='aa\nb\nc\nd\nf\n') # 9/11 match
  552. tree1 = self.commit_tree([('a', blob1)])
  553. tree2 = self.commit_tree([('b', blob2), ('c', blob3)])
  554. self.assertEqual(
  555. [TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('b', F, blob2.id)),
  556. TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id))],
  557. self.detect_renames(tree1, tree2))
  558. def test_content_rename_many_to_one(self):
  559. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  560. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  561. blob3 = make_object(Blob, data='a\nb\nc\nf\n')
  562. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  563. tree2 = self.commit_tree([('c', blob3)])
  564. self.assertEqual(
  565. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id)),
  566. TreeChange.delete(('b', F, blob2.id))],
  567. self.detect_renames(tree1, tree2))
  568. def test_content_rename_many_to_many(self):
  569. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  570. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  571. blob3 = make_object(Blob, data='a\nb\nc\nf\n')
  572. blob4 = make_object(Blob, data='a\nb\nc\ng\n')
  573. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  574. tree2 = self.commit_tree([('c', blob3), ('d', blob4)])
  575. # TODO(dborowitz): Distribute renames rather than greedily choosing
  576. # copies.
  577. self.assertEqual(
  578. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id)),
  579. TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('d', F, blob4.id)),
  580. TreeChange.delete(('b', F, blob2.id))],
  581. self.detect_renames(tree1, tree2))
  582. def test_content_rename_gitlink(self):
  583. blob1 = make_object(Blob, data='blob1')
  584. blob2 = make_object(Blob, data='blob2')
  585. link1 = '1' * 40
  586. link2 = '2' * 40
  587. tree1 = self.commit_tree([('a', blob1), ('b', link1, 0160000)])
  588. tree2 = self.commit_tree([('c', blob2), ('d', link2, 0160000)])
  589. self.assertEqual(
  590. [TreeChange.delete(('a', 0100644, blob1.id)),
  591. TreeChange.delete(('b', 0160000, link1)),
  592. TreeChange.add(('c', 0100644, blob2.id)),
  593. TreeChange.add(('d', 0160000, link2))],
  594. self.detect_renames(tree1, tree2))
  595. def test_exact_rename_swap(self):
  596. blob1 = make_object(Blob, data='1')
  597. blob2 = make_object(Blob, data='2')
  598. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  599. tree2 = self.commit_tree([('a', blob2), ('b', blob1)])
  600. self.assertEqual(
  601. [TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob2.id)),
  602. TreeChange(CHANGE_MODIFY, ('b', F, blob2.id), ('b', F, blob1.id))],
  603. self.detect_renames(tree1, tree2))
  604. self.assertEqual(
  605. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob1.id)),
  606. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('a', F, blob2.id))],
  607. self.detect_renames(tree1, tree2, rewrite_threshold=50))
  608. def test_content_rename_swap(self):
  609. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  610. blob2 = make_object(Blob, data='e\nf\ng\nh\n')
  611. blob3 = make_object(Blob, data='a\nb\nc\ne\n')
  612. blob4 = make_object(Blob, data='e\nf\ng\ni\n')
  613. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  614. tree2 = self.commit_tree([('a', blob4), ('b', blob3)])
  615. self.assertEqual(
  616. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob3.id)),
  617. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('a', F, blob4.id))],
  618. self.detect_renames(tree1, tree2, rewrite_threshold=60))
  619. def test_rewrite_threshold(self):
  620. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  621. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  622. blob3 = make_object(Blob, data='a\nb\nf\ng\n')
  623. tree1 = self.commit_tree([('a', blob1)])
  624. tree2 = self.commit_tree([('a', blob3), ('b', blob2)])
  625. no_renames = [
  626. TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob3.id)),
  627. TreeChange.add(('b', F, blob2.id))]
  628. self.assertEqual(
  629. no_renames, self.detect_renames(tree1, tree2))
  630. self.assertEqual(
  631. no_renames, self.detect_renames(tree1, tree2, rewrite_threshold=40))
  632. self.assertEqual(
  633. [TreeChange.add(('a', F, blob3.id)),
  634. TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob2.id))],
  635. self.detect_renames(tree1, tree2, rewrite_threshold=80))
  636. def test_find_copies_harder_exact(self):
  637. blob = make_object(Blob, data='blob')
  638. tree1 = self.commit_tree([('a', blob)])
  639. tree2 = self.commit_tree([('a', blob), ('b', blob)])
  640. self.assertEqual([TreeChange.add(('b', F, blob.id))],
  641. self.detect_renames(tree1, tree2))
  642. self.assertEqual(
  643. [TreeChange(CHANGE_COPY, ('a', F, blob.id), ('b', F, blob.id))],
  644. self.detect_renames(tree1, tree2, find_copies_harder=True))
  645. def test_find_copies_harder_content(self):
  646. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  647. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  648. tree1 = self.commit_tree([('a', blob1)])
  649. tree2 = self.commit_tree([('a', blob1), ('b', blob2)])
  650. self.assertEqual([TreeChange.add(('b', F, blob2.id))],
  651. self.detect_renames(tree1, tree2))
  652. self.assertEqual(
  653. [TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('b', F, blob2.id))],
  654. self.detect_renames(tree1, tree2, find_copies_harder=True))
  655. def test_find_copies_harder_modify(self):
  656. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  657. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  658. tree1 = self.commit_tree([('a', blob1)])
  659. tree2 = self.commit_tree([('a', blob2), ('b', blob2)])
  660. self.assertEqual(
  661. [TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob2.id)),
  662. TreeChange.add(('b', F, blob2.id))],
  663. self.detect_renames(tree1, tree2))
  664. self.assertEqual(
  665. [TreeChange(CHANGE_MODIFY, ('a', F, blob1.id), ('a', F, blob2.id)),
  666. TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('b', F, blob2.id))],
  667. self.detect_renames(tree1, tree2, find_copies_harder=True))
  668. def test_find_copies_harder_with_rewrites(self):
  669. blob_a1 = make_object(Blob, data='a\nb\nc\nd\n')
  670. blob_a2 = make_object(Blob, data='f\ng\nh\ni\n')
  671. blob_b2 = make_object(Blob, data='a\nb\nc\ne\n')
  672. tree1 = self.commit_tree([('a', blob_a1)])
  673. tree2 = self.commit_tree([('a', blob_a2), ('b', blob_b2)])
  674. self.assertEqual(
  675. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  676. ('a', F, blob_a2.id)),
  677. TreeChange(CHANGE_COPY, ('a', F, blob_a1.id), ('b', F, blob_b2.id))],
  678. self.detect_renames(tree1, tree2, find_copies_harder=True))
  679. self.assertEqual(
  680. [TreeChange.add(('a', F, blob_a2.id)),
  681. TreeChange(CHANGE_RENAME, ('a', F, blob_a1.id),
  682. ('b', F, blob_b2.id))],
  683. self.detect_renames(tree1, tree2, rewrite_threshold=50,
  684. find_copies_harder=True))