~bzr-pqm/bzr/bzr.dev

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
# 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.revision import NULL_REVISION
from bzrlib.tests import TestCaseWithMemoryTransport

ancestry_1 = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
              'rev3': ['rev2a'], 'rev4': ['rev3', 'rev2b']}
ancestry_2 = {'rev1a': [NULL_REVISION], 'rev2a': ['rev1a'],
              'rev1b': [NULL_REVISION], 'rev3a': ['rev2a'], 'rev4a': ['rev3a']}

criss_cross = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
               'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2a']}

criss_cross2 = {'rev1a': [NULL_REVISION], 'rev1b': [NULL_REVISION],
                'rev2a': ['rev1a', 'rev1b'], 'rev2b': ['rev1b', 'rev1a']}

class TestGraphWalker(TestCaseWithMemoryTransport):

    def test_distance_from_origin(self):
        graph_walker = self.make_walker(ancestry_1)
        self.assertEqual([1, 0, 2, 4],
                         graph_walker.distance_from_origin(['rev1', 'null:',
                         'rev2b', 'rev4']))

    def make_walker(self, ancestors):
        tree = self.build_ancestry(ancestors)
        return tree.branch.repository.get_graph_walker()

    def build_ancestry(self, ancestors):
        tree = self.make_branch_and_memory_tree('.')
        tree.lock_write()
        tree.add('.')
        pending = [NULL_REVISION]
        descendants = {}
        for descendant, parents in ancestors.iteritems():
            for parent in parents:
                descendants.setdefault(parent, []).append(descendant)
        while len(pending) > 0:
            cur_node = pending.pop()
            for descendant in descendants.get(cur_node, []):
                if tree.branch.repository.has_revision(descendant):
                    continue
                parents = [p for p in ancestors[descendant] if p is not
                           NULL_REVISION]
                if len([p for p in parents if not
                    tree.branch.repository.has_revision(p)]) > 0:
                    continue
                tree.set_parent_ids(parents)
                if len(parents) > 0:
                    left_parent = parents[0]
                else:
                    left_parent = NULL_REVISION
                tree.branch.set_last_revision_info(
                    len(tree.branch._lefthand_history(left_parent)),
                    left_parent)
                tree.commit(descendant, rev_id=descendant)
                pending.append(descendant)
        tree.unlock()
        return tree

    def test_mca(self):
        """Test finding minimal common ancestor.

        ancestry_1 should always have a single common ancestor
        """
        graph_walker = self.make_walker(ancestry_1)
        self.assertEqual(set([NULL_REVISION]),
                         graph_walker.minimal_common(NULL_REVISION,
                                                     NULL_REVISION))
        self.assertEqual(set([NULL_REVISION]),
                         graph_walker.minimal_common(NULL_REVISION,
                                                     'rev1'))
        self.assertEqual(set(['rev1']),
                         graph_walker.minimal_common('rev1', 'rev1'))
        self.assertEqual(set(['rev1']),
                         graph_walker.minimal_common('rev2a', 'rev2b'))

    def test_mca_criss_cross(self):
        graph_walker = self.make_walker(criss_cross)
        self.assertEqual(set(['rev2a', 'rev2b']),
                         graph_walker.minimal_common('rev3a', 'rev3b'))
        self.assertEqual(set(['rev2b']),
                         graph_walker.minimal_common('rev3a', 'rev3b',
                                                     'rev2b'))

    def test_recursive_unique_mca(self):
        """Test finding a unique minimal common ancestor.

        ancestry_1 should always have a single common ancestor
        """
        graph_walker = self.make_walker(ancestry_1)
        self.assertEqual(NULL_REVISION,
                         graph_walker.unique_common(NULL_REVISION,
                                                     NULL_REVISION))
        self.assertEqual(NULL_REVISION,
                         graph_walker.unique_common(NULL_REVISION,
                                                     'rev1'))
        self.assertEqual('rev1', graph_walker.unique_common('rev1', 'rev1'))
        self.assertEqual('rev1', graph_walker.unique_common('rev2a', 'rev2b'))

    def test_mca_criss_cross(self):
        graph_walker = self.make_walker(criss_cross)
        self.assertEqual(set(['rev2a', 'rev2b']),
                         graph_walker.minimal_common('rev3a', 'rev3b'))
        self.assertEqual(set(['rev2b']),
                         graph_walker.minimal_common('rev3a', 'rev3b',
                                                     'rev2b'))

    def test_unique_common_criss_cross(self):
        """Ensure we don't pick non-unique mcas in a criss-cross"""
        graph_walker = self.make_walker(criss_cross)
        self.assertEqual('rev1',
                         graph_walker.unique_common('rev3a', 'rev3b'))

    def test_unique_common_null_revision(self):
        """Ensure we pick NULL_REVISION when necessary"""
        graph_walker = self.make_walker(criss_cross2)
        self.assertEqual('rev1b',
                         graph_walker.unique_common('rev2a', 'rev1b'))
        self.assertEqual(NULL_REVISION,
                         graph_walker.unique_common('rev2a', 'rev2b'))

    def test_unique_common_null_revision2(self):
        """Ensure we pick NULL_REVISION when necessary"""
        graph_walker = self.make_walker(ancestry_2)
        self.assertEqual(NULL_REVISION,
                         graph_walker.unique_common('rev4a', 'rev1b'))