changeset 22546:aac5482db318

mq: refactor patchheader header ordering to match export (BC) The refactoring also gives more robust and extendable handling of other HG headers.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 24 Sep 2014 02:41:11 +0200
parents 6a2cfa61acb0
children 3d616d27a422
files hgext/mq.py tests/test-doctest.py tests/test-mq-header-date.t tests/test-mq-header-from.t tests/test-mq-subrepo.t
diffstat 5 files changed, 68 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/mq.py	Sat Sep 20 17:06:57 2014 +0200
+++ b/hgext/mq.py	Wed Sep 24 02:41:11 2014 +0200
@@ -104,6 +104,52 @@
     def __repr__(self):
         return hex(self.node) + ':' + self.name
 
+# The order of the headers in 'hg export' HG patches:
+HGHEADERS = [
+#   '# HG changeset patch',
+    '# User ',
+    '# Date ',
+    '#      ',
+    '# Branch ',
+    '# Node ID ',
+    '# Parent  ', # can occur twice for merges - but that is not relevant for mq
+    '', # all lines after headers 'has' this prefix - simplifies the algorithm
+    ]
+
+def inserthgheader(lines, header, value):
+    """Assuming lines contains a HG patch header, add a header line with value.
+    >>> try: inserthgheader([], '# Date ', 'z')
+    ... except ValueError, inst: print "oops"
+    oops
+    >>> inserthgheader(['# HG changeset patch'], '# Date ', 'z')
+    ['# HG changeset patch', '# Date z']
+    >>> inserthgheader(['# HG changeset patch', ''], '# Date ', 'z')
+    ['# HG changeset patch', '# Date z', '']
+    >>> inserthgheader(['# HG changeset patch', '# User y'], '# Date ', 'z')
+    ['# HG changeset patch', '# User y', '# Date z']
+    >>> inserthgheader(['# HG changeset patch', '# Date y'], '# Date ', 'z')
+    ['# HG changeset patch', '# Date z']
+    >>> inserthgheader(['# HG changeset patch', '', '# Date y'], '# Date ', 'z')
+    ['# HG changeset patch', '# Date z', '', '# Date y']
+    >>> inserthgheader(['# HG changeset patch', '# Parent  y'], '# Date ', 'z')
+    ['# HG changeset patch', '# Date z', '# Parent  y']
+    """
+    start = lines.index('# HG changeset patch') + 1
+    newindex = HGHEADERS.index(header)
+    for i in range(start, len(lines)):
+        line = lines[i]
+        for lineindex, h in enumerate(HGHEADERS):
+            if line.startswith(h):
+                if lineindex < newindex:
+                    break # next line
+                if lineindex == newindex:
+                    lines[i] = header + value
+                else:
+                    lines.insert(i, header + value)
+                return lines
+    lines.append(header + value)
+    return lines
+
 class patchheader(object):
     def __init__(self, pf, plainmode=False):
         def eatdiff(lines):
@@ -211,8 +257,7 @@
     def setuser(self, user):
         if not self.updateheader(['From: ', '# User '], user):
             try:
-                patchheaderat = self.comments.index('# HG changeset patch')
-                self.comments.insert(patchheaderat + 1, '# User ' + user)
+                inserthgheader(self.comments, '# User ', user)
             except ValueError:
                 if self.plainmode:
                     self.comments = ['From: ' + user] + self.comments
@@ -224,8 +269,7 @@
     def setdate(self, date):
         if not self.updateheader(['Date: ', '# Date '], date):
             try:
-                patchheaderat = self.comments.index('# HG changeset patch')
-                self.comments.insert(patchheaderat + 1, '# Date ' + date)
+                inserthgheader(self.comments, '# Date ', date)
             except ValueError:
                 if self.plainmode:
                     self.comments = ['Date: ' + date] + self.comments
@@ -238,8 +282,7 @@
         if not (self.updateheader(['# Parent  '], parent) or
                 self.updateheader(['# Parent '], parent)):
             try:
-                patchheaderat = self.comments.index('# HG changeset patch')
-                self.comments.insert(patchheaderat + 1, '# Parent  ' + parent)
+                inserthgheader(self.comments, '# Parent  ', parent)
             except ValueError:
                 if not self.plainmode:
                     tmp = ['# HG changeset patch', '# Parent  ' + parent]
--- a/tests/test-doctest.py	Sat Sep 20 17:06:57 2014 +0200
+++ b/tests/test-doctest.py	Wed Sep 24 02:41:11 2014 +0200
@@ -31,3 +31,4 @@
 testmod('hgext.convert.cvsps')
 testmod('hgext.convert.filemap')
 testmod('hgext.convert.subversion')
