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
1
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
2
from dotgraph import mail_map, RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge
20
3
from bzrlib.branch import Branch
21
4
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
5
from bzrlib.fetch import greedy_fetch
22
6
from bzrlib.graph import node_distances, select_farthest
23
7
from bzrlib.revision import combined_graph, revision_graph
24
8
from bzrlib.revision import MultipleRevisionSources
25
9
import bzrlib.errors
30
mail_map = {'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
31
'abentley@panoramicfeedback.com': 'Aaron Bentley',
32
'abentley@lappy' : 'Aaron Bentley',
33
'john@arbash-meinel.com' : 'John Arbash Meinel',
34
'mbp@sourcefrog.net' : 'Martin Pool',
35
'robertc@robertcollins.net' : 'Robert Collins',
13
mail_map.update({'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
14
'abentley@panoramicfeedback.com': 'Aaron Bentley',
15
'abentley@lappy' : 'Aaron Bentley',
16
'john@arbash-meinel.com' : 'John Arbash Meinel',
17
'mbp@sourcefrog.net' : 'Martin Pool',
18
'robertc@robertcollins.net' : 'Robert Collins',
38
21
committer_alias = {'abentley': 'Aaron Bentley'}
22
def short_committer(committer):
23
new_committer = re.sub('<.*>', '', committer).strip(' ')
24
if len(new_committer) < 2:
39
28
def can_skip(rev_id, descendants, ancestors):
40
29
if rev_id not in descendants:
69
58
new_parent = list(ancestors[new_parent])[0]
71
60
new_ancestors[me][new_parent] = distance
74
def get_rev_info(rev_id, source):
75
"""Return the committer, message, and date of a revision."""
80
return None, 'Null Revision', None, None
63
def get_committer(rev_id, source):
82
rev = source.get_revision(rev_id)
65
committer = short_committer(source.get_revision(rev_id).committer)
83
66
except NoSuchRevision:
85
68
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
69
if committer == '':
87
return None, None, None, None
89
return None, None, None, None
91
committer = short_committer(rev.committer)
92
if rev.message is not None:
93
message = rev.message.split('\n')[0]
94
gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
date = time.strftime('%Y/%m/%d', gmtime)
96
nick = rev.properties.get('branch-nick')
97
73
if '@' in committer:
99
75
committer = mail_map[committer]
103
79
committer = committer_alias[committer]
106
return committer, message, nick, date
108
84
class Grapher(object):
109
85
def __init__(self, branch, other_branch=None):
110
86
object.__init__(self)
111
87
self.branch = branch
112
88
self.other_branch = other_branch
113
revision_a = self.branch.last_revision()
89
revision_a = self.branch.last_patch()
114
90
if other_branch is not None:
115
branch.fetch(other_branch)
116
revision_b = self.other_branch.last_revision()
91
greedy_fetch(branch, other_branch)
92
revision_b = self.other_branch.last_patch()
118
94
self.root, self.ancestors, self.descendants, self.common = \
119
combined_graph(revision_a, revision_b,
120
self.branch.repository)
95
combined_graph(revision_a, revision_b, self.branch)
121
96
except bzrlib.errors.NoCommonRoot:
122
97
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
124
99
self.root, self.ancestors, self.descendants = \
125
revision_graph(revision_a, branch.repository)
100
revision_graph(revision_a, branch)
128
103
self.n_history = branch.revision_history()
129
self.distances = node_distances(self.descendants, self.ancestors,
104
self.distances = node_distances(self.descendants, self.ancestors,
131
106
if other_branch is not None:
132
107
self.base = select_farthest(self.distances, self.common)
133
self.m_history = other_branch.revision_history()
134
new_graph = getattr(branch.repository, 'get_graph', lambda: None)()
135
if new_graph is None:
139
self.new_base = new_graph.find_unique_lca(revision_a,
141
self.lcas = new_graph.find_lca(revision_a, revision_b)
108
self.m_history = other_branch.revision_history()
146
111
self.m_history = []
148
113
def dot_node(self, node, num):
181
146
assert m_rev is not None
182
147
cluster = "other_history"
183
148
color = "#ff0000"
184
if node in self.lcas:
186
149
if node == self.base:
188
if node == self.new_base:
190
if node == self.new_base:
194
committer, message, nick, date = get_rev_info(node,
195
self.branch.repository)
153
committer = get_committer(node, self.branch)
196
154
if committer is not None:
197
155
label.append(committer)
205
157
if node in self.distances:
206
158
rank = self.distances[node]
207
159
label.append('d%d' % self.distances[node])
211
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
212
rev_id=node, cluster=cluster, message=message,
163
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
164
rev_id=node, cluster=cluster)
214
165
d_node.rank = rank
216
167
if node not in self.ancestors:
217
168
d_node.node_style.append('dotted')
221
def get_relations(self, collapse=False, max_distance=None):
172
def get_relations(self, collapse=False):
223
174
node_relations = []
226
exceptions = self.lcas.union([self.base, self.new_base])
227
visible_ancestors = compact_ancestors(self.descendants,
177
visible_ancestors = compact_ancestors(self.descendants,
178
self.ancestors, (self.base,))
231
visible_ancestors = {}
232
for revision, parents in self.ancestors.iteritems():
233
visible_ancestors[revision] = dict((p, 0) for p in parents)
234
if max_distance is not None:
235
min_distance = max(self.distances.values()) - max_distance
236
visible_ancestors = dict((n, p) for n, p in
237
visible_ancestors.iteritems() if
238
self.distances[n] >= min_distance)
180
visible_ancestors = self.ancestors
239
181
for node, parents in visible_ancestors.iteritems():
240
182
if node not in dot_nodes:
241
183
dot_nodes[node] = self.dot_node(node, num)
243
for parent, skipped in parents.iteritems():
185
if visible_ancestors is self.ancestors:
186
parent_iter = ((f, 0) for f in parents)
188
parent_iter = (f for f in parents.iteritems())
189
for parent, skipped in parent_iter:
244
190
if parent not in dot_nodes:
245
191
dot_nodes[parent] = self.dot_node(parent, num)
254
200
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
255
merge_branch=None, ranking="forced", max_distance=None):
256
b = Branch.open_containing(branch)[0]
201
merge_branch=None, ranking="forced"):
202
b = Branch.open_containing(branch)
257
203
if merge_branch is not None:
258
m = Branch.open_containing(merge_branch)[0]
204
m = Branch.open_containing(merge_branch)
266
grapher = Grapher(b, m)
267
relations = grapher.get_relations(collapse, max_distance)
207
grapher = Grapher(b, m)
208
relations = grapher.get_relations(collapse)
274
210
ext = filename.split('.')[-1]
275
211
output = dot_output(relations, ranking)
277
if ext not in RSVG_OUTPUT_TYPES:
280
output = list(output)
212
if antialias and ext in RSVG_OUTPUT_TYPES:
282
214
invoke_dot_aa(output, filename, ext)
285
216
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
286
217
" is installed correctly.")
287
218
except NoRsvg, e:
288
print "Not antialiasing because rsvg (from librsvg-bin) is not"\
291
if ext in DOT_OUTPUT_TYPES and not antialias and not done:
219
raise BzrCommandError("Can't find 'rsvg'. Please ensure "\
220
"librsvg-bin is installed correctly, or use --noantialias.")
221
elif ext in DOT_OUTPUT_TYPES:
293
223
invoke_dot(output, filename, ext)
296
225
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
297
226
" is installed correctly, or use --noantialias")
298
elif ext == 'dot' and not done:
299
228
my_file = file(filename, 'wb')
300
229
for fragment in output:
301
my_file.write(fragment.encode('utf-8'))
304
invoke_dot_html(output, filename)
306
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
307
" is installed correctly, or use --noantialias")
230
my_file.write(fragment)
309
232
print "Unknown file extension: %s" % ext