~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2005-09-22 23:26:32 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20050922232632-4fb8b331c9509a12
tweaked docs

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
 
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
 
2
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge
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
 
9
from datetime import datetime
25
10
import bzrlib.errors
26
11
import re
27
12
import os.path
28
 
import time
29
13
 
30
14
mail_map = {'aaron.bentley@utoronto.ca'     : 'Aaron Bentley',
31
15
            'abentley@panoramicfeedback.com': 'Aaron Bentley',
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
71
61
            new_ancestors[me][new_parent] = distance
72
62
    return new_ancestors    
73
63
 
74
 
def get_rev_info(rev_id, source):
75
 
    """Return the committer, message, and date of a revision."""
76
 
    committer = None
77
 
    message = None
78
 
    date = None
79
 
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None
 
64
def get_committer(rev_id, source):
81
65
    try:
82
 
        rev = source.get_revision(rev_id)
 
66
        committer = short_committer(source.get_revision(rev_id).committer)
83
67
    except NoSuchRevision:
84
68
        try:
85
69
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
70
            if committer == '':
87
 
                return None, None, None
 
71
                return None
88
72
        except ValueError:
89
 
            return None, None, None
90
 
    else:
91
 
        committer = short_committer(rev.committer)
92
 
        if rev.message is not None:
93
 
            message = rev.message.split('\n')[0]
94
 
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
 
        date = time.strftime('%Y/%m/%d', gmtime)
 
73
            return None
96
74
    if '@' in committer:
97
75
        try:
98
76
            committer = mail_map[committer]
102
80
        committer = committer_alias[committer]
103
81
    except KeyError:
104
82
        pass
105
 
    return committer, message, date
 
83
    return committer
106
84
 
107
85
class Grapher(object):
108
86
    def __init__(self, branch, other_branch=None):
109
87
        object.__init__(self)
110
88
        self.branch = branch
111
89
        self.other_branch = other_branch
112
 
        revision_a = self.branch.last_revision()
 
90
        revision_a = self.branch.last_patch()
113
91
        if other_branch is not None:
114
 
            branch.fetch(other_branch)
115
 
            revision_b = self.other_branch.last_revision()
 
92
            greedy_fetch(branch, other_branch)
 
93
            revision_b = self.other_branch.last_patch()
116
94
            try:
117
95
                self.root, self.ancestors, self.descendants, self.common = \
118
 
                    combined_graph(revision_a, revision_b,
119
 
                                   self.branch.repository)
 
96
                    combined_graph(revision_a, revision_b, self.branch)
120
97
            except bzrlib.errors.NoCommonRoot:
121
98
                raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
122
99
        else:
123
100
            self.root, self.ancestors, self.descendants = \
124
 
                revision_graph(revision_a, branch.repository)
 
101
                revision_graph(revision_a, branch)
125
102
            self.common = []
126
103
 
127
104
        self.n_history = branch.revision_history()
134
111
            self.base = None
135
112
            self.m_history = []
136
113
 
 
114
    def get_timestamp(self, revision_id):
 
115
        try:
 
116
            return float(self.branch.get_revision(revision_id).timestamp)
 
117
        except NoSuchRevision:
 
118
            return None
 
119
 
137
120
    def dot_node(self, node, num):
138
121
        try:
139
122
            n_rev = self.n_history.index(node) + 1
174
157
            color = "#33ff99"
175
158
 
176
159
        label = [name]
177
 
        committer, message, date = get_rev_info(node, self.branch.repository)
 
160
        committer = get_committer(node, self.branch)
178
161
        if committer is not None:
179
162
            label.append(committer)
180
163
 
181
 
        if date is not None:
182
 
            label.append(date)
 
164
        timestamp = self.get_timestamp(node)
 
165
        if timestamp is not None:
 
166
            date = datetime.fromtimestamp(timestamp)
 
167
            label.append("%d/%d/%d" % (date.year, date.month, date.day))
183
168
 
184
169
        if node in self.distances:
185
170
            rank = self.distances[node]
188
173
            rank = None
189
174
 
190
175
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
191
 
                    rev_id=node, cluster=cluster, message=message,
192
 
                    date=date)
 
176
                    rev_id=node, cluster=cluster)
193
177
        d_node.rank = rank
194
178
 
195
179
        if node not in self.ancestors:
196
180
            d_node.node_style.append('dotted')
 
181
        d_node.href = '#'
197
182
 
198
183
        return d_node
199
184
        
227
212
 
228
213
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
229
214
                        merge_branch=None, ranking="forced"):
230
 
    b = Branch.open_containing(branch)[0]
 
215
    b = Branch.open_containing(branch)
231
216
    if merge_branch is not None:
232
 
        m = Branch.open_containing(merge_branch)[0]
 
217
        m = Branch.open_containing(merge_branch)
233
218
    else:
234
219
        m = None
235
 
    b.lock_write()
236
 
    try:
237
 
        if m is not None:
238
 
            m.lock_read()
239
 
        try:
240
 
            grapher = Grapher(b, m)
241
 
            relations = grapher.get_relations(collapse)
242
 
        finally:
243
 
            if m is not None:
244
 
                m.unlock()
245
 
    finally:
246
 
        b.unlock()
 
220
    grapher = Grapher(b, m)
 
221
    relations = grapher.get_relations(collapse)
247
222
 
248
223
    ext = filename.split('.')[-1]
249
224
    output = dot_output(relations, ranking)
269
244
        except NoDot, e:
270
245
            raise BzrCommandError("Can't find 'dot'.  Please ensure Graphviz"\
271
246
                " is installed correctly, or use --noantialias")
272
 
    elif ext == 'dot' and not done:
 
247
    elif ext=='dot' and not done:
273
248
        my_file = file(filename, 'wb')
274
249
        for fragment in output:
275
 
            my_file.write(fragment.encode('utf-8'))
276
 
    elif ext == 'html':
277
 
        try:
278
 
            invoke_dot_html(output, filename)
279
 
        except NoDot, e:
280
 
            raise BzrCommandError("Can't find 'dot'.  Please ensure Graphviz"\
281
 
                " is installed correctly, or use --noantialias")
 
250
            my_file.write(fragment)
282
251
    elif not done:
283
252
        print "Unknown file extension: %s" % ext
284
253