git_rebase_update_test.py 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for git_rebase_update.py"""

import os
import sys

DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS_ROOT)

from testing_support import coverage_utils
from testing_support import git_test_utils

class GitRebaseUpdateTest(git_test_utils.GitRepoReadWriteTestBase):
  REPO_SCHEMA = """
  A B C D E F G
    B H I J K
          J L
  """

  @classmethod
  def getRepoContent(cls, commit):
    # Every commit X gets a file X with the content X
    return {commit: {'data': commit}}

  @classmethod
  def setUpClass(cls):
    super(GitRebaseUpdateTest, cls).setUpClass()
    import git_rebase_update, git_new_branch, git_reparent_branch, git_common
    import git_rename_branch
    cls.reup = git_rebase_update
    cls.rp = git_reparent_branch
    cls.nb = git_new_branch
    cls.mv = git_rename_branch
    cls.gc = git_common
    cls.gc.TEST_MODE = True

  def setUp(self):
    super(GitRebaseUpdateTest, self).setUp()
    # Include branch_K, branch_L to make sure that ABCDEFG all get the
    # same commit hashes as self.repo. Otherwise they get committed with the
    # wrong timestamps, due to commit ordering.
    # TODO(iannucci): Make commit timestamps deterministic in left to right, top
    #                 to bottom order, not in lexi-topographical order.
    origin_schema = git_test_utils.GitRepoSchema("""
    A B C D E F G M N O
      B H I J K
            J L
    """, self.getRepoContent)
    self.origin = origin_schema.reify()
    self.origin.git('checkout', 'master')
    self.origin.git('branch', '-d', *['branch_'+l for l in 'KLG'])

    self.repo.git('remote', 'add', 'origin', self.origin.repo_path)
    self.repo.git('config', '--add', 'remote.origin.fetch',
                  '+refs/tags/*:refs/tags/*')
    self.repo.git('update-ref', 'refs/remotes/origin/master', 'tag_E')
    self.repo.git('branch', '--set-upstream-to', 'branch_G', 'branch_K')
    self.repo.git('branch', '--set-upstream-to', 'branch_K', 'branch_L')
    self.repo.git('branch', '--set-upstream-to', 'origin/master', 'branch_G')

    self.repo.to_schema_refs += ['origin/master']

  def tearDown(self):
    self.origin.nuke()
    super(GitRebaseUpdateTest, self).tearDown()

  def testRebaseUpdate(self):
    self.repo.git('checkout', 'branch_K')

    self.repo.run(self.nb.main, ['foobar'])
    self.assertEqual(self.repo.git('rev-parse', 'HEAD').stdout,
                     self.repo.git('rev-parse', 'origin/master').stdout)

    with self.repo.open('foobar', 'w') as f:
      f.write('this is the foobar file')
    self.repo.git('add', 'foobar')
    self.repo.git_commit('foobar1')

    with self.repo.open('foobar', 'w') as f:
      f.write('totes the Foobar file')
    self.repo.git_commit('foobar2')

87 88 89 90 91 92 93 94
    self.repo.run(self.nb.main, ['--upstream-current', 'int1_foobar'])
    self.repo.run(self.nb.main, ['--upstream-current', 'int2_foobar'])
    self.repo.run(self.nb.main, ['--upstream-current', 'sub_foobar'])
    with self.repo.open('foobar', 'w') as f:
        f.write('some more foobaring')
    self.repo.git('add', 'foobar')
    self.repo.git_commit('foobar3')

95
    self.repo.git('checkout', 'branch_K')
96
    self.repo.run(self.nb.main, ['--upstream-current', 'sub_K'])
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    with self.repo.open('K', 'w') as f:
      f.write('This depends on K')
    self.repo.git_commit('sub_K')

    self.repo.run(self.nb.main, ['old_branch'])
    self.repo.git('reset', '--hard', self.repo['A'])
    with self.repo.open('old_file', 'w') as f:
      f.write('old_files we want to keep around')
    self.repo.git('add', 'old_file')
    self.repo.git_commit('old_file')
    self.repo.git('config', 'branch.old_branch.dormant', 'true')

    self.repo.git('checkout', 'origin/master')

    self.assertSchema("""
    A B H I J K sub_K
            J L
