/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2220.2.2 by Martin Pool
Add tag command and basic implementation
1
# Copyright (C) 2006, 2007 Canonical Ltd
1685.1.63 by Martin Pool
Small Transport fixups
2
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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.
1685.1.63 by Martin Pool
Small Transport fixups
7
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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.
1685.1.63 by Martin Pool
Small Transport fixups
12
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the Repository facility that are not interface tests.
18
19
For interface tests see tests/repository_implementations/*.py.
20
21
For concrete class tests see this file, and for storage formats tests
22
also see this file.
23
"""
24
2592.3.193 by Robert Collins
Move hash tracking of new packs into NewPack.
25
import md5
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
26
from stat import S_ISDIR
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
27
from StringIO import StringIO
28
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
29
import bzrlib
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
30
from bzrlib.errors import (NotBranchError,
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
31
                           NoSuchFile,
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
32
                           UnknownFormatError,
33
                           UnsupportedFormatError,
34
                           )
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
35
from bzrlib import graph
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
36
from bzrlib.index import GraphIndex, InMemoryGraphIndex
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
37
from bzrlib.repository import RepositoryFormat
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
38
from bzrlib.smart import server
2670.3.5 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
39
from bzrlib.tests import (
40
    TestCase,
41
    TestCaseWithTransport,
3446.2.1 by Martin Pool
Failure to delete an obsolete pack file should not be fatal.
42
    TestSkipped,
2670.3.5 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
43
    test_knit,
44
    )
3446.2.1 by Martin Pool
Failure to delete an obsolete pack file should not be fatal.
45
from bzrlib.transport import (
46
    fakenfs,
47
    get_transport,
48
    )
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
49
from bzrlib.transport.memory import MemoryServer
2535.3.53 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
50
from bzrlib.util import bencode
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
51
from bzrlib import (
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
52
    bzrdir,
53
    errors,
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
54
    inventory,
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
55
    progress,
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
56
    repository,
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
57
    revision as _mod_revision,
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
58
    symbol_versioning,
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
59
    upgrade,
60
    workingtree,
61
    )
2592.3.173 by Robert Collins
Basic implementation of all_packs.
62
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
63
64
65
class TestDefaultFormat(TestCase):
66
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
67
    def test_get_set_default_format(self):
2204.5.3 by Aaron Bentley
zap old repository default handling
68
        old_default = bzrdir.format_registry.get('default')
69
        private_default = old_default().repository_format.__class__
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
70
        old_format = repository.RepositoryFormat.get_default_format()
1910.2.33 by Aaron Bentley
Fix default format test
71
        self.assertTrue(isinstance(old_format, private_default))
2204.5.3 by Aaron Bentley
zap old repository default handling
72
        def make_sample_bzrdir():
73
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
74
            my_bzrdir.repository_format = SampleRepositoryFormat()
75
            return my_bzrdir
76
        bzrdir.format_registry.remove('default')
77
        bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
78
        bzrdir.format_registry.set_default('sample')
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
79
        # creating a repository should now create an instrumented dir.
80
        try:
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
81
            # the default branch format is used by the meta dir format
82
            # which is not the default bzrdir format at this point
1685.1.63 by Martin Pool
Small Transport fixups
83
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
84
            result = dir.create_repository()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
85
            self.assertEqual(result, 'A bzr repository dir')
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
86
        finally:
2204.5.3 by Aaron Bentley
zap old repository default handling
87
            bzrdir.format_registry.remove('default')
2363.5.14 by Aaron Bentley
Prevent repository.get_set_default_format from corrupting inventory
88
            bzrdir.format_registry.remove('sample')
2204.5.3 by Aaron Bentley
zap old repository default handling
89
            bzrdir.format_registry.register('default', old_default, '')
90
        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
91
                              old_format.__class__)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
92
93
94
class SampleRepositoryFormat(repository.RepositoryFormat):
95
    """A sample format
96
97
    this format is initializable, unsupported to aid in testing the 
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
98
    open and open(unsupported=True) routines.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
99
    """
100
101
    def get_format_string(self):
102
        """See RepositoryFormat.get_format_string()."""
103
        return "Sample .bzr repository format."
104
1534.6.1 by Robert Collins
allow API creation of shared repositories
105
    def initialize(self, a_bzrdir, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
106
        """Initialize a repository in a BzrDir"""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
107
        t = a_bzrdir.get_repository_transport(self)
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
108
        t.put_bytes('format', self.get_format_string())
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
109
        return 'A bzr repository dir'
110
111
    def is_supported(self):
112
        return False
113
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
114
    def open(self, a_bzrdir, _found=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
115
        return "opened repository."
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
116
117
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
118
class TestRepositoryFormat(TestCaseWithTransport):
119
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
120
121
    def test_find_format(self):
122
        # is the right format object found for a repository?
123
        # create a branch with a few known format objects.
124
        # this is not quite the same as 
125
        self.build_tree(["foo/", "bar/"])
126
        def check_format(format, url):
127
            dir = format._matchingbzrdir.initialize(url)
128
            format.initialize(dir)
129
            t = get_transport(url)
130
            found_format = repository.RepositoryFormat.find_format(dir)
131
            self.failUnless(isinstance(found_format, format.__class__))
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
132
        check_format(weaverepo.RepositoryFormat7(), "bar")
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
133
        
134
    def test_find_format_no_repository(self):
135
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
136
        self.assertRaises(errors.NoRepositoryPresent,
137
                          repository.RepositoryFormat.find_format,
138
                          dir)
139
140
    def test_find_format_unknown_format(self):
141
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
142
        SampleRepositoryFormat().initialize(dir)
143
        self.assertRaises(UnknownFormatError,
144
                          repository.RepositoryFormat.find_format,
145
                          dir)
146
147
    def test_register_unregister_format(self):
148
        format = SampleRepositoryFormat()
149
        # make a control dir
150
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
151
        # make a repo
152
        format.initialize(dir)
153
        # register a format for it.
154
        repository.RepositoryFormat.register_format(format)
155
        # which repository.Open will refuse (not supported)
156
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
157
        # but open(unsupported) will work
158
        self.assertEqual(format.open(dir), "opened repository.")
159
        # unregister the format
160
        repository.RepositoryFormat.unregister_format(format)
161
162
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
163
class TestFormat6(TestCaseWithTransport):
164
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
165
    def test_attribute__fetch_order(self):
166
        """Weaves need topological data insertion."""
167
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
168
        repo = weaverepo.RepositoryFormat6().initialize(control)
169
        self.assertEqual('topological', repo._fetch_order)
170
171
    def test_attribute__fetch_uses_deltas(self):
172
        """Weaves do not reuse deltas."""
173
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
174
        repo = weaverepo.RepositoryFormat6().initialize(control)
175
        self.assertEqual(False, repo._fetch_uses_deltas)
176
3565.3.4 by Robert Collins
Defer decision to reconcile to the repository being fetched into.
177
    def test_attribute__fetch_reconcile(self):
178
        """Weave repositories need a reconcile after fetch."""
179
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
180
        repo = weaverepo.RepositoryFormat6().initialize(control)
181
        self.assertEqual(True, repo._fetch_reconcile)
182
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
183
    def test_no_ancestry_weave(self):
184
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
185
        repo = weaverepo.RepositoryFormat6().initialize(control)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
186
        # We no longer need to create the ancestry.weave file
187
        # since it is *never* used.
188
        self.assertRaises(NoSuchFile,
189
                          control.transport.get,
190
                          'ancestry.weave')
191
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
192
    def test_supports_external_lookups(self):
193
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
194
        repo = weaverepo.RepositoryFormat6().initialize(control)
195
        self.assertFalse(repo._format.supports_external_lookups)
196
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
197
198
class TestFormat7(TestCaseWithTransport):
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
199
200
    def test_attribute__fetch_order(self):
201
        """Weaves need topological data insertion."""
202
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
203
        repo = weaverepo.RepositoryFormat7().initialize(control)
204
        self.assertEqual('topological', repo._fetch_order)
205
206
    def test_attribute__fetch_uses_deltas(self):
207
        """Weaves do not reuse deltas."""
208
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
209
        repo = weaverepo.RepositoryFormat7().initialize(control)
210
        self.assertEqual(False, repo._fetch_uses_deltas)
211
3565.3.4 by Robert Collins
Defer decision to reconcile to the repository being fetched into.
212
    def test_attribute__fetch_reconcile(self):
213
        """Weave repositories need a reconcile after fetch."""
214
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
215
        repo = weaverepo.RepositoryFormat7().initialize(control)
216
        self.assertEqual(True, repo._fetch_reconcile)
217
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
218
    def test_disk_layout(self):
219
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
220
        repo = weaverepo.RepositoryFormat7().initialize(control)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
221
        # in case of side effects of locking.
222
        repo.lock_write()
223
        repo.unlock()
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
224
        # we want:
225
        # format 'Bazaar-NG Repository format 7'
226
        # lock ''
227
        # inventory.weave == empty_weave
228
        # empty revision-store directory
229
        # empty weaves directory
230
        t = control.get_repository_transport(None)
231
        self.assertEqualDiff('Bazaar-NG Repository format 7',
232
                             t.get('format').read())
233
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
234
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
235
        self.assertEqualDiff('# bzr weave file v5\n'
236
                             'w\n'
237
                             'W\n',
238
                             t.get('inventory.weave').read())
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
239
        # Creating a file with id Foo:Bar results in a non-escaped file name on
240
        # disk.
241
        control.create_branch()
242
        tree = control.create_workingtree()
243
        tree.add(['foo'], ['Foo:Bar'], ['file'])
244
        tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
245
        tree.commit('first post', rev_id='first')
246
        self.assertEqualDiff(
247
            '# bzr weave file v5\n'
248
            'i\n'
249
            '1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
250
            'n first\n'
251
            '\n'
252
            'w\n'
253
            '{ 0\n'
254
            '. content\n'
255
            '}\n'
256
            'W\n',
257
            t.get('weaves/74/Foo%3ABar.weave').read())
1534.6.1 by Robert Collins
allow API creation of shared repositories
258
259
    def test_shared_disk_layout(self):
260
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
261
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1534.6.1 by Robert Collins
allow API creation of shared repositories
262
        # we want:
263
        # format 'Bazaar-NG Repository format 7'
264
        # inventory.weave == empty_weave
265
        # empty revision-store directory
266
        # empty weaves directory
267
        # a 'shared-storage' marker file.
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
268
        # lock is not present when unlocked
1534.6.1 by Robert Collins
allow API creation of shared repositories
269
        t = control.get_repository_transport(None)
270
        self.assertEqualDiff('Bazaar-NG Repository format 7',
271
                             t.get('format').read())
272
        self.assertEqualDiff('', t.get('shared-storage').read())
273
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
274
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
275
        self.assertEqualDiff('# bzr weave file v5\n'
276
                             'w\n'
277
                             'W\n',
278
                             t.get('inventory.weave').read())
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
279
        self.assertFalse(t.has('branch-lock'))
280
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
281
    def test_creates_lockdir(self):
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
282
        """Make sure it appears to be controlled by a LockDir existence"""
283
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
284
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
285
        t = control.get_repository_transport(None)
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
286
        # TODO: Should check there is a 'lock' toplevel directory, 
287
        # regardless of contents
288
        self.assertFalse(t.has('lock/held/info'))
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
289
        repo.lock_write()
1658.1.4 by Martin Pool
Quieten warning from TestFormat7.test_creates_lockdir about failing to unlock
290
        try:
291
            self.assertTrue(t.has('lock/held/info'))
292
        finally:
293
            # unlock so we don't get a warning about failing to do so
294
            repo.unlock()
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
295
296
    def test_uses_lockdir(self):
297
        """repo format 7 actually locks on lockdir"""
298
        base_url = self.get_url()
299
        control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
300
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
301
        t = control.get_repository_transport(None)
302
        repo.lock_write()
303
        repo.unlock()
304
        del repo
305
        # make sure the same lock is created by opening it
306
        repo = repository.Repository.open(base_url)
307
        repo.lock_write()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
308
        self.assertTrue(t.has('lock/held/info'))
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
309
        repo.unlock()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
310
        self.assertFalse(t.has('lock/held/info'))
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
311
312
    def test_shared_no_tree_disk_layout(self):
313
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
314
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
315
        repo.set_make_working_trees(False)
316
        # we want:
317
        # format 'Bazaar-NG Repository format 7'
318
        # lock ''
319
        # inventory.weave == empty_weave
320
        # empty revision-store directory
321
        # empty weaves directory
322
        # a 'shared-storage' marker file.
323
        t = control.get_repository_transport(None)
324
        self.assertEqualDiff('Bazaar-NG Repository format 7',
325
                             t.get('format').read())
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
326
        ## self.assertEqualDiff('', t.get('lock').read())
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
327
        self.assertEqualDiff('', t.get('shared-storage').read())
328
        self.assertEqualDiff('', t.get('no-working-trees').read())
329
        repo.set_make_working_trees(True)
330
        self.assertFalse(t.has('no-working-trees'))
331
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
332
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
333
        self.assertEqualDiff('# bzr weave file v5\n'
334
                             'w\n'
335
                             'W\n',
336
                             t.get('inventory.weave').read())
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
337
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
338
    def test_supports_external_lookups(self):
339
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
340
        repo = weaverepo.RepositoryFormat7().initialize(control)
341
        self.assertFalse(repo._format.supports_external_lookups)
342
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
343
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
344
class TestFormatKnit1(TestCaseWithTransport):
345
    
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
346
    def test_attribute__fetch_order(self):
347
        """Knits need topological data insertion."""
348
        repo = self.make_repository('.',
349
                format=bzrdir.format_registry.get('knit')())
350
        self.assertEqual('topological', repo._fetch_order)
351
352
    def test_attribute__fetch_uses_deltas(self):
353
        """Knits reuse deltas."""
354
        repo = self.make_repository('.',
355
                format=bzrdir.format_registry.get('knit')())
356
        self.assertEqual(True, repo._fetch_uses_deltas)
357
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
358
    def test_disk_layout(self):
359
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
360
        repo = knitrepo.RepositoryFormatKnit1().initialize(control)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
361
        # in case of side effects of locking.
362
        repo.lock_write()
363
        repo.unlock()
364
        # we want:
365
        # format 'Bazaar-NG Knit Repository Format 1'
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
366
        # lock: is a directory
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
367
        # inventory.weave == empty_weave
368
        # empty revision-store directory
369
        # empty weaves directory
370
        t = control.get_repository_transport(None)
371
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
372
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
373
        # XXX: no locks left when unlocked at the moment
374
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
375
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
376
        self.check_knits(t)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
377
        # Check per-file knits.
378
        branch = control.create_branch()
379
        tree = control.create_workingtree()
380
        tree.add(['foo'], ['Nasty-IdC:'], ['file'])
381
        tree.put_file_bytes_non_atomic('Nasty-IdC:', '')
382
        tree.commit('1st post', rev_id='foo')
383
        self.assertHasKnit(t, 'knits/e8/%254easty-%2549d%2543%253a',
384
            '\nfoo fulltext 0 81  :')
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
385
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
386
    def assertHasKnit(self, t, knit_name, extra_content=''):
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
387
        """Assert that knit_name exists on t."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
388
        self.assertEqualDiff('# bzr knit index 8\n' + extra_content,
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
389
                             t.get(knit_name + '.kndx').read())
390
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
391
    def check_knits(self, t):
392
        """check knit content for a repository."""
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
393
        self.assertHasKnit(t, 'inventory')
394
        self.assertHasKnit(t, 'revisions')
395
        self.assertHasKnit(t, 'signatures')
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
396
397
    def test_shared_disk_layout(self):
398
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
399
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
400
        # we want:
401
        # format 'Bazaar-NG Knit Repository Format 1'
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
402
        # lock: is a directory
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
403
        # inventory.weave == empty_weave
404
        # empty revision-store directory
405
        # empty weaves directory
406
        # a 'shared-storage' marker file.
407
        t = control.get_repository_transport(None)
408
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
409
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
410
        # XXX: no locks left when unlocked at the moment
411
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
412
        self.assertEqualDiff('', t.get('shared-storage').read())
413
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
414
        self.check_knits(t)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
415
416
    def test_shared_no_tree_disk_layout(self):
417
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
418
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
419
        repo.set_make_working_trees(False)
420
        # we want:
421
        # format 'Bazaar-NG Knit Repository Format 1'
422
        # lock ''
423
        # inventory.weave == empty_weave
424
        # empty revision-store directory
425
        # empty weaves directory
426
        # a 'shared-storage' marker file.
427
        t = control.get_repository_transport(None)
428
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
429
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
430
        # XXX: no locks left when unlocked at the moment
431
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
432
        self.assertEqualDiff('', t.get('shared-storage').read())
433
        self.assertEqualDiff('', t.get('no-working-trees').read())
434
        repo.set_make_working_trees(True)
435
        self.assertFalse(t.has('no-working-trees'))
436
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
437
        self.check_knits(t)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
438
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
439
    def test_deserialise_sets_root_revision(self):
440
        """We must have a inventory.root.revision
441
442
        Old versions of the XML5 serializer did not set the revision_id for
443
        the whole inventory. So we grab the one from the expected text. Which
444
        is valid when the api is not being abused.
445
        """
446
        repo = self.make_repository('.',
447
                format=bzrdir.format_registry.get('knit')())
448
        inv_xml = '<inventory format="5">\n</inventory>\n'
449
        inv = repo.deserialise_inventory('test-rev-id', inv_xml)
450
        self.assertEqual('test-rev-id', inv.root.revision)
451
452
    def test_deserialise_uses_global_revision_id(self):
453
        """If it is set, then we re-use the global revision id"""
454
        repo = self.make_repository('.',
455
                format=bzrdir.format_registry.get('knit')())
456
        inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
457
                   '</inventory>\n')
458
        # Arguably, the deserialise_inventory should detect a mismatch, and
459
        # raise an error, rather than silently using one revision_id over the
460
        # other.
3169.2.2 by Robert Collins
Add a test to Repository.deserialise_inventory that the resulting ivnentory is the one asked for, and update relevant tests. Also tweak the model 1 to 2 regenerate inventories logic to use the revision trees parent marker which is more accurate in some cases.
461
        self.assertRaises(AssertionError, repo.deserialise_inventory,
462
            'test-rev-id', inv_xml)
463
        inv = repo.deserialise_inventory('other-rev-id', inv_xml)
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
464
        self.assertEqual('other-rev-id', inv.root.revision)
465
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
466
    def test_supports_external_lookups(self):
467
        repo = self.make_repository('.',
468
                format=bzrdir.format_registry.get('knit')())
469
        self.assertFalse(repo._format.supports_external_lookups)
470
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
471
472
class DummyRepository(object):
473
    """A dummy repository for testing."""
474
475
    _serializer = None
476
477
    def supports_rich_root(self):
478
        return False
479
480
481
class InterDummy(repository.InterRepository):
482
    """An inter-repository optimised code path for DummyRepository.
483
484
    This is for use during testing where we use DummyRepository as repositories
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
485
    so that none of the default regsitered inter-repository classes will
2818.4.2 by Robert Collins
Review feedback.
486
    MATCH.
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
487
    """
488
489
    @staticmethod
490
    def is_compatible(repo_source, repo_target):
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
491
        """InterDummy is compatible with DummyRepository."""
492
        return (isinstance(repo_source, DummyRepository) and 
493
            isinstance(repo_target, DummyRepository))
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
494
495
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
496
class TestInterRepository(TestCaseWithTransport):
497
498
    def test_get_default_inter_repository(self):
499
        # test that the InterRepository.get(repo_a, repo_b) probes
500
        # for a inter_repo class where is_compatible(repo_a, repo_b) returns
501
        # true and returns a default inter_repo otherwise.
502
        # This also tests that the default registered optimised interrepository
503
        # classes do not barf inappropriately when a surprising repository type
504
        # is handed to them.
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
505
        dummy_a = DummyRepository()
506
        dummy_b = DummyRepository()
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
507
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
508
509
    def assertGetsDefaultInterRepository(self, repo_a, repo_b):
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
510
        """Asserts that InterRepository.get(repo_a, repo_b) -> the default.
511
        
512
        The effective default is now InterSameDataRepository because there is
513
        no actual sane default in the presence of incompatible data models.
514
        """
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
515
        inter_repo = repository.InterRepository.get(repo_a, repo_b)
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
516
        self.assertEqual(repository.InterSameDataRepository,
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
517
                         inter_repo.__class__)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
518
        self.assertEqual(repo_a, inter_repo.source)
519
        self.assertEqual(repo_b, inter_repo.target)
520
521
    def test_register_inter_repository_class(self):
522
        # test that a optimised code path provider - a
523
        # InterRepository subclass can be registered and unregistered
524
        # and that it is correctly selected when given a repository
525
        # pair that it returns true on for the is_compatible static method
526
        # check
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
527
        dummy_a = DummyRepository()
528
        dummy_b = DummyRepository()
529
        repo = self.make_repository('.')
530
        # hack dummies to look like repo somewhat.
531
        dummy_a._serializer = repo._serializer
532
        dummy_b._serializer = repo._serializer
533
        repository.InterRepository.register_optimiser(InterDummy)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
534
        try:
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
535
            # we should get the default for something InterDummy returns False
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
536
            # to
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
537
            self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
538
            self.assertGetsDefaultInterRepository(dummy_a, repo)
539
            # and we should get an InterDummy for a pair it 'likes'
540
            self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
541
            inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
542
            self.assertEqual(InterDummy, inter_repo.__class__)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
543
            self.assertEqual(dummy_a, inter_repo.source)
544
            self.assertEqual(dummy_b, inter_repo.target)
545
        finally:
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
546
            repository.InterRepository.unregister_optimiser(InterDummy)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
547
        # now we should get the default InterRepository object again.
548
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
549
2241.1.17 by Martin Pool
Restore old InterWeave tests
550
551
class TestInterWeaveRepo(TestCaseWithTransport):
552
553
    def test_is_compatible_and_registered(self):
554
        # InterWeaveRepo is compatible when either side
555
        # is a format 5/6/7 branch
2241.1.20 by mbp at sourcefrog
update tests for new locations of weave repos
556
        from bzrlib.repofmt import knitrepo, weaverepo
557
        formats = [weaverepo.RepositoryFormat5(),
558
                   weaverepo.RepositoryFormat6(),
559
                   weaverepo.RepositoryFormat7()]
560
        incompatible_formats = [weaverepo.RepositoryFormat4(),
561
                                knitrepo.RepositoryFormatKnit1(),
2241.1.17 by Martin Pool
Restore old InterWeave tests
562
                                ]
563
        repo_a = self.make_repository('a')
564
        repo_b = self.make_repository('b')
565
        is_compatible = repository.InterWeaveRepo.is_compatible
566
        for source in incompatible_formats:
567
            # force incompatible left then right
568
            repo_a._format = source
569
            repo_b._format = formats[0]
570
            self.assertFalse(is_compatible(repo_a, repo_b))
571
            self.assertFalse(is_compatible(repo_b, repo_a))
572
        for source in formats:
573
            repo_a._format = source
574
            for target in formats:
575
                repo_b._format = target
576
                self.assertTrue(is_compatible(repo_a, repo_b))
577
        self.assertEqual(repository.InterWeaveRepo,
578
                         repository.InterRepository.get(repo_a,
579
                                                        repo_b).__class__)
580
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
581
582
class TestRepositoryConverter(TestCaseWithTransport):
583
584
    def test_convert_empty(self):
585
        t = get_transport(self.get_url('.'))
586
        t.mkdir('repository')
587
        repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
588
        repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
589
        target_format = knitrepo.RepositoryFormatKnit1()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
590
        converter = repository.CopyConverter(target_format)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
591
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
592
        try:
593
            converter.convert(repo, pb)
594
        finally:
595
            pb.finished()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
596
        repo = repo_dir.open_repository()
597
        self.assertTrue(isinstance(target_format, repo._format.__class__))
1843.2.5 by Aaron Bentley
Add test of _unescape_xml
598
599
600
class TestMisc(TestCase):
601
    
602
    def test_unescape_xml(self):
603
        """We get some kind of error when malformed entities are passed"""
604
        self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;') 
1910.2.13 by Aaron Bentley
Start work on converter
605
606
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
607
class TestRepositoryFormatKnit3(TestCaseWithTransport):
1910.2.13 by Aaron Bentley
Start work on converter
608
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
609
    def test_attribute__fetch_order(self):
610
        """Knits need topological data insertion."""
611
        format = bzrdir.BzrDirMetaFormat1()
612
        format.repository_format = knitrepo.RepositoryFormatKnit3()
613
        repo = self.make_repository('.', format=format)
614
        self.assertEqual('topological', repo._fetch_order)
615
616
    def test_attribute__fetch_uses_deltas(self):
617
        """Knits reuse deltas."""
618
        format = bzrdir.BzrDirMetaFormat1()
619
        format.repository_format = knitrepo.RepositoryFormatKnit3()
620
        repo = self.make_repository('.', format=format)
621
        self.assertEqual(True, repo._fetch_uses_deltas)
622
1910.2.13 by Aaron Bentley
Start work on converter
623
    def test_convert(self):
624
        """Ensure the upgrade adds weaves for roots"""
1910.2.35 by Aaron Bentley
Better fix for convesion test
625
        format = bzrdir.BzrDirMetaFormat1()
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
626
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1910.2.35 by Aaron Bentley
Better fix for convesion test
627
        tree = self.make_branch_and_tree('.', format)
1910.2.13 by Aaron Bentley
Start work on converter
628
        tree.commit("Dull commit", rev_id="dull")
629
        revision_tree = tree.branch.repository.revision_tree('dull')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
630
        revision_tree.lock_read()
631
        try:
632
            self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
633
                revision_tree.inventory.root.file_id)
634
        finally:
635
            revision_tree.unlock()
1910.2.13 by Aaron Bentley
Start work on converter
636
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
637
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.13 by Aaron Bentley
Start work on converter
638
        upgrade.Convert('.', format)
1910.2.27 by Aaron Bentley
Fixed conversion test
639
        tree = workingtree.WorkingTree.open('.')
1910.2.13 by Aaron Bentley
Start work on converter
640
        revision_tree = tree.branch.repository.revision_tree('dull')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
641
        revision_tree.lock_read()
642
        try:
643
            revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
644
        finally:
645
            revision_tree.unlock()
1910.2.27 by Aaron Bentley
Fixed conversion test
646
        tree.commit("Another dull commit", rev_id='dull2')
647
        revision_tree = tree.branch.repository.revision_tree('dull2')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
648
        revision_tree.lock_read()
649
        self.addCleanup(revision_tree.unlock)
1910.2.27 by Aaron Bentley
Fixed conversion test
650
        self.assertEqual('dull', revision_tree.inventory.root.revision)
2220.2.2 by Martin Pool
Add tag command and basic implementation
651
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
652
    def test_supports_external_lookups(self):
653
        format = bzrdir.BzrDirMetaFormat1()
654
        format.repository_format = knitrepo.RepositoryFormatKnit3()
655
        repo = self.make_repository('.', format=format)
656
        self.assertFalse(repo._format.supports_external_lookups)
657
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
658
659
class TestWithBrokenRepo(TestCaseWithTransport):
2592.3.214 by Robert Collins
Merge bzr.dev.
660
    """These tests seem to be more appropriate as interface tests?"""
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
661
662
    def make_broken_repository(self):
663
        # XXX: This function is borrowed from Aaron's "Reconcile can fix bad
664
        # parent references" branch which is due to land in bzr.dev soon.  Once
665
        # it does, this duplication should be removed.
666
        repo = self.make_repository('broken-repo')
667
        cleanups = []
668
        try:
669
            repo.lock_write()
670
            cleanups.append(repo.unlock)
671
            repo.start_write_group()
672
            cleanups.append(repo.commit_write_group)
673
            # make rev1a: A well-formed revision, containing 'file1'
674
            inv = inventory.Inventory(revision_id='rev1a')
675
            inv.root.revision = 'rev1a'
676
            self.add_file(repo, inv, 'file1', 'rev1a', [])
677
            repo.add_inventory('rev1a', inv, [])
678
            revision = _mod_revision.Revision('rev1a',
679
                committer='jrandom@example.com', timestamp=0,
680
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
681
            repo.add_revision('rev1a',revision, inv)
682
683
            # make rev1b, which has no Revision, but has an Inventory, and
684
            # file1
685
            inv = inventory.Inventory(revision_id='rev1b')
686
            inv.root.revision = 'rev1b'
687
            self.add_file(repo, inv, 'file1', 'rev1b', [])
688
            repo.add_inventory('rev1b', inv, [])
689
690
            # make rev2, with file1 and file2
691
            # file2 is sane
692
            # file1 has 'rev1b' as an ancestor, even though this is not
693
            # mentioned by 'rev1a', making it an unreferenced ancestor
694
            inv = inventory.Inventory()
695
            self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
696
            self.add_file(repo, inv, 'file2', 'rev2', [])
697
            self.add_revision(repo, 'rev2', inv, ['rev1a'])
698
699
            # make ghost revision rev1c
700
            inv = inventory.Inventory()
701
            self.add_file(repo, inv, 'file2', 'rev1c', [])
702
703
            # make rev3 with file2
704
            # file2 refers to 'rev1c', which is a ghost in this repository, so
705
            # file2 cannot have rev1c as its ancestor.
706
            inv = inventory.Inventory()
707
            self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
708
            self.add_revision(repo, 'rev3', inv, ['rev1c'])
709
            return repo
710
        finally:
711
            for cleanup in reversed(cleanups):
712
                cleanup()
713
714
    def add_revision(self, repo, revision_id, inv, parent_ids):
715
        inv.revision_id = revision_id
716
        inv.root.revision = revision_id
717
        repo.add_inventory(revision_id, inv, parent_ids)
718
        revision = _mod_revision.Revision(revision_id,
719
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
720
            timezone=0, message='foo', parent_ids=parent_ids)
721
        repo.add_revision(revision_id,revision, inv)
722
723
    def add_file(self, repo, inv, filename, revision, parents):
724
        file_id = filename + '-id'
725
        entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
726
        entry.revision = revision
2535.4.10 by Andrew Bennetts
Fix one failing test, disable another.
727
        entry.text_size = 0
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
728
        inv.add(entry)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
729
        text_key = (file_id, revision)
730
        parent_keys = [(file_id, parent) for parent in parents]
731
        repo.texts.add_lines(text_key, parent_keys, ['line\n'])
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
732
733
    def test_insert_from_broken_repo(self):
734
        """Inserting a data stream from a broken repository won't silently
735
        corrupt the target repository.
736
        """
737
        broken_repo = self.make_broken_repository()
738
        empty_repo = self.make_repository('empty-repo')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
739
        self.assertRaises(errors.RevisionNotPresent, empty_repo.fetch, broken_repo)
2592.3.214 by Robert Collins
Merge bzr.dev.
740
741
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
742
class TestKnitPackNoSubtrees(TestCaseWithTransport):
2592.3.24 by Robert Collins
Knit1 disk layout specified.
743
744
    def get_format(self):
3010.3.3 by Martin Pool
Merge trunk
745
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
2592.3.24 by Robert Collins
Knit1 disk layout specified.
746
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
747
    def test_attribute__fetch_order(self):
748
        """Packs do not need ordered data retrieval."""
749
        format = self.get_format()
750
        repo = self.make_repository('.', format=format)
751
        self.assertEqual('unsorted', repo._fetch_order)
752
753
    def test_attribute__fetch_uses_deltas(self):
754
        """Packs reuse deltas."""
755
        format = self.get_format()
756
        repo = self.make_repository('.', format=format)
757
        self.assertEqual(True, repo._fetch_uses_deltas)
758
2592.3.24 by Robert Collins
Knit1 disk layout specified.
759
    def test_disk_layout(self):
760
        format = self.get_format()
2592.3.36 by Robert Collins
Change the revision index name to NAME.rix.
761
        repo = self.make_repository('.', format=format)
2592.3.24 by Robert Collins
Knit1 disk layout specified.
762
        # in case of side effects of locking.
763
        repo.lock_write()
764
        repo.unlock()
765
        t = repo.bzrdir.get_repository_transport(None)
766
        self.check_format(t)
767
        # XXX: no locks left when unlocked at the moment
768
        # self.assertEqualDiff('', t.get('lock').read())
769
        self.check_databases(t)
770
771
    def check_format(self, t):
2939.2.5 by Ian Clatworthy
review feedback from lifeless
772
        self.assertEqualDiff(
2939.2.7 by Ian Clatworthy
fix strings used in on-disk unit tests
773
            "Bazaar pack repository format 1 (needs bzr 0.92)\n",
2592.3.24 by Robert Collins
Knit1 disk layout specified.
774
                             t.get('format').read())
775
776
    def assertHasNoKndx(self, t, knit_name):
777
        """Assert that knit_name has no index on t."""
778
        self.assertFalse(t.has(knit_name + '.kndx'))
779
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
780
    def assertHasNoKnit(self, t, knit_name):
2592.3.24 by Robert Collins
Knit1 disk layout specified.
781
        """Assert that knit_name exists on t."""
782
        # no default content
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
783
        self.assertFalse(t.has(knit_name + '.knit'))
2592.3.24 by Robert Collins
Knit1 disk layout specified.
784
785
    def check_databases(self, t):
786
        """check knit content for a repository."""
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
787
        # check conversion worked
2592.3.61 by Robert Collins
Remove inventory.kndx.
788
        self.assertHasNoKndx(t, 'inventory')
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
789
        self.assertHasNoKnit(t, 'inventory')
2592.3.24 by Robert Collins
Knit1 disk layout specified.
790
        self.assertHasNoKndx(t, 'revisions')
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
791
        self.assertHasNoKnit(t, 'revisions')
2592.3.39 by Robert Collins
Fugly version to remove signatures.kndx
792
        self.assertHasNoKndx(t, 'signatures')
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
793
        self.assertHasNoKnit(t, 'signatures')
794
        self.assertFalse(t.has('knits'))
2592.3.24 by Robert Collins
Knit1 disk layout specified.
795
        # revision-indexes file-container directory
2592.3.83 by Robert Collins
Merge bzr.dev, dropping FileNames for a trivial GraphIndex layer.
796
        self.assertEqual([],
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
797
            list(GraphIndex(t, 'pack-names', None).iter_all_entries()))
2592.3.81 by Robert Collins
Merge FileName allocation change, leading to hash based pack file names.
798
        self.assertTrue(S_ISDIR(t.stat('packs').st_mode))
799
        self.assertTrue(S_ISDIR(t.stat('upload').st_mode))
2592.3.219 by Robert Collins
Review feedback.
800
        self.assertTrue(S_ISDIR(t.stat('indices').st_mode))
801
        self.assertTrue(S_ISDIR(t.stat('obsolete_packs').st_mode))
2592.3.24 by Robert Collins
Knit1 disk layout specified.
802
803
    def test_shared_disk_layout(self):
804
        format = self.get_format()
2592.3.36 by Robert Collins
Change the revision index name to NAME.rix.
805
        repo = self.make_repository('.', shared=True, format=format)
2592.3.24 by Robert Collins
Knit1 disk layout specified.
806
        # we want:
807
        t = repo.bzrdir.get_repository_transport(None)
808
        self.check_format(t)
809
        # XXX: no locks left when unlocked at the moment
810
        # self.assertEqualDiff('', t.get('lock').read())
2592.3.219 by Robert Collins
Review feedback.
811
        # We should have a 'shared-storage' marker file.
2592.3.24 by Robert Collins
Knit1 disk layout specified.
812
        self.assertEqualDiff('', t.get('shared-storage').read())
813
        self.check_databases(t)
814
815
    def test_shared_no_tree_disk_layout(self):
816
        format = self.get_format()
2592.3.36 by Robert Collins
Change the revision index name to NAME.rix.
817
        repo = self.make_repository('.', shared=True, format=format)
2592.3.24 by Robert Collins
Knit1 disk layout specified.
818
        repo.set_make_working_trees(False)
819
        # we want:
820
        t = repo.bzrdir.get_repository_transport(None)
821
        self.check_format(t)
822
        # XXX: no locks left when unlocked at the moment
823
        # self.assertEqualDiff('', t.get('lock').read())
2592.3.219 by Robert Collins
Review feedback.
824
        # We should have a 'shared-storage' marker file.
2592.3.24 by Robert Collins
Knit1 disk layout specified.
825
        self.assertEqualDiff('', t.get('shared-storage').read())
2592.3.219 by Robert Collins
Review feedback.
826
        # We should have a marker for the no-working-trees flag.
2592.3.24 by Robert Collins
Knit1 disk layout specified.
827
        self.assertEqualDiff('', t.get('no-working-trees').read())
2592.3.219 by Robert Collins
Review feedback.
828
        # The marker should go when we toggle the setting.
2592.3.24 by Robert Collins
Knit1 disk layout specified.
829
        repo.set_make_working_trees(True)
830
        self.assertFalse(t.has('no-working-trees'))
831
        self.check_databases(t)
2592.3.25 by Robert Collins
experimental-subtrees layout defined.
832
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
833
    def test_adding_revision_creates_pack_indices(self):
2592.3.118 by Robert Collins
Record the size of the index files in the pack-names index.
834
        format = self.get_format()
835
        tree = self.make_branch_and_tree('.', format=format)
836
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
837
        self.assertEqual([],
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
838
            list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
2592.3.118 by Robert Collins
Record the size of the index files in the pack-names index.
839
        tree.commit('foobarbaz')
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
840
        index = GraphIndex(trans, 'pack-names', None)
2592.3.219 by Robert Collins
Review feedback.
841
        index_nodes = list(index.iter_all_entries())
842
        self.assertEqual(1, len(index_nodes))
843
        node = index_nodes[0]
2592.3.118 by Robert Collins
Record the size of the index files in the pack-names index.
844
        name = node[1][0]
845
        # the pack sizes should be listed in the index
846
        pack_value = node[2]
847
        sizes = [int(digits) for digits in pack_value.split(' ')]
848
        for size, suffix in zip(sizes, ['.rix', '.iix', '.tix', '.six']):
849
            stat = trans.stat('indices/%s%s' % (name, suffix))
850
            self.assertEqual(size, stat.st_size)
2592.3.60 by Robert Collins
Nuke per-fileid indices for a single unified index.
851
2592.3.52 by Robert Collins
Stop allocating new names unless new data has been inserted.
852
    def test_pulling_nothing_leads_to_no_new_names(self):
853
        format = self.get_format()
854
        tree1 = self.make_branch_and_tree('1', format=format)
855
        tree2 = self.make_branch_and_tree('2', format=format)
856
        tree1.branch.repository.fetch(tree2.branch.repository)
857
        trans = tree1.branch.repository.bzrdir.get_repository_transport(None)
2592.3.83 by Robert Collins
Merge bzr.dev, dropping FileNames for a trivial GraphIndex layer.
858
        self.assertEqual([],
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
859
            list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
2592.3.39 by Robert Collins
Fugly version to remove signatures.kndx
860
2592.3.84 by Robert Collins
Start of autopacking logic.
861
    def test_commit_across_pack_shape_boundary_autopacks(self):
862
        format = self.get_format()
863
        tree = self.make_branch_and_tree('.', format=format)
864
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
865
        # This test could be a little cheaper by replacing the packs
866
        # attribute on the repository to allow a different pack distribution
2592.3.219 by Robert Collins
Review feedback.
867
        # and max packs policy - so we are checking the policy is honoured
2592.3.84 by Robert Collins
Start of autopacking logic.
868
        # in the test. But for now 11 commits is not a big deal in a single
869
        # test.
870
        for x in range(9):
871
            tree.commit('commit %s' % x)
872
        # there should be 9 packs:
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
873
        index = GraphIndex(trans, 'pack-names', None)
2592.3.84 by Robert Collins
Start of autopacking logic.
874
        self.assertEqual(9, len(list(index.iter_all_entries())))
2948.1.1 by Robert Collins
* Obsolete packs are now cleaned up by pack and autopack operations.
875
        # insert some files in obsolete_packs which should be removed by pack.
876
        trans.put_bytes('obsolete_packs/foo', '123')
877
        trans.put_bytes('obsolete_packs/bar', '321')
2592.3.84 by Robert Collins
Start of autopacking logic.
878
        # committing one more should coalesce to 1 of 10.
879
        tree.commit('commit triggering pack')
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
880
        index = GraphIndex(trans, 'pack-names', None)
2592.3.84 by Robert Collins
Start of autopacking logic.
881
        self.assertEqual(1, len(list(index.iter_all_entries())))
882
        # packing should not damage data
883
        tree = tree.bzrdir.open_workingtree()
884
        check_result = tree.branch.repository.check(
885
            [tree.branch.last_revision()])
2948.1.1 by Robert Collins
* Obsolete packs are now cleaned up by pack and autopack operations.
886
        # We should have 50 (10x5) files in the obsolete_packs directory.
887
        obsolete_files = list(trans.list_dir('obsolete_packs'))
888
        self.assertFalse('foo' in obsolete_files)
889
        self.assertFalse('bar' in obsolete_files)
890
        self.assertEqual(50, len(obsolete_files))
2592.3.84 by Robert Collins
Start of autopacking logic.
891
        # XXX: Todo check packs obsoleted correctly - old packs and indices
892
        # in the obsolete_packs directory.
893
        large_pack_name = list(index.iter_all_entries())[0][1][0]
894
        # finally, committing again should not touch the large pack.
895
        tree.commit('commit not triggering pack')
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
896
        index = GraphIndex(trans, 'pack-names', None)
2592.3.84 by Robert Collins
Start of autopacking logic.
897
        self.assertEqual(2, len(list(index.iter_all_entries())))
898
        pack_names = [node[1][0] for node in index.iter_all_entries()]
899
        self.assertTrue(large_pack_name in pack_names)
900
3446.2.1 by Martin Pool
Failure to delete an obsolete pack file should not be fatal.
901
    def test_fail_obsolete_deletion(self):
902
        # failing to delete obsolete packs is not fatal
903
        format = self.get_format()
904
        server = fakenfs.FakeNFSServer()
905
        server.setUp()
906
        self.addCleanup(server.tearDown)
907
        transport = get_transport(server.get_url())
908
        bzrdir = self.get_format().initialize_on_transport(transport)
909
        repo = bzrdir.create_repository()
910
        repo_transport = bzrdir.get_repository_transport(None)
911
        self.assertTrue(repo_transport.has('obsolete_packs'))
912
        # these files are in use by another client and typically can't be deleted
913
        repo_transport.put_bytes('obsolete_packs/.nfsblahblah', 'contents')
914
        repo._pack_collection._clear_obsolete_packs()
915
        self.assertTrue(repo_transport.has('obsolete_packs/.nfsblahblah'))
916
2592.3.86 by Robert Collins
Implement the pack commands for knit repositories.
917
    def test_pack_after_two_commits_packs_everything(self):
918
        format = self.get_format()
919
        tree = self.make_branch_and_tree('.', format=format)
920
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
921
        tree.commit('start')
922
        tree.commit('more work')
923
        tree.branch.repository.pack()
2592.3.219 by Robert Collins
Review feedback.
924
        # there should be 1 pack:
2592.3.175 by Robert Collins
Update for GraphIndex constructor changes.
925
        index = GraphIndex(trans, 'pack-names', None)
2592.3.86 by Robert Collins
Implement the pack commands for knit repositories.
926
        self.assertEqual(1, len(list(index.iter_all_entries())))
927
        self.assertEqual(2, len(tree.branch.repository.all_revision_ids()))
928
3070.1.1 by Robert Collins
* ``bzr pack`` now orders revision texts in topological order, with newest
929
    def test_pack_layout(self):
930
        format = self.get_format()
931
        tree = self.make_branch_and_tree('.', format=format)
932
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
933
        tree.commit('start', rev_id='1')
934
        tree.commit('more work', rev_id='2')
935
        tree.branch.repository.pack()
936
        tree.lock_read()
937
        self.addCleanup(tree.unlock)
938
        pack = tree.branch.repository._pack_collection.get_pack_by_name(
939
            tree.branch.repository._pack_collection.names()[0])
940
        # revision access tends to be tip->ancestor, so ordering that way on 
941
        # disk is a good idea.
942
        for _1, key, val, refs in pack.revision_index.iter_all_entries():
943
            if key == ('1',):
944
                pos_1 = int(val[1:].split()[0])
945
            else:
946
                pos_2 = int(val[1:].split()[0])
947
        self.assertTrue(pos_2 < pos_1)
948
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
949
    def test_pack_repositories_support_multiple_write_locks(self):
950
        format = self.get_format()
951
        self.make_repository('.', shared=True, format=format)
952
        r1 = repository.Repository.open('.')
953
        r2 = repository.Repository.open('.')
954
        r1.lock_write()
955
        self.addCleanup(r1.unlock)
956
        r2.lock_write()
957
        r2.unlock()
958
959
    def _add_text(self, repo, fileid):
960
        """Add a text to the repository within a write group."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
961
        repo.texts.add_lines((fileid, 'samplerev+'+fileid), [], [])
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
962
963
    def test_concurrent_writers_merge_new_packs(self):
964
        format = self.get_format()
965
        self.make_repository('.', shared=True, format=format)
966
        r1 = repository.Repository.open('.')
967
        r2 = repository.Repository.open('.')
968
        r1.lock_write()
969
        try:
970
            # access enough data to load the names list
971
            list(r1.all_revision_ids())
972
            r2.lock_write()
973
            try:
974
                # access enough data to load the names list
975
                list(r2.all_revision_ids())
976
                r1.start_write_group()
977
                try:
978
                    r2.start_write_group()
979
                    try:
980
                        self._add_text(r1, 'fileidr1')
981
                        self._add_text(r2, 'fileidr2')
982
                    except:
983
                        r2.abort_write_group()
984
                        raise
985
                except:
986
                    r1.abort_write_group()
987
                    raise
988
                # both r1 and r2 have open write groups with data in them
989
                # created while the other's write group was open.
990
                # Commit both which requires a merge to the pack-names.
991
                try:
992
                    r1.commit_write_group()
993
                except:
2592.3.219 by Robert Collins
Review feedback.
994
                    r1.abort_write_group()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
995
                    r2.abort_write_group()
996
                    raise
997
                r2.commit_write_group()
2592.3.213 by Robert Collins
Retain packs and indices in memory within a lock, even when write groups are entered and exited.
998
                # tell r1 to reload from disk
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
999
                r1._pack_collection.reset()
2592.3.219 by Robert Collins
Review feedback.
1000
                # Now both repositories should know about both names
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1001
                r1._pack_collection.ensure_loaded()
1002
                r2._pack_collection.ensure_loaded()
1003
                self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
1004
                self.assertEqual(2, len(r1._pack_collection.names()))
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1005
            finally:
2939.1.1 by Martin Pool
Fix up mismatched lock/unlock pairs.
1006
                r2.unlock()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1007
        finally:
2939.1.1 by Martin Pool
Fix up mismatched lock/unlock pairs.
1008
            r1.unlock()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1009
1010
    def test_concurrent_writer_second_preserves_dropping_a_pack(self):
1011
        format = self.get_format()
1012
        self.make_repository('.', shared=True, format=format)
1013
        r1 = repository.Repository.open('.')
1014
        r2 = repository.Repository.open('.')
1015
        # add a pack to drop
1016
        r1.lock_write()
1017
        try:
1018
            r1.start_write_group()
1019
            try:
1020
                self._add_text(r1, 'fileidr1')
1021
            except:
1022
                r1.abort_write_group()
1023
                raise
1024
            else:
1025
                r1.commit_write_group()
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1026
            r1._pack_collection.ensure_loaded()
1027
            name_to_drop = r1._pack_collection.all_packs()[0].name
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1028
        finally:
1029
            r1.unlock()
1030
        r1.lock_write()
1031
        try:
1032
            # access enough data to load the names list
1033
            list(r1.all_revision_ids())
1034
            r2.lock_write()
1035
            try:
1036
                # access enough data to load the names list
1037
                list(r2.all_revision_ids())
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1038
                r1._pack_collection.ensure_loaded()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1039
                try:
1040
                    r2.start_write_group()
1041
                    try:
1042
                        # in r1, drop the pack
2592.3.236 by Martin Pool
Make RepositoryPackCollection.remove_pack_from_memory private
1043
                        r1._pack_collection._remove_pack_from_memory(
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1044
                            r1._pack_collection.get_pack_by_name(name_to_drop))
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1045
                        # in r2, add a pack
1046
                        self._add_text(r2, 'fileidr2')
1047
                    except:
1048
                        r2.abort_write_group()
1049
                        raise
1050
                except:
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1051
                    r1._pack_collection.reset()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1052
                    raise
1053
                # r1 has a changed names list, and r2 an open write groups with
1054
                # changes.
2592.3.208 by Robert Collins
Start refactoring the knit-pack thunking to be clearer.
1055
                # save r1, and then commit the r2 write group, which requires a
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1056
                # merge to the pack-names, which should not reinstate
1057
                # name_to_drop
1058
                try:
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1059
                    r1._pack_collection._save_pack_names()
1060
                    r1._pack_collection.reset()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1061
                except:
1062
                    r2.abort_write_group()
1063
                    raise
1064
                try:
1065
                    r2.commit_write_group()
1066
                except:
1067
                    r2.abort_write_group()
1068
                    raise
1069
                # Now both repositories should now about just one name.
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1070
                r1._pack_collection.ensure_loaded()
1071
                r2._pack_collection.ensure_loaded()
1072
                self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
1073
                self.assertEqual(1, len(r1._pack_collection.names()))
1074
                self.assertFalse(name_to_drop in r1._pack_collection.names())
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1075
            finally:
2939.1.1 by Martin Pool
Fix up mismatched lock/unlock pairs.
1076
                r2.unlock()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1077
        finally:
2939.1.1 by Martin Pool
Fix up mismatched lock/unlock pairs.
1078
            r1.unlock()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1079
1080
    def test_lock_write_does_not_physically_lock(self):
1081
        repo = self.make_repository('.', format=self.get_format())
1082
        repo.lock_write()
1083
        self.addCleanup(repo.unlock)
1084
        self.assertFalse(repo.get_physical_lock_status())
1085
1086
    def prepare_for_break_lock(self):
2592.3.219 by Robert Collins
Review feedback.
1087
        # Setup the global ui factory state so that a break-lock method call
1088
        # will find usable input in the input stream.
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1089
        old_factory = bzrlib.ui.ui_factory
1090
        def restoreFactory():
1091
            bzrlib.ui.ui_factory = old_factory
1092
        self.addCleanup(restoreFactory)
1093
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1094
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1095
1096
    def test_break_lock_breaks_physical_lock(self):
1097
        repo = self.make_repository('.', format=self.get_format())
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1098
        repo._pack_collection.lock_names()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1099
        repo2 = repository.Repository.open('.')
1100
        self.assertTrue(repo.get_physical_lock_status())
1101
        self.prepare_for_break_lock()
1102
        repo2.break_lock()
1103
        self.assertFalse(repo.get_physical_lock_status())
1104
2592.3.240 by Martin Pool
Rename RepositoryPackCollection.release_names to _unlock_names
1105
    def test_broken_physical_locks_error_on__unlock_names_lock(self):
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1106
        repo = self.make_repository('.', format=self.get_format())
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1107
        repo._pack_collection.lock_names()
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1108
        self.assertTrue(repo.get_physical_lock_status())
1109
        repo2 = repository.Repository.open('.')
1110
        self.prepare_for_break_lock()
1111
        repo2.break_lock()
2592.3.240 by Martin Pool
Rename RepositoryPackCollection.release_names to _unlock_names
1112
        self.assertRaises(errors.LockBroken, repo._pack_collection._unlock_names)
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1113
2949.1.2 by Robert Collins
* Fetch with pack repositories will no longer read the entire history graph.
1114
    def test_fetch_without_find_ghosts_ignores_ghosts(self):
1115
        # we want two repositories at this point:
1116
        # one with a revision that is a ghost in the other
1117
        # repository.
1118
        # 'ghost' is present in has_ghost, 'ghost' is absent in 'missing_ghost'.
1119
        # 'references' is present in both repositories, and 'tip' is present
1120
        # just in has_ghost.
1121
        # has_ghost       missing_ghost
1122
        #------------------------------
1123
        # 'ghost'             -
1124
        # 'references'    'references'
1125
        # 'tip'               -
1126
        # In this test we fetch 'tip' which should not fetch 'ghost'
1127
        has_ghost = self.make_repository('has_ghost', format=self.get_format())
1128
        missing_ghost = self.make_repository('missing_ghost',
1129
            format=self.get_format())
1130
1131
        def add_commit(repo, revision_id, parent_ids):
1132
            repo.lock_write()
1133
            repo.start_write_group()
1134
            inv = inventory.Inventory(revision_id=revision_id)
1135
            inv.root.revision = revision_id
1136
            root_id = inv.root.file_id
1137
            sha1 = repo.add_inventory(revision_id, inv, [])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1138
            repo.texts.add_lines((root_id, revision_id), [], [])
2949.1.2 by Robert Collins
* Fetch with pack repositories will no longer read the entire history graph.
1139
            rev = bzrlib.revision.Revision(timestamp=0,
1140
                                           timezone=None,
1141
                                           committer="Foo Bar <foo@example.com>",
1142
                                           message="Message",
1143
                                           inventory_sha1=sha1,
1144
                                           revision_id=revision_id)
1145
            rev.parent_ids = parent_ids
1146
            repo.add_revision(revision_id, rev)
1147
            repo.commit_write_group()
1148
            repo.unlock()
1149
        add_commit(has_ghost, 'ghost', [])
1150
        add_commit(has_ghost, 'references', ['ghost'])
1151
        add_commit(missing_ghost, 'references', ['ghost'])
1152
        add_commit(has_ghost, 'tip', ['references'])
1153
        missing_ghost.fetch(has_ghost, 'tip')
1154
        # missing ghost now has tip and not ghost.
1155
        rev = missing_ghost.get_revision('tip')
1156
        inv = missing_ghost.get_inventory('tip')
1157
        self.assertRaises(errors.NoSuchRevision,
1158
            missing_ghost.get_revision, 'ghost')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1159
        self.assertRaises(errors.NoSuchRevision,
2949.1.2 by Robert Collins
* Fetch with pack repositories will no longer read the entire history graph.
1160
            missing_ghost.get_inventory, 'ghost')
1161
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
1162
    def test_supports_external_lookups(self):
1163
        repo = self.make_repository('.', format=self.get_format())
1164
        self.assertFalse(repo._format.supports_external_lookups)
1165
2592.3.188 by Robert Collins
Allow pack repositories to have multiple writers active at one time, for greater concurrency.
1166
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
1167
class TestKnitPackSubtrees(TestKnitPackNoSubtrees):
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1168
1169
    def get_format(self):
2939.2.5 by Ian Clatworthy
review feedback from lifeless
1170
        return bzrdir.format_registry.make_bzrdir(
3010.3.3 by Martin Pool
Merge trunk
1171
            'pack-0.92-subtree')
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1172
1173
    def check_format(self, t):
2939.2.5 by Ian Clatworthy
review feedback from lifeless
1174
        self.assertEqualDiff(
2939.2.7 by Ian Clatworthy
fix strings used in on-disk unit tests
1175
            "Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n",
2939.2.5 by Ian Clatworthy
review feedback from lifeless
1176
            t.get('format').read())
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1177
1178
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1179
class TestDevelopment0(TestKnitPackNoSubtrees):
1180
1181
    def get_format(self):
1182
        return bzrdir.format_registry.make_bzrdir(
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1183
            'development0')
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1184
1185
    def check_format(self, t):
1186
        self.assertEqualDiff(
3152.2.3 by Robert Collins
Merge up with bzr.dev.
1187
            "Bazaar development format 0 (needs bzr.dev from before 1.3)\n",
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1188
            t.get('format').read())
1189
1190
1191
class TestDevelopment0Subtree(TestKnitPackNoSubtrees):
1192
1193
    def get_format(self):
1194
        return bzrdir.format_registry.make_bzrdir(
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1195
            'development0-subtree')
1196
1197
    def check_format(self, t):
1198
        self.assertEqualDiff(
1199
            "Bazaar development format 0 with subtree support "
1200
            "(needs bzr.dev from before 1.3)\n",
1201
            t.get('format').read())
1202
1203
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1204
class TestExternalDevelopment1(object):
1205
3517.4.16 by Martin Pool
More precise test for stack format compatibility
1206
    # mixin class for testing stack-supporting development formats
1207
3582.1.7 by Martin Pool
add_fallback_repository gives more detail on incompatibilities
1208
    def test_stack_checks_compatibility(self):
3517.4.16 by Martin Pool
More precise test for stack format compatibility
1209
        # early versions of the packing code relied on pack internals to
1210
        # stack, but the current version should be able to stack on any
1211
        # format.
3582.1.7 by Martin Pool
add_fallback_repository gives more detail on incompatibilities
1212
        #
1213
        # TODO: Possibly this should be run per-repository-format and raise
1214
        # TestNotApplicable on formats that don't support stacking. -- mbp
1215
        # 20080729
3517.4.16 by Martin Pool
More precise test for stack format compatibility
1216
        repo = self.make_repository('repo', format=self.get_format())
1217
        if repo.supports_rich_root():
1218
            # can only stack on repositories that have compatible internal
1219
            # metadata
1220
            matching_format_name = 'pack-0.92-subtree'
1221
            mismatching_format_name = 'pack-0.92'
1222
        else:
1223
            matching_format_name = 'pack-0.92'
1224
            mismatching_format_name = 'pack-0.92-subtree'
1225
        base = self.make_repository('base', format=matching_format_name)
1226
        repo.add_fallback_repository(base)
1227
        # you can't stack on something with incompatible data
1228
        bad_repo = self.make_repository('mismatch',
1229
            format=mismatching_format_name)
3582.1.7 by Martin Pool
add_fallback_repository gives more detail on incompatibilities
1230
        e = self.assertRaises(errors.IncompatibleRepositories,
3517.4.16 by Martin Pool
More precise test for stack format compatibility
1231
            repo.add_fallback_repository, bad_repo)
3582.1.7 by Martin Pool
add_fallback_repository gives more detail on incompatibilities
1232
        self.assertContainsRe(str(e),
1233
            r'(?m)KnitPackRepository.*/mismatch/.*\nis not compatible with\n'
