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
1
18
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
2
from dotgraph import mail_map, RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES
19
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
3
20
from bzrlib.branch import Branch
4
21
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
5
from bzrlib.fetch import greedy_fetch
6
22
from bzrlib.graph import node_distances, select_farthest
7
from bzrlib.revision import combined_graph, MultipleRevisionSources
23
from bzrlib.revision import combined_graph, revision_graph
24
from bzrlib.revision import MultipleRevisionSources
8
25
import bzrlib.errors
12
mail_map.update({'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
13
'abentley@panoramicfeedback.com': 'Aaron Bentley',
14
'abentley@lappy' : 'Aaron Bentley',
15
'john@arbash-meinel.com' : 'John Arbash Meinel',
16
'mbp@sourcefrog.net' : 'Martin Pool',
17
'robertc@robertcollins.net' : 'Robert Collins',
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',
20
38
committer_alias = {'abentley': 'Aaron Bentley'}
21
def add_relations(rev_id):
22
if rev_id in ancestors:
25
if rev_id not in nodes:
26
nodes[rev_id] = Node("n%d" % counter, label = rev_id)
28
revision = branch.get_revision(rev_id)
29
ancestors [rev_id] = []
30
for p in (p.revision_id for p in revision.parents):
32
if p not in descendants:
34
descendants[p].append(rev_id)
35
ancestors [rev_id].append(rev_id)
37
def short_committer(committer):
38
new_committer = re.sub('<.*>', '', committer).strip(' ')
39
if len(new_committer) < 2:
43
39
def can_skip(rev_id, descendants, ancestors):
44
40
if rev_id not in descendants:
42
elif rev_id not in ancestors:
46
44
elif len(ancestors[rev_id]) != 1:
48
elif len(descendants[ancestors[rev_id][0]]) != 1:
46
elif len(descendants[list(ancestors[rev_id])[0]]) != 1:
50
48
elif len(descendants[rev_id]) != 1:
55
def compact_descendants(descendants, ancestors):
53
def compact_ancestors(descendants, ancestors, exceptions=()):
58
for me, my_descendants in descendants.iteritems():
56
for me, my_parents in ancestors.iteritems():
61
new_descendants[me] = []
62
for descendant in my_descendants:
63
new_descendant = descendant
64
while can_skip(new_descendant, descendants, ancestors):
65
skip.add(new_descendant)
66
if new_descendant in new_descendants:
67
del new_descendants[new_descendant]
68
new_descendant = descendants[new_descendant][0]
69
new_descendants[me].append(new_descendant)
70
return new_descendants
73
def graph_ancestry(branch, collapse=True):
75
q = ((i+1, n) for (i, n) in enumerate(branch.revision_history()))
78
branch_name = os.path.basename(branch.base)
79
except AttributeError:
81
for (revno, rev_id) in q:
82
nodes[rev_id] = Node("R%d" % revno, color="#ffff00", rev_id=rev_id,
88
lines = [branch.last_patch()]
92
if rev_id not in nodes:
93
nodes[rev_id] = Node("n%d" % counter, label=rev_id,
98
revision = branch.get_revision(rev_id)
99
except bzrlib.errors.NoSuchRevision:
100
nodes[rev_id].node_style.append('dotted')
102
if nodes[rev_id].committer is None:
103
nodes[rev_id].committer = short_committer(revision.committer)
104
parent_ids = [r.revision_id for r in revision.parents]
105
ancestors [rev_id] = parent_ids
106
for parent in parent_ids:
107
if parent not in ancestors:
108
new_lines.add(parent)
109
descendants[parent] = []
110
descendants[parent].append(rev_id)
114
for node in nodes.itervalues():
115
node.label = node.get_label()
117
visible_descendants = compact_descendants(descendants, ancestors)
119
visible_descendants = descendants
121
for key, values in visible_descendants.iteritems():
123
node_relations.append((nodes[key], nodes[value]))
124
return node_relations
126
def get_committer(rev_id, source):
128
committer = short_committer(source.get_revision(rev_id).committer)
59
new_ancestors[me] = {}
60
for parent in my_parents:
63
while can_skip(new_parent, descendants, ancestors):
64
if new_parent in exceptions:
67
if new_parent in new_ancestors:
68
del new_ancestors[new_parent]
69
new_parent = list(ancestors[new_parent])[0]
71
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
82
rev = source.get_revision(rev_id)
129
83
except NoSuchRevision:
131
committer = '-'.join(rev_id.split('-')[:-2])\
85
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
87
return None, None, None
133
88
except ValueError:
89
return 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)
135
96
if '@' in committer:
137
98
committer = mail_map[committer]
141
102
committer = committer_alias[committer]
147
def graph_merge_pick(branch, other_branch):
148
greedy_fetch(branch, other_branch)
149
revision_a = branch.last_patch()
150
revision_b = other_branch.last_patch()
152
root, ancestors, descendants, common = \
153
combined_graph(revision_a, revision_b, branch)
154
except bzrlib.errors.NoCommonRoot:
155
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
156
distances = node_distances(descendants, ancestors, root)
157
base = select_farthest(distances, common)
158
n_history = branch.revision_history()
161
def dot_node(node, num):
105
return committer, message, date
107
class Grapher(object):
108
def __init__(self, branch, other_branch=None):
109
object.__init__(self)
111
self.other_branch = other_branch
112
revision_a = self.branch.last_revision()
113
if other_branch is not None:
114
branch.fetch(other_branch)
115
revision_b = self.other_branch.last_revision()
117
self.root, self.ancestors, self.descendants, self.common = \
118
combined_graph(revision_a, revision_b,
119
self.branch.repository)
120
except bzrlib.errors.NoCommonRoot:
121
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
123
self.root, self.ancestors, self.descendants = \
124
revision_graph(revision_a, branch.repository)
127
self.n_history = branch.revision_history()
128
self.distances = node_distances(self.descendants, self.ancestors,
130
if other_branch is not None:
131
self.base = select_farthest(self.distances, self.common)
132
self.m_history = other_branch.revision_history()
137
def dot_node(self, node, num):
163
n_rev = n_history.index(node) + 1
139
n_rev = self.n_history.index(node) + 1
164
140
except ValueError:
167
m_rev = m_history.index(node) + 1
143
m_rev = self.m_history.index(node) + 1
168
144
except ValueError:
170
146
if (n_rev, m_rev) == (None, None):
173
149
elif n_rev == m_rev:
174
150
name = "rR%d" % n_rev
194
170
assert m_rev is not None
195
171
cluster = "other_history"
196
172
color = "#ff0000"
173
if node == self.base:
198
174
color = "#33ff99"
201
committer = get_committer(node, branch)
177
committer, message, date = get_rev_info(node, self.branch.repository)
202
178
if committer is not None:
203
179
label.append(committer)
205
if node in distances:
206
label.append('d%d' % distances[node])
207
return Node("n%d" % num, color=color, label="\\n".join(label),
208
rev_id=node, cluster=cluster)
211
for num,node in enumerate(descendants):
212
dot_nodes[node] = dot_node(node, num)
215
for node, parents in ancestors.iteritems():
216
if node not in dot_nodes:
217
dot_nodes[node] = dot_node(node, 100000)
218
for parent in parents:
219
node_relations.append((dot_nodes[parent], dot_nodes[node]))
220
return node_relations
184
if node in self.distances:
185
rank = self.distances[node]
186
label.append('d%d' % self.distances[node])
190
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
191
rev_id=node, cluster=cluster, message=message,
195
if node not in self.ancestors:
196
d_node.node_style.append('dotted')
200
def get_relations(self, collapse=False):
205
visible_ancestors = compact_ancestors(self.descendants,
206
self.ancestors, (self.base,))
208
visible_ancestors = self.ancestors
209
for node, parents in visible_ancestors.iteritems():
210
if node not in dot_nodes:
211
dot_nodes[node] = self.dot_node(node, num)
213
if visible_ancestors is self.ancestors:
214
parent_iter = ((f, 0) for f in parents)
216
parent_iter = (f for f in parents.iteritems())
217
for parent, skipped in parent_iter:
218
if parent not in dot_nodes:
219
dot_nodes[parent] = self.dot_node(parent, num)
221
edge = Edge(dot_nodes[parent], dot_nodes[node])
223
edge.label = "%d" % skipped
224
node_relations.append(edge)
225
return node_relations
223
228
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
225
b = Branch.open_containing(branch)
226
if merge_branch is None:
227
relations = graph_ancestry(b, collapse)
229
merge_branch=None, ranking="forced"):
230
b = Branch.open_containing(branch)[0]
231
if merge_branch is not None:
232
m = Branch.open_containing(merge_branch)[0]
229
m = Branch.open_containing(merge_branch)
230
relations = graph_merge_pick(b, m)
240
grapher = Grapher(b, m)
241
relations = grapher.get_relations(collapse)
232
248
ext = filename.split('.')[-1]
233
if antialias and ext in RSVG_OUTPUT_TYPES:
249
output = dot_output(relations, ranking)
251
if ext not in RSVG_OUTPUT_TYPES:
254
output = list(output)
235
invoke_dot_aa(dot_output(relations), filename, ext)
256
invoke_dot_aa(output, filename, ext)
237
259
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
238
260
" is installed correctly.")
239
261
except NoRsvg, e:
240
raise BzrCommandError("Can't find 'rsvg'. Please ensure "\
241
"librsvg-bin is installed correctly, or use --noantialias.")
242
elif ext in DOT_OUTPUT_TYPES:
244
invoke_dot(dot_output(relations), filename, ext)
246
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
247
" is installed correctly, or use --noantialias")
249
file(filename, 'wb').write("".join(list(dot_output(relations))))
262
print "Not antialiasing because rsvg (from librsvg-bin) is not"\
265
if ext in DOT_OUTPUT_TYPES and not antialias and not done:
267
invoke_dot(output, filename, ext)
270
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
271
" is installed correctly, or use --noantialias")
272
elif ext == 'dot' and not done:
273
my_file = file(filename, 'wb')
274
for fragment in output:
275
my_file.write(fragment.encode('utf-8'))
278
invoke_dot_html(output, filename)
280
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
281
" is installed correctly, or use --noantialias")
251
283
print "Unknown file extension: %s" % ext