/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
1
# Copyright (C) 2005, 2006 Canonical Ltd
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
16
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
17
from binascii import hexlify
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
18
from copy import deepcopy
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
19
from cStringIO import StringIO
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
20
import re
21
import time
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
22
from unittest import TestSuite
1553.5.70 by Martin Pool
doc
23
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
24
import bzrlib.bzrdir as bzrdir
1534.4.28 by Robert Collins
first cut at merge from integration.
25
from bzrlib.decorators import needs_read_lock, needs_write_lock
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
26
import bzrlib.errors as errors
1534.4.28 by Robert Collins
first cut at merge from integration.
27
from bzrlib.errors import InvalidRevisionId
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
28
import bzrlib.gpg as gpg
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
29
from bzrlib.graph import Graph
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
30
from bzrlib.inter import InterObject
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
31
from bzrlib.inventory import Inventory
1628.1.4 by Robert Collins
Change knit format to use non-delta, non-annotated revisions and signatures.
32
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
33
from bzrlib.lockable_files import LockableFiles, TransportLock
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
34
from bzrlib.lockdir import LockDir
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
35
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date, 
36
                            local_time_offset)
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
37
from bzrlib.revision import NULL_REVISION, Revision
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
38
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
39
from bzrlib.store.text import TextStore
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
40
from bzrlib.symbol_versioning import *
1608.2.1 by Martin Pool
[merge] Storage filename escaping
41
from bzrlib.trace import mutter, note
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
42
from bzrlib.tree import RevisionTree
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
43
from bzrlib.tsort import topo_sort
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
44
from bzrlib.testament import Testament
1534.4.28 by Robert Collins
first cut at merge from integration.
45
from bzrlib.tree import EmptyTree
1185.79.2 by John Arbash Meinel
Adding progress bars to copy_all and copy_multi, fixing ordering of repository.clone() to pull inventories after weaves.
46
import bzrlib.ui
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
47
from bzrlib.weave import WeaveFile
1534.4.28 by Robert Collins
first cut at merge from integration.
48
import bzrlib.xml5
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
49
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
50
1185.66.5 by Aaron Bentley
Renamed RevisionStorage to Repository
51
class Repository(object):
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
52
    """Repository holding history for one or more branches.
53
54
    The repository holds and retrieves historical information including
55
    revisions and file history.  It's normally accessed only by the Branch,
56
    which views a particular line of development through that history.
57
58
    The Repository builds on top of Stores and a Transport, which respectively 
59
    describe the disk data format and the way of accessing the (possibly 
60
    remote) disk.
61
    """
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
62
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
63
    @needs_write_lock
64
    def add_inventory(self, revid, inv, parents):
65
        """Add the inventory inv to the repository as revid.
66
        
67
        :param parents: The revision ids of the parents that revid
68
                        is known to have and are in the repository already.
69
70
        returns the sha1 of the serialized inventory.
71
        """
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
72
        assert inv.revision_id is None or inv.revision_id == revid, \
73
            "Mismatch between inventory revision" \
74
            " id and insertion revid (%r, %r)" % (inv.revision_id, revid)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
75
        inv_text = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
76
        inv_sha1 = bzrlib.osutils.sha_string(inv_text)
1563.2.25 by Robert Collins
Merge in upstream.
77
        inv_vf = self.control_weaves.get_weave('inventory',
78
                                               self.get_transaction())
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
79
        self._inventory_add_lines(inv_vf, revid, parents, bzrlib.osutils.split_lines(inv_text))
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
80
        return inv_sha1
81
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
82
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
83
        final_parents = []
84
        for parent in parents:
85
            if parent in inv_vf:
86
                final_parents.append(parent)
87
88
        inv_vf.add_lines(revid, final_parents, lines)
89
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
90
    @needs_write_lock
91
    def add_revision(self, rev_id, rev, inv=None, config=None):
92
        """Add rev to the revision store as rev_id.
93
94
        :param rev_id: the revision id to use.
95
        :param rev: The revision object.
96
        :param inv: The inventory for the revision. if None, it will be looked
97
                    up in the inventory storer
98
        :param config: If None no digital signature will be created.
99
                       If supplied its signature_needed method will be used
100
                       to determine if a signature should be made.
101
        """
102
        if config is not None and config.signature_needed():
103
            if inv is None:
104
                inv = self.get_inventory(rev_id)
105
            plaintext = Testament(rev, inv).as_short_text()
106
            self.store_revision_signature(
107
                gpg.GPGStrategy(config), plaintext, rev_id)
108
        if not rev_id in self.get_inventory_weave():
109
            if inv is None:
110
                raise errors.WeaveRevisionNotPresent(rev_id,
111
                                                     self.get_inventory_weave())
112
            else:
113
                # yes, this is not suitable for adding with ghosts.
114
                self.add_inventory(rev_id, inv, rev.parent_ids)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
115
        self._revision_store.add_revision(rev, self.get_transaction())
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
116
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
117
    @needs_read_lock
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
118
    def _all_possible_ids(self):
119
        """Return all the possible revisions that we could find."""
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
120
        return self.get_inventory_weave().versions()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