1234
            r'KnitPackRepository.*/repo/.*\n'
1235
            r'different rich-root support')
3517.4.16 by Martin Pool
More precise test for stack format compatibility
1236
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1237
    def test_adding_pack_does_not_record_pack_names_from_other_repositories(self):
1238
        base = self.make_branch_and_tree('base', format=self.get_format())
1239
        base.commit('foo')
1240
        referencing = self.make_branch_and_tree('repo', format=self.get_format())
1241
        referencing.branch.repository.add_fallback_repository(base.branch.repository)
1242
        referencing.commit('bar')
1243
        new_instance = referencing.bzrdir.open_repository()
1244
        new_instance.lock_read()
1245
        self.addCleanup(new_instance.unlock)
1246
        new_instance._pack_collection.ensure_loaded()
1247
        self.assertEqual(1, len(new_instance._pack_collection.all_packs()))
1248
1249
    def test_autopack_only_considers_main_repo_packs(self):
1250
        base = self.make_branch_and_tree('base', format=self.get_format())
1251
        base.commit('foo')
1252
        tree = self.make_branch_and_tree('repo', format=self.get_format())
1253
        tree.branch.repository.add_fallback_repository(base.branch.repository)
1254
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
1255
        # This test could be a little cheaper by replacing the packs
1256
        # attribute on the repository to allow a different pack distribution
