~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testrevision.py

  • Committer: Martin Pool
  • Date: 2005-09-22 13:32:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050922133202-347cfd35d2941dd5
- simple weave-based annotate code (not complete)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.branch import Branch
22
22
from bzrlib.commit import commit
23
23
from bzrlib.fetch import fetch
24
 
from bzrlib.revision import (find_present_ancestors, combined_graph,
25
 
                             is_ancestor, MultipleRevisionSources)
 
24
from bzrlib.revision import (find_present_ancestors, common_ancestor,
 
25
                             is_ancestor)
26
26
from bzrlib.trace import mutter
27
27
from bzrlib.errors import NoSuchRevision
28
28
 
29
29
def make_branches():
30
 
    """Create two branches
31
 
 
32
 
    branch 1 has 6 commits, branch 2 has 3 commits
33
 
    commit 10 was a psuedo merge from branch 1
34
 
    but has been disabled until ghost support is
35
 
    implemented.
36
 
 
37
 
    the object graph is
38
 
    B:     A:
39
 
    a..0   a..0 
40
 
    a..1   a..1
41
 
    a..2   a..2
42
 
    b..3   a..3 merges b..4
43
 
    b..4   a..4
44
 
    b..5   a..5 merges b..5
45
 
    b..6
46
 
 
47
 
    so A is missing b6 at the start
48
 
    and B is missing a3, a4, a5
49
 
    """
50
30
    os.mkdir("branch1")
51
 
    br1 = Branch.initialize("branch1")
 
31
    br1 = Branch("branch1", init=True)
52
32
    
53
33
    commit(br1, "Commit one", rev_id="a@u-0-0")
54
34
    commit(br1, "Commit two", rev_id="a@u-0-1")
55
35
    commit(br1, "Commit three", rev_id="a@u-0-2")
56
36
 
57
37
    os.mkdir("branch2")
58
 
    br2 = Branch.initialize("branch2")
 
38
    br2 = Branch("branch2", init=True)
59
39
    br2.update_revisions(br1)
60
40
    commit(br2, "Commit four", rev_id="b@u-0-3")
61
41
    commit(br2, "Commit five", rev_id="b@u-0-4")
71
51
    fetch(from_branch=br2, to_branch=br1)
72
52
    br1.add_pending_merge(br2.revision_history()[5])
73
53
    commit(br1, "Commit nine", rev_id="a@u-0-5")
74
 
    # disabled - it makes testing fetch too hard,
75
 
    # but can be easily reenabled (without the fetch
76
 
    # when GHOSTS are supported. RBC 20050928
77
 
    #fetch(from_branch=br1, to_branch=br2)
78
 
    #br2.add_pending_merge(br1.revision_history()[4])
79
 
    commit(br2, "Commit ten - no merge", rev_id="b@u-0-6")
80
 
 
81
 
    #fetch(from_branch=br2, to_branch=br1)
 
54
 
 
55
    fetch(from_branch=br1, to_branch=br2)
 
56
    br2.add_pending_merge(br1.revision_history()[4])
 
57
    commit(br2, "Commit ten", rev_id="b@u-0-6")
 
58
 
 
59
    fetch(from_branch=br2, to_branch=br1)
82
60
    
83
61
    return br1, br2
84
62
 
102
80
             ('a@u-0-5', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
103
81
                          'b@u-0-3', 'b@u-0-4',
104
82
                          'b@u-0-5', 'a@u-0-5']),
105
 
             ('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2',
 
83
             ('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
106
84
                          'b@u-0-3', 'b@u-0-4',
107
85
                          'b@u-0-5', 'b@u-0-6']),
108
86
             ]
109
 
        br1_only = ('a@u-0-3', 'a@u-0-4', 'a@u-0-5')
110
 
        br2_only = ('b@u-0-6',)
111
87
        for branch in br1, br2:
112
88
            for rev_id, anc in d:
113
 
                if rev_id in br1_only and not branch is br1:
114
 
                    continue
115
 
                if rev_id in br2_only and not branch is br2:
116
 
                    continue
117
89
                mutter('ancestry of {%s}: %r',
118
90
                       rev_id, branch.get_ancestry(rev_id))
119
91
                self.assertEquals(sorted(branch.get_ancestry(rev_id)),
120
 
                                  [None] + sorted(anc))
 
92
                                  sorted(anc))
121
93
    
122
94
    
123
95
    def test_is_ancestor(self):
131
103
        assert is_ancestor(revisions[1], revisions[0], sources)
132
104
        assert not is_ancestor(revisions[0], revisions[1], sources)
133
105
        assert is_ancestor(revisions_2[3], revisions[0], sources)
134
 
        # disabled mbp 20050914, doesn't seem to happen anymore
 
106
        # disabled mbp 20050914, doesn't seem to happen anymore
135
107
        ## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
136
108
        ##                  revisions[0], br1)        
137
109
        assert is_ancestor(revisions[3], revisions_2[4], sources)
