mercurial/graphmod.py
author Augie Fackler <raf@durin42.com>
Fri, 10 Jun 2016 00:12:33 -0400
changeset 29341 0d83ad967bf8
parent 29184 09d0022cad83
child 29347 98535ad46fc0
permissions -rw-r--r--
cleanup: replace uses of util.(md5|sha1|sha256|sha512) with hashlib.\1 All versions of Python we support or hope to support make the hash functions available in the same way under the same name, so we may as well drop the util forwards.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     1
# Revision graph generator for Mercurial
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     2
#
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     3
# Copyright 2008 Dirkjan Ochtman <dirkjan@ochtman.nl>
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     4
# Copyright 2007 Joel Rosdahl <joel@rosdahl.net>
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7873
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10084
diff changeset
     7
# GNU General Public License version 2 or any later version.
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
     8
8840
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
     9
"""supports walking the history as DAGs suitable for graphical output
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    10
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    11
The most basic format we use is that of::
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    12
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    13
  (id, type, data, [parentids])
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    14
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    15
The node and parent ids are arbitrary integers which identify a node in the
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    16
context of the graph returned. Type is a constant specifying the node type.
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    17
Data depends on type.
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    18
"""
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    19
25951
69751804f2f5 graphmod: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
    20
from __future__ import absolute_import
8840
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    21
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
    22
import heapq
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
    23
25951
69751804f2f5 graphmod: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
    24
from .node import nullrev
26003
62371c539c89 revset: remove grandparent by using reachableroots
Laurent Charignon <lcharignon@fb.com>
parents: 25951
diff changeset
    25
from . import (
62371c539c89 revset: remove grandparent by using reachableroots
Laurent Charignon <lcharignon@fb.com>
parents: 25951
diff changeset
    26
    revset,
62371c539c89 revset: remove grandparent by using reachableroots
Laurent Charignon <lcharignon@fb.com>
parents: 25951
diff changeset
    27
    util,
62371c539c89 revset: remove grandparent by using reachableroots
Laurent Charignon <lcharignon@fb.com>
parents: 25951
diff changeset
    28
)
25951
69751804f2f5 graphmod: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24180
diff changeset
    29
8840
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
    30
CHANGESET = 'C'
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
    31
PARENT = 'P'
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
    32
GRANDPARENT = 'G'
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
    33
MISSINGPARENT = 'M'
28601
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
    34
# Style of line to draw. None signals a line that ends and is removed at this
29134
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
    35
# point. A number prefix means only the last N characters of the current block
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
    36
# will use that style, the rest will use the PARENT style. Add a - sign
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
    37
# (so making N negative) and all but the first N characters use that style.
28627
d7af9b4ae7dd graphmod: set default edge styles for ascii graphs (BC)
Martijn Pieters <mjpieters@fb.com>
parents: 28601
diff changeset
    38
EDGES = {PARENT: '|', GRANDPARENT: ':', MISSINGPARENT: None}
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
    39
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
    40
