~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2005-11-11 17:43:12 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051111174312-1c627d82a07bf8fd
Added patch for tab-in-patch-filename support

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
from bzrtools import short_committer
18
17
from dotgraph import Node, dot_output, invoke_dot, invoke_dot_aa, NoDot, NoRsvg
19
18
from dotgraph import RSVG_OUTPUT_TYPES, DOT_OUTPUT_TYPES, Edge, invoke_dot_html
20
19
from bzrlib.branch import Branch
21
20
from bzrlib.errors import BzrCommandError, NoCommonRoot, NoSuchRevision
 
21
from bzrlib.fetch import greedy_fetch
22
22
from bzrlib.graph import node_distances, select_farthest
23
23
from bzrlib.revision import combined_graph, revision_graph
24
24
from bzrlib.revision import MultipleRevisionSources
36
36
            }
37
37
 
38
38
committer_alias = {'abentley': 'Aaron Bentley'}
 
39
def short_committer(committer):
 
40
    new_committer = re.sub('<.*>', '', committer).strip(' ')
 
41
    if len(new_committer) < 2:
 
42
        return committer
 
43
    return new_committer
 
44
 
39
45
def can_skip(rev_id, descendants, ancestors):
40
46
    if rev_id not in descendants:
41
47
        return False
56
62
    for me, my_parents in ancestors.iteritems():
57
63
        if me in skip:
58
64
            continue
59
 
        new_ancestors[me] = {}
 
65
        new_ancestors[me] = {} 
60
66
        for parent in my_parents:
61
 
            new_parent = parent
 
67
            new_parent = parent 
62
68
            distance = 0
63
69
            while can_skip(new_parent, descendants, ancestors):
64
70
                if new_parent in exceptions:
69
75
                new_parent = list(ancestors[new_parent])[0]
70
76
                distance += 1
71
77
            new_ancestors[me][new_parent] = distance
72
 
    return new_ancestors
 
78
    return new_ancestors    
73
79
 
74
80
def get_rev_info(rev_id, source):
75
81
    """Return the committer, message, and date of a revision."""
77
83
    message = None
78
84
    date = None
79
85
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None, None
 
86
        return None, 'Null Revision', None
81
87
    try:
82
88
        rev = source.get_revision(rev_id)
83
89
    except NoSuchRevision:
84
90
        try:
85
91
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
92
            if committer == '':
87
 
                return None, None, None, None
 
93
                return None, None, None
88
94
        except ValueError:
89
 
            return None, None, None, None
 
95
            return None, None, None
90
96
    else:
91
97
        committer = short_committer(rev.committer)
92
98
        if rev.message is not None:
93
99
            message = rev.message.split('\n')[0]
94
100
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
101
        date = time.strftime('%Y/%m/%d', gmtime)
96
 
        nick = rev.properties.get('branch-nick')
97
102
    if '@' in committer:
98
103
        try:
99
104
            committer = mail_map[committer]
103
108
        committer = committer_alias[committer]
104
109
    except KeyError:
105
110
        pass
106
 
    return committer, message, nick, date
 
111
    return committer, message, date
107
112
 
108
113
class Grapher(object):
109
114
    def __init__(self, branch, other_branch=None):
112
117
        self.other_branch = other_branch
113
118
        revision_a = self.branch.last_revision()
114
119
        if other_branch is not None:
115
 
            branch.fetch(other_branch)
 
120
            greedy_fetch(branch, other_branch)
116
121
            revision_b = self.other_branch.last_revision()
117
122
            try:
118
123
                self.root, self.ancestors, self.descendants, self.common = \
119
 
                    combined_graph(revision_a, revision_b,
120
 
                                   self.branch.repository)
 
124
                    combined_graph(revision_a, revision_b, self.branch)
121
125
            except bzrlib.errors.NoCommonRoot:
122
126
                raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
123
127
        else:
124
128
            self.root, self.ancestors, self.descendants = \
125
 
                revision_graph(revision_a, branch.repository)
 
129
                revision_graph(revision_a, branch)
126
130
            self.common = []
127
131
 
128
132
        self.n_history = branch.revision_history()
129
 
        self.distances = node_distances(self.descendants, self.ancestors,
 
133
        self.distances = node_distances(self.descendants, self.ancestors, 
130
134
                                        self.root)
131
135
        if other_branch is not None:
132
136
            self.base = select_farthest(self.distances, self.common)
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)
 
