/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    check,
26
26
    chk_map,
27
27
    config,
28
 
    controldir,
29
28
    debug,
 
29
    errors,
30
30
    fetch as _mod_fetch,
31
31
    fifo_cache,
32
32
    generate_ids,
44
44
    symbol_versioning,
45
45
    trace,
46
46
    tsort,
 
47
    ui,
47
48
    versionedfile,
48
49
    )
49
50
from bzrlib.bundle import serializer
52
53
from bzrlib.testament import Testament
53
54
""")
54
55
 
55
 
from bzrlib import (
56
 
    errors,
57
 
    registry,
58
 
    ui,
59
 
    )
60
56
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
61
57
from bzrlib.inter import InterObject
62
58
from bzrlib.inventory import (
65
61
    ROOT_ID,
66
62
    entry_factory,
67
63
    )
68
 
from bzrlib.recordcounter import RecordCounter
69
 
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 
64
from bzrlib.lock import _RelockDebugMixin
 
65
from bzrlib import registry
70
66
from bzrlib.trace import (
71
67
    log_exception_quietly, note, mutter, mutter_callsite, warning)
72
68
 
75
71
_deprecation_warning_done = False
76
72
 
77
73
 
78
 
class IsInWriteGroupError(errors.InternalBzrError):
79
 
 
80
 
    _fmt = "May not refresh_data of repo %(repo)s while in a write group."
81
 
 
82
 
    def __init__(self, repo):
83
 
        errors.InternalBzrError.__init__(self, repo=repo)
84
 
 
85
 
 
86
74
class CommitBuilder(object):
87
75
    """Provides an interface to build up a commit.
88
76
 
243
231
 
244
232
    def _gen_revision_id(self):
245
233
        """Return new revision-id."""
246
 
        return generate_ids.gen_revision_id(self._committer, self._timestamp)
 
234
        return generate_ids.gen_revision_id(self._config.username(),
 
235
                                            self._timestamp)
247
236
 
248
237
    def _generate_revision_if_needed(self):
249
238
        """Create a revision id if None was supplied.
289
278
 
290
279
        :param tree: The tree which is being committed.
291
280
        """
292
 
        if len(self.parents) == 0:
293
 
            raise errors.RootMissing()
 
281
        # NB: if there are no parents then this method is not called, so no
 
282
        # need to guard on parents having length.
294
283
        entry = entry_factory['directory'](tree.path2id(''), '',
295
284
            None)
296
285
        entry.revision = self._new_revision_id
871
860
        # versioned roots do not change unless the tree found a change.
872
861
 
873
862
 
874
 
class RepositoryWriteLockResult(LogicalLockResult):
875
 
    """The result of write locking a repository.
876
 
 
877
 
    :ivar repository_token: The token obtained from the underlying lock, or
878
 
        None.
879
 
    :ivar unlock: A callable which will unlock the lock.
880
 
    """
881
 
 
882
 
    def __init__(self, unlock, repository_token):
883
 
        LogicalLockResult.__init__(self, unlock)
884
 
        self.repository_token = repository_token
885
 
 
886
 
    def __repr__(self):
887
 
        return "RepositoryWriteLockResult(%s, %s)" % (self.repository_token,
888
 
            self.unlock)
889
 
 
890
 
 
891
863
######################################################################
892
864
# Repositories
893
865
 
894
866
 
895
 
class Repository(_RelockDebugMixin, controldir.ControlComponent):
 
