~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: Martin Pool
  • Date: 2009-06-19 10:00:56 UTC
  • mto: This revision was merged to the branch mainline in revision 4464.
  • Revision ID: mbp@sourcefrog.net-20090619100056-fco5ooae2ybl88ne
Fix copyrights and remove assert statement from doc_generate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
 
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
 
5
# the Free Software Foundation; either version 2 of the License, or
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for the Repository facility that are not interface tests.
 
18
 
 
19
For interface tests see tests/per_repository/*.py.
 
20
 
 
21
For concrete class tests see this file, and for storage formats tests
 
22
also see this file.
 
23
"""
 
24
 
 
25
from stat import S_ISDIR
 
26
from StringIO import StringIO
 
27
 
 
28
import bzrlib
 
29
from bzrlib.errors import (NotBranchError,
 
30
                           NoSuchFile,
 
31
                           UnknownFormatError,
 
32
                           UnsupportedFormatError,
 
33
                           )
 
34
from bzrlib import (
 
35
    graph,
 
36
    tests,
 
37
    )
 
38
from bzrlib.branchbuilder import BranchBuilder
 
39
from bzrlib.btree_index import BTreeBuilder, BTreeGraphIndex
 
40
from bzrlib.index import GraphIndex, InMemoryGraphIndex
 
41
from bzrlib.repository import RepositoryFormat
 
42
from bzrlib.smart import server
 
43
from bzrlib.tests import (
 
44
    TestCase,
 
45
    TestCaseWithTransport,
 
46
    TestSkipped,
 
47
    test_knit,
 
48
    )
 
49
from bzrlib.transport import (
 
50
    fakenfs,
 
51
    get_transport,
 
52
    )
 
53
from bzrlib.transport.memory import MemoryServer
 
54
from bzrlib import (
 
55
    bencode,
 
56
    bzrdir,
 
57
    errors,
 
58
    inventory,
 
59
    osutils,
 
60
    progress,
 
61
    repository,
 
62
    revision as _mod_revision,
 
63
    symbol_versioning,
 
64
    upgrade,
 
65
    workingtree,
 
66
    )
 
67
from bzrlib.repofmt import (
 
68
    groupcompress_repo,
 
69
    knitrepo,
 
70
    pack_repo,
 
71
    weaverepo,
 
72
    )
 
73
 
 
74
 
 
75
class TestDefaultFormat(TestCase):
 
76
 
 
77
    def test_get_set_default_format(self):
 
78
        old_default = bzrdir.format_registry.get('default')
 
79
        private_default = old_default().repository_format.__class__
 
80
        old_format = repository.RepositoryFormat.get_default_format()
 
81
        self.assertTrue(isinstance(old_format, private_default))
 
82
        def make_sample_bzrdir():
 
83
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
 
84
            my_bzrdir.repository_format = SampleRepositoryFormat()
 
85
            return my_bzrdir
 
86
        bzrdir.format_registry.remove('default')
 
87
        bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
 
88
        bzrdir.format_registry.set_default('sample')
 
89
        # creating a repository should now create an instrumented dir.
 
90
        try:
 
91
            # the default branch format is used by the meta dir format
 
92
            # which is not the default bzrdir format at this point
 
93
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
 
94
            result = dir.create_repository()
 
95
            self.assertEqual(result, 'A bzr repository dir')
 
96
        finally:
 
97
            bzrdir.format_registry.remove('default')
 
98
            bzrdir.format_registry.remove('sample')
 
99
            bzrdir.format_registry.register('default', old_default, '')
 
100
        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
 
101
                              old_format.__class__)
 
102
 
 
103
 
 
104
class SampleRepositoryFormat(repository.RepositoryFormat):
 
105
    """A sample format
 
106
 
 
107
    this format is initializable, unsupported to aid in testing the
 
108
    open and open(unsupported=True) routines.
 
109
    """
 
110
 
 
111
    def get_format_string(self):
 
112
        """See RepositoryFormat.get_format_string()."""
 
113
        return "Sample .bzr repository format."
 
114
 
 
115
    def initialize(self, a_bzrdir, shared=False):
 
116
        """Initialize a repository in a BzrDir"""
 
117
        t = a_bzrdir.get_repository_transport(self)
 
118
        t.put_bytes('format', self.get_format_string())
 
119
        return 'A bzr repository dir'
 
120
 
 
121
    def is_supported(self):
 
122
        return False
 
123
 
 
124
    def open(self, a_bzrdir, _found=False):
 
125
        return "opened repository."
 
126
 
 
127
 
 
128
class TestRepositoryFormat(TestCaseWithTransport):
 
129
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
 
130
 
 
131
    def test_find_format(self):
 
132
        # is the right format object found for a repository?
 
133
        # create a branch with a few known format objects.
 
134
        # this is not quite the same as
 
135
        self.build_tree(["foo/", "bar/"])
 
136
        def check_format(format, url):
 
137
            dir = format._matchingbzrdir.initialize(url)
 
138
            format.initialize(dir)
 
139
            t = get_transport(url)
 
140
            found_format = repository.RepositoryFormat.find_format(dir)
 
141
            self.failUnless(isinstance(found_format, format.__class__))
 
142
        check_format(weaverepo.RepositoryFormat7(), "bar")
 
143
 
 
144
    def test_find_format_no_repository(self):
 
145
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
146
        self.assertRaises(errors.NoRepositoryPresent,
 
147
                          repository.RepositoryFormat.find_format,
 
148
                          dir)
 
149
 
 
150
    def test_find_format_unknown_format(self):
 
151
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
152
        SampleRepositoryFormat().initialize(dir)
 
153
        self.assertRaises(UnknownFormatError,
 
154
                          repository.RepositoryFormat.find_format,
 
155
                          dir)
 
156
 
 
157
    def test_register_unregister_format(self):
 
158
        format = SampleRepositoryFormat()
 
159
        # make a control dir
 
160
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
161
        # make a repo
 
162
        format.initialize(dir)
 
163
        # register a format for it.
 
164
        repository.RepositoryFormat.register_format(format)
 
165
        # which repository.Open will refuse (not supported)
 
166
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
 
167
        # but open(unsupported) will work
 
168
        self.assertEqual(format.open(dir), "opened repository.")
 
169
        # unregister the format
 
170
        repository.RepositoryFormat.unregister_format(format)
 
171
 
 
172
 
 
173
class TestFormat6(TestCaseWithTransport):
 
174
 
 
175
    def test_attribute__fetch_order(self):
 
176
        """Weaves need topological data insertion."""
 
177
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
178
        repo = weaverepo.RepositoryFormat6().initialize(control)
 
179
        self.assertEqual('topological', repo._format._fetch_order)
 
180
 
 
181
    def test_attribute__fetch_uses_deltas(self):
 
182
        """Weaves do not reuse deltas."""
 
183
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
184
        repo = weaverepo.RepositoryFormat6().initialize(control)
 
185
        self.assertEqual(False, repo._format._fetch_uses_deltas)
 
186
 
 
187
    def test_attribute__fetch_reconcile(self):
 
188
        """Weave repositories need a reconcile after fetch."""
 
189
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
190
        repo = weaverepo.RepositoryFormat6().initialize(control)
 
191
        self.assertEqual(True, repo._format._fetch_reconcile)
 
192
 
 
193
    def test_no_ancestry_weave(self):
 
194
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
195
        repo = weaverepo.RepositoryFormat6().initialize(control)
 
196
        # We no longer need to create the ancestry.weave file
 
197
        # since it is *never* used.
 
198
        self.assertRaises(NoSuchFile,
 
199
                          control.transport.get,
 
200
                          'ancestry.weave')
 
201
 
 
202
    def test_supports_external_lookups(self):
 
203
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
204
        repo = weaverepo.RepositoryFormat6().initialize(control)
 
205
        self.assertFalse(repo._format.supports_external_lookups)
 
206
 
 
207
 
 
208
class TestFormat7(TestCaseWithTransport):
 
209
 
 
210
    def test_attribute__fetch_order(self):
 
211
        """Weaves need topological data insertion."""
 
212
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
213
        repo = weaverepo.RepositoryFormat7().initialize(control)
 
214
        self.assertEqual('topological', repo._format._fetch_order)
 
215
 
 
216
    def test_attribute__fetch_uses_deltas(self):
 
217
        """Weaves do not reuse deltas."""
 
218
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
219
        repo = weaverepo.RepositoryFormat7().initialize(control)
 
220
        self.assertEqual(False, repo._format._fetch_uses_deltas)
 
221
 
 
222
    def test_attribute__fetch_reconcile(self):
 
223
        """Weave repositories need a reconcile after fetch."""
 
224
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
225
        repo = weaverepo.RepositoryFormat7().initialize(control)
 
226
        self.assertEqual(True, repo._format._fetch_reconcile)
 
227
 
 
228
    def test_disk_layout(self):
 
229
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
230
        repo = weaverepo.RepositoryFormat7().initialize(control)
 
231
        # in case of side effects of locking.
 
232
        repo.lock_write()
 
233
        repo.unlock()
 
234
        # we want:
 
235
        # format 'Bazaar-NG Repository format 7'
 
236
        # lock ''
 
237
        # inventory.weave == empty_weave
 
238
        # empty revision-store directory
 
239
        # empty weaves directory
 
240
        t = control.get_repository_transport(None)
 
241
        self.assertEqualDiff('Bazaar-NG Repository format 7',
 
242
                             t.get('format').read())
 
243
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
 
244
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
 
245
        self.assertEqualDiff('# bzr weave file v5\n'
 
246
                             'w\n'
 
247
                             'W\n',
 
248
                             t.get('inventory.weave').read())
 
249
        # Creating a file with id Foo:Bar results in a non-escaped file name on
 
250
        # disk.
 
251
        control.create_branch()
 
252
        tree = control.create_workingtree()
 
253
        tree.add(['foo'], ['Foo:Bar'], ['file'])
 
254
        tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
 
255
        tree.commit('first post', rev_id='first')
 
256
        self.assertEqualDiff(
 
257
            '# bzr weave file v5\n'
 
258
            'i\n'
 
259
            '1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
 
260
            'n first\n'
 
261
            '\n'
 
262
            'w\n'
 
263
            '{ 0\n'
 
264
            '. content\n'
 
265
            '}\n'
 
266
            'W\n',
 
267
            t.get('weaves/74/Foo%3ABar.weave').read())
 
268
 
 
269
    def test_shared_disk_layout(self):
 
270
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
271
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
 
272
        # we want:
 
273
        # format 'Bazaar-NG Repository format 7'
 
274
        # inventory.weave == empty_weave
 
275
        # empty revision-store directory
 
276
        # empty weaves directory
 
277
        # a 'shared-storage' marker file.
 
278
        # lock is not present when unlocked
 
279
        t = control.get_repository_transport(None)
 
280
        self.assertEqualDiff('Bazaar-NG Repository format 7',
 
281
                             t.get('format').read())
 
282
        self.assertEqualDiff('', t.get('shared-storage').read())
 
283
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
 
284
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
 
285
        self.assertEqualDiff('# bzr weave file v5\n'
 
286
                             'w\n'
 
287
                             'W\n',
 
288
                             t.get('inventory.weave').read())
 
289
        self.assertFalse(t.has('branch-lock'))
 
290
 
 
291
    def test_creates_lockdir(self):
 
292
        """Make sure it appears to be controlled by a LockDir existence"""
 
293
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
294
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
 
295
        t = control.get_repository_transport(None)
 
296
        # TODO: Should check there is a 'lock' toplevel directory,
 
297
        # regardless of contents
 
298
        self.assertFalse(t.has('lock/held/info'))
 
299
        repo.lock_write()
 
300
        try:
 
301
            self.assertTrue(t.has('lock/held/info'))
 
302
        finally:
 
303
            # unlock so we don't get a warning about failing to do so
 
304
            repo.unlock()
 
305
 
 
306
    def test_uses_lockdir(self):
 
307
        """repo format 7 actually locks on lockdir"""
 
308
        base_url = self.get_url()
 
309
        control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
 
310
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
 
311
        t = control.get_repository_transport(None)
 
312
        repo.lock_write()
 
313
        repo.unlock()
 
314
        del repo
 
315
        # make sure the same lock is created by opening it
 
316
        repo = repository.Repository.open(base_url)
 
317
        repo.lock_write()
 
318
        self.assertTrue(t.has('lock/held/info'))
 
319
        repo.unlock()
 
320
        self.assertFalse(t.has('lock/held/info'))
 
321
 
 
322
    def test_shared_no_tree_disk_layout(self):
 
323
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
324
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
 
325
        repo.set_make_working_trees(False)
 
326
        # we want:
 
327
        # format 'Bazaar-NG Repository format 7'
 
328
        # lock ''
 
329
        # inventory.weave == empty_weave
 
330
        # empty revision-store directory
 
331
        # empty weaves directory
 
332
        # a 'shared-storage' marker file.
 
333
        t = control.get_repository_transport(None)
 
334
        self.assertEqualDiff('Bazaar-NG Repository format 7',
 
335
                             t.get('format').read())
 
336
        ## self.assertEqualDiff('', t.get('lock').read())
 
337
        self.assertEqualDiff('', t.get('shared-storage').read())
 
338
        self.assertEqualDiff('', t.get('no-working-trees').read())
 
339
        repo.set_make_working_trees(True)
 
340
        self.assertFalse(t.has('no-working-trees'))
 
341
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
 
342
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
 
343
        self.assertEqualDiff('# bzr weave file v5\n'
 
344
                             'w\n'
 
345
                             'W\n',
 
346
                             t.get('inventory.weave').read())
 
347
 
 
348
    def test_supports_external_lookups(self):
 
349
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
350
        repo = weaverepo.RepositoryFormat7().initialize(control)
 
351
        self.assertFalse(repo._format.supports_external_lookups)
 
352
 
 
353
 
 
354
class TestFormatKnit1(TestCaseWithTransport):
 
355
 
 
356
    def test_attribute__fetch_order(self):
 
357
        """Knits need topological data insertion."""
 
358
        repo = self.make_repository('.',
 
359
                format=bzrdir.format_registry.get('knit')())
 
360
        self.assertEqual('topological', repo._format._fetch_order)
 
361
 
 
362
    def test_attribute__fetch_uses_deltas(self):
 
363
        """Knits reuse deltas."""
 
364
        repo = self.make_repository('.',
 
365
                format=bzrdir.format_registry.get('knit')())
 
366
        self.assertEqual(True, repo._format._fetch_uses_deltas)
 
367
 
 
368
    def test_disk_layout(self):
 
369
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
370
        repo = knitrepo.RepositoryFormatKnit1().initialize(control)
 
371
        # in case of side effects of locking.
 
372
        repo.lock_write()
 
373
        repo.unlock()
 
374
        # we want:
 
375
        # format 'Bazaar-NG Knit Repository Format 1'
 
376
        # lock: is a directory
 
377
        # inventory.weave == empty_weave
 
378
        # empty revision-store directory
 
379
        # empty weaves directory
 
380
        t = control.get_repository_transport(None)
 
381
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
 
382
                             t.get('format').read())
 
383
        # XXX: no locks left when unlocked at the moment
 
384
        # self.assertEqualDiff('', t.get('lock').read())
 
385
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
 
386
        self.check_knits(t)
 
387
        # Check per-file knits.
 
388
        branch = control.create_branch()
 
389
        tree = control.create_workingtree()
 
390
        tree.add(['foo'], ['Nasty-IdC:'], ['file'])
 
391
        tree.put_file_bytes_non_atomic('Nasty-IdC:', '')
 
392
        tree.commit('1st post', rev_id='foo')
 
393
        self.assertHasKnit(t, 'knits/e8/%254easty-%2549d%2543%253a',
 
394
            '\nfoo fulltext 0 81  :')
 
395
 
 
396
    def assertHasKnit(self, t, knit_name, extra_content=''):
 
397
        """Assert that knit_name exists on t."""
 
398
        self.assertEqualDiff('# bzr knit index 8\n' + extra_content,
 
399
                             t.get(knit_name + '.kndx').read())
 
400
 
 
401
    def check_knits(self, t):
 
402
        """check knit content for a repository."""
 
403
        self.assertHasKnit(t, 'inventory')
 
404
        self.assertHasKnit(t, 'revisions')
 
405
        self.assertHasKnit(t, 'signatures')
 
406
 
 
407
    def test_shared_disk_layout(self):
 
408
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
409
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
 
410
        # we want:
 
411
        # format 'Bazaar-NG Knit Repository Format 1'
 
412
        # lock: is a directory
 
413
        # inventory.weave == empty_weave
 
414
        # empty revision-store directory
 
415
        # empty weaves directory
 
416
        # a 'shared-storage' marker file.
 
417
        t = control.get_repository_transport(None)
 
418
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
 
419
                             t.get('format').read())
 
420
        # XXX: no locks left when unlocked at the moment
 
421
        # self.assertEqualDiff('', t.get('lock').read())
 
422
        self.assertEqualDiff('', t.get('shared-storage').read())
 
423
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
 
424
        self.check_knits(t)
 
425
 
 
426
    def test_shared_no_tree_disk_layout(self):
 
427
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
428
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
 
429
        repo.set_make_working_trees(False)
 
430
        # we want:
 
431
        # format 'Bazaar-NG Knit Repository Format 1'
 
432
        # lock ''
 
433
        # inventory.weave == empty_weave
 
434
        # empty revision-store directory
 
435
        # empty weaves directory
 
436
        # a 'shared-storage' marker file.
 
437
        t = control.get_repository_transport(None)
 
438
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
 
439
                             t.get('format').read())
 
440
        # XXX: no locks left when unlocked at the moment
 
441
        # self.assertEqualDiff('', t.get('lock').read())
 
442
        self.assertEqualDiff('', t.get('shared-storage').read())
 
443
        self.assertEqualDiff('', t.get('no-working-trees').read())
 
444
        repo.set_make_working_trees(True)
 
445
        self.assertFalse(t.has('no-working-trees'))
 
446
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
 
447
        self.check_knits(t)
 
448
 
 
449
    def test_deserialise_sets_root_revision(self):
 
450
        """We must have a inventory.root.revision
 
451
 
 
452
        Old versions of the XML5 serializer did not set the revision_id for
 
453
        the whole inventory. So we grab the one from the expected text. Which
 
454
        is valid when the api is not being abused.
 
455
        """
 
456
        repo = self.make_repository('.',
 
457
                format=bzrdir.format_registry.get('knit')())
 
458
        inv_xml = '<inventory format="5">\n</inventory>\n'
 
459
        inv = repo.deserialise_inventory('test-rev-id', inv_xml)
 
460
        self.assertEqual('test-rev-id', inv.root.revision)
 
461
 
 
462
    def test_deserialise_uses_global_revision_id(self):
 
463
        """If it is set, then we re-use the global revision id"""
 
464
        repo = self.make_repository('.',
 
465
                format=bzrdir.format_registry.get('knit')())
 
466
        inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
 
467
                   '</inventory>\n')
 
468
        # Arguably, the deserialise_inventory should detect a mismatch, and
 
469
        # raise an error, rather than silently using one revision_id over the
 
470
        # other.
 
471
        self.assertRaises(AssertionError, repo.deserialise_inventory,
 
472
            'test-rev-id', inv_xml)
 
473
        inv = repo.deserialise_inventory('other-rev-id', inv_xml)
 
474
        self.assertEqual('other-rev-id', inv.root.revision)
 
475
 
 
476
    def test_supports_external_lookups(self):
 
477
        repo = self.make_repository('.',
 
478
                format=bzrdir.format_registry.get('knit')())
 
479
        self.assertFalse(repo._format.supports_external_lookups)
 
480
 
 
481
 
 
482
class DummyRepository(object):
 
483
    """A dummy repository for testing."""
 
484
 
 
485
    _format = None
 
486
    _serializer = None
 
487
 
 
488
    def supports_rich_root(self):
 
489
        return False
 
490
 
 
491
    def get_graph(self):
 
492
        raise NotImplementedError
 
493
 
 
494
    def get_parent_map(self, revision_ids):
 
495
        raise NotImplementedError
 
496
 
 
497
 
 
498
class InterDummy(repository.InterRepository):
 
499
    """An inter-repository optimised code path for DummyRepository.
 
500
 
 
501
    This is for use during testing where we use DummyRepository as repositories
 
502
    so that none of the default regsitered inter-repository classes will
 
503
    MATCH.
 
504
    """
 
505
 
 
506
    @staticmethod
 
507
    def is_compatible(repo_source, repo_target):
 
508
        """InterDummy is compatible with DummyRepository."""
 
509
        return (isinstance(repo_source, DummyRepository) and
 
510
            isinstance(repo_target, DummyRepository))
 
511
 
 
512
 
 
513
class TestInterRepository(TestCaseWithTransport):
 
514
 
 
515
    def test_get_default_inter_repository(self):
 
516
        # test that the InterRepository.get(repo_a, repo_b) probes
 
517
        # for a inter_repo class where is_compatible(repo_a, repo_b) returns
 
518
        # true and returns a default inter_repo otherwise.
 
519
        # This also tests that the default registered optimised interrepository
 
520
        # classes do not barf inappropriately when a surprising repository type
 
521
        # is handed to them.
 
522
        dummy_a = DummyRepository()
 
523
        dummy_b = DummyRepository()
 
524
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
 
525
 
 
526
    def assertGetsDefaultInterRepository(self, repo_a, repo_b):
 
527
        """Asserts that InterRepository.get(repo_a, repo_b) -> the default.
 
528
 
 
529
        The effective default is now InterSameDataRepository because there is
 
530
        no actual sane default in the presence of incompatible data models.
 
531
        """
 
532
        inter_repo = repository.InterRepository.get(repo_a, repo_b)
 
533
        self.assertEqual(repository.InterSameDataRepository,
 
534
                         inter_repo.__class__)
 
535
        self.assertEqual(repo_a, inter_repo.source)
 
536
        self.assertEqual(repo_b, inter_repo.target)
 
537
 
 
538
    def test_register_inter_repository_class(self):
 
539
        # test that a optimised code path provider - a
 
540
        # InterRepository subclass can be registered and unregistered
 
541
        # and that it is correctly selected when given a repository
 
542
        # pair that it returns true on for the is_compatible static method
 
543
        # check
 
544
        dummy_a = DummyRepository()
 
545
        dummy_b = DummyRepository()
 
546
        repo = self.make_repository('.')
 
547
        # hack dummies to look like repo somewhat.
 
548
        dummy_a._serializer = repo._serializer
 
549
        dummy_b._serializer = repo._serializer
 
550
        repository.InterRepository.register_optimiser(InterDummy)
 
551
        try:
 
552
            # we should get the default for something InterDummy returns False
 
553
            # to
 
554
            self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
 
555
            self.assertGetsDefaultInterRepository(dummy_a, repo)
 
556
            # and we should get an InterDummy for a pair it 'likes'
 
557
            self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
 
558
            inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
 
559
            self.assertEqual(InterDummy, inter_repo.__class__)
 
560
            self.assertEqual(dummy_a, inter_repo.source)
 
561
            self.assertEqual(dummy_b, inter_repo.target)
 
562
        finally:
 
563
            repository.InterRepository.unregister_optimiser(InterDummy)
 
564
        # now we should get the default InterRepository object again.
 
565
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
 
566
 
 
567
 
 
568
class TestInterWeaveRepo(TestCaseWithTransport):
 
569
 
 
570
    def test_is_compatible_and_registered(self):
 
571
        # InterWeaveRepo is compatible when either side
 
572
        # is a format 5/6/7 branch
 
573
        from bzrlib.repofmt import knitrepo, weaverepo
 
574
        formats = [weaverepo.RepositoryFormat5(),
 
575
                   weaverepo.RepositoryFormat6(),
 
576
                   weaverepo.RepositoryFormat7()]
 
577
        incompatible_formats = [weaverepo.RepositoryFormat4(),
 
578
                                knitrepo.RepositoryFormatKnit1(),
 
579
                                ]
 
580
        repo_a = self.make_repository('a')
 
581
        repo_b = self.make_repository('b')
 
582
        is_compatible = repository.InterWeaveRepo.is_compatible
 
583
        for source in incompatible_formats:
 
584
            # force incompatible left then right
 
585
            repo_a._format = source
 
586
            repo_b._format = formats[0]
 
587
            self.assertFalse(is_compatible(repo_a, repo_b))
 
588
            self.assertFalse(is_compatible(repo_b, repo_a))
 
589
        for source in formats:
 
590
            repo_a._format = source
 
591
            for target in formats:
 
592
                repo_b._format = target
 
593
                self.assertTrue(is_compatible(repo_a, repo_b))
 
594
        self.assertEqual(repository.InterWeaveRepo,
 
595
                         repository.InterRepository.get(repo_a,
 
596
                                                        repo_b).__class__)
 
597
 
 
598
 
 
599
class TestRepositoryConverter(TestCaseWithTransport):
 
600
 
 
601
    def test_convert_empty(self):
 
602
        t = get_transport(self.get_url('.'))
 
603
        t.mkdir('repository')
 
604
        repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
 
605
        repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
 
606
        target_format = knitrepo.RepositoryFormatKnit1()
 
607
        converter = repository.CopyConverter(target_format)
 
608
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
609
        try:
 
610
            converter.convert(repo, pb)
 
611
        finally:
 
612
            pb.finished()
 
613
        repo = repo_dir.open_repository()
 
614
        self.assertTrue(isinstance(target_format, repo._format.__class__))
 
615
 
 
616
 
 
617
class TestMisc(TestCase):
 
618
 
 
619
    def test_unescape_xml(self):
 
620
        """We get some kind of error when malformed entities are passed"""
 
621
        self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
 
622
 
 
623
 
 
624
class TestRepositoryFormatKnit3(TestCaseWithTransport):
 
625
 
 
626
    def test_attribute__fetch_order(self):
 
627
        """Knits need topological data insertion."""
 
628
        format = bzrdir.BzrDirMetaFormat1()
 
629
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
630
        repo = self.make_repository('.', format=format)
 
631
        self.assertEqual('topological', repo._format._fetch_order)
 
632
 
 
633
    def test_attribute__fetch_uses_deltas(self):
 
634
        """Knits reuse deltas."""
 
635
        format = bzrdir.BzrDirMetaFormat1()
 
636
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
637
        repo = self.make_repository('.', format=format)
 
638
        self.assertEqual(True, repo._format._fetch_uses_deltas)
 
639
 
 
640
    def test_convert(self):
 
641
        """Ensure the upgrade adds weaves for roots"""
 
642
        format = bzrdir.BzrDirMetaFormat1()
 
643
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
644
        tree = self.make_branch_and_tree('.', format)
 
645
        tree.commit("Dull commit", rev_id="dull")
 
646
        revision_tree = tree.branch.repository.revision_tree('dull')
 
647
        revision_tree.lock_read()
 
648
        try:
 
649
            self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
 
650
                revision_tree.inventory.root.file_id)
 
651
        finally:
 
652
            revision_tree.unlock()
 
653
        format = bzrdir.BzrDirMetaFormat1()
 
654
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
655
        upgrade.Convert('.', format)
 
656
        tree = workingtree.WorkingTree.open('.')
 
657
        revision_tree = tree.branch.repository.revision_tree('dull')
 
658
        revision_tree.lock_read()
 
659
        try:
 
660
            revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
 
661
        finally:
 
662
            revision_tree.unlock()
 
663
        tree.commit("Another dull commit", rev_id='dull2')
 
664
        revision_tree = tree.branch.repository.revision_tree('dull2')
 
665
        revision_tree.lock_read()
 
666
        self.addCleanup(revision_tree.unlock)
 
667
        self.assertEqual('dull', revision_tree.inventory.root.revision)
 
668
 
 
669
    def test_supports_external_lookups(self):
 
670
        format = bzrdir.BzrDirMetaFormat1()
 
671
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
672
        repo = self.make_repository('.', format=format)
 
673
        self.assertFalse(repo._format.supports_external_lookups)
 
674
 
 
675
 
 
676
class TestDevelopment6(TestCaseWithTransport):
 
677
 
 
678
    def test_inventories_use_chk_map_with_parent_base_dict(self):
 
679
        tree = self.make_branch_and_tree('repo', format="development6-rich-root")
 
680
        revid = tree.commit("foo")
 
681
        tree.lock_read()
 
682
        self.addCleanup(tree.unlock)
 
683
        inv = tree.branch.repository.get_inventory(revid)
 
684
        self.assertNotEqual(None, inv.parent_id_basename_to_file_id)
 
685
        inv.parent_id_basename_to_file_id._ensure_root()
 
686
        inv.id_to_entry._ensure_root()
 
687
        self.assertEqual(65536, inv.id_to_entry._root_node.maximum_size)
 
688
        self.assertEqual(65536,
 
689
            inv.parent_id_basename_to_file_id._root_node.maximum_size)
 
690
 
 
691
    def test_stream_source_to_gc(self):
 
692
        source = self.make_repository('source', format='development6-rich-root')
 
693
        target = self.make_repository('target', format='development6-rich-root')
 
694
        stream = source._get_source(target._format)
 
695
        self.assertIsInstance(stream, groupcompress_repo.GroupCHKStreamSource)
 
696
 
 
697
    def test_stream_source_to_non_gc(self):
 
698
        source = self.make_repository('source', format='development6-rich-root')
 
699
        target = self.make_repository('target', format='rich-root-pack')
 
700
        stream = source._get_source(target._format)
 
701
        # We don't want the child GroupCHKStreamSource
 
702
        self.assertIs(type(stream), repository.StreamSource)
 
703
 
 
704
    def test_get_stream_for_missing_keys_includes_all_chk_refs(self):
 
705
        source_builder = self.make_branch_builder('source',
 
706
                            format='development6-rich-root')
 
707
        # We have to build a fairly large tree, so that we are sure the chk
 
708
        # pages will have split into multiple pages.
 
709
        entries = [('add', ('', 'a-root-id', 'directory', None))]
 
710
        for i in 'abcdefghijklmnopqrstuvwxyz123456789':
 
711
            for j in 'abcdefghijklmnopqrstuvwxyz123456789':
 
712
                fname = i + j
 
713
                fid = fname + '-id'
 
714
                content = 'content for %s\n' % (fname,)
 
715
                entries.append(('add', (fname, fid, 'file', content)))
 
716
        source_builder.start_series()
 
717
        source_builder.build_snapshot('rev-1', None, entries)
 
718
        # Now change a few of them, so we get a few new pages for the second
 
719
        # revision
 
720
        source_builder.build_snapshot('rev-2', ['rev-1'], [
 
721
            ('modify', ('aa-id', 'new content for aa-id\n')),
 
722
            ('modify', ('cc-id', 'new content for cc-id\n')),
 
723
            ('modify', ('zz-id', 'new content for zz-id\n')),
 
724
            ])
 
725
        source_builder.finish_series()
 
726
        source_branch = source_builder.get_branch()
 
727
        source_branch.lock_read()
 
728
        self.addCleanup(source_branch.unlock)
 
729
        target = self.make_repository('target', format='development6-rich-root')
 
730
        source = source_branch.repository._get_source(target._format)
 
731
        self.assertIsInstance(source, groupcompress_repo.GroupCHKStreamSource)
 
732
 
 
733
        # On a regular pass, getting the inventories and chk pages for rev-2
 
734
        # would only get the newly created chk pages
 
735
        search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
 
736
                                    set(['rev-2']))
 
737
        simple_chk_records = []
 
738
        for vf_name, substream in source.get_stream(search):
 
739
            if vf_name == 'chk_bytes':
 
740
                for record in substream:
 
741
                    simple_chk_records.append(record.key)
 
742
            else:
 
743
                for _ in substream:
 
744
                    continue
 
745
        # 3 pages, the root (InternalNode), + 2 pages which actually changed
 
746
        self.assertEqual([('sha1:91481f539e802c76542ea5e4c83ad416bf219f73',),
 
747
                          ('sha1:4ff91971043668583985aec83f4f0ab10a907d3f',),
 
748
                          ('sha1:81e7324507c5ca132eedaf2d8414ee4bb2226187',),
 
749
                          ('sha1:b101b7da280596c71a4540e9a1eeba8045985ee0',)],
 
750
                         simple_chk_records)
 
751
        # Now, when we do a similar call using 'get_stream_for_missing_keys'
 
752
        # we should get a much larger set of pages.
 
753
        missing = [('inventories', 'rev-2')]
 
754
        full_chk_records = []
 
755
        for vf_name, substream in source.get_stream_for_missing_keys(missing):
 
756
            if vf_name == 'inventories':
 
757
                for record in substream:
 
758
                    self.assertEqual(('rev-2',), record.key)
 
759
            elif vf_name == 'chk_bytes':
 
760
                for record in substream:
 
761
                    full_chk_records.append(record.key)
 
762
            else:
 
763
                self.fail('Should not be getting a stream of %s' % (vf_name,))
 
764
        # We have 257 records now. This is because we have 1 root page, and 256
 
765
        # leaf pages in a complete listing.
 
766
        self.assertEqual(257, len(full_chk_records))
 
767
        self.assertSubset(simple_chk_records, full_chk_records)
 
768
 
 
769
 
 
770
class TestKnitPackStreamSource(tests.TestCaseWithMemoryTransport):
 
771
 
 
772
    def test_source_to_exact_pack_092(self):
 
773
        source = self.make_repository('source', format='pack-0.92')
 
774
        target = self.make_repository('target', format='pack-0.92')
 
775
        stream_source = source._get_source(target._format)
 
776
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
777
 
 
778
    def test_source_to_exact_pack_rich_root_pack(self):
 
779
        source = self.make_repository('source', format='rich-root-pack')
 
780
        target = self.make_repository('target', format='rich-root-pack')
 
781
        stream_source = source._get_source(target._format)
 
782
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
783
 
 
784
    def test_source_to_exact_pack_19(self):
 
785
        source = self.make_repository('source', format='1.9')
 
786
        target = self.make_repository('target', format='1.9')
 
787
        stream_source = source._get_source(target._format)
 
788
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
789
 
 
790
    def test_source_to_exact_pack_19_rich_root(self):
 
791
        source = self.make_repository('source', format='1.9-rich-root')
 
792
        target = self.make_repository('target', format='1.9-rich-root')
 
793
        stream_source = source._get_source(target._format)
 
794
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
795
 
 
796
    def test_source_to_remote_exact_pack_19(self):
 
797
        trans = self.make_smart_server('target')
 
798
        trans.ensure_base()
 
799
        source = self.make_repository('source', format='1.9')
 
800
        target = self.make_repository('target', format='1.9')
 
801
        target = repository.Repository.open(trans.base)
 
802
        stream_source = source._get_source(target._format)
 
803
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
804
 
 
805
    def test_stream_source_to_non_exact(self):
 
806
        source = self.make_repository('source', format='pack-0.92')
 
807
        target = self.make_repository('target', format='1.9')
 
808
        stream = source._get_source(target._format)
 
809
        self.assertIs(type(stream), repository.StreamSource)
 
810
 
 
811
    def test_stream_source_to_non_exact_rich_root(self):
 
812
        source = self.make_repository('source', format='1.9')
 
813
        target = self.make_repository('target', format='1.9-rich-root')
 
814
        stream = source._get_source(target._format)
 
815
        self.assertIs(type(stream), repository.StreamSource)
 
816
 
 
817
    def test_source_to_remote_non_exact_pack_19(self):
 
818
        trans = self.make_smart_server('target')
 
819
        trans.ensure_base()
 
820
        source = self.make_repository('source', format='1.9')
 
821
        target = self.make_repository('target', format='1.6')
 
822
        target = repository.Repository.open(trans.base)
 
823
        stream_source = source._get_source(target._format)
 
824
        self.assertIs(type(stream_source), repository.StreamSource)
 
825
 
 
826
    def test_stream_source_to_knit(self):
 
827
        source = self.make_repository('source', format='pack-0.92')
 
828
        target = self.make_repository('target', format='dirstate')
 
829
        stream = source._get_source(target._format)
 
830
        self.assertIs(type(stream), repository.StreamSource)
 
831
 
 
832
 
 
833
class TestDevelopment6FindParentIdsOfRevisions(TestCaseWithTransport):
 
834
    """Tests for _find_parent_ids_of_revisions."""
 
835
 
 
836
    def setUp(self):
 
837
        super(TestDevelopment6FindParentIdsOfRevisions, self).setUp()
 
838
        self.builder = self.make_branch_builder('source',
 
839
            format='development6-rich-root')
 
840
        self.builder.start_series()
 
841
        self.builder.build_snapshot('initial', None,
 
842
            [('add', ('', 'tree-root', 'directory', None))])
 
843
        self.repo = self.builder.get_branch().repository
 
844
        self.addCleanup(self.builder.finish_series)
 
845
 
 
846
    def assertParentIds(self, expected_result, rev_set):
 
847
        self.assertEqual(sorted(expected_result),
 
848
            sorted(self.repo._find_parent_ids_of_revisions(rev_set)))
 
849
 
 
850
    def test_simple(self):
 
851
        self.builder.build_snapshot('revid1', None, [])
 
852
        self.builder.build_snapshot('revid2', ['revid1'], [])
 
853
        rev_set = ['revid2']
 
854
        self.assertParentIds(['revid1'], rev_set)
 
855
 
 
856
    def test_not_first_parent(self):
 
857
        self.builder.build_snapshot('revid1', None, [])
 
858
        self.builder.build_snapshot('revid2', ['revid1'], [])
 
859
        self.builder.build_snapshot('revid3', ['revid2'], [])
 
860
        rev_set = ['revid3', 'revid2']
 
861
        self.assertParentIds(['revid1'], rev_set)
 
862
 
 
863
    def test_not_null(self):
 
864
        rev_set = ['initial']
 
865
        self.assertParentIds([], rev_set)
 
866
 
 
867
    def test_not_null_set(self):
 
868
        self.builder.build_snapshot('revid1', None, [])
 
869
        rev_set = [_mod_revision.NULL_REVISION]
 
870
        self.assertParentIds([], rev_set)
 
871
 
 
872
    def test_ghost(self):
 
873
        self.builder.build_snapshot('revid1', None, [])
 
874
        rev_set = ['ghost', 'revid1']
 
875
        self.assertParentIds(['initial'], rev_set)
 
876
 
 
877
    def test_ghost_parent(self):
 
878
        self.builder.build_snapshot('revid1', None, [])
 
879
        self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
 
880
        rev_set = ['revid2', 'revid1']
 
881
        self.assertParentIds(['ghost', 'initial'], rev_set)
 
882
 
 
883
    def test_righthand_parent(self):
 
884
        self.builder.build_snapshot('revid1', None, [])
 
885
        self.builder.build_snapshot('revid2a', ['revid1'], [])
 
886
        self.builder.build_snapshot('revid2b', ['revid1'], [])
 
887
        self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
 
888
        rev_set = ['revid3', 'revid2a']
 
889
        self.assertParentIds(['revid1', 'revid2b'], rev_set)
 
890
 
 
891
 
 
892
class TestWithBrokenRepo(TestCaseWithTransport):
 
893
    """These tests seem to be more appropriate as interface tests?"""
 
894
 
 
895
    def make_broken_repository(self):
 
896
        # XXX: This function is borrowed from Aaron's "Reconcile can fix bad
 
897
        # parent references" branch which is due to land in bzr.dev soon.  Once
 
898
        # it does, this duplication should be removed.
 
899
        repo = self.make_repository('broken-repo')
 
900
        cleanups = []
 
901
        try:
 
902
            repo.lock_write()
 
903
            cleanups.append(repo.unlock)
 
904
            repo.start_write_group()
 
905
            cleanups.append(repo.commit_write_group)
 
906
            # make rev1a: A well-formed revision, containing 'file1'
 
907
            inv = inventory.Inventory(revision_id='rev1a')
 
908
            inv.root.revision = 'rev1a'
 
909
            self.add_file(repo, inv, 'file1', 'rev1a', [])
 
910
            repo.add_inventory('rev1a', inv, [])
 
911
            revision = _mod_revision.Revision('rev1a',
 
912
                committer='jrandom@example.com', timestamp=0,
 
913
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
 
914
            repo.add_revision('rev1a',revision, inv)
 
915
 
 
916
            # make rev1b, which has no Revision, but has an Inventory, and
 
917
            # file1
 
918
            inv = inventory.Inventory(revision_id='rev1b')
 
919
            inv.root.revision = 'rev1b'
 
920
            self.add_file(repo, inv, 'file1', 'rev1b', [])
 
921
            repo.add_inventory('rev1b', inv, [])
 
922
 
 
923
            # make rev2, with file1 and file2
 
924
            # file2 is sane
 
925
            # file1 has 'rev1b' as an ancestor, even though this is not
 
926
            # mentioned by 'rev1a', making it an unreferenced ancestor
 
927
            inv = inventory.Inventory()
 
928
            self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
 
929
            self.add_file(repo, inv, 'file2', 'rev2', [])
 
930
            self.add_revision(repo, 'rev2', inv, ['rev1a'])
 
931
 
 
932
            # make ghost revision rev1c
 
933
            inv = inventory.Inventory()
 
934
            self.add_file(repo, inv, 'file2', 'rev1c', [])
 
935
 
 
936
            # make rev3 with file2
 
937
            # file2 refers to 'rev1c', which is a ghost in this repository, so
 
938
            # file2 cannot have rev1c as its ancestor.
 
939
            inv = inventory.Inventory()
 
940
            self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
 
941
            self.add_revision(repo, 'rev3', inv, ['rev1c'])
 
942
            return repo
 
943
        finally:
 
944
            for cleanup in reversed(cleanups):
 
945
                cleanup()
 
946
 
 
947
    def add_revision(self, repo, revision_id, inv, parent_ids):
 
948
        inv.revision_id = revision_id
 
949
        inv.root.revision = revision_id
 
950
        repo.add_inventory(revision_id, inv, parent_ids)
 
951
        revision = _mod_revision.Revision(revision_id,
 
952
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
 
953
            timezone=0, message='foo', parent_ids=parent_ids)
 
954
        repo.add_revision(revision_id,revision, inv)
 
955
 
 
956
    def add_file(self, repo, inv, filename, revision, parents):
 
957
        file_id = filename + '-id'
 
958
        entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
 
959
        entry.revision = revision
 
960
        entry.text_size = 0
 
961
        inv.add(entry)
 
962
        text_key = (file_id, revision)
 
963
        parent_keys = [(file_id, parent) for parent in parents]
 
964
        repo.texts.add_lines(text_key, parent_keys, ['line\n'])
 
965
 
 
966
    def test_insert_from_broken_repo(self):
 
967
        """Inserting a data stream from a broken repository won't silently
 
968
        corrupt the target repository.
 
969
        """
 
970
        broken_repo = self.make_broken_repository()
 
971
        empty_repo = self.make_repository('empty-repo')
 
972
        # See bug https://bugs.launchpad.net/bzr/+bug/389141 for information
 
973
        # about why this was turned into expectFailure
 
974
        self.expectFailure('new Stream fetch fills in missing compression'
 
975
           ' parents (bug #389141)',
 
976
           self.assertRaises, (errors.RevisionNotPresent, errors.BzrCheckError),
 
977
                              empty_repo.fetch, broken_repo)
 
978
        self.assertRaises((errors.RevisionNotPresent, errors.BzrCheckError),
 
979
                          empty_repo.fetch, broken_repo)
 
980
 
 
981
 
 
982
class TestRepositoryPackCollection(TestCaseWithTransport):
 
983
 
 
984
    def get_format(self):
 
985
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
 
986
 
 
987
    def get_packs(self):
 
988
        format = self.get_format()
 
989
        repo = self.make_repository('.', format=format)
 
990
        return repo._pack_collection
 
991
 
 
992
    def make_packs_and_alt_repo(self, write_lock=False):
 
993
        """Create a pack repo with 3 packs, and access it via a second repo."""
 
994
        tree = self.make_branch_and_tree('.')
 
995
        tree.lock_write()
 
996
        self.addCleanup(tree.unlock)
 
997
        rev1 = tree.commit('one')
 
998
        rev2 = tree.commit('two')
 
999
        rev3 = tree.commit('three')
 
1000
        r = repository.Repository.open('.')
 
1001
        if write_lock:
 
1002
            r.lock_write()
 
1003
        else:
 
1004
            r.lock_read()
 
1005
        self.addCleanup(r.unlock)
 
1006
        packs = r._pack_collection
 
1007
        packs.ensure_loaded()
 
1008
        return tree, r, packs, [rev1, rev2, rev3]
 
1009
 
 
1010
    def test__max_pack_count(self):
 
1011
        """The maximum pack count is a function of the number of revisions."""
 
1012
        # no revisions - one pack, so that we can have a revision free repo
 
1013
        # without it blowing up
 
1014
        packs = self.get_packs()
 
1015
        self.assertEqual(1, packs._max_pack_count(0))
 
1016
        # after that the sum of the digits, - check the first 1-9
 
1017
        self.assertEqual(1, packs._max_pack_count(1))
 
1018
        self.assertEqual(2, packs._max_pack_count(2))
 
1019
        self.assertEqual(3, packs._max_pack_count(3))
 
1020
        self.assertEqual(4, packs._max_pack_count(4))
 
1021
        self.assertEqual(5, packs._max_pack_count(5))
 
1022
        self.assertEqual(6, packs._max_pack_count(6))
 
1023
        self.assertEqual(7, packs._max_pack_count(7))
 
1024
        self.assertEqual(8, packs._max_pack_count(8))
 
1025
        self.assertEqual(9, packs._max_pack_count(9))
 
1026
        # check the boundary cases with two digits for the next decade
 
1027
        self.assertEqual(1, packs._max_pack_count(10))
 
1028
        self.assertEqual(2, packs._max_pack_count(11))
 
1029
        self.assertEqual(10, packs._max_pack_count(19))
 
1030
        self.assertEqual(2, packs._max_pack_count(20))
 
1031
        self.assertEqual(3, packs._max_pack_count(21))
 
1032
        # check some arbitrary big numbers
 
1033
        self.assertEqual(25, packs._max_pack_count(112894))
 
1034
 
 
1035
    def test_pack_distribution_zero(self):
 
1036
        packs = self.get_packs()
 
1037
        self.assertEqual([0], packs.pack_distribution(0))
 
1038
 
 
1039
    def test_ensure_loaded_unlocked(self):
 
1040
        packs = self.get_packs()
 
1041
        self.assertRaises(errors.ObjectNotLocked,
 
1042
                          packs.ensure_loaded)
 
1043
 
 
1044
    def test_pack_distribution_one_to_nine(self):
 
1045
        packs = self.get_packs()
 
1046
        self.assertEqual([1],
 
1047
            packs.pack_distribution(1))
 
1048
        self.assertEqual([1, 1],
 
1049
            packs.pack_distribution(2))
 
1050
        self.assertEqual([1, 1, 1],
 
1051
            packs.pack_distribution(3))
 
1052
        self.assertEqual([1, 1, 1, 1],
 
1053
            packs.pack_distribution(4))
 
1054
        self.assertEqual([1, 1, 1, 1, 1],
 
1055
            packs.pack_distribution(5))
 
1056
        self.assertEqual([1, 1, 1, 1, 1, 1],
 
1057
            packs.pack_distribution(6))
 
1058
        self.assertEqual([1, 1, 1, 1, 1, 1, 1],
 
1059
            packs.pack_distribution(7))
 
1060
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
 
1061
            packs.pack_distribution(8))
 
1062
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
 
1063
            packs.pack_distribution(9))
 
1064
 
 
1065
    def test_pack_distribution_stable_at_boundaries(self):
 
1066
        """When there are multi-rev packs the counts are stable."""
 
1067
        packs = self.get_packs()
 
1068
        # in 10s:
 
1069
        self.assertEqual([10], packs.pack_distribution(10))
 
1070
        self.assertEqual([10, 1], packs.pack_distribution(11))
 
1071
        self.assertEqual([10, 10], packs.pack_distribution(20))
 
1072
        self.assertEqual([10, 10, 1], packs.pack_distribution(21))
 
1073
        # 100s
 
1074
        self.assertEqual([100], packs.pack_distribution(100))
 
1075
        self.assertEqual([100, 1], packs.pack_distribution(101))
 
1076
        self.assertEqual([100, 10, 1], packs.pack_distribution(111))
 
1077
        self.assertEqual([100, 100], packs.pack_distribution(200))
 
1078
        self.assertEqual([100, 100, 1], packs.pack_distribution(201))
 
1079
        self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
 
1080
 
 
1081
    def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
 
1082
        packs = self.get_packs()
 
1083
        existing_packs = [(2000, "big"), (9, "medium")]
 
1084
        # rev count - 2009 -> 2x1000 + 9x1
 
1085
        pack_operations = packs.plan_autopack_combinations(
 
1086
            existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
 
1087
        self.assertEqual([], pack_operations)
 
1088
 
 
1089
    def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
 
1090
        packs = self.get_packs()
 
1091
        existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
 
1092
        # rev count - 2010 -> 2x1000 + 1x10
 
1093
        pack_operations = packs.plan_autopack_combinations(
 
1094
            existing_packs, [1000, 1000, 10])
 
1095
        self.assertEqual([], pack_operations)
 
1096
 
 
1097
    def test_plan_pack_operations_2010_combines_smallest_two(self):
 
1098
        packs = self.get_packs()
 
1099
        existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
 
1100
            (1, "single1")]
 
1101
        # rev count - 2010 -> 2x1000 + 1x10 (3)
 
1102
        pack_operations = packs.plan_autopack_combinations(
 
1103
            existing_packs, [1000, 1000, 10])
 
1104
        self.assertEqual([[2, ["single2", "single1"]]], pack_operations)
 
1105
 
 
1106
    def test_plan_pack_operations_creates_a_single_op(self):
 
1107
        packs = self.get_packs()
 
1108
        existing_packs = [(50, 'a'), (40, 'b'), (30, 'c'), (10, 'd'),
 
1109
                          (10, 'e'), (6, 'f'), (4, 'g')]
 
1110
        # rev count 150 -> 1x100 and 5x10
 
1111
        # The two size 10 packs do not need to be touched. The 50, 40, 30 would
 
1112
        # be combined into a single 120 size pack, and the 6 & 4 would
 
1113
        # becombined into a size 10 pack. However, if we have to rewrite them,
 
1114
        # we save a pack file with no increased I/O by putting them into the
 
1115
        # same file.
 
1116
        distribution = packs.pack_distribution(150)
 
1117
        pack_operations = packs.plan_autopack_combinations(existing_packs,
 
1118
                                                           distribution)
 
1119
        self.assertEqual([[130, ['a', 'b', 'c', 'f', 'g']]], pack_operations)
 
1120
 
 
1121
    def test_all_packs_none(self):
 
1122
        format = self.get_format()
 
1123
        tree = self.make_branch_and_tree('.', format=format)
 
1124
        tree.lock_read()
 
1125
        self.addCleanup(tree.unlock)
 
1126
        packs = tree.branch.repository._pack_collection
 
1127
        packs.ensure_loaded()
 
1128
        self.assertEqual([], packs.all_packs())
 
1129
 
 
1130
    def test_all_packs_one(self):
 
1131
        format = self.get_format()
 
1132
        tree = self.make_branch_and_tree('.', format=format)
 
1133
        tree.commit('start')
 
1134
        tree.lock_read()
 
1135
        self.addCleanup(tree.unlock)
 
1136
        packs = tree.branch.repository._pack_collection
 
1137
        packs.ensure_loaded()
 
1138
        self.assertEqual([
 
1139
            packs.get_pack_by_name(packs.names()[0])],
 
1140
            packs.all_packs())
 
1141
 
 
1142
    def test_all_packs_two(self):
 
1143
        format = self.get_format()
 
1144
        tree = self.make_branch_and_tree('.', format=format)
 
1145
        tree.commit('start')
 
1146
        tree.commit('continue')
 
1147
        tree.lock_read()
 
1148
        self.addCleanup(tree.unlock)
 
1149
        packs = tree.branch.repository._pack_collection
 
1150
        packs.ensure_loaded()
 
1151
        self.assertEqual([
 
1152
            packs.get_pack_by_name(packs.names()[0]),
 
1153
            packs.get_pack_by_name(packs.names()[1]),
 
1154
            ], packs.all_packs())
 
1155
 
 
1156
    def test_get_pack_by_name(self):
 
1157
        format = self.get_format()
 
1158
        tree = self.make_branch_and_tree('.', format=format)
 
1159
        tree.commit('start')
 
1160
        tree.lock_read()
 
1161
        self.addCleanup(tree.unlock)
 
1162
        packs = tree.branch.repository._pack_collection
 
1163
        packs.reset()
 
1164
        packs.ensure_loaded()
 
1165
        name = packs.names()[0]
 
1166
        pack_1 = packs.get_pack_by_name(name)
 
1167
        # the pack should be correctly initialised
 
1168
        sizes = packs._names[name]
 
1169
        rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
 
1170
        inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
 
1171
        txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
 
1172
        sig_index = GraphIndex(packs._index_transport, name + '.six', sizes[3])
 
1173
        self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
 
1174
            name, rev_index, inv_index, txt_index, sig_index), pack_1)
 
1175
        # and the same instance should be returned on successive calls.
 
1176
        self.assertTrue(pack_1 is packs.get_pack_by_name(name))
 
1177
 
 
1178
    def test_reload_pack_names_new_entry(self):
 
1179
        tree, r, packs, revs = self.make_packs_and_alt_repo()
 
1180
        names = packs.names()
 
1181
        # Add a new pack file into the repository
 
1182
        rev4 = tree.commit('four')
 
1183
        new_names = tree.branch.repository._pack_collection.names()
 
1184
        new_name = set(new_names).difference(names)
 
1185
        self.assertEqual(1, len(new_name))
 
1186
        new_name = new_name.pop()
 
1187
        # The old collection hasn't noticed yet
 
1188
        self.assertEqual(names, packs.names())
 
1189
        self.assertTrue(packs.reload_pack_names())
 
1190
        self.assertEqual(new_names, packs.names())
 
1191
        # And the repository can access the new revision
 
1192
        self.assertEqual({rev4:(revs[-1],)}, r.get_parent_map([rev4]))
 
1193
        self.assertFalse(packs.reload_pack_names())
 
1194
 
 
1195
    def test_reload_pack_names_added_and_removed(self):
 
1196
        tree, r, packs, revs = self.make_packs_and_alt_repo()
 
1197
        names = packs.names()
 
1198
        # Now repack the whole thing
 
1199
        tree.branch.repository.pack()
 
1200
        new_names = tree.branch.repository._pack_collection.names()
 
1201
        # The other collection hasn't noticed yet
 
1202
        self.assertEqual(names, packs.names())
 
1203
        self.assertTrue(packs.reload_pack_names())
 
1204
        self.assertEqual(new_names, packs.names())
 
1205
        self.assertEqual({revs[-1]:(revs[-2],)}, r.get_parent_map([revs[-1]]))
 
1206
        self.assertFalse(packs.reload_pack_names())
 
1207
 
 
1208
    def test_autopack_reloads_and_stops(self):
 
1209
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
 
1210
        # After we have determined what needs to be autopacked, trigger a
 
1211
        # full-pack via the other repo which will cause us to re-evaluate and
 
1212
        # decide we don't need to do anything
 
1213
        orig_execute = packs._execute_pack_operations
 
1214
        def _munged_execute_pack_ops(*args, **kwargs):
 
1215
            tree.branch.repository.pack()
 
1216
            return orig_execute(*args, **kwargs)
 
1217
        packs._execute_pack_operations = _munged_execute_pack_ops
 
1218
        packs._max_pack_count = lambda x: 1
 
1219
        packs.pack_distribution = lambda x: [10]
 
1220
        self.assertFalse(packs.autopack())
 
1221
        self.assertEqual(1, len(packs.names()))
 
1222
        self.assertEqual(tree.branch.repository._pack_collection.names(),
 
1223
                         packs.names())
 
1224
 
 
1225
 
 
1226
class TestPack(TestCaseWithTransport):
 
1227
    """Tests for the Pack object."""
 
1228
 
 
1229
    def assertCurrentlyEqual(self, left, right):
 
1230
        self.assertTrue(left == right)
 
1231
        self.assertTrue(right == left)
 
1232
        self.assertFalse(left != right)
 
1233
        self.assertFalse(right != left)
 
1234
 
 
1235
    def assertCurrentlyNotEqual(self, left, right):
 
1236
        self.assertFalse(left == right)
 
1237
        self.assertFalse(right == left)
 
1238
        self.assertTrue(left != right)
 
1239
        self.assertTrue(right != left)
 
1240
 
 
1241
    def test___eq____ne__(self):
 
1242
        left = pack_repo.ExistingPack('', '', '', '', '', '')
 
1243
        right = pack_repo.ExistingPack('', '', '', '', '', '')
 
1244
        self.assertCurrentlyEqual(left, right)
 
1245
        # change all attributes and ensure equality changes as we do.
 
1246
        left.revision_index = 'a'
 
1247
        self.assertCurrentlyNotEqual(left, right)
 
1248
        right.revision_index = 'a'
 
1249
        self.assertCurrentlyEqual(left, right)
 
1250
        left.inventory_index = 'a'
 
1251
        self.assertCurrentlyNotEqual(left, right)
 
1252
        right.inventory_index = 'a'
 
1253
        self.assertCurrentlyEqual(left, right)
 
1254
        left.text_index = 'a'
 
1255
        self.assertCurrentlyNotEqual(left, right)
 
1256
        right.text_index = 'a'
 
1257
        self.assertCurrentlyEqual(left, right)
 
1258
        left.signature_index = 'a'
 
1259
        self.assertCurrentlyNotEqual(left, right)
 
1260
        right.signature_index = 'a'
 
1261
        self.assertCurrentlyEqual(left, right)
 
1262
        left.name = 'a'
 
1263
        self.assertCurrentlyNotEqual(left, right)
 
1264
        right.name = 'a'
 
1265
        self.assertCurrentlyEqual(left, right)
 
1266
        left.transport = 'a'
 
1267
        self.assertCurrentlyNotEqual(left, right)
 
1268
        right.transport = 'a'
 
1269
        self.assertCurrentlyEqual(left, right)
 
1270
 
 
1271
    def test_file_name(self):
 
1272
        pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
 
1273
        self.assertEqual('a_name.pack', pack.file_name())
 
1274
 
 
1275
 
 
1276
class TestNewPack(TestCaseWithTransport):
 
1277
    """Tests for pack_repo.NewPack."""
 
1278
 
 
1279
    def test_new_instance_attributes(self):
 
1280
        upload_transport = self.get_transport('upload')
 
1281
        pack_transport = self.get_transport('pack')
 
1282
        index_transport = self.get_transport('index')
 
1283
        upload_transport.mkdir('.')
 
1284
        collection = pack_repo.RepositoryPackCollection(
 
1285
            repo=None,
 
1286
            transport=self.get_transport('.'),
 
1287
            index_transport=index_transport,
 
1288
            upload_transport=upload_transport,
 
1289
            pack_transport=pack_transport,
 
1290
            index_builder_class=BTreeBuilder,
 
1291
            index_class=BTreeGraphIndex,
 
1292
            use_chk_index=False)
 
1293
        pack = pack_repo.NewPack(collection)
 
1294
        self.assertIsInstance(pack.revision_index, BTreeBuilder)
 
1295
        self.assertIsInstance(pack.inventory_index, BTreeBuilder)
 
1296
        self.assertIsInstance(pack._hash, type(osutils.md5()))
 
1297
        self.assertTrue(pack.upload_transport is upload_transport)
 
1298
        self.assertTrue(pack.index_transport is index_transport)
 
1299
        self.assertTrue(pack.pack_transport is pack_transport)
 
1300
        self.assertEqual(None, pack.index_sizes)
 
1301
        self.assertEqual(20, len(pack.random_name))
 
1302
        self.assertIsInstance(pack.random_name, str)
 
1303
        self.assertIsInstance(pack.start_time, float)
 
1304
 
 
1305
 
 
1306
class TestPacker(TestCaseWithTransport):
 
1307
    """Tests for the packs repository Packer class."""
 
1308
 
 
1309
    def test_pack_optimizes_pack_order(self):
 
1310
        builder = self.make_branch_builder('.')
 
1311
        builder.start_series()
 
1312
        builder.build_snapshot('A', None, [
 
1313
            ('add', ('', 'root-id', 'directory', None)),
 
1314
            ('add', ('f', 'f-id', 'file', 'content\n'))])
 
1315
        builder.build_snapshot('B', ['A'],
 
1316
            [('modify', ('f-id', 'new-content\n'))])
 
1317
        builder.build_snapshot('C', ['B'],
 
1318
            [('modify', ('f-id', 'third-content\n'))])
 
1319
        builder.build_snapshot('D', ['C'],
 
1320
            [('modify', ('f-id', 'fourth-content\n'))])
 
1321
        b = builder.get_branch()
 
1322
        b.lock_read()
 
1323
        builder.finish_series()
 
1324
        self.addCleanup(b.unlock)
 
1325
        # At this point, we should have 4 pack files available
 
1326
        # Because of how they were built, they correspond to
 
1327
        # ['D', 'C', 'B', 'A']
 
1328
        packs = b.repository._pack_collection.packs
 
1329
        packer = pack_repo.Packer(b.repository._pack_collection,
 
1330
                                  packs, 'testing',
 
1331
                                  revision_ids=['B', 'C'])
 
1332
        # Now, when we are copying the B & C revisions, their pack files should
 
1333
        # be moved to the front of the stack
 
1334
        # The new ordering moves B & C to the front of the .packs attribute,
 
1335
        # and leaves the others in the original order.
 
1336
        new_packs = [packs[1], packs[2], packs[0], packs[3]]
 
1337
        new_pack = packer.pack()
 
1338
        self.assertEqual(new_packs, packer.packs)
 
1339
 
 
1340
 
 
1341
class TestOptimisingPacker(TestCaseWithTransport):
 
1342
    """Tests for the OptimisingPacker class."""
 
1343
 
 
1344
    def get_pack_collection(self):
 
1345
        repo = self.make_repository('.')
 
1346
        return repo._pack_collection
 
1347
 
 
1348
    def test_open_pack_will_optimise(self):
 
1349
        packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
 
1350
                                            [], '.test')
 
1351
        new_pack = packer.open_pack()
 
1352
        self.assertIsInstance(new_pack, pack_repo.NewPack)
 
1353
        self.assertTrue(new_pack.revision_index._optimize_for_size)
 
1354
        self.assertTrue(new_pack.inventory_index._optimize_for_size)
 
1355
        self.assertTrue(new_pack.text_index._optimize_for_size)
 
1356
        self.assertTrue(new_pack.signature_index._optimize_for_size)