137
            self.m_history = other_branch.revision_history() 
142
138
        else:
143
139
            self.base = None
144
 
            self.new_base = None
145
 
            self.lcas = set()
146
140
            self.m_history = []
147
141
 
148
142
    def dot_node(self, node, num):
181
175
            assert m_rev is not None
182
176
            cluster = "other_history"
183
177
            color = "#ff0000"
184
 
        if node in self.lcas:
185
 
            color = "#9933cc"
186
178
        if node == self.base:
187
 
            color = "#669933"
188
 
            if node == self.new_base:
189
 
                color = "#33ff33"
190
 
        if node == self.new_base:
191
 
            color = '#33cc99'
 
179
            color = "#33ff99"
192
180
 
193
181
        label = [name]
194
 
        committer, message, nick, date = get_rev_info(node,
195
 
                                                      self.branch.repository)
 
182
        committer, message, date = get_rev_info(node, self.branch)
196
183
        if committer is not None:
197
184
            label.append(committer)
198
185
 
199
 
        if nick is not None:
200
 
            label.append(nick)
201
 
 
202
186
        if date is not None:
203
187
            label.append(date)
204
188
 
208
192
        else:
209
193
            rank = None
210
194
 
211
 
        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), 
212
196
                    rev_id=node, cluster=cluster, message=message,
213
197
                    date=date)
214
198
        d_node.rank = rank
217
201
            d_node.node_style.append('dotted')
218
202
 
219
203
        return d_node
220
 
 
221
 
    def get_relations(self, collapse=False, max_distance=None):
 
204
        
 
205
    def get_relations(self, collapse=False):
222
206
        dot_nodes = {}
223
207
        node_relations = []
224
208
        num = 0
225
209
        if collapse:
226
 
            exceptions = self.lcas.union([self.base, self.new_base])
227
 
            visible_ancestors = compact_ancestors(self.descendants,
228
 
                                                  self.ancestors,
229
 
                                                  exceptions)
 
210
            visible_ancestors = compact_ancestors(self.descendants, 
 
211
                                                  self.ancestors, (self.base,))
230
212
        else:
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)
 
213
            visible_ancestors = self.ancestors
239
214
        for node, parents in visible_ancestors.iteritems():
240
215
            if node not in dot_nodes:
241
216
                dot_nodes[node] = self.dot_node(node, num)
242
217
                num += 1
243
 
            for parent, skipped in parents.iteritems():
 
218
            if visible_ancestors is self.ancestors:
 
219
                parent_iter = ((f, 0) for f in parents)
 
220
            else:
 
221
                parent_iter = (f for f in parents.iteritems())
 
222
            for parent, skipped in parent_iter:
244
223
                if parent not in dot_nodes:
245
224
                    dot_nodes[parent] = self.dot_node(parent, num)
246
225
                    num += 1
252
231
 
253
232
 
254
233
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
255
 
                        merge_branch=None, ranking="forced", max_distance=None):
 
234
                        merge_branch=None, ranking="forced"):
256
235
    b = Branch.open_containing(branch)[0]
257
236
    if merge_branch is not None:
258
237
        m = Branch.open_containing(merge_branch)[0]
259
238
    else:
260
239
        m = None
261
 
    b.lock_write()
 
240
    b.lock_read()
262
241
    try:
263
242
        if m is not None:
264
243
            m.lock_read()
265
244
        try:
266
245
            grapher = Grapher(b, m)
267
 
            relations = grapher.get_relations(collapse, max_distance)
 
246
            relations = grapher.get_relations(collapse)
268
247
        finally:
269
248
            if m is not None:
270
249
                m.unlock()
276
255
    done = False
277
256
    if ext not in RSVG_OUTPUT_TYPES:
278
257
        antialias = False
279
 
    if antialias:
 
258
    if antialias: 
280
259
        output = list(output)
281
260
        try:
282
261
            invoke_dot_aa(output, filename, ext)
298
277
    elif ext == 'dot' and not done:
299
278
        my_file = file(filename, 'wb')
300
279
        for fragment in output:
301
 
            my_file.write(fragment.encode('utf-8'))
 
280
            my_file.write(fragment)
302
281
    elif ext == 'html':
303
282
        try:
304
283
            invoke_dot_html(output, filename)