hgext/beautifygraph.py
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 10 Dec 2019 14:40:44 -0800
changeset 43838 a47ccdcce4f9
parent 43506 9f70512ae2cf
child 44345 14d0e89520a2
permissions -rw-r--r--
dirs: fix out-of-bounds access in Py3 The hack for mutating Python's variable-length integers that was ported to py3 in cb3048746dae (dirs: port PyInt code to work on Python 3, 2016-10-08) was reading from ob_digit[1] instead of ob_digit[0] for some reason. Space for ob_digit[1] would only be allocated for integers larger than 30 bits, so we ended up writing to unallocated memory. Also, we would write an integer that's 2^30 times too large, so we would never free these integers. Found by AddressSanitizer. Differential Revision: https://phab.mercurial-scm.org/D7597
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     2
# beautifygraph.py - improve graph output by using Unicode characters
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     3
#
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     4
# Copyright 2018 John Stiles <johnstiles@gmail.com>
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     5
#
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms of the
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     7
# GNU General Public License version 2 or any later version.
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     8
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
     9
'''beautify log -G output by using Unicode characters (EXPERIMENTAL)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    10
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    11
   A terminal with UTF-8 support and monospace narrow text are required.
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    12
'''
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    13
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    14
from __future__ import absolute_import
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    15
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    16
from mercurial.i18n import _
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    17
from mercurial import (
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    18
    encoding,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    19
    extensions,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    20
    graphmod,
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38340
diff changeset
    21
    pycompat,
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    22
    templatekw,
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    23
)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    24
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    25
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    26
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    27
# be specifying the version(s) of Mercurial they are tested with, or
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    28
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
testedwith = b'ships-with-hg-core'
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    30
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    31
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    32
def prettyedge(before, edge, after):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
    if edge == b'~':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
        return b'\xE2\x95\xA7'  # U+2567 ╧
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
    if edge == b'/':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
        return b'\xE2\x95\xB1'  # U+2571 ╱
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
    if edge == b'-':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    38
        return b'\xE2\x94\x80'  # U+2500 ─
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
    if edge == b'|':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
        return b'\xE2\x94\x82'  # U+2502 │
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
    if edge == b':':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    42
        return b'\xE2\x94\x86'  # U+2506 ┆
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    43
    if edge == b'\\':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
        return b'\xE2\x95\xB2'  # U+2572 ╲
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
    if edge == b'+':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
        if before == b' ' and not after == b' ':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    47
            return b'\xE2\x94\x9C'  # U+251C ├
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
        if after == b' ' and not before == b' ':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    49
            return b'\xE2\x94\xA4'  # U+2524 ┤
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
        return b'\xE2\x94\xBC'  # U+253C ┼
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    51
    return edge
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    53
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    54
def convertedges(line):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
    line = b' %s ' % line
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    56
    pretty = []
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38340
diff changeset
    57
    for idx in pycompat.xrange(len(line) - 2):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    58
        pretty.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    59
            prettyedge(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    60
                line[idx : idx + 1],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    61
                line[idx + 1 : idx + 2],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    62
                line[idx + 2 : idx + 3],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    63
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    64
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    65
    return b''.join(pretty)
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    66
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    67
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    68
def getprettygraphnode(orig, *args, **kwargs):
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    69
    node = orig(*args, **kwargs)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
    if node == b'o':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
        return b'\xE2\x97\x8B'  # U+25CB ○
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
    if node == b'@':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    73
        return b'\xE2\x97\x8D'  # U+25CD ◍
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
    if node == b'*':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
        return b'\xE2\x88\x97'  # U+2217 ∗
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    76
    if node == b'x':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    77
        return b'\xE2\x97\x8C'  # U+25CC ◌
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
    if node == b'_':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
        return b'\xE2\x95\xA4'  # U+2564 ╤
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    80
    return node
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    81
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    82
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    83
def outputprettygraph(orig, ui, graph, *args, **kwargs):
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    84
    (edges, text) = zip(*graph)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    85
    graph = zip([convertedges(e) for e in edges], text)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    86
    return orig(ui, graph, *args, **kwargs)
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    87
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    88
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    89
def extsetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    90
    if ui.plain(b'graph'):
39198
362cb82385ea beautifygraph: don't warn about busted terminal if HGPLAIN is set
Augie Fackler <augie@google.com>
parents: 38340
diff changeset
    91
        return
362cb82385ea beautifygraph: don't warn about busted terminal if HGPLAIN is set
Augie Fackler <augie@google.com>
parents: 38340
diff changeset
    92
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    93
    if encoding.encoding != b'UTF-8':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    94
        ui.warn(_(b'beautifygraph: unsupported encoding, UTF-8 required\n'))
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    95
        return
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
    96
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    97
    if 'A' in encoding._wide:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    98
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
    99
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
                b'beautifygraph: unsupported terminal settings, '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
                b'monospace narrow text required\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
   102
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40457
diff changeset
   103
        )
38340
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
   104
        return
9abe91a503da graph: improve graph output by using Unicode characters
John Stiles <johnstiles@gmail.com>
parents:
diff changeset
   105
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   106
    extensions.wrapfunction(graphmod, b'outputgraph', outputprettygraph)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    extensions.wrapfunction(templatekw, b'getgraphnode', getprettygraphnode)