/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
1
# Copyright (C) 2006-2007 by Jelmer Vernooij
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
0.436.131 by Jelmer Vernooij
Change license back to GPLv2+
5
# the Free Software Foundation; either version 2 of the License, or
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
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
0.436.16 by Jelmer Vernooij
Some more work on maptree.
16
"""Tests for the rebase code."""
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
17
0.436.56 by Jelmer Vernooij
Fix test.
18
from bzrlib.conflicts import ConflictList
0.436.37 by Jelmer Vernooij
Store parents correctly.
19
from bzrlib.errors import UnknownFormatError, NoSuchFile, ConflictsInTree
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
20
from bzrlib.graph import Graph, DictParentsProvider
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
21
from bzrlib.revision import NULL_REVISION
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
22
from bzrlib.tests import TestCase, TestCaseWithTransport
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
23
from bzrlib.trace import mutter
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
24
0.436.157 by Jelmer Vernooij
use absolute imports everywhere.
25
from bzrlib.plugins.rebase.rebase import (
26
    marshall_rebase_plan,
27
    unmarshall_rebase_plan,
28
    replay_snapshot,
29
    generate_simple_plan,
30
    generate_transpose_plan,
31
    rebase_plan_exists,
32
    rebase_todo,
33
    REBASE_PLAN_FILENAME,
34
    REBASE_CURRENT_REVID_FILENAME,
35
    read_rebase_plan,
36
    remove_rebase_plan,
37
    read_active_rebase_revid,
38
    write_active_rebase_revid,
39
    write_rebase_plan,
40
    MapTree,
41
    ReplaySnapshotError,
42
    ReplayParentsInconsistent,
43
    replay_delta_workingtree,
44
    replay_determine_base,
45
    commit_rebase,
46
    )
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
47
0.436.4 by Jelmer Vernooij
Add some tests.
48
0.436.3 by Jelmer Vernooij
Fill in commands.
49
class RebasePlanReadWriterTests(TestCase):
0.436.145 by Jelmer Vernooij
Fix test.
50
0.436.4 by Jelmer Vernooij
Add some tests.
51
    def test_simple_marshall_rebase_plan(self):
52
        self.assertEqualDiff(
53
"""# Bazaar rebase plan 1
54
1 bla
55
oldrev newrev newparent1 newparent2
56
""", marshall_rebase_plan((1, "bla"), 
0.436.95 by Jelmer Vernooij
Use tuples more consistently.
57
                          {"oldrev": ("newrev", ("newparent1", "newparent2"))}))
0.436.4 by Jelmer Vernooij
Add some tests.
58
59
    def test_simple_unmarshall_rebase_plan(self):
60
        self.assertEquals(((1, "bla"), 
0.436.95 by Jelmer Vernooij
Use tuples more consistently.
61
                          {"oldrev": ("newrev", ("newparent1", "newparent2"))}),
0.436.4 by Jelmer Vernooij
Add some tests.
62
                         unmarshall_rebase_plan("""# Bazaar rebase plan 1
63
1 bla
64
oldrev newrev newparent1 newparent2
65
"""))
66
67
    def test_unmarshall_rebase_plan_formatunknown(self):
68
        self.assertRaises(UnknownFormatError,
69
                         unmarshall_rebase_plan, """# Bazaar rebase plan x
70
1 bla
71
oldrev newrev newparent1 newparent2
72
""")
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
73
74
75
class ConversionTests(TestCaseWithTransport):
0.436.145 by Jelmer Vernooij
Fix test.
76
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
77
    def test_simple(self):
78
        wt = self.make_branch_and_tree('.')
79
        b = wt.branch
80
        file('hello', 'w').write('hello world')
81
        wt.add('hello')
82
        wt.commit(message='add hello', rev_id="bla")
83
        file('hello', 'w').write('world')
84
        wt.commit(message='change hello', rev_id="bloe")
85
        wt.set_last_revision("bla")
86
        b.set_revision_history(["bla"])
87
        file('hello', 'w').write('world')
88
        wt.commit(message='change hello', rev_id="bla2")
0.436.49 by Jelmer Vernooij
Fix locking.
89
90
        wt.branch.repository.lock_write()
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
91
        newrev = replay_snapshot(wt.branch.repository, "bla2", "bla4", 
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
92
                ("bloe",))
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
93
        self.assertEqual("bla4", newrev)
94
        self.assertTrue(wt.branch.repository.has_revision(newrev))
0.436.80 by Jelmer Vernooij
Avoid using deprecated revision_parents() function.
95
        self.assertEqual(("bloe",), 
96
                wt.branch.repository.get_parent_map([newrev])[newrev])
0.436.20 by Jelmer Vernooij
Some more blackbox tests.
97
        self.assertEqual("bla2", 
98
            wt.branch.repository.get_revision(newrev).properties["rebase-of"])
0.436.49 by Jelmer Vernooij
Fix locking.
99
        wt.branch.repository.unlock()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