1257
        # and max packs policy - so we are checking the policy is honoured
1258
        # in the test. But for now 11 commits is not a big deal in a single
1259
        # test.
1260
        for x in range(9):
1261
            tree.commit('commit %s' % x)
1262
        # there should be 9 packs:
1263
        index = GraphIndex(trans, 'pack-names', None)
1264
        self.assertEqual(9, len(list(index.iter_all_entries())))
1265
        # committing one more should coalesce to 1 of 10.
1266
        tree.commit('commit triggering pack')
1267
        index = GraphIndex(trans, 'pack-names', None)
1268
        self.assertEqual(1, len(list(index.iter_all_entries())))
1269
        # packing should not damage data
1270
        tree = tree.bzrdir.open_workingtree()
1271
        check_result = tree.branch.repository.check(
1272
            [tree.branch.last_revision()])
1273
        # We should have 50 (10x5) files in the obsolete_packs directory.
1274
        obsolete_files = list(trans.list_dir('obsolete_packs'))
1275
        self.assertFalse('foo' in obsolete_files)
1276
        self.assertFalse('bar' in obsolete_files)
1277
        self.assertEqual(50, len(obsolete_files))
1278
        # XXX: Todo check packs obsoleted correctly - old packs and indices
1279
        # in the obsolete_packs directory.
