~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2005-10-28 03:16:25 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051028031625-aad040bd7217a4fa
Added locking to graph-ancestry

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
18
1
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
19
2
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
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
36
20
            }
37
21
 
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:
 
26
        return committer
 
27
    return new_committer
 
28
 
39
29
def can_skip(rev_id, descendants, ancestors):
40
30
    if rev_id not in descendants:
41
31
        return False
56
46
    for me, my_parents in ancestors.iteritems():
57
47
        if me in skip:
58
48
            continue
59
 
        new_ancestors[me] = {}
 
49
        new_ancestors[me] = {} 
60
50
        for parent in my_parents:
61
 
            new_parent = parent
 
51
            new_parent = parent 
62
52
            distance = 0
63
53
            while can_skip(new_parent, descendants, ancestors):
64
54
                if new_parent in exceptions:
69
59
                new_parent = list(ancestors[new_parent])[0]
70
60
                distance += 1
71
61
            new_ancestors[me][new_parent] = distance
72
 
    return new_ancestors
 
62
    return new_ancestors    
73
63
 
74
64
def get_rev_info(rev_id, source):
75
65
    """Return the committer, message, and date of a revision."""
77
67
    message = None
78
68
    date = None
79
69
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None, None
 
70
        return None, 'Null Revision', None
81
71
    try:
82
72
        rev = source.get_revision(rev_id)
83
73
    except NoSuchRevision:
84
74
        try:
85
75
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
76
            if committer == '':
87
 
                return None, None, None, None
 
77
                return None, None, None
88
78
        except ValueError:
89
 
            return None, None, None, None
 
79
            return None, None, None
90
80
    else:
91
81
        committer = short_committer(rev.committer)
92
82
        if rev.message is not None:
93
83
            message = rev.message.split('\n')[0]
94
84
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
85
        date = time.strftime('%Y/%m/%d', gmtime)
96
 
        nick = rev.properties.get('branch-nick')
97
86
    if '@' in committer:
98
87
        try:
99
88
            committer = mail_map[committer]
103
92
        committer = committer_alias[committer]
104
93
    except KeyError:
105
94
        pass
106
 
    return committer, message, nick, date
 
95
    return committer, message, date
107
96
 
108
97
class Grapher(object):
109
98
    def __init__(self, branch, other_branch=None):
112
101
        self.other_branch = other_branch
113
102
        revision_a = self.branch.last_revision()
114
103
        if other_branch is not None:
115
 
            branch.fetch(other_branch)
 
104
            greedy_fetch(branch, other_branch)
116
105
            revision_b = self.other_branch.last_revision()
117
106
            try:
118
107
                self.root, self.ancestors, self.descendants, self.common = \
119
 
                    combined_graph(revision_a, revision_b,
120
 
                                   self.branch.repository)
 
108
                    combined_graph(revision_a, revision_b, self.branch)
121
109
            except bzrlib.errors.NoCommonRoot:
122
110
                raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
123
111
        else:
124
112
            self.root, self.ancestors, self.descendants = \
125
 
                revision_graph(revision_a, branch.repository)
 
113
                revision_graph(revision_a, branch)
126
114
            self.common = []
127
115
 
128
116
        self.n_history = branch.revision_history()
129
 
        self.distances = node_distances(self.descendants, self.ancestors,
 
117
        self.distances = node_distances(self.descendants, self.ancestors, 
130
118
                                        self.root)
131
119
        if other_branch is not None:
132
120
            self.base = select_farthest(self.distances, self.common)
133
 
            self.m_history = other_branch.revision_history()
 
121
            self.m_history = other_branch.revision_history() 
134
122
        else:
135
123
            self.base = None
136
124
            self.m_history = []
175
163
            color = "#33ff99"
176
164
 
177
165
        label = [name]
178
 
        committer, message, nick, date = get_rev_info(node,
179
 
                                                      self.branch.repository)
 
166
        committer, message, date = get_rev_info(node, self.branch)
180
167
        if committer is not None:
181
168
            label.append(committer)
182
169
 
183
 
        if nick is not None:
184
 
            label.append(nick)
185
 
 
186
170
        if date is not None:
187
171
            label.append(date)
188
172
 
192
176
        else:
193
177
            rank = None
194
178
 
195
 
        d_node = Node("n%d" % num, color=color, label="\\n".join(label),
 
179
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
196
180
                    rev_id=node, cluster=cluster, message=message,
197
181
                    date=date)
198
182
        d_node.rank = rank
201
185
            d_node.node_style.append('dotted')
202
186
 
203
187
        return d_node
204
 
 
205
 
    def get_relations(self, collapse=False, max_distance=None):
 
188
        
 
189
    def get_relations(self, collapse=False):
206
190
        dot_nodes = {}
207
191
        node_relations = []
208
192
        num = 0
209
193
        if collapse:
210
 
            visible_ancestors = compact_ancestors(self.descendants,
 
194
            visible_ancestors = compact_ancestors(self.descendants, 
211
195
                                                  self.ancestors, (self.base,))
212
196
        else:
213
197
            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
198
        for node, parents in visible_ancestors.iteritems():
219
199
            if node not in dot_nodes:
220
200
                dot_nodes[node] = self.dot_node(node, num)
235
215
 
236
216
 
237
217
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
238
 
                        merge_branch=None, ranking="forced", max_distance=None):
 
218
                        merge_branch=None, ranking="forced"):
239
219
    b = Branch.open_containing(branch)[0]
240
220
    if merge_branch is not None:
241
221
        m = Branch.open_containing(merge_branch)[0]
242
222
    else:
243
223
        m = None
244
 
    b.lock_write()
 
224
    b.lock_read()
245
225
    try:
246
226
        if m is not None:
247
227
            m.lock_read()
248
228
        try:
249
229
            grapher = Grapher(b, m)
250
 
            relations = grapher.get_relations(collapse, max_distance)
 
230
            relations = grapher.get_relations(collapse)
251
231
        finally:
252
232
            if m is not None:
253
233
                m.unlock()
259
239
    done = False
260
240
    if ext not in RSVG_OUTPUT_TYPES:
261
241
        antialias = False
262
 
    if antialias:
 
242
    if antialias: 
263
243
        output = list(output)
264
244
        try:
265
245
            invoke_dot_aa(output, filename, ext)
281
261
    elif ext == 'dot' and not done:
282
262
        my_file = file(filename, 'wb')
283
263
        for fragment in output:
284
 
            my_file.write(fragment.encode('utf-8'))
 
264
            my_file.write(fragment)
285
265
    elif ext == 'html':
286
266
        try:
287
267
            invoke_dot_html(output, filename)