~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_foreign.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 23:12:55 UTC
  • mfrom: (3920.2.37 dpush)
  • Revision ID: pqm@pqm.ubuntu.com-20090409231255-o8w1g2q3igiyf8b2
(Jelmer) Add the dpush command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
 
21
21
from bzrlib import (
22
22
    branch,
23
 
    bzrdir,
24
23
    errors,
25
24
    foreign,
26
25
    lockable_files,
27
26
    lockdir,
28
 
    revision,
29
 
    tests,
30
27
    trace,
31
28
    )
 
29
from bzrlib.bzrdir import (
 
30
    BzrDir,
 
31
    BzrDirFormat,
 
32
    BzrDirMeta1,
 
33
    BzrDirMetaFormat1,
 
34
    format_registry,
 
35
    )
 
36
from bzrlib.inventory import Inventory
 
37
from bzrlib.revision import Revision
 
38
from bzrlib.tests import (
 
39
    TestCase,
 
40
    TestCaseWithTransport,
 
41
    )
32
42
 
33
43
# This is the dummy foreign revision control system, used 
34
44
# mainly here in the testsuite to test the foreign VCS infrastructure.
74
84
        self.mapping_registry = DummyForeignVcsMappingRegistry()
75
85
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
76
86
                                       "Version 1")
77
 
        self.abbreviation = "dummy"
78
87
 
79
88
    def show_foreign_revid(self, foreign_revid):
80
89
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
81
90
 
82
 
    def serialize_foreign_revid(self, foreign_revid):
83
 
        return "%s|%s|%s" % foreign_revid
84
 
 
85
91
 
86
92
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
87
93
    """A Dummy VCS Branch."""
90
96
        self._format = _format
91
97
        self._base = a_bzrdir.transport.base
92
98
        self._ignore_fallbacks = False
93
 
        self.bzrdir = a_bzrdir
94
99
        foreign.ForeignBranch.__init__(self, 
95
100
            DummyForeignVcsMapping(DummyForeignVcs()))
96
101
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, 
97
102
            *args, **kwargs)
98
103
 
99
 
 
100
 
class InterToDummyVcsBranch(branch.GenericInterBranch,
101
 
                            foreign.InterToForeignBranch):
102
 
 
103
 
    @staticmethod
104
 
    def is_compatible(source, target):
105
 
        return isinstance(target, DummyForeignVcsBranch)
106
 
 
107
 
    def push(self, overwrite=False, stop_revision=None):
108
 
        raise errors.NoRoundtrippingSupport(self.source, self.target)
109
 
 
110
 
    def lossy_push(self, stop_revision=None):
111
 
        result = branch.BranchPushResult()
112
 
        result.source_branch = self.source
113
 
        result.target_branch = self.target
114
 
        result.old_revno, result.old_revid = self.target.last_revision_info()
115
 
        self.source.lock_read()
 
104
    def dpull(self, source, stop_revision=None):
 
105
        source.lock_read()
116
106
        try:
117
107
            # This just handles simple cases, but that's good enough for tests
118
 
            my_history = self.target.revision_history()
119
 
            their_history = self.source.revision_history()
 
108
            my_history = self.revision_history()
 
109
            their_history = source.revision_history()
120
110
            if their_history[:min(len(my_history), len(their_history))] != my_history:
121
 
                raise errors.DivergedBranches(self.target, self.source)
 
111
                raise errors.DivergedBranches(self, source)
122
112
            todo = their_history[len(my_history):]
123
113
            revidmap = {}
124
114
            for revid in todo:
125
 
                rev = self.source.repository.get_revision(revid)
126
 
                tree = self.source.repository.revision_tree(revid)
 
115
                rev = source.repository.get_revision(revid)
 
116
                tree = source.repository.revision_tree(revid)
127
117
                def get_file_with_stat(file_id, path=None):
128
118
                    return (tree.get_file(file_id), None)
129
119
                tree.get_file_with_stat = get_file_with_stat
130
 
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
131
 
                    (str(rev.timestamp), str(rev.timezone), 
132
 
                        str(self.target.revno())))
133
 
                parent_revno, parent_revid= self.target.last_revision_info()
