/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
1 by Scott James Remnant
Commit the first version of bzrk.
1
#!/usr/bin/python
2
# -*- coding: UTF-8 -*-
3
"""Directed graph production.
4
5
This module contains the code to produce an ordered directed graph of a
6
bzr branch, such as we display in the tree view at the top of the bzrk
7
window.
8
"""
9
10
__copyright__ = "Copyright © 2005 Canonical Ltd."
11
__author__    = "Scott James Remnant <scott@ubuntu.com>"
12
13
14
from bzrlib.errors import NoSuchRevision
15
16
17
class DummyRevision(object):
18
    """Dummy bzr revision.
19
20
    Sometimes, especially in older bzr branches, a revision is referenced
21
    as the parent of another but not actually present in the branch's store.
22
    When this happens we use an instance of this class instead of the real
23
    Revision object (which we can't get).
24
    """
25
26
    def __init__(self, revid):
27
        self.revision_id = revid
28
        self.parent_ids = []
29
        self.committer = None
30
        self.message = self.revision_id
31
32
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
33
def distances(branch, start):
34
    """Sort the revisions.
1 by Scott James Remnant
Commit the first version of bzrk.
35
36
    Traverses the branch revision tree starting at start and produces an
37
    ordered list of revisions such that a revision always comes after
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
38
    any revision it is the parent of.
39
40
    Returns a tuple of (revids, revisions, colours, children)
1 by Scott James Remnant
Commit the first version of bzrk.
41
    """
42
    revisions = { start: branch.get_revision(start) }
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
43
    children = { revisions[start]: set() }
1 by Scott James Remnant
Commit the first version of bzrk.
44
    distances = { start: 0 }
45
    colours = { start: 0 }
46
    last_colour = 0
47
48
    # Sort the revisions; the fastest way to do this is to visit each node
49
    # as few times as possible (by keeping the todo list in a set) and record
50
    # the largest distance to it before queuing up the children if we
51
    # increased the distance.  This produces the sort order we desire
52
    todo = set([ start ])
53
    while todo:
54
        revid = todo.pop()
55
        revision = revisions[revid]
56
        distance = distances[revid] + 1
57
        colour = colours[revid]
58
59
        for parent_id in revision.parent_ids:
60
            # Check whether there's any point re-processing this
61
            if parent_id in distances and distances[parent_id] >= distance:
62
                continue
63
64
            # Get the parent from the cache, or put it in the cache
65
            try:
66
                parent = revisions[parent_id]
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
67
                children[parent].add(revision)
1 by Scott James Remnant
Commit the first version of bzrk.
68
            except KeyError:
69
                try:
70
                    parent = revisions[parent_id] \
71
                             = branch.get_revision(parent_id)
72
                except NoSuchRevision:
73
                    parent = revisions[parent_id] = DummyRevision(parent_id)
74
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
75
                children[parent] = set([ revision ])
76
2 by Scott James Remnant
Split the same branch functionality out into a separate function so
77
            # Penalise revisions a little at a fork if we think they're on
78
            # the same branch -- this makes the few few (at least) revisions
79
            # of a branch appear straight after the fork
80
            if same_branch(revision, parent):
81
                colours[parent_id] = colour
82
                if len(revision.parent_ids) > 1:
83
                    distances[parent_id] = distance + 10
84
                else:
85
                    distances[parent_id] = distance
1 by Scott James Remnant
Commit the first version of bzrk.
86
            else:
87
                colours[parent_id] = last_colour = last_colour + 1
2 by Scott James Remnant
Split the same branch functionality out into a separate function so
88
                distances[parent_id] = distance
1 by Scott James Remnant
Commit the first version of bzrk.
89
90
            todo.add(parent_id)
91
3 by Scott James Remnant
Split the display in two with a pane, we'll use the bottom half to show
92
    return ( sorted(distances, key=distances.get), revisions, colours,
93
             children )
94
95
def graph(revids, revisions, colours):
96
    """Produce a directed graph of a bzr branch.
97
98
    For each revision it then yields a tuple of (revision, node, lines).
99
    If the revision is only referenced in the branch and not present in the
100
    store, revision will be a DummyRevision object, otherwise it is the bzr
101
    Revision object with the meta-data for the revision.
102
103
    Node is a tuple of (column, colour) with column being a zero-indexed
104
    column number of the graph that this revision represents and colour
105
    being a zero-indexed colour (which doesn't specify any actual colour
106
    in particular) to draw the node in.
107
108
    Lines is a list of tuples which represent lines you should draw away
109
    from the revision, if you also need to draw lines into the revision
110
    you should use the lines list from the previous iteration.  Each
111
    typle in the list is in the form (start, end, colour) with start and
112
    end being zero-indexed column numbers and colour as in node.
113
114
    It's up to you how to actually draw the nodes and lines (straight,
115
    curved, kinked, etc.) and to pick the actual colours for each index.
116
    """
117
    hanging = revids[:1]
118
    for revid in revids:
1 by Scott James Remnant
Commit the first version of bzrk.
119
        lines = []
120
        node = None
121
122
        new_hanging = []
123
        for h_idx, hang in enumerate(hanging):
124
            if hang == revid:
125
                # We've matched a hanging revision, so need to output a node
126
                # at this point
127
                node = (h_idx, colours[revid])
128
129
                # Now we need to hang its parents, we put them at the point
130
                # the old column was so anything to the right of this has
131
                # to move outwards to make room.  We also try and collapse
132
                # hangs to keep the graph small.
133
                for parent_id in revisions[revid].parent_ids:
134
                    try:
135
                        n_idx = new_hanging.index(parent_id)
136
                    except ValueError:
137
                        n_idx = len(new_hanging)
138
                        new_hanging.append(parent_id)
139
                    lines.append((h_idx, n_idx, colours[parent_id]))
140
            else:
141
                # Revision keeps on hanging, adjust for any change in the
142
                # graph shape and try to collapse hangs to keep the graph
143
                # small.
144
                try:
145
                    n_idx = new_hanging.index(hang)
146
                except ValueError:
147
                    n_idx = len(new_hanging)
148
                    new_hanging.append(hang)
149
                lines.append((h_idx, n_idx, colours[hang]))
150
        hanging = new_hanging
151
152
        yield (revisions[revid], node, lines)
2 by Scott James Remnant
Split the same branch functionality out into a separate function so
153
154
def same_branch(a, b):
155
    """Return whether we think revisions a and b are on the same branch."""
156
    if len(a.parent_ids) == 1:
157
        # Defacto same branch if only parent
158
        return True
159
    elif a.committer == b.committer:
160
        # Same committer so may as well be
161
        return True
162
    else:
163
        return False