1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
from dotgraph import mail_map
from bzrlib.branch import Branch
from bzrlib.errors import BzrCommandError, NoCommonRoot
from bzrlib.fetch import greedy_fetch
from bzrlib.graph import node_distances, select_farthest
from bzrlib.revision import combined_graph
import bzrlib.errors
import re
import os.path
mail_map.update({'aaron.bentley@utoronto.ca' : 'Aaron Bentley',
'abentley@panoramicfeedback.com': 'Aaron Bentley',
'abentley@lappy' : 'Aaron Bentley',
'john@arbash-meinel.com' : 'John Arbash Meinel',
'mbp@sourcefrog.net' : 'Martin Pool',
'robertc@robertcollins.net' : 'Robert Collins',
})
def add_relations(rev_id):
if rev_id in ancestors:
return
print rev_id
if rev_id not in nodes:
nodes[rev_id] = Node("n%d" % counter, label = rev_id)
counter += 1
revision = branch.get_revision(rev_id)
ancestors [rev_id] = []
for p in (p.revision_id for p in revision.parents):
add_relations(p)
if p not in descendants:
descendants[p] = []
descendants[p].append(rev_id)
ancestors [rev_id].append(rev_id)
def short_committer(committer):
new_committer = re.sub('<.*>', '', committer).strip(' ')
if len(new_committer) < 2:
return committer
return new_committer
def can_skip(rev_id, descendants, ancestors):
if rev_id not in descendants:
return False
elif len(ancestors[rev_id]) != 1:
return False
elif len(descendants[ancestors[rev_id][0]]) != 1:
return False
elif len(descendants[rev_id]) != 1:
return False
else:
return True
def compact_descendants(descendants, ancestors):
new_descendants={}
skip = set()
for me, my_descendants in descendants.iteritems():
if me in skip:
continue
new_descendants[me] = []
for descendant in my_descendants:
new_descendant = descendant
while can_skip(new_descendant, descendants, ancestors):
skip.add(new_descendant)
if new_descendant in new_descendants:
del new_descendants[new_descendant]
new_descendant = descendants[new_descendant][0]
new_descendants[me].append(new_descendant)
return new_descendants
def graph_ancestry(branch, collapse=True):
nodes = {}
q = ((i+1, n) for (i, n) in enumerate(branch.revision_history()))
r = 1
try:
branch_name = os.path.basename(branch.base)
except AttributeError:
branch_name = "main"
for (revno, rev_id) in q:
nodes[rev_id] = Node("R%d" % revno, color="#ffff00", rev_id=rev_id,
cluster=branch_name)
ancestors = {}
descendants = {}
counter = 0
lines = [branch.last_patch()]
while len(lines) > 0:
new_lines = set()
for rev_id in lines:
if rev_id not in nodes:
nodes[rev_id] = Node("n%d" % counter, label=rev_id,
rev_id=rev_id)
counter+=1
try:
revision = branch.get_revision(rev_id)
except bzrlib.errors.NoSuchRevision:
nodes[rev_id].node_style.append('dotted')
continue
if nodes[rev_id].committer is None:
nodes[rev_id].committer = short_committer(revision.committer)
parent_ids = [r.revision_id for r in revision.parents]
ancestors [rev_id] = parent_ids
for parent in parent_ids:
if parent not in ancestors:
new_lines.add(parent)
descendants[parent] = []
descendants[parent].append(rev_id)
lines = new_lines
node_relations = []
if collapse:
visible_descendants = compact_descendants(descendants, ancestors)
else:
visible_descendants = descendants
for key, values in visible_descendants.iteritems():
for value in values:
node_relations.append((nodes[key], nodes[value]))
return node_relations
def graph_merge_pick(branch, other_branch):
greedy_fetch(branch, other_branch)
revision_a = branch.last_patch()
revision_b = other_branch.last_patch()
try:
root, ancestors, descendants, common = \
combined_graph(revision_a, revision_b, branch)
except bzrlib.errors.NoCommonRoot:
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
distances = node_distances(descendants, ancestors, root)
base = select_farthest(distances, common)
n_history = branch.revision_history()
m_history = other_branch.revision_history()
dot_nodes = {}
def dot_node(node, num):
try:
n_rev = n_history.index(node) + 1
except ValueError:
n_rev = None
try:
m_rev = m_history.index(node) + 1
except ValueError:
m_rev = None
if (n_rev, m_rev) == (None, None):
name = node
cluster = None
elif n_rev == m_rev:
name = "rR%d" % n_rev
else:
namelist = []
for prefix, num in (('r', n_rev), ('R', m_rev)):
if num is not None:
namelist.append("%s%d" % (prefix, num))
name = ' '.join(namelist)
if None not in (n_rev, m_rev):
cluster = "common_history"
color = "#ff9900"
elif (None, None) == (n_rev, m_rev):
cluster = None
if node in common:
color = "#6699ff"
else:
color = None
elif n_rev is not None:
cluster = "my_history"
color = "#ffff00"
else:
assert m_rev is not None
cluster = "other_history"
color = "#ff0000"
if node == base:
color = "#33ff99"
label = [name]
if node in distances:
label.append('%d' % distances[node])
return Node("n%d" % num, color=color, label=label, rev_id=node,
cluster=cluster)
for num,node in enumerate(descendants):
dot_nodes[node] = dot_node(node, num)
node_relations = []
for ancestor, children in ancestors.iteritems():
if ancestor not in dot_nodes:
dot_nodes[ancestor] = dot_node(node, 100000)
for child in children:
if None in (dot_nodes[ancestor], dot_nodes[child]):
print (dot_nodes[ancestor], dot_nodes[child])
node_relations.append((dot_nodes[ancestor], dot_nodes[child]))
return node_relations
def write_ancestry_file(branch, filename, collapse=True, antialias=True):
b = Branch(branch)
relations = graph_merge_pick(b, b)
ext = filename.split('.')[-1]
if antialias and ext in ('png', 'jpg'):
try:
invoke_dot_aa(dot_output(relations), filename, ext)
except NoDot, e:
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
" is installed correctly.")
except NoRsvg, e:
raise BzrCommandError("Can't find 'rsvg'. Please ensure "\
"librsvg-bin is installed correctly, or use --noantialias.")
elif ext in ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png'):
try:
invoke_dot(dot_output(relations), filename, ext)
except NoDot, e:
raise BzrCommandError("Can't find 'dot'. Please ensure Graphviz"\
" is installed correctly, or use --noantialias")
elif ext=='dot':
file(filename, 'wb').write("".join(list(dot_output(relations))))
else:
print "Unknown file extension: %s" % ext
|