test_diff_tree.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. tree_changes,
  27. _count_blocks,
  28. _similarity_score,
  29. _tree_change_key,
  30. RenameDetector,
  31. )
  32. from dulwich.index import (
  33. commit_tree,
  34. )
  35. from dulwich.misc import (
  36. permutations,
  37. )
  38. from dulwich.object_store import (
  39. MemoryObjectStore,
  40. )
  41. from dulwich.objects import (
  42. ShaFile,
  43. Blob,
  44. TreeEntry,
  45. )
  46. from dulwich.tests import (
  47. TestCase,
  48. )
  49. from dulwich.tests.utils import (
  50. make_object,
  51. )
  52. # Shorthand mode for Files.
  53. F = 0100644
  54. class DiffTestCase(TestCase):
  55. def setUp(self):
  56. super(DiffTestCase, self).setUp()
  57. self.store = MemoryObjectStore()
  58. self.empty_tree = self.commit_tree([])
  59. def commit_tree(self, entries):
  60. commit_blobs = []
  61. for entry in entries:
  62. if len(entry) == 2:
  63. path, obj = entry
  64. mode = F
  65. else:
  66. path, obj, mode = entry
  67. if isinstance(obj, Blob):
  68. self.store.add_object(obj)
  69. sha = obj.id
  70. else:
  71. sha = obj
  72. commit_blobs.append((path, sha, mode))
  73. return self.store[commit_tree(self.store, commit_blobs)]
  74. class TreeChangesTest(DiffTestCase):
  75. def test_merge_entries(self):
  76. blob_a1 = make_object(Blob, data='a1')
  77. blob_a2 = make_object(Blob, data='a2')
  78. blob_b1 = make_object(Blob, data='b1')
  79. blob_c2 = make_object(Blob, data='c2')
  80. tree1 = self.commit_tree([('a', blob_a1, 0100644),
  81. ('b', blob_b1, 0100755)])
  82. tree2 = self.commit_tree([('a', blob_a2, 0100644),
  83. ('c', blob_c2, 0100755)])
  84. self.assertEqual([], _merge_entries('', self.empty_tree,
  85. self.empty_tree))
  86. self.assertEqual([
  87. ((None, None, None), ('a', 0100644, blob_a1.id)),
  88. ((None, None, None), ('b', 0100755, blob_b1.id)),
  89. ], _merge_entries('', self.empty_tree, tree1))
  90. self.assertEqual([
  91. ((None, None, None), ('x/a', 0100644, blob_a1.id)),
  92. ((None, None, None), ('x/b', 0100755, blob_b1.id)),
  93. ], _merge_entries('x', self.empty_tree, tree1))
  94. self.assertEqual([
  95. (('a', 0100644, blob_a2.id), (None, None, None)),
  96. (('c', 0100755, blob_c2.id), (None, None, None)),
  97. ], _merge_entries('', tree2, self.empty_tree))
  98. self.assertEqual([
  99. (('a', 0100644, blob_a1.id), ('a', 0100644, blob_a2.id)),
  100. (('b', 0100755, blob_b1.id), (None, None, None)),
  101. ((None, None, None), ('c', 0100755, blob_c2.id)),
  102. ], _merge_entries('', tree1, tree2))
  103. self.assertEqual([
  104. (('a', 0100644, blob_a2.id), ('a', 0100644, blob_a1.id)),
  105. ((None, None, None), ('b', 0100755, blob_b1.id)),
  106. (('c', 0100755, blob_c2.id), (None, None, None)),
  107. ], _merge_entries('', tree2, tree1))
  108. def assertChangesEqual(self, expected, tree1, tree2, **kwargs):
  109. actual = list(tree_changes(self.store, tree1.id, tree2.id, **kwargs))
  110. self.assertEqual(expected, actual)
  111. # For brevity, the following tests use tuples instead of TreeEntry objects.
  112. def test_tree_changes_empty(self):
  113. self.assertChangesEqual([], self.empty_tree, self.empty_tree)
  114. def test_tree_changes_no_changes(self):
  115. blob = make_object(Blob, data='blob')
  116. tree = self.commit_tree([('a', blob), ('b/c', blob)])
  117. self.assertChangesEqual([], self.empty_tree, self.empty_tree)
  118. self.assertChangesEqual([], tree, tree)
  119. self.assertChangesEqual(
  120. [TreeChange(CHANGE_UNCHANGED, ('a', F, blob.id), ('a', F, blob.id)),
  121. TreeChange(CHANGE_UNCHANGED, ('b/c', F, blob.id),
  122. ('b/c', F, blob.id))],
  123. tree, tree, want_unchanged=True)
  124. def test_tree_changes_add_delete(self):
  125. blob_a = make_object(Blob, data='a')
  126. blob_b = make_object(Blob, data='b')
  127. tree = self.commit_tree([('a', blob_a, 0100644),
  128. ('x/b', blob_b, 0100755)])
  129. self.assertChangesEqual(
  130. [TreeChange.add(('a', 0100644, blob_a.id)),
  131. TreeChange.add(('x/b', 0100755, blob_b.id))],
  132. self.empty_tree, tree)
  133. self.assertChangesEqual(
  134. [TreeChange.delete(('a', 0100644, blob_a.id)),
  135. TreeChange.delete(('x/b', 0100755, blob_b.id))],
  136. tree, self.empty_tree)
  137. def test_tree_changes_modify_contents(self):
  138. blob_a1 = make_object(Blob, data='a1')
  139. blob_a2 = make_object(Blob, data='a2')
  140. tree1 = self.commit_tree([('a', blob_a1)])
  141. tree2 = self.commit_tree([('a', blob_a2)])
  142. self.assertChangesEqual(
  143. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  144. ('a', F, blob_a2.id))], tree1, tree2)
  145. def test_tree_changes_modify_mode(self):
  146. blob_a = make_object(Blob, data='a')
  147. tree1 = self.commit_tree([('a', blob_a, 0100644)])
  148. tree2 = self.commit_tree([('a', blob_a, 0100755)])
  149. self.assertChangesEqual(
  150. [TreeChange(CHANGE_MODIFY, ('a', 0100644, blob_a.id),
  151. ('a', 0100755, blob_a.id))], tree1, tree2)
  152. def test_tree_changes_change_type(self):
  153. blob_a1 = make_object(Blob, data='a')
  154. blob_a2 = make_object(Blob, data='/foo/bar')
  155. tree1 = self.commit_tree([('a', blob_a1, 0100644)])
  156. tree2 = self.commit_tree([('a', blob_a2, 0120000)])
  157. self.assertChangesEqual(
  158. [TreeChange.delete(('a', 0100644, blob_a1.id)),
  159. TreeChange.add(('a', 0120000, blob_a2.id))],
  160. tree1, tree2)
  161. def test_tree_changes_to_tree(self):
  162. blob_a = make_object(Blob, data='a')
  163. blob_x = make_object(Blob, data='x')
  164. tree1 = self.commit_tree([('a', blob_a)])
  165. tree2 = self.commit_tree([('a/x', blob_x)])
  166. self.assertChangesEqual(
  167. [TreeChange.delete(('a', F, blob_a.id)),
  168. TreeChange.add(('a/x', F, blob_x.id))],
  169. tree1, tree2)
  170. def test_tree_changes_complex(self):
  171. blob_a_1 = make_object(Blob, data='a1_1')
  172. blob_bx1_1 = make_object(Blob, data='bx1_1')
  173. blob_bx2_1 = make_object(Blob, data='bx2_1')
  174. blob_by1_1 = make_object(Blob, data='by1_1')
  175. blob_by2_1 = make_object(Blob, data='by2_1')
  176. tree1 = self.commit_tree([
  177. ('a', blob_a_1),
  178. ('b/x/1', blob_bx1_1),
  179. ('b/x/2', blob_bx2_1),
  180. ('b/y/1', blob_by1_1),
  181. ('b/y/2', blob_by2_1),
  182. ])
  183. blob_a_2 = make_object(Blob, data='a1_2')
  184. blob_bx1_2 = blob_bx1_1
  185. blob_by_2 = make_object(Blob, data='by_2')
  186. blob_c_2 = make_object(Blob, data='c_2')
  187. tree2 = self.commit_tree([
  188. ('a', blob_a_2),
  189. ('b/x/1', blob_bx1_2),
  190. ('b/y', blob_by_2),
  191. ('c', blob_c_2),
  192. ])
  193. self.assertChangesEqual(
  194. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a_1.id),
  195. ('a', F, blob_a_2.id)),
  196. TreeChange.delete(('b/x/2', F, blob_bx2_1.id)),
  197. TreeChange.add(('b/y', F, blob_by_2.id)),
  198. TreeChange.delete(('b/y/1', F, blob_by1_1.id)),
  199. TreeChange.delete(('b/y/2', F, blob_by2_1.id)),
  200. TreeChange.add(('c', F, blob_c_2.id))],
  201. tree1, tree2)
  202. def test_tree_changes_name_order(self):
  203. blob = make_object(Blob, data='a')
  204. tree1 = self.commit_tree([('a', blob), ('a.', blob), ('a..', blob)])
  205. # Tree order is the reverse of this, so if we used tree order, 'a..'
  206. # would not be merged.
  207. tree2 = self.commit_tree([('a/x', blob), ('a./x', blob), ('a..', blob)])
  208. self.assertChangesEqual(
  209. [TreeChange.delete(('a', F, blob.id)),
  210. TreeChange.add(('a/x', F, blob.id)),
  211. TreeChange.delete(('a.', F, blob.id)),
  212. TreeChange.add(('a./x', F, blob.id))],
  213. tree1, tree2)
  214. def test_tree_changes_prune(self):
  215. blob_a1 = make_object(Blob, data='a1')
  216. blob_a2 = make_object(Blob, data='a2')
  217. blob_x = make_object(Blob, data='x')
  218. tree1 = self.commit_tree([('a', blob_a1), ('b/x', blob_x)])
  219. tree2 = self.commit_tree([('a', blob_a2), ('b/x', blob_x)])
  220. # Remove identical items so lookups will fail unless we prune.
  221. subtree = self.store[tree1['b'][1]]
  222. for entry in subtree.iteritems():
  223. del self.store[entry.sha]
  224. del self.store[subtree.id]
  225. self.assertChangesEqual(
  226. [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  227. ('a', F, blob_a2.id))],
  228. tree1, tree2)
  229. class RenameDetectionTest(DiffTestCase):
  230. def test_count_blocks(self):
  231. blob = make_object(Blob, data='a\nb\na\n')
  232. self.assertEqual({'a\n': 2, 'b\n': 1}, _count_blocks(blob))
  233. def test_count_blocks_no_newline(self):
  234. blob = make_object(Blob, data='a\na')
  235. self.assertEqual({'a\n': 1, 'a': 1}, _count_blocks(blob))
  236. def test_count_blocks_chunks(self):
  237. blob = ShaFile.from_raw_chunks(Blob.type_num, ['a\nb', '\na\n'])
  238. self.assertEqual({'a\n': 2, 'b\n': 1}, _count_blocks(blob))
  239. def test_count_blocks_long_lines(self):
  240. a = 'a' * 64
  241. data = a + 'xxx\ny\n' + a + 'zzz\n'
  242. blob = make_object(Blob, data=data)
  243. self.assertEqual({'a' * 64: 2, 'xxx\n': 1, 'y\n': 1, 'zzz\n': 1},
  244. _count_blocks(blob))
  245. def assertSimilar(self, expected_score, blob1, blob2):
  246. self.assertEqual(expected_score, _similarity_score(blob1, blob2))
  247. self.assertEqual(expected_score, _similarity_score(blob2, blob1))
  248. def test_similarity_score(self):
  249. blob0 = make_object(Blob, data='')
  250. blob1 = make_object(Blob, data='ab\ncd\ncd\n')
  251. blob2 = make_object(Blob, data='ab\n')
  252. blob3 = make_object(Blob, data='cd\n')
  253. blob4 = make_object(Blob, data='cd\ncd\n')
  254. self.assertSimilar(100, blob0, blob0)
  255. self.assertSimilar(0, blob0, blob1)
  256. self.assertSimilar(33, blob1, blob2)
  257. self.assertSimilar(33, blob1, blob3)
  258. self.assertSimilar(66, blob1, blob4)
  259. self.assertSimilar(0, blob2, blob3)
  260. self.assertSimilar(50, blob3, blob4)
  261. def test_similarity_score_cache(self):
  262. blob1 = make_object(Blob, data='ab\ncd\n')
  263. blob2 = make_object(Blob, data='ab\n')
  264. block_cache = {}
  265. self.assertEqual(
  266. 50, _similarity_score(blob1, blob2, block_cache=block_cache))
  267. self.assertEqual(set([blob1.id, blob2.id]), set(block_cache))
  268. def fail_chunks():
  269. self.fail('Unexpected call to as_raw_chunks()')
  270. blob1.as_raw_chunks = blob2.as_raw_chunks = fail_chunks
  271. blob1.raw_length = lambda: 6
  272. blob2.raw_length = lambda: 3
  273. self.assertEqual(
  274. 50, _similarity_score(blob1, blob2, block_cache=block_cache))
  275. def test_tree_entry_sort(self):
  276. sha = 'abcd' * 10
  277. expected_entries = [
  278. TreeChange.add(TreeEntry('aaa', F, sha)),
  279. TreeChange(CHANGE_COPY, TreeEntry('bbb', F, sha),
  280. TreeEntry('aab', F, sha)),
  281. TreeChange(CHANGE_MODIFY, TreeEntry('bbb', F, sha),
  282. TreeEntry('bbb', F, 'dabc' * 10)),
  283. TreeChange(CHANGE_RENAME, TreeEntry('bbc', F, sha),
  284. TreeEntry('ddd', F, sha)),
  285. TreeChange.delete(TreeEntry('ccc', F, sha)),
  286. ]
  287. for perm in permutations(expected_entries):
  288. self.assertEqual(expected_entries,
  289. sorted(perm, key=_tree_change_key))
  290. def detect_renames(self, tree1, tree2, **kwargs):
  291. detector = RenameDetector(self.store, tree1.id, tree2.id, **kwargs)
  292. return detector.changes_with_renames()
  293. def test_exact_rename_one_to_one(self):
  294. blob1 = make_object(Blob, data='1')
  295. blob2 = make_object(Blob, data='2')
  296. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  297. tree2 = self.commit_tree([('c', blob1), ('d', blob2)])
  298. self.assertEqual(
  299. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob1.id)),
  300. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('d', F, blob2.id))],
  301. self.detect_renames(tree1, tree2))
  302. def test_exact_rename_split_different_type(self):
  303. blob = make_object(Blob, data='/foo')
  304. tree1 = self.commit_tree([('a', blob, 0100644)])
  305. tree2 = self.commit_tree([('a', blob, 0120000)])
  306. self.assertEqual(
  307. [TreeChange.add(('a', 0120000, blob.id)),
  308. TreeChange.delete(('a', 0100644, blob.id))],
  309. self.detect_renames(tree1, tree2))
  310. def test_exact_rename_and_different_type(self):
  311. blob1 = make_object(Blob, data='1')
  312. blob2 = make_object(Blob, data='2')
  313. tree1 = self.commit_tree([('a', blob1)])
  314. tree2 = self.commit_tree([('a', blob2, 0120000), ('b', blob1)])
  315. self.assertEqual(
  316. [TreeChange.add(('a', 0120000, blob2.id)),
  317. TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob1.id))],
  318. self.detect_renames(tree1, tree2))
  319. def test_exact_rename_one_to_many(self):
  320. blob = make_object(Blob, data='1')
  321. tree1 = self.commit_tree([('a', blob)])
  322. tree2 = self.commit_tree([('b', blob), ('c', blob)])
  323. self.assertEqual(
  324. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('b', F, blob.id)),
  325. TreeChange(CHANGE_COPY, ('a', F, blob.id), ('c', F, blob.id))],
  326. self.detect_renames(tree1, tree2))
  327. def test_exact_rename_many_to_one(self):
  328. blob = make_object(Blob, data='1')
  329. tree1 = self.commit_tree([('a', blob), ('b', blob)])
  330. tree2 = self.commit_tree([('c', blob)])
  331. self.assertEqual(
  332. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('c', F, blob.id)),
  333. TreeChange.delete(('b', F, blob.id))],
  334. self.detect_renames(tree1, tree2))
  335. def test_exact_rename_many_to_many(self):
  336. blob = make_object(Blob, data='1')
  337. tree1 = self.commit_tree([('a', blob), ('b', blob)])
  338. tree2 = self.commit_tree([('c', blob), ('d', blob), ('e', blob)])
  339. self.assertEqual(
  340. [TreeChange(CHANGE_RENAME, ('a', F, blob.id), ('c', F, blob.id)),
  341. TreeChange(CHANGE_COPY, ('a', F, blob.id), ('e', F, blob.id)),
  342. TreeChange(CHANGE_RENAME, ('b', F, blob.id), ('d', F, blob.id))],
  343. self.detect_renames(tree1, tree2))
  344. def test_content_rename_threshold(self):
  345. blob1 = make_object(Blob, data='a\nb\nc\n')
  346. blob2 = make_object(Blob, data='a\nb\nd\n')
  347. tree1 = self.commit_tree([('a', blob1)])
  348. tree2 = self.commit_tree([('b', blob2)])
  349. self.assertEqual(
  350. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('b', F, blob2.id))],
  351. self.detect_renames(tree1, tree2, rename_threshold=50))
  352. self.assertEqual(
  353. [TreeChange.delete(('a', F, blob1.id)),
  354. TreeChange.add(('b', F, blob2.id))],
  355. self.detect_renames(tree1, tree2, rename_threshold=75))
  356. def test_content_rename_max_files(self):
  357. blob1 = make_object(Blob, data='a\nb\nc\nd')
  358. blob4 = make_object(Blob, data='a\nb\nc\ne\n')
  359. blob2 = make_object(Blob, data='e\nf\ng\nh\n')
  360. blob3 = make_object(Blob, data='e\nf\ng\ni\n')
  361. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  362. tree2 = self.commit_tree([('c', blob3), ('d', blob4)])
  363. self.assertEqual(
  364. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('d', F, blob4.id)),
  365. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('c', F, blob3.id))],
  366. self.detect_renames(tree1, tree2))
  367. self.assertEqual(
  368. [TreeChange.delete(('a', F, blob1.id)),
  369. TreeChange.delete(('b', F, blob2.id)),
  370. TreeChange.add(('c', F, blob3.id)),
  371. TreeChange.add(('d', F, blob4.id))],
  372. self.detect_renames(tree1, tree2, max_files=1))
  373. def test_content_rename_one_to_one(self):
  374. b11 = make_object(Blob, data='a\nb\nc\nd\n')
  375. b12 = make_object(Blob, data='a\nb\nc\ne\n')
  376. b21 = make_object(Blob, data='e\nf\ng\n\h')
  377. b22 = make_object(Blob, data='e\nf\ng\n\i')
  378. tree1 = self.commit_tree([('a', b11), ('b', b21)])
  379. tree2 = self.commit_tree([('c', b12), ('d', b22)])
  380. self.assertEqual(
  381. [TreeChange(CHANGE_RENAME, ('a', F, b11.id), ('c', F, b12.id)),
  382. TreeChange(CHANGE_RENAME, ('b', F, b21.id), ('d', F, b22.id))],
  383. self.detect_renames(tree1, tree2))
  384. def test_content_rename_one_to_one_ordering(self):
  385. blob1 = make_object(Blob, data='a\nb\nc\nd\ne\nf\n')
  386. blob2 = make_object(Blob, data='a\nb\nc\nd\ng\nh\n')
  387. # 6/10 match to blob1, 8/10 match to blob2
  388. blob3 = make_object(Blob, data='a\nb\nc\nd\ng\ni\n')
  389. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  390. tree2 = self.commit_tree([('c', blob3)])
  391. self.assertEqual(
  392. [TreeChange.delete(('a', F, blob1.id)),
  393. TreeChange(CHANGE_RENAME, ('b', F, blob2.id), ('c', F, blob3.id))],
  394. self.detect_renames(tree1, tree2))
  395. tree3 = self.commit_tree([('a', blob2), ('b', blob1)])
  396. tree4 = self.commit_tree([('c', blob3)])
  397. self.assertEqual(
  398. [TreeChange(CHANGE_RENAME, ('a', F, blob2.id), ('c', F, blob3.id)),
  399. TreeChange.delete(('b', F, blob1.id))],
  400. self.detect_renames(tree3, tree4))
  401. def test_content_rename_one_to_many(self):
  402. blob1 = make_object(Blob, data='aa\nb\nc\nd\ne\n')
  403. blob2 = make_object(Blob, data='ab\nb\nc\nd\ne\n') # 8/11 match
  404. blob3 = make_object(Blob, data='aa\nb\nc\nd\nf\n') # 9/11 match
  405. tree1 = self.commit_tree([('a', blob1)])
  406. tree2 = self.commit_tree([('b', blob2), ('c', blob3)])
  407. self.assertEqual(
  408. [TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('b', F, blob2.id)),
  409. TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id))],
  410. self.detect_renames(tree1, tree2))
  411. def test_content_rename_many_to_one(self):
  412. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  413. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  414. blob3 = make_object(Blob, data='a\nb\nc\nf\n')
  415. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  416. tree2 = self.commit_tree([('c', blob3)])
  417. self.assertEqual(
  418. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id)),
  419. TreeChange.delete(('b', F, blob2.id))],
  420. self.detect_renames(tree1, tree2))
  421. def test_content_rename_many_to_many(self):
  422. blob1 = make_object(Blob, data='a\nb\nc\nd\n')
  423. blob2 = make_object(Blob, data='a\nb\nc\ne\n')
  424. blob3 = make_object(Blob, data='a\nb\nc\nf\n')
  425. blob4 = make_object(Blob, data='a\nb\nc\ng\n')
  426. tree1 = self.commit_tree([('a', blob1), ('b', blob2)])
  427. tree2 = self.commit_tree([('c', blob3), ('d', blob4)])
  428. # TODO(dborowitz): Distribute renames rather than greedily choosing
  429. # copies.
  430. self.assertEqual(
  431. [TreeChange(CHANGE_RENAME, ('a', F, blob1.id), ('c', F, blob3.id)),
  432. TreeChange(CHANGE_COPY, ('a', F, blob1.id), ('d', F, blob4.id)),
  433. TreeChange.delete(('b', F, blob2.id))],
  434. self.detect_renames(tree1, tree2))
  435. def test_content_rename_gitlink(self):
  436. blob1 = make_object(Blob, data='blob1')
  437. blob2 = make_object(Blob, data='blob2')
  438. link1 = '1' * 40
  439. link2 = '2' * 40
  440. tree1 = self.commit_tree([('a', blob1), ('b', link1, 0160000)])
  441. tree2 = self.commit_tree([('c', blob2), ('d', link2, 0160000)])
  442. self.assertEqual(
  443. [TreeChange.delete(('a', 0100644, blob1.id)),
  444. TreeChange.delete(('b', 0160000, link1)),
  445. TreeChange.add(('c', 0100644, blob2.id)),
  446. TreeChange.add(('d', 0160000, link2))],
  447. self.detect_renames(tree1, tree2))