git_rebase_update_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #!/usr/bin/env vpython3
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for git_rebase_update.py"""
  6. import os
  7. import sys
  8. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  9. sys.path.insert(0, DEPOT_TOOLS_ROOT)
  10. from testing_support import coverage_utils
  11. from testing_support import git_test_utils
  12. # TODO: Should fix these warnings.
  13. # pylint: disable=line-too-long
  14. class GitRebaseUpdateTest(git_test_utils.GitRepoReadWriteTestBase):
  15. REPO_SCHEMA = """
  16. A B C D E F G
  17. B H I J K
  18. J L
  19. """
  20. @classmethod
  21. def getRepoContent(cls, commit):
  22. # Every commit X gets a file X with the content X
  23. return {commit: {'data': commit.encode('utf-8')}}
  24. @classmethod
  25. def setUpClass(cls):
  26. super(GitRebaseUpdateTest, cls).setUpClass()
  27. import git_rebase_update, git_new_branch, git_reparent_branch, git_common
  28. import git_rename_branch
  29. cls.reup = git_rebase_update
  30. cls.rp = git_reparent_branch
  31. cls.nb = git_new_branch
  32. cls.mv = git_rename_branch
  33. cls.gc = git_common
  34. cls.gc.TEST_MODE = True
  35. def setUp(self):
  36. super(GitRebaseUpdateTest, self).setUp()
  37. # Include branch_K, branch_L to make sure that ABCDEFG all get the
  38. # same commit hashes as self.repo. Otherwise they get committed with the
  39. # wrong timestamps, due to commit ordering.
  40. # TODO(iannucci): Make commit timestamps deterministic in left to right,
  41. # top to bottom order, not in lexi-topographical order.
  42. origin_schema = git_test_utils.GitRepoSchema(
  43. """
  44. A B C D E F G M N O
  45. B H I J K
  46. J L
  47. """, self.getRepoContent)
  48. self.origin = origin_schema.reify()
  49. self.origin.git('checkout', 'main')
  50. self.origin.git('branch', '-d', *['branch_' + l for l in 'KLG'])
  51. self.repo.git('remote', 'add', 'origin', self.origin.repo_path)
  52. self.repo.git('config', '--add', 'remote.origin.fetch',
  53. '+refs/tags/*:refs/tags/*')
  54. self.repo.git('update-ref', 'refs/remotes/origin/main', 'tag_E')
  55. self.repo.git('branch', '--set-upstream-to', 'branch_G', 'branch_K')
  56. self.repo.git('branch', '--set-upstream-to', 'branch_K', 'branch_L')
  57. self.repo.git('branch', '--set-upstream-to', 'origin/main', 'branch_G')
  58. self.repo.to_schema_refs += ['origin/main']
  59. def tearDown(self):
  60. self.origin.nuke()
  61. super(GitRebaseUpdateTest, self).tearDown()
  62. def testRebaseUpdate(self):
  63. self.repo.git('checkout', 'branch_K')
  64. self.repo.run(self.nb.main, ['foobar'])
  65. self.assertEqual(
  66. self.repo.git('rev-parse', 'HEAD').stdout,
  67. self.repo.git('rev-parse', 'origin/main').stdout)
  68. with self.repo.open('foobar', 'w') as f:
  69. f.write('this is the foobar file')
  70. self.repo.git('add', 'foobar')
  71. self.repo.git_commit('foobar1')
  72. with self.repo.open('foobar', 'w') as f:
  73. f.write('totes the Foobar file')
  74. self.repo.git_commit('foobar2')
  75. self.repo.run(self.nb.main, ['--upstream-current', 'int1_foobar'])
  76. self.repo.run(self.nb.main, ['--upstream-current', 'int2_foobar'])
  77. self.repo.run(self.nb.main, ['--upstream-current', 'sub_foobar'])
  78. with self.repo.open('foobar', 'w') as f:
  79. f.write('some more foobaring')
  80. self.repo.git('add', 'foobar')
  81. self.repo.git_commit('foobar3')
  82. self.repo.git('checkout', 'branch_K')
  83. self.repo.run(self.nb.main, ['--upstream-current', 'sub_K'])
  84. with self.repo.open('K', 'w') as f:
  85. f.write('This depends on K')
  86. self.repo.git_commit('sub_K')
  87. self.repo.run(self.nb.main, ['old_branch'])
  88. self.repo.git('reset', '--hard', self.repo['A'])
  89. with self.repo.open('old_file', 'w') as f:
  90. f.write('old_files we want to keep around')
  91. self.repo.git('add', 'old_file')
  92. self.repo.git_commit('old_file')
  93. self.repo.git('config', 'branch.old_branch.dormant', 'true')
  94. self.repo.git('checkout', 'origin/main')
  95. self.assertSchema("""
  96. A B H I J K sub_K
  97. J L
  98. B C D E foobar1 foobar2 foobar3
  99. E F G
  100. A old_file
  101. """)
  102. self.assertEqual(self.repo['A'], self.origin['A'])
  103. self.assertEqual(self.repo['E'], self.origin['E'])
  104. with self.repo.open('bob', 'wb') as f:
  105. f.write(b'testing auto-freeze/thaw')
  106. output, _ = self.repo.capture_stdio(self.reup.main)
  107. self.assertIn('Cannot rebase-update', output)
  108. self.repo.run(self.nb.main, ['empty_branch'])
  109. self.repo.run(self.nb.main, ['--upstream-current', 'empty_branch2'])
  110. self.repo.git('checkout', 'branch_K')
  111. output, _ = self.repo.capture_stdio(self.reup.main)
  112. self.assertIn('Rebasing: branch_G', output)
  113. self.assertIn('Rebasing: branch_K', output)
  114. self.assertIn('Rebasing: branch_L', output)
  115. self.assertIn('Rebasing: foobar', output)
  116. self.assertIn('Rebasing: sub_K', output)
  117. self.assertIn('Deleted branch branch_G', output)
  118. self.assertIn('Deleted branch empty_branch', output)
  119. self.assertIn('Deleted branch empty_branch2', output)
  120. self.assertIn('Deleted branch int1_foobar', output)
  121. self.assertIn('Deleted branch int2_foobar', output)
  122. self.assertIn('Reparented branch_K to track origin/main', output)
  123. self.assertIn('Reparented sub_foobar to track foobar', output)
  124. self.assertSchema("""
  125. A B C D E F G M N O H I J K sub_K
  126. K L
  127. O foobar1 foobar2 foobar3
  128. A old_file
  129. """)
  130. output, _ = self.repo.capture_stdio(self.reup.main)
  131. self.assertIn('branch_K up-to-date', output)
  132. self.assertIn('branch_L up-to-date', output)
  133. self.assertIn('foobar up-to-date', output)
  134. self.assertIn('sub_K up-to-date', output)
  135. with self.repo.open('bob') as f:
  136. self.assertEqual(b'testing auto-freeze/thaw', f.read())
  137. self.assertEqual(
  138. self.repo.git('status', '--porcelain').stdout, '?? bob\n')
  139. self.repo.git('checkout', 'origin/main')
  140. _, err = self.repo.capture_stdio(self.rp.main, [])
  141. self.assertIn('Must specify new parent somehow', err)
  142. _, err = self.repo.capture_stdio(self.rp.main, ['foobar'])
  143. self.assertIn('Must be on the branch', err)
  144. self.repo.git('checkout', 'branch_K')
  145. _, err = self.repo.capture_stdio(self.rp.main, ['origin/main'])
  146. self.assertIn('Cannot reparent a branch to its existing parent', err)
  147. output, _ = self.repo.capture_stdio(self.rp.main, ['foobar'])
  148. self.assertIn('Rebasing: branch_K', output)
  149. self.assertIn('Rebasing: sub_K', output)
  150. self.assertIn('Rebasing: branch_L', output)
  151. self.assertSchema("""
  152. A B C D E F G M N O foobar1 foobar2 H I J K L
  153. foobar2 foobar3
  154. K sub_K
  155. A old_file
  156. """)
  157. self.repo.git('checkout', 'sub_K')
  158. output, _ = self.repo.capture_stdio(self.rp.main, ['foobar'])
  159. self.assertIn('Squashing failed', output)
  160. self.assertTrue(self.repo.run(self.gc.in_rebase))
  161. self.repo.git('rebase', '--abort')
  162. self.assertIsNone(self.repo.run(self.gc.thaw))
  163. self.assertSchema("""
  164. A B C D E F G M N O foobar1 foobar2 H I J K L
  165. foobar2 foobar3
  166. A old_file
  167. K sub_K
  168. """)
  169. self.assertEqual(
  170. self.repo.git('status', '--porcelain').stdout, '?? bob\n')
  171. branches = self.repo.run(set, self.gc.branches())
  172. self.assertEqual(
  173. branches, {
  174. 'branch_K', 'main', 'sub_K', 'root_A', 'branch_L', 'old_branch',
  175. 'foobar', 'sub_foobar'
  176. })
  177. self.repo.git('checkout', 'branch_K')
  178. self.repo.run(self.mv.main, ['special_K'])
  179. branches = self.repo.run(set, self.gc.branches())
  180. self.assertEqual(
  181. branches, {
  182. 'special_K', 'main', 'sub_K', 'root_A', 'branch_L',
  183. 'old_branch', 'foobar', 'sub_foobar'
  184. })
  185. self.repo.git('checkout', 'origin/main')
  186. _, err = self.repo.capture_stdio(self.mv.main,
  187. ['special_K', 'cool branch'])
  188. self.assertIn('fatal: \'cool branch\' is not a valid branch name', err)
  189. self.repo.run(self.mv.main, ['special_K', 'cool_branch'])
  190. branches = self.repo.run(set, self.gc.branches())
  191. # This check fails with git 2.4 (see crbug.com/487172)
  192. self.assertEqual(
  193. branches, {
  194. 'cool_branch', 'main', 'sub_K', 'root_A', 'branch_L',
  195. 'old_branch', 'foobar', 'sub_foobar'
  196. })
  197. _, branch_tree = self.repo.run(self.gc.get_branch_tree)
  198. self.assertEqual(branch_tree['sub_K'], 'foobar')
  199. def testRebaseConflicts(self):
  200. # Pretend that branch_L landed
  201. self.origin.git('checkout', 'main')
  202. with self.origin.open('L', 'w') as f:
  203. f.write('L')
  204. self.origin.git('add', 'L')
  205. self.origin.git_commit('L')
  206. # Add a commit to branch_K so that things fail
  207. self.repo.git('checkout', 'branch_K')
  208. with self.repo.open('M', 'w') as f:
  209. f.write('NOPE')
  210. self.repo.git('add', 'M')
  211. self.repo.git_commit('K NOPE')
  212. # Add a commits to branch_L which will work when squashed
  213. self.repo.git('checkout', 'branch_L')
  214. self.repo.git('reset', 'branch_L~')
  215. with self.repo.open('L', 'w') as f:
  216. f.write('NOPE')
  217. self.repo.git('add', 'L')
  218. self.repo.git_commit('L NOPE')
  219. with self.repo.open('L', 'w') as f:
  220. f.write('L')
  221. self.repo.git('add', 'L')
  222. self.repo.git_commit('L YUP')
  223. # start on a branch which will be deleted
  224. self.repo.git('checkout', 'branch_G')
  225. output, _ = self.repo.capture_stdio(self.reup.main)
  226. self.assertIn('branch.branch_K.dormant true', output)
  227. output, _ = self.repo.capture_stdio(self.reup.main)
  228. self.assertIn('Rebase in progress', output)
  229. self.repo.git('checkout', '--theirs', 'M')
  230. self.repo.git('rebase', '--skip')
  231. output, _ = self.repo.capture_stdio(self.reup.main)
  232. self.assertIn('Failed! Attempting to squash', output)
  233. self.assertIn('Deleted branch branch_G', output)
  234. self.assertIn('Deleted branch branch_L', output)
  235. self.assertIn('\'branch_G\' was merged', output)
  236. self.assertIn('checking out \'origin/main\'', output)
  237. def testRebaseConflictsKeepGoing(self):
  238. # Pretend that branch_L landed
  239. self.origin.git('checkout', 'main')
  240. with self.origin.open('L', 'w') as f:
  241. f.write('L')
  242. self.origin.git('add', 'L')
  243. self.origin.git_commit('L')
  244. # Add a commit to branch_K so that things fail
  245. self.repo.git('checkout', 'branch_K')
  246. with self.repo.open('M', 'w') as f:
  247. f.write('NOPE')
  248. self.repo.git('add', 'M')
  249. self.repo.git_commit('K NOPE')
  250. # Add a commits to branch_L which will work when squashed
  251. self.repo.git('checkout', 'branch_L')
  252. self.repo.git('reset', 'branch_L~')
  253. with self.repo.open('L', 'w') as f:
  254. f.write('NOPE')
  255. self.repo.git('add', 'L')
  256. self.repo.git_commit('L NOPE')
  257. with self.repo.open('L', 'w') as f:
  258. f.write('L')
  259. self.repo.git('add', 'L')
  260. self.repo.git_commit('L YUP')
  261. # start on a branch which will be deleted
  262. self.repo.git('checkout', 'branch_G')
  263. self.repo.git('config', 'branch.branch_K.dormant', 'false')
  264. output, _ = self.repo.capture_stdio(self.reup.main, ['-k'])
  265. self.assertIn('--keep-going set, continuing with next branch.', output)
  266. self.assertIn('could not be cleanly rebased:', output)
  267. self.assertIn(' branch_K', output)
  268. def testTrackTag(self):
  269. self.origin.git('tag', 'tag-to-track', self.origin['M'])
  270. self.repo.git('tag', 'tag-to-track', self.repo['D'])
  271. self.repo.git('config', 'branch.branch_G.remote', '.')
  272. self.repo.git('config', 'branch.branch_G.merge',
  273. 'refs/tags/tag-to-track')
  274. self.assertIn(
  275. 'fatal: \'foo bar\' is not a valid branch name',
  276. self.repo.capture_stdio(
  277. self.nb.main,
  278. ['--upstream', 'tags/tag-to-track', 'foo bar'])[1])
  279. self.repo.run(self.nb.main,
  280. ['--upstream', 'tags/tag-to-track', 'foobar'])
  281. with self.repo.open('foobar', 'w') as f:
  282. f.write('this is the foobar file')
  283. self.repo.git('add', 'foobar')
  284. self.repo.git_commit('foobar1')
  285. with self.repo.open('foobar', 'w') as f:
  286. f.write('totes the Foobar file')
  287. self.repo.git_commit('foobar2')
  288. self.assertSchema("""
  289. A B H I J K
  290. J L
  291. B C D E F G
  292. D foobar1 foobar2
  293. """)
  294. self.assertEqual(self.repo['A'], self.origin['A'])
  295. self.assertEqual(self.repo['G'], self.origin['G'])
  296. output, _ = self.repo.capture_stdio(self.reup.main)
  297. self.assertIn('Rebasing: branch_G', output)
  298. self.assertIn('Rebasing: branch_K', output)
  299. self.assertIn('Rebasing: branch_L', output)
  300. self.assertIn('Rebasing: foobar', output)
  301. self.assertEqual(
  302. self.repo.git('rev-parse', 'tags/tag-to-track').stdout.strip(),
  303. self.origin['M'])
  304. self.assertSchema("""
  305. A B C D E F G M N O
  306. M H I J K L
  307. M foobar1 foobar2
  308. """)
  309. _, err = self.repo.capture_stdio(self.rp.main, ['tag F'])
  310. self.assertIn('fatal: invalid reference', err)
  311. output, _ = self.repo.capture_stdio(self.rp.main, ['tag_F'])
  312. self.assertIn('to track tag_F [tag] (was tag-to-track [tag])', output)
  313. self.assertSchema("""
  314. A B C D E F G M N O
  315. M H I J K L
  316. F foobar1 foobar2
  317. """)
  318. output, _ = self.repo.capture_stdio(self.rp.main, ['tag-to-track'])
  319. self.assertIn('to track tag-to-track [tag] (was tag_F [tag])', output)
  320. self.assertSchema("""
  321. A B C D E F G M N O
  322. M H I J K L
  323. M foobar1 foobar2
  324. """)
  325. output, _ = self.repo.capture_stdio(self.rp.main, ['--root'])
  326. self.assertIn('to track origin/main (was tag-to-track [tag])', output)
  327. self.assertSchema("""
  328. A B C D E F G M N O foobar1 foobar2
  329. M H I J K L
  330. """)
  331. def testReparentBranchWithoutUpstream(self):
  332. self.repo.git('branch', 'nerp')
  333. self.repo.git('checkout', 'nerp')
  334. _, err = self.repo.capture_stdio(self.rp.main, ['branch_K'])
  335. self.assertIn('Unable to determine nerp@{upstream}', err)
  336. if __name__ == '__main__':
  337. sys.exit(
  338. coverage_utils.covered_main(
  339. (os.path.join(DEPOT_TOOLS_ROOT, 'git_rebase_update.py'),
  340. os.path.join(DEPOT_TOOLS_ROOT, 'git_new_branch.py'),
  341. os.path.join(DEPOT_TOOLS_ROOT, 'git_reparent_branch.py'),
  342. os.path.join(DEPOT_TOOLS_ROOT, 'git_rename_branch.py'))))