~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2007-08-16 03:30:21 UTC
  • mto: This revision was merged to the branch mainline in revision 576.
  • Revision ID: aaron.bentley@utoronto.ca-20070816033021-e9k6t6rj25ndlhrk
Allow zap --force to delete modified checkouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
    for me, my_parents in ancestors.iteritems():
57
57
        if me in skip:
58
58
            continue
59
 
        new_ancestors[me] = {} 
 
59
        new_ancestors[me] = {}
60
60
        for parent in my_parents:
61
 
            new_parent = parent 
 
61
            new_parent = parent
62
62
            distance = 0
63
63
            while can_skip(new_parent, descendants, ancestors):
64
64
                if new_parent in exceptions:
69
69
                new_parent = list(ancestors[new_parent])[0]
70
70
                distance += 1
71
71
            new_ancestors[me][new_parent] = distance
72
 
    return new_ancestors    
 
72
    return new_ancestors
73
73
 
74
74
def get_rev_info(rev_id, source):
75
75
    """Return the committer, message, and date of a revision."""
77
77
    message = None
78
78
    date = None
79
79
    if rev_id == 'null:':
80
 
        return None, 'Null Revision', None
 
80
        return None, 'Null Revision', None, None
81
81
    try:
82
82
        rev = source.get_revision(rev_id)
83
83
    except NoSuchRevision:
84
84
        try:
85
85
            committer = '-'.join(rev_id.split('-')[:-2]).strip(' ')
86
86
            if committer == '':
87
 
                return None, None, None
 
87
                return None, None, None, None
88
88
        except ValueError:
89
 
            return None, None, None
 
89
            return None, None, None, None
90
90
    else:
91
91
        committer = short_committer(rev.committer)
92
92
        if rev.message is not None:
93
93
            message = rev.message.split('\n')[0]
94
94
        gmtime = time.gmtime(rev.timestamp + (rev.timezone or 0))
95
95
        date = time.strftime('%Y/%m/%d', gmtime)
 
96
        nick = rev.properties.get('branch-nick')
96
97
    if '@' in committer:
97
98
        try:
98
99
            committer = mail_map[committer]
102
103
        committer = committer_alias[committer]
103
104
    except KeyError:
104
105
        pass
105
 
    return committer, message, date
 
106
    return committer, message, nick, date
106
107
 
107
108
class Grapher(object):
108
109
    def __init__(self, branch, other_branch=None):
125
126
            self.common = []
126
127
 
127
128
        self.n_history = branch.revision_history()
128
 
        self.distances = node_distances(self.descendants, self.ancestors, 
 
129
        self.distances = node_distances(self.descendants, self.ancestors,
129
130
                                        self.root)
130
131
        if other_branch is not None:
131
132
            self.base = select_farthest(self.distances, self.common)
132
 
            self.m_history = other_branch.revision_history() 
 
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)
133
142
        else:
134
143
            self.base = None
 
144
            self.new_base = None
 
145
            self.lcas = set()
135
146
            self.m_history = []
136
147
 
137
148
    def dot_node(self, node, num):
170
181
            assert m_rev is not None
171
182
            cluster = "other_history"
172
183
            color = "#ff0000"
 
184
        if node in self.lcas:
 
185
            color = "#9933cc"
173
186
        if node == self.base:
174
 
            color = "#33ff99"
 
187
            color = "#669933"
 
188
            if node == self.new_base:
 
189
                color = "#33ff33"
 
190
        if node == self.new_base:
 
191
            color = '#33cc99'
175
192
 
176
193
        label = [name]
177
 
        committer, message, date = get_rev_info(node, self.branch.repository)
 
194
        committer, message, nick, date = get_rev_info(node,
 
195
                                                      self.branch.repository)
178
196
        if committer is not None:
179
197
            label.append(committer)
180
198
 
 
199
        if nick is not None:
 
200
            label.append(nick)
 
201
 
181
202
        if date is not None:
182
203
            label.append(date)
183
204
 
187
208
        else:
188
209
            rank = None
189
210
 
190
 
        d_node = Node("n%d" % num, color=color, label="\\n".join(label), 
 
211
        d_node = Node("n%d" % num, color=color, label="\\n".join(label),
191
212
                    rev_id=node, cluster=cluster, message=message,
192
213
                    date=date)
193
214
        d_node.rank = rank
196
217
            d_node.node_style.append('dotted')
197
218
 
198
219
        return d_node
199
 
        
200
 
    def get_relations(self, collapse=False):
 
220
 
 
221
    def get_relations(self, collapse=False, max_distance=None):
201
222
        dot_nodes = {}
202
223
        node_relations = []
203
224
        num = 0
204
225
        if collapse:
205
 
            visible_ancestors = compact_ancestors(self.descendants, 
206
 
                                                  self.ancestors, (self.base,))
 
226
            exceptions = self.lcas.union([self.base, self.new_base])
 
227
            visible_ancestors = compact_ancestors(self.descendants,
 
228
                                                  self.ancestors,
 
229
                                                  exceptions)
207
230
        else:
208
 
            visible_ancestors = self.ancestors
 
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)
209
239
        for node, parents in visible_ancestors.iteritems():
210
240
            if node not in dot_nodes:
211
241
                dot_nodes[node] = self.dot_node(node, num)
212
242
                num += 1
213
 
            if visible_ancestors is self.ancestors:
214
 
                parent_iter = ((f, 0) for f in parents)
215
 
            else:
216
 
                parent_iter = (f for f in parents.iteritems())
217
 
            for parent, skipped in parent_iter:
 
243
            for parent, skipped in parents.iteritems():
218
244
                if parent not in dot_nodes:
219
245
                    dot_nodes[parent] = self.dot_node(parent, num)
220
246
                    num += 1
226
252
 
227
253
 
228
254
def write_ancestry_file(branch, filename, collapse=True, antialias=True,
229
 
                        merge_branch=None, ranking="forced"):
 
255
                        merge_branch=None, ranking="forced", max_distance=None):
230
256
    b = Branch.open_containing(branch)[0]
231
257
    if merge_branch is not None:
232
258
        m = Branch.open_containing(merge_branch)[0]
238
264
            m.lock_read()
239
265
        try:
240
266
            grapher = Grapher(b, m)
241
 
            relations = grapher.get_relations(collapse)
 
267
            relations = grapher.get_relations(collapse, max_distance)
242
268
        finally:
243
269
            if m is not None:
244
270
                m.unlock()
250
276
    done = False
251
277
    if ext not in RSVG_OUTPUT_TYPES:
252
278
        antialias = False
253
 
    if antialias: 
 
279
    if antialias:
254
280
        output = list(output)
255
281
        try:
256
282
            invoke_dot_aa(output, filename, ext)