1280
        large_pack_name = list(index.iter_all_entries())[0][1][0]
1281
        # finally, committing again should not touch the large pack.
1282
        tree.commit('commit not triggering pack')
1283
        index = GraphIndex(trans, 'pack-names', None)
1284
        self.assertEqual(2, len(list(index.iter_all_entries())))
1285
        pack_names = [node[1][0] for node in index.iter_all_entries()]
1286
        self.assertTrue(large_pack_name in pack_names)
1287
1288
1289
class TestDevelopment1(TestKnitPackNoSubtrees, TestExternalDevelopment1):
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1290
1291
    def get_format(self):
1292
        return bzrdir.format_registry.make_bzrdir(
1293
            'development')
1294
1295
    def check_format(self, t):
1296
        self.assertEqualDiff(
3221.17.1 by Ian Clatworthy
tweak version numbers being released in
1297
            "Bazaar development format 1 (needs bzr.dev from before 1.6)\n",
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1298
            t.get('format').read())
1299
1300
    def test_supports_external_lookups(self):
1301
        repo = self.make_repository('.', format=self.get_format())
1302
        self.assertTrue(repo._format.supports_external_lookups)
1303
1304
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1305
class TestDevelopment1Subtree(TestKnitPackNoSubtrees, TestExternalDevelopment1):
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1306
1307
    def get_format(self):
