~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2007-07-12 13:50:21 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070712135021-yaszj8162d9dm71d
Adjust to use NULL_REVISION in API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
20
36
            }
21
37
 
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:
26
 
        return committer
27
 
    return new_committer
28
 
 
29
39
def can_skip(rev_id, descendants, ancestors):
30
40
    if rev_id not in descendants:
31
41
        return False
46
56
    for me, my_parents in ancestors.iteritems():
47
57
        if me in skip:
48
58
            continue
49
 
        new_ancestors[me] = {} 
 
59
        new_ancestors[me] = {}
50
60
        for parent in my_parents:
51
 
            new_parent = parent 
 
61
            new_parent = parent
52
62
            distance = 0
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]
60
70
                distance += 1
61
71
            new_ancestors[me][new_parent] = distance
62
 
    return new_ancestors    
 
72
    return new_ancestors
63
73
 
64
74
def get_rev_info(rev_id, source):
65
75
    """Return the committer, message, and date of a revision."""
67
77
    message = None
68
78
    date = None
69
79
    if rev_id == 'null:':
70
 
        return None, 'Null Revision', None
 
80
        return None, 'Null Revision', None, None
71
81
    try:
72
82
        rev = source.get_revision(rev_id)
73
83
    except NoSuchRevision:
74
84
        try:
75
85
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
76
86
            if committer == '':
77
 
                return None, None, None
 
87
                return None, None, None, None
78
88
        except ValueError:
79
 
            return None, None, None
 
89
            return None, None, None, None
80
90
    else:
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:
87
98
        try:
88
99
            committer = mail_map[committer]
92
103
        committer = committer_alias[committer]
93
104
    except KeyError:
94
105
        pass
95
 
    return committer, message, date
 
106
    return committer, message, nick, date
96
107
 
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()
106
117
            try:
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)
111
123
        else:
112
124
            self.root, self.ancestors, self.descendants = \
113
 
                revision_graph(revision_a, branch)
 
125
                revision_graph(revision_a, branch.repository)
114
126
            self.common = []
115
127
 
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,
118
130
                                        self.root)
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:
 
136
                self.new_base = None
 
137
                self.lcas = set()
 
138
            else:
 
139
                self.new_base = new_graph.find_unique_lca(revision_a,
 
140
                                                          revision_b)
 
141
                self.lcas = new_graph.find_lca(revision_a, revision_b)
122
142
        else:
123
143
            self.base = None
 
144
            self.new_base = None
 
145
            self.lcas = set()
124
146
            self.m_history = []
125
147
 
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:
 
185
            color = "#9933cc"
162
186
        if node == self.base:
163
 
            color = "#33ff99"
 
187
            color = "#669933"
 
188
            if node == self.new_base:
 
189
                color = "#33ff33"
 
190
        if node == self.new_base:
 
191
            color = '#33cc99'
164
192
 
165
193
        label = [name]
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)
169
198
 
 
199
        if nick is not None:
 
200
            label.append(nick)
 
201
 
170
202
        if date is not None:
171
203
            label.append(date)
172
204
 
176
208
        else:
177
209
            rank = None
178
210
 
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,
181
213
                    date=date)
182
214
        d_node.rank = rank
185
217
            d_node.node_style.append('dotted')
186
218
 
187
219
        return d_node
188
 
        
189
 
    def get_relations(self, collapse=False):
 
220
 
 
221
    def get_relations(self, collapse=False, max_distance=None):
190
222
        dot_nodes = {}
191
223
        node_relations = []
192
224
        num = 0
193
225
        if collapse:
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,
 
228
                                                  self.ancestors,
 
229
                                                  exceptions)
196
230
        else:
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)
201
242
                num += 1
202
 
            if visible_ancestors is self.ancestors:
203
 
                parent_iter = ((f, 0) for f in parents)
204
 
            else:
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)
209
246
                    num += 1
215
252
 
216
253
 
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]
222
259
    else:
223
260
        m = None
224
 
    b.lock_read()
 
261
    b.lock_write()
225
262
    try:
226
263
        if m is not None:
227
264
            m.lock_read()
228
265
        try:
229
266
            grapher = Grapher(b, m)
230
 
            relations = grapher.get_relations(collapse)
 
267
            relations = grapher.get_relations(collapse, max_distance)
231
268
        finally:
232
269
            if m is not None:
233
270
                m.unlock()
239
276
    done = False
240
277
    if ext not in RSVG_OUTPUT_TYPES:
241
278
        antialias = False
242
 
    if antialias: 
 
279
    if antialias:
243
280
        output = list(output)
244
281
        try:
245
282
            invoke_dot_aa(output, filename, ext)
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':
266
303
        try:
267
304
            invoke_dot_html(output, filename)