1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
# Copyright (C) 2005, 2006 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Topological sorting routines."""
from bzrlib import errors
__all__ = ["topo_sort", "TopoSorter", "merge_sort", "MergeSorter"]
def topo_sort(graph):
"""Topological sort a graph.
graph -- sequence of pairs of node->parents_list.
The result is a list of node names, such that all parents come before
their children.
node identifiers can be any hashable object, and are typically strings.
"""
return TopoSorter(graph).sorted()
class TopoSorter(object):
def __init__(self, graph):
"""Topological sorting of a graph.
:param graph: sequence of pairs of node_name->parent_names_list.
i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
For this input the output from the sort or
iter_topo_order routines will be:
'A', 'B', 'C'
node identifiers can be any hashable object, and are typically strings.
If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
one of the two values for 'a'.
The graph is sorted lazily: until you iterate or sort the input is
not processed other than to create an internal representation.
iteration or sorting may raise GraphCycleError if a cycle is present
in the graph.
"""
# a dict of the graph.
self._graph = dict(graph)
### if debugging:
# self._original_graph = dict(graph)
# this is a stack storing the depth first search into the graph.
self._node_name_stack = []
# at each level of 'recursion' we have to check each parent. This
# stack stores the parents we have not yet checked for the node at the
# matching depth in _node_name_stack
self._pending_parents_stack = []
# this is a set of the completed nodes for fast checking whether a
# parent in a node we are processing on the stack has already been
# emitted and thus can be skipped.
self._completed_node_names = set()
def sorted(self):
"""Sort the graph and return as a list.
After calling this the sorter is empty and you must create a new one.
"""
return list(self.iter_topo_order())
### Useful if fiddling with this code.
### # cross check
### sorted_names = list(self.iter_topo_order())
### for index in range(len(sorted_names)):
### rev = sorted_names[index]
### for left_index in range(index):
### if rev in self.original_graph[sorted_names[left_index]]:
### print "revision in parent list of earlier revision"
### import pdb;pdb.set_trace()
def iter_topo_order(self):
"""Yield the nodes of the graph in a topological order.
After finishing iteration the sorter is empty and you cannot continue
iteration.
"""
while self._graph:
# now pick a random node in the source graph, and transfer it to the
# top of the depth first search stack.
node_name, parents = self._graph.popitem()
self._push_node(node_name, parents)
while self._node_name_stack:
# loop until this call completes.
parents_to_visit = self._pending_parents_stack[-1]
# if all parents are done, the revision is done
if not parents_to_visit:
# append the revision to the topo sorted list
# all the nodes parents have been added to the output, now
# we can add it to the output.
yield self._pop_node()
else:
while self._pending_parents_stack[-1]:
# recurse depth first into a single parent
next_node_name = self._pending_parents_stack[-1].pop()
if next_node_name in self._completed_node_names:
# this parent was completed by a child on the
# call stack. skip it.
continue
# otherwise transfer it from the source graph into the
# top of the current depth first search stack.
try:
parents = self._graph.pop(next_node_name)
except KeyError:
# if the next node is not in the source graph it has
# already been popped from it and placed into the
# current search stack (but not completed or we would
# have hit the continue 4 lines up.
# this indicates a cycle.
raise errors.GraphCycleError(self._node_name_stack)
self._push_node(next_node_name, parents)
# and do not continue processing parents until this 'call'
# has recursed.
break
def _push_node(self, node_name, parents):
"""Add node_name to the pending node stack.
Names in this stack will get emitted into the output as they are popped
off the stack.
"""
self._node_name_stack.append(node_name)
self._pending_parents_stack.append(list(parents))
def _pop_node(self):
"""Pop the top node off the stack
The node is appended to the sorted output.
"""
# we are returning from the flattened call frame:
# pop off the local variables
node_name = self._node_name_stack.pop()
self._pending_parents_stack.pop()
self._completed_node_names.add(node_name)
return node_name
def merge_sort(graph, branch_tip, mainline_revisions=None, generate_revno=False):
"""Topological sort a graph which groups merges.
:param graph: sequence of pairs of node->parents_list.
:param branch_tip: the tip of the branch to graph. Revisions not
reachable from branch_tip are not included in the
output.
:param mainline_revisions: If not None this forces a mainline to be
used rather than synthesised from the graph.
This must be a valid path through some part
of the graph. If the mainline does not cover all
the revisions, output stops at the start of the
old revision listed in the mainline revisions
list.
The order for this parameter is oldest-first.
:param generate_revno: Optional parameter controlling the generation of
revision number sequences in the output. See the output description of
the MergeSorter docstring for details.
:result: See the MergeSorter docstring for details.
node identifiers can be any hashable object, and are typically strings.
"""
return MergeSorter(graph, branch_tip, mainline_revisions,
generate_revno).sorted()
class MergeSorter(object):
__slots__ = ['_node_name_stack',
'_node_merge_depth_stack',
'_pending_parents_stack',
'_assigned_sequence_stack',
'_left_subtree_pushed_stack',
'_generate_revno',
'_graph',
'_mainline_revisions',
'_stop_revision',
'_original_graph',
'_revnos',
'_root_sequence',
'_completed_node_names',
'_scheduled_nodes',
]
def __init__(self, graph, branch_tip, mainline_revisions=None,
generate_revno=False):
"""Merge-aware topological sorting of a graph.
:param graph: sequence of pairs of node_name->parent_names_list.
i.e. [('C', ['B']), ('B', ['A']), ('A', [])]
For this input the output from the sort or
iter_topo_order routines will be:
'A', 'B', 'C'
:param branch_tip: the tip of the branch to graph. Revisions not
reachable from branch_tip are not included in the
output.
:param mainline_revisions: If not None this forces a mainline to be
used rather than synthesised from the graph.
This must be a valid path through some part
of the graph. If the mainline does not cover all
the revisions, output stops at the start of the
old revision listed in the mainline revisions
list.
The order for this parameter is oldest-first.
:param generate_revno: Optional parameter controlling the generation of
revision number sequences in the output. See the output description
for more details.
The result is a list sorted so that all parents come before
their children. Each element of the list is a tuple containing:
(sequence_number, node_name, merge_depth, end_of_merge)
* sequence_number: The sequence of this row in the output. Useful for
GUIs.
* node_name: The node name: opaque text to the merge routine.
* merge_depth: How many levels of merging deep this node has been
found.
* revno_sequence: When requested this field provides a sequence of
revision numbers for all revisions. The format is:
REVNO[[.BRANCHREVNO.REVNO] ...]. BRANCHREVNO is the number of the
branch that the revno is on. From left to right the REVNO numbers
are the sequence numbers within that branch of the revision.
For instance, the graph {A:[], B:['A'], C:['A', 'B']} will get
the following revno_sequences assigned: A:(1,), B:(1,1,1), C:(2,).
This should be read as 'A is the first commit in the trunk',
'B is the first commit on the first branch made from A', 'C is the
second commit in the trunk'.
* end_of_merge: When True the next node is part of a different merge.
node identifiers can be any hashable object, and are typically strings.
If you have a graph like [('a', ['b']), ('a', ['c'])] this will only use
one of the two values for 'a'.
The graph is sorted lazily: until you iterate or sort the input is
not processed other than to create an internal representation.
iteration or sorting may raise GraphCycleError if a cycle is present
in the graph.
Background information on the design:
-------------------------------------
definition: the end of any cluster or 'merge' occurs when:
1 - the next revision has a lower merge depth than we do.
i.e.
A 0
B 1
C 2
D 1
E 0
C, D are the ends of clusters, E might be but we need more data.
2 - or the next revision at our merge depth is not our left most
ancestor.
This is required to handle multiple-merges in one commit.
i.e.
A 0 [F, B, E]
B 1 [D, C]
C 2 [D]
D 1 [F]
E 1 [F]
F 0
C is the end of a cluster due to rule 1.
D is not the end of a cluster from rule 1, but is from rule 2: E
is not its left most ancestor
E is the end of a cluster due to rule 1
F might be but we need more data.
we show connecting lines to a parent when:
- The parent is the start of a merge within this cluster.
That is, the merge was not done to the mainline before this cluster
was merged to the mainline.
This can be detected thus:
* The parent has a higher merge depth and is the next revision in
the list.
The next revision in the list constraint is needed for this case:
A 0 [D, B]
B 1 [C, F] # we do not want to show a line to F which is depth 2
but not a merge
C 1 [H] # note that this is a long line to show back to the
ancestor - see the end of merge rules.
D 0 [G, E]
E 1 [G, F]
F 2 [G]
G 1 [H]
H 0
- Part of this merges 'branch':
The parent has the same merge depth and is our left most parent and we
are not the end of the cluster.
A 0 [C, B] lines: [B, C]
B 1 [E, C] lines: [C]
C 0 [D] lines: [D]
D 0 [F, E] lines: [E, F]
E 1 [F] lines: [F]
F 0
- The end of this merge/cluster:
we can ONLY have multiple parents at the end of a cluster if this
branch was previously merged into the 'mainline'.
- if we have one and only one parent, show it
Note that this may be to a greater merge depth - for instance if
this branch continued from a deeply nested branch to add something
to it.
- if we have more than one parent - show the second oldest (older ==
further down the list) parent with
an equal or lower merge depth
XXXX revisit when awake. ddaa asks about the relevance of each one
- maybe more than one parent is relevant
"""
self._generate_revno = generate_revno
# a dict of the graph.
self._graph = dict(graph)
# if there is an explicit mainline, alter the graph to match. This is
# easier than checking at every merge whether we are on the mainline and
# if so which path to take.
if mainline_revisions is None:
self._mainline_revisions = []
self._stop_revision = None
else:
self._mainline_revisions = list(mainline_revisions)
self._stop_revision = self._mainline_revisions[0]
# skip the first revision, its what we reach and its parents are
# therefore irrelevant
for index, revision in enumerate(self._mainline_revisions[1:]):
# NB: index 0 means self._mainline_revisions[1]
# if the mainline matches the graph, nothing to do.
parent = self._mainline_revisions[index]
if parent is None:
# end of mainline_revisions history
continue
if self._graph[revision][0] == parent:
continue
# remove it from its prior spot
self._graph[revision].remove(parent)
# insert it into the start of the mainline
self._graph[revision].insert(0, parent)
# we need to do a check late in the process to detect end-of-merges
# which requires the parents to be accessible: its easier for now
# to just keep the original graph around.
self._original_graph = dict(self._graph.items())
# we need to know the revision numbers of revisions to determine
# the revision numbers of their descendants
# this is a graph from node to [revno_tuple, sequence_number]
# where sequence is the number of branches made from the node,
# and revno_tuple is the tuple that was assigned to the node.
# we dont know revnos to start with, so we start it seeded with
# [None, 0]
self._revnos = dict((revision, [None, 0]) for revision in self._graph)
# the global implicit root node has revno 0, but we need to know
# the sequence number for it too:
self._root_sequence = 0
# this is a stack storing the depth first search into the graph.
self._node_name_stack = []
# at each level of recursion we need the merge depth this node is at:
self._node_merge_depth_stack = []
# at each level of 'recursion' we have to check each parent. This
# stack stores the parents we have not yet checked for the node at the
# matching depth in _node_name_stack
self._pending_parents_stack = []
# When we first look at a node we assign it a seqence number from its
# leftmost parent.
self._assigned_sequence_stack = []
# this is a set of the nodes who have been completely analysed for fast
# membership checking
self._completed_node_names = set()
# this is the scheduling of nodes list.
# Nodes are scheduled
# from the bottom left of the tree: in the tree
# A 0 [D, B]
# B 1 [C]
# C 1 [D]
# D 0 [F, E]
# E 1 [F]
# F 0
# the scheduling order is: F, E, D, C, B, A
# that is - 'left subtree, right subtree, node'
# which would mean that when we schedule A we can emit the entire tree.
self._scheduled_nodes = []
# This records for each node when we have processed its left most
# unmerged subtree. After this subtree is scheduled, all other subtrees
# have their merge depth increased by one from this nodes merge depth.
# it contains tuples - name, merge_depth
self._left_subtree_pushed_stack = []
# seed the search with the tip of the branch
if branch_tip is not None:
parents = self._graph.pop(branch_tip)
self._push_node(branch_tip, 0, parents)
def sorted(self):
"""Sort the graph and return as a list.
After calling this the sorter is empty and you must create a new one.
"""
return list(self.iter_topo_order())
def iter_topo_order(self):
"""Yield the nodes of the graph in a topological order.
After finishing iteration the sorter is empty and you cannot continue
iteration.
"""
# These are safe to offload to local variables, because they are used
# as a stack and modified in place, never assigned to.
node_name_stack = self._node_name_stack
node_merge_depth_stack = self._node_merge_depth_stack
pending_parents_stack = self._pending_parents_stack
left_subtree_pushed_stack = self._left_subtree_pushed_stack
completed_node_names = self._completed_node_names
scheduled_nodes = self._scheduled_nodes
graph_pop = self._graph.pop
def push_node(node_name, merge_depth, parents,
node_name_stack_append=node_name_stack.append,
node_merge_depth_stack_append=node_merge_depth_stack.append,
left_subtree_pushed_stack_append=left_subtree_pushed_stack.append,
pending_parents_stack_append=pending_parents_stack.append,
assigned_sequence_stack_append=self._assigned_sequence_stack.append,
original_graph=self._original_graph,
revnos=self._revnos,
):
"""Add node_name to the pending node stack.
Names in this stack will get emitted into the output as they are popped
off the stack.
This inlines a lot of self._variable.append functions as local
variables.
"""
node_name_stack_append(node_name)
node_merge_depth_stack_append(merge_depth)
left_subtree_pushed_stack_append(False)
pending_parents_stack_append(list(parents))
# as we push it, assign it a sequence number against its parent:
parents = original_graph[node_name]
if parents:
# node has parents, assign from the left most parent.
parent_revno = revnos[parents[0]]
sequence = parent_revno[1]
parent_revno[1] += 1
else:
# no parents, use the root sequence
sequence = self._root_sequence
self._root_sequence +=1
assigned_sequence_stack_append(sequence)
def pop_node(node_name_stack_pop=node_name_stack.pop,
node_merge_depth_stack_pop=node_merge_depth_stack.pop,
assigned_sequence_stack_pop=self._assigned_sequence_stack.pop,
left_subtree_pushed_stack_pop=left_subtree_pushed_stack.pop,
pending_parents_stack_pop=pending_parents_stack.pop,
original_graph=self._original_graph,
revnos=self._revnos,
completed_node_names_add=self._completed_node_names.add,
scheduled_nodes_append=scheduled_nodes.append,
):
"""Pop the top node off the stack
The node is appended to the sorted output.
"""
# we are returning from the flattened call frame:
# pop off the local variables
node_name = node_name_stack_pop()
merge_depth = node_merge_depth_stack_pop()
sequence = assigned_sequence_stack_pop()
# remove this node from the pending lists:
left_subtree_pushed_stack_pop()
pending_parents_stack_pop()
parents = original_graph[node_name]
if parents:
# node has parents, assign from the left most parent.
parent_revno = revnos[parents[0]]
if sequence:
# not the first child, make a new branch
revno = parent_revno[0] + (sequence, 1)
else:
# increment the sequence number within the branch
revno = parent_revno[0][:-1] + (parent_revno[0][-1] + 1,)
else:
# no parents, use the root sequence
if sequence:
# make a parallel import revision number
revno = (0, sequence, 1)
else:
revno = (1,)
# store the revno for this node for future reference
revnos[node_name][0] = revno
completed_node_names_add(node_name)
scheduled_nodes_append((node_name, merge_depth, revno))
return node_name
while node_name_stack:
# loop until this call completes.
parents_to_visit = pending_parents_stack[-1]
# if all parents are done, the revision is done
if not parents_to_visit:
# append the revision to the topo sorted scheduled list:
# all the nodes parents have been scheduled added, now
# we can add it to the output.
pop_node()
else:
while pending_parents_stack[-1]:
if not left_subtree_pushed_stack[-1]:
# recurse depth first into the primary parent
next_node_name = pending_parents_stack[-1].pop(0)
else:
# place any merges in right-to-left order for scheduling
# which gives us left-to-right order after we reverse
# the scheduled queue. XXX: This has the effect of
# allocating common-new revisions to the right-most
# subtree rather than the left most, which will
# display nicely (you get smaller trees at the top
# of the combined merge).
next_node_name = pending_parents_stack[-1].pop()
if next_node_name in completed_node_names:
# this parent was completed by a child on the
# call stack. skip it.
continue
# otherwise transfer it from the source graph into the
# top of the current depth first search stack.
try:
parents = graph_pop(next_node_name)
except KeyError:
# if the next node is not in the source graph it has
# already been popped from it and placed into the
# current search stack (but not completed or we would
# have hit the continue 4 lines up.
# this indicates a cycle.
raise errors.GraphCycleError(node_name_stack)
next_merge_depth = 0
if left_subtree_pushed_stack[-1]:
# a new child branch from name_stack[-1]
next_merge_depth = 1
else:
next_merge_depth = 0
left_subtree_pushed_stack[-1] = True
next_merge_depth = (
node_merge_depth_stack[-1] + next_merge_depth)
push_node(
next_node_name,
next_merge_depth,
parents)
# and do not continue processing parents until this 'call'
# has recursed.
break
# We have scheduled the graph. Now deliver the ordered output:
sequence_number = 0
stop_revision = self._stop_revision
generate_revno = self._generate_revno
original_graph = self._original_graph
while scheduled_nodes:
node_name, merge_depth, revno = scheduled_nodes.pop()
if node_name == stop_revision:
return
if not len(scheduled_nodes):
# last revision is the end of a merge
end_of_merge = True
elif scheduled_nodes[-1][1] < merge_depth:
# the next node is to our left
end_of_merge = True
elif (scheduled_nodes[-1][1] == merge_depth and
(scheduled_nodes[-1][0] not in
original_graph[node_name])):
# the next node was part of a multiple-merge.
end_of_merge = True
else:
end_of_merge = False
if generate_revno:
yield (sequence_number, node_name, merge_depth, revno, end_of_merge)
else:
yield (sequence_number, node_name, merge_depth, end_of_merge)
sequence_number += 1
def _push_node(self, node_name, merge_depth, parents):
"""Add node_name to the pending node stack.
Names in this stack will get emitted into the output as they are popped
off the stack.
"""
self._node_name_stack.append(node_name)
self._node_merge_depth_stack.append(merge_depth)
self._left_subtree_pushed_stack.append(False)
self._pending_parents_stack.append(list(parents))
# as we push it, assign it a sequence number against its parent:
parents = self._original_graph[node_name]
if parents:
# node has parents, assign from the left most parent.
parent_revno = self._revnos[parents[0]]
sequence = parent_revno[1]
parent_revno[1] += 1
else:
# no parents, use the root sequence
sequence = self._root_sequence
self._root_sequence +=1
self._assigned_sequence_stack.append(sequence)
def _pop_node(self):
"""Pop the top node off the stack
The node is appended to the sorted output.
"""
# we are returning from the flattened call frame:
# pop off the local variables
node_name = self._node_name_stack.pop()
merge_depth = self._node_merge_depth_stack.pop()
sequence = self._assigned_sequence_stack.pop()
# remove this node from the pending lists:
self._left_subtree_pushed_stack.pop()
self._pending_parents_stack.pop()
parents = self._original_graph[node_name]
if parents:
# node has parents, assign from the left most parent.
parent_revno = self._revnos[parents[0]]
if sequence:
# not the first child, make a new branch
revno = parent_revno[0] + (sequence, 1)
else:
# increment the sequence number within the branch
revno = parent_revno[0][:-1] + (parent_revno[0][-1] + 1,)
else:
# no parents, use the root sequence
if sequence:
# make a parallel import revision number
revno = (0, sequence, 1)
else:
revno = (1,)
# store the revno for this node for future reference
self._revnos[node_name][0] = revno
self._completed_node_names.add(node_name)
self._scheduled_nodes.append((node_name, merge_depth, self._revnos[node_name][0]))
return node_name
|