def groupbranchiter(revs, parentsfunc, firstbranch=()):
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    41
    """Yield revisions from heads to roots one (topo) branch at a time.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    42
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    43
    This function aims to be used by a graph generator that wishes to minimize
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    44
    the number of parallel branches and their interleaving.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    45
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    46
    Example iteration order (numbers show the "true" order in a changelog):
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    47
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    48
      o  4
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    49
      |
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    50
      o  1
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    51
      |
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    52
      | o  3
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    53
      | |
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    54
      | o  2
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    55
      |/
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    56
      o  0
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    57
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    58
    Note that the ancestors of merges are understood by the current
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    59
    algorithm to be on the same branch. This means no reordering will
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    60
    occur behind a merge.
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
    61
    """
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    62
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    63
    ### Quick summary of the algorithm
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    64
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    65
    # This function is based around a "retention" principle. We keep revisions
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    66
    # in memory until we are ready to emit a whole branch that immediately
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    67
    # "merges" into an existing one. This reduces the number of parallel
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    68
    # branches with interleaved revisions.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    69
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    70
    # During iteration revs are split into two groups:
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    71
    # A) revision already emitted
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    72
    # B) revision in "retention". They are stored as different subgroups.
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    73
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    74
    # for each REV, we do the following logic:
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    75
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    76
    #   1) if REV is a parent of (A), we will emit it. If there is a
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    77
    #   retention group ((B) above) that is blocked on REV being
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    78
    #   available, we emit all the revisions out of that retention
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    79
    #   group first.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    80
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    81
    #   2) else, we'll search for a subgroup in (B) awaiting for REV to be
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    82
    #   available, if such subgroup exist, we add REV to it and the subgroup is
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    83
    #   now awaiting for REV.parents() to be available.
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    84
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    85
    #   3) finally if no such group existed in (B), we create a new subgroup.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    86
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    87
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    88
    # To bootstrap the algorithm, we emit the tipmost revision (which
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    89
    # puts it in group (A) from above).
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    90
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    91
    revs.sort(reverse=True)
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    92
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    93
    # Set of parents of revision that have been emitted. They can be considered
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
    94
    # unblocked as the graph generator is already aware of them so there is no
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    95
    # need to delay the revisions that reference them.
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
    96
    #
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
    97
    # If someone wants to prioritize a branch over the others, pre-filling this
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
    98
    # set will force all other branches to wait until this branch is ready to be
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
    99
    # emitted.
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
   100
    unblocked = set(firstbranch)
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   101
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   102
    # list of groups waiting to be displayed, each group is defined by:
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   103
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   104
    #   (revs:    lists of revs waiting to be displayed,
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   105
    #    blocked: set of that cannot be displayed before those in 'revs')
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   106
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   107
    # The second value ('blocked') correspond to parents of any revision in the
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   108
    # group ('revs') that is not itself contained in the group. The main idea
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   109
    # of this algorithm is to delay as much as possible the emission of any
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   110
    # revision.  This means waiting for the moment we are about to display
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   111
    # these parents to display the revs in a group.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   112
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   113
    # This first implementation is smart until it encounters a merge: it will
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   114
    # emit revs as soon as any parent is about to be emitted and can grow an
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   115
    # arbitrary number of revs in 'blocked'. In practice this mean we properly
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   116
    # retains new branches but gives up on any special ordering for ancestors
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   117
    # of merges. The implementation can be improved to handle this better.
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   118
    #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   119
    # The first subgroup is special. It corresponds to all the revision that
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   120
    # were already emitted. The 'revs' lists is expected to be empty and the
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   121
    # 'blocked' set contains the parents revisions of already emitted revision.
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   122
    #
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   123
    # You could pre-seed the <parents> set of groups[0] to a specific
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   124
    # changesets to select what the first emitted branch should be.
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   125
    groups = [([], unblocked)]
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   126
    pendingheap = []
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   127
    pendingset = set()
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   128
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   129
    heapq.heapify(pendingheap)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   130
    heappop = heapq.heappop
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   131
    heappush = heapq.heappush
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   132
    for currentrev in revs:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   133
        # Heap works with smallest element, we want highest so we invert
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   134
        if currentrev not in pendingset:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   135
            heappush(pendingheap, -currentrev)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   136
            pendingset.add(currentrev)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   137
        # iterates on pending rev until after the current rev have been
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23570
diff changeset
   138
        # processed.
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   139
        rev = None
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   140
        while rev != currentrev:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   141
            rev = -heappop(pendingheap)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   142
            pendingset.remove(rev)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   143
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   144
            # Seek for a subgroup blocked, waiting for the current revision.
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   145
            matching = [i for i, g in enumerate(groups) if rev in g[1]]
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   146
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   147
            if matching:
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   148
                # The main idea is to gather together all sets that are blocked
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   149
                # on the same revision.
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   150
                #
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   151
                # Groups are merged when a common blocking ancestor is
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   152
                # observed. For example, given two groups:
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   153
                #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   154
                # revs [5, 4] waiting for 1
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   155
                # revs [3, 2] waiting for 1
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   156
                #
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   157
                # These two groups will be merged when we process
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   158
                # 1. In theory, we could have merged the groups when
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   159
                # we added 2 to the group it is now in (we could have
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   160
                # noticed the groups were both blocked on 1 then), but
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   161
                # the way it works now makes the algorithm simpler.
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   162
                #
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   163
                # We also always keep the oldest subgroup first. We can
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   164
                # probably improve the behavior by having the longest set
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   165
                # first. That way, graph algorithms could minimise the length
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   166
                # of parallel lines their drawing. This is currently not done.
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   167
                targetidx = matching.pop(0)
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   168
                trevs, tparents = groups[targetidx]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   169
                for i in matching:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   170
                    gr = groups[i]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   171
                    trevs.extend(gr[0])
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   172
                    tparents |= gr[1]
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   173
                # delete all merged subgroups (except the one we kept)
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   174
                # (starting from the last subgroup for performance and
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   175
                # sanity reasons)
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   176
                for i in reversed(matching):
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   177
                    del groups[i]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   178
            else:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   179
                # This is a new head. We create a new subgroup for it.
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   180
                targetidx = len(groups)
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   181
                groups.append(([], set([rev])))
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   182
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   183
            gr = groups[targetidx]
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   184
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   185
            # We now add the current nodes to this subgroups. This is done
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   186
            # after the subgroup merging because all elements from a subgroup
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   187
            # that relied on this rev must precede it.
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   188
            #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   189
            # we also update the <parents> set to include the parents of the
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   190
            # new nodes.
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   191
            if rev == currentrev: # only display stuff in rev
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   192
                gr[0].append(rev)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   193
            gr[1].remove(rev)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   194
            parents = [p for p in parentsfunc(rev) if p > nullrev]
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   195
            gr[1].update(parents)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   196
            for p in parents:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   197
                if p not in pendingset:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   198
                    pendingset.add(p)
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   199
                    heappush(pendingheap, -p)
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   200
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   201
            # Look for a subgroup to display
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   202
            #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   203
            # When unblocked is empty (if clause), we were not waiting for any
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   204
            # revisions during the first iteration (if no priority was given) or
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   205
            # if we emitted a whole disconnected set of the graph (reached a
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   206
            # root).  In that case we arbitrarily take the oldest known
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   207
            # subgroup. The heuristic could probably be better.
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   208
            #
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   209
            # Otherwise (elif clause) if the subgroup is blocked on
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   210
            # a revision we just emitted, we can safely emit it as
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   211
            # well.
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   212
            if not unblocked:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   213
                if len(groups) > 1:  # display other subset
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   214
                    targetidx = 1
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   215
                    gr = groups[1]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   216
            elif not gr[1] & unblocked:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   217
                gr = None
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   218
23566
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   219
            if gr is not None:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   220
                # update the set of awaited revisions with the one from the
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   221
                # subgroup
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   222
                unblocked |= gr[1]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   223
                # output all revisions in the subgroup
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   224
                for r in gr[0]:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   225
                    yield r
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   226
                # delete the subgroup that you just output
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   227
                # unless it is groups[0] in which case you just empty it.
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   228
                if targetidx:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   229
                    del groups[targetidx]
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   230
                else:
fee7a30cfdf5 groubranchhiter: indent most of the inner code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23565
diff changeset
   231
                    gr[0][:] = []
23570
3f86fe9bcef0 graphmod: attempt to clarify documentation of groupbranchiter()
Augie Fackler <raf@durin42.com>
parents: 23569
diff changeset
   232
    # Check if we have some subgroup waiting for revisions we are not going to
23567
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   233
    # iterate over
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   234
    for g in groups:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   235
        for r in g[0]:
1f080c9c6a35 groupbranchiter: support for non-contiguous revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23566
diff changeset
   236
            yield r
23564
f7ce0837eefd graphmod: add a function for topological iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23006
diff changeset
   237
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   238
def dagwalker(repo, revs):
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   239
    """cset DAG generator yielding (id, CHANGESET, ctx, [parentinfo]) tuples
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   240
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   241
    This generator function walks through revisions (which should be ordered
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   242
    from bigger to lower). It returns a tuple for each node.
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   243
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   244
    Each parentinfo entry is a tuple with (edgetype, parentid), where edgetype
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   245
    is one of PARENT, GRANDPARENT or MISSINGPARENT. The node and parent ids
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   246
    are arbitrary integers which identify a node in the context of the graph
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   247
    returned.
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   248
8836
11ff34956ee7 graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8835
diff changeset
   249
    """
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   250
    if not revs:
14087
f3d585c9b042 graphmod: restore generator nature of dagwalker
Idan Kamara <idankk86@gmail.com>
parents: 14064
diff changeset
   251
        return
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   252
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   253
    gpcache = {}
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   254
23569
3ecbcffdeb0c graphmod: rename graph-topological config to graph-group-branches
Augie Fackler <raf@durin42.com>
parents: 23568
diff changeset
   255
    if repo.ui.configbool('experimental', 'graph-group-branches', False):
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
   256
        firstbranch = ()
23569
3ecbcffdeb0c graphmod: rename graph-topological config to graph-group-branches
Augie Fackler <raf@durin42.com>
parents: 23568
diff changeset
   257
        firstbranchrevset = repo.ui.config(
3ecbcffdeb0c graphmod: rename graph-topological config to graph-group-branches
Augie Fackler <raf@durin42.com>
parents: 23568
diff changeset
   258
            'experimental', 'graph-group-branches.firstbranch', '')
23568
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
   259
        if firstbranchrevset:
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
   260
            firstbranch = repo.revs(firstbranchrevset)
740ae54573a3 groupbranchiter: allow callers to select the first branch
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23567
diff changeset
   261
        parentrevs = repo.changelog.parentrevs
26092
014044dbd4e8 graphmod: stop building a list out of branchgroupiter
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26003
diff changeset
   262
        revs = groupbranchiter(revs, parentrevs, firstbranch)
