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
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
23
from bzrlib.revision import combined_graph, revision_graph
8
24
from bzrlib.revision import MultipleRevisionSources
22
38
committer_alias = {'abentley': 'Aaron Bentley'}
23
def short_committer(committer):
24
new_committer = re.sub('<.*>', '', committer).strip(' ')
25
if len(new_committer) < 2:
29
39
def can_skip(rev_id, descendants, ancestors):
30
40
if rev_id not in descendants:
46
56
for me, my_parents in ancestors.iteritems():
49
new_ancestors[me] = {}
59
new_ancestors[me] = {}
50
60
for parent in my_parents:
53
63
while can_skip(new_parent, descendants, ancestors):
54
64
if new_parent in exceptions:
59
69
new_parent = list(ancestors[new_parent])[0]
61
71
new_ancestors[me][new_parent] = distance
64
74
def get_rev_info(rev_id, source):
65
75
"""Return the committer, message, and date of a revision."""
69
79
if rev_id == 'null:':
70
return None, 'Null Revision', None
80
return None, 'Null Revision', None, None
72
82
rev = source.get_revision(rev_id)
73
83
except NoSuchRevision:
75
85
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
76
86
if committer == '':
77
return None, None, None
87
return None, None, None, None
79
return None, None, None
89
return None, None, None, None
81
91
committer = short_committer(rev.committer)
82
92
if rev.message is not None:
83
93
message = rev.message.split('\n')[0]
84
94
gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
85
95
date = time.strftime('%Y/%m/%d', gmtime)
96
nick = rev.properties.get('branch-nick')
86
97
if '@' in committer:
88
99
committer = mail_map[committer]
92
103
committer = committer_alias[committer]
95
return committer, message, date
106
return committer, message, nick, date
97
108
class Grapher(object):
98
109
def __init__(self, branch, other_branch=None):
101
112
self.other_branch = other_branch
102
113
revision_a = self.branch.last_revision()
103
114
if other_branch is not None:
104
greedy_fetch(branch, other_branch)
115
branch.fetch(other_branch)
105
116
revision_b = self.other_branch.last_revision()
107
118
self.root, self.ancestors, self.descendants, self.common = \
108
combined_graph(revision_a, revision_b, self.branch)
119
combined_graph(revision_a, revision_b,
120
self.branch.repository)
109
121
except bzrlib.errors.NoCommonRoot:
110
122
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
112
124
self.root, self.ancestors, self.descendants = \
113
revision_graph(revision_a, branch)
125
revision_graph(revision_a, branch.repository)
116
128
self.n_history = branch.revision_history()
117
self.distances = node_distances(self.descendants, self.ancestors,
129
self.distances = node_distances(self.descendants, self.ancestors,
119
131
if other_branch is not None:
120
132
self.base = select_farthest(self.distances, self.common)
121
self.m_history = other_branch.revision_history()
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)
124
146
self.m_history = []
126
148
def dot_node(self, node, num):
159
181
assert m_rev is not None
160
182
cluster = "other_history"
161
183
color = "#ff0000"
184
if node in self.lcas:
162
186
if node == self.base:
188
if node == self.new_base:
190
if node == self.new_base:
166
committer, message, date = get_rev_info(node, self.branch)
194
committer, message, nick, date = get_rev_info(node,
195
self.branch.repository)
167
196
if committer is not None:
168
197
label.append(committer)
170
202
if date is not None:
171
203
label.append(date)
179
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
211
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
180
212
rev_id=node, cluster=cluster, message=message,
182
214
d_node.rank = rank
185
217
d_node.node_style.append('dotted')
189
def get_relations(self, collapse=False):
221
def get_relations(self, collapse=False, max_distance=None):
191
223
node_relations = []
194
visible_ancestors = compact_ancestors(self.descendants,
195
self.ancestors, (self.base,))
226
exceptions = self.lcas.union([self.base, self.new_base])
227
visible_ancestors = compact_ancestors(self.descendants,
197
visible_ancestors = self.ancestors
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)
198
239
for node, parents in visible_ancestors.iteritems():
199
240
if node not in dot_nodes:
200
241
dot_nodes[node] = self.dot_node(node, num)
202
if visible_ancestors is self.ancestors:
203
parent_iter = ((f, 0) for f in parents)
205
parent_iter = (f for f in parents.iteritems())
206
for parent, skipped in parent_iter:
243
for parent, skipped in parents.iteritems():
207
244
if parent not in dot_nodes:
208
245
dot_nodes[parent] = self.dot_node(parent, num)
217
254
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
218
merge_branch=None, ranking="forced"):
255
merge_branch=None, ranking="forced", max_distance=None):
219
256
b = Branch.open_containing(branch)[0]
220
257
if merge_branch is not None:
221
258
m = Branch.open_containing(merge_branch)[0]
226
263
if m is not None:
229
266
grapher = Grapher(b, m)
230
relations = grapher.get_relations(collapse)
267
relations = grapher.get_relations(collapse, max_distance)
232
269
if m is not None:
261
298
elif ext == 'dot' and not done:
262
299
my_file = file(filename, 'wb')
263
300
for fragment in output:
264
my_file.write(fragment)
301
my_file.write(fragment.encode('utf-8'))
265
302
elif ext == 'html':
267
304
invoke_dot_html(output, filename)