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(), """
39
revision as _mod_revision,
44
from bzrlib.bundle import serializer
45
from bzrlib.revisiontree import RevisionTree
46
from bzrlib.store.versioned import VersionedFileStore
47
from bzrlib.store.text import TextStore
48
from bzrlib.testament import Testament
49
from bzrlib.util import bencode
52
from bzrlib.decorators import needs_read_lock, needs_write_lock
53
from bzrlib.inter import InterObject
54
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
55
from bzrlib.symbol_versioning import (
58
from bzrlib.trace import mutter, mutter_callsite, note, warning
61
# Old formats display a warning, but only once
62
_deprecation_warning_done = False
65
class CommitBuilder(object):
66
"""Provides an interface to build up a commit.
68
This allows describing a tree to be committed without needing to
69
know the internals of the format of the repository.
72
# all clients should supply tree roots.
73
record_root_entry = True
74
# the default CommitBuilder does not manage trees whose root is versioned.
75
_versioned_root = False
77
def __init__(self, repository, parents, config, timestamp=None,
78
timezone=None, committer=None, revprops=None,
80
"""Initiate a CommitBuilder.
82
:param repository: Repository to commit to.
83
:param parents: Revision ids of the parents of the new revision.
84
:param config: Configuration to use.
85
:param timestamp: Optional timestamp recorded for commit.
86
:param timezone: Optional timezone for timestamp.
87
:param committer: Optional committer to set for commit.
88
:param revprops: Optional dictionary of revision properties.
89
:param revision_id: Optional revision id.
94
self._committer = self._config.username()
96
assert isinstance(committer, basestring), type(committer)
97
self._committer = committer
99
self.new_inventory = Inventory(None)
100
self._new_revision_id = revision_id
101
self.parents = parents
102
self.repository = repository
105
if revprops is not None:
106
self._revprops.update(revprops)
108
if timestamp is None:
109
timestamp = time.time()
110
# Restrict resolution to 1ms
111
self._timestamp = round(timestamp, 3)
114
self._timezone = osutils.local_time_offset()
116
self._timezone = int(timezone)
118
self._generate_revision_if_needed()
119
self._heads = graph.HeadsCache(repository.get_graph()).heads
121
def commit(self, message):
122
"""Make the actual commit.
124
:return: The revision id of the recorded revision.
126
rev = _mod_revision.Revision(
127
timestamp=self._timestamp,
128
timezone=self._timezone,
129
committer=self._committer,
131
inventory_sha1=self.inv_sha1,
132
revision_id=self._new_revision_id,
133
properties=self._revprops)
134
rev.parent_ids = self.parents
135
self.repository.add_revision(self._new_revision_id, rev,
136
self.new_inventory, self._config)
137
self.repository.commit_write_group()
138
return self._new_revision_id
141
"""Abort the commit that is being built.
143
self.repository.abort_write_group()
145
def revision_tree(self):
146
"""Return the tree that was just committed.
148
After calling commit() this can be called to get a RevisionTree
149
representing the newly committed tree. This is preferred to
150
calling Repository.revision_tree() because that may require
151
deserializing the inventory, while we already have a copy in
154
return RevisionTree(self.repository, self.new_inventory,
155
self._new_revision_id)
157
def finish_inventory(self):
158
"""Tell the builder that the inventory is finished."""
159
if self.new_inventory.root is None:
160
raise AssertionError('Root entry should be supplied to'
161
' record_entry_contents, as of bzr 0.10.',
162
DeprecationWarning, stacklevel=2)
163
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
164
self.new_inventory.revision_id = self._new_revision_id
165
self.inv_sha1 = self.repository.add_inventory(
166
self._new_revision_id,
171
def _gen_revision_id(self):
172
"""Return new revision-id."""
173
return generate_ids.gen_revision_id(self._config.username(),
176
def _generate_revision_if_needed(self):
177
"""Create a revision id if None was supplied.
179
If the repository can not support user-specified revision ids
180
they should override this function and raise CannotSetRevisionId
181
if _new_revision_id is not None.
183
:raises: CannotSetRevisionId
185
if self._new_revision_id is None:
186
self._new_revision_id = self._gen_revision_id()
187
self.random_revid = True
189
self.random_revid = False
191
def _check_root(self, ie, parent_invs, tree):
192
"""Helper for record_entry_contents.
194
:param ie: An entry being added.
195
:param parent_invs: The inventories of the parent revisions of the
197
:param tree: The tree that is being committed.
199
# In this revision format, root entries have no knit or weave When
200
# serializing out to disk and back in root.revision is always
202
ie.revision = self._new_revision_id
204
def _get_delta(self, ie, basis_inv, path):
205
"""Get a delta against the basis inventory for ie."""
206
if ie.file_id not in basis_inv:
208
return (None, path, ie.file_id, ie)
209
elif ie != basis_inv[ie.file_id]:
211
# TODO: avoid tis id2path call.
212
return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
217
def record_entry_contents(self, ie, parent_invs, path, tree,
219
"""Record the content of ie from tree into the commit if needed.
221
Side effect: sets ie.revision when unchanged
223
:param ie: An inventory entry present in the commit.
224
:param parent_invs: The inventories of the parent revisions of the
226
:param path: The path the entry is at in the tree.
227
:param tree: The tree which contains this entry and should be used to
229
:param content_summary: Summary data from the tree about the paths
230
content - stat, length, exec, sha/link target. This is only
231
accessed when the entry has a revision of None - that is when it is
232
a candidate to commit.
233
:return: A tuple (change_delta, version_recorded). change_delta is
234
an inventory_delta change for this entry against the basis tree of
235
the commit, or None if no change occured against the basis tree.
236
version_recorded is True if a new version of the entry has been
237
recorded. For instance, committing a merge where a file was only
238
changed on the other side will return (delta, False).
240
if self.new_inventory.root is None:
241
if ie.parent_id is not None:
242
raise errors.RootMissing()
243
self._check_root(ie, parent_invs, tree)
244
if ie.revision is None:
245
kind = content_summary[0]
247
# ie is carried over from a prior commit
249
# XXX: repository specific check for nested tree support goes here - if
250
# the repo doesn't want nested trees we skip it ?
251
if (kind == 'tree-reference' and
252
not self.repository._format.supports_tree_reference):
253
# mismatch between commit builder logic and repository:
254
# this needs the entry creation pushed down into the builder.
255
raise NotImplementedError('Missing repository subtree support.')
256
self.new_inventory.add(ie)
258
# TODO: slow, take it out of the inner loop.
260
basis_inv = parent_invs[0]
262
basis_inv = Inventory(root_id=None)
264
# ie.revision is always None if the InventoryEntry is considered
265
# for committing. We may record the previous parents revision if the
266
# content is actually unchanged against a sole head.
267
if ie.revision is not None:
268
if not self._versioned_root and path == '':
269
# repositories that do not version the root set the root's
270
# revision to the new commit even when no change occurs, and
271
# this masks when a change may have occurred against the basis,
272
# so calculate if one happened.
273
if ie.file_id in basis_inv:
274
delta = (basis_inv.id2path(ie.file_id), path,
278
delta = (None, path, ie.file_id, ie)
281
# we don't need to commit this, because the caller already
282
# determined that an existing revision of this file is
284
return None, (ie.revision == self._new_revision_id)
285
# XXX: Friction: parent_candidates should return a list not a dict
286
# so that we don't have to walk the inventories again.
287
parent_candiate_entries = ie.parent_candidates(parent_invs)
288
head_set = self._heads(parent_candiate_entries.keys())
290
for inv in parent_invs:
291
if ie.file_id in inv:
292
old_rev = inv[ie.file_id].revision
293
if old_rev in head_set:
294
heads.append(inv[ie.file_id].revision)
295
head_set.remove(inv[ie.file_id].revision)
298
# now we check to see if we need to write a new record to the
300
# We write a new entry unless there is one head to the ancestors, and
301
# the kind-derived content is unchanged.
303
# Cheapest check first: no ancestors, or more the one head in the
304
# ancestors, we write a new node.
308
# There is a single head, look it up for comparison
309
parent_entry = parent_candiate_entries[heads[0]]
310
# if the non-content specific data has changed, we'll be writing a
312
if (parent_entry.parent_id != ie.parent_id or
313
parent_entry.name != ie.name):
315
# now we need to do content specific checks:
317
# if the kind changed the content obviously has
318
if kind != parent_entry.kind:
322
if (# if the file length changed we have to store:
323
parent_entry.text_size != content_summary[1] or
324
# if the exec bit has changed we have to store:
325
parent_entry.executable != content_summary[2]):
327
elif parent_entry.text_sha1 == content_summary[3]:
328
# all meta and content is unchanged (using a hash cache
329
# hit to check the sha)
330
ie.revision = parent_entry.revision
331
ie.text_size = parent_entry.text_size
332
ie.text_sha1 = parent_entry.text_sha1
333
ie.executable = parent_entry.executable
334
return self._get_delta(ie, basis_inv, path), False
336
# Either there is only a hash change(no hash cache entry,
337
# or same size content change), or there is no change on
339
# Provide the parent's hash to the store layer, so that the
340
# content is unchanged we will not store a new node.
341
nostore_sha = parent_entry.text_sha1
343
# We want to record a new node regardless of the presence or
344
# absence of a content change in the file.
346
ie.executable = content_summary[2]
347
lines = tree.get_file(ie.file_id, path).readlines()
349
ie.text_sha1, ie.text_size = self._add_text_to_weave(
350
ie.file_id, lines, heads, nostore_sha)
351
except errors.ExistingContent:
352
# Turns out that the file content was unchanged, and we were
353
# only going to store a new node if it was changed. Carry over
355
ie.revision = parent_entry.revision
356
ie.text_size = parent_entry.text_size
357
ie.text_sha1 = parent_entry.text_sha1
358
ie.executable = parent_entry.executable
359
return self._get_delta(ie, basis_inv, path), False
360
elif kind == 'directory':
362
# all data is meta here, nothing specific to directory, so
364
ie.revision = parent_entry.revision
365
return self._get_delta(ie, basis_inv, path), False
367
self._add_text_to_weave(ie.file_id, lines, heads, None)
368
elif kind == 'symlink':
369
current_link_target = content_summary[3]
371
# symlink target is not generic metadata, check if it has
373
if current_link_target != parent_entry.symlink_target:
376
# unchanged, carry over.
377
ie.revision = parent_entry.revision
378
ie.symlink_target = parent_entry.symlink_target
379
return self._get_delta(ie, basis_inv, path), False
380
ie.symlink_target = current_link_target
382
self._add_text_to_weave(ie.file_id, lines, heads, None)
383
elif kind == 'tree-reference':
385
if content_summary[3] != parent_entry.reference_revision:
388
# unchanged, carry over.
389
ie.reference_revision = parent_entry.reference_revision
390
ie.revision = parent_entry.revision
391
return self._get_delta(ie, basis_inv, path), False
392
ie.reference_revision = content_summary[3]
394
self._add_text_to_weave(ie.file_id, lines, heads, None)
396
raise NotImplementedError('unknown kind')
397
ie.revision = self._new_revision_id
398
return self._get_delta(ie, basis_inv, path), True
400
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
401
versionedfile = self.repository.weave_store.get_weave_or_empty(
402
file_id, self.repository.get_transaction())
403
# Don't change this to add_lines - add_lines_with_ghosts is cheaper
404
# than add_lines, and allows committing when a parent is ghosted for
406
# Note: as we read the content directly from the tree, we know its not
407
# been turned into unicode or badly split - but a broken tree
408
# implementation could give us bad output from readlines() so this is
409
# not a guarantee of safety. What would be better is always checking
410
# the content during test suite execution. RBC 20070912
412
return versionedfile.add_lines_with_ghosts(
413
self._new_revision_id, parents, new_lines,
414
nostore_sha=nostore_sha, random_id=self.random_revid,
415
check_content=False)[0:2]
417
versionedfile.clear_cache()
420
class RootCommitBuilder(CommitBuilder):
421
"""This commitbuilder actually records the root id"""
423
# the root entry gets versioned properly by this builder.
424
_versioned_root = True
426
def _check_root(self, ie, parent_invs, tree):
427
"""Helper for record_entry_contents.
429
:param ie: An entry being added.
430
:param parent_invs: The inventories of the parent revisions of the
432
:param tree: The tree that is being committed.
436
######################################################################
439
class Repository(object):
440
"""Repository holding history for one or more branches.
442
The repository holds and retrieves historical information including
443
revisions and file history. It's normally accessed only by the Branch,
444
which views a particular line of development through that history.
446
The Repository builds on top of Stores and a Transport, which respectively
447
describe the disk data format and the way of accessing the (possibly
451
# What class to use for a CommitBuilder. Often its simpler to change this
452
# in a Repository class subclass rather than to override
453
# get_commit_builder.
454
_commit_builder_class = CommitBuilder
455
# The search regex used by xml based repositories to determine what things
456
# where changed in a single commit.
457
_file_ids_altered_regex = lazy_regex.lazy_compile(
458
r'file_id="(?P<file_id>[^"]+)"'
459
r'.* revision="(?P<revision_id>[^"]+)"'
462
def abort_write_group(self):
463
"""Commit the contents accrued within the current write group.
465
:seealso: start_write_group.
467
if self._write_group is not self.get_transaction():
468
# has an unlock or relock occured ?
469
raise errors.BzrError('mismatched lock context and write group.')
470
self._abort_write_group()
471
self._write_group = None
473
def _abort_write_group(self):
474
"""Template method for per-repository write group cleanup.
476
This is called during abort before the write group is considered to be
477
finished and should cleanup any internal state accrued during the write
478
group. There is no requirement that data handed to the repository be
479
*not* made available - this is not a rollback - but neither should any
480
attempt be made to ensure that data added is fully commited. Abort is
481
invoked when an error has occured so futher disk or network operations
482
may not be possible or may error and if possible should not be
487
def add_inventory(self, revision_id, inv, parents):
488
"""Add the inventory inv to the repository as revision_id.
490
:param parents: The revision ids of the parents that revision_id
491
is known to have and are in the repository already.
493
returns the sha1 of the serialized inventory.
495
_mod_revision.check_not_reserved_id(revision_id)
496
assert inv.revision_id is None or inv.revision_id == revision_id, \
497
"Mismatch between inventory revision" \
498
" id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
499
assert inv.root is not None
500
inv_lines = self._serialise_inventory_to_lines(inv)
501
inv_vf = self.get_inventory_weave()
502
return self._inventory_add_lines(inv_vf, revision_id, parents,
503
inv_lines, check_content=False)
505
def _inventory_add_lines(self, inv_vf, revision_id, parents, lines,
507
"""Store lines in inv_vf and return the sha1 of the inventory."""
509
for parent in parents:
511
final_parents.append(parent)
512
return inv_vf.add_lines(revision_id, final_parents, lines,
513
check_content=check_content)[0]
516
def add_revision(self, revision_id, rev, inv=None, config=None):
517
"""Add rev to the revision store as revision_id.
519
:param revision_id: the revision id to use.
520
:param rev: The revision object.
521
:param inv: The inventory for the revision. if None, it will be looked
522
up in the inventory storer
523
:param config: If None no digital signature will be created.
524
If supplied its signature_needed method will be used
525
to determine if a signature should be made.
527
# TODO: jam 20070210 Shouldn't we check rev.revision_id and
529
_mod_revision.check_not_reserved_id(revision_id)
530
if config is not None and config.signature_needed():
532
inv = self.get_inventory(revision_id)
533
plaintext = Testament(rev, inv).as_short_text()
534
self.store_revision_signature(
535
gpg.GPGStrategy(config), plaintext, revision_id)
536
if not revision_id in self.get_inventory_weave():
538
raise errors.WeaveRevisionNotPresent(revision_id,
539
self.get_inventory_weave())
541
# yes, this is not suitable for adding with ghosts.
542
self.add_inventory(revision_id, inv, rev.parent_ids)
543
self._revision_store.add_revision(rev, self.get_transaction())
545
def _add_revision_text(self, revision_id, text):
546
revision = self._revision_store._serializer.read_revision_from_string(
548
self._revision_store._add_revision(revision, StringIO(text),
549
self.get_transaction())
551
def all_revision_ids(self):
552
"""Returns a list of all the revision ids in the repository.
554
This is deprecated because code should generally work on the graph
555
reachable from a particular revision, and ignore any other revisions
556
that might be present. There is no direct replacement method.
558
if 'evil' in debug.debug_flags:
559
mutter_callsite(2, "all_revision_ids is linear with history.")
560
return self._all_revision_ids()
562
def _all_revision_ids(self):
563
"""Returns a list of all the revision ids in the repository.
565
These are in as much topological order as the underlying store can
568
raise NotImplementedError(self._all_revision_ids)
570
def break_lock(self):
571
"""Break a lock if one is present from another instance.
573
Uses the ui factory to ask for confirmation if the lock may be from
576
self.control_files.break_lock()
579
def _eliminate_revisions_not_present(self, revision_ids):
580
"""Check every revision id in revision_ids to see if we have it.
582
Returns a set of the present revisions.
585
for id in revision_ids:
586
if self.has_revision(id):
591
def create(a_bzrdir):
592
"""Construct the current default format repository in a_bzrdir."""
593
return RepositoryFormat.get_default_format().initialize(a_bzrdir)
595
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
596
"""instantiate a Repository.
598
:param _format: The format of the repository on disk.
599
:param a_bzrdir: The BzrDir of the repository.
601
In the future we will have a single api for all stores for
602
getting file texts, inventories and revisions, then
603
this construct will accept instances of those things.
605
super(Repository, self).__init__()
606
self._format = _format
607
# the following are part of the public API for Repository:
608
self.bzrdir = a_bzrdir
609
self.control_files = control_files
610
self._revision_store = _revision_store
611
# backwards compatibility
612
self.weave_store = text_store
614
self._reconcile_does_inventory_gc = True
615
self._reconcile_fixes_text_parents = False
616
# not right yet - should be more semantically clear ?
618
self.control_store = control_store
619
self.control_weaves = control_store
620
# TODO: make sure to construct the right store classes, etc, depending
621
# on whether escaping is required.
622
self._warn_if_deprecated()
623
self._write_group = None
624
self.base = control_files._transport.base
627
return '%s(%r)' % (self.__class__.__name__,
630
def has_same_location(self, other):
631
"""Returns a boolean indicating if this repository is at the same
632
location as another repository.
634
This might return False even when two repository objects are accessing
635
the same physical repository via different URLs.
637
if self.__class__ is not other.__class__:
639
return (self.control_files._transport.base ==
640
other.control_files._transport.base)
642
def is_in_write_group(self):
643
"""Return True if there is an open write group.
645
:seealso: start_write_group.
647
return self._write_group is not None
650
return self.control_files.is_locked()
652
def is_write_locked(self):
653
"""Return True if this object is write locked."""
654
return self.is_locked() and self.control_files._lock_mode == 'w'
656
def lock_write(self, token=None):
657
"""Lock this repository for writing.
659
This causes caching within the repository obejct to start accumlating
660
data during reads, and allows a 'write_group' to be obtained. Write
661
groups must be used for actual data insertion.
663
:param token: if this is already locked, then lock_write will fail
664
unless the token matches the existing lock.
665
:returns: a token if this instance supports tokens, otherwise None.
666
:raises TokenLockingNotSupported: when a token is given but this
667
instance doesn't support using token locks.
668
:raises MismatchedToken: if the specified token doesn't match the token
669
of the existing lock.
670
:seealso: start_write_group.
672
A token should be passed in if you know that you have locked the object
673
some other way, and need to synchronise this object's state with that
676
XXX: this docstring is duplicated in many places, e.g. lockable_files.py
678
result = self.control_files.lock_write(token=token)
683
self.control_files.lock_read()
686
def get_physical_lock_status(self):
687
return self.control_files.get_physical_lock_status()
689
def leave_lock_in_place(self):
690
"""Tell this repository not to release the physical lock when this
693
If lock_write doesn't return a token, then this method is not supported.
695
self.control_files.leave_in_place()
697
def dont_leave_lock_in_place(self):
698
"""Tell this repository to release the physical lock when this
699
object is unlocked, even if it didn't originally acquire it.
701
If lock_write doesn't return a token, then this method is not supported.
703
self.control_files.dont_leave_in_place()
706
def gather_stats(self, revid=None, committers=None):
707
"""Gather statistics from a revision id.
709
:param revid: The revision id to gather statistics from, if None, then
710
no revision specific statistics are gathered.
711
:param committers: Optional parameter controlling whether to grab
712
a count of committers from the revision specific statistics.
713
:return: A dictionary of statistics. Currently this contains:
714
committers: The number of committers if requested.
715
firstrev: A tuple with timestamp, timezone for the penultimate left
716
most ancestor of revid, if revid is not the NULL_REVISION.
717
latestrev: A tuple with timestamp, timezone for revid, if revid is
718
not the NULL_REVISION.
719
revisions: The total revision count in the repository.
720
size: An estimate disk size of the repository in bytes.
723
if revid and committers:
724
result['committers'] = 0
725
if revid and revid != _mod_revision.NULL_REVISION:
727
all_committers = set()
728
revisions = self.get_ancestry(revid)
729
# pop the leading None
731
first_revision = None
733
# ignore the revisions in the middle - just grab first and last
734
revisions = revisions[0], revisions[-1]
735
for revision in self.get_revisions(revisions):
736
if not first_revision:
737
first_revision = revision
739
all_committers.add(revision.committer)
740
last_revision = revision
742
result['committers'] = len(all_committers)
743
result['firstrev'] = (first_revision.timestamp,
744
first_revision.timezone)
745
result['latestrev'] = (last_revision.timestamp,
746
last_revision.timezone)
748
# now gather global repository information
749
if self.bzrdir.root_transport.listable():
750
c, t = self._revision_store.total_size(self.get_transaction())
751
result['revisions'] = c
755
def get_data_stream(self, revision_ids):
756
raise NotImplementedError(self.get_data_stream)
758
def insert_data_stream(self, stream):
759
for item_key, bytes in stream:
760
if item_key[0] == 'file':
761
(file_id,) = item_key[1:]
762
knit = self.weave_store.get_weave_or_empty(
763
file_id, self.get_transaction())
764
elif item_key == ('inventory',):
765
knit = self.get_inventory_weave()
766
elif item_key == ('revisions',):
767
knit = self._revision_store.get_revision_file(
768
self.get_transaction())
769
elif item_key == ('signatures',):
770
knit = self._revision_store.get_signature_file(
771
self.get_transaction())
773
raise RepositoryDataStreamError(
774
"Unrecognised data stream key '%s'" % (item_key,))
775
decoded_list = bencode.bdecode(bytes)
776
format = decoded_list.pop(0)
779
for version, options, parents, some_bytes in decoded_list:
780
data_list.append((version, options, len(some_bytes), parents))
781
knit_bytes += some_bytes
782
knit.insert_data_stream(
783
(format, data_list, StringIO(knit_bytes).read))
786
def missing_revision_ids(self, other, revision_id=None):
787
"""Return the revision ids that other has that this does not.
789
These are returned in topological order.
791
revision_id: only return revision ids included by revision_id.
793
return InterRepository.get(other, self).missing_revision_ids(revision_id)
797
"""Open the repository rooted at base.
799
For instance, if the repository is at URL/.bzr/repository,
800
Repository.open(URL) -> a Repository instance.
802
control = bzrdir.BzrDir.open(base)
803
return control.open_repository()
805
def copy_content_into(self, destination, revision_id=None):
806
"""Make a complete copy of the content in self into destination.
808
This is a destructive operation! Do not use it on existing
811
return InterRepository.get(self, destination).copy_content(revision_id)
813
def commit_write_group(self):
814
"""Commit the contents accrued within the current write group.
816
:seealso: start_write_group.
818
if self._write_group is not self.get_transaction():
819
# has an unlock or relock occured ?
820
raise errors.BzrError('mismatched lock context %r and '
822
(self.get_transaction(), self._write_group))
823
self._commit_write_group()
824
self._write_group = None
826
def _commit_write_group(self):
827
"""Template method for per-repository write group cleanup.
829
This is called before the write group is considered to be
830
finished and should ensure that all data handed to the repository
831
for writing during the write group is safely committed (to the
832
extent possible considering file system caching etc).
835
def fetch(self, source, revision_id=None, pb=None):
836
"""Fetch the content required to construct revision_id from source.
838
If revision_id is None all content is copied.
840
# fast path same-url fetch operations
841
if self.has_same_location(source):
842
# check that last_revision is in 'from' and then return a
844
if (revision_id is not None and
845
not _mod_revision.is_null(revision_id)):
846
self.get_revision(revision_id)
848
inter = InterRepository.get(source, self)
850
return inter.fetch(revision_id=revision_id, pb=pb)
851
except NotImplementedError:
852
raise errors.IncompatibleRepositories(source, self)
854
def create_bundle(self, target, base, fileobj, format=None):
855
return serializer.write_bundle(self, target, base, fileobj, format)
857
def get_commit_builder(self, branch, parents, config, timestamp=None,
858
timezone=None, committer=None, revprops=None,
860
"""Obtain a CommitBuilder for this repository.
862
:param branch: Branch to commit to.
863
:param parents: Revision ids of the parents of the new revision.
864
:param config: Configuration to use.
865
:param timestamp: Optional timestamp recorded for commit.
866
:param timezone: Optional timezone for timestamp.
867
:param committer: Optional committer to set for commit.
868
:param revprops: Optional dictionary of revision properties.
869
:param revision_id: Optional revision id.
871
result = self._commit_builder_class(self, parents, config,
872
timestamp, timezone, committer, revprops, revision_id)
873
self.start_write_group()
877
if (self.control_files._lock_count == 1 and
878
self.control_files._lock_mode == 'w'):
879
if self._write_group is not None:
880
raise errors.BzrError(
881
'Must end write groups before releasing write locks.')
882
self.control_files.unlock()
885
def clone(self, a_bzrdir, revision_id=None):
886
"""Clone this repository into a_bzrdir using the current format.
888
Currently no check is made that the format of this repository and
889
the bzrdir format are compatible. FIXME RBC 20060201.
891
:return: The newly created destination repository.
893
# TODO: deprecate after 0.16; cloning this with all its settings is
894
# probably not very useful -- mbp 20070423
895
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
896
self.copy_content_into(dest_repo, revision_id)
899
def start_write_group(self):
900
"""Start a write group in the repository.
902
Write groups are used by repositories which do not have a 1:1 mapping
903
between file ids and backend store to manage the insertion of data from
904
both fetch and commit operations.
906
A write lock is required around the start_write_group/commit_write_group
907
for the support of lock-requiring repository formats.
909
One can only insert data into a repository inside a write group.
913
if not self.is_write_locked():
914
raise errors.NotWriteLocked(self)
915
if self._write_group:
916
raise errors.BzrError('already in a write group')
917
self._start_write_group()
918
# so we can detect unlock/relock - the write group is now entered.
919
self._write_group = self.get_transaction()
921
def _start_write_group(self):
922
"""Template method for per-repository write group startup.
924
This is called before the write group is considered to be
929
def sprout(self, to_bzrdir, revision_id=None):
930
"""Create a descendent repository for new development.
932
Unlike clone, this does not copy the settings of the repository.
934
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
935
dest_repo.fetch(self, revision_id=revision_id)
938
def _create_sprouting_repo(self, a_bzrdir, shared):
939
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
940
# use target default format.
941
dest_repo = a_bzrdir.create_repository()
943
# Most control formats need the repository to be specifically
944
# created, but on some old all-in-one formats it's not needed
946
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
947
except errors.UninitializableFormat:
948
dest_repo = a_bzrdir.open_repository()
952
def has_revision(self, revision_id):
953
"""True if this repository has a copy of the revision."""
954
if 'evil' in debug.debug_flags:
955
mutter_callsite(3, "has_revision is a LBYL symptom.")
956
return self._revision_store.has_revision_id(revision_id,
957
self.get_transaction())
960
def get_revision(self, revision_id):
961
"""Return the Revision object for a named revision."""
962
return self.get_revisions([revision_id])[0]
965
def get_revision_reconcile(self, revision_id):
966
"""'reconcile' helper routine that allows access to a revision always.
968
This variant of get_revision does not cross check the weave graph
969
against the revision one as get_revision does: but it should only
970
be used by reconcile, or reconcile-alike commands that are correcting
971
or testing the revision graph.
973
return self._get_revisions([revision_id])[0]
976
def get_revisions(self, revision_ids):
977
"""Get many revisions at once."""
978
return self._get_revisions(revision_ids)
981
def _get_revisions(self, revision_ids):
982
"""Core work logic to get many revisions without sanity checks."""
983
for rev_id in revision_ids:
984
if not rev_id or not isinstance(rev_id, basestring):
985
raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
986
revs = self._revision_store.get_revisions(revision_ids,
987
self.get_transaction())
989
assert not isinstance(rev.revision_id, unicode)
990
for parent_id in rev.parent_ids:
991
assert not isinstance(parent_id, unicode)
995
def get_revision_xml(self, revision_id):
996
# TODO: jam 20070210 This shouldn't be necessary since get_revision
997
# would have already do it.
998
# TODO: jam 20070210 Just use _serializer.write_revision_to_string()
999
rev = self.get_revision(revision_id)
1000
rev_tmp = StringIO()
1001
# the current serializer..
1002
self._revision_store._serializer.write_revision(rev, rev_tmp)
1004
return rev_tmp.getvalue()
1007
def get_deltas_for_revisions(self, revisions):
1008
"""Produce a generator of revision deltas.
1010
Note that the input is a sequence of REVISIONS, not revision_ids.
1011
Trees will be held in memory until the generator exits.
1012
Each delta is relative to the revision's lefthand predecessor.
1014
required_trees = set()
1015
for revision in revisions:
1016
required_trees.add(revision.revision_id)
1017
required_trees.update(revision.parent_ids[:1])
1018
trees = dict((t.get_revision_id(), t) for
1019
t in self.revision_trees(required_trees))
1020
for revision in revisions:
1021
if not revision.parent_ids:
1022
old_tree = self.revision_tree(None)
1024
old_tree = trees[revision.parent_ids[0]]
1025
yield trees[revision.revision_id].changes_from(old_tree)
1028
def get_revision_delta(self, revision_id):
1029
"""Return the delta for one revision.
1031
The delta is relative to the left-hand predecessor of the
1034
r = self.get_revision(revision_id)
1035
return list(self.get_deltas_for_revisions([r]))[0]
1038
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1039
signature = gpg_strategy.sign(plaintext)
1040
self._revision_store.add_revision_signature_text(revision_id,
1042
self.get_transaction())
1044
def fileids_altered_by_revision_ids(self, revision_ids):
1045
"""Find the file ids and versions affected by revisions.
1047
:param revisions: an iterable containing revision ids.
1048
:return: a dictionary mapping altered file-ids to an iterable of
1049
revision_ids. Each altered file-ids has the exact revision_ids that
1050
altered it listed explicitly.
1052
assert self._serializer.support_altered_by_hack, \
1053
("fileids_altered_by_revision_ids only supported for branches "
1054
"which store inventory as unnested xml, not on %r" % self)
1055
selected_revision_ids = set(revision_ids)
1056
w = self.get_inventory_weave()
1059
# this code needs to read every new line in every inventory for the
1060
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
1061
# not present in one of those inventories is unnecessary but not
1062
# harmful because we are filtering by the revision id marker in the
1063
# inventory lines : we only select file ids altered in one of those
1064
# revisions. We don't need to see all lines in the inventory because
1065
# only those added in an inventory in rev X can contain a revision=X
1067
unescape_revid_cache = {}
1068
unescape_fileid_cache = {}
1070
# jam 20061218 In a big fetch, this handles hundreds of thousands
1071
# of lines, so it has had a lot of inlining and optimizing done.
1072
# Sorry that it is a little bit messy.
1073
# Move several functions to be local variables, since this is a long
1075
search = self._file_ids_altered_regex.search
1076
unescape = _unescape_xml
1077
setdefault = result.setdefault
1078
pb = ui.ui_factory.nested_progress_bar()
1080
for line in w.iter_lines_added_or_present_in_versions(
1081
selected_revision_ids, pb=pb):
1082
match = search(line)
1085
# One call to match.group() returning multiple items is quite a
1086
# bit faster than 2 calls to match.group() each returning 1
1087
file_id, revision_id = match.group('file_id', 'revision_id')
1089
# Inlining the cache lookups helps a lot when you make 170,000
1090
# lines and 350k ids, versus 8.4 unique ids.
1091
# Using a cache helps in 2 ways:
1092
# 1) Avoids unnecessary decoding calls
1093
# 2) Re-uses cached strings, which helps in future set and
1095
# (2) is enough that removing encoding entirely along with
1096
# the cache (so we are using plain strings) results in no
1097
# performance improvement.
1099
revision_id = unescape_revid_cache[revision_id]
1101
unescaped = unescape(revision_id)
1102
unescape_revid_cache[revision_id] = unescaped
1103
revision_id = unescaped
1105
if revision_id in selected_revision_ids:
1107
file_id = unescape_fileid_cache[file_id]
1109
unescaped = unescape(file_id)
1110
unescape_fileid_cache[file_id] = unescaped
1112
setdefault(file_id, set()).add(revision_id)
1117
def iter_files_bytes(self, desired_files):
1118
"""Iterate through file versions.
1120
Files will not necessarily be returned in the order they occur in
1121
desired_files. No specific order is guaranteed.
1123
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1124
value supplied by the caller as part of desired_files. It should
1125
uniquely identify the file version in the caller's context. (Examples:
1126
an index number or a TreeTransform trans_id.)
1128
bytes_iterator is an iterable of bytestrings for the file. The
1129
kind of iterable and length of the bytestrings are unspecified, but for
1130
this implementation, it is a list of lines produced by
1131
VersionedFile.get_lines().
1133
:param desired_files: a list of (file_id, revision_id, identifier)
1136
transaction = self.get_transaction()
1137
for file_id, revision_id, callable_data in desired_files:
1139
weave = self.weave_store.get_weave(file_id, transaction)
1140
except errors.NoSuchFile:
1141
raise errors.NoSuchIdInRepository(self, file_id)
1142
yield callable_data, weave.get_lines(revision_id)
1144
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1145
"""Get an iterable listing the keys of all the data introduced by a set
1148
The keys will be ordered so that the corresponding items can be safely
1149
fetched and inserted in that order.
1151
:returns: An iterable producing tuples of (knit-kind, file-id,
1152
versions). knit-kind is one of 'file', 'inventory', 'signatures',
1153
'revisions'. file-id is None unless knit-kind is 'file'.
1155
# XXX: it's a bit weird to control the inventory weave caching in this
1156
# generator. Ideally the caching would be done in fetch.py I think. Or
1157
# maybe this generator should explicitly have the contract that it
1158
# should not be iterated until the previously yielded item has been
1161
inv_w = self.get_inventory_weave()
1162
inv_w.enable_cache()
1164
# file ids that changed
1165
file_ids = self.fileids_altered_by_revision_ids(revision_ids)
1167
num_file_ids = len(file_ids)
1168
for file_id, altered_versions in file_ids.iteritems():
1169
if _files_pb is not None:
1170
_files_pb.update("fetch texts", count, num_file_ids)
1172
yield ("file", file_id, altered_versions)
1173
# We're done with the files_pb. Note that it finished by the caller,
1174
# just as it was created by the caller.
1178
yield ("inventory", None, revision_ids)
1182
revisions_with_signatures = set()
1183
for rev_id in revision_ids:
1185
self.get_signature_text(rev_id)
1186
except errors.NoSuchRevision:
1190
revisions_with_signatures.add(rev_id)
1192
yield ("signatures", None, revisions_with_signatures)
1195
yield ("revisions", None, revision_ids)
1198
def get_inventory_weave(self):
1199
return self.control_weaves.get_weave('inventory',
1200
self.get_transaction())
1203
def get_inventory(self, revision_id):
1204
"""Get Inventory object by hash."""
1205
return self.deserialise_inventory(
1206
revision_id, self.get_inventory_xml(revision_id))
1208
def deserialise_inventory(self, revision_id, xml):
1209
"""Transform the xml into an inventory object.
1211
:param revision_id: The expected revision id of the inventory.
1212
:param xml: A serialised inventory.
1214
return self._serializer.read_inventory_from_string(xml, revision_id)
1216
def serialise_inventory(self, inv):
1217
return self._serializer.write_inventory_to_string(inv)
1219
def _serialise_inventory_to_lines(self, inv):
1220
return self._serializer.write_inventory_to_lines(inv)
1222
def get_serializer_format(self):
1223
return self._serializer.format_num
1226
def get_inventory_xml(self, revision_id):
1227
"""Get inventory XML as a file object."""
1229
assert isinstance(revision_id, str), type(revision_id)
1230
iw = self.get_inventory_weave()
1231
return iw.get_text(revision_id)
1233
raise errors.HistoryMissing(self, 'inventory', revision_id)
1236
def get_inventory_sha1(self, revision_id):
1237
"""Return the sha1 hash of the inventory entry
1239
return self.get_revision(revision_id).inventory_sha1
1242
def get_revision_graph(self, revision_id=None):
1243
"""Return a dictionary containing the revision graph.
1245
NB: This method should not be used as it accesses the entire graph all
1246
at once, which is much more data than most operations should require.
1248
:param revision_id: The revision_id to get a graph from. If None, then
1249
the entire revision graph is returned. This is a deprecated mode of
1250
operation and will be removed in the future.
1251
:return: a dictionary of revision_id->revision_parents_list.
1253
raise NotImplementedError(self.get_revision_graph)
1256
def get_revision_graph_with_ghosts(self, revision_ids=None):
1257
"""Return a graph of the revisions with ghosts marked as applicable.
1259
:param revision_ids: an iterable of revisions to graph or None for all.
1260
:return: a Graph object with the graph reachable from revision_ids.
1262
if 'evil' in debug.debug_flags:
1264
"get_revision_graph_with_ghosts scales with size of history.")
1265
result = deprecated_graph.Graph()
1266
if not revision_ids:
1267
pending = set(self.all_revision_ids())
1270
pending = set(revision_ids)
1271
# special case NULL_REVISION
1272
if _mod_revision.NULL_REVISION in pending:
1273
pending.remove(_mod_revision.NULL_REVISION)
1274
required = set(pending)
1277
revision_id = pending.pop()
1279
rev = self.get_revision(revision_id)
1280
except errors.NoSuchRevision:
1281
if revision_id in required:
1284
result.add_ghost(revision_id)
1286
for parent_id in rev.parent_ids:
1287
# is this queued or done ?
1288
if (parent_id not in pending and
1289
parent_id not in done):
1291
pending.add(parent_id)
1292
result.add_node(revision_id, rev.parent_ids)
1293
done.add(revision_id)
1296
def _get_history_vf(self):
1297
"""Get a versionedfile whose history graph reflects all revisions.
1299
For weave repositories, this is the inventory weave.
1301
return self.get_inventory_weave()
1303
def iter_reverse_revision_history(self, revision_id):
1304
"""Iterate backwards through revision ids in the lefthand history
1306
:param revision_id: The revision id to start with. All its lefthand
1307
ancestors will be traversed.
1309
if revision_id in (None, _mod_revision.NULL_REVISION):
1311
next_id = revision_id
1312
versionedfile = self._get_history_vf()
1315
parents = versionedfile.get_parents(next_id)
1316
if len(parents) == 0:
1319
next_id = parents[0]
1322
def get_revision_inventory(self, revision_id):
1323
"""Return inventory of a past revision."""
1324
# TODO: Unify this with get_inventory()
1325
# bzr 0.0.6 and later imposes the constraint that the inventory_id
1326
# must be the same as its revision, so this is trivial.
1327
if revision_id is None:
1328
# This does not make sense: if there is no revision,
1329
# then it is the current tree inventory surely ?!
1330
# and thus get_root_id() is something that looks at the last
1331
# commit on the branch, and the get_root_id is an inventory check.
1332
raise NotImplementedError
1333
# return Inventory(self.get_root_id())
1335
return self.get_inventory(revision_id)
1338
def is_shared(self):
1339
"""Return True if this repository is flagged as a shared repository."""
1340
raise NotImplementedError(self.is_shared)
1343
def reconcile(self, other=None, thorough=False):
1344
"""Reconcile this repository."""
1345
from bzrlib.reconcile import RepoReconciler
1346
reconciler = RepoReconciler(self, thorough=thorough)
1347
reconciler.reconcile()
1350
def _refresh_data(self):
1351
"""Helper called from lock_* to ensure coherency with disk.
1353
The default implementation does nothing; it is however possible
1354
for repositories to maintain loaded indices across multiple locks
1355
by checking inside their implementation of this method to see
1356
whether their indices are still valid. This depends of course on
1357
the disk format being validatable in this manner.
1361
def revision_tree(self, revision_id):
1362
"""Return Tree for a revision on this branch.
1364
`revision_id` may be None for the empty tree revision.
1366
# TODO: refactor this to use an existing revision object
1367
# so we don't need to read it in twice.
1368
if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
1369
return RevisionTree(self, Inventory(root_id=None),
1370
_mod_revision.NULL_REVISION)
1372
inv = self.get_revision_inventory(revision_id)
1373
return RevisionTree(self, inv, revision_id)
1376
def revision_trees(self, revision_ids):
1377
"""Return Tree for a revision on this branch.
1379
`revision_id` may not be None or 'null:'"""
1380
assert None not in revision_ids
1381
assert _mod_revision.NULL_REVISION not in revision_ids
1382
texts = self.get_inventory_weave().get_texts(revision_ids)
1383
for text, revision_id in zip(texts, revision_ids):
1384
inv = self.deserialise_inventory(revision_id, text)
1385
yield RevisionTree(self, inv, revision_id)
1388
def get_ancestry(self, revision_id, topo_sorted=True):
1389
"""Return a list of revision-ids integrated by a revision.
1391
The first element of the list is always None, indicating the origin
1392
revision. This might change when we have history horizons, or
1393
perhaps we should have a new API.
1395
This is topologically sorted.
1397
if _mod_revision.is_null(revision_id):
1399
if not self.has_revision(revision_id):
1400
raise errors.NoSuchRevision(self, revision_id)
1401
w = self.get_inventory_weave()
1402
candidates = w.get_ancestry(revision_id, topo_sorted)
1403
return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1406
"""Compress the data within the repository.
1408
This operation only makes sense for some repository types. For other
1409
types it should be a no-op that just returns.
1411
This stub method does not require a lock, but subclasses should use
1412
@needs_write_lock as this is a long running call its reasonable to
1413
implicitly lock for the user.
1417
def print_file(self, file, revision_id):
1418
"""Print `file` to stdout.
1420
FIXME RBC 20060125 as John Meinel points out this is a bad api
1421
- it writes to stdout, it assumes that that is valid etc. Fix
1422
by creating a new more flexible convenience function.
1424
tree = self.revision_tree(revision_id)
1425
# use inventory as it was in that revision
1426
file_id = tree.inventory.path2id(file)
1428
# TODO: jam 20060427 Write a test for this code path
1429
# it had a bug in it, and was raising the wrong
1431
raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
1432
tree.print_file(file_id)
1434
def get_transaction(self):
1435
return self.control_files.get_transaction()
1437
def revision_parents(self, revision_id):
1438
return self.get_inventory_weave().parent_names(revision_id)
1440
def get_parents(self, revision_ids):
1441
"""See StackedParentsProvider.get_parents"""
1443
for revision_id in revision_ids:
1444
if revision_id == _mod_revision.NULL_REVISION:
1448
parents = self.get_revision(revision_id).parent_ids
1449
except errors.NoSuchRevision:
1452
if len(parents) == 0:
1453
parents = [_mod_revision.NULL_REVISION]
1454
parents_list.append(parents)
1457
def _make_parents_provider(self):
1460
def get_graph(self, other_repository=None):
1461
"""Return the graph walker for this repository format"""
1462
parents_provider = self._make_parents_provider()
1463
if (other_repository is not None and
1464
other_repository.bzrdir.transport.base !=
1465
self.bzrdir.transport.base):
1466
parents_provider = graph._StackedParentsProvider(
1467
[parents_provider, other_repository._make_parents_provider()])
1468
return graph.Graph(parents_provider)
1470
def get_versioned_file_checker(self, revisions, revision_versions_cache):
1471
return VersionedFileChecker(revisions, revision_versions_cache, self)
1474
def set_make_working_trees(self, new_value):
1475
"""Set the policy flag for making working trees when creating branches.
1477
This only applies to branches that use this repository.
1479
The default is 'True'.
1480
:param new_value: True to restore the default, False to disable making
1483
raise NotImplementedError(self.set_make_working_trees)
1485
def make_working_trees(self):
1486
"""Returns the policy for making working trees on new branches."""
1487
raise NotImplementedError(self.make_working_trees)
1490
def sign_revision(self, revision_id, gpg_strategy):
1491
plaintext = Testament.from_revision(self, revision_id).as_short_text()
1492
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1495
def has_signature_for_revision_id(self, revision_id):
1496
"""Query for a revision signature for revision_id in the repository."""
1497
return self._revision_store.has_signature(revision_id,
1498
self.get_transaction())
1501
def get_signature_text(self, revision_id):
1502
"""Return the text for a signature."""
1503
return self._revision_store.get_signature_text(revision_id,
1504
self.get_transaction())
1507
def check(self, revision_ids=None):
1508
"""Check consistency of all history of given revision_ids.
1510
Different repository implementations should override _check().
1512
:param revision_ids: A non-empty list of revision_ids whose ancestry
1513
will be checked. Typically the last revision_id of a branch.
1515
return self._check(revision_ids)
1517
def _check(self, revision_ids):
1518
result = check.Check(self)
1522
def _warn_if_deprecated(self):
1523
global _deprecation_warning_done
1524
if _deprecation_warning_done:
1526
_deprecation_warning_done = True
1527
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1528
% (self._format, self.bzrdir.transport.base))
1530
def supports_rich_root(self):
1531
return self._format.rich_root_data
1533
def _check_ascii_revisionid(self, revision_id, method):
1534
"""Private helper for ascii-only repositories."""
1535
# weave repositories refuse to store revisionids that are non-ascii.
1536
if revision_id is not None:
1537
# weaves require ascii revision ids.
1538
if isinstance(revision_id, unicode):
1540
revision_id.encode('ascii')
1541
except UnicodeEncodeError:
1542
raise errors.NonAsciiRevisionId(method, self)
1545
revision_id.decode('ascii')
1546
except UnicodeDecodeError:
1547
raise errors.NonAsciiRevisionId(method, self)
1549
def revision_graph_can_have_wrong_parents(self):
1550
"""Is it possible for this repository to have a revision graph with
1553
If True, then this repository must also implement
1554
_find_inconsistent_revision_parents so that check and reconcile can
1555
check for inconsistencies before proceeding with other checks that may
1556
depend on the revision index being consistent.
1558
raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1560
# remove these delegates a while after bzr 0.15
1561
def __make_delegated(name, from_module):
1562
def _deprecated_repository_forwarder():
1563
symbol_versioning.warn('%s moved to %s in bzr 0.15'
1564
% (name, from_module),
1567
m = __import__(from_module, globals(), locals(), [name])
1569
return getattr(m, name)
1570
except AttributeError:
1571
raise AttributeError('module %s has no name %s'
1573
globals()[name] = _deprecated_repository_forwarder
1576
'AllInOneRepository',
1577
'WeaveMetaDirRepository',
1578
'PreSplitOutRepositoryFormat',
1579
'RepositoryFormat4',
1580
'RepositoryFormat5',
1581
'RepositoryFormat6',
1582
'RepositoryFormat7',
1584
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1588
'RepositoryFormatKnit',
1589
'RepositoryFormatKnit1',
1591
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
1594
def install_revision(repository, rev, revision_tree):
1595
"""Install all revision data into a repository."""
1596
present_parents = []
1598
for p_id in rev.parent_ids:
1599
if repository.has_revision(p_id):
1600
present_parents.append(p_id)
1601
parent_trees[p_id] = repository.revision_tree(p_id)
1603
parent_trees[p_id] = repository.revision_tree(None)
1605
inv = revision_tree.inventory
1606
entries = inv.iter_entries()
1607
# backwards compatibility hack: skip the root id.
1608
if not repository.supports_rich_root():
1609
path, root = entries.next()
1610
if root.revision != rev.revision_id:
1611
raise errors.IncompatibleRevision(repr(repository))
1612
# Add the texts that are not already present
1613
for path, ie in entries:
1614
w = repository.weave_store.get_weave_or_empty(ie.file_id,
1615
repository.get_transaction())
1616
if ie.revision not in w:
1618
# FIXME: TODO: The following loop *may* be overlapping/duplicate
1619
# with InventoryEntry.find_previous_heads(). if it is, then there
1620
# is a latent bug here where the parents may have ancestors of each
1622
for revision, tree in parent_trees.iteritems():
1623
if ie.file_id not in tree:
1625
parent_id = tree.inventory[ie.file_id].revision
1626
if parent_id in text_parents:
1628
text_parents.append(parent_id)
1630
vfile = repository.weave_store.get_weave_or_empty(ie.file_id,
1631
repository.get_transaction())
1632
lines = revision_tree.get_file(ie.file_id).readlines()
1633
vfile.add_lines(rev.revision_id, text_parents, lines)
1635
# install the inventory
1636
repository.add_inventory(rev.revision_id, inv, present_parents)
1637
except errors.RevisionAlreadyPresent:
1639
repository.add_revision(rev.revision_id, rev, inv)
1642
class MetaDirRepository(Repository):
1643
"""Repositories in the new meta-dir layout."""
1645
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1646
super(MetaDirRepository, self).__init__(_format,
1652
dir_mode = self.control_files._dir_mode
1653
file_mode = self.control_files._file_mode
1656
def is_shared(self):
1657
"""Return True if this repository is flagged as a shared repository."""
1658
return self.control_files._transport.has('shared-storage')
1661
def set_make_working_trees(self, new_value):
1662
"""Set the policy flag for making working trees when creating branches.
1664
This only applies to branches that use this repository.
1666
The default is 'True'.
1667
:param new_value: True to restore the default, False to disable making
1672
self.control_files._transport.delete('no-working-trees')
1673
except errors.NoSuchFile:
1676
self.control_files.put_utf8('no-working-trees', '')
1678
def make_working_trees(self):
1679
"""Returns the policy for making working trees on new branches."""
1680
return not self.control_files._transport.has('no-working-trees')
1683
class RepositoryFormatRegistry(registry.Registry):
1684
"""Registry of RepositoryFormats."""
1686
def get(self, format_string):
1687
r = registry.Registry.get(self, format_string)
1693
format_registry = RepositoryFormatRegistry()
1694
"""Registry of formats, indexed by their identifying format string.
1696
This can contain either format instances themselves, or classes/factories that
1697
can be called to obtain one.
1701
#####################################################################
1702
# Repository Formats
1704
class RepositoryFormat(object):
1705
"""A repository format.
1707
Formats provide three things:
1708
* An initialization routine to construct repository data on disk.
1709
* a format string which is used when the BzrDir supports versioned
1711
* an open routine which returns a Repository instance.
1713
There is one and only one Format subclass for each on-disk format. But
1714
there can be one Repository subclass that is used for several different
1715
formats. The _format attribute on a Repository instance can be used to
1716
determine the disk format.
1718
Formats are placed in an dict by their format string for reference
1719
during opening. These should be subclasses of RepositoryFormat
1722
Once a format is deprecated, just deprecate the initialize and open
1723
methods on the format class. Do not deprecate the object, as the
1724
object will be created every system load.
1726
Common instance attributes:
1727
_matchingbzrdir - the bzrdir format that the repository format was
1728
originally written to work with. This can be used if manually
1729
constructing a bzrdir and repository, or more commonly for test suite
1734
return "<%s>" % self.__class__.__name__
1736
def __eq__(self, other):
1737
# format objects are generally stateless
1738
return isinstance(other, self.__class__)
1740
def __ne__(self, other):
1741
return not self == other
1744
def find_format(klass, a_bzrdir):
1745
"""Return the format for the repository object in a_bzrdir.
1747
This is used by bzr native formats that have a "format" file in
1748
the repository. Other methods may be used by different types of
1752
transport = a_bzrdir.get_repository_transport(None)
1753
format_string = transport.get("format").read()
1754
return format_registry.get(format_string)
1755
except errors.NoSuchFile:
1756
raise errors.NoRepositoryPresent(a_bzrdir)
1758
raise errors.UnknownFormatError(format=format_string)
1761
def register_format(klass, format):
1762
format_registry.register(format.get_format_string(), format)
1765
def unregister_format(klass, format):
1766
format_registry.remove(format.get_format_string())
1769
def get_default_format(klass):
1770
"""Return the current default format."""
1771
from bzrlib import bzrdir
1772
return bzrdir.format_registry.make_bzrdir('default').repository_format
1774
def _get_control_store(self, repo_transport, control_files):
1775
"""Return the control store for this repository."""
1776
raise NotImplementedError(self._get_control_store)
1778
def get_format_string(self):
1779
"""Return the ASCII format string that identifies this format.
1781
Note that in pre format ?? repositories the format string is
1782
not permitted nor written to disk.
1784
raise NotImplementedError(self.get_format_string)
1786
def get_format_description(self):
1787
"""Return the short description for this format."""
1788
raise NotImplementedError(self.get_format_description)
1790
def _get_revision_store(self, repo_transport, control_files):
1791
"""Return the revision store object for this a_bzrdir."""
1792
raise NotImplementedError(self._get_revision_store)
1794
def _get_text_rev_store(self,
1801
"""Common logic for getting a revision store for a repository.
1803
see self._get_revision_store for the subclass-overridable method to
1804
get the store for a repository.
1806
from bzrlib.store.revision.text import TextRevisionStore
1807
dir_mode = control_files._dir_mode
1808
file_mode = control_files._file_mode
1809
text_store = TextStore(transport.clone(name),
1811
compressed=compressed,
1813
file_mode=file_mode)
1814
_revision_store = TextRevisionStore(text_store, serializer)
1815
return _revision_store
1817
# TODO: this shouldn't be in the base class, it's specific to things that
1818
# use weaves or knits -- mbp 20070207
1819
def _get_versioned_file_store(self,
1824
versionedfile_class=None,
1825
versionedfile_kwargs={},
1827
if versionedfile_class is None:
1828
versionedfile_class = self._versionedfile_class
1829
weave_transport = control_files._transport.clone(name)
1830
dir_mode = control_files._dir_mode
1831
file_mode = control_files._file_mode
1832
return VersionedFileStore(weave_transport, prefixed=prefixed,
1834
file_mode=file_mode,
1835
versionedfile_class=versionedfile_class,
1836
versionedfile_kwargs=versionedfile_kwargs,
1839
def initialize(self, a_bzrdir, shared=False):
1840
"""Initialize a repository of this format in a_bzrdir.
1842
:param a_bzrdir: The bzrdir to put the new repository in it.
1843
:param shared: The repository should be initialized as a sharable one.
1844
:returns: The new repository object.
1846
This may raise UninitializableFormat if shared repository are not
1847
compatible the a_bzrdir.
1849
raise NotImplementedError(self.initialize)
1851
def is_supported(self):
1852
"""Is this format supported?
1854
Supported formats must be initializable and openable.
1855
Unsupported formats may not support initialization or committing or
1856
some other features depending on the reason for not being supported.
1860
def check_conversion_target(self, target_format):
1861
raise NotImplementedError(self.check_conversion_target)
1863
def open(self, a_bzrdir, _found=False):
1864
"""Return an instance of this format for the bzrdir a_bzrdir.
1866
_found is a private parameter, do not use it.
1868
raise NotImplementedError(self.open)
1871
class MetaDirRepositoryFormat(RepositoryFormat):
1872
"""Common base class for the new repositories using the metadir layout."""
1874
rich_root_data = False
1875
supports_tree_reference = False
1876
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1879
super(MetaDirRepositoryFormat, self).__init__()
1881
def _create_control_files(self, a_bzrdir):
1882
"""Create the required files and the initial control_files object."""
1883
# FIXME: RBC 20060125 don't peek under the covers
1884
# NB: no need to escape relative paths that are url safe.
1885
repository_transport = a_bzrdir.get_repository_transport(self)
1886
control_files = lockable_files.LockableFiles(repository_transport,
1887
'lock', lockdir.LockDir)
1888
control_files.create_lock()
1889
return control_files
1891
def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1892
"""Upload the initial blank content."""
1893
control_files = self._create_control_files(a_bzrdir)
1894
control_files.lock_write()
1896
control_files._transport.mkdir_multi(dirs,
1897
mode=control_files._dir_mode)
1898
for file, content in files:
1899
control_files.put(file, content)
1900
for file, content in utf8_files:
1901
control_files.put_utf8(file, content)
1903
control_files.put_utf8('shared-storage', '')
1905
control_files.unlock()
1908
# formats which have no format string are not discoverable
1909
# and not independently creatable, so are not registered. They're
1910
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
1911
# needed, it's constructed directly by the BzrDir. Non-native formats where
1912
# the repository is not separately opened are similar.
1914
format_registry.register_lazy(
1915
'Bazaar-NG Repository format 7',
1916
'bzrlib.repofmt.weaverepo',
1920
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1921
# default control directory format
1922
format_registry.register_lazy(
1923
'Bazaar-NG Knit Repository Format 1',
1924
'bzrlib.repofmt.knitrepo',
1925
'RepositoryFormatKnit1',
1927
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
1929
format_registry.register_lazy(
1930
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
1931
'bzrlib.repofmt.knitrepo',
1932
'RepositoryFormatKnit3',
1936
class InterRepository(InterObject):
1937
"""This class represents operations taking place between two repositories.
1939
Its instances have methods like copy_content and fetch, and contain
1940
references to the source and target repositories these operations can be
1943
Often we will provide convenience methods on 'repository' which carry out
1944
operations with another repository - they will always forward to
1945
InterRepository.get(other).method_name(parameters).
1949
"""The available optimised InterRepository types."""
1951
def copy_content(self, revision_id=None):
1952
raise NotImplementedError(self.copy_content)
1954
def fetch(self, revision_id=None, pb=None):
1955
"""Fetch the content required to construct revision_id.
1957
The content is copied from self.source to self.target.
1959
:param revision_id: if None all content is copied, if NULL_REVISION no
1961
:param pb: optional progress bar to use for progress reports. If not
1962
provided a default one will be created.
1964
Returns the copied revision count and the failed revisions in a tuple:
1967
raise NotImplementedError(self.fetch)
1970
def missing_revision_ids(self, revision_id=None):
1971
"""Return the revision ids that source has that target does not.
1973
These are returned in topological order.
1975
:param revision_id: only return revision ids included by this
1978
# generic, possibly worst case, slow code path.
1979
target_ids = set(self.target.all_revision_ids())
1980
if revision_id is not None:
1981
source_ids = self.source.get_ancestry(revision_id)
1982
assert source_ids[0] is None
1985
source_ids = self.source.all_revision_ids()
1986
result_set = set(source_ids).difference(target_ids)
1987
# this may look like a no-op: its not. It preserves the ordering
1988
# other_ids had while only returning the members from other_ids
1989
# that we've decided we need.
1990
return [rev_id for rev_id in source_ids if rev_id in result_set]
1993
def _same_model(source, target):
1994
"""True if source and target have the same data representation."""
1995
if source.supports_rich_root() != target.supports_rich_root():
1997
if source._serializer != target._serializer:
2002
class InterSameDataRepository(InterRepository):
2003
"""Code for converting between repositories that represent the same data.
2005
Data format and model must match for this to work.
2009
def _get_repo_format_to_test(self):
2010
"""Repository format for testing with.
2012
InterSameData can pull from subtree to subtree and from non-subtree to
2013
non-subtree, so we test this with the richest repository format.
2015
from bzrlib.repofmt import knitrepo
2016
return knitrepo.RepositoryFormatKnit3()
2019
def is_compatible(source, target):
2020
return InterRepository._same_model(source, target)
2023
def copy_content(self, revision_id=None):
2024
"""Make a complete copy of the content in self into destination.
2026
This copies both the repository's revision data, and configuration information
2027
such as the make_working_trees setting.
2029
This is a destructive operation! Do not use it on existing
2032
:param revision_id: Only copy the content needed to construct
2033
revision_id and its parents.
2036
self.target.set_make_working_trees(self.source.make_working_trees())
2037
except NotImplementedError:
2039
# but don't bother fetching if we have the needed data now.
2040
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2041
self.target.has_revision(revision_id)):
2043
self.target.fetch(self.source, revision_id=revision_id)
2046
def fetch(self, revision_id=None, pb=None):
2047
"""See InterRepository.fetch()."""
2048
from bzrlib.fetch import GenericRepoFetcher
2049
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2050
self.source, self.source._format, self.target,
2051
self.target._format)
2052
f = GenericRepoFetcher(to_repository=self.target,
2053
from_repository=self.source,
2054
last_revision=revision_id,
2056
return f.count_copied, f.failed_revisions
2059
class InterWeaveRepo(InterSameDataRepository):
2060
"""Optimised code paths between Weave based repositories.
2062
This should be in bzrlib/repofmt/weaverepo.py but we have not yet
2063
implemented lazy inter-object optimisation.
2067
def _get_repo_format_to_test(self):
2068
from bzrlib.repofmt import weaverepo
2069
return weaverepo.RepositoryFormat7()
2072
def is_compatible(source, target):
2073
"""Be compatible with known Weave formats.
2075
We don't test for the stores being of specific types because that
2076
could lead to confusing results, and there is no need to be
2079
from bzrlib.repofmt.weaverepo import (
2085
return (isinstance(source._format, (RepositoryFormat5,
2087
RepositoryFormat7)) and
2088
isinstance(target._format, (RepositoryFormat5,
2090
RepositoryFormat7)))
2091
except AttributeError:
2095
def copy_content(self, revision_id=None):
2096
"""See InterRepository.copy_content()."""
2097
# weave specific optimised path:
2099
self.target.set_make_working_trees(self.source.make_working_trees())
2100
except NotImplementedError:
2102
# FIXME do not peek!
2103
if self.source.control_files._transport.listable():
2104
pb = ui.ui_factory.nested_progress_bar()
2106
self.target.weave_store.copy_all_ids(
2107
self.source.weave_store,
2109
from_transaction=self.source.get_transaction(),
2110
to_transaction=self.target.get_transaction())
2111
pb.update('copying inventory', 0, 1)
2112
self.target.control_weaves.copy_multi(
2113
self.source.control_weaves, ['inventory'],
2114
from_transaction=self.source.get_transaction(),
2115
to_transaction=self.target.get_transaction())
2116
self.target._revision_store.text_store.copy_all_ids(
2117
self.source._revision_store.text_store,
2122
self.target.fetch(self.source, revision_id=revision_id)
2125
def fetch(self, revision_id=None, pb=None):
2126
"""See InterRepository.fetch()."""
2127
from bzrlib.fetch import GenericRepoFetcher
2128
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2129
self.source, self.source._format, self.target, self.target._format)
2130
f = GenericRepoFetcher(to_repository=self.target,
2131
from_repository=self.source,
2132
last_revision=revision_id,
2134
return f.count_copied, f.failed_revisions
2137
def missing_revision_ids(self, revision_id=None):
2138
"""See InterRepository.missing_revision_ids()."""
2139
# we want all revisions to satisfy revision_id in source.
2140
# but we don't want to stat every file here and there.
2141
# we want then, all revisions other needs to satisfy revision_id
2142
# checked, but not those that we have locally.
2143
# so the first thing is to get a subset of the revisions to
2144
# satisfy revision_id in source, and then eliminate those that
2145
# we do already have.
2146
# this is slow on high latency connection to self, but as as this
2147
# disk format scales terribly for push anyway due to rewriting
2148
# inventory.weave, this is considered acceptable.
2150
if revision_id is not None:
2151
source_ids = self.source.get_ancestry(revision_id)
2152
assert source_ids[0] is None
2155
source_ids = self.source._all_possible_ids()
2156
source_ids_set = set(source_ids)
2157
# source_ids is the worst possible case we may need to pull.
2158
# now we want to filter source_ids against what we actually
2159
# have in target, but don't try to check for existence where we know
2160
# we do not have a revision as that would be pointless.
2161
target_ids = set(self.target._all_possible_ids())
2162
possibly_present_revisions = target_ids.intersection(source_ids_set)
2163
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2164
required_revisions = source_ids_set.difference(actually_present_revisions)
2165
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2166
if revision_id is not None:
2167
# we used get_ancestry to determine source_ids then we are assured all
2168
# revisions referenced are present as they are installed in topological order.
2169
# and the tip revision was validated by get_ancestry.
2170
return required_topo_revisions
2172
# if we just grabbed the possibly available ids, then
2173
# we only have an estimate of whats available and need to validate
2174
# that against the revision records.
2175
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2178
class InterKnitRepo(InterSameDataRepository):
2179
"""Optimised code paths between Knit based repositories."""
2182
def _get_repo_format_to_test(self):
2183
from bzrlib.repofmt import knitrepo
2184
return knitrepo.RepositoryFormatKnit1()
2187
def is_compatible(source, target):
2188
"""Be compatible with known Knit formats.
2190
We don't test for the stores being of specific types because that
2191
could lead to confusing results, and there is no need to be
2194
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
2196
are_knits = (isinstance(source._format, RepositoryFormatKnit) and
2197
isinstance(target._format, RepositoryFormatKnit))
2198
except AttributeError:
2200
return are_knits and InterRepository._same_model(source, target)
2203
def fetch(self, revision_id=None, pb=None):
2204
"""See InterRepository.fetch()."""
2205
from bzrlib.fetch import KnitRepoFetcher
2206
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2207
self.source, self.source._format, self.target, self.target._format)
2208
f = KnitRepoFetcher(to_repository=self.target,
2209
from_repository=self.source,
2210
last_revision=revision_id,
2212
return f.count_copied, f.failed_revisions
2215
def missing_revision_ids(self, revision_id=None):
2216
"""See InterRepository.missing_revision_ids()."""
2217
if revision_id is not None:
2218
source_ids = self.source.get_ancestry(revision_id)
2219
assert source_ids[0] is None
2222
source_ids = self.source.all_revision_ids()
2223
source_ids_set = set(source_ids)
2224
# source_ids is the worst possible case we may need to pull.
2225
# now we want to filter source_ids against what we actually
2226
# have in target, but don't try to check for existence where we know
2227
# we do not have a revision as that would be pointless.
2228
target_ids = set(self.target.all_revision_ids())
2229
possibly_present_revisions = target_ids.intersection(source_ids_set)
2230
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2231
required_revisions = source_ids_set.difference(actually_present_revisions)
2232
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2233
if revision_id is not None:
2234
# we used get_ancestry to determine source_ids then we are assured all
2235
# revisions referenced are present as they are installed in topological order.
2236
# and the tip revision was validated by get_ancestry.
2237
return required_topo_revisions
2239
# if we just grabbed the possibly available ids, then
2240
# we only have an estimate of whats available and need to validate
2241
# that against the revision records.
2242
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2245
class InterModel1and2(InterRepository):
2248
def _get_repo_format_to_test(self):
2252
def is_compatible(source, target):
2253
if not source.supports_rich_root() and target.supports_rich_root():
2259
def fetch(self, revision_id=None, pb=None):
2260
"""See InterRepository.fetch()."""
2261
from bzrlib.fetch import Model1toKnit2Fetcher
2262
f = Model1toKnit2Fetcher(to_repository=self.target,
2263
from_repository=self.source,
2264
last_revision=revision_id,
2266
return f.count_copied, f.failed_revisions
2269
def copy_content(self, revision_id=None):
2270
"""Make a complete copy of the content in self into destination.
2272
This is a destructive operation! Do not use it on existing
2275
:param revision_id: Only copy the content needed to construct
2276
revision_id and its parents.
2279
self.target.set_make_working_trees(self.source.make_working_trees())
2280
except NotImplementedError:
2282
# but don't bother fetching if we have the needed data now.
2283
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2284
self.target.has_revision(revision_id)):
2286
self.target.fetch(self.source, revision_id=revision_id)
2289
class InterKnit1and2(InterKnitRepo):
2292
def _get_repo_format_to_test(self):
2296
def is_compatible(source, target):
2297
"""Be compatible with Knit1 source and Knit3 target"""
2298
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
2300
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1, \
2301
RepositoryFormatKnit3
2302
return (isinstance(source._format, (RepositoryFormatKnit1)) and
2303
isinstance(target._format, (RepositoryFormatKnit3)))
2304
except AttributeError:
2308
def fetch(self, revision_id=None, pb=None):
2309
"""See InterRepository.fetch()."""
2310
from bzrlib.fetch import Knit1to2Fetcher
2311
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2312
self.source, self.source._format, self.target,
2313
self.target._format)
2314
f = Knit1to2Fetcher(to_repository=self.target,
2315
from_repository=self.source,
2316
last_revision=revision_id,
2318
return f.count_copied, f.failed_revisions
2321
class InterRemoteToOther(InterRepository):
2323
def __init__(self, source, target):
2324
InterRepository.__init__(self, source, target)
2325
self._real_inter = None
2328
def is_compatible(source, target):
2329
if not isinstance(source, remote.RemoteRepository):
2331
source._ensure_real()
2332
real_source = source._real_repository
2333
# Is source's model compatible with target's model, and are they the
2334
# same format? Currently we can only optimise fetching from an
2335
# identical model & format repo.
2336
assert not isinstance(real_source, remote.RemoteRepository), (
2337
"We don't support remote repos backed by remote repos yet.")
2338
return real_source._format == target._format
2341
def fetch(self, revision_id=None, pb=None):
2342
"""See InterRepository.fetch()."""
2343
from bzrlib.fetch import RemoteToOtherFetcher
2344
mutter("Using fetch logic to copy between %s(remote) and %s(%s)",
2345
self.source, self.target, self.target._format)
2346
# TODO: jam 20070210 This should be an assert, not a translate
2347
revision_id = osutils.safe_revision_id(revision_id)
2348
f = RemoteToOtherFetcher(to_repository=self.target,
2349
from_repository=self.source,
2350
last_revision=revision_id,
2352
return f.count_copied, f.failed_revisions
2355
def _get_repo_format_to_test(self):
2359
class InterOtherToRemote(InterRepository):
2361
def __init__(self, source, target):
2362
InterRepository.__init__(self, source, target)
2363
self._real_inter = None
2366
def is_compatible(source, target):
2367
if isinstance(target, remote.RemoteRepository):
2371
def _ensure_real_inter(self):
2372
if self._real_inter is None:
2373
self.target._ensure_real()
2374
real_target = self.target._real_repository
2375
self._real_inter = InterRepository.get(self.source, real_target)
2377
def copy_content(self, revision_id=None):
2378
self._ensure_real_inter()
2379
self._real_inter.copy_content(revision_id=revision_id)
2381
def fetch(self, revision_id=None, pb=None):
2382
self._ensure_real_inter()
2383
self._real_inter.fetch(revision_id=revision_id, pb=pb)
2386
def _get_repo_format_to_test(self):
2390
InterRepository.register_optimiser(InterSameDataRepository)
2391
InterRepository.register_optimiser(InterWeaveRepo)
2392
InterRepository.register_optimiser(InterKnitRepo)
2393
InterRepository.register_optimiser(InterModel1and2)
2394
InterRepository.register_optimiser(InterKnit1and2)
2395
InterRepository.register_optimiser(InterRemoteToOther)
2396
InterRepository.register_optimiser(InterOtherToRemote)
2399
class CopyConverter(object):
2400
"""A repository conversion tool which just performs a copy of the content.
2402
This is slow but quite reliable.
2405
def __init__(self, target_format):
2406
"""Create a CopyConverter.
2408
:param target_format: The format the resulting repository should be.
2410
self.target_format = target_format
2412
def convert(self, repo, pb):
2413
"""Perform the conversion of to_convert, giving feedback via pb.
2415
:param to_convert: The disk object to convert.
2416
:param pb: a progress bar to use for progress information.
2421
# this is only useful with metadir layouts - separated repo content.
2422
# trigger an assertion if not such
2423
repo._format.get_format_string()
2424
self.repo_dir = repo.bzrdir
2425
self.step('Moving repository to repository.backup')
2426
self.repo_dir.transport.move('repository', 'repository.backup')
2427
backup_transport = self.repo_dir.transport.clone('repository.backup')
2428
repo._format.check_conversion_target(self.target_format)
2429
self.source_repo = repo._format.open(self.repo_dir,
2431
_override_transport=backup_transport)
2432
self.step('Creating new repository')
2433
converted = self.target_format.initialize(self.repo_dir,
2434
self.source_repo.is_shared())
2435
converted.lock_write()
2437
self.step('Copying content into repository.')
2438
self.source_repo.copy_content_into(converted)
2441
self.step('Deleting old repository content.')
2442
self.repo_dir.transport.delete_tree('repository.backup')
2443
self.pb.note('repository converted')
2445
def step(self, message):
2446
"""Update the pb by a step."""
2448
self.pb.update(message, self.count, self.total)
2460
def _unescaper(match, _map=_unescape_map):
2461
code = match.group(1)
2465
if not code.startswith('#'):
2467
return unichr(int(code[1:])).encode('utf8')
2473
def _unescape_xml(data):
2474
"""Unescape predefined XML entities in a string of data."""
2476
if _unescape_re is None:
2477
_unescape_re = re.compile('\&([^;]*);')
2478
return _unescape_re.sub(_unescaper, data)
2481
class _RevisionTextVersionCache(object):
2482
"""A cache of the versionedfile versions for revision and file-id."""
2484
def __init__(self, repository):
2485
self.repository = repository
2486
self.revision_versions = {}
2487
self.revision_parents = {}
2488
self.repo_graph = self.repository.get_graph()
2489
# XXX: RBC: I haven't tracked down what uses this, but it would be
2490
# better to use the headscache directly I think.
2491
self.heads = graph.HeadsCache(self.repo_graph).heads
2493
def add_revision_text_versions(self, tree):
2494
"""Cache text version data from the supplied revision tree"""
2496
for path, entry in tree.iter_entries_by_dir():
2497
inv_revisions[entry.file_id] = entry.revision
2498
self.revision_versions[tree.get_revision_id()] = inv_revisions
2499
return inv_revisions
2501
def get_text_version(self, file_id, revision_id):
2502
"""Determine the text version for a given file-id and revision-id"""
2504
inv_revisions = self.revision_versions[revision_id]
2507
tree = self.repository.revision_tree(revision_id)
2508
except errors.RevisionNotPresent:
2509
self.revision_versions[revision_id] = inv_revisions = {}
2511
inv_revisions = self.add_revision_text_versions(tree)
2512
return inv_revisions.get(file_id)
2514
def prepopulate_revs(self, revision_ids):
2515
# Filter out versions that we don't have an inventory for, so that the
2516
# revision_trees() call won't fail.
2517
inv_weave = self.repository.get_inventory_weave()
2518
revs = [r for r in revision_ids if inv_weave.has_version(r)]
2519
# XXX: this loop is very similar to
2520
# bzrlib.fetch.Inter1and2Helper.iter_rev_trees.
2522
for tree in self.repository.revision_trees(revs[:100]):
2523
if tree.inventory.revision_id is None:
2524
tree.inventory.revision_id = tree.get_revision_id()
2525
self.add_revision_text_versions(tree)
2528
def get_parents(self, revision_id):
2530
return self.revision_parents[revision_id]
2532
parents = self.repository.get_parents([revision_id])[0]
2533
self.revision_parents[revision_id] = parents
2537
class VersionedFileChecker(object):
2539
def __init__(self, planned_revisions, revision_versions, repository):
2540
self.planned_revisions = planned_revisions
2541
self.revision_versions = revision_versions
2542
self.repository = repository
2544
def calculate_file_version_parents(self, revision_id, file_id):
2545
text_revision = self.revision_versions.get_text_version(
2546
file_id, revision_id)
2547
if text_revision is None:
2549
parents_of_text_revision = self.revision_versions.get_parents(
2551
parents_from_inventories = []
2552
for parent in parents_of_text_revision:
2553
if parent == _mod_revision.NULL_REVISION:
2555
introduced_in = self.revision_versions.get_text_version(file_id,
2557
if introduced_in is not None:
2558
parents_from_inventories.append(introduced_in)
2559
heads = set(self.revision_versions.heads(parents_from_inventories))
2561
for parent in parents_from_inventories:
2562
if parent in heads and parent not in new_parents:
2563
new_parents.append(parent)
2566
def check_file_version_parents(self, weave, file_id):
2568
for num, revision_id in enumerate(self.planned_revisions):
2569
correct_parents = self.calculate_file_version_parents(
2570
revision_id, file_id)
2571
if correct_parents is None:
2573
text_revision = self.revision_versions.get_text_version(
2574
file_id, revision_id)
2575
knit_parents = weave.get_parents(text_revision)
2576
if correct_parents != knit_parents:
2577
result[revision_id] = (knit_parents, correct_parents)