867
class Repository(_RelockDebugMixin, bzrdir.ControlComponent):
896
868
    """Repository holding history for one or more branches.
897
869
 
898
870
    The repository holds and retrieves historical information including
1046
1018
                " id and insertion revid (%r, %r)"
1047
1019
                % (inv.revision_id, revision_id))
1048
1020
        if inv.root is None:
1049
 
            raise errors.RootMissing()
 
1021
            raise AssertionError()
1050
1022
        return self._add_inventory_checked(revision_id, inv, parents)
1051
1023
 
1052
1024
    def _add_inventory_checked(self, revision_id, inv, parents):
1404
1376
        data during reads, and allows a 'write_group' to be obtained. Write
1405
1377
        groups must be used for actual data insertion.
1406
1378
 
1407
 
        A token should be passed in if you know that you have locked the object
1408
 
        some other way, and need to synchronise this object's state with that
1409
 
        fact.
1410
 
 
1411
 
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
1412
 
 
1413
1379
        :param token: if this is already locked, then lock_write will fail
1414
1380
            unless the token matches the existing lock.
1415
1381
        :returns: a token if this instance supports tokens, otherwise None.
1418
1384
        :raises MismatchedToken: if the specified token doesn't match the token
1419
1385
            of the existing lock.
1420
1386
        :seealso: start_write_group.
1421
 
        :return: A RepositoryWriteLockResult.
 
1387
 
 
1388
        A token should be passed in if you know that you have locked the object
 
1389
        some other way, and need to synchronise this object's state with that
 
1390
        fact.
 
1391
 
 
1392
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
1422
1393
        """
1423
1394
        locked = self.is_locked()
1424
 
        token = self.control_files.lock_write(token=token)
 
1395
        result = self.control_files.lock_write(token=token)
1425
1396
        if not locked:
1426
1397
            self._warn_if_deprecated()
1427
1398
            self._note_lock('w')
1429
1400
                # Writes don't affect fallback repos
1430
1401
                repo.lock_read()
1431
1402
            self._refresh_data()
1432
 
        return RepositoryWriteLockResult(self.unlock, token)
 
1403
        return result
1433
1404
 
1434
1405
    def lock_read(self):
1435
 
        """Lock the repository for read operations.
1436
 
 
1437
 
        :return: An object with an unlock method which will release the lock
1438
 
            obtained.
1439
 
        """
1440
1406
        locked = self.is_locked()
1441
1407
        self.control_files.lock_read()
1442
1408
        if not locked:
1445
1411
            for repo in self._fallback_repositories:
1446
1412
                repo.lock_read()
1447
1413
            self._refresh_data()
1448
 
        return LogicalLockResult(self.unlock)
1449
1414
 
1450
1415
    def get_physical_lock_status(self):
1451
1416
        return self.control_files.get_physical_lock_status()
1669
1634
        return missing_keys
1670
1635
 
1671
1636
    def refresh_data(self):
1672
 
        """Re-read any data needed to synchronise with disk.
 
1637
        """Re-read any data needed to to synchronise with disk.
1673
1638
 
1674
1639
        This method is intended to be called after another repository instance
1675
1640
        (such as one used by a smart server) has inserted data into the
1676
 
        repository. On all repositories this will work outside of write groups.
1677
 
        Some repository formats (pack and newer for bzrlib native formats)
1678
 
        support refresh_data inside write groups. If called inside a write
1679
 
        group on a repository that does not support refreshing in a write group
1680
 
        IsInWriteGroupError will be raised.
 
1641
        repository. It may not be called during a write group, but may be
 
1642
        called at any other time.
1681
1643
        """
 
1644
        if self.is_in_write_group():
 
1645
            raise errors.InternalBzrError(
 
1646
                "May not refresh_data while in a write group.")
1682
1647
        self._refresh_data()
1683
1648
 
1684
1649
    def resume_write_group(self, tokens):
1723
1688
                "May not fetch while in a write group.")
1724
1689
        # fast path same-url fetch operations
1725
1690
        # TODO: lift out to somewhere common with RemoteRepository
1726
 
        # <https://bugs.launchpad.net/bzr/+bug/401646>
 
1691
        # <https://bugs.edge.launchpad.net/bzr/+bug/401646>
1727
1692
        if (self.has_same_location(source)
1728
1693
            and fetch_spec is None
1729
1694
            and self._has_same_fallbacks(source)):
3389
3354
    'bzrlib.repofmt.groupcompress_repo',
3390
3355
    'RepositoryFormat2a',
3391
3356
    )
3392
 
format_registry.register_lazy(
3393
 
    'Bazaar development format 8\n',
3394
 
    'bzrlib.repofmt.groupcompress_repo',
3395
 
    'RepositoryFormat2aSubtree',
3396
 
    )
3397
3357
 
3398
3358
 
3399
3359
class InterRepository(InterObject):
3853
3813
                basis_id, delta, current_revision_id, parents_parents)
3854
3814
            cache[current_revision_id] = parent_tree
3855
3815
 
3856
 
    def _fetch_batch(self, revision_ids, basis_id, cache):
 
3816
    def _fetch_batch(self, revision_ids, basis_id, cache, a_graph=None):
3857
3817
        """Fetch across a few revisions.
