Preserve merges when doing a rebase

When a person is using git-review after having merged in a branch, the
default rebase behavior as currently implemented will rebase all of the
commits that are inside of the merge. This is almost never what someone
wants to do.

Add -p to the rebase commands which will tell git to preserve the merges
and only rebase the merge commit itself.

Explicitly preserve original SHA1 from before the rebase, since git may
not set ORIG_HEAD as expected depending the operations performed.

Change-Id: I8a5f0307113cd0cf0d08c664c13a0676400bd05c
This commit is contained in:
Monty Taylor
2013-10-08 12:06:48 -04:00
committed by Darragh Bailey
parent e79e69f66b
commit f0315ba9b8
2 changed files with 23 additions and 5 deletions

View File

@@ -56,6 +56,7 @@ DEFAULTS = dict(hostname=False, port='29418', project=False,
_branch_name = None
_has_color = None
_orig_head = None
class colors:
@@ -456,15 +457,28 @@ def check_remote(branch, remote, hostname, port, project):
def rebase_changes(branch, remote, interactive=True):
global _orig_head
remote_branch = "remotes/%s/%s" % (remote, branch)
if not update_remote(remote):
return False
# since the value of ORIG_HEAD may not be set by rebase as expected
# for use in undo_rebase, make sure to save it explicitly
cmd = "git rev-parse HEAD"
(status, output) = run_command_status(cmd)
if status != 0:
print("Errors running %s" % cmd)
if interactive:
print(output)
return False
_orig_head = output
if interactive:
cmd = "git rebase -i %s" % remote_branch
cmd = "git rebase -p -i %s" % remote_branch
else:
cmd = "git rebase %s" % remote_branch
cmd = "git rebase -p %s" % remote_branch
(status, output) = run_command_status(cmd, GIT_EDITOR='true')
if status != 0:
@@ -476,7 +490,11 @@ def rebase_changes(branch, remote, interactive=True):
def undo_rebase():
cmd = "git reset --hard ORIG_HEAD"
global _orig_head
if not _orig_head:
return True
cmd = "git reset --hard %s" % _orig_head
(status, output) = run_command_status(cmd)
if status != 0:
print("Errors running %s" % cmd)

View File

@@ -100,7 +100,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
'create conflict with master')
exc = self.assertRaises(Exception, self._run_git_review)
self.assertIn("Errors running git rebase -i remotes/gerrit/master",
self.assertIn("Errors running git rebase -p -i remotes/gerrit/master",
exc.args[0])
def test_upload_without_rebase(self):
@@ -115,7 +115,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
self._dir('test', 'new_test_file.txt'))
review_res = self._run_git_review('-v')
self.assertIn("Running: git rebase -i remotes/gerrit/master",
self.assertIn("Running: git rebase -p -i remotes/gerrit/master",
review_res)
self.assertEqual(self._run_git('rev-parse', 'HEAD^1'), head_1)