~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/knitrepo.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-06-05 04:05:05 UTC
  • mfrom: (3473.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080605040505-i9kqxg2fps2qjdi0
Add the 'alias' command (Tim Penhey)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from bzrlib.lazy_import import lazy_import
18
18
lazy_import(globals(), """
19
19
from bzrlib import (
 
20
    debug,
 
21
    )
 
22
from bzrlib.store import revision
 
23
from bzrlib.store.revision.knit import KnitRevisionStore
 
24
""")
 
25
from bzrlib import (
20
26
    bzrdir,
21
27
    errors,
22
 
    knit as _mod_knit,
 
28
    knit,
23
29
    lockable_files,
24
30
    lockdir,
25
31
    osutils,
26
 
    revision as _mod_revision,
 
32
    symbol_versioning,
27
33
    transactions,
28
 
    versionedfile,
29
34
    xml5,
30
35
    xml6,
31
36
    xml7,
32
37
    )
33
 
""")
34
 
from bzrlib import (
35
 
    symbol_versioning,
36
 
    )
 
38
 
37
39
from bzrlib.decorators import needs_read_lock, needs_write_lock
38
40
from bzrlib.repository import (
39
41
    CommitBuilder,
42
44
    RepositoryFormat,
43
45
    RootCommitBuilder,
44
46
    )
 
47
import bzrlib.revision as _mod_revision
 
48
from bzrlib.store.versioned import VersionedFileStore
45
49
from bzrlib.trace import mutter, mutter_callsite
 
50
from bzrlib.util import bencode
46
51
 
47
52
 
48
53
class _KnitParentsProvider(object):
80
85
        return parent_map
81
86
 
82
87
 
83
 
class _KnitsParentsProvider(object):
84
 
 
85
 
    def __init__(self, knit, prefix=()):
86
 
        """Create a parent provider for string keys mapped to tuple keys."""
87
 
        self._knit = knit
88
 
        self._prefix = prefix
89
 
 
90
 
    def __repr__(self):
91
 
        return 'KnitsParentsProvider(%r)' % self._knit
92
 
 
93
 
    def get_parent_map(self, keys):
94
 
        """See graph._StackedParentsProvider.get_parent_map"""
95
 
        parent_map = self._knit.get_parent_map(
96
 
            [self._prefix + (key,) for key in keys])
97
 
        result = {}
98
 
        for key, parents in parent_map.items():
99
 
            revid = key[-1]
100
 
            if len(parents) == 0:
101
 
                parents = (_mod_revision.NULL_REVISION,)
102
 
            else:
103
 
                parents = tuple(parent[-1] for parent in parents)
104
 
            result[revid] = parents
105
 
        for revision_id in keys:
106
 
            if revision_id == _mod_revision.NULL_REVISION:
107
 
                result[revision_id] = ()
108
 
        return result
109
 
 
110
 
 
111
88
class KnitRepository(MetaDirRepository):
112
89
    """Knit format repository."""
113
90
 
118
95
    _commit_builder_class = None
119
96
    _serializer = None
120
97
 
121
 
    def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
122
 
        _serializer):
123
 
        MetaDirRepository.__init__(self, _format, a_bzrdir, control_files)
 
98
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
99
        control_store, text_store, _commit_builder_class, _serializer):
 
100
        MetaDirRepository.__init__(self, _format, a_bzrdir, control_files,
 
101
            _revision_store, control_store, text_store)
124
102
        self._commit_builder_class = _commit_builder_class
125
103
        self._serializer = _serializer
126
104
        self._reconcile_fixes_text_parents = True
127
 
        self._fetch_uses_deltas = True
128
 
        self._fetch_order = 'topological'
 
105
        control_store.get_scope = self.get_transaction
 
106
        text_store.get_scope = self.get_transaction
 
107
        _revision_store.get_scope = self.get_transaction
 
108
 
 
109
    def _warn_if_deprecated(self):
 
110
        # This class isn't deprecated
 
111
        pass
 
112
 
 
113
    def _inventory_add_lines(self, inv_vf, revid, parents, lines, check_content):
 
114
        return inv_vf.add_lines_with_ghosts(revid, parents, lines,
 
115
            check_content=check_content)[0]
129
116
 
130
117
    @needs_read_lock
131
118
    def _all_revision_ids(self):
132
119
        """See Repository.all_revision_ids()."""
133
 
        return [key[0] for key in self.revisions.keys()]
134
 
 
135
 
    def _activate_new_inventory(self):
136
 
        """Put a replacement inventory.new into use as inventories."""
137
 
        # Copy the content across
138
 
        t = self._transport
139
 
        t.copy('inventory.new.kndx', 'inventory.kndx')
140
 
        try:
141
 
            t.copy('inventory.new.knit', 'inventory.knit')
142
 
        except errors.NoSuchFile:
143
 
            # empty inventories knit
144
 
            t.delete('inventory.knit')
145
 
        # delete the temp inventory
146
 
        t.delete('inventory.new.kndx')
147
 
        try:
148
 
            t.delete('inventory.new.knit')
149
 
        except errors.NoSuchFile:
150
 
            # empty inventories knit
151
 
            pass
152
 
        # Force index reload (sanity check)
153
 
        self.inventories._index._reset_cache()
154
 
        self.inventories.keys()
155
 
 
156
 
    def _backup_inventory(self):
157
 
        t = self._transport
158
 
        t.copy('inventory.kndx', 'inventory.backup.kndx')
159
 
        t.copy('inventory.knit', 'inventory.backup.knit')
160
 
 
161
 
    def _move_file_id(self, from_id, to_id):
162
 
        t = self._transport.clone('knits')
163
 
        from_rel_url = self.texts._index._mapper.map((from_id, None))
164
 
        to_rel_url = self.texts._index._mapper.map((to_id, None))
165
 
        # We expect both files to always exist in this case.
166
 
        for suffix in ('.knit', '.kndx'):
167
 
            t.rename(from_rel_url + suffix, to_rel_url + suffix)
168
 
 
169
 
    def _remove_file_id(self, file_id):
170
 
        t = self._transport.clone('knits')
171
 
        rel_url = self.texts._index._mapper.map((file_id, None))
172
 
        for suffix in ('.kndx', '.knit'):
173
 
            try:
174
 
                t.delete(rel_url + suffix)
175
 
            except errors.NoSuchFile:
176
 
                pass
177
 
 
178
 
    def _temp_inventories(self):
179
 
        result = self._format._get_inventories(self._transport, self,
180
 
            'inventory.new')
181
 
        # Reconciling when the output has no revisions would result in no
182
 
        # writes - but we want to ensure there is an inventory for
183
 
        # compatibility with older clients that don't lazy-load.
184
 
        result.get_parent_map([('A',)])
185
 
        return result
 
120
        # Knits get the revision graph from the index of the revision knit, so
 
121
        # it's always possible even if they're on an unlistable transport.
 
122
        return self._revision_store.all_revision_ids(self.get_transaction())
186
123
 
187
124
    def fileid_involved_between_revs(self, from_revid, to_revid):
188
125
        """Find file_id(s) which are involved in the changes between revisions.
210
147
        return self._fileid_involved_by_set(changed)
211
148
 
212
149
    @needs_read_lock
 
150
    def get_ancestry(self, revision_id, topo_sorted=True):
 
151
        """Return a list of revision-ids integrated by a revision.
 
152
        
 
153
        This is topologically sorted, unless 'topo_sorted' is specified as
 
154
        False.
 
155
        """
 
156
        if _mod_revision.is_null(revision_id):
 
157
            return [None]
 
158
        vf = self._get_revision_vf()
 
159
        try:
 
160
            return [None] + vf.get_ancestry(revision_id, topo_sorted)
 
161
        except errors.RevisionNotPresent:
 
162
            raise errors.NoSuchRevision(self, revision_id)
 
163
 
 
164
    @symbol_versioning.deprecated_method(symbol_versioning.one_two)
 
165
    def get_data_stream(self, revision_ids):
 
166
        """See Repository.get_data_stream.
 
167
        
 
168
        Deprecated in 1.2 for get_data_stream_for_search.
 
169
        """
 
170
        search_result = self.revision_ids_to_search_result(set(revision_ids))
 
171
        return self.get_data_stream_for_search(search_result)
 
172
 
 
173
    def get_data_stream_for_search(self, search):
 
174
        """See Repository.get_data_stream_for_search."""
 
175
        item_keys = self.item_keys_introduced_by(search.get_keys())
 
176
        for knit_kind, file_id, versions in item_keys:
 
177
            name = (knit_kind,)
 
178
            if knit_kind == 'file':
 
179
                name = ('file', file_id)
 
180
                knit = self.weave_store.get_weave_or_empty(
 
181
                    file_id, self.get_transaction())
 
182
            elif knit_kind == 'inventory':
 
183
                knit = self.get_inventory_weave()
 
184
            elif knit_kind == 'revisions':
 
185
                knit = self._revision_store.get_revision_file(
 
186
                    self.get_transaction())
 
187
            elif knit_kind == 'signatures':
 
188
                knit = self._revision_store.get_signature_file(
 
189
                    self.get_transaction())
 
190
            else:
 
191
                raise AssertionError('Unknown knit kind %r' % (knit_kind,))
 
192
            yield name, _get_stream_as_bytes(knit, versions)
 
193
 
 
194
    @needs_read_lock
213
195
    def get_revision(self, revision_id):
214
196
        """Return the Revision object for a named revision"""
215
197
        revision_id = osutils.safe_revision_id(revision_id)
216
198
        return self.get_revision_reconcile(revision_id)
217
199
 
 
200
    def _get_revision_vf(self):
 
201
        """:return: a versioned file containing the revisions."""
 
202
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
203
        return vf
 
204
 
 
205
    def has_revisions(self, revision_ids):
 
206
        """See Repository.has_revisions()."""
 
207
        result = set()
 
208
        transaction = self.get_transaction()
 
209
        for revision_id in revision_ids:
 
210
            if self._revision_store.has_revision_id(revision_id, transaction):
 
211
                result.add(revision_id)
 
212
        return result
 
213
 
218
214
    @needs_write_lock
219
215
    def reconcile(self, other=None, thorough=False):
220
216
        """Reconcile this repository."""
224
220
        return reconciler
225
221
    
226
222
    def _make_parents_provider(self):
227
 
        return _KnitsParentsProvider(self.revisions)
 
223
        return _KnitParentsProvider(self._get_revision_vf())
228
224
 
229
225
    def _find_inconsistent_revision_parents(self):
230
226
        """Find revisions with different parent lists in the revision object
235
231
        """
236
232
        if not self.is_locked():
237
233
            raise AssertionError()
238
 
        vf = self.revisions
239
 
        for index_version in vf.keys():
240
 
            parent_map = vf.get_parent_map([index_version])
241
 
            parents_according_to_index = tuple(parent[-1] for parent in
242
 
                parent_map[index_version])
243
 
            revision = self.get_revision(index_version[-1])
 
234
        vf = self._get_revision_vf()
 
235
        for index_version in vf.versions():
 
236
            parents_according_to_index = tuple(vf.get_parents_with_ghosts(
 
237
                index_version))
 
238
            revision = self.get_revision(index_version)
244
239
            parents_according_to_revision = tuple(revision.parent_ids)
245
240
            if parents_according_to_index != parents_according_to_revision:
246
 
                yield (index_version[-1], parents_according_to_index,
 
241
                yield (index_version, parents_according_to_index,
247
242
                    parents_according_to_revision)
248
243
 
249
244
    def _check_for_inconsistent_revision_parents(self):
281
276
    _commit_builder_class = None
282
277
    # Set this attribute in derived clases to control the _serializer that the
283
278
    # repository objects will have passed to their constructor.
284
 
    @property
285
 
    def _serializer(self):
286
 
        return xml5.serializer_v5
 
279
    _serializer = xml5.serializer_v5
287
280
    # Knit based repositories handle ghosts reasonably well.
288
281
    supports_ghosts = True
289
282
    # External lookups are not supported in this format.
290
283
    supports_external_lookups = False
291
284
 
292
 
    def _get_inventories(self, repo_transport, repo, name='inventory'):
293
 
        mapper = versionedfile.ConstantMapper(name)
294
 
        index = _mod_knit._KndxIndex(repo_transport, mapper,
295
 
            repo.get_transaction, repo.is_write_locked, repo.is_locked)
296
 
        access = _mod_knit._KnitKeyAccess(repo_transport, mapper)
297
 
        return _mod_knit.KnitVersionedFiles(index, access, annotated=False)
298
 
 
299
 
    def _get_revisions(self, repo_transport, repo):
300
 
        mapper = versionedfile.ConstantMapper('revisions')
301
 
        index = _mod_knit._KndxIndex(repo_transport, mapper,
302
 
            repo.get_transaction, repo.is_write_locked, repo.is_locked)
303
 
        access = _mod_knit._KnitKeyAccess(repo_transport, mapper)
304
 
        return _mod_knit.KnitVersionedFiles(index, access, max_delta_chain=0,
305
 
            annotated=False)
306
 
 
307
 
    def _get_signatures(self, repo_transport, repo):
308
 
        mapper = versionedfile.ConstantMapper('signatures')
309
 
        index = _mod_knit._KndxIndex(repo_transport, mapper,
310
 
            repo.get_transaction, repo.is_write_locked, repo.is_locked)
311
 
        access = _mod_knit._KnitKeyAccess(repo_transport, mapper)
312
 
        return _mod_knit.KnitVersionedFiles(index, access, max_delta_chain=0,
313
 
            annotated=False)
314
 
 
315
 
    def _get_texts(self, repo_transport, repo):
316
 
        mapper = versionedfile.HashEscapedPrefixMapper()
317
 
        base_transport = repo_transport.clone('knits')
318
 
        index = _mod_knit._KndxIndex(base_transport, mapper,
319
 
            repo.get_transaction, repo.is_write_locked, repo.is_locked)
320
 
        access = _mod_knit._KnitKeyAccess(base_transport, mapper)
321
 
        return _mod_knit.KnitVersionedFiles(index, access, max_delta_chain=200,
322
 
            annotated=True)
 
285
    def _get_control_store(self, repo_transport, control_files):
 
286
        """Return the control store for this repository."""
 
287
        return VersionedFileStore(
 
288
            repo_transport,
 
289
            prefixed=False,
 
290
            file_mode=control_files._file_mode,
 
291
            versionedfile_class=knit.make_file_knit,
 
292
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
 
293
            )
 
294
 
 
295
    def _get_revision_store(self, repo_transport, control_files):
 
296
        """See RepositoryFormat._get_revision_store()."""
 
297
        versioned_file_store = VersionedFileStore(
 
298
            repo_transport,
 
299
            file_mode=control_files._file_mode,
 
300
            prefixed=False,
 
301
            precious=True,
 
302
            versionedfile_class=knit.make_file_knit,
 
303
            versionedfile_kwargs={'delta':False,
 
304
                                  'factory':knit.KnitPlainFactory(),
 
305
                                 },
 
306
            escaped=True,
 
307
            )
 
308
        return KnitRevisionStore(versioned_file_store)
 
309
 
 
310
    def _get_text_store(self, transport, control_files):
 
311
        """See RepositoryFormat._get_text_store()."""
 
312
        return self._get_versioned_file_store('knits',
 
313
                                  transport,
 
314
                                  control_files,
 
315
                                  versionedfile_class=knit.make_file_knit,
 
316
                                  versionedfile_kwargs={
 
317
                                      'create_parent_dir':True,
 
318
                                      'delay_create':True,
 
319
                                      'dir_mode':control_files._dir_mode,
 
320
                                  },
 
321
                                  escaped=True)
323
322
 
324
323
    def initialize(self, a_bzrdir, shared=False):
325
324
        """Create a knit format 1 repository.
338
337
        repo_transport = a_bzrdir.get_repository_transport(None)
339
338
        control_files = lockable_files.LockableFiles(repo_transport,
340
339
                                'lock', lockdir.LockDir)
 
340
        control_store = self._get_control_store(repo_transport, control_files)
341
341
        transaction = transactions.WriteTransaction()
342
 
        result = self.open(a_bzrdir=a_bzrdir, _found=True)
343
 
        result.lock_write()
 
342
        # trigger a write of the inventory store.
 
343
        control_store.get_weave_or_empty('inventory', transaction)
 
344
        _revision_store = self._get_revision_store(repo_transport, control_files)
344
345
        # the revision id here is irrelevant: it will not be stored, and cannot
345
 
        # already exist, we do this to create files on disk for older clients.
346
 
        result.inventories.get_parent_map([('A',)])
347
 
        result.revisions.get_parent_map([('A',)])
348
 
        result.signatures.get_parent_map([('A',)])
349
 
        result.unlock()
350
 
        return result
 
346
        # already exist.
 
347
        _revision_store.has_revision_id('A', transaction)
 
348
        _revision_store.get_signature_file(transaction)
 
349
        return self.open(a_bzrdir=a_bzrdir, _found=True)
351
350
 
352
351
    def open(self, a_bzrdir, _found=False, _override_transport=None):
353
352
        """See RepositoryFormat.open().
364
363
            repo_transport = a_bzrdir.get_repository_transport(None)
365
364
        control_files = lockable_files.LockableFiles(repo_transport,
366
365
                                'lock', lockdir.LockDir)
367
 
        repo = self.repository_class(_format=self,
 
366
        text_store = self._get_text_store(repo_transport, control_files)
 
367
        control_store = self._get_control_store(repo_transport, control_files)
 
368
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
369
        return self.repository_class(_format=self,
368
370
                              a_bzrdir=a_bzrdir,
369
371
                              control_files=control_files,
 
372
                              _revision_store=_revision_store,
 
373
                              control_store=control_store,
 
374
                              text_store=text_store,
370
375
                              _commit_builder_class=self._commit_builder_class,
371
376
                              _serializer=self._serializer)
372
 
        repo.revisions = self._get_revisions(repo_transport, repo)
373
 
        repo.signatures = self._get_signatures(repo_transport, repo)
374
 
        repo.inventories = self._get_inventories(repo_transport, repo)
375
 
        repo.texts = self._get_texts(repo_transport, repo)
376
 
        repo._transport = repo_transport
377
 
        return repo
378
377
 
379
378
 
380
379
class RepositoryFormatKnit1(RepositoryFormatKnit):
395
394
 
396
395
    repository_class = KnitRepository
397
396
    _commit_builder_class = CommitBuilder
398
 
    @property
399
 
    def _serializer(self):
400
 
        return xml5.serializer_v5
 
397
    _serializer = xml5.serializer_v5
401
398
 
402
399
    def __ne__(self, other):
403
400
        return self.__class__ is not other.__class__
434
431
    _commit_builder_class = RootCommitBuilder
435
432
    rich_root_data = True
436
433
    supports_tree_reference = True
437
 
    @property
438
 
    def _serializer(self):
439
 
        return xml7.serializer_v7
 
434
    _serializer = xml7.serializer_v7
440
435
 
441
436
    def _get_matching_bzrdir(self):
442
437
        return bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
483
478
    _commit_builder_class = RootCommitBuilder
484
479
    rich_root_data = True
485
480
    supports_tree_reference = False
486
 
    @property
487
 
    def _serializer(self):
488
 
        return xml6.serializer_v6
 
481
    _serializer = xml6.serializer_v6
489
482
 
490
483
    def _get_matching_bzrdir(self):
491
484
        return bzrdir.format_registry.make_bzrdir('rich-root')
507
500
    def get_format_description(self):
508
501
        """See RepositoryFormat.get_format_description()."""
509
502
        return "Knit repository format 4"
 
503
 
 
504
 
 
505
def _get_stream_as_bytes(knit, required_versions):
 
506
    """Generate a serialised data stream.
 
507
 
 
508
    The format is a bencoding of a list.  The first element of the list is a
 
509
    string of the format signature, then each subsequent element is a list
 
510
    corresponding to a record.  Those lists contain:
 
511
 
 
512
      * a version id
 
513
      * a list of options
 
514
      * a list of parents
 
515
      * the bytes
 
516
 
 
517
    :returns: a bencoded list.
 
518
    """
 
519
    knit_stream = knit.get_data_stream(required_versions)
 
520
    format_signature, data_list, callable = knit_stream
 
521
    data = []
 
522
    data.append(format_signature)
 
523
    for version, options, length, parents in data_list:
 
524
        data.append([version, options, parents, callable(length)])
 
525
    return bencode.bencode(data)