134
 
                if parent_revid == revision.NULL_REVISION:
135
 
                    parent_revids = []
136
 
                else:
137
 
                    parent_revids = [parent_revid]
138
 
                builder = self.target.get_commit_builder(parent_revids, 
139
 
                        self.target.get_config(), rev.timestamp,
 
120
                new_revid = self.mapping.revision_id_foreign_to_bzr(
 
121
                    (str(rev.timestamp), str(rev.timezone), str(self.revno())))
 
122
                parent_revno, parent_revid= self.last_revision_info()
 
123
                builder = self.get_commit_builder([parent_revid], 
 
124
                        self.get_config(), rev.timestamp,
140
125
                        rev.timezone, rev.committer, rev.properties,
141
126
                        new_revid)
142
127
                try:
144
129
                        new_ie = ie.copy()
145
130
                        new_ie.revision = None
146
131
                        builder.record_entry_contents(new_ie, 
147
 
                            [self.target.repository.revision_tree(parent_revid).inventory],
 
132
                            [self.repository.get_inventory(parent_revid)],
148
133
                            path, tree, 
149
134
                            (ie.kind, ie.text_size, ie.executable, ie.text_sha1))
150
135
                    builder.finish_inventory()
152
137
                    builder.abort()
153
138
                    raise
154
139
                revidmap[revid] = builder.commit(rev.message)
155
 
                self.target.set_last_revision_info(parent_revno+1, 
156
 
                    revidmap[revid])
 
140
                self.set_last_revision_info(parent_revno+1, revidmap[revid])
157
141
                trace.mutter('lossily pushed revision %s -> %s', 
158
142
                    revid, revidmap[revid])
159
143
        finally:
160
 
            self.source.unlock()
161
 
        result.new_revno, result.new_revid = self.target.last_revision_info()
162
 
        result.revidmap = revidmap
163
 
        return result
 
144
            source.unlock()
 
145
        return revidmap
164
146
 
165
147
 
166
148
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
172
154
        super(DummyForeignVcsBranchFormat, self).__init__()
173
155
        self._matchingbzrdir = DummyForeignVcsDirFormat()
174
156
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
157
    def open(self, a_bzrdir, _found=False):
176
158
        if not _found:
177
159
            raise NotImplementedError
178
160
        try:
179
 
            transport = a_bzrdir.get_branch_transport(None, name=name)
 
161
            transport = a_bzrdir.get_branch_transport(None)
180
162
            control_files = lockable_files.LockableFiles(transport, 'lock',
181
163
                                                         lockdir.LockDir)
182
164
            return DummyForeignVcsBranch(_format=self,
187
169
            raise errors.NotBranchError(path=transport.base)
188
170
 
189
171
 
190
 
class DummyForeignVcsDirFormat(bzrdir.BzrDirMetaFormat1):
 
172
class DummyForeignVcsDirFormat(BzrDirMetaFormat1):
191
173
    """BzrDirFormat for the dummy foreign VCS."""
192
174
 
193
175
    @classmethod
234
216
        return DummyForeignVcsDir(transport, self)
235
217
 
236
218
 
237
 
class DummyForeignVcsDir(bzrdir.BzrDirMeta1):
 
219
class DummyForeignVcsDir(BzrDirMeta1):
238
220
 
239
221
    def __init__(self, _transport, _format):
240
222
        self._format = _format
244
226
        self._control_files = lockable_files.LockableFiles(self.transport,
245
227
            "lock", lockable_files.TransportLock)
246
228
 
247
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
248
 
        if name is not None:
249
 
            raise errors.NoColocatedBranchSupport(self)
 
229
    def open_branch(self, ignore_fallbacks=True):
250
230
        return self._format.get_branch_format().open(self, _found=True)
251
231
 
252
232
    def cloning_metadir(self, stacked=False):
253
233
        """Produce a metadir suitable for cloning with."""
254
 
        return bzrdir.format_registry.make_bzrdir("default")
 
234
        return format_registry.make_bzrdir("default")
255
235
 
256
236
    def sprout(self, url, revision_id=None, force_new_repo=False,
257
237
               recurse='down', possible_transports=None,
265
245
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
266
246
 
267
247
 
268
 
def register_dummy_foreign_for_test(testcase):
269
 
    bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
 
    testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
 
                        DummyForeignVcsDirFormat)
272
 
    # We need to register the optimiser to make the dummy appears really
273
 
    # different from a regular bzr repository.
274
 
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
275
 
    testcase.addCleanup(branch.InterBranch.unregister_optimiser,
276
 
                        InterToDummyVcsBranch)
277
 
 
278
 
 
279
 
class ForeignVcsRegistryTests(tests.TestCase):
 
248
class ForeignVcsRegistryTests(TestCase):
280
249
    """Tests for the ForeignVcsRegistry class."""
281
250
 
282
251
    def test_parse_revision_id_no_dash(self):
297
266
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
298
267
 
299
268
 
300
 
class ForeignRevisionTests(tests.TestCase):
 
269
class ForeignRevisionTests(TestCase):
301
270
    """Tests for the ForeignRevision class."""
302
271
 
303
272
    def test_create(self):
309
278
        self.assertEquals(mapp, rev.mapping)
310
279
 
311
280
 
312
 
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
313
 
    """Tests for update_workingtree_fileids()."""
314
 
 
315
 
    def test_update_workingtree(self):
 
281
class ShowForeignPropertiesTests(TestCase):
 
282
    """Tests for the show_foreign_properties() function."""
 
283
 
 
284
    def setUp(self):
 
285
        super(ShowForeignPropertiesTests, self).setUp()
 
286
        self.vcs = DummyForeignVcs()
 
287
        foreign.foreign_vcs_registry.register("dummy",
 
288
            self.vcs, "Dummy VCS")
 
289
 
 
290
    def tearDown(self):
 
291
        super(ShowForeignPropertiesTests, self).tearDown()
 
292
        foreign.foreign_vcs_registry.remove("dummy")
 
293
 
 
294
    def test_show_non_foreign(self):
 
295
        """Test use with a native (non-foreign) bzr revision."""
 
296
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
 
297
 
 
298
    def test_show_imported(self):
 
299
        rev = Revision("dummy-v1:my-foreign-revid")
 
300
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
 
301
                          foreign.show_foreign_properties(rev))
 
302
 
 
303
    def test_show_direct(self):
 
304
        rev = foreign.ForeignRevision(("some", "foreign", "revid"),
 
305
                                      DummyForeignVcsMapping(self.vcs),
 
306
                                      "roundtrip-revid")
 
307
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
 
308
                          foreign.show_foreign_properties(rev))
 
