Patchwork [V2] stringutil: fix prettyrepr() to not orphan foo=<...> line

login
register
mail settings
Submitter Yuya Nishihara
Date June 11, 2018, 1:26 p.m.
Message ID <af74498c544a42284418.1528723613@mimosa>
Download mbox | patch
Permalink /patch/32060/
State Accepted
Headers show

Comments

Yuya Nishihara - June 11, 2018, 1:26 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1528601093 -32400
#      Sun Jun 10 12:24:53 2018 +0900
# Node ID af74498c544a42284418c9b8744494fe81a2c00e
# Parent  703464999bb0d3a2b10723398ac5bd73b86f875a
stringutil: fix prettyrepr() to not orphan foo=<...> line
Augie Fackler - June 11, 2018, 7:36 p.m.
On Mon, Jun 11, 2018 at 10:26:53PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1528601093 -32400
> #      Sun Jun 10 12:24:53 2018 +0900
> # Node ID af74498c544a42284418c9b8744494fe81a2c00e
> # Parent  703464999bb0d3a2b10723398ac5bd73b86f875a
> stringutil: fix prettyrepr() to not orphan foo=<...> line

queued, thanks

Patch

diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -49,15 +49,26 @@  def prettyrepr(o):
     """Pretty print a representation of a possibly-nested object"""
     lines = []
     rs = pycompat.byterepr(o)
-    p = 0
-    while p < len(rs):
-        q = rs.find('<', p + 1)
-        if q < 0:
-            q = len(rs)
-        l = rs.count('<', 0, p) - rs.count('>', 0, p)
+    p0 = p1 = 0
+    while p0 < len(rs):
+        # '... field=<type ... field=<type ...'
+        #      ~~~~~~~~~~~~~~~~
+        #      p0    p1        q0    q1
+        q0 = -1
+        q1 = rs.find('<', p1 + 1)
+        if q1 < 0:
+            q1 = len(rs)
+        elif q1 > p1 + 1 and rs.startswith('=', q1 - 1):
+            # backtrack for ' field=<'
+            q0 = rs.rfind(' ', p1 + 1, q1 - 1)
+        if q0 < 0:
+            q0 = q1
+        else:
+            q0 += 1  # skip ' '
+        l = rs.count('<', 0, p0) - rs.count('>', 0, p0)
         assert l >= 0
-        lines.append((l, rs[p:q].rstrip()))
-        p = q
+        lines.append((l, rs[p0:q0].rstrip()))
+        p0, p1 = q0, q1
     return '\n'.join('  ' * l + s for l, s in lines)
 
 def binary(s):
diff --git a/tests/test-walk.t b/tests/test-walk.t
--- a/tests/test-walk.t
+++ b/tests/test-walk.t
@@ -80,9 +80,9 @@ 
   f  mammals/skunk                   skunk
   $ hg debugwalk -v -X ../beans
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   f  fennel                          ../fennel
   f  fenugreek                       ../fenugreek
   f  fiddlehead                      ../fiddlehead
@@ -106,9 +106,9 @@ 
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 'relglob:*k' .
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:(?:|.*/)[^/]*k(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:(?:|.*/)[^/]*k(?:/|$))'>>
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 're:.*k$'
   * matcher:
@@ -167,9 +167,9 @@ 
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk -v -X 'rootfilesin:'
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:[^/]+$)'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -228,9 +228,9 @@ 
   f  mammals/skunk  skunk
   $ hg debugwalk -v -X 'rootfilesin:mammals'
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:mammals/[^/]+$)'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:mammals/[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -322,9 +322,9 @@ 
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk -v -Ibeans mammals
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -Inon-existent
   * matcher:
   <includematcher includes='(?:non\\-existent(?:/|$))'>
@@ -334,21 +334,21 @@ 
   f  beans/black  beans/black
   $ hg debugwalk -v -Ibeans beans/black
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   f  beans/black  beans/black  exact
   $ hg debugwalk -v -Ibeans/black beans
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/black  beans/black
   $ hg debugwalk -v -Xbeans/black beans
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
   f  beans/navy      beans/navy
@@ -356,9 +356,9 @@ 
   f  beans/turtle    beans/turtle
   $ hg debugwalk -v -Xbeans/black -Ibeans
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
   f  beans/navy      beans/navy
@@ -366,24 +366,24 @@ 
   f  beans/turtle    beans/turtle
   $ hg debugwalk -v -Xbeans/black beans/black
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans/black -Ibeans/black
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans beans/black
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -Xbeans -Ibeans/black
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v 'glob:mammals/../beans/b*'
   * matcher:
   <patternmatcher patterns='(?:beans\\/b[^/]*$)'>
@@ -391,9 +391,9 @@ 
   f  beans/borlotti  beans/borlotti
   $ hg debugwalk -v '-X*/Procyonidae' mammals
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk -v path:mammals
   * matcher:
@@ -425,24 +425,24 @@  Test explicit paths and excludes:
 
   $ hg debugwalk -v fennel -X fennel
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:fennel(?:/|$))'>, m2=
-    <includematcher includes='(?:fennel(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:fennel(?:/|$))'>,
+    m2=<includematcher includes='(?:fennel(?:/|$))'>>
   $ hg debugwalk -v fennel -X 'f*'
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:fennel(?:/|$))'>, m2=
-    <includematcher includes='(?:f[^/]*(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:fennel(?:/|$))'>,
+    m2=<includematcher includes='(?:f[^/]*(?:/|$))'>>
   $ hg debugwalk -v beans/black -X 'path:beans'
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -I 'path:beans/black' -X 'path:beans'
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
 
 Test absolute paths: