Patchwork [7,of,8] mq: refactor patchheader header ordering - use export order for HG patches

login
register
mail settings
Submitter Mads Kiilerich
Date Sept. 24, 2014, 1 a.m.
Message ID <154f22c36d16a6244c11.1411520408@localhost.localdomain>
Download mbox | patch
Permalink /patch/5954/
State Accepted
Headers show

Comments

Mads Kiilerich - Sept. 24, 2014, 1 a.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1411519271 -7200
#      Wed Sep 24 02:41:11 2014 +0200
# Node ID 154f22c36d16a6244c11cf3c99451b6a2e2fa8fe
# Parent  fd737e3bd2f55ef70f7c82914da1d1fb001c9936
mq: refactor patchheader header ordering - use export order for HG patches

The refactoring also gives more robust and extendable handling of other HG
headers.

Patch

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -104,6 +104,52 @@  class statusentry(object):
     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.
+    >>> inserthgheader([], '# Date ', 'z')
+    Traceback (most recent call last):
+    ValueError: '# HG changeset patch' is not in list
+    >>> 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 @@  class patchheader(object):
     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 @@  class patchheader(object):
     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 @@  class patchheader(object):
         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]
diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -31,3 +31,4 @@  testmod('mercurial.util', testtarget='pl
 testmod('hgext.convert.cvsps')
 testmod('hgext.convert.filemap')
 testmod('hgext.convert.subversion')
+testmod('hgext.mq')
diff --git a/tests/test-mq-header-date.t b/tests/test-mq-header-date.t
--- a/tests/test-mq-header-date.t
+++ b/tests/test-mq-header-date.t
@@ -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
   
diff --git a/tests/test-mq-header-from.t b/tests/test-mq-header-from.t
--- a/tests/test-mq-header-from.t
+++ b/tests/test-mq-header-from.t
@@ -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
diff --git a/tests/test-mq-subrepo.t b/tests/test-mq-subrepo.t
--- a/tests/test-mq-subrepo.t
+++ b/tests/test-mq-subrepo.t
@@ -482,8 +482,8 @@  check whether qrefresh imports updated .
   +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 @@  check whether qrefresh imports updated .
   +88ac1bef5ed43b689d1d200b59886b675dec474b sub
   $ cat .hg/patches/import-at-qrefresh
   # HG changeset patch
+  # User test
   # Date 0 0
-  # User test
   # Parent  05b056bb9c8c05ff15258b84fd42ab3527271033
   
   diff -r 05b056bb9c8c .hgsubstate