309
 
 
310
 
 
311
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
 
312
    """Tests for _determine_fileid_renames()."""
 
313
 
 
314
    def test_det_renames_same(self):
 
315
        a = Inventory()
 
316
        a.add_path("bla", "directory", "bla-a")
 
317
        b = Inventory()
 
318
        b.add_path("bla", "directory", "bla-a")
 
319
        self.assertEquals({
 
320
            '': ('TREE_ROOT', 'TREE_ROOT'), 
 
321
            'bla': ('bla-a', 'bla-a')},
 
322
            foreign._determine_fileid_renames(a, b))
 
323
 
 
324
    def test_det_renames_simple(self):
 
325
        a = Inventory()
 
326
        a.add_path("bla", "directory", "bla-a")
 
327
        b = Inventory()
 
328
        b.add_path("bla", "directory", "bla-b")
 
329
        self.assertEquals({
 
330
            '': ('TREE_ROOT', 'TREE_ROOT'), 
 
331
            'bla': ('bla-a', 'bla-b'),
 
332
            }, foreign._determine_fileid_renames(a, b))
 
333
 
 
334
    def test_det_renames_root(self):
 
335
        a = Inventory()
 
336
        a.add_path("", "directory", "bla-a")
 
337
        b = Inventory()
 
338
        b.add_path("", "directory", "bla-b")
 
339
        self.assertEquals(
 
340
                {"": ("bla-a", "bla-b")},
 
341
                foreign._determine_fileid_renames(a, b))
 
342
 
 
343
    def test_update_workinginv(self):
 
344
        a = Inventory()
 