100
101
102
class PlanCreatorTests(TestCaseWithTransport):
0.441.4 by Robert Collins
* Fixed O(history) access during plan creation (Robert Collins, lp:#249823).
103
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
104
    def test_simple_plan_creator(self):
105
        wt = self.make_branch_and_tree('.')
106
        b = wt.branch
107
        file('hello', 'w').write('hello world')
108
        wt.add('hello')
109
        wt.commit(message='add hello', rev_id="bla")
110
        file('hello', 'w').write('world')
111
        wt.commit(message='change hello', rev_id="bloe")
112
        wt.set_last_revision("bla")
113
        b.set_revision_history(["bla"])
114
        file('hello', 'w').write('world')
115
        wt.commit(message='change hello', rev_id="bla2")
116
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
117
        b.repository.lock_read()
0.441.4 by Robert Collins
* Fixed O(history) access during plan creation (Robert Collins, lp:#249823).
118
        graph = b.repository.get_graph()
119
        self.assertEquals({'bla2': ('newbla2', ("bloe",))},
120
            generate_simple_plan(
121
                graph.find_difference(b.last_revision(),"bla")[0],
122
                "bla2", None, "bloe", graph, lambda y: "new"+y))
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
123
        b.repository.unlock()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
124
     
125
    def test_simple_plan_creator_extra_history(self):
126
        wt = self.make_branch_and_tree('.')
127
        b = wt.branch
128
        file('hello', 'w').write('hello world')
129
        wt.add('hello')
130
        wt.commit(message='add hello', rev_id="bla")
131
        file('hello', 'w').write('world')
132
        wt.commit(message='change hello', rev_id="bloe")
133
        wt.set_last_revision("bla")
134
        b.set_revision_history(["bla"])
135
        file('hello', 'w').write('world')
136
        wt.commit(message='change hello', rev_id="bla2")
137
        file('hello', 'w').write('universe')
138
        wt.commit(message='change hello again', rev_id="bla3")
139
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
140
        b.repository.lock_read()
0.441.4 by Robert Collins
* Fixed O(history) access during plan creation (Robert Collins, lp:#249823).
141
        graph = b.repository.get_graph()
142
        self.assertEquals(
143
            {'bla2': ('newbla2', ("bloe",)), 'bla3': ('newbla3', ('newbla2',))},
144
            generate_simple_plan(
145
                graph.find_difference(b.last_revision(),"bloe")[0],
146
                "bla2", None, "bloe",
147
                graph, lambda y: "new"+y))
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
148
        b.repository.unlock()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
149
150
    def test_generate_transpose_plan(self):
151
        wt = self.make_branch_and_tree('.')
152
        b = wt.branch
153
        file('hello', 'w').write('hello world')
154
        wt.add('hello')
155
        wt.commit(message='add hello', rev_id="bla")
156
        file('hello', 'w').write('world')
157
        wt.commit(message='change hello', rev_id="bloe")
158
        wt.set_last_revision("bla")
159
        b.set_revision_history(["bla"])
160
        file('hello', 'w').write('world')
161
        wt.commit(message='change hello', rev_id="bla2")
162
        file('hello', 'w').write('universe')
163
        wt.commit(message='change hello again', rev_id="bla3")
164
        wt.set_last_revision("bla")
165
        b.set_revision_history(["bla"])
166
        file('hello', 'w').write('somebar')
167
        wt.commit(message='change hello yet again', rev_id="blie")
168
        wt.set_last_revision(NULL_REVISION)
169
        b.set_revision_history([])
170
        wt.add('hello')
171
        wt.commit(message='add hello', rev_id="lala")
172
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
173
        b.repository.lock_read()
0.436.88 by Jelmer Vernooij
Avoid use of get_revision_graph() calls.
174
        graph = b.repository.get_graph()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
175
        self.assertEquals({
0.436.90 by Jelmer Vernooij
Deal with parents being a tuple.
176
            'blie': ('newblie', ('lala',))},
0.436.88 by Jelmer Vernooij
Avoid use of get_revision_graph() calls.
177
            generate_transpose_plan(graph.iter_ancestry(["blie"]), 
178
            {"bla": "lala"}, graph, lambda y: "new"+y))
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
179
        self.assertEquals({
0.436.90 by Jelmer Vernooij
Deal with parents being a tuple.
180
            'bla2': ('newbla2', ('lala',)),
181
            'bla3': ('newbla3', ('newbla2',)),
182
            'blie': ('newblie', ('lala',)),
183
            'bloe': ('newbloe', ('lala',))},
0.436.88 by Jelmer Vernooij
Avoid use of get_revision_graph() calls.
184
            generate_transpose_plan(graph.iter_ancestry(b.repository._all_revision_ids()), 
0.436.36 by Jelmer Vernooij
Fix compatibility with bzr 0.19.
185
            {"bla": "lala"}, 
0.436.88 by Jelmer Vernooij
Avoid use of get_revision_graph() calls.
186
            graph, lambda y: "new"+y))
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
187
        b.repository.unlock()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
188
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
189
    def test_generate_transpose_plan_one(self):
0.436.89 by Jelmer Vernooij
Use tuples for parents.
190
        graph = Graph(DictParentsProvider({"bla": ("bloe",), "bloe": (), "lala": ()}))
0.436.90 by Jelmer Vernooij
Deal with parents being a tuple.
191
        self.assertEquals({"bla": ("newbla", ("lala",))},
0.436.88 by Jelmer Vernooij
Avoid use of get_revision_graph() calls.
192
                generate_transpose_plan(graph.iter_ancestry(["bla", "bloe"]),
193
                    {"bloe": "lala"}, graph, lambda y: "new"+y))
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
194
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
195
    def test_plan_with_already_merged(self):
196
        """We need to use a merge base that makes sense. 
197
        
198
        A
199
        | \
200
        B  D
201
        | \|
202
        C  E
203
204
        Rebasing E on C should result in:
205
206
        A -> B -> C -> D -> E
207
208
        with a plan of:
209
210
        D -> (D', [C])
0.436.40 by Jelmer Vernooij
Adjust expectations.
211
        E -> (E', [D', C])
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
212
        """
213
        parents_map = {
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
214
                "A": (),
215
                "B": ("A",),
216
                "C": ("B",),
217
                "D": ("A",),
218
                "E": ("D", "B")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
219
        }
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
220
        graph = Graph(DictParentsProvider(parents_map))
0.441.4 by Robert Collins
* Fixed O(history) access during plan creation (Robert Collins, lp:#249823).
221
        self.assertEquals({"D": ("D'", ("C",)), "E": ("E'", ("D'",))},
222
            generate_simple_plan(["D", "E"], "D", None, "C",
223
                graph, lambda y: y+"'"))
0.436.55 by Jelmer Vernooij
Add functinality for skipping duplicate merges.
224
225
    def test_plan_with_already_merged_skip_merges(self):
226
        """We need to use a merge base that makes sense. 
227
        
228
        A
229
        | \
230
        B  D
231
        | \|
232
        C  E
233
234
        Rebasing E on C should result in:
235
236
        A -> B -> C -> D'
237
238
        with a plan of:
239
240
        D -> (D', [C])
241
        """
242
        parents_map = {
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
243
                "A": (),
244
                "B": ("A",),
245
                "C": ("B",),
246
                "D": ("A",),
247
                "E": ("D", "B")
0.436.55 by Jelmer Vernooij
Add functinality for skipping duplicate merges.
248
        }
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
249
        graph = Graph(DictParentsProvider(parents_map))
0.441.4 by Robert Collins
* Fixed O(history) access during plan creation (Robert Collins, lp:#249823).
250
        self.assertEquals({"D": ("D'", ("C",))},
251
                generate_simple_plan(["D", "E"], "D", None, "C",
0.436.86 by Jelmer Vernooij
Fix some uses of get_revision_graph().
252
                    graph, lambda y: y+"'", True))
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
253
 
254
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
255
class PlanFileTests(TestCaseWithTransport):
0.436.145 by Jelmer Vernooij
Fix test.
256
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
257
   def test_rebase_plan_exists_false(self):
258
        wt = self.make_branch_and_tree('.')
259
        self.assertFalse(rebase_plan_exists(wt))
260
261
   def test_rebase_plan_exists_empty(self):
262
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
263
        wt._transport.put_bytes(REBASE_PLAN_FILENAME, "")
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
264
        self.assertFalse(rebase_plan_exists(wt))
265
266
   def test_rebase_plan_exists(self):
267
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
268
        wt._transport.put_bytes(REBASE_PLAN_FILENAME, "foo")
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
269
        self.assertTrue(rebase_plan_exists(wt))
270
271
   def test_remove_rebase_plan(self):
272
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
273
        wt._transport.put_bytes(REBASE_PLAN_FILENAME, "foo")
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
274
        remove_rebase_plan(wt)
275
        self.assertFalse(rebase_plan_exists(wt))
276
277
   def test_remove_rebase_plan_twice(self):
278
        wt = self.make_branch_and_tree('.')
279
        remove_rebase_plan(wt)
280
        self.assertFalse(rebase_plan_exists(wt))
281
282
   def test_write_rebase_plan(self):
283
        wt = self.make_branch_and_tree('.')
284
        file('hello', 'w').write('hello world')
285
        wt.add('hello')
286
        wt.commit(message='add hello', rev_id="bla")
287
        write_rebase_plan(wt, 
288
                {"oldrev": ("newrev", ["newparent1", "newparent2"])})
289
        self.assertEqualDiff("""# Bazaar rebase plan 1
290
1 bla
291
oldrev newrev newparent1 newparent2
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
292
""", wt._transport.get_bytes(REBASE_PLAN_FILENAME))
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
293
294
   def test_read_rebase_plan_nonexistant(self):
295
        wt = self.make_branch_and_tree('.')
296
        self.assertRaises(NoSuchFile, read_rebase_plan, wt)
297
298
   def test_read_rebase_plan_empty(self):
299
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
300
        wt._transport.put_bytes(REBASE_PLAN_FILENAME, "")
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
301
        self.assertRaises(NoSuchFile, read_rebase_plan, wt)
302
        
303
   def test_read_rebase_plan(self):
304
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
305
        wt._transport.put_bytes(REBASE_PLAN_FILENAME, """# Bazaar rebase plan 1
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
306
1 bla
307
oldrev newrev newparent1 newparent2
308
""")
0.436.36 by Jelmer Vernooij
Fix compatibility with bzr 0.19.
309
        self.assertEquals(((1, "bla"), 
0.436.95 by Jelmer Vernooij
Use tuples more consistently.
310
            {"oldrev": ("newrev", ("newparent1", "newparent2"))}),
0.436.36 by Jelmer Vernooij
Fix compatibility with bzr 0.19.
311
            read_rebase_plan(wt))
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
312
0.436.20 by Jelmer Vernooij
Some more blackbox tests.
313
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
314
class CurrentRevidFileTests(TestCaseWithTransport):
0.436.145 by Jelmer Vernooij
Fix test.
315
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
316
    def test_read_nonexistant(self):
317
        wt = self.make_branch_and_tree('.')
318
        self.assertIs(None, read_active_rebase_revid(wt))
319
320
    def test_read_null(self):
321
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
322
        wt._transport.put_bytes(REBASE_CURRENT_REVID_FILENAME, NULL_REVISION)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
323
        self.assertIs(None, read_active_rebase_revid(wt))
324
325
    def test_read(self):
326
        wt = self.make_branch_and_tree('.')
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
327
        wt._transport.put_bytes(REBASE_CURRENT_REVID_FILENAME, "bla")
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
328
        self.assertEquals("bla", read_active_rebase_revid(wt))
329
330
    def test_write(self):
331
        wt = self.make_branch_and_tree('.')
332
        write_active_rebase_revid(wt, "bloe")
333
        self.assertEquals("bloe", read_active_rebase_revid(wt))
334
335
    def test_write_null(self):
336
        wt = self.make_branch_and_tree('.')
337
        write_active_rebase_revid(wt, None)
338
        self.assertIs(None, read_active_rebase_revid(wt))
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
339
0.436.20 by Jelmer Vernooij
Some more blackbox tests.
340
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
341
class RebaseTodoTests(TestCase):
0.436.145 by Jelmer Vernooij
Fix test.
342
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
343
    def test_done(self):
344
        class Repository:
345
            def has_revision(self, revid):
346
                return revid == "bloe"
347
        self.assertEquals([], 
348
                list(rebase_todo(Repository(), { "bla": ("bloe", [])})))
349
350
    def test_notstarted(self):
351
        class Repository:
352
            def has_revision(self, revid):
353
                return False
354
        self.assertEquals(["bla"], 
355
                list(rebase_todo(Repository(), { "bla": ("bloe", [])})))
356
357
    def test_halfway(self):
358
        class Repository:
359
            def has_revision(self, revid):
360
                return revid == "bloe"
361
        self.assertEquals(["ha"], 
362
                list(rebase_todo(Repository(), { "bla": ("bloe", []), 
363
                                                 "ha": ("hee", [])})))
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
364
365
class ReplaySnapshotTests(TestCaseWithTransport):
366
    def test_single_revision(self):
367
        wt = self.make_branch_and_tree(".")
368
        self.build_tree(['afile'])
369
        wt.add(["afile"])
370
        wt.commit("bla", rev_id="oldcommit")
0.436.49 by Jelmer Vernooij
Fix locking.
371
        wt.branch.repository.lock_write()
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
372
        replay_snapshot(wt.branch.repository, "oldcommit", "newcommit", ())
0.436.49 by Jelmer Vernooij
Fix locking.
373
        wt.branch.repository.unlock()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
374
        oldrev = wt.branch.repository.get_revision("oldcommit")
375
        newrev = wt.branch.repository.get_revision("newcommit")
376
        self.assertEquals([], newrev.parent_ids)
377
        self.assertEquals("newcommit", newrev.revision_id)
378
        self.assertEquals(oldrev.committer, newrev.committer)
379
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
380
        self.assertEquals(oldrev.timezone, newrev.timezone)
381
        inv = wt.branch.repository.get_inventory("newcommit")
382
        self.assertEquals("newcommit", inv[inv.path2id("afile")].revision)
383
384
    def test_parents_different(self):
385
        """replay_snapshot() relies on the fact that the contents of 
386
        the old and new parents is equal (at least concerning tree shape). If 
387
        it turns out it isn't, an exception should be raised."""
388
        wt = self.make_branch_and_tree(".")
389
        wt.commit("bloe", rev_id="base")
390
        self.build_tree(['afile', 'notherfile'])
391
        wt.add(["afile"])
392
        wt.commit("bla", rev_id="oldparent")
393
        wt.add(["notherfile"])
394
        wt.commit("bla", rev_id="oldcommit")
0.436.34 by Jelmer Vernooij
Some more tests.
395
        # this should raise an exception since oldcommit is being rewritten 
396
        # but 'afile' is present in the old parents but not in the new ones.
0.436.49 by Jelmer Vernooij
Fix locking.
397
        wt.branch.repository.lock_write()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
398
        self.assertRaises(
0.436.34 by Jelmer Vernooij
Some more tests.
399
                ReplayParentsInconsistent, replay_snapshot, 
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
400
                wt.branch.repository, "oldcommit", "newcommit", ("base",))
0.436.49 by Jelmer Vernooij
Fix locking.
401
        wt.branch.repository.unlock()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
402
0.436.34 by Jelmer Vernooij
Some more tests.
403
    def test_two_revisions(self):
404
        wt = self.make_branch_and_tree("old")
0.436.103 by Jelmer Vernooij
Loosen requirements about text parents a bit - check text sha1s instead.
405
        self.build_tree_contents([('old/afile', 'afilecontents'), ('old/notherfile', 'notherfilecontents')])
0.436.34 by Jelmer Vernooij
Some more tests.
406
        wt.add(["afile"], ["somefileid"])
407
        wt.commit("bla", rev_id="oldparent")
408
        wt.add(["notherfile"])
409
        wt.commit("bla", rev_id="oldcommit")
410
        oldrepos = wt.branch.repository
411
        wt = self.make_branch_and_tree("new")
0.436.103 by Jelmer Vernooij
Loosen requirements about text parents a bit - check text sha1s instead.
412
        self.build_tree_contents([('new/afile', 'afilecontents'), ('new/notherfile', 'notherfilecontents')])
0.436.34 by Jelmer Vernooij
Some more tests.
413
        wt.add(["afile"], ["afileid"])
414
        wt.commit("bla", rev_id="newparent")
415
        wt.branch.repository.fetch(oldrepos)
0.436.49 by Jelmer Vernooij
Fix locking.
416
        wt.branch.repository.lock_write()
0.436.34 by Jelmer Vernooij
Some more tests.
417
        replay_snapshot(wt.branch.repository, "oldcommit", "newcommit", 
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
418
                ("newparent",))
0.436.49 by Jelmer Vernooij
Fix locking.
419
        wt.branch.repository.unlock()
0.436.34 by Jelmer Vernooij
Some more tests.
420
        oldrev = wt.branch.repository.get_revision("oldcommit")
421
        newrev = wt.branch.repository.get_revision("newcommit")
422
        self.assertEquals(["newparent"], newrev.parent_ids)
423
        self.assertEquals("newcommit", newrev.revision_id)
424
        self.assertEquals(oldrev.committer, newrev.committer)
425
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
426
        self.assertEquals(oldrev.timezone, newrev.timezone)
427
        inv = wt.branch.repository.get_inventory("newcommit")
428
        self.assertEquals("afileid", inv.path2id("afile"))
429
        self.assertEquals("newcommit", inv[inv.path2id("notherfile")].revision)
430
431
    def test_two_revisions_no_renames(self):
432
        wt = self.make_branch_and_tree("old")
433
        self.build_tree(['old/afile', 'old/notherfile'])
434
        wt.add(["afile"], ["somefileid"])
435
        wt.commit("bla", rev_id="oldparent")
436
        wt.add(["notherfile"])
437
        wt.commit("bla", rev_id="oldcommit")
438
        oldrepos = wt.branch.repository
439
        wt = self.make_branch_and_tree("new")
440
        self.build_tree(['new/afile', 'new/notherfile'])
441
        wt.add(["afile"], ["afileid"])
442
        wt.commit("bla", rev_id="newparent")
443
        wt.branch.repository.fetch(oldrepos)
0.436.49 by Jelmer Vernooij
Fix locking.
444
        wt.branch.repository.lock_write()
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
445
        replay_snapshot(wt.branch.repository, "oldcommit", "newcommit", 
446
                        ("newparent",))
0.436.49 by Jelmer Vernooij
Fix locking.
447
        wt.branch.repository.unlock()
0.436.34 by Jelmer Vernooij
Some more tests.
448
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
449
    def test_multi_revisions(self):
450
        wt = self.make_branch_and_tree("old")
0.436.103 by Jelmer Vernooij
Loosen requirements about text parents a bit - check text sha1s instead.
451
        self.build_tree_contents([('old/afile', 'afilecontent'), 
452
                         ('old/sfile', 'sfilecontent'),
453
                         ('old/notherfile', 'notherfilecontent')])
0.436.35 by Jelmer Vernooij
Make revid_renames argument mandatory.
454
        wt.add(['sfile'])
0.436.34 by Jelmer Vernooij
Some more tests.
455
        wt.add(["afile"], ["somefileid"])
456
        wt.commit("bla", rev_id="oldgrandparent")
457
        open("old/afile", "w").write("data")
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
458
        wt.commit("bla", rev_id="oldparent")
459
        wt.add(["notherfile"])
460
        wt.commit("bla", rev_id="oldcommit")
461
        oldrepos = wt.branch.repository
462
        wt = self.make_branch_and_tree("new")
0.436.103 by Jelmer Vernooij
Loosen requirements about text parents a bit - check text sha1s instead.
463
        self.build_tree_contents([('new/afile', 'afilecontent'), 
464
                         ('new/sfile', 'sfilecontent'),
465
                         ('new/notherfile', 'notherfilecontent')])
0.436.35 by Jelmer Vernooij
Make revid_renames argument mandatory.
466
        wt.add(['sfile'])
0.436.34 by Jelmer Vernooij
Some more tests.
467
        wt.add(["afile"], ["afileid"])
468
        wt.commit("bla", rev_id="newgrandparent")
469
        open("new/afile", "w").write("data")
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
470
        wt.commit("bla", rev_id="newparent")
471
        wt.branch.repository.fetch(oldrepos)
0.436.49 by Jelmer Vernooij
Fix locking.
472
        wt.branch.repository.lock_write()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
473
        replay_snapshot(wt.branch.repository, "oldcommit", "newcommit", 
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
474
                ("newparent",))
0.436.49 by Jelmer Vernooij
Fix locking.
475
        wt.branch.repository.unlock()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
476
        oldrev = wt.branch.repository.get_revision("oldcommit")
477
        newrev = wt.branch.repository.get_revision("newcommit")
0.436.34 by Jelmer Vernooij
Some more tests.
478
        self.assertEquals(["newparent"], newrev.parent_ids)
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
479
        self.assertEquals("newcommit", newrev.revision_id)
480
        self.assertEquals(oldrev.committer, newrev.committer)
481
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
482
        self.assertEquals(oldrev.timezone, newrev.timezone)
483
        inv = wt.branch.repository.get_inventory("newcommit")
0.436.34 by Jelmer Vernooij
Some more tests.
484
        self.assertEquals("afileid", inv.path2id("afile"))
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
485
        self.assertEquals("newcommit", inv[inv.path2id("notherfile")].revision)
0.436.35 by Jelmer Vernooij
Make revid_renames argument mandatory.
486
        self.assertEquals("newgrandparent", inv[inv.path2id("sfile")].revision)
0.436.34 by Jelmer Vernooij
Some more tests.
487
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
488
    def test_maps_ids(self):
489
        wt = self.make_branch_and_tree("old")
490
        wt.commit("base", rev_id="base")
491
        self.build_tree(['old/afile'])
492
        wt.add(["afile"], ids=["originalid"])
493
        wt.commit("bla", rev_id="oldparent")
494
        file("old/afile", "w").write("bloe")
495
        wt.commit("bla", rev_id="oldcommit")
496
        oldrepos = wt.branch.repository
497
        wt = self.make_branch_and_tree("new")
498
        self.build_tree(['new/afile'])
499
        wt.add(["afile"], ids=["newid"])
500
        wt.commit("bla", rev_id="newparent")
501
        wt.branch.repository.fetch(oldrepos)
0.436.49 by Jelmer Vernooij
Fix locking.
502
        wt.branch.repository.lock_write()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
503
        replay_snapshot(wt.branch.repository, "oldcommit", "newcommit", 
0.436.121 by Jelmer Vernooij
Simplify replay snapshot functions.
504
                ("newparent",))
0.436.49 by Jelmer Vernooij
Fix locking.
505
        wt.branch.repository.unlock()
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
506
        oldrev = wt.branch.repository.get_revision("oldcommit")
507
        newrev = wt.branch.repository.get_revision("newcommit")
508
        self.assertEquals(["newparent"], newrev.parent_ids)
509
        self.assertEquals("newcommit", newrev.revision_id)
510
        self.assertEquals(oldrev.committer, newrev.committer)
511
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
512
        self.assertEquals(oldrev.timezone, newrev.timezone)
513
        inv = wt.branch.repository.get_inventory("newcommit")
514
        self.assertEquals("newid", inv.path2id("afile"))
515
        self.assertEquals("newcommit", inv[inv.path2id("afile")].revision)
516
0.436.37 by Jelmer Vernooij
Store parents correctly.
517
518
class TestReplayWorkingtree(TestCaseWithTransport):
519
    def test_conflicts(self):
520
        wt = self.make_branch_and_tree("old")
521
        wt.commit("base", rev_id="base")
522
        self.build_tree(['old/afile'])
523
        wt.add(["afile"], ids=["originalid"])
524
        wt.commit("bla", rev_id="oldparent")
525
        file("old/afile", "w").write("bloe")
526
        wt.commit("bla", rev_id="oldcommit")
527
        oldrepos = wt.branch.repository
528
        wt = self.make_branch_and_tree("new")
529
        self.build_tree(['new/afile'])
530
        wt.add(["afile"], ids=["newid"])
531
        wt.commit("bla", rev_id="newparent")
532
        wt.branch.repository.fetch(oldrepos)
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
533
        wt.lock_write()
0.436.37 by Jelmer Vernooij
Store parents correctly.
534
        self.assertRaises(ConflictsInTree, 
535
            replay_delta_workingtree, wt, "oldcommit", "newcommit", 
536
            ["newparent"])
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
537
        wt.unlock()
0.436.37 by Jelmer Vernooij
Store parents correctly.
538
539
    def test_simple(self):
540
        wt = self.make_branch_and_tree("old")
541
        wt.commit("base", rev_id="base")
542
        self.build_tree(['old/afile'])
543
        wt.add(["afile"], ids=["originalid"])
544
        wt.commit("bla", rev_id="oldparent")
545
        file("old/afile", "w").write("bloe")
0.436.79 by Jelmer Vernooij
Fix tests when using the latest version of bzr.
546
        wt.commit("bla", rev_id="oldcommit")
0.436.37 by Jelmer Vernooij
Store parents correctly.
547
        wt = wt.bzrdir.sprout("new").open_workingtree()
548
        self.build_tree(['new/bfile'])
549
        wt.add(["bfile"], ids=["newid"])
550
        wt.commit("bla", rev_id="newparent")
551
        replay_delta_workingtree(wt, "oldcommit", "newcommit", 
552
            ["newparent"])
553
        oldrev = wt.branch.repository.get_revision("oldcommit")
554
        newrev = wt.branch.repository.get_revision("newcommit")
555
        self.assertEquals(["newparent"], newrev.parent_ids)
556
        self.assertEquals("newcommit", newrev.revision_id)
557
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
558
        self.assertEquals(oldrev.timezone, newrev.timezone)
559
560
    def test_multiple(self):
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
561
        # rebase from 
562
        # base: []
563
        # oldparent: [base]
564
        # newparent: [base]
565
        # oldcommit: [oldparent, ghost]
566
        # create newcommit by rebasing oldcommit from oldparent to newparent,
567
        # keeping the merge of ghost.
568
        # Common base:
0.436.37 by Jelmer Vernooij
Store parents correctly.
569
        wt = self.make_branch_and_tree("old")
570
        wt.commit("base", rev_id="base")
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
571
        # oldparent:
572
        self.build_tree_contents([('old/afile', 'base content')])
0.436.37 by Jelmer Vernooij
Store parents correctly.
573
        wt.add(["afile"], ids=["originalid"])
574
        wt.commit("bla", rev_id="oldparent")
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
575
        # oldcommit (the delta getting rebased)
576
        #  - change the content of afile to be 'bloe'
0.436.37 by Jelmer Vernooij
Store parents correctly.
577
        file("old/afile", "w").write("bloe")
578
        wt.add_pending_merge("ghost")
579
        wt.commit("bla", rev_id="oldcommit")
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
580
        # newparent (the new base for the rebased commit)
581
        new_tree = wt.bzrdir.sprout("new",
582
            revision_id='base').open_workingtree()
583
        new_tree.branch.repository.fetch(wt.branch.repository)
584
        wt = new_tree
585
        self.build_tree_contents([('new/afile', 'base content')])
586
        wt.add(["afile"], ids=["originalid"])
0.436.37 by Jelmer Vernooij
Store parents correctly.
587
        wt.commit("bla", rev_id="newparent")
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
588
        # And do it!
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
589
        wt.lock_write()
0.441.3 by Robert Collins
Update rebase to 1.6b3 API changes.
590
        replay_delta_workingtree(wt, "oldcommit", "newcommit",
0.436.93 by Jelmer Vernooij
Clarify test.
591
            ("newparent", "ghost"))
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
592
        wt.unlock()
0.436.37 by Jelmer Vernooij
Store parents correctly.
593
        oldrev = wt.branch.repository.get_revision("oldcommit")
594
        newrev = wt.branch.repository.get_revision("newcommit")
0.436.93 by Jelmer Vernooij
Clarify test.
595
        self.assertEquals(["oldparent", "ghost"], oldrev.parent_ids)
0.436.37 by Jelmer Vernooij
Store parents correctly.
596
        self.assertEquals(["newparent", "ghost"], newrev.parent_ids)
597
        self.assertEquals("newcommit", newrev.revision_id)
598
        self.assertEquals(oldrev.timestamp, newrev.timestamp)
599
        self.assertEquals(oldrev.timezone, newrev.timezone)
600
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
601
    def test_already_merged(self):
602
        """We need to use a merge base that makes sense. 
603
        
604
        A
605
        | \
606
        B  D
607
        | \|
608
        C  E
609
610
        Rebasing E on C should result in:
611
0.438.1 by Jelmer Vernooij
Split out function for determining rebase base.
612
        A -> B -> C -> D' -> E'
613
614
        Ancestry:
615
        A: 
616
        B: A
617
        C: A, B
618
        D: A
619
        E: A, B, D
620
        D': A, B, C
621
        E': A, B, C, D'
622
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
623
        """
624
        oldwt = self.make_branch_and_tree("old")
625
        self.build_tree(['old/afile'])
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
626
        file("old/afile", "w").write("A\n" * 10)
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
627
        oldwt.add(["afile"])
628
        oldwt.commit("base", rev_id="A")
629
        newwt = oldwt.bzrdir.sprout("new").open_workingtree()
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
630
        file("old/afile", "w").write("A\n"*10 + "B\n")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
631
        oldwt.commit("bla", rev_id="B")
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
632
        file("old/afile", "w").write("A\n" * 10 + "C\n")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
633
        oldwt.commit("bla", rev_id="C")
634
        self.build_tree(['new/bfile'])
635
        newwt.add(["bfile"])
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
636
        file("new/bfile", "w").write("D\n")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
637
        newwt.commit("bla", rev_id="D")
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
638
        file("new/afile", "w").write("E\n" + "A\n"*10 + "B\n")
639
        file("new/bfile", "w").write("D\nE\n")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
640
        newwt.add_pending_merge("B")
641
        newwt.commit("bla", rev_id="E")
642
        newwt.branch.repository.fetch(oldwt.branch.repository)
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
643
        newwt.lock_write()
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
644
        replay_delta_workingtree(newwt, "D", "D'", ["C"])
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
645
        newwt.unlock()
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
646
        oldrev = newwt.branch.repository.get_revision("D")
647
        newrev = newwt.branch.repository.get_revision("D'")
648
        self.assertEquals(["C"], newrev.parent_ids)
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
649
        newwt.lock_write()
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
650
        self.assertRaises(ConflictsInTree, 
651
            lambda: replay_delta_workingtree(newwt, "E", "E'", ["D'"]))
0.436.62 by Jelmer Vernooij
Fix compatibility with packs.
652
        newwt.unlock()
0.438.2 by Jelmer Vernooij
More work using proper merge bases.
653
        self.assertEquals("E\n" + "A\n" * 10 + "C\n",
654
                open("new/afile", 'r').read())
0.436.56 by Jelmer Vernooij
Fix test.
655
        newwt.set_conflicts(ConflictList())
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
656
        oldrev = newwt.branch.repository.get_revision("E")
0.436.56 by Jelmer Vernooij
Fix test.
657
        commit_rebase(newwt, oldrev, "E'")
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
658
        newrev = newwt.branch.repository.get_revision("E'")
0.438.1 by Jelmer Vernooij
Split out function for determining rebase base.
659
        self.assertEquals(["D'"], newrev.parent_ids)
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
660
        self.assertEquals(["A", "B", "C", "D'", "E'"], 
661
                          newwt.branch.revision_history())
662
0.436.37 by Jelmer Vernooij
Store parents correctly.
663
0.436.34 by Jelmer Vernooij
Some more tests.
664
class TestReplaySnapshotError(TestCase):
665
    def test_create(self):
666
        ReplaySnapshotError("message")
667
668
669
class TestReplayParentsInconsistent(TestCase):
670
    def test_create(self):
671
        ReplayParentsInconsistent("afileid", "arevid")
0.438.1 by Jelmer Vernooij
Split out function for determining rebase base.
672
0.436.54 by Jelmer Vernooij
Move determination of merge base to separate function.
673
674
class ReplayDetermineBaseTests(TestCase):
0.436.145 by Jelmer Vernooij
Fix test.
675
0.436.54 by Jelmer Vernooij
Move determination of merge base to separate function.
676
    def setUp(self):
0.436.145 by Jelmer Vernooij
Fix test.
677
        TestCase.setUp(self)
0.436.54 by Jelmer Vernooij
Move determination of merge base to separate function.
678
        self.graph = Graph(self)
679
0.436.73 by Jelmer Vernooij
Fix tests.
680
    def get_parent_map(self, keys):
681
        return dict((revid, self._parent_dict[revid]) for k in self._parent_dict if k in keys)
682
0.436.54 by Jelmer Vernooij
Move determination of merge base to separate function.
683
    def get_parents(self, revid):
684
        return self._parent_dict[revid]
685
686
    def test_null(self):
687
        self._parent_dict = {"myrev": []}
688
        self.assertEquals(NULL_REVISION, 
689
          replay_determine_base(self.graph, "myrev", [], "mynewrev", ["bar"]))
690
691
    def test_simple(self):
692
        self._parent_dict = {"myinitrev": [], "myrev": ["myinitrev"]}
693
        self.assertEquals("myinitrev", 
694
          replay_determine_base(self.graph, "myrev", ["myinitrev"], 
695
                                "mynewrev", ["mynewparents"]))
696