+testmod('hgext.mq')
--- a/tests/test-mq-header-date.t	Sat Sep 20 17:06:57 2014 +0200
+++ b/tests/test-mq-header-date.t	Wed Sep 24 02:41:11 2014 +0200
@@ -329,8 +329,8 @@
   ==== hg qref
   adding 5
   # HG changeset patch
+  # Date 10 0
   # Parent 
-  # Date 10 0
   
   diff -r ... 5
   --- /dev/null
@@ -342,8 +342,8 @@
   0: [mq]: 1.patch - test - 4.00
   ==== hg qref -d
   # HG changeset patch
+  # Date 11 0
   # Parent 
-  # Date 11 0
   
   diff -r ... 5
   --- /dev/null
@@ -683,8 +683,8 @@
   ==== hg qref
   adding 5
   # HG changeset patch
+  # Date 10 0
   # Parent 
-  # Date 10 0
   
   diff -r ... 5
   --- /dev/null
@@ -696,8 +696,8 @@
   0: [mq]: 1.patch - test - 4.00
   ==== hg qref -d
   # HG changeset patch
+  # Date 11 0
   # Parent 
-  # Date 11 0
   
   diff -r ... 5
   --- /dev/null
@@ -761,8 +761,8 @@
   0: [mq]: 1.patch - test
   ==== qref -d
   # HG changeset patch
+  # User jane
   # Date 12 0
-  # User jane
   # Parent 
   
   diff -r ... 6
@@ -823,8 +823,8 @@
   0: [mq]: 1.patch - test
   ==== qref -u -d
   # HG changeset patch
+  # User john
   # Date 14 0
-  # User john
   # Parent 
   
   diff -r ... 8
@@ -857,8 +857,8 @@
   0: [mq]: 1.patch - test
   ==== qref -u -d
   # HG changeset patch
+  # User john
   # Date 15 0
-  # User john
   # Parent 
   Nine
   
--- a/tests/test-mq-header-from.t	Sat Sep 20 17:06:57 2014 +0200
+++ b/tests/test-mq-header-from.t	Wed Sep 24 02:41:11 2014 +0200
@@ -270,8 +270,8 @@
   ==== hg qref
   adding 5
   # HG changeset patch
+  # User johndoe
   # Parent 
-  # User johndoe
   
   diff -r ... 5
   --- /dev/null
@@ -285,8 +285,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -U
   # HG changeset patch
+  # User test
   # Parent 
-  # User test
   
   diff -r ... 5
   --- /dev/null
@@ -300,8 +300,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -u
   # HG changeset patch
+  # User johndeere
   # Parent 
-  # User johndeere
   
   diff -r ... 5
   --- /dev/null
@@ -560,8 +560,8 @@
   ==== hg qref
   adding 5
   # HG changeset patch
+  # User johndoe
   # Parent 
-  # User johndoe
   
   diff -r ... 5
   --- /dev/null
@@ -575,8 +575,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -U
   # HG changeset patch
+  # User test
   # Parent 
-  # User test
   
   diff -r ... 5
   --- /dev/null
@@ -590,8 +590,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -u
   # HG changeset patch
+  # User johndeere
   # Parent 
-  # User johndeere
   
   diff -r ... 5
   --- /dev/null
@@ -845,8 +845,8 @@
   ==== hg qref
   adding 5
   # HG changeset patch
+  # User johndoe
   # Parent 
-  # User johndoe
   
   diff -r ... 5
   --- /dev/null
@@ -860,8 +860,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -U
   # HG changeset patch
+  # User test
   # Parent 
-  # User test
   
   diff -r ... 5
   --- /dev/null
@@ -875,8 +875,8 @@
   0: [mq]: 1.patch - mary
   ==== hg qref -u
   # HG changeset patch
+  # User johndeere
   # Parent 
-  # User johndeere
   
   diff -r ... 5
   --- /dev/null
--- a/tests/test-mq-subrepo.t	Sat Sep 20 17:06:57 2014 +0200
+++ b/tests/test-mq-subrepo.t	Wed Sep 24 02:41:11 2014 +0200
@@ -482,8 +482,8 @@
   +88ac1bef5ed43b689d1d200b59886b675dec474b sub
   $ cat .hg/patches/import-at-qrefresh
   # HG changeset patch
+  # User test
   # Date 0 0
-  # User test
   # Parent  05b056bb9c8c05ff15258b84fd42ab3527271033
   
   diff -r 05b056bb9c8c .hgsubstate
@@ -507,8 +507,8 @@
   +88ac1bef5ed43b689d1d200b59886b675dec474b sub
   $ cat .hg/patches/import-at-qrefresh
   # HG changeset patch
+  # User test
   # Date 0 0
-  # User test
   # Parent  05b056bb9c8c05ff15258b84fd42ab3527271033
   
   diff -r 05b056bb9c8c .hgsubstate