~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2007-05-08 14:49:33 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070508144933-5m98ujj1blhd8ns9
Tags: release-0.16.1
ReleaseĀ 0.16.1

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
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):
99
110
        object.__init__(self)
100
111
        self.branch = branch
101
112
        self.other_branch = other_branch
102
 
        revision_a = self.branch.last_patch()
 
113
        revision_a = self.branch.last_revision()
103
114
        if other_branch is not None:
104
 
            greedy_fetch(branch, other_branch)
105
 
            revision_b = self.other_branch.last_patch()
 
115
            branch.fetch(other_branch)
 
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()
163
175
            color = "#33ff99"
164
176
 
165
177
        label = [name]
166
 
        committer, message, date = get_rev_info(node, self.branch)
 
178
        committer, message, nick, date = get_rev_info(node, 
 
179
                                                      self.branch.repository)
167
180
        if committer is not None:
168
181
            label.append(committer)
169
182
 
 
183
        if nick is not None:
 
184
            label.append(nick)
 
185
 
170
186
        if date is not None:
171
187
            label.append(date)
172
188
 
185
201
            d_node.node_style.append('dotted')
186
202
 
187
203
        return d_node
188
 
        
189
 
    def get_relations(self, collapse=False):
 
204
       
 
205
    def get_relations(self, collapse=False, max_distance=None):
190
206
        dot_nodes = {}
191
207
        node_relations = []
192
208
        num = 0
195
211
                                                  self.ancestors, (self.base,))
196
212
        else:
197
213
            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)
198
218
        for node, parents in visible_ancestors.iteritems():
199
219
            if node not in dot_nodes:
200
220
                dot_nodes[node] = self.dot_node(node, num)
215
235
 
216
236
 
217
237
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
218
 
                        merge_branch=None, ranking="forced"):
219
 
    b = Branch.open_containing(branch)
 
238
                        merge_branch=None, ranking="forced", max_distance=None):
 
239
    b = Branch.open_containing(branch)[0]
220
240
    if merge_branch is not None:
221
 
        m = Branch.open_containing(merge_branch)
 
241
        m = Branch.open_containing(merge_branch)[0]
222
242
    else:
223
243
        m = None
224
 
    grapher = Grapher(b, m)
225
 
    relations = grapher.get_relations(collapse)
 
244
    b.lock_write()
 
245
    try:
 
246
        if m is not None:
 
247
            m.lock_read()
 
248
        try:
 
249
            grapher = Grapher(b, m)
 
250
            relations = grapher.get_relations(collapse, max_distance)
 
251
        finally:
 
252
            if m is not None:
 
253
                m.unlock()
 
254
    finally:
 
255
        b.unlock()
226
256
 
227
257
    ext = filename.split('.')[-1]
228
258
    output = dot_output(relations, ranking)
251
281
    elif ext == 'dot' and not done:
252
282
        my_file = file(filename, 'wb')
253
283
        for fragment in output:
254
 
            my_file.write(fragment)
 
284
            my_file.write(fragment.encode('utf-8'))
255
285
    elif ext == 'html':
256
286
        try:
257
287
            invoke_dot_html(output, filename)