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 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
9
from datetime import datetime
25
10
import bzrlib.errors
30
14
mail_map = {'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
31
15
'abentley@panoramicfeedback.com': 'Aaron Bentley',
38
22
committer_alias = {'abentley': 'Aaron Bentley'}
23
def short_committer(committer):
24
new_committer = re.sub('<.*>', '', committer).strip(' ')
25
if len(new_committer) < 2:
39
29
def can_skip(rev_id, descendants, ancestors):
40
30
if rev_id not in descendants:
71
61
new_ancestors[me][new_parent] = distance
72
62
return new_ancestors
74
def get_rev_info(rev_id, source):
75
"""Return the committer, message, and date of a revision."""
80
return None, 'Null Revision', None
64
def get_committer(rev_id, source):
82
rev = source.get_revision(rev_id)
66
committer = short_committer(source.get_revision(rev_id).committer)
83
67
except NoSuchRevision:
85
69
committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
70
if committer == '':
87
return None, None, None
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)
96
74
if '@' in committer:
98
76
committer = mail_map[committer]
102
80
committer = committer_alias[committer]
105
return committer, message, date
107
85
class Grapher(object):
108
86
def __init__(self, branch, other_branch=None):
109
87
object.__init__(self)
110
88
self.branch = branch
111
89
self.other_branch = other_branch
112
revision_a = self.branch.last_revision()
90
revision_a = self.branch.last_patch()
113
91
if other_branch is not None:
114
branch.fetch(other_branch)
115
revision_b = self.other_branch.last_revision()
92
greedy_fetch(branch, other_branch)
93
revision_b = self.other_branch.last_patch()
117
95
self.root, self.ancestors, self.descendants, self.common = \
118
combined_graph(revision_a, revision_b,
119
self.branch.repository)
96
combined_graph(revision_a, revision_b, self.branch)
120
97
except bzrlib.errors.NoCommonRoot:
121
98
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
123
100
self.root, self.ancestors, self.descendants = \
124
revision_graph(revision_a, branch.repository)
101
revision_graph(revision_a, branch)
127
104
self.n_history = branch.revision_history()
135
112
self.m_history = []
114
def get_timestamp(self, revision_id):
116
return float(self.branch.get_revision(revision_id).timestamp)
117
except NoSuchRevision:
137
120
def dot_node(self, node, num):
139
122
n_rev = self.n_history.index(node) + 1
174
157
color = "#33ff99"
177
committer, message, date = get_rev_info(node, self.branch.repository)
160
committer = get_committer(node, self.branch)
178
161
if committer is not None:
179
162
label.append(committer)
164
timestamp = self.get_timestamp(node)
165
if timestamp is not None:
166
date = datetime.fromtimestamp(timestamp)
167
label.append("%d/%d/%d" % (date.year, date.month, date.day))
184
169
if node in self.distances:
185
170
rank = self.distances[node]
190
175
d_node = Node("n%d" % num, color=color, label="\\n".join(label),
191
rev_id=node, cluster=cluster, message=message,
176
rev_id=node, cluster=cluster)
193
177
d_node.rank = rank
195
179
if node not in self.ancestors:
196
180
d_node.node_style.append('dotted')
228
213
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
229
214
merge_branch=None, ranking="forced"):
230
b = Branch.open_containing(branch)[0]
215
b = Branch.open_containing(branch)
231
216
if merge_branch is not None:
232
m = Branch.open_containing(merge_branch)[0]
217
m = Branch.open_containing(merge_branch)
240
grapher = Grapher(b, m)
241
relations = grapher.get_relations(collapse)
220
grapher = Grapher(b, m)
221
relations = grapher.get_relations(collapse)
248
223
ext = filename.split('.')[-1]
249
224
output = dot_output(relations, ranking)
270
245
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
271
246
" is installed correctly, or use --noantialias")
272
elif ext == 'dot' and not done:
247
elif ext=='dot' and not done:
273
248
my_file = file(filename, 'wb')
274
249
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")
250
my_file.write(fragment)
283
252
print "Unknown file extension: %s" % ext