changeset 7955:c3d4ff03ec72

rebase: keep original mq patch format (Issue1574) Rebase now doesn't make assumptions about which format has been used for a mq patch (git or normal). Before finalizing a patch it keeps track of the original format, by reading its header, and then restores the format when reimporting it. This way it also allows for having mixed styles. Note: this version corrects a failure in the test
author Stefano Tortarolo <stefano.tortarolo@gmail.com>
date Thu, 26 Mar 2009 16:58:50 +0200
parents b969611064ae
children 3e7611a83230
files hgext/rebase.py tests/test-rebase-mq tests/test-rebase-mq.out
diffstat 3 files changed, 104 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/rebase.py	Mon Mar 30 18:26:32 2009 +0200
+++ b/hgext/rebase.py	Thu Mar 26 16:58:50 2009 +0200
@@ -14,7 +14,7 @@
 '''
 
 from mercurial import util, repair, merge, cmdutil, commands, error
-from mercurial import extensions, ancestor, copies
+from mercurial import extensions, ancestor, copies, patch
 from mercurial.commands import templateopts
 from mercurial.node import nullrev
 from mercurial.i18n import _
@@ -264,6 +264,14 @@
             p2 = P2n
     return p1, p2
 
+def isagitpatch(repo, patchname):
+    'Return true if the given patch is in git format'
+    mqpatch = os.path.join(repo.mq.path, patchname)
+    for line in patch.linereader(file(mqpatch, 'rb')):
+        if line.startswith('diff --git'):
+            return True
+    return False
+
 def updatemq(repo, state, skipped, **opts):
     'Update rebased mq patches - finalize and then import them'
     mqrebase = {}
@@ -271,7 +279,7 @@
         if repo[p.rev].rev() in state:
             repo.ui.debug(_('revision %d is an mq patch (%s), finalize it.\n') %
                                         (repo[p.rev].rev(), p.name))
-            mqrebase[repo[p.rev].rev()] = p.name
+            mqrebase[repo[p.rev].rev()] = (p.name, isagitpatch(repo, p.name))
 
     if mqrebase:
         repo.mq.finish(repo, mqrebase.keys())
@@ -283,9 +291,9 @@
         for rev in mq:
             if rev not in skipped:
                 repo.ui.debug(_('import mq patch %d (%s)\n')
-                              % (state[rev], mqrebase[rev]))
-                repo.mq.qimport(repo, (), patchname=mqrebase[rev],
-                            git=opts.get('git', False),rev=[str(state[rev])])
+                              % (state[rev], mqrebase[rev][0]))
+                repo.mq.qimport(repo, (), patchname=mqrebase[rev][0],
+                            git=mqrebase[rev][1],rev=[str(state[rev])])
         repo.mq.save_dirty()
 
 def storestatus(repo, originalwd, target, state, collapse, keep, keepbranches,
--- a/tests/test-rebase-mq	Mon Mar 30 18:26:32 2009 +0200
+++ b/tests/test-rebase-mq	Thu Mar 26 16:58:50 2009 +0200
@@ -11,6 +11,7 @@
         -e "s/^\(# Node ID\).*/\1/" \
         -e "s/^\(# Parent\).*/\1/" \
         -e "s/^\(diff -r \)\([a-f0-9]* \)\(-r \)\([a-f0-9]* \)/\1x \3y /" \
+        -e "s/^\(diff -r \)\([a-f0-9]* \)/\1x /" \
         -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" \
         -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/"
 }
@@ -74,3 +75,38 @@
 echo '% And the patch is correct'
 cat .hg/patches/f2.patch | filterpatch
 
+echo
+echo '% Adding one git-style patch and one normal'
+hg qpop -a
+rm -fr .hg/patches
+hg qinit -c
+
+hg up 0
+hg qnew --git f_git.patch
+echo 'mq1' > p
+hg add p
+hg qref --git -m 'P0 (git)'
+
+hg qnew f.patch
+echo 'mq2' > p
+hg qref -m 'P1'
+
+echo '% Git patch'
+cat .hg/patches/f_git.patch  | filterpatch
+
+echo
+echo '% Normal patch'
+cat .hg/patches/f.patch | filterpatch
+
+echo
+echo '% Rebase the applied mq patches'
+hg rebase -s 2 -d 1 --quiet 2>&1 | sed -e 's/\(saving bundle to \).*/\1/'
+
+echo '% And the patches are correct'
+echo '% Git patch'
+cat .hg/patches/f_git.patch  | filterpatch
+
+echo
+echo '% Normal patch'
+cat .hg/patches/f.patch | filterpatch
+
--- a/tests/test-rebase-mq.out	Mon Mar 30 18:26:32 2009 +0200
+++ b/tests/test-rebase-mq.out	Thu Mar 26 16:58:50 2009 +0200
@@ -77,3 +77,58 @@
 @@ -1,1 +1,1 @@
 -mq1r1
 +mq1r1mq2
+
+% Adding one git-style patch and one normal
+patch queue now empty
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+% Git patch
+P0 (git)
+
+diff --git a/p b/p
+new file mode 100644
+--- /dev/null
++++ b/p
+@@ -0,0 +1,1 @@
++mq1
+
+% Normal patch
+P1
+
+diff -r x p
+--- a/p
++++ b/p
+@@ -1,1 +1,1 @@
+-mq1
++mq2
+
+% Rebase the applied mq patches
+saving bundle to 
+% And the patches are correct
+% Git patch
+# HG changeset patch
+# User test
+# Date
+# Node ID
+# Parent
+P0 (git)
+
+diff --git a/p b/p
+new file mode 100644
+--- /dev/null
++++ b/p
+@@ -0,0 +1,1 @@
++mq1
+
+% Normal patch
+# HG changeset patch
+# User test
+# Date
+# Node ID
+# Parent
+P1
+
+--- a/p
++++ b/p
+@@ -1,1 +1,1 @@
+-mq1
++mq2