~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2006-05-10 01:29:41 UTC
  • mfrom: (364.1.4 bzrtools)
  • Revision ID: aaron.bentley@utoronto.ca-20060510012941-c46cf94b001d8d3e
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Aaron Bentley
2
 
# <aaron@aaronbentley.com>
 
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
56
56
    for me, my_parents in ancestors.iteritems():
57
57
        if me in skip:
58
58
            continue
59
 
        new_ancestors[me] = {}
 
59
        new_ancestors[me] = {} 
60
60
        for parent in my_parents:
61
 
            new_parent = parent
 
61
            new_parent = parent 
62
62
            distance = 0
63
63
            while can_skip(new_parent, descendants, ancestors):
64
64
                if new_parent in exceptions:
69
69
                new_parent = list(ancestors[new_parent])[0]
70
70
                distance += 1
71
71
            new_ancestors[me][new_parent] = distance
72
 
    return new_ancestors
 
72
    return new_ancestors    
73
73
 
74
74
def get_rev_info(rev_id, source):
75
75
    """Return the committer, message, and date of a revision."""
77
77
    message = None
78
78
    date = None
79
79
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None, None
 
80
        return None, 'Null Revision', None
81
81
    try:
82
82
        rev = source.get_revision(rev_id)
83
83
    except NoSuchRevision:
84
84
        try:
85
85
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
86
            if committer == '':
87
 
                return None, None, None, None
 
87
                return None, None, None
88
88
        except ValueError:
89
 
            return None, None, None, None
 
89
            return None, None, None
90
90
    else:
91
91
        committer = short_committer(rev.committer)
92
92
        if rev.message is not None:
93
93
            message = rev.message.split('\n')[0]
94
94
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
95
        date = time.strftime('%Y/%m/%d', gmtime)
96
 
        nick = rev.properties.get('branch-nick')
97
96
    if '@' in committer:
98
97
        try:
99
98
            committer = mail_map[committer]
103
102
        committer = committer_alias[committer]
104
103
    except KeyError:
105
104
        pass
106
 
    return committer, message, nick, date
 
105
    return committer, message, date
107
106
 
108
107
class Grapher(object):
109
108
    def __init__(self, branch, other_branch=None):
126
125
            self.common = []
127
126
 
128
127
        self.n_history = branch.revision_history()
129
 
        self.n_revnos = branch.get_revision_id_to_revno_map()