1308
        return bzrdir.format_registry.make_bzrdir(
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1309
            'development-subtree')
1310
1311
    def check_format(self, t):
1312
        self.assertEqualDiff(
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1313
            "Bazaar development format 1 with subtree support "
3221.17.1 by Ian Clatworthy
tweak version numbers being released in
1314
            "(needs bzr.dev from before 1.6)\n",
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1315
            t.get('format').read())
1316
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
1317
    def test_supports_external_lookups(self):
1318
        repo = self.make_repository('.', format=self.get_format())
1319
        self.assertTrue(repo._format.supports_external_lookups)
1320
3152.2.1 by Robert Collins
* A new repository format 'development' has been added. This format will
1321
2592.3.84 by Robert Collins
Start of autopacking logic.
1322
class TestRepositoryPackCollection(TestCaseWithTransport):
1323
1324
    def get_format(self):
3010.3.3 by Martin Pool
Merge trunk
1325
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
2592.3.84 by Robert Collins
Start of autopacking logic.
1326
1327
    def test__max_pack_count(self):
2592.3.219 by Robert Collins
Review feedback.
1328
        """The maximum pack count is a function of the number of revisions."""
2592.3.84 by Robert Collins
Start of autopacking logic.
1329
        format = self.get_format()
1330
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1331
        packs = repo._pack_collection
