/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/graph.py

  • Committer: Jelmer Vernooij
  • Date: 2009-04-02 15:54:49 UTC
  • mto: (0.200.326 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090402155449-nuqhu1fsnqk6bt0g
Check that regenerated objects have the expected sha1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
from bzrlib import (
18
 
    errors,
19
 
    tsort,
20
 
    )
21
 
from bzrlib.deprecated_graph import (node_distances, select_farthest)
22
 
from bzrlib.revision import NULL_REVISION
23
 
 
24
 
# DIAGRAM of terminology
25
 
#       A
26
 
#       /\
27
 
#      B  C
28
 
#      |  |\
29
 
#      D  E F
30
 
#      |\/| |
31
 
#      |/\|/
32
 
#      G  H
33
 
#
34
 
# In this diagram, relative to G and H:
35
 
# A, B, C, D, E are common ancestors.
36
 
# C, D and E are border ancestors, because each has a non-common descendant.
37
 
# D and E are least common ancestors because none of their descendants are
38
 
# common ancestors.
39
 
# C is not a least common ancestor because its descendant, E, is a common
40
 
# ancestor.
41
 
#
42
 
# The find_unique_lca algorithm will pick A in two steps:
43
 
# 1. find_lca('G', 'H') => ['D', 'E']
44
 
# 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
45
 
 
46
 
 
47
 
 
48
 
class _StackedParentsProvider(object):
49
 
 
50
 
    def __init__(self, parent_providers):
51
 
        self._parent_providers = parent_providers
52
 
 
53
 
    def __repr__(self):
54
 
        return "_StackedParentsProvider(%r)" % self._parent_providers
55
 
 
56
 
    def get_parents(self, revision_ids):
57
 
        """Find revision ids of the parents of a list of revisions
58
 
 
59
 
        A list is returned of the same length as the input.  Each entry
60
 
        is a list of parent ids for the corresponding input revision.
61
 
 
62
 
        [NULL_REVISION] is used as the parent of the first user-committed
63
 
        revision.  Its parent list is empty.
64
 
 
65
 
        If the revision is not present (i.e. a ghost), None is used in place
66
 
        of the list of parents.
67
 
        """
68
 
        found = {}
69
 
        for parents_provider in self._parent_providers:
70
 
            pending_revisions = [r for r in revision_ids if r not in found]
71
 
            parent_list = parents_provider.get_parents(pending_revisions)
72
 
            new_found = dict((k, v) for k, v in zip(pending_revisions,
73
 
                             parent_list) if v is not None)
74
 
            found.update(new_found)
75
 
            if len(found) == len(revision_ids):
76
 
                break
77
 
        return [found.get(r, None) for r in revision_ids]
78
 
 
79
 
 
80
 
class Graph(object):
81
 
    """Provide incremental access to revision graphs.
82
 
 
83
 
    This is the generic implementation; it is intended to be subclassed to
84
 
    specialize it for other repository types.
85
 
    """
86
 
 
87
 
    def __init__(self, parents_provider):
88
 
        """Construct a Graph that uses several graphs as its input
89
 
 
90
 
        This should not normally be invoked directly, because there may be
91
 
        specialized implementations for particular repository types.  See
92
 
        Repository.get_graph()
93
 
 
94
 
        :param parents_func: an object providing a get_parents call
95
 
            conforming to the behavior of StackedParentsProvider.get_parents
96
 
        """
97
 
        self.get_parents = parents_provider.get_parents
98
 
        self._parents_provider = parents_provider
99
 
 
100
 
    def __repr__(self):
101
 
        return 'Graph(%r)' % self._parents_provider
102
 
 
103
 
    def find_lca(self, *revisions):
104
 
        """Determine the lowest common ancestors of the provided revisions
105
 
 
106
 
        A lowest common ancestor is a common ancestor none of whose
107
 
        descendants are common ancestors.  In graphs, unlike trees, there may
108
 
        be multiple lowest common ancestors.
109
 
 
110
 
        This algorithm has two phases.  Phase 1 identifies border ancestors,
111
 
        and phase 2 filters border ancestors to determine lowest common
112
 
        ancestors.
113
 
 
114
 
        In phase 1, border ancestors are identified, using a breadth-first
115
 
        search starting at the bottom of the graph.  Searches are stopped
116
 
        whenever a node or one of its descendants is determined to be common
117
 
 
118
 
        In phase 2, the border ancestors are filtered to find the least
119
 
        common ancestors.  This is done by searching the ancestries of each
120
 
        border ancestor.
121
 
 
122
 
        Phase 2 is perfomed on the principle that a border ancestor that is
123
 
        not an ancestor of any other border ancestor is a least common
124
 
        ancestor.
125
 
 
126
 
        Searches are stopped when they find a node that is determined to be a
127
 
        common ancestor of all border ancestors, because this shows that it
128
 
        cannot be a descendant of any border ancestor.
129
 
 
130
 
        The scaling of this operation should be proportional to
131
 
        1. The number of uncommon ancestors
132
 
        2. The number of border ancestors
133
 
        3. The length of the shortest path between a border ancestor and an
134
 
           ancestor of all border ancestors.
135
 
        """
136
 
        border_common, common, sides = self._find_border_ancestors(revisions)
137
 
        # We may have common ancestors that can be reached from each other.
138
 
        # - ask for the heads of them to filter it down to only ones that
139
 
        # cannot be reached from each other - phase 2.
140
 
        return self.heads(border_common)
141
 
 
142
 
    def find_difference(self, left_revision, right_revision):
143
 
        """Determine the graph difference between two revisions"""
144
 
        border, common, (left, right) = self._find_border_ancestors(
145
 
            [left_revision, right_revision])
146
 
        return (left.difference(right).difference(common),
147
 
                right.difference(left).difference(common))
148
 
 
149
 
    def _make_breadth_first_searcher(self, revisions):
150
 
        return _BreadthFirstSearcher(revisions, self)
151
 
 
152
 
    def _find_border_ancestors(self, revisions):
153
 
        """Find common ancestors with at least one uncommon descendant.
154
 
 
155
 
        Border ancestors are identified using a breadth-first
156
 
        search starting at the bottom of the graph.  Searches are stopped
157
 
        whenever a node or one of its descendants is determined to be common.
158
 
 
159
 
        This will scale with the number of uncommon ancestors.
160
 
 
161
 
        As well as the border ancestors, a set of seen common ancestors and a
162
 
        list of sets of seen ancestors for each input revision is returned.
163
 
        This allows calculation of graph difference from the results of this
164
 
        operation.
165
 
        """
166
 
        if None in revisions:
167
 
            raise errors.InvalidRevisionId(None, self)
168
 
        common_searcher = self._make_breadth_first_searcher([])
169
 
        common_ancestors = set()
170
 
        searchers = [self._make_breadth_first_searcher([r])
171
 
                     for r in revisions]
172
 
        active_searchers = searchers[:]
173
 
        border_ancestors = set()
174
 
        def update_common(searcher, revisions):
175
 
            w_seen_ancestors = searcher.find_seen_ancestors(
176
 
                revision)
177
 
            stopped = searcher.stop_searching_any(w_seen_ancestors)
178
 
            common_ancestors.update(w_seen_ancestors)
179
 
            common_searcher.start_searching(stopped)
180
 
 
181
 
        while True:
182
 
            if len(active_searchers) == 0:
183
 
                return border_ancestors, common_ancestors, [s.seen for s in
184
 
                                                            searchers]
185
 
            try:
186
 
                new_common = common_searcher.next()
187
 
                common_ancestors.update(new_common)
188
 
            except StopIteration:
189
 
                pass
190
 
            else:
191
 
                for searcher in active_searchers:
192
 
                    for revision in new_common.intersection(searcher.seen):
193
 
                        update_common(searcher, revision)
194
 
 
195
 
            newly_seen = set()
196
 
            new_active_searchers = []
197
 
            for searcher in active_searchers:
198
 
                try:
199
 
                    newly_seen.update(searcher.next())
200
 
                except StopIteration:
201
 
                    pass
202
 
                else:
203
 
                    new_active_searchers.append(searcher)
204
 
            active_searchers = new_active_searchers
205
 
            for revision in newly_seen:
206
 
                if revision in common_ancestors:
207
 
                    for searcher in searchers:
208
 
                        update_common(searcher, revision)
209
 
                    continue
210
 
                for searcher in searchers:
211
 
                    if revision not in searcher.seen:
212
 
                        break
213
 
                else:
214
 
                    border_ancestors.add(revision)
215
 
                    for searcher in searchers:
216
 
                        update_common(searcher, revision)
217
 
 
218
 
    def heads(self, keys):
219
 
        """Return the heads from amongst keys.
220
 
 
221
 
        This is done by searching the ancestries of each key.  Any key that is
222
 
        reachable from another key is not returned; all the others are.
223
 
 
224
 
        This operation scales with the relative depth between any two keys. If
225
 
        any two keys are completely disconnected all ancestry of both sides
226
 
        will be retrieved.
227
 
 
228
 
        :param keys: An iterable of keys.
229
 
        :return: A set of the heads. Note that as a set there is no ordering
230
 
            information. Callers will need to filter their input to create
231
 
            order if they need it.
232
 
        """
233
 
        candidate_heads = set(keys)
234
 
        if len(candidate_heads) < 2:
235
 
            return candidate_heads
236
 
        searchers = dict((c, self._make_breadth_first_searcher([c]))
237
 
                          for c in candidate_heads)
238
 
        active_searchers = dict(searchers)
239
 
        # skip over the actual candidate for each searcher
240
 
        for searcher in active_searchers.itervalues():
241
 
            searcher.next()
242
 
        while len(active_searchers) > 0:
243
 
            for candidate in active_searchers.keys():
244
 
                try:
245
 
                    searcher = active_searchers[candidate]
246
 
                except KeyError:
247
 
                    # rare case: we deleted candidate in a previous iteration
248
 
                    # through this for loop, because it was determined to be
249
 
                    # a descendant of another candidate.
250
 
                    continue
251
 
                try:
252
 
                    ancestors = searcher.next()
253
 
                except StopIteration:
254
 
                    del active_searchers[candidate]
255
 
                    continue
256
 
                for ancestor in ancestors:
257
 
                    if ancestor in candidate_heads:
258
 
                        candidate_heads.remove(ancestor)
259
 
                        del searchers[ancestor]
260
 
                        if ancestor in active_searchers:
261
 
                            del active_searchers[ancestor]
262
 
                    for searcher in searchers.itervalues():
263
 
                        if ancestor not in searcher.seen:
264
 
                            break
265
 
                    else:
266
 
                        # if this revision was seen by all searchers, then it
267
 
                        # is a descendant of all candidates, so we can stop
268
 
                        # searching it, and any seen ancestors
269
 
                        for searcher in searchers.itervalues():
270
 
                            seen_ancestors =\
271
 
                                searcher.find_seen_ancestors(ancestor)
272
 
                            searcher.stop_searching_any(seen_ancestors)
273
 
        return candidate_heads
274
 
 
275
 
    def find_unique_lca(self, left_revision, right_revision):
276
 
        """Find a unique LCA.
277
 
 
278
 
        Find lowest common ancestors.  If there is no unique  common
279
 
        ancestor, find the lowest common ancestors of those ancestors.
280
 
 
281
 
        Iteration stops when a unique lowest common ancestor is found.
282
 
        The graph origin is necessarily a unique lowest common ancestor.
283
 
 
284
 
        Note that None is not an acceptable substitute for NULL_REVISION.
285
 
        in the input for this method.
286
 
        """
287
 
        revisions = [left_revision, right_revision]
288
 
        while True:
289
 
            lca = self.find_lca(*revisions)
290
 
            if len(lca) == 1:
291
 
                return lca.pop()
292
 
            if len(lca) == 0:
293
 
                raise errors.NoCommonAncestor(left_revision, right_revision)
294
 
            revisions = lca
295
 
 
296
 
    def iter_topo_order(self, revisions):
297
 
        """Iterate through the input revisions in topological order.
298
 
 
299
 
        This sorting only ensures that parents come before their children.
300
 
        An ancestor may sort after a descendant if the relationship is not
301
 
        visible in the supplied list of revisions.
302
 
        """
303
 
        sorter = tsort.TopoSorter(zip(revisions, self.get_parents(revisions)))
304
 
        return sorter.iter_topo_order()
305
 
 
306
 
    def is_ancestor(self, candidate_ancestor, candidate_descendant):
307
 
        """Determine whether a revision is an ancestor of another.
308
 
 
309
 
        There are two possible outcomes: True and False, but there are three
310
 
        possible relationships:
311
 
 
312
 
        a) candidate_ancestor is an ancestor of candidate_descendant
313
 
        b) candidate_ancestor is an descendant of candidate_descendant
314
 
        c) candidate_ancestor is an sibling of candidate_descendant
315
 
 
316
 
        To check for a, we walk from candidate_descendant, looking for
317
 
        candidate_ancestor.
318
 
 
319
 
        To check for b, we walk from candidate_ancestor, looking for
320
 
        candidate_descendant.
321
 
 
322
 
        To make a and b more efficient, we can stop any searches that hit
323
 
        common ancestors.
324
 
 
325
 
        If we exhaust our searches, but neither a or b is true, then c is true.
326
 
 
327
 
        In order to find c efficiently, we must avoid searching from
328
 
        candidate_descendant or candidate_ancestor into common ancestors.  But
329
 
        if we don't search common ancestors at all, we won't know if we hit
330
 
        common ancestors.  So we have a walker for common ancestors.  Note that
331
 
        its searches are not required to terminate in order to determine c to
332
 
        be true.
333
 
        """
334
 
        ancestor_walker = self._make_breadth_first_searcher(
335
 
            [candidate_ancestor])
336
 
        descendant_walker = self._make_breadth_first_searcher(
337
 
            [candidate_descendant])
338
 
        common_walker = self._make_breadth_first_searcher([])
339
 
        active_ancestor = True
340
 
        active_descendant = True
341
 
        while (active_ancestor or active_descendant):
342
 
            new_common = set()
343
 
            if active_descendant:
344
 
                try:
345
 
                    nodes = descendant_walker.next()
346
 
                except StopIteration:
347
 
                    active_descendant = False
348
 
                else:
349
 
                    if candidate_ancestor in nodes:
350
 
                        return True
351
 
                    new_common.update(nodes.intersection(ancestor_walker.seen))
352
 
            if active_ancestor:
353
 
                try:
354
 
                    nodes = ancestor_walker.next()
355
 
                except StopIteration:
356
 
                    active_ancestor = False
357
 
                else:
358
 
                    if candidate_descendant in nodes:
359
 
                        return False
360
 
                    new_common.update(nodes.intersection(
361
 
                        descendant_walker.seen))
362
 
            try:
363
 
                new_common.update(common_walker.next())
364
 
            except StopIteration:
365
 
                pass
366
 
            for walker in (ancestor_walker, descendant_walker):
367
 
                for node in new_common:
368
 
                    c_ancestors = walker.find_seen_ancestors(node)
369
 
                    walker.stop_searching_any(c_ancestors)
370
 
                common_walker.start_searching(new_common)
371
 
        return False
372
 
 
373
 
 
374
 
class _BreadthFirstSearcher(object):
375
 
    """Parallel search the breadth-first the ancestry of revisions.
376
 
 
377
 
    This class implements the iterator protocol, but additionally
378
 
    1. provides a set of seen ancestors, and
379
 
    2. allows some ancestries to be unsearched, via stop_searching_any
380
 
    """
381
 
 
382
 
    def __init__(self, revisions, parents_provider):
383
 
        self._start = set(revisions)
384
 
        self._search_revisions = None
385
 
        self.seen = set(revisions)
386
 
        self._parents_provider = parents_provider 
387
 
 
388
 
    def __repr__(self):
389
 
        return ('_BreadthFirstSearcher(self._search_revisions=%r,'
390
 
                ' self.seen=%r)' % (self._search_revisions, self.seen))
391
 
 
392
 
    def next(self):
393
 
        """Return the next ancestors of this revision.
394
 
 
395
 
        Ancestors are returned in the order they are seen in a breadth-first
396
 
        traversal.  No ancestor will be returned more than once.
397
 
        """
398
 
        if self._search_revisions is None:
399
 
            self._search_revisions = self._start
400
 
        else:
401
 
            new_search_revisions = set()
402
 
            for parents in self._parents_provider.get_parents(
403
 
                self._search_revisions):
404
 
                if parents is None:
405
 
                    continue
406
 
                new_search_revisions.update(p for p in parents if
407
 
                                            p not in self.seen)
408
 
            self._search_revisions = new_search_revisions
409
 
        if len(self._search_revisions) == 0:
410
 
            raise StopIteration()
411
 
        self.seen.update(self._search_revisions)
412
 
        return self._search_revisions
413
 
 
414
 
    def __iter__(self):
415
 
        return self
416
 
 
417
 
    def find_seen_ancestors(self, revision):
418
 
        """Find ancestors of this revision that have already been seen."""
419
 
        searcher = _BreadthFirstSearcher([revision], self._parents_provider)
420
 
        seen_ancestors = set()
421
 
        for ancestors in searcher:
422
 
            for ancestor in ancestors:
423
 
                if ancestor not in self.seen:
424
 
                    searcher.stop_searching_any([ancestor])
425
 
                else:
426
 
                    seen_ancestors.add(ancestor)
427
 
        return seen_ancestors
428
 
 
429
 
    def stop_searching_any(self, revisions):
430
 
        """
431
 
        Remove any of the specified revisions from the search list.
432
 
 
433
 
        None of the specified revisions are required to be present in the
434
 
        search list.  In this case, the call is a no-op.
435
 
        """
436
 
        stopped = self._search_revisions.intersection(revisions)
437
 
        self._search_revisions = self._search_revisions.difference(revisions)
438
 
        return stopped
439
 
 
440
 
    def start_searching(self, revisions):
441
 
        if self._search_revisions is None:
442
 
            self._start = set(revisions)
443
 
        else:
444
 
            self._search_revisions.update(revisions.difference(self.seen))
445
 
        self.seen.update(revisions)