changeset 991:26e9fd21f3bf

push: avoid rebasing when we know there are no conflicts When pushing multiple commits, push would do one rebase per commit. This changes it track the files that have been changed as part of the push and only perform a rebase if the commit touches a file that has been changed previously. On our repo, push used to take 25 seconds per commit. With this change it takes closer to 10 seconds per commit (if there are no conflicts).
author Durham Goode <durham@fb.com>
date Wed, 02 Jan 2013 17:54:30 -0800
parents def2144c0a8c
children 110794582448
files hgsubversion/wrappers.py tests/test_push_command.py
diffstat 2 files changed, 63 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/wrappers.py
+++ b/hgsubversion/wrappers.py
@@ -211,6 +211,7 @@ def push(repo, dest, force, revs):
 
         tip_ctx = repo[outgoing[-1]].p1()
         svnbranch = tip_ctx.branch()
+        modified_files = {}
         for i in range(len(outgoing) - 1, -1, -1):
             # 2. Pick the oldest changeset that needs to be pushed
             current_ctx = repo[outgoing[i]]
@@ -223,29 +224,36 @@ def push(repo, dest, force, revs):
                 return 0
 
             # 3. Move the changeset to the tip of the branch if necessary
-            util.swap_out_encoding(old_encoding)
-            try:
-                def extrafn(ctx, extra):
-                    extra['branch'] = ctx.branch()
-
-                ui.status('rebasing %s onto %s \n' % (current_ctx, tip_ctx))
-                hgrebase.rebase(ui, repo,
-                                dest=node.hex(tip_ctx.node()),
-                                rev=[node.hex(current_ctx.node())],
-                                extrafn=extrafn, keep=True)
-            finally:
-                util.swap_out_encoding()
-
-            # Don't trust the pre-rebase repo and context.
-            repo = getlocalpeer(ui, {}, meta.path)
-            tip_ctx = repo[tip_ctx.node()]
-            for c in tip_ctx.descendants():
-                rebasesrc = c.extra().get('rebase_source')
-                if rebasesrc and node.bin(rebasesrc) == current_ctx.node():
-                    current_ctx = c
-                    temporary_commits.append(c.node())
+            conflicts = False
+            for file in current_ctx.files():
+                if file in modified_files:
+                    conflicts = True
                     break
 
+            if conflicts or current_ctx.branch() != svnbranch:
+                util.swap_out_encoding(old_encoding)
+                try:
+                    def extrafn(ctx, extra):
+                        extra['branch'] = ctx.branch()
+
+                    ui.status('rebasing %s onto %s \n' % (current_ctx, tip_ctx))
+                    hgrebase.rebase(ui, repo,
+                                    dest=node.hex(tip_ctx.node()),
+                                    rev=[node.hex(current_ctx.node())],
+                                    extrafn=extrafn, keep=True)
+                finally:
+                    util.swap_out_encoding()
+
+                # Don't trust the pre-rebase repo and context.
+                repo = getlocalpeer(ui, {}, meta.path)
+                tip_ctx = repo[tip_ctx.node()]
+                for c in tip_ctx.descendants():
+                    rebasesrc = c.extra().get('rebase_source')
+                    if rebasesrc and node.bin(rebasesrc) == current_ctx.node():
+                        current_ctx = c
+                        temporary_commits.append(c.node())
+                        break
+
             # 4. Push the changeset to subversion
             tip_hash = hashes[tip_ctx.node()][0]
             try:
@@ -268,6 +276,11 @@ def push(repo, dest, force, revs):
                 if c.node() in hashes and c.branch() == svnbranch:
                     tip_ctx = c
 
+                    # Remember what files have been modified since the
+                    # whole push started.
+                    for file in c.files():
+                        modified_files[file] = True
+
             # 7. Rebase any children of the commit we just pushed
             # that are not in the outgoing set
             for c in original_ctx.children():
--- a/tests/test_push_command.py
+++ b/tests/test_push_command.py
@@ -551,6 +551,35 @@ class PushTests(test_util.TestBase):
         self.assertEqual(commit1.files(), ['gamma', ])
         self.assertFalse(commit1.mutable())
 
+    def test_push_two_that_modify_same_file(self):
+        '''
+        Push performs a rebase if two commits touch the same file.
+        This test verifies that code path works.
+        '''
+
+        oldlen = len(self.repo)
+        oldtiphash = self.repo['default'].node()
+
+        changes = [('gamma', 'gamma', 'sometext')]
+        newhash = self.commitchanges(changes)
+        changes = [('gamma', 'gamma', 'sometext\n moretext'),
+                   ('delta', 'delta', 'sometext\n moretext'),
+                  ]
+        newhash = self.commitchanges(changes)
+
+        repo = self.repo
+        hg.update(repo, newhash)
+        commands.push(repo.ui, repo)
+        self.assertEqual(len(self.repo), oldlen + 2)
+
+        # verify that both commits are pushed
+        commit1 = self.repo['tip']
+        self.assertEqual(commit1.files(), ['delta', 'gamma'])
+        self.assertFalse(commit1.mutable())
+        commit2 = commit1.parents()[0]
+        self.assertEqual(commit2.files(), ['gamma'])
+        self.assertFalse(commit2.mutable())
+
 
 def suite():
     test_classes = [PushTests, ]