2592.3.84 by Robert Collins
Start of autopacking logic.
1332
        # no revisions - one pack, so that we can have a revision free repo
1333
        # without it blowing up
1334
        self.assertEqual(1, packs._max_pack_count(0))
1335
        # after that the sum of the digits, - check the first 1-9
1336
        self.assertEqual(1, packs._max_pack_count(1))
1337
        self.assertEqual(2, packs._max_pack_count(2))
1338
        self.assertEqual(3, packs._max_pack_count(3))
1339
        self.assertEqual(4, packs._max_pack_count(4))
1340
        self.assertEqual(5, packs._max_pack_count(5))
1341
        self.assertEqual(6, packs._max_pack_count(6))
1342
        self.assertEqual(7, packs._max_pack_count(7))
1343
        self.assertEqual(8, packs._max_pack_count(8))
1344
        self.assertEqual(9, packs._max_pack_count(9))
1345
        # check the boundary cases with two digits for the next decade
1346
        self.assertEqual(1, packs._max_pack_count(10))
1347
        self.assertEqual(2, packs._max_pack_count(11))
1348
        self.assertEqual(10, packs._max_pack_count(19))
1349
        self.assertEqual(2, packs._max_pack_count(20))
1350
        self.assertEqual(3, packs._max_pack_count(21))