014044dbd4e8 graphmod: stop building a list out of branchgroupiter
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26003
diff changeset
   263
        revs = revset.baseset(revs)
23565
996c01bfbec4 graphlog: add a way to test the 'groupbranchiter' function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23564
diff changeset
   264
14087
f3d585c9b042 graphmod: restore generator nature of dagwalker
Idan Kamara <idankk86@gmail.com>
parents: 14064
diff changeset
   265
    for rev in revs:
f3d585c9b042 graphmod: restore generator nature of dagwalker
Idan Kamara <idankk86@gmail.com>
parents: 14064
diff changeset
   266
        ctx = repo[rev]
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   267
        # partition into parents in the rev set and missing parents, then
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   268
        # augment the lists with markers, to inform graph drawing code about
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   269
        # what kind of edge to draw between nodes.
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   270
        pset = set(p.rev() for p in ctx.parents() if p.rev() in revs)
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   271
        mpars = [p.rev() for p in ctx.parents()
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   272
                 if p.rev() != nullrev and p.rev() not in pset]
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   273
        parents = [(PARENT, p) for p in sorted(pset)]
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   274
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   275
        for mpar in mpars:
14131
03e1c2d35c6a graphmod: correctly emit nodes with more than 2 predecessors
Patrick Mezard <pmezard@gmail.com>
parents: 14088
diff changeset
   276
            gp = gpcache.get(mpar)
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   277
            if gp is None:
26187
9cf65f43b49b graphmod: compute slow revset query once prior to reachableroots (issue4782)
Yuya Nishihara <yuya@tcha.org>
parents: 26092
diff changeset
   278
                # precompute slow query as we know reachableroots() goes
9cf65f43b49b graphmod: compute slow revset query once prior to reachableroots (issue4782)
Yuya Nishihara <yuya@tcha.org>
parents: 26092
diff changeset
   279
                # through all revs (issue4782)
9cf65f43b49b graphmod: compute slow revset query once prior to reachableroots (issue4782)
Yuya Nishihara <yuya@tcha.org>
parents: 26092
diff changeset
   280
                if not isinstance(revs, revset.baseset):
9cf65f43b49b graphmod: compute slow revset query once prior to reachableroots (issue4782)
Yuya Nishihara <yuya@tcha.org>
parents: 26092
diff changeset
   281
                    revs = revset.baseset(revs)
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   282
                gp = gpcache[mpar] = sorted(set(revset.reachableroots(
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   283
                    repo, revs, [mpar])))
14131
03e1c2d35c6a graphmod: correctly emit nodes with more than 2 predecessors
Patrick Mezard <pmezard@gmail.com>
parents: 14088
diff changeset
   284
            if not gp:
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   285
                parents.append((MISSINGPARENT, mpar))
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   286
                pset.add(mpar)
14131
03e1c2d35c6a graphmod: correctly emit nodes with more than 2 predecessors
Patrick Mezard <pmezard@gmail.com>
parents: 14088
diff changeset
   287
            else:
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   288
                parents.extend((GRANDPARENT, g) for g in gp if g not in pset)
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   289
                pset.update(gp)
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   290
14087
f3d585c9b042 graphmod: restore generator nature of dagwalker
Idan Kamara <idankk86@gmail.com>
parents: 14064
diff changeset
   291
        yield (ctx.rev(), CHANGESET, ctx, parents)
8836
11ff34956ee7 graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8835
diff changeset
   292
8837
d8e3a98018cb graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8836
diff changeset
   293
def nodes(repo, nodes):
8840
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   294
    """cset DAG generator yielding (id, CHANGESET, ctx, [parentids]) tuples
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   295
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   296
    This generator function walks the given nodes. It only returns parents
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   297
    that are in nodes, too.
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   298
    """
8837
d8e3a98018cb graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8836
diff changeset
   299
    include = set(nodes)
d8e3a98018cb graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8836
diff changeset
   300
    for node in nodes:
d8e3a98018cb graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8836
diff changeset
   301
        ctx = repo[node]
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   302
        parents = set((PARENT, p.rev()) for p in ctx.parents()
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   303
                      if p.node() in include)
8840
d9acbe7b0049 graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8837
diff changeset
   304
        yield (ctx.rev(), CHANGESET, ctx, sorted(parents))
8837
d8e3a98018cb graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8836
diff changeset
   305
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   306
def colored(dag, repo):
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   307
    """annotates a DAG with colored edge information
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   308
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   309
    For each DAG node this function emits tuples::
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   310
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   311
      (id, type, data, (col, color), [(col, nextcol, color)])
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   312
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   313
    with the following new elements:
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   314
8835
ec5483efc31f graphmod: code cleanup and doc fix
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8225
diff changeset
   315
      - Tuple (col, color) with column and color index for the current node
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   316
      - A list of tuples indicating the edges between the current node and its
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   317
        parents.
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   318
    """
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   319
    seen = []
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   320
    colors = {}
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   321
    newcolor = 1
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   322
    config = {}
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   323
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   324
    for key, val in repo.ui.configitems('graph'):
