[PATCH] rebase: keep original mq patch format (Issue1574)

Stefano Tortarolo stefano.tortarolo at gmail.com
Thu Apr 2 12:13:36 CDT 2009


# HG changeset patch
# User Stefano Tortarolo <stefano.tortarolo at gmail.com>
# Date 1238079530 -7200
# Node ID d5b66185e082998a5c62cc9ecef0f53d42934144
# Parent  39566bb99a9cf21a7890bad83e2dfcdb10f47257
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

diff -r 39566bb99a9c -r d5b66185e082 hgext/rebase.py
--- a/hgext/rebase.py	Tue Mar 31 21:21:53 2009 -0500
+++ 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
+from mercurial import extensions, ancestor, patch
 from mercurial.commands import templateopts
 from mercurial.node import nullrev
 from mercurial.i18n import _
@@ -249,6 +249,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 = {}
@@ -256,7 +264,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())
@@ -268,9 +276,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, external):
diff -r 39566bb99a9c -r d5b66185e082 tests/test-rebase-mq
--- a/tests/test-rebase-mq	Tue Mar 31 21:21:53 2009 -0500
+++ 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
+
diff -r 39566bb99a9c -r d5b66185e082 tests/test-rebase-mq.out
--- a/tests/test-rebase-mq.out	Tue Mar 31 21:21:53 2009 -0500
+++ 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


More information about the Mercurial-devel mailing list