/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_foreign.py

  • Committer: Lawrence Mitchell
  • Date: 2011-11-08 10:54:09 UTC
  • mto: This revision was merged to the branch mainline in revision 6245.
  • Revision ID: wence@gmx.li-20111108105409-cgdqeek7vl7e8nbb
Only set check_clean if {clean} is asked for in template
      
The {clean} flag in templates requires an lstat of every file in the
repository.  If the filesystem is slow this can take a noticeable
amount of time.  So only switch check_clean to True if the template
actually asks for it, to avoid penalising those who don't want to
know.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008-2011 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
21
21
from bzrlib import (
22
22
    branch,
23
23
    bzrdir,
 
24
    controldir,
24
25
    errors,
25
26
    foreign,
26
27
    lockable_files,
27
28
    lockdir,
 
29
    repository,
28
30
    revision,
29
31
    tests,
30
32
    trace,
 
33
    vf_repository,
31
34
    )
32
35
 
 
36
from bzrlib.repofmt import groupcompress_repo
 
37
 
33
38
# This is the dummy foreign revision control system, used 
34
39
# mainly here in the testsuite to test the foreign VCS infrastructure.
35
40
# It is basically standard Bazaar with some minor modifications to 
91
96
        self._base = a_bzrdir.transport.base
92
97
        self._ignore_fallbacks = False
93
98
        self.bzrdir = a_bzrdir
94
 
        foreign.ForeignBranch.__init__(self, 
 
99
        foreign.ForeignBranch.__init__(self,
95
100
            DummyForeignVcsMapping(DummyForeignVcs()))
96
 
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, 
 
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):
 
104
    def _get_checkout_format(self, lightweight=False):
 
105
        """Return the most suitable metadir for a checkout of this branch.
 
106
        Weaves are used if this branch's repository uses weaves.
 
107
        """
 
108
        return self.bzrdir.checkout_metadir()
 
109
 
 
110
    def import_last_revision_info_and_tags(self, source, revno, revid,
 
111
                                           lossy=False):
 
112
        interbranch = InterToDummyVcsBranch(source, self)
 
113
        result = interbranch.push(stop_revision=revid, lossy=True)
 
114
        if lossy:
 
115
            revid = result.revidmap[revid]
 
116
        return (revno, revid)
 
117
 
 
118
 
 
119
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
 
120
 
 
121
    def _generate_revision_if_needed(self):
 
122
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
 
123
        if self._lossy:
 
124
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
 
125
                (str(self._timestamp), str(self._timezone), "UNKNOWN"))
 
126
            self.random_revid = False
 
127
        elif self._new_revision_id is not None:
 
128
            self.random_revid = False
 
129
        else:
 
130
            self._new_revision_id = self._gen_revision_id()
 
131
            self.random_revid = True
 
132
 
 
133
 
 
134
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
 
135
    foreign.ForeignRepository):
 
136
    """Dummy foreign vcs repository."""
 
137
 
 
138
 
 
139
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
 
140
 
 
141
    repository_class = DummyForeignVcsRepository
 
142
    _commit_builder_class = DummyForeignCommitBuilder
 
143
 
 
144
    def get_format_string(self):
 
145
        return "Dummy Foreign Vcs Repository"
 
146
 
 
147
    def get_format_description(self):
 
148
        return "Dummy Foreign Vcs Repository"
 
149
 
 
150
 
 
151
def branch_history(graph, revid):
 
152
    ret = list(graph.iter_lefthand_ancestry(revid,
 
153
        (revision.NULL_REVISION,)))
 
154
    ret.reverse()
 
155
    return ret
 
156
 
 
157
 
 
158
class InterToDummyVcsBranch(branch.GenericInterBranch):
102
159
 
103
160
    @staticmethod
104
161
    def is_compatible(source, target):
105
162
        return isinstance(target, DummyForeignVcsBranch)
106
163
 
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):
 
164
    def push(self, overwrite=False, stop_revision=None, lossy=False):
 
165
        if not lossy:
 
166
            raise errors.NoRoundtrippingSupport(self.source, self.target)
111
167
        result = branch.BranchPushResult()
112
168
        result.source_branch = self.source
113
169
        result.target_branch = self.target
114
170
        result.old_revno, result.old_revid = self.target.last_revision_info()
115
171
        self.source.lock_read()
116
172
        try:
 
173
            graph = self.source.repository.get_graph()
117
174
            # 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()
 
175
            my_history = branch_history(self.target.repository.get_graph(),
 
176
                result.old_revid)
 
177
            if stop_revision is None:
 
178
                stop_revision = self.source.last_revision()
 
179
            their_history = branch_history(graph, stop_revision)
120
180
            if their_history[:min(len(my_history), len(their_history))] != my_history:
121
181
                raise errors.DivergedBranches(self.target, self.source)
122
182
            todo = their_history[len(my_history):]
128
188
                    return (tree.get_file(file_id), None)
129
189
                tree.get_file_with_stat = get_file_with_stat
130
190
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
131
 
                    (str(rev.timestamp), str(rev.timezone), 
 
191
                    (str(rev.timestamp), str(rev.timezone),
132
192
                        str(self.target.revno())))
133
193
                parent_revno, parent_revid= self.target.last_revision_info()
134
194
                if parent_revid == revision.NULL_REVISION:
168
228
    def get_format_string(self):
169
229
        return "Branch for Testing"
170
230
 