16131
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   325
        if '.' in key:
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   326
            branch, setting = key.rsplit('.', 1)
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   327
            # Validation
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   328
            if setting == "width" and val.isdigit():
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   329
                config.setdefault(branch, {})[setting] = int(val)
16131
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   330
            elif setting == "color" and val.isalnum():
6f236c8bdc01 graphmod: rewrite graph config validation
Matt Mackall <mpm@selenic.com>
parents: 16130
diff changeset
   331
                config.setdefault(branch, {})[setting] = val
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   332
16132
41fc1e078d68 graphmod: add config cache
Matt Mackall <mpm@selenic.com>
parents: 16131
diff changeset
   333
    if config:
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   334
        getconf = util.lrucachefunc(
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   335
            lambda rev: config.get(repo[rev].branch(), {}))
16132
41fc1e078d68 graphmod: add config cache
Matt Mackall <mpm@selenic.com>
parents: 16131
diff changeset
   336
    else:
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   337
        getconf = lambda rev: {}
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   338
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   339
    for (cur, type, data, parents) in dag:
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   340
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   341
        # Compute seen and next
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   342
        if cur not in seen:
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   343
            seen.append(cur) # new head
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   344
            colors[cur] = newcolor
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   345
            newcolor += 1
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   346
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   347
        col = seen.index(cur)
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   348
        color = colors.pop(cur)
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   349
        next = seen[:]
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   350
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   351
        # Add parents to next
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   352
        addparents = [p for pt, p in parents if p not in next]
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   353
        next[col:col + 1] = addparents
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   354
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   355
        # Set colors for the parents
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   356
        for i, p in enumerate(addparents):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   357
            if not i:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   358
                colors[p] = color
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   359
            else:
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   360
                colors[p] = newcolor
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   361
                newcolor += 1
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   362
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   363
        # Add edges to the graph
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   364
        edges = []
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   365
        for ecol, eid in enumerate(seen):
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   366
            if eid in next:
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   367
                bconf = getconf(eid)
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   368
                edges.append((
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   369
                    ecol, next.index(eid), colors[eid],
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   370
                    bconf.get('width', -1),
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   371
                    bconf.get('color', '')))
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   372
            elif eid == cur:
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   373
                for ptype, p in parents:
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   374
                    bconf = getconf(p)
16129
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   375
                    edges.append((
5e50982c633c graph: in hgrc specify line width for main branch
Constantine Linnick <theaspect@gmail.com>
parents: 14131
diff changeset
   376
                        ecol, next.index(p), color,
16138
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   377
                        bconf.get('width', -1),
6e4de55a41a4 hgweb: refactor graph customization javascript
Patrick Mezard <patrick@mezard.eu>
parents: 16132
diff changeset
   378
                        bconf.get('color', '')))
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   379
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
   380
        # Yield and move on
8842
acd03a6e2426 graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8841
diff changeset
   381
        yield (cur, type, data, (col, color), edges)
8841
94ac080e7af9 graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 8840
diff changeset
   382
        seen = next
14042
9966c95b8c4f graphmod: use revsets internally
Alexander Solovyov <alexander@solovyov.net>
parents: 12951
diff changeset
   383
28375
97cb1aeaca78 graphmod: refactor state handling
Martijn Pieters <mjpieters@fb.com>
parents: 26187
diff changeset
   384
def asciiedges(type, char, lines, state, rev, parents):
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   385
    """adds edge info to changelog DAG walk suitable for ascii()"""
28375
97cb1aeaca78 graphmod: refactor state handling
Martijn Pieters <mjpieters@fb.com>
parents: 26187
diff changeset
   386
    seen = state['seen']
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   387
    if rev not in seen:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   388
        seen.append(rev)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   389
    nodeidx = seen.index(rev)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   390
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   391
    knownparents = []
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   392
    newparents = []
