Patchwork [stable] mq: fix update of headers that occur in the "wrong" order

login
register
mail settings
Submitter Mads Kiilerich
Date Nov. 28, 2014, 2:34 a.m.
Message ID <f11d2f46c07535a926cf.1417142045@localhost.localdomain>
Download mbox | patch
Permalink /patch/6882/
State Accepted
Commit 94092019e8395f4ee9570a5712981a13545aaf51
Headers show

Comments

Mads Kiilerich - Nov. 28, 2014, 2:34 a.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1417139853 -3600
#      Fri Nov 28 02:57:33 2014 +0100
# Branch stable
# Node ID f11d2f46c07535a926cf09d500532f142574f6fe
# Parent  edf29f9c15f0f171847f4c7a8184cca4e95c8b31
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.
Matt Mackall - Dec. 1, 2014, 8:39 p.m.
On Fri, 2014-11-28 at 03:34 +0100, Mads Kiilerich wrote:
> # HG changeset patch
> # User Mads Kiilerich <madski@unity3d.com>
> # Date 1417139853 -3600
> #      Fri Nov 28 02:57:33 2014 +0100
> # Branch stable
> # Node ID f11d2f46c07535a926cf09d500532f142574f6fe
> # Parent  edf29f9c15f0f171847f4c7a8184cca4e95c8b31
> mq: fix update of headers that occur in the "wrong" order

Queued for stable, thanks.

Patch

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -113,7 +113,6 @@  HGHEADERS = [
     '# 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 @@  def inserthgheader(lines, header, value)
     ['# 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 @@  def inserthgheader(lines, header, value)
     """
     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):