~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

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,
40
40
    lru_cache,
41
41
    osutils,
42
42
    revision as _mod_revision,
43
 
    static_tuple,
44
43
    symbol_versioning,
45
44
    trace,
46
45
    tsort,
 
46
    ui,
47
47
    versionedfile,
48
48
    )
49
49
from bzrlib.bundle import serializer
52
52
from bzrlib.testament import Testament
53
53
""")
54
54
 
55
 
from bzrlib import (
56
 
    errors,
57
 
    registry,
58
 
    ui,
59
 
    )
60
55
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
61
56
from bzrlib.inter import InterObject
62
57
from bzrlib.inventory import (
65
60
    ROOT_ID,
66
61
    entry_factory,
67
62
    )
68
 
from bzrlib.recordcounter import RecordCounter
69
 
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 
63
from bzrlib.lock import _RelockDebugMixin
 
64
from bzrlib import registry
70
65
from bzrlib.trace import (
71
66
    log_exception_quietly, note, mutter, mutter_callsite, warning)
72
67
 
75
70
_deprecation_warning_done = False
76
71
 
77
72
 
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
73
class CommitBuilder(object):
87
74
    """Provides an interface to build up a commit.
88
75
 
243
230
 
244
231
    def _gen_revision_id(self):
245
232
        """Return new revision-id."""
246
 
        return generate_ids.gen_revision_id(self._committer, self._timestamp)
 
233
        return generate_ids.gen_revision_id(self._config.username(),
 
234
                                            self._timestamp)
247
235
 
248
236
    def _generate_revision_if_needed(self):
249
237
        """Create a revision id if None was supplied.
289
277
 
290
278
        :param tree: The tree which is being committed.
291
279
        """
292
 
        if len(self.parents) == 0:
293
 
            raise errors.RootMissing()
 
280
        # NB: if there are no parents then this method is not called, so no
 
281
        # need to guard on parents having length.
294
282
        entry = entry_factory['directory'](tree.path2id(''), '',
295
283
            None)
296
284
        entry.revision = self._new_revision_id
871
859
        # versioned roots do not change unless the tree found a change.
872
860
 
873
861
 
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
862
######################################################################
892
863
# Repositories
893
864
 
894
865
 
895
 
class Repository(_RelockDebugMixin, controldir.ControlComponent):
 
866
class Repository(_RelockDebugMixin):
896
867
    """Repository holding history for one or more branches.
897
868
 
898
869
    The repository holds and retrieves historical information including
1046
1017
                " id and insertion revid (%r, %r)"
1047
1018
                % (inv.revision_id, revision_id))
1048
1019
        if inv.root is None:
1049
 
            raise errors.RootMissing()
 
1020
            raise AssertionError()
1050
1021
        return self._add_inventory_checked(revision_id, inv, parents)
1051
1022
 
1052
1023
    def _add_inventory_checked(self, revision_id, inv, parents):
1319
1290
 
1320
1291
        :param _format: The format of the repository on disk.
1321
1292
        :param a_bzrdir: The BzrDir of the repository.
 
1293
 
 
1294
        In the future we will have a single api for all stores for
 
1295
        getting file texts, inventories and revisions, then
 
1296
        this construct will accept instances of those things.
1322
1297
        """
1323
 
        # In the future we will have a single api for all stores for
1324
 
        # getting file texts, inventories and revisions, then
1325
 
        # this construct will accept instances of those things.
1326
1298
        super(Repository, self).__init__()
1327
1299
        self._format = _format
1328
1300
        # the following are part of the public API for Repository:
1343
1315
        # rather copying them?
1344
1316
        self._safe_to_return_from_cache = False
1345
1317
 
1346
 
    @property
1347
 
    def user_transport(self):
1348
 
        return self.bzrdir.user_transport
1349
 
 
1350
 
    @property
1351
 
    def control_transport(self):
1352
 
        return self._transport
1353
 
 
1354
1318
    def __repr__(self):
1355
1319
        if self._fallback_repositories:
1356
1320
            return '%s(%r, fallback_repositories=%r)' % (
1404
1368
        data during reads, and allows a 'write_group' to be obtained. Write
1405
1369
        groups must be used for actual data insertion.
1406
1370
 
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
1371
        :param token: if this is already locked, then lock_write will fail
1414
1372
            unless the token matches the existing lock.
1415
1373
        :returns: a token if this instance supports tokens, otherwise None.
1418
1376
        :raises MismatchedToken: if the specified token doesn't match the token
1419
1377
            of the existing lock.
1420
1378
        :seealso: start_write_group.
1421
 
        :return: A RepositoryWriteLockResult.
 
1379
 
 
1380
        A token should be passed in if you know that you have locked the object
 
1381
        some other way, and need to synchronise this object's state with that
 
1382
        fact.
 
1383
 
 
1384
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
1422
1385
        """
1423
1386
        locked = self.is_locked()
1424
 
        token = self.control_files.lock_write(token=token)
 
1387
        result = self.control_files.lock_write(token=token)
1425
1388
        if not locked:
1426
1389
            self._warn_if_deprecated()
1427
1390
            self._note_lock('w')
1429
1392
                # Writes don't affect fallback repos
1430
1393
                repo.lock_read()
1431
1394
            self._refresh_data()
1432
 
        return RepositoryWriteLockResult(self.unlock, token)
 
1395
        return result
1433
1396
 
1434
1397
    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
1398
        locked = self.is_locked()
1441
1399
        self.control_files.lock_read()
1442
1400
        if not locked:
1445
1403
            for repo in self._fallback_repositories:
1446
1404
                repo.lock_read()
1447
1405
            self._refresh_data()
1448
 
        return LogicalLockResult(self.unlock)
1449
1406
 
1450
1407
    def get_physical_lock_status(self):
1451
1408
        return self.control_files.get_physical_lock_status()
1511
1468
 
1512
1469
        # now gather global repository information
1513
1470
        # XXX: This is available for many repos regardless of listability.
1514
 
        if self.user_transport.listable():
 
1471
        if self.bzrdir.root_transport.listable():
1515
1472
            # XXX: do we want to __define len__() ?
1516
1473
            # Maybe the versionedfiles object should provide a different
1517
1474
            # method to get the number of keys.
1549
1506
 
1550
1507
        ret = []
1551
1508
        for branches, repository in bzrdir.BzrDir.find_bzrdirs(
1552
 
                self.user_transport, evaluate=Evaluator()):
 
1509
                self.bzrdir.root_transport, evaluate=Evaluator()):
1553
1510
            if branches is not None:
1554
1511
                ret.extend(branches)
1555
1512
            if not using and repository is not None:
1669
1626
        return missing_keys
1670
1627
 
1671
1628
    def refresh_data(self):
1672
 
        """Re-read any data needed to synchronise with disk.
 
1629
        """Re-read any data needed to to synchronise with disk.
1673
1630
 
1674
1631
        This method is intended to be called after another repository instance
1675
1632
        (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.
 
1633
        repository. It may not be called during a write group, but may be
 
1634
        called at any other time.
1681
1635
        """
 
1636
        if self.is_in_write_group():
 
1637
            raise errors.InternalBzrError(
 
1638
                "May not refresh_data while in a write group.")
1682
1639
        self._refresh_data()
1683
1640
 
1684
1641
    def resume_write_group(self, tokens):
1723
1680
                "May not fetch while in a write group.")
