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
1
17
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
2
from dotgraph import mail_map
18
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
3
19
from bzrlib.branch import Branch
4
from bzrlib.errors import BzrCommandError
20
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
21
from bzrlib.fetch import greedy_fetch
22
from bzrlib.graph import node_distances, select_farthest
23
from bzrlib.revision import combined_graph, revision_graph
24
from bzrlib.revision import MultipleRevisionSources
5
25
import bzrlib.errors
9
mail_map.update({'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
10
'abentley@panoramicfeedback.com': 'Aaron Bentley',
11
'john@arbash-meinel.com' : 'John A. Meinel',
12
'mbp@sourcefrog.net' : 'Martin Pool'
15
def add_relations(rev_id):
16
if rev_id in ancestors:
19
if rev_id not in nodes:
20
nodes[rev_id] = Node("n%d" % counter, label = rev_id)
22
revision = branch.get_revision(rev_id)
23
ancestors [rev_id] = []
24
for p in (p.revision_id for p in revision.parents):
26
if p not in descendants:
28
descendants[p].append(rev_id)
29
ancestors [rev_id].append(rev_id)
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'}
31
39
def short_committer(committer):
32
40
new_committer = re.sub('<.*>', '', committer).strip(' ')
33
41
if len(new_committer) < 2:
37
45
def can_skip(rev_id, descendants, ancestors):
38
46
if rev_id not in descendants:
48
elif rev_id not in ancestors:
40
50
elif len(ancestors[rev_id]) != 1:
42
elif len(descendants[ancestors[rev_id][0]]) != 1:
52
elif len(descendants[list(ancestors[rev_id])[0]]) != 1:
44
54
elif len(descendants[rev_id]) != 1:
49
def compact_descendants(descendants, ancestors):
59
def compact_ancestors(descendants, ancestors, exceptions=()):
52
for me, my_descendants in descendants.iteritems():
62
for me, my_parents in ancestors.iteritems():
55
new_descendants[me] = []
56
for descendant in my_descendants:
57
new_descendant = descendant
58
while can_skip(new_descendant, descendants, ancestors):
59
skip.add(new_descendant)
60
if new_descendant in new_descendants:
61
del new_descendants[new_descendant]
62
new_descendant = descendants[new_descendant][0]
63
new_descendants[me].append(new_descendant)
64
return new_descendants
67
def graph_ancestry(branch, collapse=True):
69
q = ((i+1, n) for (i, n) in enumerate(branch.revision_history()))
72
branch_name = os.path.basename(branch.base)
73
except AttributeError:
75
for (revno, rev_id) in q:
76
nodes[rev_id] = Node("R%d" % revno, color="#ffff00", rev_id=rev_id,
82
lines = [branch.last_patch()]
86
if rev_id not in nodes:
87
nodes[rev_id] = Node("n%d" % counter, label=rev_id,
65
new_ancestors[me] = {}
66
for parent in my_parents:
69
while can_skip(new_parent, descendants, ancestors):
70
if new_parent in exceptions:
73
if new_parent in new_ancestors:
74
del new_ancestors[new_parent]
75
new_parent = list(ancestors[new_parent])[0]
77
new_ancestors[me][new_parent] = distance
80
def get_rev_info(rev_id, source):
81
"""Return the committer, message, and date of a revision."""
86
return None, 'Null Revision', None
88
rev = source.get_revision(rev_id)
89
except NoSuchRevision:
91
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
93
return None, None, None
95
return None, None, None
97
committer = short_committer(rev.committer)
98
if rev.message is not None:
99
message = rev.message.split('\n')[0]
100
gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
101
date = time.strftime('%Y/%m/%d', gmtime)
104
committer = mail_map[committer]
108
committer = committer_alias[committer]
111
return committer, message, date
113
class Grapher(object):
114
def __init__(self, branch, other_branch=None):
115
object.__init__(self)
117
self.other_branch = other_branch
118
revision_a = self.branch.last_revision()
119
if other_branch is not None:
120
greedy_fetch(branch, other_branch)
121
revision_b = self.other_branch.last_revision()
92
revision = branch.get_revision(rev_id)
93
except bzrlib.errors.NoSuchRevision:
94
nodes[rev_id].node_style.append('dotted')
96
if nodes[rev_id].committer is None:
97
nodes[rev_id].committer = short_committer(revision.committer)
98
parent_ids = [r.revision_id for r in revision.parents]
99
ancestors [rev_id] = parent_ids
100
for parent in parent_ids:
101
if parent not in ancestors:
102
new_lines.add(parent)
103
descendants[parent] = []
104
descendants[parent].append(rev_id)
109
visible_descendants = compact_descendants(descendants, ancestors)
123
self.root, self.ancestors, self.descendants, self.common = \
124
combined_graph(revision_a, revision_b, self.branch)
125
except bzrlib.errors.NoCommonRoot:
126
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
128
self.root, self.ancestors, self.descendants = \
129
revision_graph(revision_a, branch)
132
self.n_history = branch.revision_history()
133
self.distances = node_distances(self.descendants, self.ancestors,
135
if other_branch is not None:
136
self.base = select_farthest(self.distances, self.common)
137
self.m_history = other_branch.revision_history()
142
def dot_node(self, node, num):
144
n_rev = self.n_history.index(node) + 1
148
m_rev = self.m_history.index(node) + 1
151
if (n_rev, m_rev) == (None, None):
155
name = "rR%d" % n_rev
158
for prefix, revno in (('r', n_rev), ('R', m_rev)):
159
if revno is not None:
160
namelist.append("%s%d" % (prefix, revno))
161
name = ' '.join(namelist)
162
if None not in (n_rev, m_rev):
163
cluster = "common_history"
165
elif (None, None) == (n_rev, m_rev):
167
if node in self.common:
171
elif n_rev is not None:
172
cluster = "my_history"
175
assert m_rev is not None
176
cluster = "other_history"
178
if node == self.base:
182
committer, message, date = get_rev_info(node, self.branch)
183
if committer is not None:
184
label.append(committer)
189
if node in self.distances:
190
rank = self.distances[node]
191
label.append('d%d' % self.distances[node])
195
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
196
rev_id=node, cluster=cluster, message=message,
200
if node not in self.ancestors:
201
d_node.node_style.append('dotted')
205
def get_relations(self, collapse=False):
210
visible_ancestors = compact_ancestors(self.descendants,
211
self.ancestors, (self.base,))
213
visible_ancestors = self.ancestors
214
for node, parents in visible_ancestors.iteritems():
215
if node not in dot_nodes:
216
dot_nodes[node] = self.dot_node(node, num)
218
if visible_ancestors is self.ancestors:
219
parent_iter = ((f, 0) for f in parents)
221
parent_iter = (f for f in parents.iteritems())
222
for parent, skipped in parent_iter:
223
if parent not in dot_nodes:
224
dot_nodes[parent] = self.dot_node(parent, num)
226
edge = Edge(dot_nodes[parent], dot_nodes[node])
228
edge.label = "%d" % skipped
229
node_relations.append(edge)
230
return node_relations
233
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
234
merge_branch=None, ranking="forced"):
235
b = Branch.open_containing(branch)[0]
236
if merge_branch is not None:
237
m = Branch.open_containing(merge_branch)[0]
111
visible_descendants = descendants
113
for key, values in visible_descendants.iteritems():
115
node_relations.append((nodes[key], nodes[value]))
116
return node_relations
245
grapher = Grapher(b, m)
246
relations = grapher.get_relations(collapse)
118
def write_ancestry_file(branch, filename, collapse=True, antialias=True):
120
relations = graph_ancestry(b, collapse)
121
253
ext = filename.split('.')[-1]
122
if antialias and ext in ('png', 'jpg'):
254
output = dot_output(relations, ranking)
256
if ext not in RSVG_OUTPUT_TYPES:
259
output = list(output)
124
invoke_dot_aa(dot_output(relations), filename, ext)
261
invoke_dot_aa(output, filename, ext)
126
264
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
127
265
" is installed correctly.")
128
266
except NoRsvg, e:
129
raise BzrCommandError("Can't find 'rsvg'. Please ensure "\
130
"librsvg-bin is installed correctly, or use --noantialias.")
131
elif ext in ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png'):
133
invoke_dot(dot_output(relations), filename, ext)
135
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
136
" is installed correctly, or use --noantialias")
138
file(filename, 'wb').write("".join(list(dot_output(relations))))
267
print "Not antialiasing because rsvg (from librsvg-bin) is not"\
270
if ext in DOT_OUTPUT_TYPES and not antialias and not done:
272
invoke_dot(output, filename, ext)
275
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
276
" is installed correctly, or use --noantialias")
277
elif ext == 'dot' and not done:
278
my_file = file(filename, 'wb')
279
for fragment in output:
280
my_file.write(fragment)
283
invoke_dot_html(output, filename)
285
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
286
" is installed correctly, or use --noantialias")
140
288
print "Unknown file extension: %s" % ext