171
 
    def __init__(self):
172
 
        super(DummyForeignVcsBranchFormat, self).__init__()
173
 
        self._matchingbzrdir = DummyForeignVcsDirFormat()
 
231
    @property
 
232
    def _matchingbzrdir(self):
 
233
        return DummyForeignVcsDirFormat()
174
234
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
235
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
236
            found_repository=None):
176
237
        if not _found:
177
238
            raise NotImplementedError
178
239
        try:
179
240
            transport = a_bzrdir.get_branch_transport(None, name=name)
180
241
            control_files = lockable_files.LockableFiles(transport, 'lock',
181
242
                                                         lockdir.LockDir)
 
243
            if found_repository is None:
 
244
                found_repository = a_bzrdir.find_repository()
182
245
            return DummyForeignVcsBranch(_format=self,
183
246
                              _control_files=control_files,
184
247
                              a_bzrdir=a_bzrdir,
185
 
                              _repository=a_bzrdir.find_repository())
 
248
                              _repository=found_repository)
186
249
        except errors.NoSuchFile:
187
250
            raise errors.NotBranchError(path=transport.base)
188
251
 
205
268
    def get_branch_format(self):
206
269
        return DummyForeignVcsBranchFormat()
207
270
 
208
 
    @classmethod
209
 
    def probe_transport(klass, transport):
210
 
        """Return the .bzrdir style format present in a directory."""
211
 
        if not transport.has('.dummy'):
212
 
            raise errors.NotBranchError(path=transport.base)
213
 
        return klass()
 
271
    @property
 
272
    def repository_format(self):
 
273
        return DummyForeignVcsRepositoryFormat()
214
274
 
215
275
    def initialize_on_transport(self, transport):
216
276
        """Initialize a new bzrdir in the base directory of a Transport."""
244
304
        self._control_files = lockable_files.LockableFiles(self.transport,
245
305
            "lock", lockable_files.TransportLock)
246
306
 
 
307
    def create_workingtree(self):
 
308
        # dirstate requires a ".bzr" entry to exist
 
309
        self.root_transport.put_bytes(".bzr", "foo")
 
310
        return super(DummyForeignVcsDir, self).create_workingtree()
 
311
 
247
312
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
248
313
        if name is not None:
249
314
            raise errors.NoColocatedBranchSupport(self)
253
318
        """Produce a metadir suitable for cloning with."""
254
319
        return bzrdir.format_registry.make_bzrdir("default")
255
320
 
 
321
    def checkout_metadir(self):
 
322
        return self.cloning_metadir()
 
323
 
256
324
    def sprout(self, url, revision_id=None, force_new_repo=False,
257
325
               recurse='down', possible_transports=None,
258
326
               accelerator_tree=None, hardlink=False, stacked=False,
266
334
 
267
335
 
268
336
def register_dummy_foreign_for_test(testcase):
269
 
    bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
 
    testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
 
                        DummyForeignVcsDirFormat)
 
337
    controldir.ControlDirFormat.register_prober(DummyForeignProber)
 
338
    testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
 
339
        DummyForeignProber)
 
340
    repository.format_registry.register(DummyForeignVcsRepositoryFormat())
 
341
    testcase.addCleanup(repository.format_registry.remove,
 
342
            DummyForeignVcsRepositoryFormat())
 
343
    branch.format_registry.register(DummyForeignVcsBranchFormat())
 
344
    testcase.addCleanup(branch.format_registry.remove,
 
345
            DummyForeignVcsBranchFormat())
272
346
    # We need to register the optimiser to make the dummy appears really
273
347
    # different from a regular bzr repository.
274
348
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
276
350
                        InterToDummyVcsBranch)
277
351
 
278
352
 
 
353
class DummyForeignProber(controldir.Prober):
 
354
 
 
355
    @classmethod
 
356
    def probe_transport(klass, transport):
 
357
        """Return the .bzrdir style format present in a directory."""
 
358
        if not transport.has('.dummy'):
 
359
            raise errors.NotBranchError(path=transport.base)
 
360
        return DummyForeignVcsDirFormat()
 
361
 
 
362
    @classmethod
 
363
    def known_formats(cls):
 
364
        return set([DummyForeignVcsDirFormat()])
 
365
 
 
366
 
279
367
class ForeignVcsRegistryTests(tests.TestCase):
280
368
    """Tests for the ForeignVcsRegistry class."""
281
369
 
293
381
        reg = foreign.ForeignVcsRegistry()
294
382
        vcs = DummyForeignVcs()
295
383
        reg.register("dummy", vcs, "Dummy VCS")
296
 
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
297
 
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
 
384
        self.assertEquals((
 
385
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
 
386
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
298
387
 
299
388
 
300
389
class ForeignRevisionTests(tests.TestCase):
368
457
        source_tree = self.make_branch_and_tree("source")
369
458
        target_tree = self.make_branch_and_tree("target", 
370
459
            format=DummyForeignVcsDirFormat())
371
 
        pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
460
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
372
461
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
373
462
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
374
463
        self.assertEquals({}, pushresult.revidmap)
382
471
            format=DummyForeignVcsDirFormat())
383
472
        target_tree.branch.lock_write()
384
473
        try:
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
474
            pushresult = source_tree.branch.push(
 
475
                target_tree.branch, lossy=True)
386
476
        finally:
387
477
            target_tree.branch.unlock()
388
478
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)