152
124
        self.br2.commit("Commit twelve", rev_id="b@u-0-8")
153
125
        self.br2.commit("Commit thirtteen", rev_id="b@u-0-9")
154
126
 
155
 
        fetch(from_branch=self.br2, to_branch=self.br1)
 
127
        fetch(from_branch=self.br2, to_branch=self.br1)
156
128
        self.br1.add_pending_merge(self.br2.revision_history()[6])
157
129
        self.br1.commit("Commit fourtten", rev_id="a@u-0-6")
158
130
 
159
 
        fetch(from_branch=self.br1, to_branch=self.br2)
 
131
        fetch(from_branch=self.br1, to_branch=self.br2)
160
132
        self.br2.add_pending_merge(self.br1.revision_history()[6])
161
133
        self.br2.commit("Commit fifteen", rev_id="b@u-0-10")
162
134
 
186
158
                                        self.br1.revision_history()), 
187
159
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
188
160
                          'a@u-0-5'])
189
 
        print ("testrevision.py 191 - intervene appears to return b..6 even"
190
 
               "though it is not reachable!")
191
 
#        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6', 
192
 
#                         self.br1.revision_history()), 
193
 
#                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
194
 
#                          'b@u-0-6'])
195
 
#        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'), 
196
 
#                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4', 
197
 
#                          'b@u-0-5'])
 
161
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6', 
 
162
                         self.br1.revision_history()), 
 
163
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
164
                          'b@u-0-6'])
 
165
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'), 
 
166
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4', 
 
167
                          'b@u-0-5'])
198
168
        self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6', 
199
169
                         self.br2.revision_history()), 
200
170
                         ['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
214
184
class TestCommonAncestor(TestCaseInTempDir):
215
185
    """Test checking whether a revision is an ancestor of another revision"""
216
186
 
217
 
    def test_old_common_ancestor(self):
218
 
        """Pick a resonable merge base using the old functionality"""
219
 
        from bzrlib.revision import old_common_ancestor as common_ancestor
220
 
        br1, br2 = make_branches()
221
 
        revisions = br1.revision_history()
222
 
        revisions_2 = br2.revision_history()
223
 
        sources = br1
224
 
 
225
 
        expected_ancestors_list = {revisions[3]:(0, 0), 
226
 
                                   revisions[2]:(1, 1),
227
 
                                   revisions_2[4]:(2, 1), 
228
 
                                   revisions[1]:(3, 2),
229
 
                                   revisions_2[3]:(4, 2),
230
 
                                   revisions[0]:(5, 3) }
231
 
        ancestors_list = find_present_ancestors(revisions[3], sources)
232
 
        assert len(expected_ancestors_list) == len(ancestors_list)
233
 
        for key, value in expected_ancestors_list.iteritems():
234
 
            self.assertEqual(ancestors_list[key], value, 
235
 
                              "key %r, %r != %r" % (key, ancestors_list[key],
236
 
                                                    value))
237
 
 
238
 
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
239
 
                          revisions[0])
240
 
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
241
 
                          revisions[1])
242
 
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
243
 
                          revisions[1])
244
 
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
245
 
                          revisions[2])
246
 
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
247
 
                          revisions_2[4])
248
 
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
249
 
                          revisions_2[4])
250
 
        fetch(from_branch=br2, to_branch=br1)
251
 
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
252
 
                          revisions_2[5])
253
 
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
254
 
                          revisions_2[5])
255
 
 
256
187
    def test_common_ancestor(self):
257
 
        """Pick a reasonable merge base"""
258
 
        from bzrlib.revision import common_ancestor
259
188
        br1, br2 = make_branches()
260
189
        revisions = br1.revision_history()
261
190
        revisions_2 = br2.revision_history()
262
 
        sources = MultipleRevisionSources(br1, br2)
 
191
        sources = br1
263
192
 
264
193
        expected_ancestors_list = {revisions[3]:(0, 0), 
265
194
                                   revisions[2]:(1, 1),
287
216
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
288
217
                          revisions_2[4])
289
218
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
290
 
                          revisions_2[5])
 
219
                          revisions[4])
291
220
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
292
221
                          revisions_2[5])
293
222
 
294
 
    def test_combined(self):
295
 
        """combined_graph
296
 
        Ensure it's not order-sensitive
297
 
        """
298
 
        br1, br2 = make_branches()
299
 
        source = MultipleRevisionSources(br1, br2)
300
 
        combined_1 = combined_graph(br1.last_revision(), 
301
 
                                    br2.last_revision(), source)
302
 
        combined_2 = combined_graph(br2.last_revision(),
303
 
                                    br1.last_revision(), source)
304
 
        assert combined_1[1] == combined_2[1]
305
 
        assert combined_1[2] == combined_2[2]
306
 
        assert combined_1[3] == combined_2[3]
307
 
        assert combined_1 == combined_2
 
223
 
 
224
if __name__ == '__main__':
 
225
    import unittest, sys
 
226
    unittest.main()
 
227