~bzr-pqm/bzr/bzr.dev

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