1
# Copyright (C) 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from bzrtools import short_committer
18
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
19
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
20
from bzrlib.branch import Branch
21
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
22
from bzrlib.fetch import greedy_fetch
23
from bzrlib.graph import node_distances, select_farthest
24
from bzrlib.revision import combined_graph, revision_graph
25
from bzrlib.revision import MultipleRevisionSources
31
mail_map = {'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
32
'abentley@panoramicfeedback.com': 'Aaron Bentley',
33
'abentley@lappy' : 'Aaron Bentley',
34
'john@arbash-meinel.com' : 'John Arbash Meinel',
35
'mbp@sourcefrog.net' : 'Martin Pool',
36
'robertc@robertcollins.net' : 'Robert Collins',
39
committer_alias = {'abentley': 'Aaron Bentley'}
40
def can_skip(rev_id, descendants, ancestors):
41
if rev_id not in descendants:
43
elif rev_id not in ancestors:
45
elif len(ancestors[rev_id]) != 1:
47
elif len(descendants[list(ancestors[rev_id])[0]]) != 1:
49
elif len(descendants[rev_id]) != 1:
54
def compact_ancestors(descendants, ancestors, exceptions=()):
57
for me, my_parents in ancestors.iteritems():
60
new_ancestors[me] = {}
61
for parent in my_parents:
64
while can_skip(new_parent, descendants, ancestors):
65
if new_parent in exceptions:
68
if new_parent in new_ancestors:
69
del new_ancestors[new_parent]
70
new_parent = list(ancestors[new_parent])[0]
72
new_ancestors[me][new_parent] = distance
75
def get_rev_info(rev_id, source):
76
"""Return the committer, message, and date of a revision."""
81
return None, 'Null Revision', None
83
rev = source.get_revision(rev_id)
84
except NoSuchRevision:
86
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
88
return None, None, None
90
return None, None, None
92
committer = short_committer(rev.committer)
93
if rev.message is not None:
94
message = rev.message.split('\n')[0]
95
gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
96
date = time.strftime('%Y/%m/%d', gmtime)
99
committer = mail_map[committer]
103
committer = committer_alias[committer]
106
return committer, message, date
108
class Grapher(object):
109
def __init__(self, branch, other_branch=None):
110
object.__init__(self)
112
self.other_branch = other_branch
113
revision_a = self.branch.last_revision()
114
if other_branch is not None:
115
greedy_fetch(branch, other_branch)
116
revision_b = self.other_branch.last_revision()
118
self.root, self.ancestors, self.descendants, self.common = \
119
combined_graph(revision_a, revision_b,
120
self.branch.repository)
121
except bzrlib.errors.NoCommonRoot:
122
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
124
self.root, self.ancestors, self.descendants = \
125
revision_graph(revision_a, branch.repository)
128
self.n_history = branch.revision_history()
129
self.distances = node_distances(self.descendants, self.ancestors,
131
if other_branch is not None:
132
self.base = select_farthest(self.distances, self.common)
133
self.m_history = other_branch.revision_history()
138
def dot_node(self, node, num):
140
n_rev = self.n_history.index(node) + 1
144
m_rev = self.m_history.index(node) + 1
147
if (n_rev, m_rev) == (None, None):
151
name = "rR%d" % n_rev
154
for prefix, revno in (('r', n_rev), ('R', m_rev)):
155
if revno is not None:
156
namelist.append("%s%d" % (prefix, revno))
157
name = ' '.join(namelist)
158
if None not in (n_rev, m_rev):
159
cluster = "common_history"
161
elif (None, None) == (n_rev, m_rev):
163
if node in self.common:
167
elif n_rev is not None:
168
cluster = "my_history"
171
assert m_rev is not None
172
cluster = "other_history"
174
if node == self.base:
178
committer, message, date = get_rev_info(node, self.branch.repository)
179
if committer is not None:
180
label.append(committer)
185
if node in self.distances:
186
rank = self.distances[node]
187
label.append('d%d' % self.distances[node])
191
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
192
rev_id=node, cluster=cluster, message=message,
196
if node not in self.ancestors:
197
d_node.node_style.append('dotted')
201
def get_relations(self, collapse=False):
206
visible_ancestors = compact_ancestors(self.descendants,
207
self.ancestors, (self.base,))
209
visible_ancestors = self.ancestors
210
for node, parents in visible_ancestors.iteritems():
211
if node not in dot_nodes:
212
dot_nodes[node] = self.dot_node(node, num)
214
if visible_ancestors is self.ancestors:
215
parent_iter = ((f, 0) for f in parents)
217
parent_iter = (f for f in parents.iteritems())
218
for parent, skipped in parent_iter:
219
if parent not in dot_nodes:
220
dot_nodes[parent] = self.dot_node(parent, num)
222
edge = Edge(dot_nodes[parent], dot_nodes[node])
224
edge.label = "%d" % skipped
225
node_relations.append(edge)
226
return node_relations
229
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
230
merge_branch=None, ranking="forced"):
231
b = Branch.open_containing(branch)[0]
232
if merge_branch is not None:
233
m = Branch.open_containing(merge_branch)[0]
241
grapher = Grapher(b, m)
242
relations = grapher.get_relations(collapse)
249
ext = filename.split('.')[-1]
250
output = dot_output(relations, ranking)
252
if ext not in RSVG_OUTPUT_TYPES:
255
output = list(output)
257
invoke_dot_aa(output, filename, ext)
260
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
261
" is installed correctly.")
263
print "Not antialiasing because rsvg (from librsvg-bin) is not"\
266
if ext in DOT_OUTPUT_TYPES and not antialias and not done:
268
invoke_dot(output, filename, ext)
271
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
272
" is installed correctly, or use --noantialias")
273
elif ext == 'dot' and not done:
274
my_file = file(filename, 'wb')
275
for fragment in output:
276
my_file.write(fragment)
279
invoke_dot_html(output, filename)
281
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
282
" is installed correctly, or use --noantialias")
284
print "Unknown file extension: %s" % ext