74
84
        self.mapping_registry = DummyForeignVcsMappingRegistry()
 
75
85
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
 
77
 
        self.abbreviation = "dummy"
 
79
88
    def show_foreign_revid(self, foreign_revid):
 
80
89
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
 
82
 
    def serialize_foreign_revid(self, foreign_revid):
 
83
 
        return "%s|%s|%s" % foreign_revid
 
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, 
 
100
 
class InterToDummyVcsBranch(branch.GenericInterBranch,
 
101
 
                            foreign.InterToForeignBranch):
 
104
 
    def is_compatible(source, target):
 
105
 
        return isinstance(target, DummyForeignVcsBranch)
 
107
 
    def push(self, overwrite=False, stop_revision=None):
 
108
 
        raise errors.NoRoundtrippingSupport(self.source, self.target)
 
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):
 
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):]
 
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:
 
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,
 
 
172
154
        super(DummyForeignVcsBranchFormat, self).__init__()
 
173
155
        self._matchingbzrdir = DummyForeignVcsDirFormat()
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
 
157
    def open(self, a_bzrdir, _found=False):
 
177
159
            raise NotImplementedError
 
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',
 
182
164
            return DummyForeignVcsBranch(_format=self,
 
 
244
226
        self._control_files = lockable_files.LockableFiles(self.transport,
 
245
227
            "lock", lockable_files.TransportLock)
 
247
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
 
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)
 
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")
 
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)
 
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)
 
279
 
class ForeignVcsRegistryTests(tests.TestCase):
 
 
248
class ForeignVcsRegistryTests(TestCase):
 
280
249
    """Tests for the ForeignVcsRegistry class."""
 
282
251
    def test_parse_revision_id_no_dash(self):
 
 
309
278
        self.assertEquals(mapp, rev.mapping)
 
312
 
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
 
 
281
class ShowForeignPropertiesTests(TestCase):
 
 
282
    """Tests for the show_foreign_properties() function."""
 
 
285
        super(ShowForeignPropertiesTests, self).setUp()
 
 
286
        self.vcs = DummyForeignVcs()
 
 
287
        foreign.foreign_vcs_registry.register("dummy",
 
 
288
            self.vcs, "Dummy VCS")
 
 
291
        super(ShowForeignPropertiesTests, self).tearDown()
 
 
292
        foreign.foreign_vcs_registry.remove("dummy")
 
 
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")))
 
 
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))
 
 
303
    def test_show_direct(self):
 
 
304
        rev = foreign.ForeignRevision(("some", "foreign", "revid"),
 
 
305
                                      DummyForeignVcsMapping(self.vcs),
 
 
307
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
 
 
308
                          foreign.show_foreign_properties(rev))
 
 
311
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
 
313
312
    """Tests for update_workingtree_fileids()."""
 
315
314
    def test_update_workingtree(self):
 
 
328
326
        foreign.update_workingtree_fileids(wt, target_basis)
 
331
 
            self.assertEquals(set([root_id, "bla-b"]), set(wt.inventory))
 
 
329
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
 
336
 
class DummyForeignVcsTests(tests.TestCaseWithTransport):
 
 
334
class DummyForeignVcsTests(TestCaseWithTransport):
 
337
335
    """Very basic test for DummyForeignVcs."""
 
 
338
        BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
 
 
339
        self.addCleanup(self.unregister)
 
340
340
        super(DummyForeignVcsTests, self).setUp()
 
341
 
        register_dummy_foreign_for_test(self)
 
 
342
    def unregister(self):
 
 
344
            BzrDirFormat.unregister_control_format(DummyForeignVcsDirFormat)
 
343
348
    def test_create(self):
 
344
349
        """Test we can create dummies."""
 
345
350
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
 
346
 
        dir = bzrdir.BzrDir.open("d")
 
 
351
        dir = BzrDir.open("d")
 
347
352
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
 
348
353
        dir.open_repository()
 
349
354
        dir.open_branch()
 
 
352
357
    def test_sprout(self):
 
353
358
        """Test we can clone dummies and that the format is not preserved."""
 
354
359
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
 
355
 
        dir = bzrdir.BzrDir.open("d")
 
 
360
        dir = BzrDir.open("d")
 
356
361
        newdir = dir.sprout("e")
 
357
 
        self.assertNotEquals("A Dummy VCS Dir",
 
358
 
                             newdir._format.get_format_string())
 
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)
 
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)
 
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()
 
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
387
 
            target_tree.branch.unlock()
 
388
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
 
389
 
        self.assertEquals({revid1:target_tree.branch.last_revision()}, 
 
391
 
        self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)
 
 
362
        self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())