1351
        # check some arbitrary big numbers
1352
        self.assertEqual(25, packs._max_pack_count(112894))
1353
1354
    def test_pack_distribution_zero(self):
1355
        format = self.get_format()
1356
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1357
        packs = repo._pack_collection
2592.3.84 by Robert Collins
Start of autopacking logic.
1358
        self.assertEqual([0], packs.pack_distribution(0))
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
1359
1360
    def test_ensure_loaded_unlocked(self):
1361
        format = self.get_format()
1362
        repo = self.make_repository('.', format=format)
1363
        self.assertRaises(errors.ObjectNotLocked,
1364
                          repo._pack_collection.ensure_loaded)
1365
2592.3.84 by Robert Collins
Start of autopacking logic.
1366
    def test_pack_distribution_one_to_nine(self):
1367
        format = self.get_format()
1368
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1369
        packs = repo._pack_collection
2592.3.84 by Robert Collins
Start of autopacking logic.
1370
        self.assertEqual([1],
1371
            packs.pack_distribution(1))
1372
        self.assertEqual([1, 1],
1373
            packs.pack_distribution(2))
1374
        self.assertEqual([1, 1, 1],
1375
            packs.pack_distribution(3))
1376
        self.assertEqual([1, 1, 1, 1],
1377
            packs.pack_distribution(4))
1378
        self.assertEqual([1, 1, 1, 1, 1],
1379
            packs.pack_distribution(5))
1380
        self.assertEqual([1, 1, 1, 1, 1, 1],
1381
            packs.pack_distribution(6))
1382
        self.assertEqual([1, 1, 1, 1, 1, 1, 1],
1383
            packs.pack_distribution(7))
1384
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
1385
            packs.pack_distribution(8))
1386
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
1387
            packs.pack_distribution(9))
1388
1389
    def test_pack_distribution_stable_at_boundaries(self):
1390
        """When there are multi-rev packs the counts are stable."""
1391
        format = self.get_format()
1392
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1393
        packs = repo._pack_collection
2592.3.84 by Robert Collins
Start of autopacking logic.
1394
        # in 10s:
1395
        self.assertEqual([10], packs.pack_distribution(10))
1396
        self.assertEqual([10, 1], packs.pack_distribution(11))
1397
        self.assertEqual([10, 10], packs.pack_distribution(20))