114
      B C D E foobar1 foobar2 foobar3
115 116 117 118 119 120 121 122 123 124 125 126
            E F G
    A old_file
    """)
    self.assertEquals(self.repo['A'], self.origin['A'])
    self.assertEquals(self.repo['E'], self.origin['E'])

    with self.repo.open('bob', 'wb') as f:
      f.write('testing auto-freeze/thaw')

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('Cannot rebase-update', output)

127
    self.repo.run(self.nb.main, ['empty_branch'])
128
    self.repo.run(self.nb.main, ['--upstream-current', 'empty_branch2'])
129

130 131 132 133 134 135 136 137 138 139
    self.repo.git('checkout', 'branch_K')

    output, _ = self.repo.capture_stdio(self.reup.main)

    self.assertIn('Rebasing: branch_G', output)
    self.assertIn('Rebasing: branch_K', output)
    self.assertIn('Rebasing: branch_L', output)
    self.assertIn('Rebasing: foobar', output)
    self.assertIn('Rebasing: sub_K', output)
    self.assertIn('Deleted branch branch_G', output)
140 141
    self.assertIn('Deleted branch empty_branch', output)
    self.assertIn('Deleted branch empty_branch2', output)
142 143
    self.assertIn('Deleted branch int1_foobar', output)
    self.assertIn('Deleted branch int2_foobar', output)
144
    self.assertIn('Reparented branch_K to track origin/master', output)
145
    self.assertIn('Reparented sub_foobar to track foobar', output)
146 147 148 149

    self.assertSchema("""
    A B C D E F G M N O H I J K sub_K
                              K L
150
                      O foobar1 foobar2 foobar3
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    A old_file
    """)

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('branch_K up-to-date', output)
    self.assertIn('branch_L up-to-date', output)
    self.assertIn('foobar up-to-date', output)
    self.assertIn('sub_K up-to-date', output)

    with self.repo.open('bob') as f:
      self.assertEquals('testing auto-freeze/thaw', f.read())

    self.assertEqual(self.repo.git('status', '--porcelain').stdout, '?? bob\n')

    self.repo.git('checkout', 'origin/master')
166 167
    _, err = self.repo.capture_stdio(self.rp.main, [])
    self.assertIn('Must specify new parent somehow', err)
168 169 170 171 172 173 174 175 176 177 178 179 180
    _, err = self.repo.capture_stdio(self.rp.main, ['foobar'])
    self.assertIn('Must be on the branch', err)

    self.repo.git('checkout', 'branch_K')
    _, err = self.repo.capture_stdio(self.rp.main, ['origin/master'])
    self.assertIn('Cannot reparent a branch to its existing parent', err)
    output, _ = self.repo.capture_stdio(self.rp.main, ['foobar'])
    self.assertIn('Rebasing: branch_K', output)
    self.assertIn('Rebasing: sub_K', output)
    self.assertIn('Rebasing: branch_L', output)

    self.assertSchema("""
    A B C D E F G M N O foobar1 foobar2 H I J K L
181
                                foobar2 foobar3
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
                                              K sub_K
    A old_file
    """)

    self.repo.git('checkout', 'sub_K')
    output, _ = self.repo.capture_stdio(self.rp.main, ['foobar'])
    self.assertIn('Squashing failed', output)

    self.assertTrue(self.repo.run(self.gc.in_rebase))

    self.repo.git('rebase', '--abort')
    self.assertIsNone(self.repo.run(self.gc.thaw))

    self.assertSchema("""
    A B C D E F G M N O foobar1 foobar2 H I J K L
197
                                foobar2 foobar3
