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
|
# Copyright (C) 2007 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
from bzrlib.deprecated_graph import (node_distances, select_farthest)
from bzrlib.revision import NULL_REVISION
class _StackedParentsProvider(object):
def __init__(self, parent_providers):
self._parent_providers = parent_providers
def get_parents(self, revision_ids):
"""
Find revision ids of the parents of a list of revisions
A list is returned of the same length as the input. Each entry
is a list of parent ids for the corresponding input revision.
[NULL_REVISION] is used as the parent of the first user-committed
revision. Its parent list is empty.
If the revision is not present (i.e. a ghost), None is used in place
of the list of parents.
"""
found = {}
for parents_provider in self._parent_providers:
parent_list = parents_provider.get_parents(
[r for r in revision_ids if r not in found])
new_found = dict((k, v) for k, v in zip(revision_ids, parent_list)
if v is not None)
found.update(new_found)
if len(found) == len(revision_ids):
break
return [found.get(r) for r in revision_ids]
class GraphWalker(object):
"""Provide incremental access to revision graphs.
This is the generic implementation; it is intended to be subclassed to
specialize it for other repository types.
"""
def __init__(self, parents_provider):
"""Construct a GraphWalker that uses several graphs as its input
This should not normally be invoked directly, because there may be
specialized implementations for particular repository types. See
Repository.get_graph()
:param parents_func: a list of objects providing a get_parents call
conforming to the behavior of GraphWalker.get_parents
"""
self.get_parents = parents_provider.get_parents
def find_lca(self, *revisions):
"""Determine the lowest common ancestors of the provided revisions
A lowest common ancestor is a common ancestor none of whose
descendants are common ancestors. In graphs, unlike trees, there may
be multiple lowest common ancestors.
This algorithm has two phases. Phase 1 identifies border ancestors,
and phase 2 filters border ancestors to determine lowest common
ancestors.
In phase 1, border ancestors are identified, using a breadth-first
search starting at the bottom of the graph. Searches are stopped
whenever a node or one of its descendants is determined to be common
In phase 2, the border ancestors are filtered to find the least
common ancestors. This is done by searching the ancestries of each
border ancestor.
Phase 2 is perfomed on the principle that a border ancestor that is
not an ancestor of any other border ancestor is a least common
ancestor.
Searches are stopped when they find a node that is determined to be a
common ancestor of all border ancestors, because this shows that it
cannot be a descendant of any border ancestor.
The scaling of this operation should be proportional to
1. The number of uncommon ancestors
2. The number of border ancestors
3. The length of the shortest path between a border ancestor and an
ancestor of all border ancestors.
"""
border_common = self._find_border_ancestors(revisions)
return self._filter_candidate_lca(border_common)
def _find_border_ancestors(self, revisions):
"""Find common ancestors with at least one uncommon descendant.
Border ancestors are identified using a breadth-first
search starting at the bottom of the graph. Searches are stopped
whenever a node or one of its descendants is determined to be common.
This will scale with the number of uncommon ancestors.
"""
common_walker = _AncestryWalker([], self)
common_ancestors = set()
walkers = [_AncestryWalker([r], self) for r in revisions]
active_walkers = walkers[:]
border_ancestors = set()
def update_common(walker, revisions):
w_seen_ancestors = walker.find_seen_ancestors(
revision)
stopped = walker.stop_searching_any(w_seen_ancestors)
common_ancestors.update(w_seen_ancestors)
common_walker.start_searching(stopped)
while True:
if len(active_walkers) == 0:
return border_ancestors
try:
new_common = common_walker.next()
common_ancestors.update(new_common)
except StopIteration:
pass
else:
for walker in active_walkers:
for revision in new_common.intersection(walker.seen):
update_common(walker, revision)
newly_seen = set()
new_active_walkers = []
for walker in active_walkers:
try:
newly_seen.update(walker.next())
except StopIteration:
pass
else:
new_active_walkers.append(walker)
active_walkers = new_active_walkers
for revision in newly_seen:
if revision in common_ancestors:
for walker in walkers:
update_common(walker, revision)
continue
for walker in walkers:
if revision not in walker.seen:
break
else:
border_ancestors.add(revision)
for walker in walkers:
update_common(walker, revision)
def _filter_candidate_lca(self, candidate_lca):
"""Remove candidates which are ancestors of other candidates.
This is done by searching the ancestries of each border ancestor. It
is perfomed on the principle that a border ancestor that is not an
ancestor of any other border ancestor is a lowest common ancestor.
Searches are stopped when they find a node that is determined to be a
common ancestor of all border ancestors, because this shows that it
cannot be a descendant of any border ancestor.
This will scale with the number of candidate ancestors and the length
of the shortest path from a candidate to an ancestor common to all
candidates.
"""
walkers = dict((c, _AncestryWalker([c], self))
for c in candidate_lca)
active_walkers = dict(walkers)
# skip over the actual candidate for each walker
for walker in active_walkers.itervalues():
walker.next()
while len(active_walkers) > 0:
for candidate, walker in list(active_walkers.iteritems()):
try:
ancestors = walker.next()
except StopIteration:
del active_walkers[candidate]
continue
for ancestor in ancestors:
if ancestor in candidate_lca:
candidate_lca.remove(ancestor)
del walkers[ancestor]
if ancestor in active_walkers:
del active_walkers[ancestor]
for walker in walkers.itervalues():
if ancestor not in walker.seen:
break
else:
# if this revision was seen by all walkers, then it
# is a descendant of all candidates, so we can stop
# searching it, and any seen ancestors
for walker in walkers.itervalues():
seen_ancestors =\
walker.find_seen_ancestors(ancestor)
walker.stop_searching_any(seen_ancestors)
return candidate_lca
def find_unique_lca(self, left_revision, right_revision):
"""Find a unique LCA.
Find lowest common ancestors. If there is no unique common
ancestor, find the lowest common ancestors of those ancestors.
Iteration stops when a unique lowest common ancestor is found.
The graph origin is necessarily a unique lowest common ancestor.
Note that None is not an acceptable substitute for NULL_REVISION.
in the input for this method.
"""
revisions = [left_revision, right_revision]
while True:
lca = self.find_lca(*revisions)
if len(lca) == 1:
return lca.pop()
revisions = lca
class _AncestryWalker(object):
"""Walk the ancestry of a single revision.
This class implements the iterator protocol, but additionally
1. provides a set of seen ancestors, and
2. allows some ancestries to be unsearched, via stop_searching_any
"""
def __init__(self, revisions, graph_walker):
self._start = set(revisions)
self._search_revisions = None
self.seen = set(revisions)
self._graph_walker = graph_walker
def __repr__(self):
return '_AncestryWalker(self._search_revisions=%r, self.seen=%r)' %\
(self._search_revisions, self.seen)
def next(self):
"""Return the next ancestors of this revision.
Ancestors are returned in the order they are seen in a breadth-first
traversal. No ancestor will be returned more than once.
"""
if self._search_revisions is None:
self._search_revisions = self._start
else:
new_search_revisions = set()
for parents in self._graph_walker.get_parents(
self._search_revisions):
if parents is None:
continue
new_search_revisions.update(p for p in parents if
p not in self.seen)
self._search_revisions = new_search_revisions
if len(self._search_revisions) == 0:
raise StopIteration()
self.seen.update(self._search_revisions)
return self._search_revisions
def __iter__(self):
return self
def find_seen_ancestors(self, revision):
"""Find ancstors of this revision that have already been seen."""
walker = _AncestryWalker([revision], self._graph_walker)
seen_ancestors = set()
for ancestors in walker:
for ancestor in ancestors:
if ancestor not in self.seen:
walker.stop_searching_any([ancestor])
else:
seen_ancestors.add(ancestor)
return seen_ancestors
def stop_searching_any(self, revisions):
"""
Remove any of the specified revisions from the search list.
None of the specified revisions are required to be present in the
search list. In this case, the call is a no-op.
"""
stopped_searches = set(l for l in self._search_revisions
if l in revisions)
self._search_revisions = set(l for l in self._search_revisions
if l not in revisions)
return stopped_searches
def start_searching(self, revisions):
if self._search_revisions is None:
self._start = set(revisions)
else:
self._search_revisions.update(r for r in revisions if
r not in self.seen)
self.seen.update(revisions)
|