121
122
    @needs_read_lock
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
123
    def all_revision_ids(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
124
        """Returns a list of all the revision ids in the repository. 
125
126
        These are in as much topological order as the underlying store can 
127
        present: for weaves ghosts may lead to a lack of correctness until
128
        the reweave updates the parents list.
129
        """
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
130
        if self._revision_store.text_store.listable():
131
            return self._revision_store.all_revision_ids(self.get_transaction())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
132
        result = self._all_possible_ids()
133
        return self._eliminate_revisions_not_present(result)
134
1687.1.7 by Robert Collins
Teach Repository about break_lock.
135
    def break_lock(self):
136
        """Break a lock if one is present from another instance.
137
138
        Uses the ui factory to ask for confirmation if the lock may be from
139
        an active process.
140
        """
141
        self.control_files.break_lock()
142
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
143
    @needs_read_lock
144
    def _eliminate_revisions_not_present(self, revision_ids):
145
        """Check every revision id in revision_ids to see if we have it.
146
147
        Returns a set of the present revisions.
148
        """
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
149
        result = []
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
150
        for id in revision_ids:
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
151
            if self.has_revision(id):
152
               result.append(id)
153
        return result
154
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
155
    @staticmethod
156
    def create(a_bzrdir):
157
        """Construct the current default format repository in a_bzrdir."""
158
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
159
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
160
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
161
        """instantiate a Repository.
162
163
        :param _format: The format of the repository on disk.
164
        :param a_bzrdir: The BzrDir of the repository.
165
166
        In the future we will have a single api for all stores for
167
        getting file texts, inventories and revisions, then
168
        this construct will accept instances of those things.
169
        """
1608.2.1 by Martin Pool
[merge] Storage filename escaping
170
        super(Repository, self).__init__()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
171
        self._format = _format
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
172
        # the following are part of the public API for Repository:
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
173
        self.bzrdir = a_bzrdir
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
174
        self.control_files = control_files
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
175
        self._revision_store = _revision_store
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
176
        self.text_store = text_store
177
        # backwards compatability
178
        self.weave_store = text_store
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
179
        # not right yet - should be more semantically clear ? 
180
        # 
181
        self.control_store = control_store
182
        self.control_weaves = control_store
1608.2.1 by Martin Pool
[merge] Storage filename escaping
183
        # TODO: make sure to construct the right store classes, etc, depending
184
        # on whether escaping is required.
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
185
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
186
    def __repr__(self):
187
        return '%s(%r)' % (self.__class__.__name__, 
188
                           self.bzrdir.transport.base)
189
1694.2.6 by Martin Pool
[merge] bzr.dev
190
    def is_locked(self):
191
        return self.control_files.is_locked()
192
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
193
    def lock_write(self):
194
        self.control_files.lock_write()
195
196
    def lock_read(self):
1553.5.55 by Martin Pool
[revert] broken changes
197
        self.control_files.lock_read()
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
198
1694.2.6 by Martin Pool
[merge] bzr.dev
199
    def get_physical_lock_status(self):
200
        return self.control_files.get_physical_lock_status()
1624.3.36 by Olaf Conradi
Rename is_transport_locked() to get_physical_lock_status() as the
201
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
202
    @needs_read_lock
203
    def missing_revision_ids(self, other, revision_id=None):
204
        """Return the revision ids that other has that this does not.
205
        
206
        These are returned in topological order.
207
208
        revision_id: only return revision ids included by revision_id.
209
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
210
        return InterRepository.get(other, self).missing_revision_ids(revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
211
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
212
    @staticmethod
213
    def open(base):
214
        """Open the repository rooted at base.
215
216
        For instance, if the repository is at URL/.bzr/repository,
217
        Repository.open(URL) -> a Repository instance.
218
        """
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.
219
        control = bzrlib.bzrdir.BzrDir.open(base)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
220
        return control.open_repository()
221
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
222
    def copy_content_into(self, destination, revision_id=None, basis=None):
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
223
        """Make a complete copy of the content in self into destination.
224
        
225
        This is a destructive operation! Do not use it on existing 
226
        repositories.
227
        """
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.
228
        return InterRepository.get(self, destination).copy_content(revision_id, basis)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
229
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
230
    def fetch(self, source, revision_id=None, pb=None):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
231
        """Fetch the content required to construct revision_id from source.
232
233
        If revision_id is None all content is copied.
234
        """
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
235
        return InterRepository.get(source, self).fetch(revision_id=revision_id,
236
                                                       pb=pb)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
237
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
238
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
239
                           timezone=None, committer=None, revprops=None, 
240
                           revision_id=None):
241
        """Obtain a CommitBuilder for this repository.
242
        
243
        :param branch: Branch to commit to.
244
        :param parents: Revision ids of the parents of the new revision.
245
        :param config: Configuration to use.
246
        :param timestamp: Optional timestamp recorded for commit.
247
        :param timezone: Optional timezone for timestamp.
248
        :param committer: Optional committer to set for commit.
249
        :param revprops: Optional dictionary of revision properties.
250
        :param revision_id: Optional revision id.
251
        """
252
        return CommitBuilder(self, parents, config, timestamp, timezone,
253
                             committer, revprops, revision_id)
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
254
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
255
    def unlock(self):
256
        self.control_files.unlock()
257
1185.65.27 by Robert Collins
Tweak storage towards mergability.
258
    @needs_read_lock
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
259
    def clone(self, a_bzrdir, revision_id=None, basis=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
260
        """Clone this repository into a_bzrdir using the current format.
261
262
        Currently no check is made that the format of this repository and
263
        the bzrdir format are compatible. FIXME RBC 20060201.
264
        """
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
265
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
266
            # use target default format.
267
            result = a_bzrdir.create_repository()
268
        # FIXME RBC 20060209 split out the repository type to avoid this check ?
269
        elif isinstance(a_bzrdir._format,
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.
270
                      (bzrlib.bzrdir.BzrDirFormat4,
271
                       bzrlib.bzrdir.BzrDirFormat5,
272
                       bzrlib.bzrdir.BzrDirFormat6)):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
273
            result = a_bzrdir.open_repository()
274
        else:
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
275
            result = self._format.initialize(a_bzrdir, shared=self.is_shared())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
276
        self.copy_content_into(result, revision_id, basis)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
277
        return result
278
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
279
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
280
    def has_revision(self, revision_id):
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
281
        """True if this repository has a copy of the revision."""
282
        return self._revision_store.has_revision_id(revision_id,
283
                                                    self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
284
1185.65.27 by Robert Collins
Tweak storage towards mergability.
285
    @needs_read_lock
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
286
    def get_revision_reconcile(self, revision_id):
287
        """'reconcile' helper routine that allows access to a revision always.
288
        
289
        This variant of get_revision does not cross check the weave graph
290
        against the revision one as get_revision does: but it should only
291
        be used by reconcile, or reconcile-alike commands that are correcting
292
        or testing the revision graph.
293
        """
1563.2.25 by Robert Collins
Merge in upstream.
294
        if not revision_id or not isinstance(revision_id, basestring):
295
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
296
        return self._revision_store.get_revision(revision_id,
297
                                                 self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
298
1185.65.27 by Robert Collins
Tweak storage towards mergability.
299
    @needs_read_lock
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
300
    def get_revision_xml(self, revision_id):
301
        rev = self.get_revision(revision_id) 
302
        rev_tmp = StringIO()
303
        # the current serializer..
304
        self._revision_store._serializer.write_revision(rev, rev_tmp)
305
        rev_tmp.seek(0)
306
        return rev_tmp.getvalue()
307
308
    @needs_read_lock
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
309
    def get_revision(self, revision_id):
310
        """Return the Revision object for a named revision"""
311
        r = self.get_revision_reconcile(revision_id)
312
        # weave corruption can lead to absent revision markers that should be
313
        # present.
314
        # the following test is reasonably cheap (it needs a single weave read)
315
        # and the weave is cached in read transactions. In write transactions
316
        # it is not cached but typically we only read a small number of
317
        # revisions. For knits when they are introduced we will probably want
318
        # to ensure that caching write transactions are in use.
319
        inv = self.get_inventory_weave()
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
320
        self._check_revision_parents(r, inv)
321
        return r
322
323
    def _check_revision_parents(self, revision, inventory):
324
        """Private to Repository and Fetch.
325
        
326
        This checks the parentage of revision in an inventory weave for 
327
        consistency and is only applicable to inventory-weave-for-ancestry
328
        using repository formats & fetchers.
329
        """
1563.2.25 by Robert Collins
Merge in upstream.
330
        weave_parents = inventory.get_parents(revision.revision_id)
331
        weave_names = inventory.versions()
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
332
        for parent_id in revision.parent_ids:
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
333
            if parent_id in weave_names:
334
                # this parent must not be a ghost.
335
                if not parent_id in weave_parents:
336
                    # but it is a ghost
337
                    raise errors.CorruptRepository(self)
338
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
339
    @needs_write_lock
340
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
341
        signature = gpg_strategy.sign(plaintext)
342
        self._revision_store.add_revision_signature_text(revision_id,
343
                                                         signature,
344
                                                         self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
345
1694.2.6 by Martin Pool
[merge] bzr.dev
346
    def fileids_altered_by_revision_ids(self, revision_ids):
347
        """Find the file ids and versions affected by revisions.
348
349
        :param revisions: an iterable containing revision ids.
350
        :return: a dictionary mapping altered file-ids to an iterable of
351
        revision_ids. Each altered file-ids has the exact revision_ids that
352
        altered it listed explicitly.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
353
        """
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
354
        assert isinstance(self._format, (RepositoryFormat5,
355
                                         RepositoryFormat6,
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
356
                                         RepositoryFormat7,
357
                                         RepositoryFormatKnit1)), \
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
358
            "fileid_involved only supported for branches which store inventory as unnested xml"
1694.2.6 by Martin Pool
[merge] bzr.dev
359
        selected_revision_ids = set(revision_ids)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
360
        w = self.get_inventory_weave()
1694.2.6 by Martin Pool
[merge] bzr.dev
361
        result = {}
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
362
1694.2.6 by Martin Pool
[merge] bzr.dev
363
        # this code needs to read every new line in every inventory for the
364
        # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
365
        # not pesent in one of those inventories is unnecessary but not 
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
366
        # harmful because we are filtering by the revision id marker in the
1694.2.6 by Martin Pool
[merge] bzr.dev
367
        # inventory lines : we only select file ids altered in one of those  
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
368
        # revisions. We dont need to see all lines in the inventory because
369
        # only those added in an inventory in rev X can contain a revision=X
370
        # line.
1694.2.6 by Martin Pool
[merge] bzr.dev
371
        for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
372
            start = line.find('file_id="')+9
373
            if start < 9: continue
374
            end = line.find('"', start)
375
            assert end>= 0
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
376
            file_id = _unescape_xml(line[start:end])
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
377
378
            start = line.find('revision="')+10
379
            if start < 10: continue
380
            end = line.find('"', start)
381
            assert end>= 0
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
382
            revision_id = _unescape_xml(line[start:end])
1694.2.6 by Martin Pool
[merge] bzr.dev
383
            if revision_id in selected_revision_ids:
384
                result.setdefault(file_id, set()).add(revision_id)
385
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
386
1185.65.27 by Robert Collins
Tweak storage towards mergability.
387
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
388
    def get_inventory_weave(self):
389
        return self.control_weaves.get_weave('inventory',
390
            self.get_transaction())
391
1185.65.27 by Robert Collins
Tweak storage towards mergability.
392
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
393
    def get_inventory(self, revision_id):
394
        """Get Inventory object by hash."""
1740.2.3 by Aaron Bentley
Only reserialize the working tree basis inventory when needed.
395
        return self.deserialise_inventory(
396
            revision_id, self.get_inventory_xml(revision_id))
397
398
    def deserialise_inventory(self, revision_id, xml):
399
        """Transform the xml into an inventory object. 
400
401
        :param revision_id: The expected revision id of the inventory.
402
        :param xml: A serialised inventory.
403
        """
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
404
        return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
405
1185.65.27 by Robert Collins
Tweak storage towards mergability.
406
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
407
    def get_inventory_xml(self, revision_id):
408
        """Get inventory XML as a file object."""
409
        try:
410
            assert isinstance(revision_id, basestring), type(revision_id)
411
            iw = self.get_inventory_weave()
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
412
            return iw.get_text(revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
413
        except IndexError:
414
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
415
1185.65.27 by Robert Collins
Tweak storage towards mergability.
416
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
417
    def get_inventory_sha1(self, revision_id):
418
        """Return the sha1 hash of the inventory entry
419
        """
420
        return self.get_revision(revision_id).inventory_sha1
421
1185.65.27 by Robert Collins
Tweak storage towards mergability.
422
    @needs_read_lock
1590.1.1 by Robert Collins
Improve common_ancestor performance.
423
    def get_revision_graph(self, revision_id=None):
424
        """Return a dictionary containing the revision graph.
425
        
426
        :return: a dictionary of revision_id->revision_parents_list.
427
        """
428
        weave = self.get_inventory_weave()
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
429
        all_revisions = self._eliminate_revisions_not_present(weave.versions())
430
        entire_graph = dict([(node, weave.get_parents(node)) for 
1590.1.1 by Robert Collins
Improve common_ancestor performance.
431
                             node in all_revisions])
432
        if revision_id is None:
433
            return entire_graph
434
        elif revision_id not in entire_graph:
435
            raise errors.NoSuchRevision(self, revision_id)
436
        else:
437
            # add what can be reached from revision_id
438
            result = {}
439
            pending = set([revision_id])
440
            while len(pending) > 0:
441
                node = pending.pop()
442
                result[node] = entire_graph[node]
443
                for revision_id in result[node]:
444
                    if revision_id not in result:
445
                        pending.add(revision_id)
446
            return result
447
448
    @needs_read_lock
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
449
    def get_revision_graph_with_ghosts(self, revision_ids=None):
450
        """Return a graph of the revisions with ghosts marked as applicable.
451
452
        :param revision_ids: an iterable of revisions to graph or None for all.
453
        :return: a Graph object with the graph reachable from revision_ids.
454
        """
455
        result = Graph()
456
        if not revision_ids:
457
            pending = set(self.all_revision_ids())
458
            required = set([])
459
        else:
460
            pending = set(revision_ids)
461
            required = set(revision_ids)
462
        done = set([])
463
        while len(pending):
464
            revision_id = pending.pop()
465
            try:
466
                rev = self.get_revision(revision_id)
467
            except errors.NoSuchRevision:
468
                if revision_id in required:
469
                    raise
470
                # a ghost
471
                result.add_ghost(revision_id)
472
                continue
473
            for parent_id in rev.parent_ids:
474
                # is this queued or done ?
475
                if (parent_id not in pending and
476
                    parent_id not in done):
477
                    # no, queue it.
478
                    pending.add(parent_id)
479
            result.add_node(revision_id, rev.parent_ids)
1594.2.15 by Robert Collins
Unfuck performance.
480
            done.add(revision_id)
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
481
        return result
482
483
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
484
    def get_revision_inventory(self, revision_id):
485
        """Return inventory of a past revision."""
486
        # TODO: Unify this with get_inventory()
487
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
488
        # must be the same as its revision, so this is trivial.
1534.4.28 by Robert Collins
first cut at merge from integration.
489
        if revision_id is None:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
490
            # This does not make sense: if there is no revision,
491
            # then it is the current tree inventory surely ?!
492
            # and thus get_root_id() is something that looks at the last
493
            # commit on the branch, and the get_root_id is an inventory check.
494
            raise NotImplementedError
495
            # return Inventory(self.get_root_id())
496
        else:
497
            return self.get_inventory(revision_id)
498
1185.65.27 by Robert Collins
Tweak storage towards mergability.
499
    @needs_read_lock
1534.6.3 by Robert Collins
find_repository sufficiently robust.
500
    def is_shared(self):
501
        """Return True if this repository is flagged as a shared repository."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
502
        raise NotImplementedError(self.is_shared)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
503
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
504
    @needs_write_lock
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
505
    def reconcile(self, other=None, thorough=False):
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
506
        """Reconcile this repository."""
507
        from bzrlib.reconcile import RepoReconciler
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
508
        reconciler = RepoReconciler(self, thorough=thorough)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
509
        reconciler.reconcile()
510
        return reconciler
511
    
1534.6.3 by Robert Collins
find_repository sufficiently robust.
512
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
513
    def revision_tree(self, revision_id):
514
        """Return Tree for a revision on this branch.
515
516
        `revision_id` may be None for the null revision, in which case
517
        an `EmptyTree` is returned."""
518
        # TODO: refactor this to use an existing revision object
519
        # so we don't need to read it in twice.
1534.4.28 by Robert Collins
first cut at merge from integration.
520
        if revision_id is None or revision_id == NULL_REVISION:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
521
            return EmptyTree()
522
        else:
523
            inv = self.get_revision_inventory(revision_id)
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
524
            return RevisionTree(self, inv, revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
525
1185.65.27 by Robert Collins
Tweak storage towards mergability.
526
    @needs_read_lock
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
527
    def get_ancestry(self, revision_id):
528
        """Return a list of revision-ids integrated by a revision.
529
        
530
        This is topologically sorted.
531
        """
532
        if revision_id is None:
533
            return [None]
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
534
        if not self.has_revision(revision_id):
535
            raise errors.NoSuchRevision(self, revision_id)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
536
        w = self.get_inventory_weave()
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
537
        candidates = w.get_ancestry(revision_id)
538
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
539
1185.65.4 by Aaron Bentley
Fixed cat command
540
    @needs_read_lock
541
    def print_file(self, file, revision_id):
1185.65.29 by Robert Collins
Implement final review suggestions.
542
        """Print `file` to stdout.
543
        
544
        FIXME RBC 20060125 as John Meinel points out this is a bad api
545
        - it writes to stdout, it assumes that that is valid etc. Fix
546
        by creating a new more flexible convenience function.
547
        """
1185.65.4 by Aaron Bentley
Fixed cat command
548
        tree = self.revision_tree(revision_id)
549
        # use inventory as it was in that revision
550
        file_id = tree.inventory.path2id(file)
551
        if not file_id:
1685.1.26 by John Arbash Meinel
Repository had a bug with what exception was raised when a file was missing
552
            # TODO: jam 20060427 Write a test for this code path
553
            #       it had a bug in it, and was raising the wrong
554
            #       exception.
555
            raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
1185.65.4 by Aaron Bentley
Fixed cat command
556
        tree.print_file(file_id)
557
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
558
    def get_transaction(self):
559
        return self.control_files.get_transaction()
560
1590.1.1 by Robert Collins
Improve common_ancestor performance.
561
    def revision_parents(self, revid):
562
        return self.get_inventory_weave().parent_names(revid)
563
1185.65.27 by Robert Collins
Tweak storage towards mergability.
564
    @needs_write_lock
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
565
    def set_make_working_trees(self, new_value):
566
        """Set the policy flag for making working trees when creating branches.
567
568
        This only applies to branches that use this repository.
569
570
        The default is 'True'.
571
        :param new_value: True to restore the default, False to disable making
572
                          working trees.
573
        """
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
574
        raise NotImplementedError(self.set_make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
575
    
576
    def make_working_trees(self):
577
        """Returns the policy for making working trees on new branches."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
578
        raise NotImplementedError(self.make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
579
580
    @needs_write_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
581
    def sign_revision(self, revision_id, gpg_strategy):
582
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
583
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
584
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
585
    @needs_read_lock
586
    def has_signature_for_revision_id(self, revision_id):
587
        """Query for a revision signature for revision_id in the repository."""
588
        return self._revision_store.has_signature(revision_id,
589
                                                  self.get_transaction())
590
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
591
    @needs_read_lock
592
    def get_signature_text(self, revision_id):
593
        """Return the text for a signature."""
594
        return self._revision_store.get_signature_text(revision_id,
595
                                                       self.get_transaction())
596
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
597
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
598
class AllInOneRepository(Repository):
599
    """Legacy support - the repository behaviour for all-in-one branches."""
600
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
601
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
602
        # we reuse one control files instance.
603
        dir_mode = a_bzrdir._control_files._dir_mode
604
        file_mode = a_bzrdir._control_files._file_mode
605
606
        def get_store(name, compressed=True, prefixed=False):
607
            # FIXME: This approach of assuming stores are all entirely compressed
608
            # or entirely uncompressed is tidy, but breaks upgrade from 
609
            # some existing branches where there's a mixture; we probably 
610
            # still want the option to look for both.
611
            relpath = a_bzrdir._control_files._escape(name)
612
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
613
                              prefixed=prefixed, compressed=compressed,
614
                              dir_mode=dir_mode,
615
                              file_mode=file_mode)
616
            #if self._transport.should_cache():
617
            #    cache_path = os.path.join(self.cache_root, name)
618
            #    os.mkdir(cache_path)
619
            #    store = bzrlib.store.CachedStore(store, cache_path)
620
            return store
621
622
        # not broken out yet because the controlweaves|inventory_store
623
        # and text_store | weave_store bits are still different.
624
        if isinstance(_format, RepositoryFormat4):
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
625
            # cannot remove these - there is still no consistent api 
626
            # which allows access to this old info.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
627
            self.inventory_store = get_store('inventory-store')
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
628
            text_store = get_store('text-store')
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
629
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
630
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
631
    @needs_read_lock
632
    def is_shared(self):
633
        """AllInOne repositories cannot be shared."""
634
        return False
635
636
    @needs_write_lock
637
    def set_make_working_trees(self, new_value):
638
        """Set the policy flag for making working trees when creating branches.
639
640
        This only applies to branches that use this repository.
641
642
        The default is 'True'.
643
        :param new_value: True to restore the default, False to disable making
644
                          working trees.
645
        """
646
        raise NotImplementedError(self.set_make_working_trees)
647
    
648
    def make_working_trees(self):
649
        """Returns the policy for making working trees on new branches."""
650
        return True
651
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
652
1185.82.84 by Aaron Bentley
Moved stuff around
653
def install_revision(repository, rev, revision_tree):
654
    """Install all revision data into a repository."""
655
    present_parents = []
656
    parent_trees = {}
657
    for p_id in rev.parent_ids:
658
        if repository.has_revision(p_id):
659
            present_parents.append(p_id)
660
            parent_trees[p_id] = repository.revision_tree(p_id)
661
        else:
662
            parent_trees[p_id] = EmptyTree()
663
664
    inv = revision_tree.inventory
665
    
666
    # Add the texts that are not already present
667
    for path, ie in inv.iter_entries():
668
        w = repository.weave_store.get_weave_or_empty(ie.file_id,
669
                repository.get_transaction())
670
        if ie.revision not in w:
671
            text_parents = []
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
672
            # FIXME: TODO: The following loop *may* be overlapping/duplicate
673
            # with inventoryEntry.find_previous_heads(). if it is, then there
674
            # is a latent bug here where the parents may have ancestors of each
675
            # other. RBC, AB
1185.82.84 by Aaron Bentley
Moved stuff around
676
            for revision, tree in parent_trees.iteritems():
677
                if ie.file_id not in tree:
678
                    continue
679
                parent_id = tree.inventory[ie.file_id].revision
680
                if parent_id in text_parents:
681
                    continue
682
                text_parents.append(parent_id)
683
                    
684
            vfile = repository.weave_store.get_weave_or_empty(ie.file_id, 
685
                repository.get_transaction())
686
            lines = revision_tree.get_file(ie.file_id).readlines()
687
            vfile.add_lines(rev.revision_id, text_parents, lines)
688
    try:
689
        # install the inventory
690
        repository.add_inventory(rev.revision_id, inv, present_parents)
691
    except errors.RevisionAlreadyPresent:
692
        pass
693
    repository.add_revision(rev.revision_id, rev, inv)
694
695
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
696
class MetaDirRepository(Repository):
697
    """Repositories in the new meta-dir layout."""
698
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
699
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
700
        super(MetaDirRepository, self).__init__(_format,
701
                                                a_bzrdir,
702
                                                control_files,
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
703
                                                _revision_store,
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
704
                                                control_store,
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
705
                                                text_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
706
707
        dir_mode = self.control_files._dir_mode
708
        file_mode = self.control_files._file_mode
709
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
710
    @needs_read_lock
711
    def is_shared(self):
712
        """Return True if this repository is flagged as a shared repository."""
713
        return self.control_files._transport.has('shared-storage')
714
715
    @needs_write_lock
716
    def set_make_working_trees(self, new_value):
717
        """Set the policy flag for making working trees when creating branches.
718
719
        This only applies to branches that use this repository.
720
721
        The default is 'True'.
722
        :param new_value: True to restore the default, False to disable making
723
                          working trees.
724
        """
725
        if new_value:
726
            try:
727
                self.control_files._transport.delete('no-working-trees')
728
            except errors.NoSuchFile:
729
                pass
730
        else:
731
            self.control_files.put_utf8('no-working-trees', '')
732
    
733
    def make_working_trees(self):
734
        """Returns the policy for making working trees on new branches."""
735
        return not self.control_files._transport.has('no-working-trees')
736
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
737
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
738
class KnitRepository(MetaDirRepository):
739
    """Knit format repository."""
740
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
741
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
742
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
743
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
744
    @needs_read_lock
745
    def all_revision_ids(self):
746
        """See Repository.all_revision_ids()."""
747
        return self._revision_store.all_revision_ids(self.get_transaction())
748
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
749
    def fileid_involved_between_revs(self, from_revid, to_revid):
750
        """Find file_id(s) which are involved in the changes between revisions.
751
752
        This determines the set of revisions which are involved, and then
753
        finds all file ids affected by those revisions.
754
        """
755
        vf = self._get_revision_vf()
756
        from_set = set(vf.get_ancestry(from_revid))
757
        to_set = set(vf.get_ancestry(to_revid))
758
        changed = to_set.difference(from_set)
759
        return self._fileid_involved_by_set(changed)
760
761
    def fileid_involved(self, last_revid=None):
762
        """Find all file_ids modified in the ancestry of last_revid.
763
764
        :param last_revid: If None, last_revision() will be used.
765
        """
766
        if not last_revid:
767
            changed = set(self.all_revision_ids())
768
        else:
769
            changed = set(self.get_ancestry(last_revid))
770
        if None in changed:
771
            changed.remove(None)
772
        return self._fileid_involved_by_set(changed)
773
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
774
    @needs_read_lock
775
    def get_ancestry(self, revision_id):
776
        """Return a list of revision-ids integrated by a revision.
777
        
778
        This is topologically sorted.
779
        """
780
        if revision_id is None:
781
            return [None]
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
782
        vf = self._get_revision_vf()
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
783
        try:
784
            return [None] + vf.get_ancestry(revision_id)
785
        except errors.RevisionNotPresent:
786
            raise errors.NoSuchRevision(self, revision_id)
787
788
    @needs_read_lock
1594.2.10 by Robert Collins
Teach knit fetching and branching to only duplicate relevant data avoiding unnecessary reconciles.
789
    def get_revision(self, revision_id):
790
        """Return the Revision object for a named revision"""
791
        return self.get_revision_reconcile(revision_id)
792
793
    @needs_read_lock
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
794
    def get_revision_graph(self, revision_id=None):
795
        """Return a dictionary containing the revision graph.
796
        
797
        :return: a dictionary of revision_id->revision_parents_list.
798
        """
799
        weave = self._get_revision_vf()
800
        entire_graph = weave.get_graph()
801
        if revision_id is None:
802
            return weave.get_graph()
803
        elif revision_id not in weave:
804
            raise errors.NoSuchRevision(self, revision_id)
805
        else:
806
            # add what can be reached from revision_id
807
            result = {}
808
            pending = set([revision_id])
809
            while len(pending) > 0:
810
                node = pending.pop()
811
                result[node] = weave.get_parents(node)
812
                for revision_id in result[node]:
813
                    if revision_id not in result:
814
                        pending.add(revision_id)
815
            return result
816
817
    @needs_read_lock
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
818
    def get_revision_graph_with_ghosts(self, revision_ids=None):
819
        """Return a graph of the revisions with ghosts marked as applicable.
820
821
        :param revision_ids: an iterable of revisions to graph or None for all.
822
        :return: a Graph object with the graph reachable from revision_ids.
823
        """
824
        result = Graph()
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
825
        vf = self._get_revision_vf()
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
826
        versions = set(vf.versions())
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
827
        if not revision_ids:
828
            pending = set(self.all_revision_ids())
829
            required = set([])
830
        else:
831
            pending = set(revision_ids)
832
            required = set(revision_ids)
833
        done = set([])
834
        while len(pending):
835
            revision_id = pending.pop()
836
            if not revision_id in versions:
837
                if revision_id in required:
838
                    raise errors.NoSuchRevision(self, revision_id)
839
                # a ghost
840
                result.add_ghost(revision_id)
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
841
                # mark it as done so we dont try for it again.
842
                done.add(revision_id)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
843
                continue
844
            parent_ids = vf.get_parents_with_ghosts(revision_id)
845
            for parent_id in parent_ids:
846
                # is this queued or done ?
847
                if (parent_id not in pending and
848
                    parent_id not in done):
849
                    # no, queue it.
850
                    pending.add(parent_id)
851
            result.add_node(revision_id, parent_ids)
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
852
            done.add(revision_id)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
853
        return result
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
854
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
855
    def _get_revision_vf(self):
1607.1.2 by Robert Collins
Merge in knit-using-revision-versioned-file-graph tuning work.
856
        """:return: a versioned file containing the revisions."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
857
        vf = self._revision_store.get_revision_file(self.get_transaction())
858
        return vf
859
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
860
    @needs_write_lock
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
861
    def reconcile(self, other=None, thorough=False):
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
862
        """Reconcile this repository."""
863
        from bzrlib.reconcile import KnitReconciler
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
864
        reconciler = KnitReconciler(self, thorough=thorough)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
865
        reconciler.reconcile()
866
        return reconciler
867
    
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
868
    def revision_parents(self, revid):
869
        return self._get_revision_vf().get_parents(rev_id)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
870
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
871
class RepositoryFormat(object):
872
    """A repository format.
873
874
    Formats provide three things:
875
     * An initialization routine to construct repository data on disk.
876
     * a format string which is used when the BzrDir supports versioned
877
       children.
878
     * an open routine which returns a Repository instance.
879
880
    Formats are placed in an dict by their format string for reference 
881
    during opening. These should be subclasses of RepositoryFormat
882
    for consistency.
883
884
    Once a format is deprecated, just deprecate the initialize and open
885
    methods on the format class. Do not deprecate the object, as the 
886
    object will be created every system load.
887
888
    Common instance attributes:
889
    _matchingbzrdir - the bzrdir format that the repository format was
890
    originally written to work with. This can be used if manually
891
    constructing a bzrdir and repository, or more commonly for test suite
892
    parameterisation.
893
    """
894
895
    _default_format = None
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
896
    """The default format used for new repositories."""
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
897
898
    _formats = {}
899
    """The known formats."""
900
901
    @classmethod
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
902
    def find_format(klass, a_bzrdir):
903
        """Return the format for the repository object in a_bzrdir."""
904
        try:
905
            transport = a_bzrdir.get_repository_transport(None)
906
            format_string = transport.get("format").read()
907
            return klass._formats[format_string]
908
        except errors.NoSuchFile:
909
            raise errors.NoRepositoryPresent(a_bzrdir)
910
        except KeyError:
911
            raise errors.UnknownFormatError(format_string)
912
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
913
    def _get_control_store(self, repo_transport, control_files):
914
        """Return the control store for this repository."""
915
        raise NotImplementedError(self._get_control_store)
916
    
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
917
    @classmethod
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
918
    def get_default_format(klass):
919
        """Return the current default format."""
920
        return klass._default_format
921
922
    def get_format_string(self):
923
        """Return the ASCII format string that identifies this format.
924
        
925
        Note that in pre format ?? repositories the format string is 
926
        not permitted nor written to disk.
927
        """
928
        raise NotImplementedError(self.get_format_string)
929
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
930
    def get_format_description(self):
931
        """Return the short desciption for this format."""
932
        raise NotImplementedError(self.get_format_description)
933
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
934
    def _get_revision_store(self, repo_transport, control_files):
935
        """Return the revision store object for this a_bzrdir."""
1556.1.5 by Robert Collins
Review feedback.
936
        raise NotImplementedError(self._get_revision_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
937
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
938
    def _get_text_rev_store(self,
939
                            transport,
940
                            control_files,
941
                            name,
942
                            compressed=True,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
943
                            prefixed=False,
944
                            serializer=None):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
945
        """Common logic for getting a revision store for a repository.
946
        
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
947
        see self._get_revision_store for the subclass-overridable method to 
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
948
        get the store for a repository.
949
        """
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
950
        from bzrlib.store.revision.text import TextRevisionStore
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
951
        dir_mode = control_files._dir_mode
952
        file_mode = control_files._file_mode
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
953
        text_store =TextStore(transport.clone(name),
954
                              prefixed=prefixed,
955
                              compressed=compressed,
956
                              dir_mode=dir_mode,
957
                              file_mode=file_mode)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
958
        _revision_store = TextRevisionStore(text_store, serializer)
959
        return _revision_store
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
960
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
961
    def _get_versioned_file_store(self,
962
                                  name,
963
                                  transport,
964
                                  control_files,
965
                                  prefixed=True,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
966
                                  versionedfile_class=WeaveFile,
967
                                  escaped=False):
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
968
        weave_transport = control_files._transport.clone(name)
969
        dir_mode = control_files._dir_mode
970
        file_mode = control_files._file_mode
971
        return VersionedFileStore(weave_transport, prefixed=prefixed,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
972
                                  dir_mode=dir_mode,
973
                                  file_mode=file_mode,
974
                                  versionedfile_class=versionedfile_class,
975
                                  escaped=escaped)
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
976
1534.6.1 by Robert Collins
allow API creation of shared repositories
977
    def initialize(self, a_bzrdir, shared=False):
978
        """Initialize a repository of this format in a_bzrdir.
979
980
        :param a_bzrdir: The bzrdir to put the new repository in it.
981
        :param shared: The repository should be initialized as a sharable one.
982
983
        This may raise UninitializableFormat if shared repository are not
984
        compatible the a_bzrdir.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
985
        """
986
987
    def is_supported(self):
988
        """Is this format supported?
989
990
        Supported formats must be initializable and openable.
991
        Unsupported formats may not support initialization or committing or 
992
        some other features depending on the reason for not being supported.
993
        """
994
        return True
995
996
    def open(self, a_bzrdir, _found=False):
997
        """Return an instance of this format for the bzrdir a_bzrdir.
998
        
999
        _found is a private parameter, do not use it.
1000
        """
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1001
        raise NotImplementedError(self.open)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1002
1003
    @classmethod
1004
    def register_format(klass, format):
1005
        klass._formats[format.get_format_string()] = format
1006
1007
    @classmethod
1008
    def set_default_format(klass, format):
1009
        klass._default_format = format
1010
1011
    @classmethod
1012
    def unregister_format(klass, format):
1013
        assert klass._formats[format.get_format_string()] is format
1014
        del klass._formats[format.get_format_string()]
1015
1016
1534.6.1 by Robert Collins
allow API creation of shared repositories
1017
class PreSplitOutRepositoryFormat(RepositoryFormat):
1018
    """Base class for the pre split out repository formats."""
1019
1020
    def initialize(self, a_bzrdir, shared=False, _internal=False):
1021
        """Create a weave repository.
1022
        
1023
        TODO: when creating split out bzr branch formats, move this to a common
1024
        base for Format5, Format6. or something like that.
1025
        """
1026
        from bzrlib.weavefile import write_weave_v5
1027
        from bzrlib.weave import Weave
1028
1029
        if shared:
1030
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
1031
1032
        if not _internal:
1033
            # always initialized when the bzrdir is.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1034
            return self.open(a_bzrdir, _found=True)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1035
        
1036
        # Create an empty weave
1037
        sio = StringIO()
1038
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
1039
        empty_weave = sio.getvalue()
1040
1041
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1042
        dirs = ['revision-store', 'weaves']
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
1043
        files = [('inventory.weave', StringIO(empty_weave)),
1534.6.1 by Robert Collins
allow API creation of shared repositories
1044
                 ]
1045
        
1046
        # FIXME: RBC 20060125 dont peek under the covers
1047
        # NB: no need to escape relative paths that are url safe.
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
1048
        control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
1049
                                      TransportLock)
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
1050
        control_files.create_lock()
1534.6.1 by Robert Collins
allow API creation of shared repositories
1051
        control_files.lock_write()
1052
        control_files._transport.mkdir_multi(dirs,
1053
                mode=control_files._dir_mode)
1054
        try:
1055
            for file, content in files:
1056
                control_files.put(file, content)
1057
        finally:
1058
            control_files.unlock()
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1059
        return self.open(a_bzrdir, _found=True)
1060
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1061
    def _get_control_store(self, repo_transport, control_files):
1062
        """Return the control store for this repository."""
1063
        return self._get_versioned_file_store('',
1064
                                              repo_transport,
1065
                                              control_files,
1066
                                              prefixed=False)
1067
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1068
    def _get_text_store(self, transport, control_files):
1069
        """Get a store for file texts for this format."""
1070
        raise NotImplementedError(self._get_text_store)
1071
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1072
    def open(self, a_bzrdir, _found=False):
1073
        """See RepositoryFormat.open()."""
1074
        if not _found:
1075
            # we are being called directly and must probe.
1076
            raise NotImplementedError
1077
1078
        repo_transport = a_bzrdir.get_repository_transport(None)
1079
        control_files = a_bzrdir._control_files
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1080
        text_store = self._get_text_store(repo_transport, control_files)
1081
        control_store = self._get_control_store(repo_transport, control_files)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1082
        _revision_store = self._get_revision_store(repo_transport, control_files)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1083
        return AllInOneRepository(_format=self,
1084
                                  a_bzrdir=a_bzrdir,
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1085
                                  _revision_store=_revision_store,
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1086
                                  control_store=control_store,
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1087
                                  text_store=text_store)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1088
1089
1090
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1091
    """Bzr repository format 4.
1092
1093
    This repository format has:
1094
     - flat stores
1095
     - TextStores for texts, inventories,revisions.
1096
1097
    This format is deprecated: it indexes texts using a text id which is
1098
    removed in format 5; initializationa and write support for this format
1099
    has been removed.
1100
    """
1101
1102
    def __init__(self):
1103
        super(RepositoryFormat4, self).__init__()
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.
1104
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat4()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1105
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1106
    def get_format_description(self):
1107
        """See RepositoryFormat.get_format_description()."""
1108
        return "Repository format 4"
1109
1534.6.1 by Robert Collins
allow API creation of shared repositories
1110
    def initialize(self, url, shared=False, _internal=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1111
        """Format 4 branches cannot be created."""
1112
        raise errors.UninitializableFormat(self)
1113
1114
    def is_supported(self):
1115
        """Format 4 is not supported.
1116
1117
        It is not supported because the model changed from 4 to 5 and the
1118
        conversion logic is expensive - so doing it on the fly was not 
1119
        feasible.
1120
        """
1121
        return False
1122
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1123
    def _get_control_store(self, repo_transport, control_files):
1124
        """Format 4 repositories have no formal control store at this point.
1125
        
1126
        This will cause any control-file-needing apis to fail - this is desired.
1127
        """
1128
        return None
1129
    
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1130
    def _get_revision_store(self, repo_transport, control_files):
1131
        """See RepositoryFormat._get_revision_store()."""
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1132
        from bzrlib.xml4 import serializer_v4
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1133
        return self._get_text_rev_store(repo_transport,
1134
                                        control_files,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1135
                                        'revision-store',
1136
                                        serializer=serializer_v4)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1137
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1138
    def _get_text_store(self, transport, control_files):
1139
        """See RepositoryFormat._get_text_store()."""
1140
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1141
1534.6.1 by Robert Collins
allow API creation of shared repositories
1142
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1143
    """Bzr control format 5.
1144
1145
    This repository format has:
1146
     - weaves for file texts and inventory
1147
     - flat stores
1148
     - TextStores for revisions and signatures.
1149
    """
1150
1151
    def __init__(self):
1152
        super(RepositoryFormat5, self).__init__()
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.
1153
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat5()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1154
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1155
    def get_format_description(self):
1156
        """See RepositoryFormat.get_format_description()."""
1157
        return "Weave repository format 5"
1158
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1159
    def _get_revision_store(self, repo_transport, control_files):
1160
        """See RepositoryFormat._get_revision_store()."""
1161
        """Return the revision store object for this a_bzrdir."""
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1162
        return self._get_text_rev_store(repo_transport,
1163
                                        control_files,
1164
                                        'revision-store',
1165
                                        compressed=False)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1166
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1167
    def _get_text_store(self, transport, control_files):
1168
        """See RepositoryFormat._get_text_store()."""
1169
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1170
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1171
1534.6.1 by Robert Collins
allow API creation of shared repositories
1172
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1173
    """Bzr control format 6.
1174
1175
    This repository format has:
1176
     - weaves for file texts and inventory
1177
     - hash subdirectory based stores.
1178
     - TextStores for revisions and signatures.
1179
    """
1180
1181
    def __init__(self):
1182
        super(RepositoryFormat6, self).__init__()
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.
1183
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat6()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1184
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1185
    def get_format_description(self):
1186
        """See RepositoryFormat.get_format_description()."""
1187
        return "Weave repository format 6"
1188
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1189
    def _get_revision_store(self, repo_transport, control_files):
1190
        """See RepositoryFormat._get_revision_store()."""
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1191
        return self._get_text_rev_store(repo_transport,
1192
                                        control_files,
1193
                                        'revision-store',
1194
                                        compressed=False,
1195
                                        prefixed=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1196
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1197
    def _get_text_store(self, transport, control_files):
1198
        """See RepositoryFormat._get_text_store()."""
1199
        return self._get_versioned_file_store('weaves', transport, control_files)
1200
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1201
1202
class MetaDirRepositoryFormat(RepositoryFormat):
1203
    """Common base class for the new repositories using the metadir layour."""
1204
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.
1205
    def __init__(self):
1206
        super(MetaDirRepositoryFormat, self).__init__()
1207
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirMetaFormat1()
1208
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1209
    def _create_control_files(self, a_bzrdir):
1210
        """Create the required files and the initial control_files object."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1211
        # FIXME: RBC 20060125 dont peek under the covers
1212
        # NB: no need to escape relative paths that are url safe.
1213
        repository_transport = a_bzrdir.get_repository_transport(self)
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
1214
        control_files = LockableFiles(repository_transport, 'lock', LockDir)
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
1215
        control_files.create_lock()
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1216
        return control_files
1217
1218
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1219
        """Upload the initial blank content."""
1220
        control_files = self._create_control_files(a_bzrdir)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1221
        control_files.lock_write()
1222
        try:
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
1223
            control_files._transport.mkdir_multi(dirs,
1224
                    mode=control_files._dir_mode)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1225
            for file, content in files:
1226
                control_files.put(file, content)
1227
            for file, content in utf8_files:
1228
                control_files.put_utf8(file, content)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1229
            if shared == True:
1230
                control_files.put_utf8('shared-storage', '')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1231
        finally:
1232
            control_files.unlock()
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1233
1234
1235
class RepositoryFormat7(MetaDirRepositoryFormat):
1236
    """Bzr repository 7.
1237
1238
    This repository format has:
1239
     - weaves for file texts and inventory
1240
     - hash subdirectory based stores.
1241
     - TextStores for revisions and signatures.
1242
     - a format marker of its own
1243
     - an optional 'shared-storage' flag
1244
     - an optional 'no-working-trees' flag
1245
    """
1246
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1247
    def _get_control_store(self, repo_transport, control_files):
1248
        """Return the control store for this repository."""
1249
        return self._get_versioned_file_store('',
1250
                                              repo_transport,
1251
                                              control_files,
1252
                                              prefixed=False)
1253
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1254
    def get_format_string(self):
1255
        """See RepositoryFormat.get_format_string()."""
1256
        return "Bazaar-NG Repository format 7"
1257
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1258
    def get_format_description(self):
1259
        """See RepositoryFormat.get_format_description()."""
1260
        return "Weave repository format 7"
1261
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1262
    def _get_revision_store(self, repo_transport, control_files):
1263
        """See RepositoryFormat._get_revision_store()."""
1264
        return self._get_text_rev_store(repo_transport,
1265
                                        control_files,
1266
                                        'revision-store',
1267
                                        compressed=False,
1268
                                        prefixed=True,
1269
                                        )
1270
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1271
    def _get_text_store(self, transport, control_files):
1272
        """See RepositoryFormat._get_text_store()."""
1273
        return self._get_versioned_file_store('weaves',
1274
                                              transport,
1275
                                              control_files)
1276
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1277
    def initialize(self, a_bzrdir, shared=False):
1278
        """Create a weave repository.
1279
1280
        :param shared: If true the repository will be initialized as a shared
1281
                       repository.
1282
        """
1283
        from bzrlib.weavefile import write_weave_v5
1284
        from bzrlib.weave import Weave
1285
1286
        # Create an empty weave
1287
        sio = StringIO()
1288
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
1289
        empty_weave = sio.getvalue()
1290
1291
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1292
        dirs = ['revision-store', 'weaves']
1293
        files = [('inventory.weave', StringIO(empty_weave)), 
1294
                 ]
1295
        utf8_files = [('format', self.get_format_string())]
1296
 
1297
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1298
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1299
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1300
    def open(self, a_bzrdir, _found=False, _override_transport=None):
1301
        """See RepositoryFormat.open().
1302
        
1303
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
1304
                                    repository at a slightly different url
1305
                                    than normal. I.e. during 'upgrade'.
1306
        """
1307
        if not _found:
1308
            format = RepositoryFormat.find_format(a_bzrdir)
1309
            assert format.__class__ ==  self.__class__
1310
        if _override_transport is not None:
1311
            repo_transport = _override_transport
1312
        else:
1313
            repo_transport = a_bzrdir.get_repository_transport(None)
1563.2.36 by Robert Collins
Merge in bzr.dev
1314
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1315
        text_store = self._get_text_store(repo_transport, control_files)
1316
        control_store = self._get_control_store(repo_transport, control_files)
1317
        _revision_store = self._get_revision_store(repo_transport, control_files)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1318
        return MetaDirRepository(_format=self,
1319
                                 a_bzrdir=a_bzrdir,
1320
                                 control_files=control_files,
1321
                                 _revision_store=_revision_store,
1322
                                 control_store=control_store,
1323
                                 text_store=text_store)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1324
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1325
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1326
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1327
    """Bzr repository knit format 1.
1328
1329
    This repository format has:
1330
     - knits for file texts and inventory
1331
     - hash subdirectory based stores.
1332
     - knits for revisions and signatures
1333
     - TextStores for revisions and signatures.
1334
     - a format marker of its own
1335
     - an optional 'shared-storage' flag
1336
     - an optional 'no-working-trees' flag
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
1337
     - a LockDir lock
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1338
1339
    This format was introduced in bzr 0.8.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1340
    """
1341
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1342
    def _get_control_store(self, repo_transport, control_files):
1343
        """Return the control store for this repository."""
1628.1.5 by Robert Collins
Make inventory knits not annotated, only delta compressed.
1344
        return VersionedFileStore(
1345
            repo_transport,
1346
            prefixed=False,
1347
            file_mode=control_files._file_mode,
1348
            versionedfile_class=KnitVersionedFile,
1349
            versionedfile_kwargs={'factory':KnitPlainFactory()},
1350
            )
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1351
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1352
    def get_format_string(self):
1353
        """See RepositoryFormat.get_format_string()."""
1354
        return "Bazaar-NG Knit Repository Format 1"
1355
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1356
    def get_format_description(self):
1357
        """See RepositoryFormat.get_format_description()."""
1358
        return "Knit repository format 1"
1359
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1360
    def _get_revision_store(self, repo_transport, control_files):
1361
        """See RepositoryFormat._get_revision_store()."""
1362
        from bzrlib.store.revision.knit import KnitRevisionStore
1363
        versioned_file_store = VersionedFileStore(
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1364
            repo_transport,
1651.1.1 by Martin Pool
[merge][wip] Storage escaping
1365
            file_mode=control_files._file_mode,
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1366
            prefixed=False,
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1367
            precious=True,
1628.1.4 by Robert Collins
Change knit format to use non-delta, non-annotated revisions and signatures.
1368
            versionedfile_class=KnitVersionedFile,
1651.1.1 by Martin Pool
[merge][wip] Storage escaping
1369
            versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1370
            escaped=True,
1651.1.1 by Martin Pool
[merge][wip] Storage escaping
1371
            )
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1372
        return KnitRevisionStore(versioned_file_store)
1373
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1374
    def _get_text_store(self, transport, control_files):
1375
        """See RepositoryFormat._get_text_store()."""
1376
        return self._get_versioned_file_store('knits',
1377
                                              transport,
1378
                                              control_files,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1379
                                              versionedfile_class=KnitVersionedFile,
1380
                                              escaped=True)
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1381
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1382
    def initialize(self, a_bzrdir, shared=False):
1383
        """Create a knit format 1 repository.
1384
1658.1.7 by Martin Pool
(RepositoryFormatKnit1.initialize) remove dead code that constructs weaves
1385
        :param a_bzrdir: bzrdir to contain the new repository; must already
1386
            be initialized.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1387
        :param shared: If true the repository will be initialized as a shared
1388
                       repository.
1389
        """
1390
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1707.3.29 by John Arbash Meinel
reverting 1734
1391
        dirs = ['revision-store', 'knits']
1658.1.7 by Martin Pool
(RepositoryFormatKnit1.initialize) remove dead code that constructs weaves
1392
        files = []
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1393
        utf8_files = [('format', self.get_format_string())]
1394
        
1395
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1563.2.25 by Robert Collins
Merge in upstream.
1396
        repo_transport = a_bzrdir.get_repository_transport(None)
1563.2.36 by Robert Collins
Merge in bzr.dev
1397
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
1563.2.25 by Robert Collins
Merge in upstream.
1398
        control_store = self._get_control_store(repo_transport, control_files)
1594.2.20 by Robert Collins
Add finished() notifications to transactions.
1399
        transaction = bzrlib.transactions.WriteTransaction()
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1400
        # trigger a write of the inventory store.
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1401
        control_store.get_weave_or_empty('inventory', transaction)
1402
        _revision_store = self._get_revision_store(repo_transport, control_files)
1403
        _revision_store.has_revision_id('A', transaction)
1404
        _revision_store.get_signature_file(transaction)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1405
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1406
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1407
    def open(self, a_bzrdir, _found=False, _override_transport=None):
1408
        """See RepositoryFormat.open().
1409
        
1410
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
1411
                                    repository at a slightly different url
1412
                                    than normal. I.e. during 'upgrade'.
1413
        """
1414
        if not _found:
1415
            format = RepositoryFormat.find_format(a_bzrdir)
1416
            assert format.__class__ ==  self.__class__
1417
        if _override_transport is not None:
1418
            repo_transport = _override_transport
1419
        else:
1420
            repo_transport = a_bzrdir.get_repository_transport(None)
1563.2.36 by Robert Collins
Merge in bzr.dev
1421
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1422
        text_store = self._get_text_store(repo_transport, control_files)
1423
        control_store = self._get_control_store(repo_transport, control_files)
1424
        _revision_store = self._get_revision_store(repo_transport, control_files)
1425
        return KnitRepository(_format=self,
1426
                              a_bzrdir=a_bzrdir,
1427
                              control_files=control_files,
1428
                              _revision_store=_revision_store,
1429
                              control_store=control_store,
1430
                              text_store=text_store)
1431
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1432
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1433
# formats which have no format string are not discoverable
1434
# and not independently creatable, so are not registered.
1666.1.6 by Robert Collins
Make knit the default format.
1435
RepositoryFormat.register_format(RepositoryFormat7())
1436
_default_format = RepositoryFormatKnit1()
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.
1437
RepositoryFormat.register_format(_default_format)
1438
RepositoryFormat.set_default_format(_default_format)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1439
_legacy_formats = [RepositoryFormat4(),
1440
                   RepositoryFormat5(),
1441
                   RepositoryFormat6()]
1442
1443
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1444
class InterRepository(InterObject):
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1445
    """This class represents operations taking place between two repositories.
1446
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.
1447
    Its instances have methods like copy_content and fetch, and contain
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1448
    references to the source and target repositories these operations can be 
1449
    carried out on.
1450
1451
    Often we will provide convenience methods on 'repository' which carry out
1452
    operations with another repository - they will always forward to
1453
    InterRepository.get(other).method_name(parameters).
1454
    """
1455
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1456
    _optimisers = set()
1457
    """The available optimised InterRepository types."""
1458
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.
1459
    @needs_write_lock
1460
    def copy_content(self, revision_id=None, basis=None):
1461
        """Make a complete copy of the content in self into destination.
1462
        
1463
        This is a destructive operation! Do not use it on existing 
1464
        repositories.
1465
1466
        :param revision_id: Only copy the content needed to construct
1467
                            revision_id and its parents.
1468
        :param basis: Copy the needed data preferentially from basis.
1469
        """
1470
        try:
1471
            self.target.set_make_working_trees(self.source.make_working_trees())
1472
        except NotImplementedError:
1473
            pass
1474
        # grab the basis available data
1475
        if basis is not None:
1476
            self.target.fetch(basis, revision_id=revision_id)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1477
        # but dont bother fetching if we have the needed data now.
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.
1478
        if (revision_id not in (None, NULL_REVISION) and 
1479
            self.target.has_revision(revision_id)):
1480
            return
1481
        self.target.fetch(self.source, revision_id=revision_id)
1482
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1483
    def _double_lock(self, lock_source, lock_target):
1484
        """Take out too locks, rolling back the first if the second throws."""
1485
        lock_source()
1486
        try:
1487
            lock_target()
1488
        except Exception:
1489
            # we want to ensure that we don't leave source locked by mistake.
1490
            # and any error on target should not confuse source.
1491
            self.source.unlock()
1492
            raise
1493
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.
1494
    @needs_write_lock
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1495
    def fetch(self, revision_id=None, pb=None):
1496
        """Fetch the content required to construct revision_id.
1497
1498
        The content is copied from source to target.
1499
1500
        :param revision_id: if None all content is copied, if NULL_REVISION no
1501
                            content is copied.
1502
        :param pb: optional progress bar to use for progress reports. If not
1503
                   provided a default one will be created.
1504
1505
        Returns the copied revision count and the failed revisions in a tuple:
1506
        (copied, failures).
1507
        """
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1508
        from bzrlib.fetch import GenericRepoFetcher
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1509
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1510
               self.source, self.source._format, self.target, self.target._format)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1511
        f = GenericRepoFetcher(to_repository=self.target,
1512
                               from_repository=self.source,
1513
                               last_revision=revision_id,
1514
                               pb=pb)
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.
1515
        return f.count_copied, f.failed_revisions
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1516
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1517
    def lock_read(self):
1518
        """Take out a logical read lock.
1519
1520
        This will lock the source branch and the target branch. The source gets
1521
        a read lock and the target a read lock.
1522
        """
1523
        self._double_lock(self.source.lock_read, self.target.lock_read)
1524
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.
1525
    def lock_write(self):
1526
        """Take out a logical write lock.
1527
1528
        This will lock the source branch and the target branch. The source gets
1529
        a read lock and the target a write lock.
1530
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1531
        self._double_lock(self.source.lock_read, self.target.lock_write)
1532
1533
    @needs_read_lock
1534
    def missing_revision_ids(self, revision_id=None):
1535
        """Return the revision ids that source has that target does not.
1536
        
1537
        These are returned in topological order.
1538
1539
        :param revision_id: only return revision ids included by this
1540
                            revision_id.
1541
        """
1542
        # generic, possibly worst case, slow code path.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1543
        target_ids = set(self.target.all_revision_ids())
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1544
        if revision_id is not None:
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1545
            source_ids = self.source.get_ancestry(revision_id)
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
1546
            assert source_ids[0] == None
1547
            source_ids.pop(0)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1548
        else:
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1549
            source_ids = self.source.all_revision_ids()
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1550
        result_set = set(source_ids).difference(target_ids)
1551
        # this may look like a no-op: its not. It preserves the ordering
1552
        # other_ids had while only returning the members from other_ids
1553
        # that we've decided we need.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1554
        return [rev_id for rev_id in source_ids if rev_id in result_set]
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.
1555
1556
    def unlock(self):
1557
        """Release the locks on source and target."""
1558
        try:
1559
            self.target.unlock()
1560
        finally:
1561
            self.source.unlock()
1562
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1563
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1564
class InterWeaveRepo(InterRepository):
1565
    """Optimised code paths between Weave based repositories."""
1566
1666.1.6 by Robert Collins
Make knit the default format.
1567
    _matching_repo_format = RepositoryFormat7()
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.
1568
    """Repository format for testing with."""
1569
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1570
    @staticmethod
1571
    def is_compatible(source, target):
1572
        """Be compatible with known Weave formats.
1573
        
1574
        We dont test for the stores being of specific types becase that
1575
        could lead to confusing results, and there is no need to be 
1576
        overly general.
1577
        """
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.
1578
        try:
1579
            return (isinstance(source._format, (RepositoryFormat5,
1580
                                                RepositoryFormat6,
1581
                                                RepositoryFormat7)) and
1582
                    isinstance(target._format, (RepositoryFormat5,
1583
                                                RepositoryFormat6,
1584
                                                RepositoryFormat7)))
1585
        except AttributeError:
1586
            return False
1587
    
1588
    @needs_write_lock
1589
    def copy_content(self, revision_id=None, basis=None):
1590
        """See InterRepository.copy_content()."""
1591
        # weave specific optimised path:
1592
        if basis is not None:
1593
            # copy the basis in, then fetch remaining data.
1594
            basis.copy_content_into(self.target, revision_id)
1595
            # the basis copy_content_into could misset this.
1596
            try:
1597
                self.target.set_make_working_trees(self.source.make_working_trees())
1598
            except NotImplementedError:
1599
                pass
1600
            self.target.fetch(self.source, revision_id=revision_id)
1601
        else:
1602
            try:
1603
                self.target.set_make_working_trees(self.source.make_working_trees())
1604
            except NotImplementedError:
1605
                pass
1606
            # FIXME do not peek!
1607
            if self.source.control_files._transport.listable():
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1608
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
1609
                try:
1563.2.37 by Robert Collins
Merge in nested progress bars
1610
                    self.target.weave_store.copy_all_ids(
1611
                        self.source.weave_store,
1612
                        pb=pb,
1613
                        from_transaction=self.source.get_transaction(),
1614
                        to_transaction=self.target.get_transaction())
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1615
                    pb.update('copying inventory', 0, 1)
1616
                    self.target.control_weaves.copy_multi(
1563.2.37 by Robert Collins
Merge in nested progress bars
1617
                        self.source.control_weaves, ['inventory'],
1618
                        from_transaction=self.source.get_transaction(),
1619
                        to_transaction=self.target.get_transaction())
1620
                    self.target._revision_store.text_store.copy_all_ids(
1621
                        self.source._revision_store.text_store,
1622
                        pb=pb)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1623
                finally:
1624
                    pb.finished()
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.
1625
            else:
1626
                self.target.fetch(self.source, revision_id=revision_id)
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1627
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.
1628
    @needs_write_lock
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1629
    def fetch(self, revision_id=None, pb=None):
1630
        """See InterRepository.fetch()."""
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1631
        from bzrlib.fetch import GenericRepoFetcher
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1632
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1633
               self.source, self.source._format, self.target, self.target._format)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1634
        f = GenericRepoFetcher(to_repository=self.target,
1635
                               from_repository=self.source,
1636
                               last_revision=revision_id,
1637
                               pb=pb)
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.
1638
        return f.count_copied, f.failed_revisions
1639
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1640
    @needs_read_lock
1641
    def missing_revision_ids(self, revision_id=None):
1642
        """See InterRepository.missing_revision_ids()."""
1643
        # we want all revisions to satisfy revision_id in source.
1644
        # but we dont want to stat every file here and there.
1645
        # we want then, all revisions other needs to satisfy revision_id 
1646
        # checked, but not those that we have locally.
1647
        # so the first thing is to get a subset of the revisions to 
1648
        # satisfy revision_id in source, and then eliminate those that
1649
        # we do already have. 
1650
        # this is slow on high latency connection to self, but as as this
1651
        # disk format scales terribly for push anyway due to rewriting 
1652
        # inventory.weave, this is considered acceptable.
1653
        # - RBC 20060209
1654
        if revision_id is not None:
1655
            source_ids = self.source.get_ancestry(revision_id)
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
1656
            assert source_ids[0] == None
1657
            source_ids.pop(0)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1658
        else:
1659
            source_ids = self.source._all_possible_ids()
1660
        source_ids_set = set(source_ids)
1661
        # source_ids is the worst possible case we may need to pull.
1662
        # now we want to filter source_ids against what we actually
1663
        # have in target, but dont try to check for existence where we know
1664
        # we do not have a revision as that would be pointless.
1665
        target_ids = set(self.target._all_possible_ids())
1666
        possibly_present_revisions = target_ids.intersection(source_ids_set)
1667
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1668
        required_revisions = source_ids_set.difference(actually_present_revisions)
1669
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1670
        if revision_id is not None:
1671
            # we used get_ancestry to determine source_ids then we are assured all
1672
            # revisions referenced are present as they are installed in topological order.
1673
            # and the tip revision was validated by get_ancestry.
1674
            return required_topo_revisions
1675
        else:
1676
            # if we just grabbed the possibly available ids, then 
1677
            # we only have an estimate of whats available and need to validate
1678
            # that against the revision records.
1679
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
1680
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.
1681
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1682
class InterKnitRepo(InterRepository):
1683
    """Optimised code paths between Knit based repositories."""
1684
1685
    _matching_repo_format = RepositoryFormatKnit1()
1686
    """Repository format for testing with."""
1687
1688
    @staticmethod
1689
    def is_compatible(source, target):
1690
        """Be compatible with known Knit formats.
1691
        
1692
        We dont test for the stores being of specific types becase that
1693
        could lead to confusing results, and there is no need to be 
1694
        overly general.
1695
        """
1696
        try:
1697
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
1698
                    isinstance(target._format, (RepositoryFormatKnit1)))
1699
        except AttributeError:
1700
            return False
1701
1702
    @needs_write_lock
1703
    def fetch(self, revision_id=None, pb=None):
1704
        """See InterRepository.fetch()."""
1705
        from bzrlib.fetch import KnitRepoFetcher
1706
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1707
               self.source, self.source._format, self.target, self.target._format)
1708
        f = KnitRepoFetcher(to_repository=self.target,
1709
                            from_repository=self.source,
1710
                            last_revision=revision_id,
1711
                            pb=pb)
1712
        return f.count_copied, f.failed_revisions
1713
1714
    @needs_read_lock
1715
    def missing_revision_ids(self, revision_id=None):
1716
        """See InterRepository.missing_revision_ids()."""
1717
        if revision_id is not None:
1718
            source_ids = self.source.get_ancestry(revision_id)
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
1719
            assert source_ids[0] == None
1720
            source_ids.pop(0)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1721
        else:
1722
            source_ids = self.source._all_possible_ids()
1723
        source_ids_set = set(source_ids)
1724
        # source_ids is the worst possible case we may need to pull.
1725
        # now we want to filter source_ids against what we actually
1726
        # have in target, but dont try to check for existence where we know
1727
        # we do not have a revision as that would be pointless.
1728
        target_ids = set(self.target._all_possible_ids())
1729
        possibly_present_revisions = target_ids.intersection(source_ids_set)
1730
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1731
        required_revisions = source_ids_set.difference(actually_present_revisions)
1732
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1733
        if revision_id is not None:
1734
            # we used get_ancestry to determine source_ids then we are assured all
1735
            # revisions referenced are present as they are installed in topological order.
1736
            # and the tip revision was validated by get_ancestry.
1737
            return required_topo_revisions
1738
        else:
1739
            # if we just grabbed the possibly available ids, then 
1740
            # we only have an estimate of whats available and need to validate
1741
            # that against the revision records.
1742
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
1743
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.
1744
InterRepository.register_optimiser(InterWeaveRepo)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1745
InterRepository.register_optimiser(InterKnitRepo)
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1746
1747
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1748
class RepositoryTestProviderAdapter(object):
1749
    """A tool to generate a suite testing multiple repository formats at once.
1750
1751
    This is done by copying the test once for each transport and injecting
1752
    the transport_server, transport_readonly_server, and bzrdir_format and
1753
    repository_format classes into each copy. Each copy is also given a new id()
1754
    to make it easy to identify.
1755
    """
1756
1757
    def __init__(self, transport_server, transport_readonly_server, formats):
1758
        self._transport_server = transport_server
1759
        self._transport_readonly_server = transport_readonly_server
1760
        self._formats = formats
1761
    
1762
    def adapt(self, test):
1763
        result = TestSuite()
1764
        for repository_format, bzrdir_format in self._formats:
1765
            new_test = deepcopy(test)
1766
            new_test.transport_server = self._transport_server
1767
            new_test.transport_readonly_server = self._transport_readonly_server
1768
            new_test.bzrdir_format = bzrdir_format
1769
            new_test.repository_format = repository_format
1770
            def make_new_test_id():
1771
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1772
                return lambda: new_id
1773
            new_test.id = make_new_test_id()
1774
            result.addTest(new_test)
1775
        return result
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
1776
1777
1778
class InterRepositoryTestProviderAdapter(object):
1779
    """A tool to generate a suite testing multiple inter repository formats.
1780
1781
    This is done by copying the test once for each interrepo provider and injecting
1782
    the transport_server, transport_readonly_server, repository_format and 
1783
    repository_to_format classes into each copy.
1784
    Each copy is also given a new id() to make it easy to identify.
1785
    """
1786
1787
    def __init__(self, transport_server, transport_readonly_server, formats):
1788
        self._transport_server = transport_server
1789
        self._transport_readonly_server = transport_readonly_server
1790
        self._formats = formats
1791
    
1792
    def adapt(self, test):
1793
        result = TestSuite()
1794
        for interrepo_class, repository_format, repository_format_to in self._formats:
1795
            new_test = deepcopy(test)
1796
            new_test.transport_server = self._transport_server
1797
            new_test.transport_readonly_server = self._transport_readonly_server
1798
            new_test.interrepo_class = interrepo_class
1799
            new_test.repository_format = repository_format
1800
            new_test.repository_format_to = repository_format_to
1801
            def make_new_test_id():
1802
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1803
                return lambda: new_id
1804
            new_test.id = make_new_test_id()
1805
            result.addTest(new_test)
1806
        return result
1807
1808
    @staticmethod
1809
    def default_test_list():
1810
        """Generate the default list of interrepo permutations to test."""
1811
        result = []
1812
        # test the default InterRepository between format 6 and the current 
1813
        # default format.
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.
1814
        # XXX: robertc 20060220 reinstate this when there are two supported
1815
        # formats which do not have an optimal code path between them.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1816
        result.append((InterRepository,
1817
                       RepositoryFormat6(),
1818
                       RepositoryFormatKnit1()))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
1819
        for optimiser in InterRepository._optimisers:
1820
            result.append((optimiser,
1821
                           optimiser._matching_repo_format,
1822
                           optimiser._matching_repo_format
1823
                           ))
1824
        # if there are specific combinations we want to use, we can add them 
1825
        # here.
1826
        return result
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.
1827
1828
1829
class CopyConverter(object):
1830
    """A repository conversion tool which just performs a copy of the content.
1831
    
1832
    This is slow but quite reliable.
1833
    """
1834
1835
    def __init__(self, target_format):
1836
        """Create a CopyConverter.
1837
1838
        :param target_format: The format the resulting repository should be.
1839
        """
1840
        self.target_format = target_format
1841
        
1842
    def convert(self, repo, pb):
1843
        """Perform the conversion of to_convert, giving feedback via pb.
1844
1845
        :param to_convert: The disk object to convert.
1846
        :param pb: a progress bar to use for progress information.
1847
        """
1848
        self.pb = pb
1849
        self.count = 0
1596.2.22 by Robert Collins
Fetch changes to use new pb.
1850
        self.total = 4
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.
1851
        # this is only useful with metadir layouts - separated repo content.
1852
        # trigger an assertion if not such
1853
        repo._format.get_format_string()
1854
        self.repo_dir = repo.bzrdir
1855
        self.step('Moving repository to repository.backup')
1856
        self.repo_dir.transport.move('repository', 'repository.backup')
1857
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
1858
        self.source_repo = repo._format.open(self.repo_dir,
1859
            _found=True,
1860
            _override_transport=backup_transport)
1861
        self.step('Creating new repository')
1862
        converted = self.target_format.initialize(self.repo_dir,
1863
                                                  self.source_repo.is_shared())
1864
        converted.lock_write()
1865
        try:
1866
            self.step('Copying content into repository.')
1867
            self.source_repo.copy_content_into(converted)
1868
        finally:
1869
            converted.unlock()
1870
        self.step('Deleting old repository content.')
1871
        self.repo_dir.transport.delete_tree('repository.backup')
1872
        self.pb.note('repository converted')
1873
1874
    def step(self, message):
1875
        """Update the pb by a step."""
1876
        self.count +=1
1877
        self.pb.update(message, self.count, self.total)
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
1878
1879
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
1880
class CommitBuilder(object):
1881
    """Provides an interface to build up a commit.
1882
1883
    This allows describing a tree to be committed without needing to 
1884
    know the internals of the format of the repository.
1885
    """
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1886
    def __init__(self, repository, parents, config, timestamp=None, 
1887
                 timezone=None, committer=None, revprops=None, 
1888
                 revision_id=None):
1889
        """Initiate a CommitBuilder.
1890
1891
        :param repository: Repository to commit to.
1892
        :param parents: Revision ids of the parents of the new revision.
1893
        :param config: Configuration to use.
1894
        :param timestamp: Optional timestamp recorded for commit.
1895
        :param timezone: Optional timezone for timestamp.
1896
        :param committer: Optional committer to set for commit.
1897
        :param revprops: Optional dictionary of revision properties.
1898
        :param revision_id: Optional revision id.
1899
        """
1900
        self._config = config
1901
1902
        if committer is None:
1903
            self._committer = self._config.username()
1904
        else:
1905
            assert isinstance(committer, basestring), type(committer)
1906
            self._committer = committer
1907
1908
        self.new_inventory = Inventory()
1909
        self._new_revision_id = revision_id
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1910
        self.parents = parents
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
1911
        self.repository = repository
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1912
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1913
        self._revprops = {}
1914
        if revprops is not None:
1915
            self._revprops.update(revprops)
1916
1917
        if timestamp is None:
1918
            self._timestamp = time.time()
1919
        else:
1920
            self._timestamp = long(timestamp)
1921
1922
        if timezone is None:
1923
            self._timezone = local_time_offset()
1924
        else:
1925
            self._timezone = int(timezone)
1926
1927
        self._generate_revision_if_needed()
1928
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
1929
    def commit(self, message):
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
1930
        """Make the actual commit.
1931
1932
        :return: The revision id of the recorded revision.
1933
        """
1934
        rev = Revision(timestamp=self._timestamp,
1935
                       timezone=self._timezone,
1936
                       committer=self._committer,
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
1937
                       message=message,
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
1938
                       inventory_sha1=self.inv_sha1,
1939
                       revision_id=self._new_revision_id,
1940
                       properties=self._revprops)
1941
        rev.parent_ids = self.parents
1942
        self.repository.add_revision(self._new_revision_id, rev, 
1943
            self.new_inventory, self._config)
1944
        return self._new_revision_id
1945
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1946
    def finish_inventory(self):
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
1947
        """Tell the builder that the inventory is finished."""
1757.1.2 by Robert Collins
Bugfix CommitBuilders recording of the inventory revision id.
1948
        self.new_inventory.revision_id = self._new_revision_id
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
1949
        self.inv_sha1 = self.repository.add_inventory(
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1950
            self._new_revision_id,
1951
            self.new_inventory,
1952
            self.parents
1953
            )
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
1954
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1955
    def _gen_revision_id(self):
1956
        """Return new revision-id."""
1957
        s = '%s-%s-' % (self._config.user_email(), 
1958
                        compact_date(self._timestamp))
1959
        s += hexlify(rand_bytes(8))
1960
        return s
1961
1962
    def _generate_revision_if_needed(self):
1963
        """Create a revision id if None was supplied.
1964
        
1965
        If the repository can not support user-specified revision ids
1966
        they should override this function and raise UnsupportedOperation
1967
        if _new_revision_id is not None.
1968
1969
        :raises: UnsupportedOperation
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
1970
        """
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1971
        if self._new_revision_id is None:
1972
            self._new_revision_id = self._gen_revision_id()
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
1973
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1974
    def record_entry_contents(self, ie, parent_invs, path, tree):
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
1975
        """Record the content of ie from tree into the commit if needed.
1976
1977
        :param ie: An inventory entry present in the commit.
1978
        :param parent_invs: The inventories of the parent revisions of the
1979
            commit.
1980
        :param path: The path the entry is at in the tree.
1981
        :param tree: The tree which contains this entry and should be used to 
1982
        obtain content.
1983
        """
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1984
        self.new_inventory.add(ie)
1985
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
1986
        # ie.revision is always None if the InventoryEntry is considered
1987
        # for committing. ie.snapshot will record the correct revision 
1988
        # which may be the sole parent if it is untouched.
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1989
        if ie.revision is not None:
1990
            return
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
1991
        previous_entries = ie.find_previous_heads(
1992
            parent_invs,
1993
            self.repository.weave_store,
1994
            self.repository.get_transaction())
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
1995
        # we are creating a new revision for ie in the history store
1996
        # and inventory.
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
1997
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
1998
1999
    def modified_directory(self, file_id, file_parents):
2000
        """Record the presence of a symbolic link.
2001
2002
        :param file_id: The file_id of the link to record.
2003
        :param file_parents: The per-file parent revision ids.
2004
        """
2005
        self._add_text_to_weave(file_id, [], file_parents.keys())
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2006
    
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2007
    def modified_file_text(self, file_id, file_parents,
2008
                           get_content_byte_lines, text_sha1=None,
2009
                           text_size=None):
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2010
        """Record the text of file file_id
2011
2012
        :param file_id: The file_id of the file to record the text of.
2013
        :param file_parents: The per-file parent revision ids.
2014
        :param get_content_byte_lines: A callable which will return the byte
2015
            lines for the file.
2016
        :param text_sha1: Optional SHA1 of the file contents.
2017
        :param text_size: Optional size of the file contents.
2018
        """
2019
        mutter('storing text of file {%s} in revision {%s} into %r',
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2020
               file_id, self._new_revision_id, self.repository.weave_store)
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2021
        # special case to avoid diffing on renames or 
2022
        # reparenting
2023
        if (len(file_parents) == 1
2024
            and text_sha1 == file_parents.values()[0].text_sha1
2025
            and text_size == file_parents.values()[0].text_size):
2026
            previous_ie = file_parents.values()[0]
2027
            versionedfile = self.repository.weave_store.get_weave(file_id, 
2028
                self.repository.get_transaction())
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2029
            versionedfile.clone_text(self._new_revision_id, 
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2030
                previous_ie.revision, file_parents.keys())
2031
            return text_sha1, text_size
2032
        else:
2033
            new_lines = get_content_byte_lines()
2034
            # TODO: Rather than invoking sha_strings here, _add_text_to_weave
2035
            # should return the SHA1 and size
2036
            self._add_text_to_weave(file_id, new_lines, file_parents.keys())
2037
            return bzrlib.osutils.sha_strings(new_lines), \
2038
                sum(map(len, new_lines))
2039
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2040
    def modified_link(self, file_id, file_parents, link_target):
2041
        """Record the presence of a symbolic link.
2042
2043
        :param file_id: The file_id of the link to record.
2044
        :param file_parents: The per-file parent revision ids.
2045
        :param link_target: Target location of this link.
2046
        """
2047
        self._add_text_to_weave(file_id, [], file_parents.keys())
2048
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2049
    def _add_text_to_weave(self, file_id, new_lines, parents):
2050
        versionedfile = self.repository.weave_store.get_weave_or_empty(
2051
            file_id, self.repository.get_transaction())
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2052
        versionedfile.add_lines(self._new_revision_id, parents, new_lines)
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2053
        versionedfile.clear_cache()
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2054
2055
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
2056
# Copied from xml.sax.saxutils
2057
def _unescape_xml(data):
2058
    """Unescape &amp;, &lt;, and &gt; in a string of data.
2059
    """
2060
    data = data.replace("&lt;", "<")
2061
    data = data.replace("&gt;", ">")
2062
    # must do ampersand last
2063
    return data.replace("&amp;", "&")