~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2006-02-21 04:04:27 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060221040427-70ecc1e24f2c4e80
forced ancestry graph to use utf-8 Dot output

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
20
20
from bzrlib.branch import Branch
21
21
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
 
22
from bzrlib.fetch import greedy_fetch
22
23
from bzrlib.graph import node_distances, select_farthest
23
24
from bzrlib.revision import combined_graph, revision_graph
24
25
from bzrlib.revision import MultipleRevisionSources
56
57
    for me, my_parents in ancestors.iteritems():
57
58
        if me in skip:
58
59
            continue
59
 
        new_ancestors[me] = {}
 
60
        new_ancestors[me] = {} 
60
61
        for parent in my_parents:
61
 
            new_parent = parent
 
62
            new_parent = parent 
62
63
            distance = 0
63
64
            while can_skip(new_parent, descendants, ancestors):
64
65
                if new_parent in exceptions:
69
70
                new_parent = list(ancestors[new_parent])[0]
70
71
                distance += 1
71
72
            new_ancestors[me][new_parent] = distance
72
 
    return new_ancestors
 
73
    return new_ancestors    
73
74
 
74
75
def get_rev_info(rev_id, source):
75
76
    """Return the committer, message, and date of a revision."""
77
78
    message = None
78
79
    date = None
79
80
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None, None
 
81
        return None, 'Null Revision', None
81
82
    try:
82
83
        rev = source.get_revision(rev_id)
83
84
    except NoSuchRevision:
84
85
        try:
85
86
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
87
            if committer == '':
87
 
                return None, None, None, None
 
88
                return None, None, None
88
89
        except ValueError:
89
 
            return None, None, None, None
 
90
            return None, None, None
90
91
    else:
91
92
        committer = short_committer(rev.committer)
92
93
        if rev.message is not None:
93
94
            message = rev.message.split('\n')[0]
94
95
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
96
        date = time.strftime('%Y/%m/%d', gmtime)
96
 
        nick = rev.properties.get('branch-nick')
97
97
    if '@' in committer:
98
98
        try:
99
99
            committer = mail_map[committer]
103
103
        committer = committer_alias[committer]
104
104
    except KeyError:
105
105
        pass
106
 
    return committer, message, nick, date
 
106
    return committer, message, date
107
107
 
108
108
class Grapher(object):
109
109
    def __init__(self, branch, other_branch=None):
112
112
        self.other_branch = other_branch
113
113
        revision_a = self.branch.last_revision()
114
114
        if other_branch is not None:
115
 
            branch.fetch(other_branch)
 
115
            greedy_fetch(branch, other_branch)
116
116
            revision_b = self.other_branch.last_revision()
117
117
            try:
118
118
                self.root, self.ancestors, self.descendants, self.common = \
126
126
            self.common = []
127
127
 
128
128
        self.n_history = branch.revision_history()
129
 
        self.distances = node_distances(self.descendants, self.ancestors,
 
129
        self.distances = node_distances(self.descendants, self.ancestors, 
130
130
                                        self.root)
131
131
        if other_branch is not None:
132
132
            self.base = select_farthest(self.distances, self.common)
133
 
            self.m_history = other_branch.revision_history()
 
133
            self.m_history = other_branch.revision_history() 
134
134
        else:
135
135
            self.base = None
136
136
            self.m_history = []
175
175
            color = "#33ff99"
176
176
 
177
177
        label = [name]
178
 
        committer, message, nick, date = get_rev_info(node,
179
 
                                                      self.branch.repository)
 
178
        committer, message, date = get_rev_info(node, self.branch.repository)
180
179
        if committer is not None:
181
180
            label.append(committer)
182
181
 
183
 
        if nick is not None:
184
 
            label.append(nick)
185
 
 
186
182
        if date is not None:
187
183
            label.append(date)
188
184
 
192
188
        else:
193
189
            rank = None
194
190
 
195
 
        d_node = Node("n%d" % num, color=color, label="\\n".join(label),
 
191
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
196
192
                    rev_id=node, cluster=cluster, message=message,
197
193
                    date=date)
198
194
        d_node.rank = rank
201
197
            d_node.node_style.append('dotted')
202
198
 
203
199
        return d_node
204
 
 
205
 
    def get_relations(self, collapse=False, max_distance=None):
 
200
        
 
201
    def get_relations(self, collapse=False):
206
202
        dot_nodes = {}
207
203
        node_relations = []
208
204
        num = 0
209
205
        if collapse:
210
 
            visible_ancestors = compact_ancestors(self.descendants,
 
206
            visible_ancestors = compact_ancestors(self.descendants, 
211
207
                                                  self.ancestors, (self.base,))
212
208
        else:
213
209
            visible_ancestors = self.ancestors
214
 
        if max_distance is not None:
215
 
            min_distance = max(self.distances.values()) - max_distance
216
 
            visible_ancestors = dict((n, p) for n, p in visible_ancestors.iteritems() if
217
 
                    self.distances[n] >= min_distance)
218
210
        for node, parents in visible_ancestors.iteritems():
219
211
            if node not in dot_nodes:
220
212
                dot_nodes[node] = self.dot_node(node, num)
235
227
 
236
228
 
237
229
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
238
 
                        merge_branch=None, ranking="forced", max_distance=None):
 
230
                        merge_branch=None, ranking="forced"):
239
231
    b = Branch.open_containing(branch)[0]
240
232
    if merge_branch is not None:
241
233
        m = Branch.open_containing(merge_branch)[0]
247
239
            m.lock_read()
248
240
        try:
249
241
            grapher = Grapher(b, m)
250
 
            relations = grapher.get_relations(collapse, max_distance)
 
242
            relations = grapher.get_relations(collapse)
251
243
        finally:
252
244
            if m is not None:
253
245
                m.unlock()
259
251
    done = False
260
252
    if ext not in RSVG_OUTPUT_TYPES:
261
253
        antialias = False
262
 
    if antialias:
 
254
    if antialias: 
263
255
        output = list(output)
264
256
        try:
265
257
            invoke_dot_aa(output, filename, ext)
281
273
    elif ext == 'dot' and not done:
282
274
        my_file = file(filename, 'wb')
283
275
        for fragment in output:
284
 
            my_file.write(fragment.encode('utf-8'))
 
276
            my_file.write(fragment)
285
277
    elif ext == 'html':
286
278
        try:
287
279
            invoke_dot_html(output, filename)