1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Topological sorting routines."""
21
import bzrlib.errors as errors
24
__all__ = ["topo_sort", "TopoSorter", "merge_sort", "MergeSorter"]
28
"""Topological sort a graph.
30
graph -- sequence of pairs of node->parents_list.
32
The result is a list of node names, such that all parents come before
35
node identifiers can be any hashable object, and are typically strings.
37
return TopoSorter(graph).sorted()
40
class TopoSorter(object):
42
def __init__(self, graph):
43
"""Topological sorting of a graph.
45
:param graph: sequence of pairs of node_name->parent_names_list.
46
i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
47
For this input the output from the sort or
48
iter_topo_order routines will be:
51
node identifiers can be any hashable object, and are typically strings.
53
If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
54
one of the two values for 'a'.
56
The graph is sorted lazily: until you iterate or sort the input is
57
not processed other than to create an internal representation.
59
iteration or sorting may raise GraphCycleError if a cycle is present
62
# a dict of the graph.
63
self._graph = dict(graph)
65
# self._original_graph = dict(graph)
67
# this is a stack storing the depth first search into the graph.
68
self._node_name_stack = []
69
# at each level of 'recursion' we have to check each parent. This
70
# stack stores the parents we have not yet checked for the node at the
71
# matching depth in _node_name_stack
72
self._pending_parents_stack = []
73
# this is a set of the completed nodes for fast checking whether a
74
# parent in a node we are processing on the stack has already been
75
# emitted and thus can be skipped.
76
self._completed_node_names = set()
79
"""Sort the graph and return as a list.
81
After calling this the sorter is empty and you must create a new one.
83
return list(self.iter_topo_order())
85
### Useful if fiddling with this code.
87
### sorted_names = list(self.iter_topo_order())
88
### for index in range(len(sorted_names)):
89
### rev = sorted_names[index]
90
### for left_index in range(index):
91
### if rev in self.original_graph[sorted_names[left_index]]:
92
### print "revision in parent list of earlier revision"
93
### import pdb;pdb.set_trace()
95
def iter_topo_order(self):
96
"""Yield the nodes of the graph in a topological order.
98
After finishing iteration the sorter is empty and you cannot continue
102
# now pick a random node in the source graph, and transfer it to the
103
# top of the depth first search stack.
104
node_name, parents = self._graph.popitem()
105
self._push_node(node_name, parents)
106
while self._node_name_stack:
107
# loop until this call completes.
108
parents_to_visit = self._pending_parents_stack[-1]
109
# if all parents are done, the revision is done
110
if not parents_to_visit:
111
# append the revision to the topo sorted list
112
# all the nodes parents have been added to the output, now
113
# we can add it to the output.
114
yield self._pop_node()
116
while self._pending_parents_stack[-1]:
117
# recurse depth first into a single parent
118
next_node_name = self._pending_parents_stack[-1].pop()
119
if next_node_name in self._completed_node_names:
120
# this parent was completed by a child on the
121
# call stack. skip it.
123
# otherwise transfer it from the source graph into the
124
# top of the current depth first search stack.
126
parents = self._graph.pop(next_node_name)
128
# if the next node is not in the source graph it has
129
# already been popped from it and placed into the
130
# current search stack (but not completed or we would
131
# have hit the continue 4 lines up.
132
# this indicates a cycle.
133
raise errors.GraphCycleError(self._node_name_stack)
134
self._push_node(next_node_name, parents)
135
# and do not continue processing parents until this 'call'
139
def _push_node(self, node_name, parents):
140
"""Add node_name to the pending node stack.
142
Names in this stack will get emitted into the output as they are popped
145
self._node_name_stack.append(node_name)
146
self._pending_parents_stack.append(list(parents))
149
"""Pop the top node off the stack
151
The node is appended to the sorted output.
153
# we are returning from the flattened call frame:
154
# pop off the local variables
155
node_name = self._node_name_stack.pop()
156
self._pending_parents_stack.pop()
158
self._completed_node_names.add(node_name)
162
def merge_sort(graph, branch_tip, mainline_revisions=None, generate_revno=False):
163
"""Topological sort a graph which groups merges.
165
:param graph: sequence of pairs of node->parents_list.
166
:param branch_tip: the tip of the branch to graph. Revisions not
167
reachable from branch_tip are not included in the
169
:param mainline_revisions: If not None this forces a mainline to be
170
used rather than synthesised from the graph.
171
This must be a valid path through some part
172
of the graph. If the mainline does not cover all
173
the revisions, output stops at the start of the
174
old revision listed in the mainline revisions
176
The order for this parameter is oldest-first.
177
:param generate_revno: Optional parameter controlling the generation of
178
revision number sequences in the output. See the output description of
179
the MergeSorter docstring for details.
180
:result: See the MergeSorter docstring for details.
181
node identifiers can be any hashable object, and are typically strings.
183
return MergeSorter(graph, branch_tip, mainline_revisions,
184
generate_revno).sorted()
187
class MergeSorter(object):
189
def __init__(self, graph, branch_tip, mainline_revisions=None,
190
generate_revno=False):
191
"""Merge-aware topological sorting of a graph.
193
:param graph: sequence of pairs of node_name->parent_names_list.
194
i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
195
For this input the output from the sort or
196
iter_topo_order routines will be:
198
:param branch_tip: the tip of the branch to graph. Revisions not
199
reachable from branch_tip are not included in the
201
:param mainline_revisions: If not None this forces a mainline to be
202
used rather than synthesised from the graph.
203
This must be a valid path through some part
204
of the graph. If the mainline does not cover all
205
the revisions, output stops at the start of the
206
old revision listed in the mainline revisions
208
The order for this parameter is oldest-first.
209
:param generate_revno: Optional parameter controlling the generation of
210
revision number sequences in the output. See the output description
213
The result is a list sorted so that all parents come before
214
their children. Each element of the list is a tuple containing:
215
(sequence_number, node_name, merge_depth, end_of_merge)
216
* sequence_number: The sequence of this row in the output. Useful for
218
* node_name: The node name: opaque text to the merge routine.
219
* merge_depth: How many levels of merging deep this node has been
221
* revno_sequence: When requested this field provides a sequence of
222
revision numbers for all revisions. The format is:
223
REVNO[[.BRANCHREVNO.REVNO] ...]. BRANCHREVNO is the number of the
224
branch that the revno is on. From left to right the REVNO numbers
225
are the sequence numbers within that branch of the revision.
226
For instance, the graph {A:[], B:['A'], C:['A', 'B']} will get
227
the following revno_sequences assigned: A:(1,), B:(1,1,1), C:(2,).
228
This should be read as 'A is the first commit in the trunk',
229
'B is the first commit on the first branch made from A', 'C is the
230
second commit in the trunk'.
231
* end_of_merge: When True the next node is part of a different merge.
234
node identifiers can be any hashable object, and are typically strings.
236
If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
237
one of the two values for 'a'.
239
The graph is sorted lazily: until you iterate or sort the input is
240
not processed other than to create an internal representation.
242
iteration or sorting may raise GraphCycleError if a cycle is present
245
Background information on the design:
246
-------------------------------------
247
definition: the end of any cluster or 'merge' occurs when:
248
1 - the next revision has a lower merge depth than we do.
255
C, D are the ends of clusters, E might be but we need more data.
256
2 - or the next revision at our merge depth is not our left most
258
This is required to handle multiple-merges in one commit.
266
C is the end of a cluster due to rule 1.
267
D is not the end of a cluster from rule 1, but is from rule 2: E
268
is not its left most ancestor
269
E is the end of a cluster due to rule 1
270
F might be but we need more data.
272
we show connecting lines to a parent when:
273
- The parent is the start of a merge within this cluster.
274
That is, the merge was not done to the mainline before this cluster
275
was merged to the mainline.
276
This can be detected thus:
277
* The parent has a higher merge depth and is the next revision in
280
The next revision in the list constraint is needed for this case:
282
B 1 [C, F] # we do not want to show a line to F which is depth 2
284
C 1 [H] # note that this is a long line to show back to the
285
ancestor - see the end of merge rules.
291
- Part of this merges 'branch':
292
The parent has the same merge depth and is our left most parent and we
293
are not the end of the cluster.
294
A 0 [C, B] lines: [B, C]
295
B 1 [E, C] lines: [C]
297
D 0 [F, E] lines: [E, F]
300
- The end of this merge/cluster:
301
we can ONLY have multiple parents at the end of a cluster if this
302
branch was previously merged into the 'mainline'.
303
- if we have one and only one parent, show it
304
Note that this may be to a greater merge depth - for instance if
305
this branch continued from a deeply nested branch to add something
307
- if we have more than one parent - show the second oldest (older ==
308
further down the list) parent with
309
an equal or lower merge depth
310
XXXX revisit when awake. ddaa asks about the relevance of each one
311
- maybe more than one parent is relevant
313
self._generate_revno = generate_revno
314
# a dict of the graph.
315
self._graph = dict(graph)
316
# if there is an explicit mainline, alter the graph to match. This is
317
# easier than checking at every merge whether we are on the mainline and
318
# if so which path to take.
319
if mainline_revisions is None:
320
self._mainline_revisions = []
321
self._stop_revision = None
323
self._mainline_revisions = list(mainline_revisions)
324
self._stop_revision = self._mainline_revisions[0]
325
# skip the first revision, its what we reach and its parents are
326
# therefore irrelevant
327
for index, revision in enumerate(self._mainline_revisions[1:]):
328
# NB: index 0 means self._mainline_revisions[1]
329
# if the mainline matches the graph, nothing to do.
330
parent = self._mainline_revisions[index]
332
# end of mainline_revisions history
334
if self._graph[revision][0] == parent:
336
# remove it from its prior spot
337
self._graph[revision].remove(parent)
338
# insert it into the start of the mainline
339
self._graph[revision].insert(0, parent)
340
# we need to do a check late in the process to detect end-of-merges
341
# which requires the parents to be accessible: its easier for now
342
# to just keep the original graph around.
343
self._original_graph = dict(self._graph.items())
344
# we need to know the revision numbers of revisions to determine
345
# the revision numbers of their descendants
346
# this is a graph from node to [revno_tuple, sequence_number]
347
# where sequence is the number of branches made from the node,
348
# and revno_tuple is the tuple that was assigned to the node.
349
# we dont know revnos to start with, so we start it seeded with
351
self._revnos = dict((revision, [None, 0]) for revision in self._graph)
352
# the global implicit root node has revno 0, but we need to know
353
# the sequence number for it too:
354
self._root_sequence = 0
356
# this is a stack storing the depth first search into the graph.
357
self._node_name_stack = []
358
# at each level of recursion we need the merge depth this node is at:
359
self._node_merge_depth_stack = []
360
# at each level of 'recursion' we have to check each parent. This
361
# stack stores the parents we have not yet checked for the node at the
362
# matching depth in _node_name_stack
363
self._pending_parents_stack = []
364
# When we first look at a node we assign it a seqence number from its
366
self._assigned_sequence_stack = []
367
# this is a set of the nodes who have been completely analysed for fast
368
# membership checking
369
self._completed_node_names = set()
370
# this is the scheduling of nodes list.
371
# Nodes are scheduled
372
# from the bottom left of the tree: in the tree
379
# the scheduling order is: F, E, D, C, B, A
380
# that is - 'left subtree, right subtree, node'
381
# which would mean that when we schedule A we can emit the entire tree.
382
self._scheduled_nodes = []
383
# This records for each node when we have processed its left most
384
# unmerged subtree. After this subtree is scheduled, all other subtrees
385
# have their merge depth increased by one from this nodes merge depth.
386
# it contains tuples - name, merge_depth
387
self._left_subtree_pushed_stack = []
389
# seed the search with the tip of the branch
390
if branch_tip is not None:
391
parents = self._graph.pop(branch_tip)
392
self._push_node(branch_tip, 0, parents)
395
"""Sort the graph and return as a list.
397
After calling this the sorter is empty and you must create a new one.
399
return list(self.iter_topo_order())
401
def iter_topo_order(self):
402
"""Yield the nodes of the graph in a topological order.
404
After finishing iteration the sorter is empty and you cannot continue
407
while self._node_name_stack:
408
# loop until this call completes.
409
parents_to_visit = self._pending_parents_stack[-1]
410
# if all parents are done, the revision is done
411
if not parents_to_visit:
412
# append the revision to the topo sorted scheduled list:
413
# all the nodes parents have been scheduled added, now
414
# we can add it to the output.
417
while self._pending_parents_stack[-1]:
418
if not self._left_subtree_pushed_stack[-1]:
419
# recurse depth first into the primary parent
420
next_node_name = self._pending_parents_stack[-1].pop(0)
422
# place any merges in right-to-left order for scheduling
423
# which gives us left-to-right order after we reverse
424
# the scheduled queue. XXX: This has the effect of
425
# allocating common-new revisions to the right-most
426
# subtree rather than the left most, which will
427
# display nicely (you get smaller trees at the top
428
# of the combined merge).
429
next_node_name = self._pending_parents_stack[-1].pop()
430
if next_node_name in self._completed_node_names:
431
# this parent was completed by a child on the
432
# call stack. skip it.
434
# otherwise transfer it from the source graph into the
435
# top of the current depth first search stack.
437
parents = self._graph.pop(next_node_name)
439
# if the next node is not in the source graph it has
440
# already been popped from it and placed into the
441
# current search stack (but not completed or we would
442
# have hit the continue 4 lines up.
443
# this indicates a cycle.
444
raise errors.GraphCycleError(self._node_name_stack)
446
if self._left_subtree_pushed_stack[-1]:
447
# a new child branch from name_stack[-1]
451
self._left_subtree_pushed_stack[-1] = True
453
self._node_merge_depth_stack[-1] + next_merge_depth)
458
# and do not continue processing parents until this 'call'
461
# We have scheduled the graph. Now deliver the ordered output:
463
while self._scheduled_nodes:
464
node_name, merge_depth, revno = self._scheduled_nodes.pop()
465
if node_name == self._stop_revision:
467
if not len(self._scheduled_nodes):
468
# last revision is the end of a merge
470
elif self._scheduled_nodes[-1][1] < merge_depth:
471
# the next node is to our left
473
elif (self._scheduled_nodes[-1][1] == merge_depth and
474
(self._scheduled_nodes[-1][0] not in
475
self._original_graph[node_name])):
476
# the next node was part of a multiple-merge.
480
if self._generate_revno:
481
yield (sequence_number, node_name, merge_depth, revno, end_of_merge)
483
yield (sequence_number, node_name, merge_depth, end_of_merge)
486
def _push_node(self, node_name, merge_depth, parents):
487
"""Add node_name to the pending node stack.
489
Names in this stack will get emitted into the output as they are popped
492
self._node_name_stack.append(node_name)
493
self._node_merge_depth_stack.append(merge_depth)
494
self._left_subtree_pushed_stack.append(False)
495
self._pending_parents_stack.append(list(parents))
496
# as we push it, assign it a sequence number against its parent:
497
parents = self._original_graph[node_name]
499
# node has parents, assign from the left most parent.
500
parent_revno = self._revnos[parents[0]]
501
sequence = parent_revno[1]
504
# no parents, use the root sequence
505
sequence = self._root_sequence
506
self._root_sequence +=1
507
self._assigned_sequence_stack.append(sequence)
510
"""Pop the top node off the stack
512
The node is appended to the sorted output.
514
# we are returning from the flattened call frame:
515
# pop off the local variables
516
node_name = self._node_name_stack.pop()
517
merge_depth = self._node_merge_depth_stack.pop()
518
sequence = self._assigned_sequence_stack.pop()
519
# remove this node from the pending lists:
520
self._left_subtree_pushed_stack.pop()
521
self._pending_parents_stack.pop()
523
parents = self._original_graph[node_name]
525
# node has parents, assign from the left most parent.
526
parent_revno = self._revnos[parents[0]]
528
# not the first child, make a new branch
529
revno = parent_revno[0] + (sequence, 1)
531
# increment the sequence number within the branch
532
revno = parent_revno[0][:-1] + (parent_revno[0][-1] + 1,)
534
# no parents, use the root sequence
536
# make a parallel import revision number
537
revno = (0, sequence, 1)
541
# store the revno for this node for future reference
542
self._revnos[node_name][0] = revno
543
self._completed_node_names.add(node_name)
544
self._scheduled_nodes.append((node_name, merge_depth, self._revnos[node_name][0]))