198 199 200 201 202 203 204 205
    A old_file
                                              K sub_K
    """)

    self.assertEqual(self.repo.git('status', '--porcelain').stdout, '?? bob\n')

    branches = self.repo.run(set, self.gc.branches())
    self.assertEqual(branches, {'branch_K', 'master', 'sub_K', 'root_A',
206 207
                                'branch_L', 'old_branch', 'foobar',
                                'sub_foobar'})
208 209 210 211 212 213

    self.repo.git('checkout', 'branch_K')
    self.repo.run(self.mv.main, ['special_K'])

    branches = self.repo.run(set, self.gc.branches())
    self.assertEqual(branches, {'special_K', 'master', 'sub_K', 'root_A',
214 215
                                'branch_L', 'old_branch', 'foobar',
                                'sub_foobar'})
216 217 218 219 220 221 222

    self.repo.git('checkout', 'origin/master')
    _, err = self.repo.capture_stdio(self.mv.main, ['special_K', 'cool branch'])
    self.assertIn('fatal: \'cool branch\' is not a valid branch name.', err)

    self.repo.run(self.mv.main, ['special_K', 'cool_branch'])
    branches = self.repo.run(set, self.gc.branches())
223
    # This check fails with git 2.4 (see crbug.com/487172)
224
    self.assertEqual(branches, {'cool_branch', 'master', 'sub_K', 'root_A',
225 226
                                'branch_L', 'old_branch', 'foobar',
                                'sub_foobar'})
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    _, branch_tree = self.repo.run(self.gc.get_branch_tree)
    self.assertEqual(branch_tree['sub_K'], 'foobar')


  def testRebaseConflicts(self):
    # Pretend that branch_L landed
    self.origin.git('checkout', 'master')
    with self.origin.open('L', 'w') as f:
      f.write('L')
    self.origin.git('add', 'L')
    self.origin.git_commit('L')

    # Add a commit to branch_K so that things fail
    self.repo.git('checkout', 'branch_K')
    with self.repo.open('M', 'w') as f:
      f.write('NOPE')
    self.repo.git('add', 'M')
    self.repo.git_commit('K NOPE')

    # Add a commits to branch_L which will work when squashed
    self.repo.git('checkout', 'branch_L')
    self.repo.git('reset', 'branch_L~')
    with self.repo.open('L', 'w') as f:
      f.write('NOPE')
    self.repo.git('add', 'L')
    self.repo.git_commit('L NOPE')
    with self.repo.open('L', 'w') as f:
      f.write('L')
    self.repo.git('add', 'L')
    self.repo.git_commit('L YUP')

    # start on a branch which will be deleted
    self.repo.git('checkout', 'branch_G')

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('branch.branch_K.dormant true', output)

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('Rebase in progress', output)

    self.repo.git('checkout', '--theirs', 'M')
    self.repo.git('rebase', '--skip')

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('Failed! Attempting to squash', output)
    self.assertIn('Deleted branch branch_G', output)
    self.assertIn('Deleted branch branch_L', output)
    self.assertIn('\'branch_G\' was merged', output)
    self.assertIn('checking out \'origin/master\'', output)

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  def testRebaseConflictsKeepGoing(self):
    # Pretend that branch_L landed
    self.origin.git('checkout', 'master')
    with self.origin.open('L', 'w') as f:
      f.write('L')
    self.origin.git('add', 'L')
    self.origin.git_commit('L')

    # Add a commit to branch_K so that things fail
    self.repo.git('checkout', 'branch_K')
    with self.repo.open('M', 'w') as f:
      f.write('NOPE')
    self.repo.git('add', 'M')
    self.repo.git_commit('K NOPE')

    # Add a commits to branch_L which will work when squashed
    self.repo.git('checkout', 'branch_L')
    self.repo.git('reset', 'branch_L~')
    with self.repo.open('L', 'w') as f:
      f.write('NOPE')
    self.repo.git('add', 'L')
    self.repo.git_commit('L NOPE')
    with self.repo.open('L', 'w') as f:
      f.write('L')
    self.repo.git('add', 'L')
    self.repo.git_commit('L YUP')

    # start on a branch which will be deleted
    self.repo.git('checkout', 'branch_G')

    self.repo.git('config', 'branch.branch_K.dormant', 'false')
    output, _ = self.repo.capture_stdio(self.reup.main, ['-k'])
    self.assertIn('--keep-going set, continuing with next branch.', output)
    self.assertIn('could not be cleanly rebased:', output)
    self.assertIn('  branch_K', output)

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

  def testTrackTag(self):
    self.origin.git('tag', 'lkgr', self.origin['M'])
    self.repo.git('tag', 'lkgr', self.repo['D'])

    self.repo.git('config', 'branch.branch_G.remote', '.')
    self.repo.git('config', 'branch.branch_G.merge', 'refs/tags/lkgr')

    self.assertIn(
        'fatal: \'foo bar\' is not a valid branch name',
        self.repo.capture_stdio(self.nb.main, ['--lkgr', 'foo bar'])[1])

    self.repo.run(self.nb.main, ['--lkgr', 'foobar'])

    with self.repo.open('foobar', 'w') as f:
      f.write('this is the foobar file')
    self.repo.git('add', 'foobar')
    self.repo.git_commit('foobar1')

    with self.repo.open('foobar', 'w') as f:
      f.write('totes the Foobar file')
    self.repo.git_commit('foobar2')

    self.assertSchema("""
    A B H I J K
            J L
      B C D E F G
          D foobar1 foobar2
    """)
    self.assertEquals(self.repo['A'], self.origin['A'])
    self.assertEquals(self.repo['G'], self.origin['G'])

    output, _ = self.repo.capture_stdio(self.reup.main)
    self.assertIn('Fetching', output)
    self.assertIn('Rebasing: branch_G', output)
    self.assertIn('Rebasing: branch_K', output)
    self.assertIn('Rebasing: branch_L', output)
    self.assertIn('Rebasing: foobar', output)
    self.assertEquals(self.repo.git('rev-parse', 'lkgr').stdout.strip(),
                      self.origin['M'])

    self.assertSchema("""
    A B C D E F G M N O
                  M H I J K L
                  M foobar1 foobar2
    """)

    _, err = self.repo.capture_stdio(self.rp.main, ['tag F'])
    self.assertIn('fatal: invalid reference', err)

    output, _ = self.repo.capture_stdio(self.rp.main, ['tag_F'])
    self.assertIn('to track tag_F [tag] (was lkgr [tag])', output)

    self.assertSchema("""
    A B C D E F G M N O
                  M H I J K L
              F foobar1 foobar2
    """)

    output, _ = self.repo.capture_stdio(self.rp.main, ['--lkgr'])
    self.assertIn('to track lkgr [tag] (was tag_F [tag])', output)

    self.assertSchema("""
    A B C D E F G M N O
                  M H I J K L
                  M foobar1 foobar2
    """)

    output, _ = self.repo.capture_stdio(self.rp.main, ['--root'])
    self.assertIn('to track origin/master (was lkgr [tag])', output)

    self.assertSchema("""
    A B C D E F G M N O foobar1 foobar2
                  M H I J K L
    """)

390 391 392 393 394 395 396 397
  def testReparentBranchWithoutUpstream(self):
    self.repo.git('branch', 'nerp')
    self.repo.git('checkout', 'nerp')

    _, err = self.repo.capture_stdio(self.rp.main, ['branch_K'])

    self.assertIn('Unable to determine nerp@{upstream}', err)

398 399 400 401 402 403 404

if __name__ == '__main__':
  sys.exit(coverage_utils.covered_main((
    os.path.join(DEPOT_TOOLS_ROOT, 'git_rebase_update.py'),
    os.path.join(DEPOT_TOOLS_ROOT, 'git_new_branch.py'),
    os.path.join(DEPOT_TOOLS_ROOT, 'git_reparent_branch.py'),
    os.path.join(DEPOT_TOOLS_ROOT, 'git_rename_branch.py')
405
  )))