~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/graph_walker.py

  • Committer: Aaron Bentley
  • Date: 2007-05-25 03:17:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2534.
  • Revision ID: aaron.bentley@utoronto.ca-20070525031726-tey81wph8x3hg4po
Start work on GraphWalker

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from bzrlib import graph
 
2
from bzrlib.revision import NULL_REVISION
 
3
 
 
4
class GraphWalker(object):
 
5
    """Provide incremental access to revision graphs"""
 
6
 
 
7
    def __init__(self, graph):
 
8
        self._graph = graph
 
9
 
 
10
    def distance_from_origin(self, revisions):
 
11
        ancestors = self._graph.get_ancestors()
 
12
        descendants = self._graph.get_descendants()
 
13
        descendants[NULL_REVISION] = {}
 
14
        ancestors[NULL_REVISION] = []
 
15
        for root in self._graph.roots:
 
16
            descendants[NULL_REVISION][root] = 1
 
17
            ancestors[root].append(NULL_REVISION)
 
18
        for ghost in self._graph.ghosts:
 
19
            # ghosts act as roots for the purpose of finding
 
20
            # the longest paths from the root: any ghost *might*
 
21
            # be directly attached to the root, so we treat them
 
22
            # as being such.
 
23
            # ghost now descends from NULL
 
24
            descendants[NULL_REVISION][ghost] = 1
 
25
            # that is it has an ancestor of NULL
 
26
            ancestors[ghost] = [NULL_REVISION]
 
27
        distances = graph.node_distances(descendants, ancestors,
 
28
                                         NULL_REVISION)
 
29
        return [distances.get(r) for r in revisions]