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
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
from bzrlib.revision import combined_graph, revision_graph
24
from bzrlib.revision import MultipleRevisionSources
7
from bzrlib.revision import combined_graph, MultipleRevisionSources
25
8
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',
38
committer_alias = {'abentley': 'Aaron Bentley'}
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',
20
def add_relations(rev_id):
21
if rev_id in ancestors:
24
if rev_id not in nodes:
25
nodes[rev_id] = Node("n%d" % counter, label = rev_id)
27
revision = branch.get_revision(rev_id)
28
ancestors [rev_id] = []
29
for p in (p.revision_id for p in revision.parents):
31
if p not in descendants:
33
descendants[p].append(rev_id)
34
ancestors [rev_id].append(rev_id)
36
def short_committer(committer):
37
new_committer = re.sub('<.*>', '', committer).strip(' ')
38
if len(new_committer) < 2:
39
42
def can_skip(rev_id, descendants, ancestors):
40
43
if rev_id not in descendants:
42
elif rev_id not in ancestors:
44
45
elif len(ancestors[rev_id]) != 1:
46
elif len(descendants[list(ancestors[rev_id])[0]]) != 1:
47
elif len(descendants[ancestors[rev_id][0]]) != 1:
48
49
elif len(descendants[rev_id]) != 1:
53
def compact_ancestors(descendants, ancestors, exceptions=()):
54
def compact_descendants(descendants, ancestors):
56
for me, my_parents in ancestors.iteritems():
57
for me, my_descendants in descendants.iteritems():
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)
60
new_descendants[me] = []
61
for descendant in my_descendants:
62
new_descendant = descendant
63
while can_skip(new_descendant, descendants, ancestors):
64
skip.add(new_descendant)
65
if new_descendant in new_descendants:
66
del new_descendants[new_descendant]
67
new_descendant = descendants[new_descendant][0]
68
new_descendants[me].append(new_descendant)
69
return new_descendants
72
def graph_ancestry(branch, collapse=True):
74
q = ((i+1, n) for (i, n) in enumerate(branch.revision_history()))
77
branch_name = os.path.basename(branch.base)
78
except AttributeError:
80
for (revno, rev_id) in q:
81
nodes[rev_id] = Node("R%d" % revno, color="#ffff00", rev_id=rev_id,
87
lines = [branch.last_patch()]
91
if rev_id not in nodes:
92
nodes[rev_id] = Node("n%d" % counter, label=rev_id,
97
revision = branch.get_revision(rev_id)
98
except bzrlib.errors.NoSuchRevision:
99
nodes[rev_id].node_style.append('dotted')
101
if nodes[rev_id].committer is None:
102
nodes[rev_id].committer = short_committer(revision.committer)
103
parent_ids = [r.revision_id for r in revision.parents]
104
ancestors [rev_id] = parent_ids
105
for parent in parent_ids:
106
if parent not in ancestors:
107
new_lines.add(parent)
108
descendants[parent] = []
109
descendants[parent].append(rev_id)
113
for node in nodes.itervalues():
114
node.label = node.get_label()
116
visible_descendants = compact_descendants(descendants, ancestors)
118
visible_descendants = descendants
120
for key, values in visible_descendants.iteritems():
122
node_relations.append((nodes[key], nodes[value]))
123
return node_relations
125
def get_committer(rev_id, source):
127
committer = short_committer(source.get_revision(rev_id).committer)
130
return mail_map[committer]
83
134
except NoSuchRevision:
85
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
87
return None, None, None
136
first_segment = '-'.join(rev_id.split('-')[:-2])\
88
138
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)
98
committer = mail_map[committer]
140
if '@' in first_segment:
142
return mail_map[first_segment]
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()
151
source = MultipleRevisionSources(branch)
102
committer = committer_alias[committer]
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):
153
root, ancestors, descendants, common = \
154
combined_graph(revision_a, revision_b, source)
155
except bzrlib.errors.NoCommonRoot:
156
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
157
distances = node_distances(descendants, ancestors, root)
158
base = select_farthest(distances, common)
159
n_history = branch.revision_history()
160
m_history = other_branch.revision_history()
162
def dot_node(node, num):
139
n_rev = self.n_history.index(node) + 1
164
n_rev = n_history.index(node) + 1
140
165
except ValueError:
143
m_rev = self.m_history.index(node) + 1
168
m_rev = m_history.index(node) + 1
144
169
except ValueError:
146
171
if (n_rev, m_rev) == (None, None):
149
174
elif n_rev == m_rev:
150
175
name = "rR%d" % n_rev
153
for prefix, revno in (('r', n_rev), ('R', m_rev)):
154
if revno is not None:
155
namelist.append("%s%d" % (prefix, revno))
178
for prefix, num in (('r', n_rev), ('R', m_rev)):
180
namelist.append("%s%d" % (prefix, num))
156
181
name = ' '.join(namelist)
157
182
if None not in (n_rev, m_rev):
158
183
cluster = "common_history"
159
184
color = "#ff9900"
160
185
elif (None, None) == (n_rev, m_rev):
162
if node in self.common:
163
188
color = "#6699ff"
166
191
elif n_rev is not None:
167
192
cluster = "my_history"
168
193
color = "#ffff00"
170
195
assert m_rev is not None
171
196
cluster = "other_history"
172
197
color = "#ff0000"
173
if node == self.base:
174
199
color = "#33ff99"
177
committer, message, date = get_rev_info(node, self.branch.repository)
202
committer = get_committer(node, source)
178
203
if committer is not None:
179
204
label.append(committer)
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
206
if node in distances:
207
label.append('d%d' % distances[node])
208
return Node("n%d" % num, color=color, label="\\n".join(label),
209
rev_id=node, cluster=cluster)
212
for num,node in enumerate(descendants):
213
dot_nodes[node] = dot_node(node, num)
216
for node, parents in ancestors.iteritems():
217
if node not in dot_nodes:
218
dot_nodes[node] = dot_node(node, 100000)
219
for parent in parents:
220
node_relations.append((dot_nodes[parent], dot_nodes[node]))
221
return node_relations
228
224
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
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]
226
b = Branch.open_containing(branch)
227
if merge_branch is None:
228
relations = graph_ancestry(b, collapse)
240
grapher = Grapher(b, m)
241
relations = grapher.get_relations(collapse)
230
m = Branch.open_containing(branch)
231
relations = graph_merge_pick(b, m)
248
233
ext = filename.split('.')[-1]
249
output = dot_output(relations, ranking)
251
if ext not in RSVG_OUTPUT_TYPES:
254
output = list(output)
234
if antialias and ext in ('png', 'jpg'):
256
invoke_dot_aa(output, filename, ext)
236
invoke_dot_aa(dot_output(relations), filename, ext)
259
238
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
260
239
" is installed correctly.")
261
240
except NoRsvg, e:
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")
241
raise BzrCommandError("Can't find 'rsvg'. Please ensure "\
242
"librsvg-bin is installed correctly, or use --noantialias.")
243
elif ext in ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png'):
245
invoke_dot(dot_output(relations), filename, ext)
247
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
248
" is installed correctly, or use --noantialias")
250
file(filename, 'wb').write("".join(list(dot_output(relations))))
283
252
print "Unknown file extension: %s" % ext