3858
3818
 
3859
3819
        :param revision_ids: The revisions to copy
3860
3820
        :param basis_id: The revision_id of a tree that must be in cache, used
3861
3821
            as a basis for delta when no other base is available
3862
3822
        :param cache: A cache of RevisionTrees that we can use.
 
3823
        :param a_graph: A Graph object to determine the heads() of the
 
3824
            rich-root data stream.
3863
3825
        :return: The revision_id of the last converted tree. The RevisionTree
3864
3826
            for it will be in cache
3865
3827
        """
3933
3895
        if root_keys_to_create:
3934
3896
            root_stream = _mod_fetch._new_root_data_stream(
3935
3897
                root_keys_to_create, self._revision_id_to_root_id, parent_map,
3936
 
                self.source)
 
3898
                self.source, graph=a_graph)
3937
3899
            to_texts.insert_record_stream(root_stream)
3938
3900
        to_texts.insert_record_stream(from_texts.get_record_stream(
3939
3901
            text_keys, self.target._format._fetch_order,
3996
3958
        cache[basis_id] = basis_tree
3997
3959
        del basis_tree # We don't want to hang on to it here
3998
3960
        hints = []
3999
 
        a_graph = None
 
3961
        if self._converting_to_rich_root and len(revision_ids) > 100:
 
3962
            a_graph = _mod_fetch._get_rich_root_heads_graph(self.source,
 
3963
                                                            revision_ids)
 
3964
        else:
 
3965
            a_graph = None
4000
3966
 
4001
3967
        for offset in range(0, len(revision_ids), batch_size):
4002
3968
            self.target.start_write_group()
4004
3970
                pb.update('Transferring revisions', offset,
4005
3971
                          len(revision_ids))
4006
3972
                batch = revision_ids[offset:offset+batch_size]
4007
 
                basis_id = self._fetch_batch(batch, basis_id, cache)
 
3973
                basis_id = self._fetch_batch(batch, basis_id, cache,
 
3974
                                             a_graph=a_graph)
4008
3975
            except:
4009
3976
                self.source._safe_to_return_from_cache = False
4010
3977
                self.target.abort_write_group()
4282
4249
                is_resume = False
4283
4250
            try:
4284
4251
                # locked_insert_stream performs a commit|suspend.
4285
 
                return self._locked_insert_stream(stream, src_format,
4286
 
                    is_resume)
 
4252
                return self._locked_insert_stream(stream, src_format, is_resume)
4287
4253
            except:
4288
4254
                self.target_repo.abort_write_group(suppress_errors=True)
4289
4255
                raise
4336
4302
                # required if the serializers are different only in terms of
4337
4303
                # the inventory.
4338
4304
                if src_serializer == to_serializer:
4339
 
                    self.target_repo.revisions.insert_record_stream(substream)
 
4305
                    self.target_repo.revisions.insert_record_stream(
 
4306
                        substream)
4340
4307
                else:
4341
4308
                    self._extract_and_insert_revisions(substream,
4342
4309
                        src_serializer)
4450
4417
        """Create a StreamSource streaming from from_repository."""
4451
4418
        self.from_repository = from_repository
4452
4419
        self.to_format = to_format
4453
 
        self._record_counter = RecordCounter()
4454
4420
 
4455
4421
    def delta_on_metadata(self):
4456
4422
        """Return True if delta's are permitted on metadata streams.