2
# -*- coding: UTF-8 -*-
3
"""Directed graph production.
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
10
__copyright__ = "Copyright © 2005 Canonical Ltd."
11
__author__ = "Scott James Remnant <scott@ubuntu.com>"
14
from bzrlib.errors import NoSuchRevision
17
class DummyRevision(object):
18
"""Dummy bzr revision.
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).
26
def __init__(self, revid):
27
self.revision_id = revid
30
self.message = self.revision_id
33
def distances(branch, start):
34
"""Sort the revisions.
36
Traverses the branch revision tree starting at start and produces an
37
ordered list of revisions such that a revision always comes after
38
any revision it is the parent of.
40
Returns a tuple of (revids, revisions, colours, children)
42
revisions = { start: branch.get_revision(start) }
43
children = { revisions[start]: set() }
44
distances = { start: 0 }
45
colours = { start: 0 }
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
55
revision = revisions[revid]
56
distance = distances[revid] + 1
57
colour = colours[revid]
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:
64
# Get the parent from the cache, or put it in the cache
66
parent = revisions[parent_id]
67
children[parent].add(revision)
70
parent = revisions[parent_id] \
71
= branch.get_revision(parent_id)
72
except NoSuchRevision:
73
parent = revisions[parent_id] = DummyRevision(parent_id)
75
children[parent] = set([ revision ])
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
85
distances[parent_id] = distance
87
colours[parent_id] = last_colour = last_colour + 1
88
distances[parent_id] = distance
92
return ( sorted(distances, key=distances.get), revisions, colours,
95
def graph(revids, revisions, colours):
96
"""Produce a directed graph of a bzr branch.
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.
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.
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.
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.
123
for h_idx, hang in enumerate(hanging):
125
# We've matched a hanging revision, so need to output a node
127
node = (h_idx, colours[revid])
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:
135
n_idx = new_hanging.index(parent_id)
137
n_idx = len(new_hanging)
138
new_hanging.append(parent_id)
139
lines.append((h_idx, n_idx, colours[parent_id]))
141
# Revision keeps on hanging, adjust for any change in the
142
# graph shape and try to collapse hangs to keep the graph
145
n_idx = new_hanging.index(hang)
147
n_idx = len(new_hanging)
148
new_hanging.append(hang)
149
lines.append((h_idx, n_idx, colours[hang]))
150
hanging = new_hanging
152
yield (revisions[revid], node, lines)
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
159
elif a.committer == b.committer:
160
# Same committer so may as well be