mq: fix update of headers that occur in the "wrong" order stable
authorMads Kiilerich <madski@unity3d.com>
Fri, 28 Nov 2014 02:57:33 +0100
branchstable
changeset 23412 94092019e839
parent 23409 dc4d2cd3aa3e
child 23413 0c432696dae3
mq: fix update of headers that occur in the "wrong" order Mq tried to insert headers in the right order. Sometimes it would stop searching before checking all headers and it could thus duplicate a header instead of replacing it.
hgext/mq.py
--- a/hgext/mq.py	Fri Nov 28 10:59:02 2014 -0800
+++ b/hgext/mq.py	Fri Nov 28 02:57:33 2014 +0100
@@ -113,7 +113,6 @@
     '# 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):
@@ -127,6 +126,9 @@
     ['# 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 x', '# User y'],
+    ...                '# User ', 'z')
+    ['# HG changeset patch', '# Date x', '# User z']
     >>> inserthgheader(['# HG changeset patch', '# Date y'], '# Date ', 'z')
     ['# HG changeset patch', '# Date z']
     >>> inserthgheader(['# HG changeset patch', '', '# Date y'], '# Date ', 'z')
@@ -136,18 +138,21 @@
     """
     start = lines.index('# HG changeset patch') + 1
     newindex = HGHEADERS.index(header)
+    bestpos = len(lines)
     for i in range(start, len(lines)):
         line = lines[i]
+        if not line.startswith('# '):
+            bestpos = min(bestpos, i)
+            break
         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
+                if lineindex > newindex:
+                    bestpos = min(bestpos, i)
+                break # next line
+    lines.insert(bestpos, header + value)
     return lines
 
 def insertplainheader(lines, header, value):