1398
        self.assertEqual([10, 10, 1], packs.pack_distribution(21))
1399
        # 100s
1400
        self.assertEqual([100], packs.pack_distribution(100))
1401
        self.assertEqual([100, 1], packs.pack_distribution(101))
1402
        self.assertEqual([100, 10, 1], packs.pack_distribution(111))
1403
        self.assertEqual([100, 100], packs.pack_distribution(200))
1404
        self.assertEqual([100, 100, 1], packs.pack_distribution(201))
1405
        self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
1406
2592.3.85 by Robert Collins
Finish autopack corner cases.
1407
    def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
1408
        format = self.get_format()
1409
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1410
        packs = repo._pack_collection
2592.3.85 by Robert Collins
Finish autopack corner cases.
1411
        existing_packs = [(2000, "big"), (9, "medium")]
1412
        # rev count - 2009 -> 2x1000 + 9x1
1413
        pack_operations = packs.plan_autopack_combinations(
1414
            existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1415
        self.assertEqual([], pack_operations)
1416
1417
    def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
1418
        format = self.get_format()
1419
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1420
        packs = repo._pack_collection
2592.3.85 by Robert Collins
Finish autopack corner cases.
1421
        existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
1422
        # rev count - 2010 -> 2x1000 + 1x10
1423
        pack_operations = packs.plan_autopack_combinations(
1424
            existing_packs, [1000, 1000, 10])
1425
        self.assertEqual([], pack_operations)
1426
1427
    def test_plan_pack_operations_2010_combines_smallest_two(self):
1428
        format = self.get_format()
1429
        repo = self.make_repository('.', format=format)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1430
        packs = repo._pack_collection
2592.3.85 by Robert Collins
Finish autopack corner cases.
1431
        existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
1432
            (1, "single1")]
1433
        # rev count - 2010 -> 2x1000 + 1x10 (3)
1434
        pack_operations = packs.plan_autopack_combinations(
1435
            existing_packs, [1000, 1000, 10])
1436
        self.assertEqual([[2, ["single2", "single1"]], [0, []]], pack_operations)
1437
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1438
    def test_all_packs_none(self):
1439
        format = self.get_format()
1440
        tree = self.make_branch_and_tree('.', format=format)
1441
        tree.lock_read()
1442
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1443
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1444
        packs.ensure_loaded()
1445
        self.assertEqual([], packs.all_packs())
1446
1447
    def test_all_packs_one(self):
1448
        format = self.get_format()
1449
        tree = self.make_branch_and_tree('.', format=format)
1450
        tree.commit('start')
1451
        tree.lock_read()
1452
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1453
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1454
        packs.ensure_loaded()
2592.3.176 by Robert Collins
Various pack refactorings.
1455
        self.assertEqual([
1456
            packs.get_pack_by_name(packs.names()[0])],
1457
            packs.all_packs())
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1458
1459
    def test_all_packs_two(self):
1460
        format = self.get_format()
1461
        tree = self.make_branch_and_tree('.', format=format)
1462
        tree.commit('start')
1463
        tree.commit('continue')
1464
        tree.lock_read()
1465
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1466
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1467
        packs.ensure_loaded()
1468
        self.assertEqual([
2592.3.176 by Robert Collins
Various pack refactorings.
1469
            packs.get_pack_by_name(packs.names()[0]),
1470
            packs.get_pack_by_name(packs.names()[1]),
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1471
            ], packs.all_packs())
1472
2592.3.176 by Robert Collins
Various pack refactorings.
1473
    def test_get_pack_by_name(self):
1474
        format = self.get_format()
1475
        tree = self.make_branch_and_tree('.', format=format)
1476
        tree.commit('start')
1477
        tree.lock_read()
1478
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1479
        packs = tree.branch.repository._pack_collection
2592.3.176 by Robert Collins
Various pack refactorings.
1480
        packs.ensure_loaded()
1481
        name = packs.names()[0]
1482
        pack_1 = packs.get_pack_by_name(name)
1483
        # the pack should be correctly initialised
3517.4.5 by Martin Pool
Correct use of packs._names in test_get_pack_by_name
1484
        sizes = packs._names[name]
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1485
        rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
1486
        inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
1487
        txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
1488
        sig_index = GraphIndex(packs._index_transport, name + '.six', sizes[3])
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1489
        self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
2592.3.219 by Robert Collins
Review feedback.
1490
            name, rev_index, inv_index, txt_index, sig_index), pack_1)
2592.3.176 by Robert Collins
Various pack refactorings.
1491
        # and the same instance should be returned on successive calls.
1492
        self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1493
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1494
1495
class TestPack(TestCaseWithTransport):
1496
    """Tests for the Pack object."""
1497
1498
    def assertCurrentlyEqual(self, left, right):
1499
        self.assertTrue(left == right)
1500
        self.assertTrue(right == left)
1501
        self.assertFalse(left != right)
1502
        self.assertFalse(right != left)
1503
1504
    def assertCurrentlyNotEqual(self, left, right):
1505
        self.assertFalse(left == right)
1506
        self.assertFalse(right == left)
1507
        self.assertTrue(left != right)
1508
        self.assertTrue(right != left)
1509
1510
    def test___eq____ne__(self):
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1511
        left = pack_repo.ExistingPack('', '', '', '', '', '')
1512
        right = pack_repo.ExistingPack('', '', '', '', '', '')
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1513
        self.assertCurrentlyEqual(left, right)
1514
        # change all attributes and ensure equality changes as we do.
1515
        left.revision_index = 'a'
1516
        self.assertCurrentlyNotEqual(left, right)
1517
        right.revision_index = 'a'
1518
        self.assertCurrentlyEqual(left, right)
1519
        left.inventory_index = 'a'
1520
        self.assertCurrentlyNotEqual(left, right)
1521
        right.inventory_index = 'a'
1522
        self.assertCurrentlyEqual(left, right)
1523
        left.text_index = 'a'
1524
        self.assertCurrentlyNotEqual(left, right)
1525
        right.text_index = 'a'
1526
        self.assertCurrentlyEqual(left, right)
1527
        left.signature_index = 'a'
1528
        self.assertCurrentlyNotEqual(left, right)
1529
        right.signature_index = 'a'
1530
        self.assertCurrentlyEqual(left, right)
1531
        left.name = 'a'
1532
        self.assertCurrentlyNotEqual(left, right)
1533
        right.name = 'a'
1534
        self.assertCurrentlyEqual(left, right)
1535
        left.transport = 'a'
1536
        self.assertCurrentlyNotEqual(left, right)
1537
        right.transport = 'a'
1538
        self.assertCurrentlyEqual(left, right)
2592.3.179 by Robert Collins
Generate the revision_index_map for packing during the core operation, from the pack objects.
1539
1540
    def test_file_name(self):
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1541
        pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
2592.3.179 by Robert Collins
Generate the revision_index_map for packing during the core operation, from the pack objects.
1542
        self.assertEqual('a_name.pack', pack.file_name())
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
1543
1544
1545
class TestNewPack(TestCaseWithTransport):
1546
    """Tests for pack_repo.NewPack."""
1547
2592.3.193 by Robert Collins
Move hash tracking of new packs into NewPack.
1548
    def test_new_instance_attributes(self):
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1549
        upload_transport = self.get_transport('upload')
1550
        pack_transport = self.get_transport('pack')
1551
        index_transport = self.get_transport('index')
1552
        upload_transport.mkdir('.')
1553
        pack = pack_repo.NewPack(upload_transport, index_transport,
1554
            pack_transport)
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
1555
        self.assertIsInstance(pack.revision_index, InMemoryGraphIndex)
2592.3.195 by Robert Collins
Move some inventory index logic to NewPack.
1556
        self.assertIsInstance(pack.inventory_index, InMemoryGraphIndex)
2592.3.193 by Robert Collins
Move hash tracking of new packs into NewPack.
1557
        self.assertIsInstance(pack._hash, type(md5.new()))
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1558
        self.assertTrue(pack.upload_transport is upload_transport)
1559
        self.assertTrue(pack.index_transport is index_transport)
1560
        self.assertTrue(pack.pack_transport is pack_transport)
1561
        self.assertEqual(None, pack.index_sizes)
1562
        self.assertEqual(20, len(pack.random_name))
1563
        self.assertIsInstance(pack.random_name, str)
1564
        self.assertIsInstance(pack.start_time, float)
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
1565
1566
1567
class TestPacker(TestCaseWithTransport):
1568
    """Tests for the packs repository Packer class."""
2951.1.10 by Robert Collins
Peer review feedback with Ian.
1569
1570
    # To date, this class has been factored out and nothing new added to it;
1571
    # thus there are not yet any tests.
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
1572
1573
1574
class TestInterDifferingSerializer(TestCaseWithTransport):
1575
1576
    def test_progress_bar(self):
1577
        tree = self.make_branch_and_tree('tree')
1578
        tree.commit('rev1', rev_id='rev-1')
1579
        tree.commit('rev2', rev_id='rev-2')
1580
        tree.commit('rev3', rev_id='rev-3')
1581
        repo = self.make_repository('repo')
1582
        inter_repo = repository.InterDifferingSerializer(
1583
            tree.branch.repository, repo)
1584
        pb = progress.InstrumentedProgress(to_file=StringIO())
1585
        pb.never_throttle = True
1586
        inter_repo.fetch('rev-1', pb)
1587
        self.assertEqual('Transferring revisions', pb.last_msg)
1588
        self.assertEqual(1, pb.last_cnt)
1589
        self.assertEqual(1, pb.last_total)
1590
        inter_repo.fetch('rev-3', pb)
1591
        self.assertEqual(2, pb.last_cnt)
1592
        self.assertEqual(2, pb.last_total)