1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
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.
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.
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
17
from cStringIO import StringIO
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
21
from binascii import hexlify
22
from copy import deepcopy
41
revision as _mod_revision,
50
from bzrlib.osutils import (
55
from bzrlib.revisiontree import RevisionTree
56
from bzrlib.store.versioned import VersionedFileStore
57
from bzrlib.store.text import TextStore
58
from bzrlib.testament import Testament
61
from bzrlib.decorators import needs_read_lock, needs_write_lock
62
from bzrlib.inter import InterObject
63
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
64
from bzrlib.symbol_versioning import (
68
from bzrlib.trace import mutter, note, warning
71
# Old formats display a warning, but only once
72
_deprecation_warning_done = False
75
class Repository(object):
76
"""Repository holding history for one or more branches.
78
The repository holds and retrieves historical information including
79
revisions and file history. It's normally accessed only by the Branch,
80
which views a particular line of development through that history.
82
The Repository builds on top of Stores and a Transport, which respectively
83
describe the disk data format and the way of accessing the (possibly
87
_file_ids_altered_regex = lazy_regex.lazy_compile(
88
r'file_id="(?P<file_id>[^"]+)"'
89
r'.*revision="(?P<revision_id>[^"]+)"'
93
def add_inventory(self, revision_id, inv, parents):
94
"""Add the inventory inv to the repository as revision_id.
96
:param parents: The revision ids of the parents that revision_id
97
is known to have and are in the repository already.
99
returns the sha1 of the serialized inventory.
101
revision_id = osutils.safe_revision_id(revision_id)
102
_mod_revision.check_not_reserved_id(revision_id)
103
assert inv.revision_id is None or inv.revision_id == revision_id, \
104
"Mismatch between inventory revision" \
105
" id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
106
assert inv.root is not None
107
inv_text = self.serialise_inventory(inv)
108
inv_sha1 = osutils.sha_string(inv_text)
109
inv_vf = self.control_weaves.get_weave('inventory',
110
self.get_transaction())
111
self._inventory_add_lines(inv_vf, revision_id, parents,
112
osutils.split_lines(inv_text))
115
def _inventory_add_lines(self, inv_vf, revision_id, parents, lines):
117
for parent in parents:
119
final_parents.append(parent)
121
inv_vf.add_lines(revision_id, final_parents, lines)
124
def add_revision(self, revision_id, rev, inv=None, config=None):
125
"""Add rev to the revision store as revision_id.
127
:param revision_id: the revision id to use.
128
:param rev: The revision object.
129
:param inv: The inventory for the revision. if None, it will be looked
130
up in the inventory storer
131
:param config: If None no digital signature will be created.
132
If supplied its signature_needed method will be used
133
to determine if a signature should be made.
135
revision_id = osutils.safe_revision_id(revision_id)
136
_mod_revision.check_not_reserved_id(revision_id)
137
if config is not None and config.signature_needed():
139
inv = self.get_inventory(revision_id)
140
plaintext = Testament(rev, inv).as_short_text()
141
self.store_revision_signature(
142
gpg.GPGStrategy(config), plaintext, revision_id)
143
if not revision_id in self.get_inventory_weave():
145
raise errors.WeaveRevisionNotPresent(revision_id,
146
self.get_inventory_weave())
148
# yes, this is not suitable for adding with ghosts.
149
self.add_inventory(revision_id, inv, rev.parent_ids)
150
self._revision_store.add_revision(rev, self.get_transaction())
153
def _all_possible_ids(self):
154
"""Return all the possible revisions that we could find."""
155
return self.get_inventory_weave().versions()
157
def all_revision_ids(self):
158
"""Returns a list of all the revision ids in the repository.
160
This is deprecated because code should generally work on the graph
161
reachable from a particular revision, and ignore any other revisions
162
that might be present. There is no direct replacement method.
164
return self._all_revision_ids()
167
def _all_revision_ids(self):
168
"""Returns a list of all the revision ids in the repository.
170
These are in as much topological order as the underlying store can
171
present: for weaves ghosts may lead to a lack of correctness until
172
the reweave updates the parents list.
174
if self._revision_store.text_store.listable():
175
return self._revision_store.all_revision_ids(self.get_transaction())
176
result = self._all_possible_ids()
177
return self._eliminate_revisions_not_present(result)
179
def break_lock(self):
180
"""Break a lock if one is present from another instance.
182
Uses the ui factory to ask for confirmation if the lock may be from
185
self.control_files.break_lock()
188
def _eliminate_revisions_not_present(self, revision_ids):
189
"""Check every revision id in revision_ids to see if we have it.
191
Returns a set of the present revisions.
194
for id in revision_ids:
195
if self.has_revision(id):
200
def create(a_bzrdir):
201
"""Construct the current default format repository in a_bzrdir."""
202
return RepositoryFormat.get_default_format().initialize(a_bzrdir)
204
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
205
"""instantiate a Repository.
207
:param _format: The format of the repository on disk.
208
:param a_bzrdir: The BzrDir of the repository.
210
In the future we will have a single api for all stores for
211
getting file texts, inventories and revisions, then
212
this construct will accept instances of those things.
214
super(Repository, self).__init__()
215
self._format = _format
216
# the following are part of the public API for Repository:
217
self.bzrdir = a_bzrdir
218
self.control_files = control_files
219
self._revision_store = _revision_store
220
self.text_store = text_store
221
# backwards compatibility
222
self.weave_store = text_store
223
# not right yet - should be more semantically clear ?
225
self.control_store = control_store
226
self.control_weaves = control_store
227
# TODO: make sure to construct the right store classes, etc, depending
228
# on whether escaping is required.
229
self._warn_if_deprecated()
230
self._serializer = xml5.serializer_v5
233
return '%s(%r)' % (self.__class__.__name__,
234
self.bzrdir.transport.base)
237
return self.control_files.is_locked()
239
def lock_write(self):
240
self.control_files.lock_write()
243
self.control_files.lock_read()
245
def get_physical_lock_status(self):
246
return self.control_files.get_physical_lock_status()
249
def missing_revision_ids(self, other, revision_id=None):
250
"""Return the revision ids that other has that this does not.
252
These are returned in topological order.
254
revision_id: only return revision ids included by revision_id.
256
return InterRepository.get(other, self).missing_revision_ids(revision_id)
260
"""Open the repository rooted at base.
262
For instance, if the repository is at URL/.bzr/repository,
263
Repository.open(URL) -> a Repository instance.
265
control = bzrdir.BzrDir.open(base)
266
return control.open_repository()
268
def copy_content_into(self, destination, revision_id=None, basis=None):
269
"""Make a complete copy of the content in self into destination.
271
This is a destructive operation! Do not use it on existing
274
return InterRepository.get(self, destination).copy_content(revision_id, basis)
276
def fetch(self, source, revision_id=None, pb=None):
277
"""Fetch the content required to construct revision_id from source.
279
If revision_id is None all content is copied.
281
return InterRepository.get(source, self).fetch(revision_id=revision_id,
284
def get_commit_builder(self, branch, parents, config, timestamp=None,
285
timezone=None, committer=None, revprops=None,
287
"""Obtain a CommitBuilder for this repository.
289
:param branch: Branch to commit to.
290
:param parents: Revision ids of the parents of the new revision.
291
:param config: Configuration to use.
292
:param timestamp: Optional timestamp recorded for commit.
293
:param timezone: Optional timezone for timestamp.
294
:param committer: Optional committer to set for commit.
295
:param revprops: Optional dictionary of revision properties.
296
:param revision_id: Optional revision id.
298
revision_id = osutils.safe_revision_id(revision_id)
299
return _CommitBuilder(self, parents, config, timestamp, timezone,
300
committer, revprops, revision_id)
303
self.control_files.unlock()
306
def clone(self, a_bzrdir, revision_id=None, basis=None):
307
"""Clone this repository into a_bzrdir using the current format.
309
Currently no check is made that the format of this repository and
310
the bzrdir format are compatible. FIXME RBC 20060201.
312
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
313
# use target default format.
314
result = a_bzrdir.create_repository()
315
# FIXME RBC 20060209 split out the repository type to avoid this check ?
316
elif isinstance(a_bzrdir._format,
317
(bzrdir.BzrDirFormat4,
318
bzrdir.BzrDirFormat5,
319
bzrdir.BzrDirFormat6)):
320
result = a_bzrdir.open_repository()
322
result = self._format.initialize(a_bzrdir, shared=self.is_shared())
323
self.copy_content_into(result, revision_id, basis)
327
def has_revision(self, revision_id):
328
"""True if this repository has a copy of the revision."""
329
return self._revision_store.has_revision_id(revision_id,
330
self.get_transaction())
333
def get_revision_reconcile(self, revision_id):
334
"""'reconcile' helper routine that allows access to a revision always.
336
This variant of get_revision does not cross check the weave graph
337
against the revision one as get_revision does: but it should only
338
be used by reconcile, or reconcile-alike commands that are correcting
339
or testing the revision graph.
341
if not revision_id or not isinstance(revision_id, basestring):
342
raise errors.InvalidRevisionId(revision_id=revision_id,
344
return self._revision_store.get_revisions([revision_id],
345
self.get_transaction())[0]
347
def get_revisions(self, revision_ids):
348
return self._revision_store.get_revisions(revision_ids,
349
self.get_transaction())
352
def get_revision_xml(self, revision_id):
353
rev = self.get_revision(revision_id)
355
# the current serializer..
356
self._revision_store._serializer.write_revision(rev, rev_tmp)
358
return rev_tmp.getvalue()
361
def get_revision(self, revision_id):
362
"""Return the Revision object for a named revision"""
363
r = self.get_revision_reconcile(revision_id)
364
# weave corruption can lead to absent revision markers that should be
366
# the following test is reasonably cheap (it needs a single weave read)
367
# and the weave is cached in read transactions. In write transactions
368
# it is not cached but typically we only read a small number of
369
# revisions. For knits when they are introduced we will probably want
370
# to ensure that caching write transactions are in use.
371
inv = self.get_inventory_weave()
372
self._check_revision_parents(r, inv)
376
def get_deltas_for_revisions(self, revisions):
377
"""Produce a generator of revision deltas.
379
Note that the input is a sequence of REVISIONS, not revision_ids.
380
Trees will be held in memory until the generator exits.
381
Each delta is relative to the revision's lefthand predecessor.
383
required_trees = set()
384
for revision in revisions:
385
required_trees.add(revision.revision_id)
386
required_trees.update(revision.parent_ids[:1])
387
trees = dict((t.get_revision_id(), t) for
388
t in self.revision_trees(required_trees))
389
for revision in revisions:
390
if not revision.parent_ids:
391
old_tree = self.revision_tree(None)
393
old_tree = trees[revision.parent_ids[0]]
394
yield trees[revision.revision_id].changes_from(old_tree)
397
def get_revision_delta(self, revision_id):
398
"""Return the delta for one revision.
400
The delta is relative to the left-hand predecessor of the
403
r = self.get_revision(revision_id)
404
return list(self.get_deltas_for_revisions([r]))[0]
406
def _check_revision_parents(self, revision, inventory):
407
"""Private to Repository and Fetch.
409
This checks the parentage of revision in an inventory weave for
410
consistency and is only applicable to inventory-weave-for-ancestry
411
using repository formats & fetchers.
413
weave_parents = inventory.get_parents(revision.revision_id)
414
weave_names = inventory.versions()
415
for parent_id in revision.parent_ids:
416
if parent_id in weave_names:
417
# this parent must not be a ghost.
418
if not parent_id in weave_parents:
420
raise errors.CorruptRepository(self)
423
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
424
signature = gpg_strategy.sign(plaintext)
425
self._revision_store.add_revision_signature_text(revision_id,
427
self.get_transaction())
429
def fileids_altered_by_revision_ids(self, revision_ids):
430
"""Find the file ids and versions affected by revisions.
432
:param revisions: an iterable containing revision ids.
433
:return: a dictionary mapping altered file-ids to an iterable of
434
revision_ids. Each altered file-ids has the exact revision_ids that
435
altered it listed explicitly.
437
assert self._serializer.support_altered_by_hack, \
438
("fileids_altered_by_revision_ids only supported for branches "
439
"which store inventory as unnested xml, not on %r" % self)
440
selected_revision_ids = set(revision_ids)
441
w = self.get_inventory_weave()
444
# this code needs to read every new line in every inventory for the
445
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
446
# not present in one of those inventories is unnecessary but not
447
# harmful because we are filtering by the revision id marker in the
448
# inventory lines : we only select file ids altered in one of those
449
# revisions. We don't need to see all lines in the inventory because
450
# only those added in an inventory in rev X can contain a revision=X
452
unescape_revid_cache = {}
453
unescape_fileid_cache = {}
455
# jam 20061218 In a big fetch, this handles hundreds of thousands
456
# of lines, so it has had a lot of inlining and optimizing done.
457
# Sorry that it is a little bit messy.
458
# Move several functions to be local variables, since this is a long
460
search = self._file_ids_altered_regex.search
461
unescape = _unescape_xml
462
setdefault = result.setdefault
463
pb = ui.ui_factory.nested_progress_bar()
465
for line in w.iter_lines_added_or_present_in_versions(
466
selected_revision_ids, pb=pb):
470
# One call to match.group() returning multiple items is quite a
471
# bit faster than 2 calls to match.group() each returning 1
472
file_id, revision_id = match.group('file_id', 'revision_id')
474
# Inlining the cache lookups helps a lot when you make 170,000
475
# lines and 350k ids, versus 8.4 unique ids.
476
# Using a cache helps in 2 ways:
477
# 1) Avoids unnecessary decoding calls
478
# 2) Re-uses cached strings, which helps in future set and
480
# (2) is enough that removing encoding entirely along with
481
# the cache (so we are using plain strings) results in no
482
# performance improvement.
484
revision_id = unescape_revid_cache[revision_id]
486
unescaped = unescape(revision_id)
487
unescape_revid_cache[revision_id] = unescaped
488
revision_id = unescaped
490
if revision_id in selected_revision_ids:
492
file_id = unescape_fileid_cache[file_id]
494
unescaped = unescape(file_id)
495
unescape_fileid_cache[file_id] = unescaped
497
setdefault(file_id, set()).add(revision_id)
503
def get_inventory_weave(self):
504
return self.control_weaves.get_weave('inventory',
505
self.get_transaction())
508
def get_inventory(self, revision_id):
509
"""Get Inventory object by hash."""
510
return self.deserialise_inventory(
511
revision_id, self.get_inventory_xml(revision_id))
513
def deserialise_inventory(self, revision_id, xml):
514
"""Transform the xml into an inventory object.
516
:param revision_id: The expected revision id of the inventory.
517
:param xml: A serialised inventory.
519
result = self._serializer.read_inventory_from_string(xml)
520
result.root.revision = revision_id
523
def serialise_inventory(self, inv):
524
return self._serializer.write_inventory_to_string(inv)
527
def get_inventory_xml(self, revision_id):
528
"""Get inventory XML as a file object."""
530
assert isinstance(revision_id, basestring), type(revision_id)
531
iw = self.get_inventory_weave()
532
return iw.get_text(revision_id)
534
raise errors.HistoryMissing(self, 'inventory', revision_id)
537
def get_inventory_sha1(self, revision_id):
538
"""Return the sha1 hash of the inventory entry
540
return self.get_revision(revision_id).inventory_sha1
543
def get_revision_graph(self, revision_id=None):
544
"""Return a dictionary containing the revision graph.
546
:param revision_id: The revision_id to get a graph from. If None, then
547
the entire revision graph is returned. This is a deprecated mode of
548
operation and will be removed in the future.
549
:return: a dictionary of revision_id->revision_parents_list.
551
# special case NULL_REVISION
552
if revision_id == _mod_revision.NULL_REVISION:
554
a_weave = self.get_inventory_weave()
555
all_revisions = self._eliminate_revisions_not_present(
557
entire_graph = dict([(node, a_weave.get_parents(node)) for
558
node in all_revisions])
559
if revision_id is None:
561
elif revision_id not in entire_graph:
562
raise errors.NoSuchRevision(self, revision_id)
564
# add what can be reached from revision_id
566
pending = set([revision_id])
567
while len(pending) > 0:
569
result[node] = entire_graph[node]
570
for revision_id in result[node]:
571
if revision_id not in result:
572
pending.add(revision_id)
576
def get_revision_graph_with_ghosts(self, revision_ids=None):
577
"""Return a graph of the revisions with ghosts marked as applicable.
579
:param revision_ids: an iterable of revisions to graph or None for all.
580
:return: a Graph object with the graph reachable from revision_ids.
582
result = graph.Graph()
584
pending = set(self.all_revision_ids())
587
pending = set(revision_ids)
588
# special case NULL_REVISION
589
if _mod_revision.NULL_REVISION in pending:
590
pending.remove(_mod_revision.NULL_REVISION)
591
required = set(pending)
594
revision_id = pending.pop()
596
rev = self.get_revision(revision_id)
597
except errors.NoSuchRevision:
598
if revision_id in required:
601
result.add_ghost(revision_id)
603
for parent_id in rev.parent_ids:
604
# is this queued or done ?
605
if (parent_id not in pending and
606
parent_id not in done):
608
pending.add(parent_id)
609
result.add_node(revision_id, rev.parent_ids)
610
done.add(revision_id)
614
def get_revision_inventory(self, revision_id):
615
"""Return inventory of a past revision."""
616
# TODO: Unify this with get_inventory()
617
# bzr 0.0.6 and later imposes the constraint that the inventory_id
618
# must be the same as its revision, so this is trivial.
619
if revision_id is None:
620
# This does not make sense: if there is no revision,
621
# then it is the current tree inventory surely ?!
622
# and thus get_root_id() is something that looks at the last
623
# commit on the branch, and the get_root_id is an inventory check.
624
raise NotImplementedError
625
# return Inventory(self.get_root_id())
627
return self.get_inventory(revision_id)
631
"""Return True if this repository is flagged as a shared repository."""
632
raise NotImplementedError(self.is_shared)
635
def reconcile(self, other=None, thorough=False):
636
"""Reconcile this repository."""
637
from bzrlib.reconcile import RepoReconciler
638
reconciler = RepoReconciler(self, thorough=thorough)
639
reconciler.reconcile()
643
def revision_tree(self, revision_id):
644
"""Return Tree for a revision on this branch.
646
`revision_id` may be None for the empty tree revision.
648
# TODO: refactor this to use an existing revision object
649
# so we don't need to read it in twice.
650
if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
651
return RevisionTree(self, Inventory(root_id=None),
652
_mod_revision.NULL_REVISION)
654
inv = self.get_revision_inventory(revision_id)
655
return RevisionTree(self, inv, revision_id)
658
def revision_trees(self, revision_ids):
659
"""Return Tree for a revision on this branch.
661
`revision_id` may not be None or 'null:'"""
662
assert None not in revision_ids
663
assert _mod_revision.NULL_REVISION not in revision_ids
664
texts = self.get_inventory_weave().get_texts(revision_ids)
665
for text, revision_id in zip(texts, revision_ids):
666
inv = self.deserialise_inventory(revision_id, text)
667
yield RevisionTree(self, inv, revision_id)
670
def get_ancestry(self, revision_id):
671
"""Return a list of revision-ids integrated by a revision.
673
The first element of the list is always None, indicating the origin
674
revision. This might change when we have history horizons, or
675
perhaps we should have a new API.
677
This is topologically sorted.
679
if revision_id is None:
681
if not self.has_revision(revision_id):
682
raise errors.NoSuchRevision(self, revision_id)
683
w = self.get_inventory_weave()
684
candidates = w.get_ancestry(revision_id)
685
return [None] + candidates # self._eliminate_revisions_not_present(candidates)
688
def print_file(self, file, revision_id):
689
"""Print `file` to stdout.
691
FIXME RBC 20060125 as John Meinel points out this is a bad api
692
- it writes to stdout, it assumes that that is valid etc. Fix
693
by creating a new more flexible convenience function.
695
tree = self.revision_tree(revision_id)
696
# use inventory as it was in that revision
697
file_id = tree.inventory.path2id(file)
699
# TODO: jam 20060427 Write a test for this code path
700
# it had a bug in it, and was raising the wrong
702
raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
703
tree.print_file(file_id)
705
def get_transaction(self):
706
return self.control_files.get_transaction()
708
def revision_parents(self, revid):
709
return self.get_inventory_weave().parent_names(revid)
712
def set_make_working_trees(self, new_value):
713
"""Set the policy flag for making working trees when creating branches.
715
This only applies to branches that use this repository.
717
The default is 'True'.
718
:param new_value: True to restore the default, False to disable making
721
raise NotImplementedError(self.set_make_working_trees)
723
def make_working_trees(self):
724
"""Returns the policy for making working trees on new branches."""
725
raise NotImplementedError(self.make_working_trees)
728
def sign_revision(self, revision_id, gpg_strategy):
729
plaintext = Testament.from_revision(self, revision_id).as_short_text()
730
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
733
def has_signature_for_revision_id(self, revision_id):
734
"""Query for a revision signature for revision_id in the repository."""
735
return self._revision_store.has_signature(revision_id,
736
self.get_transaction())
739
def get_signature_text(self, revision_id):
740
"""Return the text for a signature."""
741
return self._revision_store.get_signature_text(revision_id,
742
self.get_transaction())
745
def check(self, revision_ids):
746
"""Check consistency of all history of given revision_ids.
748
Different repository implementations should override _check().
750
:param revision_ids: A non-empty list of revision_ids whose ancestry
751
will be checked. Typically the last revision_id of a branch.
754
raise ValueError("revision_ids must be non-empty in %s.check"
756
return self._check(revision_ids)
758
def _check(self, revision_ids):
759
result = check.Check(self)
763
def _warn_if_deprecated(self):
764
global _deprecation_warning_done
765
if _deprecation_warning_done:
767
_deprecation_warning_done = True
768
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
769
% (self._format, self.bzrdir.transport.base))
771
def supports_rich_root(self):
772
return self._format.rich_root_data
774
def _check_ascii_revisionid(self, revision_id, method):
775
"""Private helper for ascii-only repositories."""
776
# weave repositories refuse to store revisionids that are non-ascii.
777
if revision_id is not None:
778
# weaves require ascii revision ids.
779
if isinstance(revision_id, unicode):
781
revision_id.encode('ascii')
782
except UnicodeEncodeError:
783
raise errors.NonAsciiRevisionId(method, self)
786
revision_id.decode('ascii')
787
except UnicodeDecodeError:
788
raise errors.NonAsciiRevisionId(method, self)
791
class AllInOneRepository(Repository):
792
"""Legacy support - the repository behaviour for all-in-one branches."""
794
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
795
# we reuse one control files instance.
796
dir_mode = a_bzrdir._control_files._dir_mode
797
file_mode = a_bzrdir._control_files._file_mode
799
def get_store(name, compressed=True, prefixed=False):
800
# FIXME: This approach of assuming stores are all entirely compressed
801
# or entirely uncompressed is tidy, but breaks upgrade from
802
# some existing branches where there's a mixture; we probably
803
# still want the option to look for both.
804
relpath = a_bzrdir._control_files._escape(name)
805
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
806
prefixed=prefixed, compressed=compressed,
809
#if self._transport.should_cache():
810
# cache_path = os.path.join(self.cache_root, name)
811
# os.mkdir(cache_path)
812
# store = bzrlib.store.CachedStore(store, cache_path)
815
# not broken out yet because the controlweaves|inventory_store
816
# and text_store | weave_store bits are still different.
817
if isinstance(_format, RepositoryFormat4):
818
# cannot remove these - there is still no consistent api
819
# which allows access to this old info.
820
self.inventory_store = get_store('inventory-store')
821
text_store = get_store('text-store')
822
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
824
def get_commit_builder(self, branch, parents, config, timestamp=None,
825
timezone=None, committer=None, revprops=None,
827
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
828
return Repository.get_commit_builder(self, branch, parents, config,
829
timestamp, timezone, committer, revprops, revision_id)
833
"""AllInOne repositories cannot be shared."""
837
def set_make_working_trees(self, new_value):
838
"""Set the policy flag for making working trees when creating branches.
840
This only applies to branches that use this repository.
842
The default is 'True'.
843
:param new_value: True to restore the default, False to disable making
846
raise NotImplementedError(self.set_make_working_trees)
848
def make_working_trees(self):
849
"""Returns the policy for making working trees on new branches."""
853
def install_revision(repository, rev, revision_tree):
854
"""Install all revision data into a repository."""
857
for p_id in rev.parent_ids:
858
if repository.has_revision(p_id):
859
present_parents.append(p_id)
860
parent_trees[p_id] = repository.revision_tree(p_id)
862
parent_trees[p_id] = repository.revision_tree(None)
864
inv = revision_tree.inventory
865
entries = inv.iter_entries()
866
# backwards compatability hack: skip the root id.
867
if not repository.supports_rich_root():
868
path, root = entries.next()
869
if root.revision != rev.revision_id:
870
raise errors.IncompatibleRevision(repr(repository))
871
# Add the texts that are not already present
872
for path, ie in entries:
873
w = repository.weave_store.get_weave_or_empty(ie.file_id,
874
repository.get_transaction())
875
if ie.revision not in w:
877
# FIXME: TODO: The following loop *may* be overlapping/duplicate
878
# with InventoryEntry.find_previous_heads(). if it is, then there
879
# is a latent bug here where the parents may have ancestors of each
881
for revision, tree in parent_trees.iteritems():
882
if ie.file_id not in tree:
884
parent_id = tree.inventory[ie.file_id].revision
885
if parent_id in text_parents:
887
text_parents.append(parent_id)
889
vfile = repository.weave_store.get_weave_or_empty(ie.file_id,
890
repository.get_transaction())
891
lines = revision_tree.get_file(ie.file_id).readlines()
892
vfile.add_lines(rev.revision_id, text_parents, lines)
894
# install the inventory
895
repository.add_inventory(rev.revision_id, inv, present_parents)
896
except errors.RevisionAlreadyPresent:
898
repository.add_revision(rev.revision_id, rev, inv)
901
class MetaDirRepository(Repository):
902
"""Repositories in the new meta-dir layout."""
904
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
905
super(MetaDirRepository, self).__init__(_format,
911
dir_mode = self.control_files._dir_mode
912
file_mode = self.control_files._file_mode
916
"""Return True if this repository is flagged as a shared repository."""
917
return self.control_files._transport.has('shared-storage')
920
def set_make_working_trees(self, new_value):
921
"""Set the policy flag for making working trees when creating branches.
923
This only applies to branches that use this repository.
925
The default is 'True'.
926
:param new_value: True to restore the default, False to disable making
931
self.control_files._transport.delete('no-working-trees')
932
except errors.NoSuchFile:
935
self.control_files.put_utf8('no-working-trees', '')
937
def make_working_trees(self):
938
"""Returns the policy for making working trees on new branches."""
939
return not self.control_files._transport.has('no-working-trees')
942
class WeaveMetaDirRepository(MetaDirRepository):
943
"""A subclass of MetaDirRepository to set weave specific policy."""
945
def get_commit_builder(self, branch, parents, config, timestamp=None,
946
timezone=None, committer=None, revprops=None,
948
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
949
return MetaDirRepository.get_commit_builder(self, branch, parents,
950
config, timestamp, timezone, committer, revprops, revision_id)
953
class KnitRepository(MetaDirRepository):
954
"""Knit format repository."""
956
def _warn_if_deprecated(self):
957
# This class isn't deprecated
960
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
961
inv_vf.add_lines_with_ghosts(revid, parents, lines)
964
def _all_revision_ids(self):
965
"""See Repository.all_revision_ids()."""
966
# Knits get the revision graph from the index of the revision knit, so
967
# it's always possible even if they're on an unlistable transport.
968
return self._revision_store.all_revision_ids(self.get_transaction())
970
def fileid_involved_between_revs(self, from_revid, to_revid):
971
"""Find file_id(s) which are involved in the changes between revisions.
973
This determines the set of revisions which are involved, and then
974
finds all file ids affected by those revisions.
976
vf = self._get_revision_vf()
977
from_set = set(vf.get_ancestry(from_revid))
978
to_set = set(vf.get_ancestry(to_revid))
979
changed = to_set.difference(from_set)
980
return self._fileid_involved_by_set(changed)
982
def fileid_involved(self, last_revid=None):
983
"""Find all file_ids modified in the ancestry of last_revid.
985
:param last_revid: If None, last_revision() will be used.
988
changed = set(self.all_revision_ids())
990
changed = set(self.get_ancestry(last_revid))
993
return self._fileid_involved_by_set(changed)
996
def get_ancestry(self, revision_id):
997
"""Return a list of revision-ids integrated by a revision.
999
This is topologically sorted.
1001
if revision_id is None:
1003
vf = self._get_revision_vf()
1005
return [None] + vf.get_ancestry(revision_id)
1006
except errors.RevisionNotPresent:
1007
raise errors.NoSuchRevision(self, revision_id)
1010
def get_revision(self, revision_id):
1011
"""Return the Revision object for a named revision"""
1012
return self.get_revision_reconcile(revision_id)
1015
def get_revision_graph(self, revision_id=None):
1016
"""Return a dictionary containing the revision graph.
1018
:param revision_id: The revision_id to get a graph from. If None, then
1019
the entire revision graph is returned. This is a deprecated mode of
1020
operation and will be removed in the future.
1021
:return: a dictionary of revision_id->revision_parents_list.
1023
# special case NULL_REVISION
1024
if revision_id == _mod_revision.NULL_REVISION:
1026
a_weave = self._get_revision_vf()
1027
entire_graph = a_weave.get_graph()
1028
if revision_id is None:
1029
return a_weave.get_graph()
1030
elif revision_id not in a_weave:
1031
raise errors.NoSuchRevision(self, revision_id)
1033
# add what can be reached from revision_id
1035
pending = set([revision_id])
1036
while len(pending) > 0:
1037
node = pending.pop()
1038
result[node] = a_weave.get_parents(node)
1039
for revision_id in result[node]:
1040
if revision_id not in result:
1041
pending.add(revision_id)
1045
def get_revision_graph_with_ghosts(self, revision_ids=None):
1046
"""Return a graph of the revisions with ghosts marked as applicable.
1048
:param revision_ids: an iterable of revisions to graph or None for all.
1049
:return: a Graph object with the graph reachable from revision_ids.
1051
result = graph.Graph()
1052
vf = self._get_revision_vf()
1053
versions = set(vf.versions())
1054
if not revision_ids:
1055
pending = set(self.all_revision_ids())
1058
pending = set(revision_ids)
1059
# special case NULL_REVISION
1060
if _mod_revision.NULL_REVISION in pending:
1061
pending.remove(_mod_revision.NULL_REVISION)
1062
required = set(pending)
1065
revision_id = pending.pop()
1066
if not revision_id in versions:
1067
if revision_id in required:
1068
raise errors.NoSuchRevision(self, revision_id)
1070
result.add_ghost(revision_id)
1071
# mark it as done so we don't try for it again.
1072
done.add(revision_id)
1074
parent_ids = vf.get_parents_with_ghosts(revision_id)
1075
for parent_id in parent_ids:
1076
# is this queued or done ?
1077
if (parent_id not in pending and
1078
parent_id not in done):
1080
pending.add(parent_id)
1081
result.add_node(revision_id, parent_ids)
1082
done.add(revision_id)
1085
def _get_revision_vf(self):
1086
""":return: a versioned file containing the revisions."""
1087
vf = self._revision_store.get_revision_file(self.get_transaction())
1091
def reconcile(self, other=None, thorough=False):
1092
"""Reconcile this repository."""
1093
from bzrlib.reconcile import KnitReconciler
1094
reconciler = KnitReconciler(self, thorough=thorough)
1095
reconciler.reconcile()
1098
def revision_parents(self, revision_id):
1099
return self._get_revision_vf().get_parents(revision_id)
1102
class KnitRepository2(KnitRepository):
1104
def __init__(self, _format, a_bzrdir, control_files, _revision_store,
1105
control_store, text_store):
1106
KnitRepository.__init__(self, _format, a_bzrdir, control_files,
1107
_revision_store, control_store, text_store)
1108
self._serializer = xml6.serializer_v6
1110
def deserialise_inventory(self, revision_id, xml):
1111
"""Transform the xml into an inventory object.
1113
:param revision_id: The expected revision id of the inventory.
1114
:param xml: A serialised inventory.
1116
result = self._serializer.read_inventory_from_string(xml)
1117
assert result.root.revision is not None
1120
def serialise_inventory(self, inv):
1121
"""Transform the inventory object into XML text.
1123
:param revision_id: The expected revision id of the inventory.
1124
:param xml: A serialised inventory.
1126
assert inv.revision_id is not None
1127
assert inv.root.revision is not None
1128
return KnitRepository.serialise_inventory(self, inv)
1130
def get_commit_builder(self, branch, parents, config, timestamp=None,
1131
timezone=None, committer=None, revprops=None,
1133
"""Obtain a CommitBuilder for this repository.
1135
:param branch: Branch to commit to.
1136
:param parents: Revision ids of the parents of the new revision.
1137
:param config: Configuration to use.
1138
:param timestamp: Optional timestamp recorded for commit.
1139
:param timezone: Optional timezone for timestamp.
1140
:param committer: Optional committer to set for commit.
1141
:param revprops: Optional dictionary of revision properties.
1142
:param revision_id: Optional revision id.
1144
return RootCommitBuilder(self, parents, config, timestamp, timezone,
1145
committer, revprops, revision_id)
1148
class RepositoryFormatRegistry(registry.Registry):
1149
"""Registry of RepositoryFormats.
1153
format_registry = RepositoryFormatRegistry()
1154
"""Registry of formats, indexed by their identifying format string."""
1157
class RepositoryFormat(object):
1158
"""A repository format.
1160
Formats provide three things:
1161
* An initialization routine to construct repository data on disk.
1162
* a format string which is used when the BzrDir supports versioned
1164
* an open routine which returns a Repository instance.
1166
Formats are placed in an dict by their format string for reference
1167
during opening. These should be subclasses of RepositoryFormat
1170
Once a format is deprecated, just deprecate the initialize and open
1171
methods on the format class. Do not deprecate the object, as the
1172
object will be created every system load.
1174
Common instance attributes:
1175
_matchingbzrdir - the bzrdir format that the repository format was
1176
originally written to work with. This can be used if manually
1177
constructing a bzrdir and repository, or more commonly for test suite
1182
return "<%s>" % self.__class__.__name__
1185
def find_format(klass, a_bzrdir):
1186
"""Return the format for the repository object in a_bzrdir.
1188
This is used by bzr native formats that have a "format" file in
1189
the repository. Other methods may be used by different types of
1193
transport = a_bzrdir.get_repository_transport(None)
1194
format_string = transport.get("format").read()
1195
return format_registry.get(format_string)
1196
except errors.NoSuchFile:
1197
raise errors.NoRepositoryPresent(a_bzrdir)
1199
raise errors.UnknownFormatError(format=format_string)
1202
@deprecated_method(symbol_versioning.zero_fourteen)
1203
def set_default_format(klass, format):
1204
klass._set_default_format(format)
1207
def _set_default_format(klass, format):
1208
"""Set the default format for new Repository creation.
1210
The format must already be registered.
1212
format_registry.default_key = format.get_format_string()
1215
def register_format(klass, format):
1216
format_registry.register(format.get_format_string(), format)
1219
def unregister_format(klass, format):
1220
format_registry.remove(format.get_format_string())
1223
def get_default_format(klass):
1224
"""Return the current default format."""
1225
return format_registry.get(format_registry.default_key)
1227
def _get_control_store(self, repo_transport, control_files):
1228
"""Return the control store for this repository."""
1229
raise NotImplementedError(self._get_control_store)
1231
def get_format_string(self):
1232
"""Return the ASCII format string that identifies this format.
1234
Note that in pre format ?? repositories the format string is
1235
not permitted nor written to disk.
1237
raise NotImplementedError(self.get_format_string)
1239
def get_format_description(self):
1240
"""Return the short description for this format."""
1241
raise NotImplementedError(self.get_format_description)
1243
def _get_revision_store(self, repo_transport, control_files):
1244
"""Return the revision store object for this a_bzrdir."""
1245
raise NotImplementedError(self._get_revision_store)
1247
def _get_text_rev_store(self,
1254
"""Common logic for getting a revision store for a repository.
1256
see self._get_revision_store for the subclass-overridable method to
1257
get the store for a repository.
1259
from bzrlib.store.revision.text import TextRevisionStore
1260
dir_mode = control_files._dir_mode
1261
file_mode = control_files._file_mode
1262
text_store =TextStore(transport.clone(name),
1264
compressed=compressed,
1266
file_mode=file_mode)
1267
_revision_store = TextRevisionStore(text_store, serializer)
1268
return _revision_store
1270
def _get_versioned_file_store(self,
1275
versionedfile_class=weave.WeaveFile,
1276
versionedfile_kwargs={},
1278
weave_transport = control_files._transport.clone(name)
1279
dir_mode = control_files._dir_mode
1280
file_mode = control_files._file_mode
1281
return VersionedFileStore(weave_transport, prefixed=prefixed,
1283
file_mode=file_mode,
1284
versionedfile_class=versionedfile_class,
1285
versionedfile_kwargs=versionedfile_kwargs,
1288
def initialize(self, a_bzrdir, shared=False):
1289
"""Initialize a repository of this format in a_bzrdir.
1291
:param a_bzrdir: The bzrdir to put the new repository in it.
1292
:param shared: The repository should be initialized as a sharable one.
1294
This may raise UninitializableFormat if shared repository are not
1295
compatible the a_bzrdir.
1298
def is_supported(self):
1299
"""Is this format supported?
1301
Supported formats must be initializable and openable.
1302
Unsupported formats may not support initialization or committing or
1303
some other features depending on the reason for not being supported.
1307
def check_conversion_target(self, target_format):
1308
raise NotImplementedError(self.check_conversion_target)
1310
def open(self, a_bzrdir, _found=False):
1311
"""Return an instance of this format for the bzrdir a_bzrdir.
1313
_found is a private parameter, do not use it.
1315
raise NotImplementedError(self.open)
1318
class PreSplitOutRepositoryFormat(RepositoryFormat):
1319
"""Base class for the pre split out repository formats."""
1321
rich_root_data = False
1323
def initialize(self, a_bzrdir, shared=False, _internal=False):
1324
"""Create a weave repository.
1326
TODO: when creating split out bzr branch formats, move this to a common
1327
base for Format5, Format6. or something like that.
1330
raise errors.IncompatibleFormat(self, a_bzrdir._format)
1333
# always initialized when the bzrdir is.
1334
return self.open(a_bzrdir, _found=True)
1336
# Create an empty weave
1338
weavefile.write_weave_v5(weave.Weave(), sio)
1339
empty_weave = sio.getvalue()
1341
mutter('creating repository in %s.', a_bzrdir.transport.base)
1342
dirs = ['revision-store', 'weaves']
1343
files = [('inventory.weave', StringIO(empty_weave)),
1346
# FIXME: RBC 20060125 don't peek under the covers
1347
# NB: no need to escape relative paths that are url safe.
1348
control_files = lockable_files.LockableFiles(a_bzrdir.transport,
1349
'branch-lock', lockable_files.TransportLock)
1350
control_files.create_lock()
1351
control_files.lock_write()
1352
control_files._transport.mkdir_multi(dirs,
1353
mode=control_files._dir_mode)
1355
for file, content in files:
1356
control_files.put(file, content)
1358
control_files.unlock()
1359
return self.open(a_bzrdir, _found=True)
1361
def _get_control_store(self, repo_transport, control_files):
1362
"""Return the control store for this repository."""
1363
return self._get_versioned_file_store('',
1368
def _get_text_store(self, transport, control_files):
1369
"""Get a store for file texts for this format."""
1370
raise NotImplementedError(self._get_text_store)
1372
def open(self, a_bzrdir, _found=False):
1373
"""See RepositoryFormat.open()."""
1375
# we are being called directly and must probe.
1376
raise NotImplementedError
1378
repo_transport = a_bzrdir.get_repository_transport(None)
1379
control_files = a_bzrdir._control_files
1380
text_store = self._get_text_store(repo_transport, control_files)
1381
control_store = self._get_control_store(repo_transport, control_files)
1382
_revision_store = self._get_revision_store(repo_transport, control_files)
1383
return AllInOneRepository(_format=self,
1385
_revision_store=_revision_store,
1386
control_store=control_store,
1387
text_store=text_store)
1389
def check_conversion_target(self, target_format):
1393
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1394
"""Bzr repository format 4.
1396
This repository format has:
1398
- TextStores for texts, inventories,revisions.
1400
This format is deprecated: it indexes texts using a text id which is
1401
removed in format 5; initialization and write support for this format
1406
super(RepositoryFormat4, self).__init__()
1407
self._matchingbzrdir = bzrdir.BzrDirFormat4()
1409
def get_format_description(self):
1410
"""See RepositoryFormat.get_format_description()."""
1411
return "Repository format 4"
1413
def initialize(self, url, shared=False, _internal=False):
1414
"""Format 4 branches cannot be created."""
1415
raise errors.UninitializableFormat(self)
1417
def is_supported(self):
1418
"""Format 4 is not supported.
1420
It is not supported because the model changed from 4 to 5 and the
1421
conversion logic is expensive - so doing it on the fly was not
1426
def _get_control_store(self, repo_transport, control_files):
1427
"""Format 4 repositories have no formal control store at this point.
1429
This will cause any control-file-needing apis to fail - this is desired.
1433
def _get_revision_store(self, repo_transport, control_files):
1434
"""See RepositoryFormat._get_revision_store()."""
1435
from bzrlib.xml4 import serializer_v4
1436
return self._get_text_rev_store(repo_transport,
1439
serializer=serializer_v4)
1441
def _get_text_store(self, transport, control_files):
1442
"""See RepositoryFormat._get_text_store()."""
1445
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1446
"""Bzr control format 5.
1448
This repository format has:
1449
- weaves for file texts and inventory
1451
- TextStores for revisions and signatures.
1455
super(RepositoryFormat5, self).__init__()
1456
self._matchingbzrdir = bzrdir.BzrDirFormat5()
1458
def get_format_description(self):
1459
"""See RepositoryFormat.get_format_description()."""
1460
return "Weave repository format 5"
1462
def _get_revision_store(self, repo_transport, control_files):
1463
"""See RepositoryFormat._get_revision_store()."""
1464
"""Return the revision store object for this a_bzrdir."""
1465
return self._get_text_rev_store(repo_transport,
1470
def _get_text_store(self, transport, control_files):
1471
"""See RepositoryFormat._get_text_store()."""
1472
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1475
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1476
"""Bzr control format 6.
1478
This repository format has:
1479
- weaves for file texts and inventory
1480
- hash subdirectory based stores.
1481
- TextStores for revisions and signatures.
1485
super(RepositoryFormat6, self).__init__()
1486
self._matchingbzrdir = bzrdir.BzrDirFormat6()
1488
def get_format_description(self):
1489
"""See RepositoryFormat.get_format_description()."""
1490
return "Weave repository format 6"
1492
def _get_revision_store(self, repo_transport, control_files):
1493
"""See RepositoryFormat._get_revision_store()."""
1494
return self._get_text_rev_store(repo_transport,
1500
def _get_text_store(self, transport, control_files):
1501
"""See RepositoryFormat._get_text_store()."""
1502
return self._get_versioned_file_store('weaves', transport, control_files)
1505
class MetaDirRepositoryFormat(RepositoryFormat):
1506
"""Common base class for the new repositories using the metadir layout."""
1508
rich_root_data = False
1511
super(MetaDirRepositoryFormat, self).__init__()
1512
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1514
def _create_control_files(self, a_bzrdir):
1515
"""Create the required files and the initial control_files object."""
1516
# FIXME: RBC 20060125 don't peek under the covers
1517
# NB: no need to escape relative paths that are url safe.
1518
repository_transport = a_bzrdir.get_repository_transport(self)
1519
control_files = lockable_files.LockableFiles(repository_transport,
1520
'lock', lockdir.LockDir)
1521
control_files.create_lock()
1522
return control_files
1524
def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1525
"""Upload the initial blank content."""
1526
control_files = self._create_control_files(a_bzrdir)
1527
control_files.lock_write()
1529
control_files._transport.mkdir_multi(dirs,
1530
mode=control_files._dir_mode)
1531
for file, content in files:
1532
control_files.put(file, content)
1533
for file, content in utf8_files:
1534
control_files.put_utf8(file, content)
1536
control_files.put_utf8('shared-storage', '')
1538
control_files.unlock()
1541
class RepositoryFormat7(MetaDirRepositoryFormat):
1542
"""Bzr repository 7.
1544
This repository format has:
1545
- weaves for file texts and inventory
1546
- hash subdirectory based stores.
1547
- TextStores for revisions and signatures.
1548
- a format marker of its own
1549
- an optional 'shared-storage' flag
1550
- an optional 'no-working-trees' flag
1553
def _get_control_store(self, repo_transport, control_files):
1554
"""Return the control store for this repository."""
1555
return self._get_versioned_file_store('',
1560
def get_format_string(self):
1561
"""See RepositoryFormat.get_format_string()."""
1562
return "Bazaar-NG Repository format 7"
1564
def get_format_description(self):
1565
"""See RepositoryFormat.get_format_description()."""
1566
return "Weave repository format 7"
1568
def check_conversion_target(self, target_format):
1571
def _get_revision_store(self, repo_transport, control_files):
1572
"""See RepositoryFormat._get_revision_store()."""
1573
return self._get_text_rev_store(repo_transport,
1580
def _get_text_store(self, transport, control_files):
1581
"""See RepositoryFormat._get_text_store()."""
1582
return self._get_versioned_file_store('weaves',
1586
def initialize(self, a_bzrdir, shared=False):
1587
"""Create a weave repository.
1589
:param shared: If true the repository will be initialized as a shared
1592
# Create an empty weave
1594
weavefile.write_weave_v5(weave.Weave(), sio)
1595
empty_weave = sio.getvalue()
1597
mutter('creating repository in %s.', a_bzrdir.transport.base)
1598
dirs = ['revision-store', 'weaves']
1599
files = [('inventory.weave', StringIO(empty_weave)),
1601
utf8_files = [('format', self.get_format_string())]
1603
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1604
return self.open(a_bzrdir=a_bzrdir, _found=True)
1606
def open(self, a_bzrdir, _found=False, _override_transport=None):
1607
"""See RepositoryFormat.open().
1609
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1610
repository at a slightly different url
1611
than normal. I.e. during 'upgrade'.
1614
format = RepositoryFormat.find_format(a_bzrdir)
1615
assert format.__class__ == self.__class__
1616
if _override_transport is not None:
1617
repo_transport = _override_transport
1619
repo_transport = a_bzrdir.get_repository_transport(None)
1620
control_files = lockable_files.LockableFiles(repo_transport,
1621
'lock', lockdir.LockDir)
1622
text_store = self._get_text_store(repo_transport, control_files)
1623
control_store = self._get_control_store(repo_transport, control_files)
1624
_revision_store = self._get_revision_store(repo_transport, control_files)
1625
return WeaveMetaDirRepository(_format=self,
1627
control_files=control_files,
1628
_revision_store=_revision_store,
1629
control_store=control_store,
1630
text_store=text_store)
1633
class RepositoryFormatKnit(MetaDirRepositoryFormat):
1634
"""Bzr repository knit format (generalized).
1636
This repository format has:
1637
- knits for file texts and inventory
1638
- hash subdirectory based stores.
1639
- knits for revisions and signatures
1640
- TextStores for revisions and signatures.
1641
- a format marker of its own
1642
- an optional 'shared-storage' flag
1643
- an optional 'no-working-trees' flag
1647
def _get_control_store(self, repo_transport, control_files):
1648
"""Return the control store for this repository."""
1649
return VersionedFileStore(
1652
file_mode=control_files._file_mode,
1653
versionedfile_class=knit.KnitVersionedFile,
1654
versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
1657
def _get_revision_store(self, repo_transport, control_files):
1658
"""See RepositoryFormat._get_revision_store()."""
1659
from bzrlib.store.revision.knit import KnitRevisionStore
1660
versioned_file_store = VersionedFileStore(
1662
file_mode=control_files._file_mode,
1665
versionedfile_class=knit.KnitVersionedFile,
1666
versionedfile_kwargs={'delta':False,
1667
'factory':knit.KnitPlainFactory(),
1671
return KnitRevisionStore(versioned_file_store)
1673
def _get_text_store(self, transport, control_files):
1674
"""See RepositoryFormat._get_text_store()."""
1675
return self._get_versioned_file_store('knits',
1678
versionedfile_class=knit.KnitVersionedFile,
1679
versionedfile_kwargs={
1680
'create_parent_dir':True,
1681
'delay_create':True,
1682
'dir_mode':control_files._dir_mode,
1686
def initialize(self, a_bzrdir, shared=False):
1687
"""Create a knit format 1 repository.
1689
:param a_bzrdir: bzrdir to contain the new repository; must already
1691
:param shared: If true the repository will be initialized as a shared
1694
mutter('creating repository in %s.', a_bzrdir.transport.base)
1695
dirs = ['revision-store', 'knits']
1697
utf8_files = [('format', self.get_format_string())]
1699
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1700
repo_transport = a_bzrdir.get_repository_transport(None)
1701
control_files = lockable_files.LockableFiles(repo_transport,
1702
'lock', lockdir.LockDir)
1703
control_store = self._get_control_store(repo_transport, control_files)
1704
transaction = transactions.WriteTransaction()
1705
# trigger a write of the inventory store.
1706
control_store.get_weave_or_empty('inventory', transaction)
1707
_revision_store = self._get_revision_store(repo_transport, control_files)
1708
# the revision id here is irrelevant: it will not be stored, and cannot
1710
_revision_store.has_revision_id('A', transaction)
1711
_revision_store.get_signature_file(transaction)
1712
return self.open(a_bzrdir=a_bzrdir, _found=True)
1714
def open(self, a_bzrdir, _found=False, _override_transport=None):
1715
"""See RepositoryFormat.open().
1717
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1718
repository at a slightly different url
1719
than normal. I.e. during 'upgrade'.
1722
format = RepositoryFormat.find_format(a_bzrdir)
1723
assert format.__class__ == self.__class__
1724
if _override_transport is not None:
1725
repo_transport = _override_transport
1727
repo_transport = a_bzrdir.get_repository_transport(None)
1728
control_files = lockable_files.LockableFiles(repo_transport,
1729
'lock', lockdir.LockDir)
1730
text_store = self._get_text_store(repo_transport, control_files)
1731
control_store = self._get_control_store(repo_transport, control_files)
1732
_revision_store = self._get_revision_store(repo_transport, control_files)
1733
return KnitRepository(_format=self,
1735
control_files=control_files,
1736
_revision_store=_revision_store,
1737
control_store=control_store,
1738
text_store=text_store)
1741
class RepositoryFormatKnit1(RepositoryFormatKnit):
1742
"""Bzr repository knit format 1.
1744
This repository format has:
1745
- knits for file texts and inventory
1746
- hash subdirectory based stores.
1747
- knits for revisions and signatures
1748
- TextStores for revisions and signatures.
1749
- a format marker of its own
1750
- an optional 'shared-storage' flag
1751
- an optional 'no-working-trees' flag
1754
This format was introduced in bzr 0.8.
1756
def get_format_string(self):
1757
"""See RepositoryFormat.get_format_string()."""
1758
return "Bazaar-NG Knit Repository Format 1"
1760
def get_format_description(self):
1761
"""See RepositoryFormat.get_format_description()."""
1762
return "Knit repository format 1"
1764
def check_conversion_target(self, target_format):
1768
class RepositoryFormatKnit2(RepositoryFormatKnit):
1769
"""Bzr repository knit format 2.
1771
THIS FORMAT IS EXPERIMENTAL
1772
This repository format has:
1773
- knits for file texts and inventory
1774
- hash subdirectory based stores.
1775
- knits for revisions and signatures
1776
- TextStores for revisions and signatures.
1777
- a format marker of its own
1778
- an optional 'shared-storage' flag
1779
- an optional 'no-working-trees' flag
1781
- Support for recording full info about the tree root
1785
rich_root_data = True
1787
def get_format_string(self):
1788
"""See RepositoryFormat.get_format_string()."""
1789
return "Bazaar Knit Repository Format 2\n"
1791
def get_format_description(self):
1792
"""See RepositoryFormat.get_format_description()."""
1793
return "Knit repository format 2"
1795
def check_conversion_target(self, target_format):
1796
if not target_format.rich_root_data:
1797
raise errors.BadConversionTarget(
1798
'Does not support rich root data.', target_format)
1800
def open(self, a_bzrdir, _found=False, _override_transport=None):
1801
"""See RepositoryFormat.open().
1803
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1804
repository at a slightly different url
1805
than normal. I.e. during 'upgrade'.
1808
format = RepositoryFormat.find_format(a_bzrdir)
1809
assert format.__class__ == self.__class__
1810
if _override_transport is not None:
1811
repo_transport = _override_transport
1813
repo_transport = a_bzrdir.get_repository_transport(None)
1814
control_files = lockable_files.LockableFiles(repo_transport, 'lock',
1816
text_store = self._get_text_store(repo_transport, control_files)
1817
control_store = self._get_control_store(repo_transport, control_files)
1818
_revision_store = self._get_revision_store(repo_transport, control_files)
1819
return KnitRepository2(_format=self,
1821
control_files=control_files,
1822
_revision_store=_revision_store,
1823
control_store=control_store,
1824
text_store=text_store)
1828
# formats which have no format string are not discoverable
1829
# and not independently creatable, so are not registered.
1830
RepositoryFormat.register_format(RepositoryFormat7())
1831
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1832
# default control directory format
1833
_default_format = RepositoryFormatKnit1()
1834
RepositoryFormat.register_format(_default_format)
1835
RepositoryFormat.register_format(RepositoryFormatKnit2())
1836
RepositoryFormat._set_default_format(_default_format)
1837
_legacy_formats = [RepositoryFormat4(),
1838
RepositoryFormat5(),
1839
RepositoryFormat6()]
1842
class InterRepository(InterObject):
1843
"""This class represents operations taking place between two repositories.
1845
Its instances have methods like copy_content and fetch, and contain
1846
references to the source and target repositories these operations can be
1849
Often we will provide convenience methods on 'repository' which carry out
1850
operations with another repository - they will always forward to
1851
InterRepository.get(other).method_name(parameters).
1855
"""The available optimised InterRepository types."""
1857
def copy_content(self, revision_id=None, basis=None):
1858
raise NotImplementedError(self.copy_content)
1860
def fetch(self, revision_id=None, pb=None):
1861
"""Fetch the content required to construct revision_id.
1863
The content is copied from self.source to self.target.
1865
:param revision_id: if None all content is copied, if NULL_REVISION no
1867
:param pb: optional progress bar to use for progress reports. If not
1868
provided a default one will be created.
1870
Returns the copied revision count and the failed revisions in a tuple:
1873
raise NotImplementedError(self.fetch)
1876
def missing_revision_ids(self, revision_id=None):
1877
"""Return the revision ids that source has that target does not.
1879
These are returned in topological order.
1881
:param revision_id: only return revision ids included by this
1884
# generic, possibly worst case, slow code path.
1885
target_ids = set(self.target.all_revision_ids())
1886
if revision_id is not None:
1887
source_ids = self.source.get_ancestry(revision_id)
1888
assert source_ids[0] is None
1891
source_ids = self.source.all_revision_ids()
1892
result_set = set(source_ids).difference(target_ids)
1893
# this may look like a no-op: its not. It preserves the ordering
1894
# other_ids had while only returning the members from other_ids
1895
# that we've decided we need.
1896
return [rev_id for rev_id in source_ids if rev_id in result_set]
1899
class InterSameDataRepository(InterRepository):
1900
"""Code for converting between repositories that represent the same data.
1902
Data format and model must match for this to work.
1905
_matching_repo_format = RepositoryFormat4()
1906
"""Repository format for testing with."""
1909
def is_compatible(source, target):
1910
if not isinstance(source, Repository):
1912
if not isinstance(target, Repository):
1914
if source._format.rich_root_data == target._format.rich_root_data:
1920
def copy_content(self, revision_id=None, basis=None):
1921
"""Make a complete copy of the content in self into destination.
1923
This is a destructive operation! Do not use it on existing
1926
:param revision_id: Only copy the content needed to construct
1927
revision_id and its parents.
1928
:param basis: Copy the needed data preferentially from basis.
1931
self.target.set_make_working_trees(self.source.make_working_trees())
1932
except NotImplementedError:
1934
# grab the basis available data
1935
if basis is not None:
1936
self.target.fetch(basis, revision_id=revision_id)
1937
# but don't bother fetching if we have the needed data now.
1938
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
1939
self.target.has_revision(revision_id)):
1941
self.target.fetch(self.source, revision_id=revision_id)
1944
def fetch(self, revision_id=None, pb=None):
1945
"""See InterRepository.fetch()."""
1946
from bzrlib.fetch import GenericRepoFetcher
1947
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1948
self.source, self.source._format, self.target,
1949
self.target._format)
1950
f = GenericRepoFetcher(to_repository=self.target,
1951
from_repository=self.source,
1952
last_revision=revision_id,
1954
return f.count_copied, f.failed_revisions
1957
class InterWeaveRepo(InterSameDataRepository):
1958
"""Optimised code paths between Weave based repositories."""
1960
_matching_repo_format = RepositoryFormat7()
1961
"""Repository format for testing with."""
1964
def is_compatible(source, target):
1965
"""Be compatible with known Weave formats.
1967
We don't test for the stores being of specific types because that
1968
could lead to confusing results, and there is no need to be
1972
return (isinstance(source._format, (RepositoryFormat5,
1974
RepositoryFormat7)) and
1975
isinstance(target._format, (RepositoryFormat5,
1977
RepositoryFormat7)))
1978
except AttributeError:
1982
def copy_content(self, revision_id=None, basis=None):
1983
"""See InterRepository.copy_content()."""
1984
# weave specific optimised path:
1985
if basis is not None:
1986
# copy the basis in, then fetch remaining data.
1987
basis.copy_content_into(self.target, revision_id)
1988
# the basis copy_content_into could miss-set this.
1990
self.target.set_make_working_trees(self.source.make_working_trees())
1991
except NotImplementedError:
1993
self.target.fetch(self.source, revision_id=revision_id)
1996
self.target.set_make_working_trees(self.source.make_working_trees())
1997
except NotImplementedError:
1999
# FIXME do not peek!
2000
if self.source.control_files._transport.listable():
2001
pb = ui.ui_factory.nested_progress_bar()
2003
self.target.weave_store.copy_all_ids(
2004
self.source.weave_store,
2006
from_transaction=self.source.get_transaction(),
2007
to_transaction=self.target.get_transaction())
2008
pb.update('copying inventory', 0, 1)
2009
self.target.control_weaves.copy_multi(
2010
self.source.control_weaves, ['inventory'],
2011
from_transaction=self.source.get_transaction(),
2012
to_transaction=self.target.get_transaction())
2013
self.target._revision_store.text_store.copy_all_ids(
2014
self.source._revision_store.text_store,
2019
self.target.fetch(self.source, revision_id=revision_id)
2022
def fetch(self, revision_id=None, pb=None):
2023
"""See InterRepository.fetch()."""
2024
from bzrlib.fetch import GenericRepoFetcher
2025
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2026
self.source, self.source._format, self.target, self.target._format)
2027
f = GenericRepoFetcher(to_repository=self.target,
2028
from_repository=self.source,
2029
last_revision=revision_id,
2031
return f.count_copied, f.failed_revisions
2034
def missing_revision_ids(self, revision_id=None):
2035
"""See InterRepository.missing_revision_ids()."""
2036
# we want all revisions to satisfy revision_id in source.
2037
# but we don't want to stat every file here and there.
2038
# we want then, all revisions other needs to satisfy revision_id
2039
# checked, but not those that we have locally.
2040
# so the first thing is to get a subset of the revisions to
2041
# satisfy revision_id in source, and then eliminate those that
2042
# we do already have.
2043
# this is slow on high latency connection to self, but as as this
2044
# disk format scales terribly for push anyway due to rewriting
2045
# inventory.weave, this is considered acceptable.
2047
if revision_id is not None:
2048
source_ids = self.source.get_ancestry(revision_id)
2049
assert source_ids[0] is None
2052
source_ids = self.source._all_possible_ids()
2053
source_ids_set = set(source_ids)
2054
# source_ids is the worst possible case we may need to pull.
2055
# now we want to filter source_ids against what we actually
2056
# have in target, but don't try to check for existence where we know
2057
# we do not have a revision as that would be pointless.
2058
target_ids = set(self.target._all_possible_ids())
2059
possibly_present_revisions = target_ids.intersection(source_ids_set)
2060
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2061
required_revisions = source_ids_set.difference(actually_present_revisions)
2062
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2063
if revision_id is not None:
2064
# we used get_ancestry to determine source_ids then we are assured all
2065
# revisions referenced are present as they are installed in topological order.
2066
# and the tip revision was validated by get_ancestry.
2067
return required_topo_revisions
2069
# if we just grabbed the possibly available ids, then
2070
# we only have an estimate of whats available and need to validate
2071
# that against the revision records.
2072
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2075
class InterKnitRepo(InterSameDataRepository):
2076
"""Optimised code paths between Knit based repositories."""
2078
_matching_repo_format = RepositoryFormatKnit1()
2079
"""Repository format for testing with."""
2082
def is_compatible(source, target):
2083
"""Be compatible with known Knit formats.
2085
We don't test for the stores being of specific types because that
2086
could lead to confusing results, and there is no need to be
2090
return (isinstance(source._format, (RepositoryFormatKnit1)) and
2091
isinstance(target._format, (RepositoryFormatKnit1)))
2092
except AttributeError:
2096
def fetch(self, revision_id=None, pb=None):
2097
"""See InterRepository.fetch()."""
2098
from bzrlib.fetch import KnitRepoFetcher
2099
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2100
self.source, self.source._format, self.target, self.target._format)
2101
f = KnitRepoFetcher(to_repository=self.target,
2102
from_repository=self.source,
2103
last_revision=revision_id,
2105
return f.count_copied, f.failed_revisions
2108
def missing_revision_ids(self, revision_id=None):
2109
"""See InterRepository.missing_revision_ids()."""
2110
if revision_id is not None:
2111
source_ids = self.source.get_ancestry(revision_id)
2112
assert source_ids[0] is None
2115
source_ids = self.source._all_possible_ids()
2116
source_ids_set = set(source_ids)
2117
# source_ids is the worst possible case we may need to pull.
2118
# now we want to filter source_ids against what we actually
2119
# have in target, but don't try to check for existence where we know
2120
# we do not have a revision as that would be pointless.
2121
target_ids = set(self.target._all_possible_ids())
2122
possibly_present_revisions = target_ids.intersection(source_ids_set)
2123
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2124
required_revisions = source_ids_set.difference(actually_present_revisions)
2125
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2126
if revision_id is not None:
2127
# we used get_ancestry to determine source_ids then we are assured all
2128
# revisions referenced are present as they are installed in topological order.
2129
# and the tip revision was validated by get_ancestry.
2130
return required_topo_revisions
2132
# if we just grabbed the possibly available ids, then
2133
# we only have an estimate of whats available and need to validate
2134
# that against the revision records.
2135
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2138
class InterModel1and2(InterRepository):
2140
_matching_repo_format = None
2143
def is_compatible(source, target):
2144
if not isinstance(source, Repository):
2146
if not isinstance(target, Repository):
2148
if not source._format.rich_root_data and target._format.rich_root_data:
2154
def fetch(self, revision_id=None, pb=None):
2155
"""See InterRepository.fetch()."""
2156
from bzrlib.fetch import Model1toKnit2Fetcher
2157
f = Model1toKnit2Fetcher(to_repository=self.target,
2158
from_repository=self.source,
2159
last_revision=revision_id,
2161
return f.count_copied, f.failed_revisions
2164
def copy_content(self, revision_id=None, basis=None):
2165
"""Make a complete copy of the content in self into destination.
2167
This is a destructive operation! Do not use it on existing
2170
:param revision_id: Only copy the content needed to construct
2171
revision_id and its parents.
2172
:param basis: Copy the needed data preferentially from basis.
2175
self.target.set_make_working_trees(self.source.make_working_trees())
2176
except NotImplementedError:
2178
# grab the basis available data
2179
if basis is not None:
2180
self.target.fetch(basis, revision_id=revision_id)
2181
# but don't bother fetching if we have the needed data now.
2182
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2183
self.target.has_revision(revision_id)):
2185
self.target.fetch(self.source, revision_id=revision_id)
2188
class InterKnit1and2(InterKnitRepo):
2190
_matching_repo_format = None
2193
def is_compatible(source, target):
2194
"""Be compatible with Knit1 source and Knit2 target"""
2196
return (isinstance(source._format, (RepositoryFormatKnit1)) and
2197
isinstance(target._format, (RepositoryFormatKnit2)))
2198
except AttributeError:
2202
def fetch(self, revision_id=None, pb=None):
2203
"""See InterRepository.fetch()."""
2204
from bzrlib.fetch import Knit1to2Fetcher
2205
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2206
self.source, self.source._format, self.target,
2207
self.target._format)
2208
f = Knit1to2Fetcher(to_repository=self.target,
2209
from_repository=self.source,
2210
last_revision=revision_id,
2212
return f.count_copied, f.failed_revisions
2215
InterRepository.register_optimiser(InterSameDataRepository)
2216
InterRepository.register_optimiser(InterWeaveRepo)
2217
InterRepository.register_optimiser(InterKnitRepo)
2218
InterRepository.register_optimiser(InterModel1and2)
2219
InterRepository.register_optimiser(InterKnit1and2)
2222
class RepositoryTestProviderAdapter(object):
2223
"""A tool to generate a suite testing multiple repository formats at once.
2225
This is done by copying the test once for each transport and injecting
2226
the transport_server, transport_readonly_server, and bzrdir_format and
2227
repository_format classes into each copy. Each copy is also given a new id()
2228
to make it easy to identify.
2231
def __init__(self, transport_server, transport_readonly_server, formats):
2232
self._transport_server = transport_server
2233
self._transport_readonly_server = transport_readonly_server
2234
self._formats = formats
2236
def adapt(self, test):
2237
result = unittest.TestSuite()
2238
for repository_format, bzrdir_format in self._formats:
2239
new_test = deepcopy(test)
2240
new_test.transport_server = self._transport_server
2241
new_test.transport_readonly_server = self._transport_readonly_server
2242
new_test.bzrdir_format = bzrdir_format
2243
new_test.repository_format = repository_format
2244
def make_new_test_id():
2245
new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
2246
return lambda: new_id
2247
new_test.id = make_new_test_id()
2248
result.addTest(new_test)
2252
class InterRepositoryTestProviderAdapter(object):
2253
"""A tool to generate a suite testing multiple inter repository formats.
2255
This is done by copying the test once for each interrepo provider and injecting
2256
the transport_server, transport_readonly_server, repository_format and
2257
repository_to_format classes into each copy.
2258
Each copy is also given a new id() to make it easy to identify.
2261
def __init__(self, transport_server, transport_readonly_server, formats):
2262
self._transport_server = transport_server
2263
self._transport_readonly_server = transport_readonly_server
2264
self._formats = formats
2266
def adapt(self, test):
2267
result = unittest.TestSuite()
2268
for interrepo_class, repository_format, repository_format_to in self._formats:
2269
new_test = deepcopy(test)
2270
new_test.transport_server = self._transport_server
2271
new_test.transport_readonly_server = self._transport_readonly_server
2272
new_test.interrepo_class = interrepo_class
2273
new_test.repository_format = repository_format
2274
new_test.repository_format_to = repository_format_to
2275
def make_new_test_id():
2276
new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
2277
return lambda: new_id
2278
new_test.id = make_new_test_id()
2279
result.addTest(new_test)
2283
def default_test_list():
2284
"""Generate the default list of interrepo permutations to test."""
2286
# test the default InterRepository between format 6 and the current
2288
# XXX: robertc 20060220 reinstate this when there are two supported
2289
# formats which do not have an optimal code path between them.
2290
#result.append((InterRepository,
2291
# RepositoryFormat6(),
2292
# RepositoryFormatKnit1()))
2293
for optimiser in InterRepository._optimisers:
2294
if optimiser._matching_repo_format is not None:
2295
result.append((optimiser,
2296
optimiser._matching_repo_format,
2297
optimiser._matching_repo_format
2299
# if there are specific combinations we want to use, we can add them
2301
result.append((InterModel1and2, RepositoryFormat5(),
2302
RepositoryFormatKnit2()))
2303
result.append((InterKnit1and2, RepositoryFormatKnit1(),
2304
RepositoryFormatKnit2()))
2308
class CopyConverter(object):
2309
"""A repository conversion tool which just performs a copy of the content.
2311
This is slow but quite reliable.
2314
def __init__(self, target_format):
2315
"""Create a CopyConverter.
2317
:param target_format: The format the resulting repository should be.
2319
self.target_format = target_format
2321
def convert(self, repo, pb):
2322
"""Perform the conversion of to_convert, giving feedback via pb.
2324
:param to_convert: The disk object to convert.
2325
:param pb: a progress bar to use for progress information.
2330
# this is only useful with metadir layouts - separated repo content.
2331
# trigger an assertion if not such
2332
repo._format.get_format_string()
2333
self.repo_dir = repo.bzrdir
2334
self.step('Moving repository to repository.backup')
2335
self.repo_dir.transport.move('repository', 'repository.backup')
2336
backup_transport = self.repo_dir.transport.clone('repository.backup')
2337
repo._format.check_conversion_target(self.target_format)
2338
self.source_repo = repo._format.open(self.repo_dir,
2340
_override_transport=backup_transport)
2341
self.step('Creating new repository')
2342
converted = self.target_format.initialize(self.repo_dir,
2343
self.source_repo.is_shared())
2344
converted.lock_write()
2346
self.step('Copying content into repository.')
2347
self.source_repo.copy_content_into(converted)
2350
self.step('Deleting old repository content.')
2351
self.repo_dir.transport.delete_tree('repository.backup')
2352
self.pb.note('repository converted')
2354
def step(self, message):
2355
"""Update the pb by a step."""
2357
self.pb.update(message, self.count, self.total)
2360
class CommitBuilder(object):
2361
"""Provides an interface to build up a commit.
2363
This allows describing a tree to be committed without needing to
2364
know the internals of the format of the repository.
2367
record_root_entry = False
2368
def __init__(self, repository, parents, config, timestamp=None,
2369
timezone=None, committer=None, revprops=None,
2371
"""Initiate a CommitBuilder.
2373
:param repository: Repository to commit to.
2374
:param parents: Revision ids of the parents of the new revision.
2375
:param config: Configuration to use.
2376
:param timestamp: Optional timestamp recorded for commit.
2377
:param timezone: Optional timezone for timestamp.
2378
:param committer: Optional committer to set for commit.
2379
:param revprops: Optional dictionary of revision properties.
2380
:param revision_id: Optional revision id.
2382
self._config = config
2384
if committer is None:
2385
self._committer = self._config.username()
2387
assert isinstance(committer, basestring), type(committer)
2388
self._committer = committer
2390
self.new_inventory = Inventory(None)
2391
self._new_revision_id = revision_id
2392
self.parents = parents
2393
self.repository = repository
2396
if revprops is not None:
2397
self._revprops.update(revprops)
2399
if timestamp is None:
2400
timestamp = time.time()
2401
# Restrict resolution to 1ms
2402
self._timestamp = round(timestamp, 3)
2404
if timezone is None:
2405
self._timezone = local_time_offset()
2407
self._timezone = int(timezone)
2409
self._generate_revision_if_needed()
2411
def commit(self, message):
2412
"""Make the actual commit.
2414
:return: The revision id of the recorded revision.
2416
rev = _mod_revision.Revision(
2417
timestamp=self._timestamp,
2418
timezone=self._timezone,
2419
committer=self._committer,
2421
inventory_sha1=self.inv_sha1,
2422
revision_id=self._new_revision_id,
2423
properties=self._revprops)
2424
rev.parent_ids = self.parents
2425
self.repository.add_revision(self._new_revision_id, rev,
2426
self.new_inventory, self._config)
2427
return self._new_revision_id
2429
def revision_tree(self):
2430
"""Return the tree that was just committed.
2432
After calling commit() this can be called to get a RevisionTree
2433
representing the newly committed tree. This is preferred to
2434
calling Repository.revision_tree() because that may require
2435
deserializing the inventory, while we already have a copy in
2438
return RevisionTree(self.repository, self.new_inventory,
2439
self._new_revision_id)
2441
def finish_inventory(self):
2442
"""Tell the builder that the inventory is finished."""
2443
if self.new_inventory.root is None:
2444
symbol_versioning.warn('Root entry should be supplied to'
2445
' record_entry_contents, as of bzr 0.10.',
2446
DeprecationWarning, stacklevel=2)
2447
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
2448
self.new_inventory.revision_id = self._new_revision_id
2449
self.inv_sha1 = self.repository.add_inventory(
2450
self._new_revision_id,
2455
def _gen_revision_id(self):
2456
"""Return new revision-id."""
2457
return generate_ids.gen_revision_id(self._config.username(),
2460
def _generate_revision_if_needed(self):
2461
"""Create a revision id if None was supplied.
2463
If the repository can not support user-specified revision ids
2464
they should override this function and raise CannotSetRevisionId
2465
if _new_revision_id is not None.
2467
:raises: CannotSetRevisionId
2469
if self._new_revision_id is None:
2470
self._new_revision_id = self._gen_revision_id()
2472
def record_entry_contents(self, ie, parent_invs, path, tree):
2473
"""Record the content of ie from tree into the commit if needed.
2475
Side effect: sets ie.revision when unchanged
2477
:param ie: An inventory entry present in the commit.
2478
:param parent_invs: The inventories of the parent revisions of the
2480
:param path: The path the entry is at in the tree.
2481
:param tree: The tree which contains this entry and should be used to
2484
if self.new_inventory.root is None and ie.parent_id is not None:
2485
symbol_versioning.warn('Root entry should be supplied to'
2486
' record_entry_contents, as of bzr 0.10.',
2487
DeprecationWarning, stacklevel=2)
2488
self.record_entry_contents(tree.inventory.root.copy(), parent_invs,
2490
self.new_inventory.add(ie)
2492
# ie.revision is always None if the InventoryEntry is considered
2493
# for committing. ie.snapshot will record the correct revision
2494
# which may be the sole parent if it is untouched.
2495
if ie.revision is not None:
2498
# In this revision format, root entries have no knit or weave
2499
if ie is self.new_inventory.root:
2500
# When serializing out to disk and back in
2501
# root.revision is always _new_revision_id
2502
ie.revision = self._new_revision_id
2504
previous_entries = ie.find_previous_heads(
2506
self.repository.weave_store,
2507
self.repository.get_transaction())
2508
# we are creating a new revision for ie in the history store
2510
ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2512
def modified_directory(self, file_id, file_parents):
2513
"""Record the presence of a symbolic link.
2515
:param file_id: The file_id of the link to record.
2516
:param file_parents: The per-file parent revision ids.
2518
self._add_text_to_weave(file_id, [], file_parents.keys())
2520
def modified_file_text(self, file_id, file_parents,
2521
get_content_byte_lines, text_sha1=None,
2523
"""Record the text of file file_id
2525
:param file_id: The file_id of the file to record the text of.
2526
:param file_parents: The per-file parent revision ids.
2527
:param get_content_byte_lines: A callable which will return the byte
2529
:param text_sha1: Optional SHA1 of the file contents.
2530
:param text_size: Optional size of the file contents.
2532
# mutter('storing text of file {%s} in revision {%s} into %r',
2533
# file_id, self._new_revision_id, self.repository.weave_store)
2534
# special case to avoid diffing on renames or
2536
if (len(file_parents) == 1
2537
and text_sha1 == file_parents.values()[0].text_sha1
2538
and text_size == file_parents.values()[0].text_size):
2539
previous_ie = file_parents.values()[0]
2540
versionedfile = self.repository.weave_store.get_weave(file_id,
2541
self.repository.get_transaction())
2542
versionedfile.clone_text(self._new_revision_id,
2543
previous_ie.revision, file_parents.keys())
2544
return text_sha1, text_size
2546
new_lines = get_content_byte_lines()
2547
# TODO: Rather than invoking sha_strings here, _add_text_to_weave
2548
# should return the SHA1 and size
2549
self._add_text_to_weave(file_id, new_lines, file_parents.keys())
2550
return osutils.sha_strings(new_lines), \
2551
sum(map(len, new_lines))
2553
def modified_link(self, file_id, file_parents, link_target):
2554
"""Record the presence of a symbolic link.
2556
:param file_id: The file_id of the link to record.
2557
:param file_parents: The per-file parent revision ids.
2558
:param link_target: Target location of this link.
2560
self._add_text_to_weave(file_id, [], file_parents.keys())
2562
def _add_text_to_weave(self, file_id, new_lines, parents):
2563
versionedfile = self.repository.weave_store.get_weave_or_empty(
2564
file_id, self.repository.get_transaction())
2565
versionedfile.add_lines(self._new_revision_id, parents, new_lines)
2566
versionedfile.clear_cache()
2569
class _CommitBuilder(CommitBuilder):
2570
"""Temporary class so old CommitBuilders are detected properly
2572
Note: CommitBuilder works whether or not root entry is recorded.
2575
record_root_entry = True
2578
class RootCommitBuilder(CommitBuilder):
2579
"""This commitbuilder actually records the root id"""
2581
record_root_entry = True
2583
def record_entry_contents(self, ie, parent_invs, path, tree):
2584
"""Record the content of ie from tree into the commit if needed.
2586
Side effect: sets ie.revision when unchanged
2588
:param ie: An inventory entry present in the commit.
2589
:param parent_invs: The inventories of the parent revisions of the
2591
:param path: The path the entry is at in the tree.
2592
:param tree: The tree which contains this entry and should be used to
2595
assert self.new_inventory.root is not None or ie.parent_id is None
2596
self.new_inventory.add(ie)
2598
# ie.revision is always None if the InventoryEntry is considered
2599
# for committing. ie.snapshot will record the correct revision
2600
# which may be the sole parent if it is untouched.
2601
if ie.revision is not None:
2604
previous_entries = ie.find_previous_heads(
2606
self.repository.weave_store,
2607
self.repository.get_transaction())
2608
# we are creating a new revision for ie in the history store
2610
ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2622
def _unescaper(match, _map=_unescape_map):
2623
return _map[match.group(1)]
2629
def _unescape_xml(data):
2630
"""Unescape predefined XML entities in a string of data."""
2632
if _unescape_re is None:
2633
_unescape_re = re.compile('\&([^;]*);')
2634
return _unescape_re.sub(_unescaper, data)