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,
45
from bzrlib.bundle import serializer
46
from bzrlib.revisiontree import RevisionTree
47
from bzrlib.store.versioned import VersionedFileStore
48
from bzrlib.store.text import TextStore
49
from bzrlib.testament import Testament
50
from bzrlib.util import bencode
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
54
from bzrlib.inter import InterObject
55
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
56
from bzrlib.symbol_versioning import (
59
from bzrlib.trace import mutter, mutter_callsite, note, warning
62
# Old formats display a warning, but only once
63
_deprecation_warning_done = False
66
class CommitBuilder(object):
67
"""Provides an interface to build up a commit.
69
This allows describing a tree to be committed without needing to
70
know the internals of the format of the repository.
73
# all clients should supply tree roots.
74
record_root_entry = True
75
# the default CommitBuilder does not manage trees whose root is versioned.
76
_versioned_root = False
78
def __init__(self, repository, parents, config, timestamp=None,
79
timezone=None, committer=None, revprops=None,
81
"""Initiate a CommitBuilder.
83
:param repository: Repository to commit to.
84
:param parents: Revision ids of the parents of the new revision.
85
:param config: Configuration to use.
86
:param timestamp: Optional timestamp recorded for commit.
87
:param timezone: Optional timezone for timestamp.
88
:param committer: Optional committer to set for commit.
89
:param revprops: Optional dictionary of revision properties.
90
:param revision_id: Optional revision id.
95
self._committer = self._config.username()
97
assert isinstance(committer, basestring), type(committer)
98
self._committer = committer
100
self.new_inventory = Inventory(None)
101
self._new_revision_id = revision_id
102
self.parents = parents
103
self.repository = repository
106
if revprops is not None:
107
self._revprops.update(revprops)
109
if timestamp is None:
110
timestamp = time.time()
111
# Restrict resolution to 1ms
112
self._timestamp = round(timestamp, 3)
115
self._timezone = osutils.local_time_offset()
117
self._timezone = int(timezone)
119
self._generate_revision_if_needed()
120
self._heads = graph.HeadsCache(repository.get_graph()).heads
122
def commit(self, message):
123
"""Make the actual commit.
125
:return: The revision id of the recorded revision.
127
rev = _mod_revision.Revision(
128
timestamp=self._timestamp,
129
timezone=self._timezone,
130
committer=self._committer,
132
inventory_sha1=self.inv_sha1,
133
revision_id=self._new_revision_id,
134
properties=self._revprops)
135
rev.parent_ids = self.parents
136
self.repository.add_revision(self._new_revision_id, rev,
137
self.new_inventory, self._config)
138
self.repository.commit_write_group()
139
return self._new_revision_id
142
"""Abort the commit that is being built.
144
self.repository.abort_write_group()
146
def revision_tree(self):
147
"""Return the tree that was just committed.
149
After calling commit() this can be called to get a RevisionTree
150
representing the newly committed tree. This is preferred to
151
calling Repository.revision_tree() because that may require
152
deserializing the inventory, while we already have a copy in
155
return RevisionTree(self.repository, self.new_inventory,
156
self._new_revision_id)
158
def finish_inventory(self):
159
"""Tell the builder that the inventory is finished."""
160
if self.new_inventory.root is None:
161
raise AssertionError('Root entry should be supplied to'
162
' record_entry_contents, as of bzr 0.10.',
163
DeprecationWarning, stacklevel=2)
164
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
165
self.new_inventory.revision_id = self._new_revision_id
166
self.inv_sha1 = self.repository.add_inventory(
167
self._new_revision_id,
172
def _gen_revision_id(self):
173
"""Return new revision-id."""
174
return generate_ids.gen_revision_id(self._config.username(),
177
def _generate_revision_if_needed(self):
178
"""Create a revision id if None was supplied.
180
If the repository can not support user-specified revision ids
181
they should override this function and raise CannotSetRevisionId
182
if _new_revision_id is not None.
184
:raises: CannotSetRevisionId
186
if self._new_revision_id is None:
187
self._new_revision_id = self._gen_revision_id()
188
self.random_revid = True
190
self.random_revid = False
192
def _check_root(self, ie, parent_invs, tree):
193
"""Helper for record_entry_contents.
195
:param ie: An entry being added.
196
:param parent_invs: The inventories of the parent revisions of the
198
:param tree: The tree that is being committed.
200
# In this revision format, root entries have no knit or weave When
201
# serializing out to disk and back in root.revision is always
203
ie.revision = self._new_revision_id
205
def _get_delta(self, ie, basis_inv, path):
206
"""Get a delta against the basis inventory for ie."""
207
if ie.file_id not in basis_inv:
209
return (None, path, ie.file_id, ie)
210
elif ie != basis_inv[ie.file_id]:
212
# TODO: avoid tis id2path call.
213
return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
218
def record_entry_contents(self, ie, parent_invs, path, tree,
220
"""Record the content of ie from tree into the commit if needed.
222
Side effect: sets ie.revision when unchanged
224
:param ie: An inventory entry present in the commit.
225
:param parent_invs: The inventories of the parent revisions of the
227
:param path: The path the entry is at in the tree.
228
:param tree: The tree which contains this entry and should be used to
230
:param content_summary: Summary data from the tree about the paths
231
content - stat, length, exec, sha/link target. This is only
232
accessed when the entry has a revision of None - that is when it is
233
a candidate to commit.
234
:return: A tuple (change_delta, version_recorded). change_delta is
235
an inventory_delta change for this entry against the basis tree of
236
the commit, or None if no change occured against the basis tree.
237
version_recorded is True if a new version of the entry has been
238
recorded. For instance, committing a merge where a file was only
239
changed on the other side will return (delta, False).
241
if self.new_inventory.root is None:
242
if ie.parent_id is not None:
243
raise errors.RootMissing()
244
self._check_root(ie, parent_invs, tree)
245
if ie.revision is None:
246
kind = content_summary[0]
248
# ie is carried over from a prior commit
250
# XXX: repository specific check for nested tree support goes here - if
251
# the repo doesn't want nested trees we skip it ?
252
if (kind == 'tree-reference' and
253
not self.repository._format.supports_tree_reference):
254
# mismatch between commit builder logic and repository:
255
# this needs the entry creation pushed down into the builder.
256
raise NotImplementedError('Missing repository subtree support.')
257
self.new_inventory.add(ie)
259
# TODO: slow, take it out of the inner loop.
261
basis_inv = parent_invs[0]
263
basis_inv = Inventory(root_id=None)
265
# ie.revision is always None if the InventoryEntry is considered
266
# for committing. We may record the previous parents revision if the
267
# content is actually unchanged against a sole head.
268
if ie.revision is not None:
269
if not self._versioned_root and path == '':
270
# repositories that do not version the root set the root's
271
# revision to the new commit even when no change occurs, and
272
# this masks when a change may have occurred against the basis,
273
# so calculate if one happened.
274
if ie.file_id in basis_inv:
275
delta = (basis_inv.id2path(ie.file_id), path,
279
delta = (None, path, ie.file_id, ie)
282
# we don't need to commit this, because the caller already
283
# determined that an existing revision of this file is
285
return None, (ie.revision == self._new_revision_id)
286
# XXX: Friction: parent_candidates should return a list not a dict
287
# so that we don't have to walk the inventories again.
288
parent_candiate_entries = ie.parent_candidates(parent_invs)
289
head_set = self._heads(parent_candiate_entries.keys())
291
for inv in parent_invs:
292
if ie.file_id in inv:
293
old_rev = inv[ie.file_id].revision
294
if old_rev in head_set:
295
heads.append(inv[ie.file_id].revision)
296
head_set.remove(inv[ie.file_id].revision)
299
# now we check to see if we need to write a new record to the
301
# We write a new entry unless there is one head to the ancestors, and
302
# the kind-derived content is unchanged.
304
# Cheapest check first: no ancestors, or more the one head in the
305
# ancestors, we write a new node.
309
# There is a single head, look it up for comparison
310
parent_entry = parent_candiate_entries[heads[0]]
311
# if the non-content specific data has changed, we'll be writing a
313
if (parent_entry.parent_id != ie.parent_id or
314
parent_entry.name != ie.name):
316
# now we need to do content specific checks:
318
# if the kind changed the content obviously has
319
if kind != parent_entry.kind:
322
assert content_summary[2] is not None, \
323
"Files must not have executable = None"
325
if (# if the file length changed we have to store:
326
parent_entry.text_size != content_summary[1] or
327
# if the exec bit has changed we have to store:
328
parent_entry.executable != content_summary[2]):
330
elif parent_entry.text_sha1 == content_summary[3]:
331
# all meta and content is unchanged (using a hash cache
332
# hit to check the sha)
333
ie.revision = parent_entry.revision
334
ie.text_size = parent_entry.text_size
335
ie.text_sha1 = parent_entry.text_sha1
336
ie.executable = parent_entry.executable
337
return self._get_delta(ie, basis_inv, path), False
339
# Either there is only a hash change(no hash cache entry,
340
# or same size content change), or there is no change on
342
# Provide the parent's hash to the store layer, so that the
343
# content is unchanged we will not store a new node.
344
nostore_sha = parent_entry.text_sha1
346
# We want to record a new node regardless of the presence or
347
# absence of a content change in the file.
349
ie.executable = content_summary[2]
350
lines = tree.get_file(ie.file_id, path).readlines()
352
ie.text_sha1, ie.text_size = self._add_text_to_weave(
353
ie.file_id, lines, heads, nostore_sha)
354
except errors.ExistingContent:
355
# Turns out that the file content was unchanged, and we were
356
# only going to store a new node if it was changed. Carry over
358
ie.revision = parent_entry.revision
359
ie.text_size = parent_entry.text_size
360
ie.text_sha1 = parent_entry.text_sha1
361
ie.executable = parent_entry.executable
362
return self._get_delta(ie, basis_inv, path), False
363
elif kind == 'directory':
365
# all data is meta here, nothing specific to directory, so
367
ie.revision = parent_entry.revision
368
return self._get_delta(ie, basis_inv, path), False
370
self._add_text_to_weave(ie.file_id, lines, heads, None)
371
elif kind == 'symlink':
372
current_link_target = content_summary[3]
374
# symlink target is not generic metadata, check if it has
376
if current_link_target != parent_entry.symlink_target:
379
# unchanged, carry over.
380
ie.revision = parent_entry.revision
381
ie.symlink_target = parent_entry.symlink_target
382
return self._get_delta(ie, basis_inv, path), False
383
ie.symlink_target = current_link_target
385
self._add_text_to_weave(ie.file_id, lines, heads, None)
386
elif kind == 'tree-reference':
388
if content_summary[3] != parent_entry.reference_revision:
391
# unchanged, carry over.
392
ie.reference_revision = parent_entry.reference_revision
393
ie.revision = parent_entry.revision
394
return self._get_delta(ie, basis_inv, path), False
395
ie.reference_revision = content_summary[3]
397
self._add_text_to_weave(ie.file_id, lines, heads, None)
399
raise NotImplementedError('unknown kind')
400
ie.revision = self._new_revision_id
401
return self._get_delta(ie, basis_inv, path), True
403
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
404
versionedfile = self.repository.weave_store.get_weave_or_empty(
405
file_id, self.repository.get_transaction())
406
# Don't change this to add_lines - add_lines_with_ghosts is cheaper
407
# than add_lines, and allows committing when a parent is ghosted for
409
# Note: as we read the content directly from the tree, we know its not
410
# been turned into unicode or badly split - but a broken tree
411
# implementation could give us bad output from readlines() so this is
412
# not a guarantee of safety. What would be better is always checking
413
# the content during test suite execution. RBC 20070912
415
return versionedfile.add_lines_with_ghosts(
416
self._new_revision_id, parents, new_lines,
417
nostore_sha=nostore_sha, random_id=self.random_revid,
418
check_content=False)[0:2]
420
versionedfile.clear_cache()
423
class RootCommitBuilder(CommitBuilder):
424
"""This commitbuilder actually records the root id"""
426
# the root entry gets versioned properly by this builder.
427
_versioned_root = True
429
def _check_root(self, ie, parent_invs, tree):
430
"""Helper for record_entry_contents.
432
:param ie: An entry being added.
433
:param parent_invs: The inventories of the parent revisions of the
435
:param tree: The tree that is being committed.
439
######################################################################
442
class Repository(object):
443
"""Repository holding history for one or more branches.
445
The repository holds and retrieves historical information including
446
revisions and file history. It's normally accessed only by the Branch,
447
which views a particular line of development through that history.
449
The Repository builds on top of Stores and a Transport, which respectively
450
describe the disk data format and the way of accessing the (possibly
454
# What class to use for a CommitBuilder. Often its simpler to change this
455
# in a Repository class subclass rather than to override
456
# get_commit_builder.
457
_commit_builder_class = CommitBuilder
458
# The search regex used by xml based repositories to determine what things
459
# where changed in a single commit.
460
_file_ids_altered_regex = lazy_regex.lazy_compile(
461
r'file_id="(?P<file_id>[^"]+)"'
462
r'.* revision="(?P<revision_id>[^"]+)"'
465
def abort_write_group(self):
466
"""Commit the contents accrued within the current write group.
468
:seealso: start_write_group.
470
if self._write_group is not self.get_transaction():
471
# has an unlock or relock occured ?
472
raise errors.BzrError('mismatched lock context and write group.')
473
self._abort_write_group()
474
self._write_group = None
476
def _abort_write_group(self):
477
"""Template method for per-repository write group cleanup.
479
This is called during abort before the write group is considered to be
480
finished and should cleanup any internal state accrued during the write
481
group. There is no requirement that data handed to the repository be
482
*not* made available - this is not a rollback - but neither should any
483
attempt be made to ensure that data added is fully commited. Abort is
484
invoked when an error has occured so futher disk or network operations
485
may not be possible or may error and if possible should not be
490
def add_inventory(self, revision_id, inv, parents):
491
"""Add the inventory inv to the repository as revision_id.
493
:param parents: The revision ids of the parents that revision_id
494
is known to have and are in the repository already.
496
returns the sha1 of the serialized inventory.
498
assert self.is_in_write_group()
499
_mod_revision.check_not_reserved_id(revision_id)
500
assert inv.revision_id is None or inv.revision_id == revision_id, \
501
"Mismatch between inventory revision" \
502
" id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
503
assert inv.root is not None
504
inv_lines = self._serialise_inventory_to_lines(inv)
505
inv_vf = self.get_inventory_weave()
506
return self._inventory_add_lines(inv_vf, revision_id, parents,
507
inv_lines, check_content=False)
509
def _inventory_add_lines(self, inv_vf, revision_id, parents, lines,
511
"""Store lines in inv_vf and return the sha1 of the inventory."""
513
for parent in parents:
515
final_parents.append(parent)
516
return inv_vf.add_lines(revision_id, final_parents, lines,
517
check_content=check_content)[0]
520
def add_revision(self, revision_id, rev, inv=None, config=None):
521
"""Add rev to the revision store as revision_id.
523
:param revision_id: the revision id to use.
524
:param rev: The revision object.
525
:param inv: The inventory for the revision. if None, it will be looked
526
up in the inventory storer
527
:param config: If None no digital signature will be created.
528
If supplied its signature_needed method will be used
529
to determine if a signature should be made.
531
# TODO: jam 20070210 Shouldn't we check rev.revision_id and
533
_mod_revision.check_not_reserved_id(revision_id)
534
if config is not None and config.signature_needed():
536
inv = self.get_inventory(revision_id)
537
plaintext = Testament(rev, inv).as_short_text()
538
self.store_revision_signature(
539
gpg.GPGStrategy(config), plaintext, revision_id)
540
if not revision_id in self.get_inventory_weave():
542
raise errors.WeaveRevisionNotPresent(revision_id,
543
self.get_inventory_weave())
545
# yes, this is not suitable for adding with ghosts.
546
self.add_inventory(revision_id, inv, rev.parent_ids)
547
self._revision_store.add_revision(rev, self.get_transaction())
549
def _add_revision_text(self, revision_id, text):
550
revision = self._revision_store._serializer.read_revision_from_string(
552
self._revision_store._add_revision(revision, StringIO(text),
553
self.get_transaction())
555
def all_revision_ids(self):
556
"""Returns a list of all the revision ids in the repository.
558
This is deprecated because code should generally work on the graph
559
reachable from a particular revision, and ignore any other revisions
560
that might be present. There is no direct replacement method.
562
if 'evil' in debug.debug_flags:
563
mutter_callsite(2, "all_revision_ids is linear with history.")
564
return self._all_revision_ids()
566
def _all_revision_ids(self):
567
"""Returns a list of all the revision ids in the repository.
569
These are in as much topological order as the underlying store can
572
raise NotImplementedError(self._all_revision_ids)
574
def break_lock(self):
575
"""Break a lock if one is present from another instance.
577
Uses the ui factory to ask for confirmation if the lock may be from
580
self.control_files.break_lock()
583
def _eliminate_revisions_not_present(self, revision_ids):
584
"""Check every revision id in revision_ids to see if we have it.
586
Returns a set of the present revisions.
589
for id in revision_ids:
590
if self.has_revision(id):
595
def create(a_bzrdir):
596
"""Construct the current default format repository in a_bzrdir."""
597
return RepositoryFormat.get_default_format().initialize(a_bzrdir)
599
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
600
"""instantiate a Repository.
602
:param _format: The format of the repository on disk.
603
:param a_bzrdir: The BzrDir of the repository.
605
In the future we will have a single api for all stores for
606
getting file texts, inventories and revisions, then
607
this construct will accept instances of those things.
609
super(Repository, self).__init__()
610
self._format = _format
611
# the following are part of the public API for Repository:
612
self.bzrdir = a_bzrdir
613
self.control_files = control_files
614
self._revision_store = _revision_store
615
# backwards compatibility
616
self.weave_store = text_store
618
self._reconcile_does_inventory_gc = True
619
self._reconcile_fixes_text_parents = False
620
self._reconcile_backsup_inventory = True
621
# not right yet - should be more semantically clear ?
623
self.control_store = control_store
624
self.control_weaves = control_store
625
# TODO: make sure to construct the right store classes, etc, depending
626
# on whether escaping is required.
627
self._warn_if_deprecated()
628
self._write_group = None
629
self.base = control_files._transport.base
632
return '%s(%r)' % (self.__class__.__name__,
635
def has_same_location(self, other):
636
"""Returns a boolean indicating if this repository is at the same
637
location as another repository.
639
This might return False even when two repository objects are accessing
640
the same physical repository via different URLs.
642
if self.__class__ is not other.__class__:
644
return (self.control_files._transport.base ==
645
other.control_files._transport.base)
647
def is_in_write_group(self):
648
"""Return True if there is an open write group.
650
:seealso: start_write_group.
652
return self._write_group is not None
655
return self.control_files.is_locked()
657
def is_write_locked(self):
658
"""Return True if this object is write locked."""
659
return self.is_locked() and self.control_files._lock_mode == 'w'
661
def lock_write(self, token=None):
662
"""Lock this repository for writing.
664
This causes caching within the repository obejct to start accumlating
665
data during reads, and allows a 'write_group' to be obtained. Write
666
groups must be used for actual data insertion.
668
:param token: if this is already locked, then lock_write will fail
669
unless the token matches the existing lock.
670
:returns: a token if this instance supports tokens, otherwise None.
671
:raises TokenLockingNotSupported: when a token is given but this
672
instance doesn't support using token locks.
673
:raises MismatchedToken: if the specified token doesn't match the token
674
of the existing lock.
675
:seealso: start_write_group.
677
A token should be passed in if you know that you have locked the object
678
some other way, and need to synchronise this object's state with that
681
XXX: this docstring is duplicated in many places, e.g. lockable_files.py
683
result = self.control_files.lock_write(token=token)
688
self.control_files.lock_read()
691
def get_physical_lock_status(self):
692
return self.control_files.get_physical_lock_status()
694
def leave_lock_in_place(self):
695
"""Tell this repository not to release the physical lock when this
698
If lock_write doesn't return a token, then this method is not supported.
700
self.control_files.leave_in_place()
702
def dont_leave_lock_in_place(self):
703
"""Tell this repository to release the physical lock when this
704
object is unlocked, even if it didn't originally acquire it.
706
If lock_write doesn't return a token, then this method is not supported.
708
self.control_files.dont_leave_in_place()
711
def gather_stats(self, revid=None, committers=None):
712
"""Gather statistics from a revision id.
714
:param revid: The revision id to gather statistics from, if None, then
715
no revision specific statistics are gathered.
716
:param committers: Optional parameter controlling whether to grab
717
a count of committers from the revision specific statistics.
718
:return: A dictionary of statistics. Currently this contains:
719
committers: The number of committers if requested.
720
firstrev: A tuple with timestamp, timezone for the penultimate left
721
most ancestor of revid, if revid is not the NULL_REVISION.
722
latestrev: A tuple with timestamp, timezone for revid, if revid is
723
not the NULL_REVISION.
724
revisions: The total revision count in the repository.
725
size: An estimate disk size of the repository in bytes.
728
if revid and committers:
729
result['committers'] = 0
730
if revid and revid != _mod_revision.NULL_REVISION:
732
all_committers = set()
733
revisions = self.get_ancestry(revid)
734
# pop the leading None
736
first_revision = None
738
# ignore the revisions in the middle - just grab first and last
739
revisions = revisions[0], revisions[-1]
740
for revision in self.get_revisions(revisions):
741
if not first_revision:
742
first_revision = revision
744
all_committers.add(revision.committer)
745
last_revision = revision
747
result['committers'] = len(all_committers)
748
result['firstrev'] = (first_revision.timestamp,
749
first_revision.timezone)
750
result['latestrev'] = (last_revision.timestamp,
751
last_revision.timezone)
753
# now gather global repository information
754
if self.bzrdir.root_transport.listable():
755
c, t = self._revision_store.total_size(self.get_transaction())
756
result['revisions'] = c
760
def get_data_stream(self, revision_ids):
761
raise NotImplementedError(self.get_data_stream)
763
def insert_data_stream(self, stream):
764
"""XXX What does this really do?
766
Is it a substitute for fetch?
767
Should it manage its own write group ?
769
for item_key, bytes in stream:
770
if item_key[0] == 'file':
771
(file_id,) = item_key[1:]
772
knit = self.weave_store.get_weave_or_empty(
773
file_id, self.get_transaction())
774
elif item_key == ('inventory',):
775
knit = self.get_inventory_weave()
776
elif item_key == ('revisions',):
777
knit = self._revision_store.get_revision_file(
778
self.get_transaction())
779
elif item_key == ('signatures',):
780
knit = self._revision_store.get_signature_file(
781
self.get_transaction())
783
raise RepositoryDataStreamError(
784
"Unrecognised data stream key '%s'" % (item_key,))
785
decoded_list = bencode.bdecode(bytes)
786
format = decoded_list.pop(0)
789
for version, options, parents, some_bytes in decoded_list:
790
data_list.append((version, options, len(some_bytes), parents))
791
knit_bytes += some_bytes
792
knit.insert_data_stream(
793
(format, data_list, StringIO(knit_bytes).read))
796
def missing_revision_ids(self, other, revision_id=None):
797
"""Return the revision ids that other has that this does not.
799
These are returned in topological order.
801
revision_id: only return revision ids included by revision_id.
803
return InterRepository.get(other, self).missing_revision_ids(revision_id)
807
"""Open the repository rooted at base.
809
For instance, if the repository is at URL/.bzr/repository,
810
Repository.open(URL) -> a Repository instance.
812
control = bzrdir.BzrDir.open(base)
813
return control.open_repository()
815
def copy_content_into(self, destination, revision_id=None):
816
"""Make a complete copy of the content in self into destination.
818
This is a destructive operation! Do not use it on existing
821
return InterRepository.get(self, destination).copy_content(revision_id)
823
def commit_write_group(self):
824
"""Commit the contents accrued within the current write group.
826
:seealso: start_write_group.
828
if self._write_group is not self.get_transaction():
829
# has an unlock or relock occured ?
830
raise errors.BzrError('mismatched lock context %r and '
832
(self.get_transaction(), self._write_group))
833
self._commit_write_group()
834
self._write_group = None
836
def _commit_write_group(self):
837
"""Template method for per-repository write group cleanup.
839
This is called before the write group is considered to be
840
finished and should ensure that all data handed to the repository
841
for writing during the write group is safely committed (to the
842
extent possible considering file system caching etc).
845
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
846
"""Fetch the content required to construct revision_id from source.
848
If revision_id is None all content is copied.
849
:param find_ghosts: Find and copy revisions in the source that are
850
ghosts in the target (and not reachable directly by walking out to
851
the first-present revision in target from revision_id).
853
# fast path same-url fetch operations
854
if self.has_same_location(source):
855
# check that last_revision is in 'from' and then return a
857
if (revision_id is not None and
858
not _mod_revision.is_null(revision_id)):
859
self.get_revision(revision_id)
861
inter = InterRepository.get(source, self)
863
return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
864
except NotImplementedError:
865
raise errors.IncompatibleRepositories(source, self)
867
def create_bundle(self, target, base, fileobj, format=None):
868
return serializer.write_bundle(self, target, base, fileobj, format)
870
def get_commit_builder(self, branch, parents, config, timestamp=None,
871
timezone=None, committer=None, revprops=None,
873
"""Obtain a CommitBuilder for this repository.
875
:param branch: Branch to commit to.
876
:param parents: Revision ids of the parents of the new revision.
877
:param config: Configuration to use.
878
:param timestamp: Optional timestamp recorded for commit.
879
:param timezone: Optional timezone for timestamp.
880
:param committer: Optional committer to set for commit.
881
:param revprops: Optional dictionary of revision properties.
882
:param revision_id: Optional revision id.
884
result = self._commit_builder_class(self, parents, config,
885
timestamp, timezone, committer, revprops, revision_id)
886
self.start_write_group()
890
if (self.control_files._lock_count == 1 and
891
self.control_files._lock_mode == 'w'):
892
if self._write_group is not None:
893
self.abort_write_group()
894
self.control_files.unlock()
895
raise errors.BzrError(
896
'Must end write groups before releasing write locks.')
897
self.control_files.unlock()
900
def clone(self, a_bzrdir, revision_id=None):
901
"""Clone this repository into a_bzrdir using the current format.
903
Currently no check is made that the format of this repository and
904
the bzrdir format are compatible. FIXME RBC 20060201.
906
:return: The newly created destination repository.
908
# TODO: deprecate after 0.16; cloning this with all its settings is
909
# probably not very useful -- mbp 20070423
910
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
911
self.copy_content_into(dest_repo, revision_id)
914
def start_write_group(self):
915
"""Start a write group in the repository.
917
Write groups are used by repositories which do not have a 1:1 mapping
918
between file ids and backend store to manage the insertion of data from
919
both fetch and commit operations.
921
A write lock is required around the start_write_group/commit_write_group
922
for the support of lock-requiring repository formats.
924
One can only insert data into a repository inside a write group.
928
if not self.is_write_locked():
929
raise errors.NotWriteLocked(self)
930
if self._write_group:
931
raise errors.BzrError('already in a write group')
932
self._start_write_group()
933
# so we can detect unlock/relock - the write group is now entered.
934
self._write_group = self.get_transaction()
936
def _start_write_group(self):
937
"""Template method for per-repository write group startup.
939
This is called before the write group is considered to be
944
def sprout(self, to_bzrdir, revision_id=None):
945
"""Create a descendent repository for new development.
947
Unlike clone, this does not copy the settings of the repository.
949
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
950
dest_repo.fetch(self, revision_id=revision_id)
953
def _create_sprouting_repo(self, a_bzrdir, shared):
954
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
955
# use target default format.
956
dest_repo = a_bzrdir.create_repository()
958
# Most control formats need the repository to be specifically
959
# created, but on some old all-in-one formats it's not needed
961
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
962
except errors.UninitializableFormat:
963
dest_repo = a_bzrdir.open_repository()
967
def has_revision(self, revision_id):
968
"""True if this repository has a copy of the revision."""
969
if 'evil' in debug.debug_flags:
970
mutter_callsite(3, "has_revision is a LBYL symptom.")
971
return self._revision_store.has_revision_id(revision_id,
972
self.get_transaction())
975
def get_revision(self, revision_id):
976
"""Return the Revision object for a named revision."""
977
return self.get_revisions([revision_id])[0]
980
def get_revision_reconcile(self, revision_id):
981
"""'reconcile' helper routine that allows access to a revision always.
983
This variant of get_revision does not cross check the weave graph
984
against the revision one as get_revision does: but it should only
985
be used by reconcile, or reconcile-alike commands that are correcting
986
or testing the revision graph.
988
return self._get_revisions([revision_id])[0]
991
def get_revisions(self, revision_ids):
992
"""Get many revisions at once."""
993
return self._get_revisions(revision_ids)
996
def _get_revisions(self, revision_ids):
997
"""Core work logic to get many revisions without sanity checks."""
998
for rev_id in revision_ids:
999
if not rev_id or not isinstance(rev_id, basestring):
1000
raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
1001
revs = self._revision_store.get_revisions(revision_ids,
1002
self.get_transaction())
1004
assert not isinstance(rev.revision_id, unicode)
1005
for parent_id in rev.parent_ids:
1006
assert not isinstance(parent_id, unicode)
1010
def get_revision_xml(self, revision_id):
1011
# TODO: jam 20070210 This shouldn't be necessary since get_revision
1012
# would have already do it.
1013
# TODO: jam 20070210 Just use _serializer.write_revision_to_string()
1014
rev = self.get_revision(revision_id)
1015
rev_tmp = StringIO()
1016
# the current serializer..
1017
self._revision_store._serializer.write_revision(rev, rev_tmp)
1019
return rev_tmp.getvalue()
1022
def get_deltas_for_revisions(self, revisions):
1023
"""Produce a generator of revision deltas.
1025
Note that the input is a sequence of REVISIONS, not revision_ids.
1026
Trees will be held in memory until the generator exits.
1027
Each delta is relative to the revision's lefthand predecessor.
1029
required_trees = set()
1030
for revision in revisions:
1031
required_trees.add(revision.revision_id)
1032
required_trees.update(revision.parent_ids[:1])
1033
trees = dict((t.get_revision_id(), t) for
1034
t in self.revision_trees(required_trees))
1035
for revision in revisions:
1036
if not revision.parent_ids:
1037
old_tree = self.revision_tree(None)
1039
old_tree = trees[revision.parent_ids[0]]
1040
yield trees[revision.revision_id].changes_from(old_tree)
1043
def get_revision_delta(self, revision_id):
1044
"""Return the delta for one revision.
1046
The delta is relative to the left-hand predecessor of the
1049
r = self.get_revision(revision_id)
1050
return list(self.get_deltas_for_revisions([r]))[0]
1053
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1054
signature = gpg_strategy.sign(plaintext)
1055
self._revision_store.add_revision_signature_text(revision_id,
1057
self.get_transaction())
1059
def find_text_key_references(self):
1060
"""Find the text key references within the repository.
1062
:return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
1063
revision_ids. Each altered file-ids has the exact revision_ids that
1064
altered it listed explicitly.
1065
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1066
to whether they were referred to by the inventory of the
1067
revision_id that they contain. The inventory texts from all present
1068
revision ids are assessed to generate this report.
1070
revision_ids = self.all_revision_ids()
1071
w = self.get_inventory_weave()
1072
pb = ui.ui_factory.nested_progress_bar()
1074
return self._find_text_key_references_from_xml_inventory_lines(
1075
w.iter_lines_added_or_present_in_versions(revision_ids, pb=pb))
1080
def _find_text_key_references_from_xml_inventory_lines(self,
1082
"""Core routine for extracting references to texts from inventories.
1084
This performs the translation of xml lines to revision ids.
1086
:param line_iterator: An iterator of lines, origin_version_id
1087
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1088
to whether they were referred to by the inventory of the
1089
revision_id that they contain. Note that if that revision_id was
1090
not part of the line_iterator's output then False will be given -
1091
even though it may actually refer to that key.
1093
assert self._serializer.support_altered_by_hack, \
1094
("_find_text_key_references_from_xml_inventory_lines only "
1095
"supported for branches which store inventory as unnested xml, "
1099
# this code needs to read every new line in every inventory for the
1100
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
1101
# not present in one of those inventories is unnecessary but not
1102
# harmful because we are filtering by the revision id marker in the
1103
# inventory lines : we only select file ids altered in one of those
1104
# revisions. We don't need to see all lines in the inventory because
1105
# only those added in an inventory in rev X can contain a revision=X
1107
unescape_revid_cache = {}
1108
unescape_fileid_cache = {}
1110
# jam 20061218 In a big fetch, this handles hundreds of thousands
1111
# of lines, so it has had a lot of inlining and optimizing done.
1112
# Sorry that it is a little bit messy.
1113
# Move several functions to be local variables, since this is a long
1115
search = self._file_ids_altered_regex.search
1116
unescape = _unescape_xml
1117
setdefault = result.setdefault
1118
for line, version_id in line_iterator:
1119
match = search(line)
1122
# One call to match.group() returning multiple items is quite a
1123
# bit faster than 2 calls to match.group() each returning 1
1124
file_id, revision_id = match.group('file_id', 'revision_id')
1126
# Inlining the cache lookups helps a lot when you make 170,000
1127
# lines and 350k ids, versus 8.4 unique ids.
1128
# Using a cache helps in 2 ways:
1129
# 1) Avoids unnecessary decoding calls
1130
# 2) Re-uses cached strings, which helps in future set and
1132
# (2) is enough that removing encoding entirely along with
1133
# the cache (so we are using plain strings) results in no
1134
# performance improvement.
1136
revision_id = unescape_revid_cache[revision_id]
1138
unescaped = unescape(revision_id)
1139
unescape_revid_cache[revision_id] = unescaped
1140
revision_id = unescaped
1142
# Note that unescaping always means that on a fulltext cached
1143
# inventory we deserialised every fileid, which for general 'pull'
1144
# is not great, but we don't really want to have some many
1145
# fulltexts that this matters anyway. RBC 20071114.
1147
file_id = unescape_fileid_cache[file_id]
1149
unescaped = unescape(file_id)
1150
unescape_fileid_cache[file_id] = unescaped
1153
key = (file_id, revision_id)
1154
setdefault(key, False)
1155
if revision_id == version_id:
1159
def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1161
"""Helper routine for fileids_altered_by_revision_ids.
1163
This performs the translation of xml lines to revision ids.
1165
:param line_iterator: An iterator of lines, origin_version_id
1166
:param revision_ids: The revision ids to filter for. This should be a
1167
set or other type which supports efficient __contains__ lookups, as
1168
the revision id from each parsed line will be looked up in the
1169
revision_ids filter.
1170
:return: a dictionary mapping altered file-ids to an iterable of
1171
revision_ids. Each altered file-ids has the exact revision_ids that
1172
altered it listed explicitly.
1175
setdefault = result.setdefault
1176
for file_id, revision_id in \
1177
self._find_text_key_references_from_xml_inventory_lines(
1178
line_iterator).iterkeys():
1179
# once data is all ensured-consistent; then this is
1180
# if revision_id == version_id
1181
if revision_id in revision_ids:
1182
setdefault(file_id, set()).add(revision_id)
1185
def fileids_altered_by_revision_ids(self, revision_ids):
1186
"""Find the file ids and versions affected by revisions.
1188
:param revisions: an iterable containing revision ids.
1189
:return: a dictionary mapping altered file-ids to an iterable of
1190
revision_ids. Each altered file-ids has the exact revision_ids that
1191
altered it listed explicitly.
1193
selected_revision_ids = set(revision_ids)
1194
w = self.get_inventory_weave()
1195
pb = ui.ui_factory.nested_progress_bar()
1197
return self._find_file_ids_from_xml_inventory_lines(
1198
w.iter_lines_added_or_present_in_versions(
1199
selected_revision_ids, pb=pb),
1200
selected_revision_ids)
1204
def iter_files_bytes(self, desired_files):
1205
"""Iterate through file versions.
1207
Files will not necessarily be returned in the order they occur in
1208
desired_files. No specific order is guaranteed.
1210
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1211
value supplied by the caller as part of desired_files. It should
1212
uniquely identify the file version in the caller's context. (Examples:
1213
an index number or a TreeTransform trans_id.)
1215
bytes_iterator is an iterable of bytestrings for the file. The
1216
kind of iterable and length of the bytestrings are unspecified, but for
1217
this implementation, it is a list of lines produced by
1218
VersionedFile.get_lines().
1220
:param desired_files: a list of (file_id, revision_id, identifier)
1223
transaction = self.get_transaction()
1224
for file_id, revision_id, callable_data in desired_files:
1226
weave = self.weave_store.get_weave(file_id, transaction)
1227
except errors.NoSuchFile:
1228
raise errors.NoSuchIdInRepository(self, file_id)
1229
yield callable_data, weave.get_lines(revision_id)
1231
def _generate_text_key_index(self):
1232
"""Generate a new text key index for the repository.
1234
This is an expensive function that will take considerable time to run.
1236
:return: A dict mapping text keys ((file_id, revision_id) tuples) to a
1237
list of parents, also text keys. When a given key has no parents,
1238
the parents list will be [NULL_REVISION].
1240
# All revisions, to find inventory parents.
1241
revision_graph = self.get_revision_graph_with_ghosts()
1242
ancestors = revision_graph.get_ancestors()
1243
revision_order = tsort.topo_sort(ancestors)
1244
invalid_keys = set()
1246
for revision_id in revision_order:
1247
revision_keys[revision_id] = set()
1248
text_key_references = self.find_text_key_references()
1249
text_count = len(text_key_references)
1250
# a cache of the text keys to allow reuse; costs a dict of all the
1251
# keys, but saves a 2-tuple for every child of a given key.
1253
for text_key, valid in text_key_references.iteritems():
1255
invalid_keys.add(text_key)
1257
revision_keys[text_key[1]].add(text_key)
1258
text_key_cache[text_key] = text_key
1259
del text_key_references
1261
text_graph = graph.Graph(graph.DictParentsProvider(text_index))
1262
NULL_REVISION = _mod_revision.NULL_REVISION
1263
pb = ui.ui_factory.nested_progress_bar()
1264
batch_size = 10 # should be ~150MB on a 55K path tree
1265
batch_count = len(revision_order) / batch_size + 1
1267
pb.update("Calculating text parents.", processed_texts, text_count)
1268
for offset in xrange(batch_count):
1269
to_query = revision_order[offset * batch_size:(offset + 1) *
1273
for rev_tree in self.revision_trees(to_query):
1274
revision_id = rev_tree.get_revision_id()
1275
parent_ids = ancestors[revision_id]
1276
for text_key in revision_keys[revision_id]:
1277
pb.update("Calculating text parents.", processed_texts)
1278
processed_texts += 1
1279
candidate_parents = []
1280
for parent_id in parent_ids:
1281
parent_text_key = (text_key[0], parent_id)
1283
check_parent = parent_text_key not in \
1284
revision_keys[parent_id]
1286
# the parent parent_id is a ghost:
1287
check_parent = False
1288
# truncate the derived graph against this ghost.
1289
parent_text_key = None
1291
# look at the parent commit details inventories to
1292
# determine possible candidates in the per file graph.
1294
parent_inv = self.revision_tree(parent_id).inventory
1295
parent_entry = parent_inv._byid.get(
1297
if parent_entry is not None:
1299
text_key[0], parent_entry.revision)
1301
parent_text_key = None
1302
if parent_text_key is not None:
1303
candidate_parents.append(
1304
text_key_cache[parent_text_key])
1305
parent_heads = text_graph.heads(candidate_parents)
1306
new_parents = list(parent_heads)
1307
new_parents.sort(key=lambda x:candidate_parents.index(x))
1308
if new_parents == []:
1309
new_parents = [NULL_REVISION]
1310
text_index[text_key] = new_parents
1312
for text_key in invalid_keys:
1313
text_index[text_key] = [NULL_REVISION]
1316
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1317
"""Get an iterable listing the keys of all the data introduced by a set
1320
The keys will be ordered so that the corresponding items can be safely
1321
fetched and inserted in that order.
1323
:returns: An iterable producing tuples of (knit-kind, file-id,
1324
versions). knit-kind is one of 'file', 'inventory', 'signatures',
1325
'revisions'. file-id is None unless knit-kind is 'file'.
1327
# XXX: it's a bit weird to control the inventory weave caching in this
1328
# generator. Ideally the caching would be done in fetch.py I think. Or
1329
# maybe this generator should explicitly have the contract that it
1330
# should not be iterated until the previously yielded item has been
1333
inv_w = self.get_inventory_weave()
1334
inv_w.enable_cache()
1336
# file ids that changed
1337
file_ids = self.fileids_altered_by_revision_ids(revision_ids)
1339
num_file_ids = len(file_ids)
1340
for file_id, altered_versions in file_ids.iteritems():
1341
if _files_pb is not None:
1342
_files_pb.update("fetch texts", count, num_file_ids)
1344
yield ("file", file_id, altered_versions)
1345
# We're done with the files_pb. Note that it finished by the caller,
1346
# just as it was created by the caller.
1350
yield ("inventory", None, revision_ids)
1354
revisions_with_signatures = set()
1355
for rev_id in revision_ids:
1357
self.get_signature_text(rev_id)
1358
except errors.NoSuchRevision:
1362
revisions_with_signatures.add(rev_id)
1364
yield ("signatures", None, revisions_with_signatures)
1367
yield ("revisions", None, revision_ids)
1370
def get_inventory_weave(self):
1371
return self.control_weaves.get_weave('inventory',
1372
self.get_transaction())
1375
def get_inventory(self, revision_id):
1376
"""Get Inventory object by hash."""
1377
return self.deserialise_inventory(
1378
revision_id, self.get_inventory_xml(revision_id))
1380
def deserialise_inventory(self, revision_id, xml):
1381
"""Transform the xml into an inventory object.
1383
:param revision_id: The expected revision id of the inventory.
1384
:param xml: A serialised inventory.
1386
return self._serializer.read_inventory_from_string(xml, revision_id)
1388
def serialise_inventory(self, inv):
1389
return self._serializer.write_inventory_to_string(inv)
1391
def _serialise_inventory_to_lines(self, inv):
1392
return self._serializer.write_inventory_to_lines(inv)
1394
def get_serializer_format(self):
1395
return self._serializer.format_num
1398
def get_inventory_xml(self, revision_id):
1399
"""Get inventory XML as a file object."""
1401
assert isinstance(revision_id, str), type(revision_id)
1402
iw = self.get_inventory_weave()
1403
return iw.get_text(revision_id)
1405
raise errors.HistoryMissing(self, 'inventory', revision_id)
1408
def get_inventory_sha1(self, revision_id):
1409
"""Return the sha1 hash of the inventory entry
1411
return self.get_revision(revision_id).inventory_sha1
1414
def get_revision_graph(self, revision_id=None):
1415
"""Return a dictionary containing the revision graph.
1417
NB: This method should not be used as it accesses the entire graph all
1418
at once, which is much more data than most operations should require.
1420
:param revision_id: The revision_id to get a graph from. If None, then
1421
the entire revision graph is returned. This is a deprecated mode of
1422
operation and will be removed in the future.
1423
:return: a dictionary of revision_id->revision_parents_list.
1425
raise NotImplementedError(self.get_revision_graph)
1428
def get_revision_graph_with_ghosts(self, revision_ids=None):
1429
"""Return a graph of the revisions with ghosts marked as applicable.
1431
:param revision_ids: an iterable of revisions to graph or None for all.
1432
:return: a Graph object with the graph reachable from revision_ids.
1434
if 'evil' in debug.debug_flags:
1436
"get_revision_graph_with_ghosts scales with size of history.")
1437
result = deprecated_graph.Graph()
1438
if not revision_ids:
1439
pending = set(self.all_revision_ids())
1442
pending = set(revision_ids)
1443
# special case NULL_REVISION
1444
if _mod_revision.NULL_REVISION in pending:
1445
pending.remove(_mod_revision.NULL_REVISION)
1446
required = set(pending)
1449
revision_id = pending.pop()
1451
rev = self.get_revision(revision_id)
1452
except errors.NoSuchRevision:
1453
if revision_id in required:
1456
result.add_ghost(revision_id)
1458
for parent_id in rev.parent_ids:
1459
# is this queued or done ?
1460
if (parent_id not in pending and
1461
parent_id not in done):
1463
pending.add(parent_id)
1464
result.add_node(revision_id, rev.parent_ids)
1465
done.add(revision_id)
1468
def _get_history_vf(self):
1469
"""Get a versionedfile whose history graph reflects all revisions.
1471
For weave repositories, this is the inventory weave.
1473
return self.get_inventory_weave()
1475
def iter_reverse_revision_history(self, revision_id):
1476
"""Iterate backwards through revision ids in the lefthand history
1478
:param revision_id: The revision id to start with. All its lefthand
1479
ancestors will be traversed.
1481
if revision_id in (None, _mod_revision.NULL_REVISION):
1483
next_id = revision_id
1484
versionedfile = self._get_history_vf()
1487
parents = versionedfile.get_parents(next_id)
1488
if len(parents) == 0:
1491
next_id = parents[0]
1494
def get_revision_inventory(self, revision_id):
1495
"""Return inventory of a past revision."""
1496
# TODO: Unify this with get_inventory()
1497
# bzr 0.0.6 and later imposes the constraint that the inventory_id
1498
# must be the same as its revision, so this is trivial.
1499
if revision_id is None:
1500
# This does not make sense: if there is no revision,
1501
# then it is the current tree inventory surely ?!
1502
# and thus get_root_id() is something that looks at the last
1503
# commit on the branch, and the get_root_id is an inventory check.
1504
raise NotImplementedError
1505
# return Inventory(self.get_root_id())
1507
return self.get_inventory(revision_id)
1510
def is_shared(self):
1511
"""Return True if this repository is flagged as a shared repository."""
1512
raise NotImplementedError(self.is_shared)
1515
def reconcile(self, other=None, thorough=False):
1516
"""Reconcile this repository."""
1517
from bzrlib.reconcile import RepoReconciler
1518
reconciler = RepoReconciler(self, thorough=thorough)
1519
reconciler.reconcile()
1522
def _refresh_data(self):
1523
"""Helper called from lock_* to ensure coherency with disk.
1525
The default implementation does nothing; it is however possible
1526
for repositories to maintain loaded indices across multiple locks
1527
by checking inside their implementation of this method to see
1528
whether their indices are still valid. This depends of course on
1529
the disk format being validatable in this manner.
1533
def revision_tree(self, revision_id):
1534
"""Return Tree for a revision on this branch.
1536
`revision_id` may be None for the empty tree revision.
1538
# TODO: refactor this to use an existing revision object
1539
# so we don't need to read it in twice.
1540
if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
1541
return RevisionTree(self, Inventory(root_id=None),
1542
_mod_revision.NULL_REVISION)
1544
inv = self.get_revision_inventory(revision_id)
1545
return RevisionTree(self, inv, revision_id)
1548
def revision_trees(self, revision_ids):
1549
"""Return Tree for a revision on this branch.
1551
`revision_id` may not be None or 'null:'"""
1552
assert None not in revision_ids
1553
assert _mod_revision.NULL_REVISION not in revision_ids
1554
texts = self.get_inventory_weave().get_texts(revision_ids)
1555
for text, revision_id in zip(texts, revision_ids):
1556
inv = self.deserialise_inventory(revision_id, text)
1557
yield RevisionTree(self, inv, revision_id)
1560
def get_ancestry(self, revision_id, topo_sorted=True):
1561
"""Return a list of revision-ids integrated by a revision.
1563
The first element of the list is always None, indicating the origin
1564
revision. This might change when we have history horizons, or
1565
perhaps we should have a new API.
1567
This is topologically sorted.
1569
if _mod_revision.is_null(revision_id):
1571
if not self.has_revision(revision_id):
1572
raise errors.NoSuchRevision(self, revision_id)
1573
w = self.get_inventory_weave()
1574
candidates = w.get_ancestry(revision_id, topo_sorted)
1575
return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1578
"""Compress the data within the repository.
1580
This operation only makes sense for some repository types. For other
1581
types it should be a no-op that just returns.
1583
This stub method does not require a lock, but subclasses should use
1584
@needs_write_lock as this is a long running call its reasonable to
1585
implicitly lock for the user.
1589
def print_file(self, file, revision_id):
1590
"""Print `file` to stdout.
1592
FIXME RBC 20060125 as John Meinel points out this is a bad api
1593
- it writes to stdout, it assumes that that is valid etc. Fix
1594
by creating a new more flexible convenience function.
1596
tree = self.revision_tree(revision_id)
1597
# use inventory as it was in that revision
1598
file_id = tree.inventory.path2id(file)
1600
# TODO: jam 20060427 Write a test for this code path
1601
# it had a bug in it, and was raising the wrong
1603
raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
1604
tree.print_file(file_id)
1606
def get_transaction(self):
1607
return self.control_files.get_transaction()
1609
def revision_parents(self, revision_id):
1610
return self.get_inventory_weave().parent_names(revision_id)
1612
def get_parents(self, revision_ids):
1613
"""See StackedParentsProvider.get_parents"""
1615
for revision_id in revision_ids:
1616
if revision_id == _mod_revision.NULL_REVISION:
1620
parents = self.get_revision(revision_id).parent_ids
1621
except errors.NoSuchRevision:
1624
if len(parents) == 0:
1625
parents = [_mod_revision.NULL_REVISION]
1626
parents_list.append(parents)
1629
def _make_parents_provider(self):
1632
def get_graph(self, other_repository=None):
1633
"""Return the graph walker for this repository format"""
1634
parents_provider = self._make_parents_provider()
1635
if (other_repository is not None and
1636
other_repository.bzrdir.transport.base !=
1637
self.bzrdir.transport.base):
1638
parents_provider = graph._StackedParentsProvider(
1639
[parents_provider, other_repository._make_parents_provider()])
1640
return graph.Graph(parents_provider)
1642
def get_versioned_file_checker(self, revisions, revision_versions_cache):
1643
return VersionedFileChecker(revisions, revision_versions_cache, self)
1646
def set_make_working_trees(self, new_value):
1647
"""Set the policy flag for making working trees when creating branches.
1649
This only applies to branches that use this repository.
1651
The default is 'True'.
1652
:param new_value: True to restore the default, False to disable making
1655
raise NotImplementedError(self.set_make_working_trees)
1657
def make_working_trees(self):
1658
"""Returns the policy for making working trees on new branches."""
1659
raise NotImplementedError(self.make_working_trees)
1662
def sign_revision(self, revision_id, gpg_strategy):
1663
plaintext = Testament.from_revision(self, revision_id).as_short_text()
1664
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1667
def has_signature_for_revision_id(self, revision_id):
1668
"""Query for a revision signature for revision_id in the repository."""
1669
return self._revision_store.has_signature(revision_id,
1670
self.get_transaction())
1673
def get_signature_text(self, revision_id):
1674
"""Return the text for a signature."""
1675
return self._revision_store.get_signature_text(revision_id,
1676
self.get_transaction())
1679
def check(self, revision_ids=None):
1680
"""Check consistency of all history of given revision_ids.
1682
Different repository implementations should override _check().
1684
:param revision_ids: A non-empty list of revision_ids whose ancestry
1685
will be checked. Typically the last revision_id of a branch.
1687
return self._check(revision_ids)
1689
def _check(self, revision_ids):
1690
result = check.Check(self)
1694
def _warn_if_deprecated(self):
1695
global _deprecation_warning_done
1696
if _deprecation_warning_done:
1698
_deprecation_warning_done = True
1699
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1700
% (self._format, self.bzrdir.transport.base))
1702
def supports_rich_root(self):
1703
return self._format.rich_root_data
1705
def _check_ascii_revisionid(self, revision_id, method):
1706
"""Private helper for ascii-only repositories."""
1707
# weave repositories refuse to store revisionids that are non-ascii.
1708
if revision_id is not None:
1709
# weaves require ascii revision ids.
1710
if isinstance(revision_id, unicode):
1712
revision_id.encode('ascii')
1713
except UnicodeEncodeError:
1714
raise errors.NonAsciiRevisionId(method, self)
1717
revision_id.decode('ascii')
1718
except UnicodeDecodeError:
1719
raise errors.NonAsciiRevisionId(method, self)
1721
def revision_graph_can_have_wrong_parents(self):
1722
"""Is it possible for this repository to have a revision graph with
1725
If True, then this repository must also implement
1726
_find_inconsistent_revision_parents so that check and reconcile can
1727
check for inconsistencies before proceeding with other checks that may
1728
depend on the revision index being consistent.
1730
raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1732
# remove these delegates a while after bzr 0.15
1733
def __make_delegated(name, from_module):
1734
def _deprecated_repository_forwarder():
1735
symbol_versioning.warn('%s moved to %s in bzr 0.15'
1736
% (name, from_module),
1739
m = __import__(from_module, globals(), locals(), [name])
1741
return getattr(m, name)
1742
except AttributeError:
1743
raise AttributeError('module %s has no name %s'
1745
globals()[name] = _deprecated_repository_forwarder
1748
'AllInOneRepository',
1749
'WeaveMetaDirRepository',
1750
'PreSplitOutRepositoryFormat',
1751
'RepositoryFormat4',
1752
'RepositoryFormat5',
1753
'RepositoryFormat6',
1754
'RepositoryFormat7',
1756
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1760
'RepositoryFormatKnit',
1761
'RepositoryFormatKnit1',
1763
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
1766
def install_revision(repository, rev, revision_tree):
1767
"""Install all revision data into a repository."""
1768
repository.start_write_group()
1770
_install_revision(repository, rev, revision_tree)
1772
repository.abort_write_group()
1775
repository.commit_write_group()
1778
def _install_revision(repository, rev, revision_tree):
1779
"""Install all revision data into a repository."""
1780
present_parents = []
1782
for p_id in rev.parent_ids:
1783
if repository.has_revision(p_id):
1784
present_parents.append(p_id)
1785
parent_trees[p_id] = repository.revision_tree(p_id)
1787
parent_trees[p_id] = repository.revision_tree(None)
1789
inv = revision_tree.inventory
1790
entries = inv.iter_entries()
1791
# backwards compatibility hack: skip the root id.
1792
if not repository.supports_rich_root():
1793
path, root = entries.next()
1794
if root.revision != rev.revision_id:
1795
raise errors.IncompatibleRevision(repr(repository))
1796
# Add the texts that are not already present
1797
for path, ie in entries:
1798
w = repository.weave_store.get_weave_or_empty(ie.file_id,
1799
repository.get_transaction())
1800
if ie.revision not in w:
1802
# FIXME: TODO: The following loop *may* be overlapping/duplicate
1803
# with InventoryEntry.find_previous_heads(). if it is, then there
1804
# is a latent bug here where the parents may have ancestors of each
1806
for revision, tree in parent_trees.iteritems():
1807
if ie.file_id not in tree:
1809
parent_id = tree.inventory[ie.file_id].revision
1810
if parent_id in text_parents:
1812
text_parents.append(parent_id)
1814
vfile = repository.weave_store.get_weave_or_empty(ie.file_id,
1815
repository.get_transaction())
1816
lines = revision_tree.get_file(ie.file_id).readlines()
1817
vfile.add_lines(rev.revision_id, text_parents, lines)
1819
# install the inventory
1820
repository.add_inventory(rev.revision_id, inv, present_parents)
1821
except errors.RevisionAlreadyPresent:
1823
repository.add_revision(rev.revision_id, rev, inv)
1826
class MetaDirRepository(Repository):
1827
"""Repositories in the new meta-dir layout."""
1829
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1830
super(MetaDirRepository, self).__init__(_format,
1836
dir_mode = self.control_files._dir_mode
1837
file_mode = self.control_files._file_mode
1840
def is_shared(self):
1841
"""Return True if this repository is flagged as a shared repository."""
1842
return self.control_files._transport.has('shared-storage')
1845
def set_make_working_trees(self, new_value):
1846
"""Set the policy flag for making working trees when creating branches.
1848
This only applies to branches that use this repository.
1850
The default is 'True'.
1851
:param new_value: True to restore the default, False to disable making
1856
self.control_files._transport.delete('no-working-trees')
1857
except errors.NoSuchFile:
1860
self.control_files.put_utf8('no-working-trees', '')
1862
def make_working_trees(self):
1863
"""Returns the policy for making working trees on new branches."""
1864
return not self.control_files._transport.has('no-working-trees')
1867
class RepositoryFormatRegistry(registry.Registry):
1868
"""Registry of RepositoryFormats."""
1870
def get(self, format_string):
1871
r = registry.Registry.get(self, format_string)
1877
format_registry = RepositoryFormatRegistry()
1878
"""Registry of formats, indexed by their identifying format string.
1880
This can contain either format instances themselves, or classes/factories that
1881
can be called to obtain one.
1885
#####################################################################
1886
# Repository Formats
1888
class RepositoryFormat(object):
1889
"""A repository format.
1891
Formats provide three things:
1892
* An initialization routine to construct repository data on disk.
1893
* a format string which is used when the BzrDir supports versioned
1895
* an open routine which returns a Repository instance.
1897
There is one and only one Format subclass for each on-disk format. But
1898
there can be one Repository subclass that is used for several different
1899
formats. The _format attribute on a Repository instance can be used to
1900
determine the disk format.
1902
Formats are placed in an dict by their format string for reference
1903
during opening. These should be subclasses of RepositoryFormat
1906
Once a format is deprecated, just deprecate the initialize and open
1907
methods on the format class. Do not deprecate the object, as the
1908
object will be created every system load.
1910
Common instance attributes:
1911
_matchingbzrdir - the bzrdir format that the repository format was
1912
originally written to work with. This can be used if manually
1913
constructing a bzrdir and repository, or more commonly for test suite
1917
# Set to True or False in derived classes. True indicates that the format
1918
# supports ghosts gracefully.
1919
supports_ghosts = None
1922
return "<%s>" % self.__class__.__name__
1924
def __eq__(self, other):
1925
# format objects are generally stateless
1926
return isinstance(other, self.__class__)
1928
def __ne__(self, other):
1929
return not self == other
1932
def find_format(klass, a_bzrdir):
1933
"""Return the format for the repository object in a_bzrdir.
1935
This is used by bzr native formats that have a "format" file in
1936
the repository. Other methods may be used by different types of
1940
transport = a_bzrdir.get_repository_transport(None)
1941
format_string = transport.get("format").read()
1942
return format_registry.get(format_string)
1943
except errors.NoSuchFile:
1944
raise errors.NoRepositoryPresent(a_bzrdir)
1946
raise errors.UnknownFormatError(format=format_string)
1949
def register_format(klass, format):
1950
format_registry.register(format.get_format_string(), format)
1953
def unregister_format(klass, format):
1954
format_registry.remove(format.get_format_string())
1957
def get_default_format(klass):
1958
"""Return the current default format."""
1959
from bzrlib import bzrdir
1960
return bzrdir.format_registry.make_bzrdir('default').repository_format
1962
def _get_control_store(self, repo_transport, control_files):
1963
"""Return the control store for this repository."""
1964
raise NotImplementedError(self._get_control_store)
1966
def get_format_string(self):
1967
"""Return the ASCII format string that identifies this format.
1969
Note that in pre format ?? repositories the format string is
1970
not permitted nor written to disk.
1972
raise NotImplementedError(self.get_format_string)
1974
def get_format_description(self):
1975
"""Return the short description for this format."""
1976
raise NotImplementedError(self.get_format_description)
1978
def _get_revision_store(self, repo_transport, control_files):
1979
"""Return the revision store object for this a_bzrdir."""
1980
raise NotImplementedError(self._get_revision_store)
1982
def _get_text_rev_store(self,
1989
"""Common logic for getting a revision store for a repository.
1991
see self._get_revision_store for the subclass-overridable method to
1992
get the store for a repository.
1994
from bzrlib.store.revision.text import TextRevisionStore
1995
dir_mode = control_files._dir_mode
1996
file_mode = control_files._file_mode
1997
text_store = TextStore(transport.clone(name),
1999
compressed=compressed,
2001
file_mode=file_mode)
2002
_revision_store = TextRevisionStore(text_store, serializer)
2003
return _revision_store
2005
# TODO: this shouldn't be in the base class, it's specific to things that
2006
# use weaves or knits -- mbp 20070207
2007
def _get_versioned_file_store(self,
2012
versionedfile_class=None,
2013
versionedfile_kwargs={},
2015
if versionedfile_class is None:
2016
versionedfile_class = self._versionedfile_class
2017
weave_transport = control_files._transport.clone(name)
2018
dir_mode = control_files._dir_mode
2019
file_mode = control_files._file_mode
2020
return VersionedFileStore(weave_transport, prefixed=prefixed,
2022
file_mode=file_mode,
2023
versionedfile_class=versionedfile_class,
2024
versionedfile_kwargs=versionedfile_kwargs,
2027
def initialize(self, a_bzrdir, shared=False):
2028
"""Initialize a repository of this format in a_bzrdir.
2030
:param a_bzrdir: The bzrdir to put the new repository in it.
2031
:param shared: The repository should be initialized as a sharable one.
2032
:returns: The new repository object.
2034
This may raise UninitializableFormat if shared repository are not
2035
compatible the a_bzrdir.
2037
raise NotImplementedError(self.initialize)
2039
def is_supported(self):
2040
"""Is this format supported?
2042
Supported formats must be initializable and openable.
2043
Unsupported formats may not support initialization or committing or
2044
some other features depending on the reason for not being supported.
2048
def check_conversion_target(self, target_format):
2049
raise NotImplementedError(self.check_conversion_target)
2051
def open(self, a_bzrdir, _found=False):
2052
"""Return an instance of this format for the bzrdir a_bzrdir.
2054
_found is a private parameter, do not use it.
2056
raise NotImplementedError(self.open)
2059
class MetaDirRepositoryFormat(RepositoryFormat):
2060
"""Common base class for the new repositories using the metadir layout."""
2062
rich_root_data = False
2063
supports_tree_reference = False
2064
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
2067
super(MetaDirRepositoryFormat, self).__init__()
2069
def _create_control_files(self, a_bzrdir):
2070
"""Create the required files and the initial control_files object."""
2071
# FIXME: RBC 20060125 don't peek under the covers
2072
# NB: no need to escape relative paths that are url safe.
2073
repository_transport = a_bzrdir.get_repository_transport(self)
2074
control_files = lockable_files.LockableFiles(repository_transport,
2075
'lock', lockdir.LockDir)
2076
control_files.create_lock()
2077
return control_files
2079
def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
2080
"""Upload the initial blank content."""
2081
control_files = self._create_control_files(a_bzrdir)
2082
control_files.lock_write()
2084
control_files._transport.mkdir_multi(dirs,
2085
mode=control_files._dir_mode)
2086
for file, content in files:
2087
control_files.put(file, content)
2088
for file, content in utf8_files:
2089
control_files.put_utf8(file, content)
2091
control_files.put_utf8('shared-storage', '')
2093
control_files.unlock()
2096
# formats which have no format string are not discoverable
2097
# and not independently creatable, so are not registered. They're
2098
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
2099
# needed, it's constructed directly by the BzrDir. Non-native formats where
2100
# the repository is not separately opened are similar.
2102
format_registry.register_lazy(
2103
'Bazaar-NG Repository format 7',
2104
'bzrlib.repofmt.weaverepo',
2108
# KEEP in sync with bzrdir.format_registry default, which controls the overall
2109
# default control directory format
2110
format_registry.register_lazy(
2111
'Bazaar-NG Knit Repository Format 1',
2112
'bzrlib.repofmt.knitrepo',
2113
'RepositoryFormatKnit1',
2115
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
2117
format_registry.register_lazy(
2118
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
2119
'bzrlib.repofmt.knitrepo',
2120
'RepositoryFormatKnit3',
2123
# Pack-based formats. There is one format for pre-subtrees, and one for
2124
# post-subtrees to allow ease of testing.
2125
# NOTE: These are experimental in 0.92.
2126
format_registry.register_lazy(
2127
'Bazaar pack repository format 1 (needs bzr 0.92)\n',
2128
'bzrlib.repofmt.pack_repo',
2129
'RepositoryFormatKnitPack1',
2131
format_registry.register_lazy(
2132
'Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n',
2133
'bzrlib.repofmt.pack_repo',
2134
'RepositoryFormatKnitPack3',
2138
class InterRepository(InterObject):
2139
"""This class represents operations taking place between two repositories.
2141
Its instances have methods like copy_content and fetch, and contain
2142
references to the source and target repositories these operations can be
2145
Often we will provide convenience methods on 'repository' which carry out
2146
operations with another repository - they will always forward to
2147
InterRepository.get(other).method_name(parameters).
2151
"""The available optimised InterRepository types."""
2153
def copy_content(self, revision_id=None):
2154
raise NotImplementedError(self.copy_content)
2156
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2157
"""Fetch the content required to construct revision_id.
2159
The content is copied from self.source to self.target.
2161
:param revision_id: if None all content is copied, if NULL_REVISION no
2163
:param pb: optional progress bar to use for progress reports. If not
2164
provided a default one will be created.
2166
Returns the copied revision count and the failed revisions in a tuple:
2169
raise NotImplementedError(self.fetch)
2172
def missing_revision_ids(self, revision_id=None):
2173
"""Return the revision ids that source has that target does not.
2175
These are returned in topological order.
2177
:param revision_id: only return revision ids included by this
2180
# generic, possibly worst case, slow code path.
2181
target_ids = set(self.target.all_revision_ids())
2182
if revision_id is not None:
2183
source_ids = self.source.get_ancestry(revision_id)
2184
assert source_ids[0] is None
2187
source_ids = self.source.all_revision_ids()
2188
result_set = set(source_ids).difference(target_ids)
2189
# this may look like a no-op: its not. It preserves the ordering
2190
# other_ids had while only returning the members from other_ids
2191
# that we've decided we need.
2192
return [rev_id for rev_id in source_ids if rev_id in result_set]
2195
def _same_model(source, target):
2196
"""True if source and target have the same data representation."""
2197
if source.supports_rich_root() != target.supports_rich_root():
2199
if source._serializer != target._serializer:
2204
class InterSameDataRepository(InterRepository):
2205
"""Code for converting between repositories that represent the same data.
2207
Data format and model must match for this to work.
2211
def _get_repo_format_to_test(self):
2212
"""Repository format for testing with.
2214
InterSameData can pull from subtree to subtree and from non-subtree to
2215
non-subtree, so we test this with the richest repository format.
2217
from bzrlib.repofmt import knitrepo
2218
return knitrepo.RepositoryFormatKnit3()
2221
def is_compatible(source, target):
2222
return InterRepository._same_model(source, target)
2225
def copy_content(self, revision_id=None):
2226
"""Make a complete copy of the content in self into destination.
2228
This copies both the repository's revision data, and configuration information
2229
such as the make_working_trees setting.
2231
This is a destructive operation! Do not use it on existing
2234
:param revision_id: Only copy the content needed to construct
2235
revision_id and its parents.
2238
self.target.set_make_working_trees(self.source.make_working_trees())
2239
except NotImplementedError:
2241
# but don't bother fetching if we have the needed data now.
2242
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2243
self.target.has_revision(revision_id)):
2245
self.target.fetch(self.source, revision_id=revision_id)
2248
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2249
"""See InterRepository.fetch()."""
2250
from bzrlib.fetch import GenericRepoFetcher
2251
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2252
self.source, self.source._format, self.target,
2253
self.target._format)
2254
f = GenericRepoFetcher(to_repository=self.target,
2255
from_repository=self.source,
2256
last_revision=revision_id,
2258
return f.count_copied, f.failed_revisions
2261
class InterWeaveRepo(InterSameDataRepository):
2262
"""Optimised code paths between Weave based repositories.
2264
This should be in bzrlib/repofmt/weaverepo.py but we have not yet
2265
implemented lazy inter-object optimisation.
2269
def _get_repo_format_to_test(self):
2270
from bzrlib.repofmt import weaverepo
2271
return weaverepo.RepositoryFormat7()
2274
def is_compatible(source, target):
2275
"""Be compatible with known Weave formats.
2277
We don't test for the stores being of specific types because that
2278
could lead to confusing results, and there is no need to be
2281
from bzrlib.repofmt.weaverepo import (
2287
return (isinstance(source._format, (RepositoryFormat5,
2289
RepositoryFormat7)) and
2290
isinstance(target._format, (RepositoryFormat5,
2292
RepositoryFormat7)))
2293
except AttributeError:
2297
def copy_content(self, revision_id=None):
2298
"""See InterRepository.copy_content()."""
2299
# weave specific optimised path:
2301
self.target.set_make_working_trees(self.source.make_working_trees())
2302
except NotImplementedError:
2304
# FIXME do not peek!
2305
if self.source.control_files._transport.listable():
2306
pb = ui.ui_factory.nested_progress_bar()
2308
self.target.weave_store.copy_all_ids(
2309
self.source.weave_store,
2311
from_transaction=self.source.get_transaction(),
2312
to_transaction=self.target.get_transaction())
2313
pb.update('copying inventory', 0, 1)
2314
self.target.control_weaves.copy_multi(
2315
self.source.control_weaves, ['inventory'],
2316
from_transaction=self.source.get_transaction(),
2317
to_transaction=self.target.get_transaction())
2318
self.target._revision_store.text_store.copy_all_ids(
2319
self.source._revision_store.text_store,
2324
self.target.fetch(self.source, revision_id=revision_id)
2327
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2328
"""See InterRepository.fetch()."""
2329
from bzrlib.fetch import GenericRepoFetcher
2330
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2331
self.source, self.source._format, self.target, self.target._format)
2332
f = GenericRepoFetcher(to_repository=self.target,
2333
from_repository=self.source,
2334
last_revision=revision_id,
2336
return f.count_copied, f.failed_revisions
2339
def missing_revision_ids(self, revision_id=None):
2340
"""See InterRepository.missing_revision_ids()."""
2341
# we want all revisions to satisfy revision_id in source.
2342
# but we don't want to stat every file here and there.
2343
# we want then, all revisions other needs to satisfy revision_id
2344
# checked, but not those that we have locally.
2345
# so the first thing is to get a subset of the revisions to
2346
# satisfy revision_id in source, and then eliminate those that
2347
# we do already have.
2348
# this is slow on high latency connection to self, but as as this
2349
# disk format scales terribly for push anyway due to rewriting
2350
# inventory.weave, this is considered acceptable.
2352
if revision_id is not None:
2353
source_ids = self.source.get_ancestry(revision_id)
2354
assert source_ids[0] is None
2357
source_ids = self.source._all_possible_ids()
2358
source_ids_set = set(source_ids)
2359
# source_ids is the worst possible case we may need to pull.
2360
# now we want to filter source_ids against what we actually
2361
# have in target, but don't try to check for existence where we know
2362
# we do not have a revision as that would be pointless.
2363
target_ids = set(self.target._all_possible_ids())
2364
possibly_present_revisions = target_ids.intersection(source_ids_set)
2365
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2366
required_revisions = source_ids_set.difference(actually_present_revisions)
2367
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2368
if revision_id is not None:
2369
# we used get_ancestry to determine source_ids then we are assured all
2370
# revisions referenced are present as they are installed in topological order.
2371
# and the tip revision was validated by get_ancestry.
2372
return required_topo_revisions
2374
# if we just grabbed the possibly available ids, then
2375
# we only have an estimate of whats available and need to validate
2376
# that against the revision records.
2377
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2380
class InterKnitRepo(InterSameDataRepository):
2381
"""Optimised code paths between Knit based repositories."""
2384
def _get_repo_format_to_test(self):
2385
from bzrlib.repofmt import knitrepo
2386
return knitrepo.RepositoryFormatKnit1()
2389
def is_compatible(source, target):
2390
"""Be compatible with known Knit formats.
2392
We don't test for the stores being of specific types because that
2393
could lead to confusing results, and there is no need to be
2396
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
2398
are_knits = (isinstance(source._format, RepositoryFormatKnit) and
2399
isinstance(target._format, RepositoryFormatKnit))
2400
except AttributeError:
2402
return are_knits and InterRepository._same_model(source, target)
2405
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2406
"""See InterRepository.fetch()."""
2407
from bzrlib.fetch import KnitRepoFetcher
2408
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2409
self.source, self.source._format, self.target, self.target._format)
2410
f = KnitRepoFetcher(to_repository=self.target,
2411
from_repository=self.source,
2412
last_revision=revision_id,
2414
return f.count_copied, f.failed_revisions
2417
def missing_revision_ids(self, revision_id=None):
2418
"""See InterRepository.missing_revision_ids()."""
2419
if revision_id is not None:
2420
source_ids = self.source.get_ancestry(revision_id)
2421
assert source_ids[0] is None
2424
source_ids = self.source.all_revision_ids()
2425
source_ids_set = set(source_ids)
2426
# source_ids is the worst possible case we may need to pull.
2427
# now we want to filter source_ids against what we actually
2428
# have in target, but don't try to check for existence where we know
2429
# we do not have a revision as that would be pointless.
2430
target_ids = set(self.target.all_revision_ids())
2431
possibly_present_revisions = target_ids.intersection(source_ids_set)
2432
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2433
required_revisions = source_ids_set.difference(actually_present_revisions)
2434
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2435
if revision_id is not None:
2436
# we used get_ancestry to determine source_ids then we are assured all
2437
# revisions referenced are present as they are installed in topological order.
2438
# and the tip revision was validated by get_ancestry.
2439
return required_topo_revisions
2441
# if we just grabbed the possibly available ids, then
2442
# we only have an estimate of whats available and need to validate
2443
# that against the revision records.
2444
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2447
class InterPackRepo(InterSameDataRepository):
2448
"""Optimised code paths between Pack based repositories."""
2451
def _get_repo_format_to_test(self):
2452
from bzrlib.repofmt import pack_repo
2453
return pack_repo.RepositoryFormatKnitPack1()
2456
def is_compatible(source, target):
2457
"""Be compatible with known Pack formats.
2459
We don't test for the stores being of specific types because that
2460
could lead to confusing results, and there is no need to be
2463
from bzrlib.repofmt.pack_repo import RepositoryFormatPack
2465
are_packs = (isinstance(source._format, RepositoryFormatPack) and
2466
isinstance(target._format, RepositoryFormatPack))
2467
except AttributeError:
2469
return are_packs and InterRepository._same_model(source, target)
2472
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2473
"""See InterRepository.fetch()."""
2474
from bzrlib.repofmt.pack_repo import Packer
2475
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2476
self.source, self.source._format, self.target, self.target._format)
2477
self.count_copied = 0
2478
if revision_id is None:
2480
# everything to do - use pack logic
2481
# to fetch from all packs to one without
2482
# inventory parsing etc, IFF nothing to be copied is in the target.
2484
revision_ids = self.source.all_revision_ids()
2485
# implementing the TODO will involve:
2486
# - detecting when all of a pack is selected
2487
# - avoiding as much as possible pre-selection, so the
2488
# more-core routines such as create_pack_from_packs can filter in
2489
# a just-in-time fashion. (though having a HEADS list on a
2490
# repository might make this a lot easier, because we could
2491
# sensibly detect 'new revisions' without doing a full index scan.
2492
elif _mod_revision.is_null(revision_id):
2497
revision_ids = self.missing_revision_ids(revision_id,
2498
find_ghosts=find_ghosts)
2499
except errors.NoSuchRevision:
2500
raise errors.InstallFailed([revision_id])
2501
packs = self.source._pack_collection.all_packs()
2502
pack = Packer(self.target._pack_collection, packs, '.fetch',
2503
revision_ids).pack()
2504
if pack is not None:
2505
self.target._pack_collection._save_pack_names()
2506
# Trigger an autopack. This may duplicate effort as we've just done
2507
# a pack creation, but for now it is simpler to think about as
2508
# 'upload data, then repack if needed'.
2509
self.target._pack_collection.autopack()
2510
return pack.get_revision_count()
2515
def missing_revision_ids(self, revision_id=None, find_ghosts=True):
2516
"""See InterRepository.missing_revision_ids().
2518
:param find_ghosts: Find ghosts throughough the ancestry of
2521
if not find_ghosts and revision_id is not None:
2522
graph = self.source.get_graph()
2523
missing_revs = set()
2524
searcher = graph._make_breadth_first_searcher([revision_id])
2526
self.target._pack_collection.revision_index.combined_index
2527
null_set = frozenset([_mod_revision.NULL_REVISION])
2530
next_revs = set(searcher.next())
2531
except StopIteration:
2533
next_revs.difference_update(null_set)
2534
target_keys = [(key,) for key in next_revs]
2535
have_revs = frozenset(node[1][0] for node in
2536
target_index.iter_entries(target_keys))
2537
missing_revs.update(next_revs - have_revs)
2538
searcher.stop_searching_any(have_revs)
2540
elif revision_id is not None:
2541
source_ids = self.source.get_ancestry(revision_id)
2542
assert source_ids[0] is None
2545
source_ids = self.source.all_revision_ids()
2546
# source_ids is the worst possible case we may need to pull.
2547
# now we want to filter source_ids against what we actually
2548
# have in target, but don't try to check for existence where we know
2549
# we do not have a revision as that would be pointless.
2550
target_ids = set(self.target.all_revision_ids())
2551
return [r for r in source_ids if (r not in target_ids)]
2554
class InterModel1and2(InterRepository):
2557
def _get_repo_format_to_test(self):
2561
def is_compatible(source, target):
2562
if not source.supports_rich_root() and target.supports_rich_root():
2568
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2569
"""See InterRepository.fetch()."""
2570
from bzrlib.fetch import Model1toKnit2Fetcher
2571
f = Model1toKnit2Fetcher(to_repository=self.target,
2572
from_repository=self.source,
2573
last_revision=revision_id,
2575
return f.count_copied, f.failed_revisions
2578
def copy_content(self, revision_id=None):
2579
"""Make a complete copy of the content in self into destination.
2581
This is a destructive operation! Do not use it on existing
2584
:param revision_id: Only copy the content needed to construct
2585
revision_id and its parents.
2588
self.target.set_make_working_trees(self.source.make_working_trees())
2589
except NotImplementedError:
2591
# but don't bother fetching if we have the needed data now.
2592
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2593
self.target.has_revision(revision_id)):
2595
self.target.fetch(self.source, revision_id=revision_id)
2598
class InterKnit1and2(InterKnitRepo):
2601
def _get_repo_format_to_test(self):
2605
def is_compatible(source, target):
2606
"""Be compatible with Knit1 source and Knit3 target"""
2607
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
2609
from bzrlib.repofmt.knitrepo import (RepositoryFormatKnit1,
2610
RepositoryFormatKnit3)
2611
from bzrlib.repofmt.pack_repo import (RepositoryFormatKnitPack1,
2612
RepositoryFormatKnitPack3)
2613
return (isinstance(source._format,
2614
(RepositoryFormatKnit1, RepositoryFormatKnitPack1)) and
2615
isinstance(target._format,
2616
(RepositoryFormatKnit3, RepositoryFormatKnitPack3))
2618
except AttributeError:
2622
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2623
"""See InterRepository.fetch()."""
2624
from bzrlib.fetch import Knit1to2Fetcher
2625
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2626
self.source, self.source._format, self.target,
2627
self.target._format)
2628
f = Knit1to2Fetcher(to_repository=self.target,
2629
from_repository=self.source,
2630
last_revision=revision_id,
2632
return f.count_copied, f.failed_revisions
2635
class InterRemoteToOther(InterRepository):
2637
def __init__(self, source, target):
2638
InterRepository.__init__(self, source, target)
2639
self._real_inter = None
2642
def is_compatible(source, target):
2643
if not isinstance(source, remote.RemoteRepository):
2645
source._ensure_real()
2646
real_source = source._real_repository
2647
# Is source's model compatible with target's model, and are they the
2648
# same format? Currently we can only optimise fetching from an
2649
# identical model & format repo.
2650
assert not isinstance(real_source, remote.RemoteRepository), (
2651
"We don't support remote repos backed by remote repos yet.")
2652
return real_source._format == target._format
2655
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2656
"""See InterRepository.fetch()."""
2657
from bzrlib.fetch import RemoteToOtherFetcher
2658
mutter("Using fetch logic to copy between %s(remote) and %s(%s)",
2659
self.source, self.target, self.target._format)
2660
# TODO: jam 20070210 This should be an assert, not a translate
2661
revision_id = osutils.safe_revision_id(revision_id)
2662
f = RemoteToOtherFetcher(to_repository=self.target,
2663
from_repository=self.source,
2664
last_revision=revision_id,
2666
return f.count_copied, f.failed_revisions
2669
def _get_repo_format_to_test(self):
2673
class InterOtherToRemote(InterRepository):
2675
def __init__(self, source, target):
2676
InterRepository.__init__(self, source, target)
2677
self._real_inter = None
2680
def is_compatible(source, target):
2681
if isinstance(target, remote.RemoteRepository):
2685
def _ensure_real_inter(self):
2686
if self._real_inter is None:
2687
self.target._ensure_real()
2688
real_target = self.target._real_repository
2689
self._real_inter = InterRepository.get(self.source, real_target)
2691
def copy_content(self, revision_id=None):
2692
self._ensure_real_inter()
2693
self._real_inter.copy_content(revision_id=revision_id)
2695
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2696
self._ensure_real_inter()
2697
self._real_inter.fetch(revision_id=revision_id, pb=pb)
2700
def _get_repo_format_to_test(self):
2704
InterRepository.register_optimiser(InterSameDataRepository)
2705
InterRepository.register_optimiser(InterWeaveRepo)
2706
InterRepository.register_optimiser(InterKnitRepo)
2707
InterRepository.register_optimiser(InterModel1and2)
2708
InterRepository.register_optimiser(InterKnit1and2)
2709
InterRepository.register_optimiser(InterPackRepo)
2710
InterRepository.register_optimiser(InterRemoteToOther)
2711
InterRepository.register_optimiser(InterOtherToRemote)
2714
class CopyConverter(object):
2715
"""A repository conversion tool which just performs a copy of the content.
2717
This is slow but quite reliable.
2720
def __init__(self, target_format):
2721
"""Create a CopyConverter.
2723
:param target_format: The format the resulting repository should be.
2725
self.target_format = target_format
2727
def convert(self, repo, pb):
2728
"""Perform the conversion of to_convert, giving feedback via pb.
2730
:param to_convert: The disk object to convert.
2731
:param pb: a progress bar to use for progress information.
2736
# this is only useful with metadir layouts - separated repo content.
2737
# trigger an assertion if not such
2738
repo._format.get_format_string()
2739
self.repo_dir = repo.bzrdir
2740
self.step('Moving repository to repository.backup')
2741
self.repo_dir.transport.move('repository', 'repository.backup')
2742
backup_transport = self.repo_dir.transport.clone('repository.backup')
2743
repo._format.check_conversion_target(self.target_format)
2744
self.source_repo = repo._format.open(self.repo_dir,
2746
_override_transport=backup_transport)
2747
self.step('Creating new repository')
2748
converted = self.target_format.initialize(self.repo_dir,
2749
self.source_repo.is_shared())
2750
converted.lock_write()
2752
self.step('Copying content into repository.')
2753
self.source_repo.copy_content_into(converted)
2756
self.step('Deleting old repository content.')
2757
self.repo_dir.transport.delete_tree('repository.backup')
2758
self.pb.note('repository converted')
2760
def step(self, message):
2761
"""Update the pb by a step."""
2763
self.pb.update(message, self.count, self.total)
2775
def _unescaper(match, _map=_unescape_map):
2776
code = match.group(1)
2780
if not code.startswith('#'):
2782
return unichr(int(code[1:])).encode('utf8')
2788
def _unescape_xml(data):
2789
"""Unescape predefined XML entities in a string of data."""
2791
if _unescape_re is None:
2792
_unescape_re = re.compile('\&([^;]*);')
2793
return _unescape_re.sub(_unescaper, data)
2796
class _RevisionTextVersionCache(object):
2797
"""A cache of the versionedfile versions for revision and file-id."""
2799
def __init__(self, repository):
2800
self.repository = repository
2801
self.revision_versions = {}
2802
self.revision_parents = {}
2803
self.repo_graph = self.repository.get_graph()
2804
# XXX: RBC: I haven't tracked down what uses this, but it would be
2805
# better to use the headscache directly I think.
2806
self.heads = graph.HeadsCache(self.repo_graph).heads
2808
def add_revision_text_versions(self, tree):
2809
"""Cache text version data from the supplied revision tree"""
2811
for path, entry in tree.iter_entries_by_dir():
2812
inv_revisions[entry.file_id] = entry.revision
2813
self.revision_versions[tree.get_revision_id()] = inv_revisions
2814
return inv_revisions
2816
def get_text_version(self, file_id, revision_id):
2817
"""Determine the text version for a given file-id and revision-id"""
2819
inv_revisions = self.revision_versions[revision_id]
2822
tree = self.repository.revision_tree(revision_id)
2823
except errors.RevisionNotPresent:
2824
self.revision_versions[revision_id] = inv_revisions = {}
2826
inv_revisions = self.add_revision_text_versions(tree)
2827
return inv_revisions.get(file_id)
2829
def prepopulate_revs(self, revision_ids):
2830
# Filter out versions that we don't have an inventory for, so that the
2831
# revision_trees() call won't fail.
2832
inv_weave = self.repository.get_inventory_weave()
2833
revs = [r for r in revision_ids if inv_weave.has_version(r)]
2834
# XXX: this loop is very similar to
2835
# bzrlib.fetch.Inter1and2Helper.iter_rev_trees.
2837
mutter('%d revisions left to prepopulate', len(revs))
2838
for tree in self.repository.revision_trees(revs[:100]):
2839
if tree.inventory.revision_id is None:
2840
tree.inventory.revision_id = tree.get_revision_id()
2841
self.add_revision_text_versions(tree)
2844
def get_parents(self, revision_id):
2846
return self.revision_parents[revision_id]
2848
parents = self.repository.get_parents([revision_id])[0]
2849
self.revision_parents[revision_id] = parents
2852
def used_file_versions(self):
2853
"""Return a set of (revision_id, file_id) pairs for each file version
2854
referenced by any inventory cached by this _RevisionTextVersionCache.
2856
If the entire repository has been cached, this can be used to find all
2857
file versions that are actually referenced by inventories. Thus any
2858
other file version is completely unused and can be removed safely.
2861
for inventory_summary in self.revision_versions.itervalues():
2862
result.update(inventory_summary.items())
2866
class VersionedFileChecker(object):
2868
def __init__(self, planned_revisions, revision_versions, repository):
2869
self.planned_revisions = planned_revisions
2870
self.revision_versions = revision_versions
2871
self.repository = repository
2873
def calculate_file_version_parents(self, revision_id, file_id):
2874
"""Calculate the correct parents for a file version according to
2877
text_revision = self.revision_versions.get_text_version(
2878
file_id, revision_id)
2879
if text_revision is None:
2881
parents_of_text_revision = self.revision_versions.get_parents(
2883
parents_from_inventories = []
2884
for parent in parents_of_text_revision:
2885
if parent == _mod_revision.NULL_REVISION:
2887
introduced_in = self.revision_versions.get_text_version(file_id,
2889
if introduced_in is not None:
2890
parents_from_inventories.append(introduced_in)
2891
heads = set(self.revision_versions.heads(parents_from_inventories))
2893
for parent in parents_from_inventories:
2894
if parent in heads and parent not in new_parents:
2895
new_parents.append(parent)
2896
return tuple(new_parents)
2898
def check_file_version_parents(self, weave, file_id):
2899
"""Check the parents stored in a versioned file are correct.
2901
It also detects file versions that are not referenced by their
2902
corresponding revision's inventory.
2904
:returns: A tuple of (wrong_parents, dangling_file_versions).
2905
wrong_parents is a dict mapping {revision_id: (stored_parents,
2906
correct_parents)} for each revision_id where the stored parents
2907
are not correct. dangling_file_versions is a set of (file_id,
2908
revision_id) tuples for versions that are present in this versioned
2909
file, but not used by the corresponding inventory.
2912
dangling_file_versions = set()
2913
for num, revision_id in enumerate(self.planned_revisions):
2914
correct_parents = self.calculate_file_version_parents(
2915
revision_id, file_id)
2916
if correct_parents is None:
2918
text_revision = self.revision_versions.get_text_version(
2919
file_id, revision_id)
2921
knit_parents = tuple(weave.get_parents(revision_id))
2922
except errors.RevisionNotPresent:
2924
if text_revision != revision_id:
2925
# This file version is not referenced by its corresponding
2927
dangling_file_versions.add((file_id, revision_id))
2928
if correct_parents != knit_parents:
2929
wrong_parents[revision_id] = (knit_parents, correct_parents)
2930
return wrong_parents, dangling_file_versions