1
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
2
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
3
from bzrlib.branch import Branch
4
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
5
from bzrlib.fetch import greedy_fetch
6
from bzrlib.graph import node_distances, select_farthest
7
from bzrlib.revision import combined_graph, revision_graph
8
from bzrlib.revision import MultipleRevisionSources
14
mail_map = {'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
15
'abentley@panoramicfeedback.com': 'Aaron Bentley',
16
'abentley@lappy' : 'Aaron Bentley',
17
'john@arbash-meinel.com' : 'John Arbash Meinel',
18
'mbp@sourcefrog.net' : 'Martin Pool',
19
'robertc@robertcollins.net' : 'Robert Collins',
22
committer_alias = {'abentley': 'Aaron Bentley'}
23
def short_committer(committer):
24
new_committer = re.sub('<.*>', '', committer).strip(' ')
25
if len(new_committer) < 2:
29
def can_skip(rev_id, descendants, ancestors):
30
if rev_id not in descendants:
32
elif rev_id not in ancestors:
34
elif len(ancestors[rev_id]) != 1:
36
elif len(descendants[list(ancestors[rev_id])[0]]) != 1:
38
elif len(descendants[rev_id]) != 1:
43
def compact_ancestors(descendants, ancestors, exceptions=()):
46
for me, my_parents in ancestors.iteritems():
49
new_ancestors[me] = {}
50
for parent in my_parents:
53
while can_skip(new_parent, descendants, ancestors):
54
if new_parent in exceptions:
57
if new_parent in new_ancestors:
58
del new_ancestors[new_parent]
59
new_parent = list(ancestors[new_parent])[0]
61
new_ancestors[me][new_parent] = distance
64
def get_rev_info(rev_id, source):
65
"""Return the committer, message, and date of a revision."""
70
return None, 'Null Revision', None
72
rev = source.get_revision(rev_id)
73
except NoSuchRevision:
75
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
77
return None, None, None
79
return None, None, None
81
committer = short_committer(rev.committer)
82
if rev.message is not None:
83
message = rev.message.split('\n')[0]
84
gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
85
date = time.strftime('%Y/%m/%d', gmtime)
88
committer = mail_map[committer]
92
committer = committer_alias[committer]
95
return committer, message, date
97
class Grapher(object):
98
def __init__(self, branch, other_branch=None):
101
self.other_branch = other_branch
102
revision_a = self.branch.last_patch()
103
if other_branch is not None:
104
greedy_fetch(branch, other_branch)
105
revision_b = self.other_branch.last_patch()
107
self.root, self.ancestors, self.descendants, self.common = \
108
combined_graph(revision_a, revision_b, self.branch)
109
except bzrlib.errors.NoCommonRoot:
110
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
112
self.root, self.ancestors, self.descendants = \
113
revision_graph(revision_a, branch)
116
self.n_history = branch.revision_history()
117
self.distances = node_distances(self.descendants, self.ancestors,
119
if other_branch is not None:
120
self.base = select_farthest(self.distances, self.common)
121
self.m_history = other_branch.revision_history()
126
def dot_node(self, node, num):
128
n_rev = self.n_history.index(node) + 1
132
m_rev = self.m_history.index(node) + 1
135
if (n_rev, m_rev) == (None, None):
139
name = "rR%d" % n_rev
142
for prefix, revno in (('r', n_rev), ('R', m_rev)):
143
if revno is not None:
144
namelist.append("%s%d" % (prefix, revno))
145
name = ' '.join(namelist)
146
if None not in (n_rev, m_rev):
147
cluster = "common_history"
149
elif (None, None) == (n_rev, m_rev):
151
if node in self.common:
155
elif n_rev is not None:
156
cluster = "my_history"
159
assert m_rev is not None
160
cluster = "other_history"
162
if node == self.base:
166
committer, message, date = get_rev_info(node, self.branch)
167
if committer is not None:
168
label.append(committer)
173
if node in self.distances:
174
rank = self.distances[node]
175
label.append('d%d' % self.distances[node])
179
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
180
rev_id=node, cluster=cluster, message=message,
184
if node not in self.ancestors:
185
d_node.node_style.append('dotted')
189
def get_relations(self, collapse=False):
194
visible_ancestors = compact_ancestors(self.descendants,
195
self.ancestors, (self.base,))
197
visible_ancestors = self.ancestors
198
for node, parents in visible_ancestors.iteritems():
199
if node not in dot_nodes:
200
dot_nodes[node] = self.dot_node(node, num)
202
if visible_ancestors is self.ancestors:
203
parent_iter = ((f, 0) for f in parents)
205
parent_iter = (f for f in parents.iteritems())
206
for parent, skipped in parent_iter:
207
if parent not in dot_nodes:
208
dot_nodes[parent] = self.dot_node(parent, num)
210
edge = Edge(dot_nodes[parent], dot_nodes[node])
212
edge.label = "%d" % skipped
213
node_relations.append(edge)
214
return node_relations
217
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
218
merge_branch=None, ranking="forced"):
219
b = Branch.open_containing(branch)
220
if merge_branch is not None:
221
m = Branch.open_containing(merge_branch)
224
grapher = Grapher(b, m)
225
relations = grapher.get_relations(collapse)
227
ext = filename.split('.')[-1]
228
output = dot_output(relations, ranking)
230
if ext not in RSVG_OUTPUT_TYPES:
233
output = list(output)
235
invoke_dot_aa(output, filename, ext)
238
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
239
" is installed correctly.")
241
print "Not antialiasing because rsvg (from librsvg-bin) is not"\
244
if ext in DOT_OUTPUT_TYPES and not antialias and not done:
246
invoke_dot(output, filename, ext)
249
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
250
" is installed correctly, or use --noantialias")
251
elif ext == 'dot' and not done:
252
my_file = file(filename, 'wb')
253
for fragment in output:
254
my_file.write(fragment)
257
invoke_dot_html(output, filename)
259
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
260
" is installed correctly, or use --noantialias")
262
print "Unknown file extension: %s" % ext