28376
fa2cd0c9a567 graphmod: augment the graph to include more information about the edges
Martijn Pieters <mjpieters@fb.com>
parents: 28375
diff changeset
   393
    for ptype, parent in parents:
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   394
        if parent in seen:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   395
            knownparents.append(parent)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   396
        else:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   397
            newparents.append(parent)
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   398
            state['edges'][parent] = state['styles'].get(ptype, '|')
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   399
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   400
    ncols = len(seen)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   401
    nextseen = seen[:]
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   402
    nextseen[nodeidx:nodeidx + 1] = newparents
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   403
    edges = [(nodeidx, nextseen.index(p))
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   404
             for p in knownparents if p != nullrev]
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   405
28998
f303b569134c graphmod: fix seen state handling for > 2 parents (issue5174)
Martijn Pieters <mjpieters@fb.com>
parents: 28891
diff changeset
   406
    seen[:] = nextseen
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   407
    while len(newparents) > 2:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   408
        # ascii() only knows how to add or remove a single column between two
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   409
        # calls. Nodes with more than two parents break this constraint so we
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   410
        # introduce intermediate expansion lines to grow the active node list
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   411
        # slowly.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   412
        edges.append((nodeidx, nodeidx))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   413
        edges.append((nodeidx, nodeidx + 1))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   414
        nmorecols = 1
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   415
        yield (type, char, lines, (nodeidx, edges, ncols, nmorecols))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   416
        char = '\\'
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   417
        lines = []
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   418
        nodeidx += 1
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   419
        ncols += 1
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   420
        edges = []
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   421
        del newparents[0]
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   422
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   423
    if len(newparents) > 0:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   424
        edges.append((nodeidx, nodeidx))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   425
    if len(newparents) > 1:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   426
        edges.append((nodeidx, nodeidx + 1))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   427
    nmorecols = len(nextseen) - ncols
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   428
    # remove current node from edge characters, no longer needed
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   429
    state['edges'].pop(rev, None)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   430
    yield (type, char, lines, (nodeidx, edges, ncols, nmorecols))
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   431
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   432
def _fixlongrightedges(edges):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   433
    for (i, (start, end)) in enumerate(edges):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   434
        if end > start:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   435
            edges[i] = (start, end + 1)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   436
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   437
def _getnodelineedgestail(
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   438
        echars, idx, pidx, ncols, coldiff, pdiff, fix_tail):
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   439
    if fix_tail and coldiff == pdiff and coldiff != 0:
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   440
        # Still going in the same non-vertical direction.
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   441
        if coldiff == -1:
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   442
            start = max(idx + 1, pidx)
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   443
            tail = echars[idx * 2:(start - 1) * 2]
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   444
            tail.extend(["/", " "] * (ncols - start))
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   445
            return tail
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   446
        else:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   447
            return ["\\", " "] * (ncols - idx - 1)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   448
    else:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   449
        remainder = (ncols - idx - 1)
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   450
        return echars[-(remainder * 2):] if remainder > 0 else []
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   451
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   452
def _drawedges(echars, edges, nodeline, interline):
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   453
    for (start, end) in edges:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   454
        if start == end + 1:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   455
            interline[2 * end + 1] = "/"
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   456
        elif start == end - 1:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   457
            interline[2 * start + 1] = "\\"
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   458
        elif start == end:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   459
            interline[2 * start] = echars[2 * start]
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   460
        else:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   461
            if 2 * end >= len(nodeline):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   462
                continue
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   463
            nodeline[2 * end] = "+"
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   464
            if start > end:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   465
                (start, end) = (end, start)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   466
            for i in range(2 * start + 1, 2 * end):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   467
                if nodeline[i] != "+":
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   468
                    nodeline[i] = "-"
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   469
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   470
def _getpaddingline(echars, idx, ncols, edges):
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   471
    # all edges up to the current node
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   472
    line = echars[:idx * 2]
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   473
    # an edge for the current node, if there is one
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   474
    if (idx, idx - 1) in edges or (idx, idx) in edges:
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   475
        # (idx, idx - 1)      (idx, idx)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   476
        # | | | |           | | | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   477
        # +---o |           | o---+
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   478
        # | | X |           | X | |
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   479
        # | |/ /            | |/ /
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   480
        # | | |             | | |
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   481
        line.extend(echars[idx * 2:(idx + 1) * 2])
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   482
    else:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   483
        line.extend('  ')
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   484
    # all edges to the right of the current node
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   485
    remainder = ncols - idx - 1
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   486
    if remainder > 0:
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   487
        line.extend(echars[-(remainder * 2):])
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   488
    return line
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   489
28601
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   490
def _drawendinglines(lines, extra, edgemap, seen):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   491
    """Draw ending lines for missing parent edges
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   492
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   493
    None indicates an edge that ends at between this node and the next
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   494
    Replace with a short line ending in ~ and add / lines to any edges to
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   495
    the right.
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   496
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   497
    """
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   498
    if None not in edgemap.values():
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   499
        return
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   500
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   501
    # Check for more edges to the right of our ending edges.
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   502
    # We need enough space to draw adjustment lines for these.
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   503
    edgechars = extra[::2]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   504
    while edgechars and edgechars[-1] is None:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   505
        edgechars.pop()
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   506
    shift_size = max((edgechars.count(None) * 2) - 1, 0)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   507
    while len(lines) < 3 + shift_size:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   508
        lines.append(extra[:])
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   509
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   510
    if shift_size:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   511
        empties = []
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   512
        toshift = []
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   513
        first_empty = extra.index(None)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   514
        for i, c in enumerate(extra[first_empty::2], first_empty // 2):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   515
            if c is None:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   516
                empties.append(i * 2)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   517
            else:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   518
                toshift.append(i * 2)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   519
        targets = list(range(first_empty, first_empty + len(toshift) * 2, 2))
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   520
        positions = toshift[:]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   521
        for line in lines[-shift_size:]:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   522
            line[first_empty:] = [' '] * (len(line) - first_empty)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   523
            for i in range(len(positions)):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   524
                pos = positions[i] - 1
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   525
                positions[i] = max(pos, targets[i])
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   526
                line[pos] = '/' if pos > targets[i] else extra[toshift[i]]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   527
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   528
    map = {1: '|', 2: '~'}
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   529
    for i, line in enumerate(lines):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   530
        if None not in line:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   531
            continue
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   532
        line[:] = [c or map.get(i, ' ') for c in line]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   533
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   534
    # remove edges that ended
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   535
    remove = [p for p, c in edgemap.items() if c is None]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   536
    for parent in remove:
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   537
        del edgemap[parent]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   538
        seen.remove(parent)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   539
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   540
def asciistate():
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   541
    """returns the initial value for the "state" argument to ascii()"""
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   542
    return {
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   543
        'seen': [],
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   544
        'edges': {},
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   545
        'lastcoldiff': 0,
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   546
        'lastindex': 0,
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   547
        'styles': EDGES.copy(),
28891
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   548
        'graphshorten': False,
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   549
    }
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   550
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   551
def ascii(ui, state, type, char, text, coldata):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   552
    """prints an ASCII graph of the DAG
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   553
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   554
    takes the following arguments (one call per node in the graph):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   555
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   556
      - ui to write to
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   557
      - Somewhere to keep the needed state in (init to asciistate())
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   558
      - Column of the current node in the set of ongoing edges.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   559
      - Type indicator of node data, usually 'C' for changesets.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   560
      - Payload: (char, lines):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   561
        - Character to use as node's symbol.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   562
        - List of lines to display as the node's text.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   563
      - Edges; a list of (col, next_col) indicating the edges between
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   564
        the current node and its parents.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   565
      - Number of columns (ongoing edges) in the current revision.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   566
      - The difference between the number of columns (ongoing edges)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   567
        in the next revision and the number of columns (ongoing edges)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   568
        in the current revision. That is: -1 means one column removed;
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   569
        0 means no columns added or removed; 1 means one column added.
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   570
    """
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   571
    idx, edges, ncols, coldiff = coldata
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   572
    assert -2 < coldiff < 2
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   573
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   574
    edgemap, seen = state['edges'], state['seen']
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   575
    # Be tolerant of history issues; make sure we have at least ncols + coldiff
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   576
    # elements to work with. See test-glog.t for broken history test cases.
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   577
    echars = [c for p in seen for c in (edgemap.get(p, '|'), ' ')]
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   578
    echars.extend(('|', ' ') * max(ncols + coldiff - len(seen), 0))
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   579
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   580
    if coldiff == -1:
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   581
        # Transform
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   582
        #
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   583
        #     | | |        | | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   584
        #     o | |  into  o---+
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   585
        #     |X /         |/ /
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   586
        #     | |          | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   587
        _fixlongrightedges(edges)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   588
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   589
    # add_padding_line says whether to rewrite
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   590
    #
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   591
    #     | | | |        | | | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   592
    #     | o---+  into  | o---+
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   593
    #     |  / /         |   | |  # <--- padding line
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   594
    #     o | |          |  / /
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   595
    #                    o | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   596
    add_padding_line = (len(text) > 2 and coldiff == -1 and
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   597
                        [x for (x, y) in edges if x + 1 < y])
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   598
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   599
    # fix_nodeline_tail says whether to rewrite
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   600
    #
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   601
    #     | | o | |        | | o | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   602
    #     | | |/ /         | | |/ /
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   603
    #     | o | |    into  | o / /   # <--- fixed nodeline tail
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   604
    #     | |/ /           | |/ /
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   605
    #     o | |            o | |
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   606
    fix_nodeline_tail = len(text) <= 2 and not add_padding_line
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   607
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   608
    # nodeline is the line containing the node character (typically o)
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   609
    nodeline = echars[:idx * 2]
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   610
    nodeline.extend([char, " "])
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   611
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   612
    nodeline.extend(
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   613
        _getnodelineedgestail(
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   614
            echars, idx, state['lastindex'], ncols, coldiff,
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   615
            state['lastcoldiff'], fix_nodeline_tail))
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   616
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   617
    # shift_interline is the line containing the non-vertical
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   618
    # edges between this entry and the next
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   619
    shift_interline = echars[:idx * 2]
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   620
    shift_interline.extend(' ' * (2 + coldiff))
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   621
    count = ncols - idx - 1
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   622
    if coldiff == -1:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   623
        shift_interline.extend('/ ' * count)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   624
    elif coldiff == 0:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   625
        shift_interline.extend(echars[(idx + 1) * 2:ncols * 2])
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   626
    else:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   627
        shift_interline.extend(r'\ ' * count)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   628
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   629
    # draw edges from the current node to its parents
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   630
    _drawedges(echars, edges, nodeline, shift_interline)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   631
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   632
    # lines is the list of all graph lines to print
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   633
    lines = [nodeline]
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   634
    if add_padding_line:
28600
0d6137891114 graphmod: allow for different styles for different edge types
Martijn Pieters <mjpieters@fb.com>
parents: 28376
diff changeset
   635
        lines.append(_getpaddingline(echars, idx, ncols, edges))
28891
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   636
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   637
    # If 'graphshorten' config, only draw shift_interline
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   638
    # when there is any non vertical flow in graph.
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   639
    if state['graphshorten']:
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   640
        if any(c in '\/' for c in shift_interline if c):
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   641
            lines.append(shift_interline)
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   642
    # Else, no 'graphshorten' config so draw shift_interline.
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   643
    else:
ac30adb260ea graphmod: shorten graph
santiagopim <santiagopim@gmail.com>
parents: 28627
diff changeset
   644
        lines.append(shift_interline)
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   645
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   646
    # make sure that there are as many graph lines as there are
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   647
    # log strings
28601
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   648
    extra_interline = echars[:(ncols + coldiff) * 2]
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   649
    if len(lines) < len(text):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   650
        while len(lines) < len(text):
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   651
            lines.append(extra_interline[:])
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   652
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   653
    _drawendinglines(lines, extra_interline, edgemap, seen)
cd10171d6c71 graphmod: allow edges to end early
Martijn Pieters <mjpieters@fb.com>
parents: 28600
diff changeset
   654
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   655
    while len(text) < len(lines):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   656
        text.append("")
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   657
29134
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   658
    if any(len(char) > 1 for char in edgemap.values()):
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   659
        # limit drawing an edge to the first or last N lines of the current
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   660
        # section the rest of the edge is drawn like a parent line.
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   661
        parent = state['styles'][PARENT][-1]
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   662
        def _drawgp(char, i):
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   663
            # should a grandparent character be drawn for this line?
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   664
            if len(char) < 2:
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   665
                return True
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   666
            num = int(char[:-1])
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   667
            # either skip first num lines or take last num lines, based on sign
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   668
            return -num <= i if num < 0 else (len(lines) - i) <= num
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   669
        for i, line in enumerate(lines):
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   670
            line[:] = [c[-1] if _drawgp(c, i) else parent for c in line]
29184
09d0022cad83 graphmod: update edgemap in-place
Martijn Pieters <mjpieters@fb.com>
parents: 29134
diff changeset
   671
        edgemap.update(
09d0022cad83 graphmod: update edgemap in-place
Martijn Pieters <mjpieters@fb.com>
parents: 29134
diff changeset
   672
            (e, (c if len(c) < 2 else parent)) for e, c in edgemap.items())
29134
8d5584d8345b graphmod: partial edge styling
Martijn Pieters <mjpieters@fb.com>
parents: 28998
diff changeset
   673
17179
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   674
    # print lines
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   675
    indentation_level = max(ncols, ncols + coldiff)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   676
    for (line, logstr) in zip(lines, text):
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   677
        ln = "%-*s %s" % (2 * indentation_level, "".join(line), logstr)
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   678
        ui.write(ln.rstrip() + '\n')
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   679
0849d725e2f9 graphlog: extract ascii drawing code into graphmod
Patrick Mezard <patrick@mezard.eu>
parents: 16138
diff changeset
   680
    # ... and start over
28375
97cb1aeaca78 graphmod: refactor state handling
Martijn Pieters <mjpieters@fb.com>
parents: 26187
diff changeset
   681
    state['lastcoldiff'] = coldiff
97cb1aeaca78 graphmod: refactor state handling
Martijn Pieters <mjpieters@fb.com>
parents: 26187
diff changeset
   682
    state['lastindex'] = idx