345
        a.add_path("bla", "directory", "bla-a")
 
346
        b = Inventory()
 
347
        b.add_path("bla", "directory", "bla-b")
316
348
        wt = self.make_branch_and_tree('br1')
317
349
        self.build_tree_contents([('br1/bla', 'original contents\n')])
318
350
        wt.add('bla', 'bla-a')
319
 
        wt.commit('bla-a')
320
 
        root_id = wt.get_root_id()
321
 
        target = wt.bzrdir.sprout('br2').open_workingtree()
322
 
        target.unversion(['bla-a'])
323
 
        target.add('bla', 'bla-b')
324
 
        target.commit('bla-b')
325
 
        target_basis = target.basis_tree()
326
 
        target_basis.lock_read()
327
 
        self.addCleanup(target_basis.unlock)
328
 
        foreign.update_workingtree_fileids(wt, target_basis)
 
351
        foreign.update_workinginv_fileids(wt, a, b)
329
352
        wt.lock_read()
330
353
        try:
331
 
            self.assertEquals(set([root_id, "bla-b"]), set(wt.inventory))
 
354
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
332
355
        finally:
333
356
            wt.unlock()
334
357
 
335
358
 
336
 
class DummyForeignVcsTests(tests.TestCaseWithTransport):
 
359
class DummyForeignVcsTests(TestCaseWithTransport):
337
360
    """Very basic test for DummyForeignVcs."""
338
361
 
339
362
    def setUp(self):
 
363
        BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
 
364
        self.addCleanup(self.unregister)
340
365
        super(DummyForeignVcsTests, self).setUp()
341
 
        register_dummy_foreign_for_test(self)
 
366
 
 
367
    def unregister(self):
 
368
        try:
 
369
            BzrDirFormat.unregister_control_format(DummyForeignVcsDirFormat)
 
370
        except ValueError:
 
371
            pass
342
372
 
343
373
    def test_create(self):
344
374
        """Test we can create dummies."""
345
375
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
346
 
        dir = bzrdir.BzrDir.open("d")
 
376
        dir = BzrDir.open("d")
347
377
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
348
378
        dir.open_repository()
349
379
        dir.open_branch()
352
382
    def test_sprout(self):
353
383
        """Test we can clone dummies and that the format is not preserved."""
354
384
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
355
 
        dir = bzrdir.BzrDir.open("d")
 
385
        dir = BzrDir.open("d")
356
386
        newdir = dir.sprout("e")
357
 
        self.assertNotEquals("A Dummy VCS Dir",
358
 
                             newdir._format.get_format_string())
359
 
 
360
 
    def test_push_not_supported(self):
361
 
        source_tree = self.make_branch_and_tree("source")
362
 
        target_tree = self.make_branch_and_tree("target", 
363
 
            format=DummyForeignVcsDirFormat())
364
 
        self.assertRaises(errors.NoRoundtrippingSupport, 
365
 
            source_tree.branch.push, target_tree.branch)
366
 
 
367
 
    def test_lossy_push_empty(self):
368
 
        source_tree = self.make_branch_and_tree("source")
369
 
        target_tree = self.make_branch_and_tree("target", 
370
 
            format=DummyForeignVcsDirFormat())
371
 
        pushresult = source_tree.branch.lossy_push(target_tree.branch)
372
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
373
 
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
374
 
        self.assertEquals({}, pushresult.revidmap)
375
 
 
376
 
    def test_lossy_push_simple(self):
377
 
        source_tree = self.make_branch_and_tree("source")
378
 
        self.build_tree(['source/a', 'source/b'])
379
 
        source_tree.add(['a', 'b'])
380
 
        revid1 = source_tree.commit("msg")
381
 
        target_tree = self.make_branch_and_tree("target", 
382
 
            format=DummyForeignVcsDirFormat())
383
 
        target_tree.branch.lock_write()
384
 
        try:
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
386
 
        finally:
387
 
            target_tree.branch.unlock()
388
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
389
 
        self.assertEquals({revid1:target_tree.branch.last_revision()}, 
390
 
                           pushresult.revidmap)
391
 
        self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)
 
387
        self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())