1724
1681
        # fast path same-url fetch operations
1725
1682
        # TODO: lift out to somewhere common with RemoteRepository
1726
 
        # <https://bugs.launchpad.net/bzr/+bug/401646>
 
1683
        # <https://bugs.edge.launchpad.net/bzr/+bug/401646>
1727
1684
        if (self.has_same_location(source)
1728
1685
            and fetch_spec is None
1729
1686
            and self._has_same_fallbacks(source)):
2623
2580
            keys = tsort.topo_sort(parent_map)
2624
2581
        return [None] + list(keys)
2625
2582
 
2626
 
    def pack(self, hint=None, clean_obsolete_packs=False):
 
2583
    def pack(self, hint=None):
2627
2584
        """Compress the data within the repository.
2628
2585
 
2629
2586
        This operation only makes sense for some repository types. For other
2639
2596
            obtained from the result of commit_write_group(). Out of
2640
2597
            date hints are simply ignored, because concurrent operations
2641
2598
            can obsolete them rapidly.
2642
 
 
2643
 
        :param clean_obsolete_packs: Clean obsolete packs immediately after
2644
 
            the pack operation.
2645
2599
        """
2646
2600
 
2647
2601
    def get_transaction(self):
2672
2626
    def _make_parents_provider(self):
2673
2627
        return self
2674
2628
 
2675
 
    @needs_read_lock
2676
 
    def get_known_graph_ancestry(self, revision_ids):
2677
 
        """Return the known graph for a set of revision ids and their ancestors.
2678
 
        """
2679
 
        st = static_tuple.StaticTuple
2680
 
        revision_keys = [st(r_id).intern() for r_id in revision_ids]
2681
 
        known_graph = self.revisions.get_known_graph_ancestry(revision_keys)
2682
 
        return graph.GraphThunkIdsToKeys(known_graph)
2683
 
 
2684
2629
    def get_graph(self, other_repository=None):
2685
2630
        """Return the graph walker for this repository format"""
2686
2631
        parents_provider = self._make_parents_provider()
3213
3158
        """
3214
3159
        raise NotImplementedError(self.open)
3215
3160
 
3216
 
    def _run_post_repo_init_hooks(self, repository, a_bzrdir, shared):
3217
 
        from bzrlib.bzrdir import BzrDir, RepoInitHookParams
3218
 
        hooks = BzrDir.hooks['post_repo_init']
3219
 
        if not hooks:
3220
 
            return
3221
 
        params = RepoInitHookParams(repository, self, a_bzrdir, shared)
3222
 
        for hook in hooks:
3223
 
            hook(params)
3224
 
 
3225
3161
 
3226
3162
class MetaDirRepositoryFormat(RepositoryFormat):
3227
3163
    """Common base class for the new repositories using the metadir layout."""
4284
4220
                is_resume = False
4285
4221
            try:
4286
4222
                # locked_insert_stream performs a commit|suspend.
4287
 
                return self._locked_insert_stream(stream, src_format,
4288
 
                    is_resume)
 
4223
                return self._locked_insert_stream(stream, src_format, is_resume)
4289
4224
            except:
4290
4225
                self.target_repo.abort_write_group(suppress_errors=True)
4291
4226
                raise
4338
4273
                # required if the serializers are different only in terms of
4339
4274
                # the inventory.
4340
4275
                if src_serializer == to_serializer:
4341
 
                    self.target_repo.revisions.insert_record_stream(substream)
 
4276
                    self.target_repo.revisions.insert_record_stream(
 
4277
                        substream)
4342
4278
                else:
4343
4279
                    self._extract_and_insert_revisions(substream,
4344
4280
                        src_serializer)
4452
4388
        """Create a StreamSource streaming from from_repository."""
4453
4389
        self.from_repository = from_repository
4454
4390
        self.to_format = to_format
4455
 
        self._record_counter = RecordCounter()
4456
4391
 
4457
4392
    def delta_on_metadata(self):
4458
4393
        """Return True if delta's are permitted on metadata streams.