~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2007-06-11 05:08:34 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20070611050834-wcbta2pfitcuopku
fix long-line detection

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()
122
134
        else:
123
135
            self.base = None
124
136
            self.m_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
 
176
192
        else:
177
193
            rank = None
178
194
 
179
 
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
 
195
        d_node = Node("n%d" % num, color=color, label="\\n".join(label),
180
196
                    rev_id=node, cluster=cluster, message=message,
181
197
                    date=date)
182
198
        d_node.rank = rank
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
193
209
        if collapse:
194
 
            visible_ancestors = compact_ancestors(self.descendants, 
 
210
            visible_ancestors = compact_ancestors(self.descendants,
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"):
 
238
                        merge_branch=None, ranking="forced", max_distance=None):
219
239
    b = Branch.open_containing(branch)[0]
220
240
    if merge_branch is not None:
221
241
        m = Branch.open_containing(merge_branch)[0]
222
242
    else:
223
243
        m = None
224
 
    b.lock_read()
 
244
    b.lock_write()
225
245
    try:
226
246
        if m is not None:
227
247
            m.lock_read()
228
248
        try:
229
249
            grapher = Grapher(b, m)
230
 
            relations = grapher.get_relations(collapse)
 
250
            relations = grapher.get_relations(collapse, max_distance)
231
251
        finally:
232
252
            if m is not None:
233
253
                m.unlock()
239
259
    done = False
240
260
    if ext not in RSVG_OUTPUT_TYPES:
241
261
        antialias = False
242
 
    if antialias: 
 
262
    if antialias:
243
263
        output = list(output)
244
264
        try:
245
265
            invoke_dot_aa(output, filename, ext)
261
281
    elif ext == 'dot' and not done:
262
282
        my_file = file(filename, 'wb')
263
283
        for fragment in output:
264
 
            my_file.write(fragment)
 
284
            my_file.write(fragment.encode('utf-8'))
265
285
    elif ext == 'html':
266
286
        try:
267
287
            invoke_dot_html(output, filename)