mercurial/utils/stringutil.py
changeset 38576 a3130208db1c
parent 38477 de275ab362cb
child 38783 e7aa113b14f7
--- a/mercurial/utils/stringutil.py	Sun Jun 10 17:07:29 2018 +0900
+++ b/mercurial/utils/stringutil.py	Sun Jun 10 17:19:31 2018 +0900
@@ -90,6 +90,29 @@
         p0, p1 = q0, q1
     return '\n'.join('  ' * l + s for l, s in lines)
 
+def buildrepr(r):
+    """Format an optional printable representation from unexpanded bits
+
+    ========  =================================
+    type(r)   example
+    ========  =================================
+    tuple     ('<not %r>', other)
+    bytes     '<branch closed>'
+    callable  lambda: '<branch %r>' % sorted(b)
+    object    other
+    ========  =================================
+    """
+    if r is None:
+        return ''
+    elif isinstance(r, tuple):
+        return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
+    elif isinstance(r, bytes):
+        return r
+    elif callable(r):
+        return r()
+    else:
+        return pycompat.byterepr(r)
+
 def binary(s):
     """return true if a string is binary data"""
     return bool(s and '\0' in s)