130
 
        self.distances = node_distances(self.descendants, self.ancestors,
 
128
        self.distances = node_distances(self.descendants, self.ancestors, 
131
129
                                        self.root)
132
130
        if other_branch is not None:
133
131
            self.base = select_farthest(self.distances, self.common)
134
 
            self.m_history = other_branch.revision_history()
135
 
            self.m_revnos = other_branch.get_revision_id_to_revno_map()
136
 
            new_graph = getattr(branch.repository, 'get_graph', lambda: None)()
137
 
            if new_graph is None:
138
 
                self.new_base = None
139
 
                self.lcas = set()
140
 
            else:
141
 
                self.new_base = new_graph.find_unique_lca(revision_a,
142
 
                                                          revision_b)
143
 
                self.lcas = new_graph.find_lca(revision_a, revision_b)
 
132
            self.m_history = other_branch.revision_history() 
144
133
        else:
145
134
            self.base = None
146
 
            self.new_base = None
147
 
            self.lcas = set()
148
135
            self.m_history = []
149
 
            self.m_revnos = {}
150
 
 
151
 
    @staticmethod
152
 
    def _get_revno_str(prefix, revno_map, revision_id):
153
 
        try:
154
 
            revno = revno_map[revision_id]
155
 
        except KeyError:
156
 
            return None
157
 
        return '%s%s' % (prefix, '.'.join(str(n) for n in revno))
158
136
 
159
137
    def dot_node(self, node, num):
160
138
        try:
166
144
        except ValueError:
167
145
            m_rev = None
168
146
        if (n_rev, m_rev) == (None, None):
169
 
            name = self._get_revno_str('r', self.n_revnos, node)
170
 
            if name is None:
171
 
                name = self._get_revno_str('R', self.m_revnos, node)
172
 
            if name is None:
173
 
                name = node[-5:]
 
147
            name = node[-5:]
174
148
            cluster = None
175
149
        elif n_rev == m_rev:
176
150
            name = "rR%d" % n_rev
196
170
            assert m_rev is not None
197
171
            cluster = "other_history"
198
172
            color = "#ff0000"
199
 
        if node in self.lcas:
200
 
            color = "#9933cc"
201
173
        if node == self.base:
202
 
            color = "#669933"
203
 
            if node == self.new_base:
204
 
                color = "#33ff33"
205
 
        if node == self.new_base:
206
 
            color = '#33cc99'
 
174
            color = "#33ff99"
207
175
 
208
176
        label = [name]
209
 
        committer, message, nick, date = get_rev_info(node,
210
 
                                                      self.branch.repository)
 
177
        committer, message, date = get_rev_info(node, self.branch.repository)
211
178
        if committer is not None:
212
179
            label.append(committer)
213
180
 
214
 
        if nick is not None:
215
 
            label.append(nick)
216
 
 
217
181
        if date is not None:
218
182
            label.append(date)
219
183
 
223
187
        else:
224
188
            rank = None
225
189
 
226
 
        d_node = Node("n%d" % num, color=color, label="\\n".join(label),
 
190
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
227
191
                    rev_id=node, cluster=cluster, message=message,
228
192
                    date=date)
229
193
        d_node.rank = rank
232
196
            d_node.node_style.append('dotted')
233
197
 
234
198
        return d_node
235
 
 
236
 
    def get_relations(self, collapse=False, max_distance=None):
 
199
        
 
200
    def get_relations(self, collapse=False):
237
201
        dot_nodes = {}
238
202
        node_relations = []
239
203
        num = 0
240
204
        if collapse:
241
 
            exceptions = self.lcas.union([self.base, self.new_base])
242
 
            visible_ancestors = compact_ancestors(self.descendants,
243
 
                                                  self.ancestors,
244
 
                                                  exceptions)
 
205
            visible_ancestors = compact_ancestors(self.descendants, 
 
206
                                                  self.ancestors, (self.base,))
245
207
        else:
246
 
            visible_ancestors = {}
247
 
            for revision, parents in self.ancestors.iteritems():
248
 
                visible_ancestors[revision] = dict((p, 0) for p in parents)
249
 
        if max_distance is not None:
250
 
            min_distance = max(self.distances.values()) - max_distance
251
 
            visible_ancestors = dict((n, p) for n, p in
252
 
                                     visible_ancestors.iteritems() if
253
 
                                     self.distances[n] >= min_distance)
 
208
            visible_ancestors = self.ancestors
254
209
        for node, parents in visible_ancestors.iteritems():
255
210
            if node not in dot_nodes:
256
211
                dot_nodes[node] = self.dot_node(node, num)
257
212
                num += 1
258
 
            for parent, skipped in parents.iteritems():
 
213
            if visible_ancestors is self.ancestors:
 
214
                parent_iter = ((f, 0) for f in parents)
 
215
            else:
 
216
                parent_iter = (f for f in parents.iteritems())
 
217
            for parent, skipped in parent_iter:
259
218
                if parent not in dot_nodes:
260
219
                    dot_nodes[parent] = self.dot_node(parent, num)
261
220
                    num += 1
267
226
 
268
227
 
269
228
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
270
 
                        merge_branch=None, ranking="forced", max_distance=None):
 
229
                        merge_branch=None, ranking="forced"):
271
230
    b = Branch.open_containing(branch)[0]
272
231
    if merge_branch is not None:
273
232
        m = Branch.open_containing(merge_branch)[0]
279
238
            m.lock_read()
280
239
        try:
281
240
            grapher = Grapher(b, m)
282
 
            relations = grapher.get_relations(collapse, max_distance)
 
241
            relations = grapher.get_relations(collapse)
283
242
        finally:
284
243
            if m is not None:
285
244
                m.unlock()
291
250
    done = False
292
251
    if ext not in RSVG_OUTPUT_TYPES:
293
252
        antialias = False
294
 
    if antialias:
 
253
    if antialias: 
295
254
        output = list(output)
296
255
        try:
297
256
            invoke_dot_aa(output, filename, ext)
309
268
            done = True
310
269
        except NoDot, e:
311
270
            raise BzrCommandError("Can't find 'dot'.  Please ensure Graphviz"\
312
 
                " is installed correctly.")
 
271
                " is installed correctly, or use --noantialias")
313
272
    elif ext == 'dot' and not done:
314
273
        my_file = file(filename, 'wb')
315
274
        for fragment in output:
319
278
            invoke_dot_html(output, filename)
320
279
        except NoDot, e:
321
280
            raise BzrCommandError("Can't find 'dot'.  Please ensure Graphviz"\
322
 
                " is installed correctly.")
 
281
                " is installed correctly, or use --noantialias")
323
282
    elif not done:
324
283
        print "Unknown file extension: %s" % ext
325
284