1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from bzrlib.lazy_import import lazy_import
18
lazy_import(globals(), """
37
revision as _mod_revision,
43
from bzrlib.bundle import serializer
44
from bzrlib.revisiontree import RevisionTree
45
from bzrlib.store.versioned import VersionedFileStore
46
from bzrlib.testament import Testament
49
from bzrlib.decorators import needs_read_lock, needs_write_lock
50
from bzrlib.inter import InterObject
51
from bzrlib.inventory import (
57
from bzrlib import registry
58
from bzrlib.symbol_versioning import (
61
from bzrlib.trace import (
62
log_exception_quietly, note, mutter, mutter_callsite, warning)
65
# Old formats display a warning, but only once
66
_deprecation_warning_done = False
69
class CommitBuilder(object):
70
"""Provides an interface to build up a commit.
72
This allows describing a tree to be committed without needing to
73
know the internals of the format of the repository.
76
# all clients should supply tree roots.
77
record_root_entry = True
78
# the default CommitBuilder does not manage trees whose root is versioned.
79
_versioned_root = False
81
def __init__(self, repository, parents, config, timestamp=None,
82
timezone=None, committer=None, revprops=None,
84
"""Initiate a CommitBuilder.
86
:param repository: Repository to commit to.
87
:param parents: Revision ids of the parents of the new revision.
88
:param config: Configuration to use.
89
:param timestamp: Optional timestamp recorded for commit.
90
:param timezone: Optional timezone for timestamp.
91
:param committer: Optional committer to set for commit.
92
:param revprops: Optional dictionary of revision properties.
93
:param revision_id: Optional revision id.
98
self._committer = self._config.username()
100
self._committer = committer
102
self.new_inventory = Inventory(None)
103
self._new_revision_id = revision_id
104
self.parents = parents
105
self.repository = repository
108
if revprops is not None:
109
self._validate_revprops(revprops)
110
self._revprops.update(revprops)
112
if timestamp is None:
113
timestamp = time.time()
114
# Restrict resolution to 1ms
115
self._timestamp = round(timestamp, 3)
118
self._timezone = osutils.local_time_offset()
120
self._timezone = int(timezone)
122
self._generate_revision_if_needed()
123
self.__heads = graph.HeadsCache(repository.get_graph()).heads
124
self._basis_delta = []
125
# API compatibility, older code that used CommitBuilder did not call
126
# .record_delete(), which means the delta that is computed would not be
127
# valid. Callers that will call record_delete() should call
128
# .will_record_deletes() to indicate that.
129
self._recording_deletes = False
130
# memo'd check for no-op commits.
131
self._any_changes = False
133
def any_changes(self):
134
"""Return True if any entries were changed.
136
This includes merge-only changes. It is the core for the --unchanged
139
:return: True if any changes have occured.
141
return self._any_changes
143
def _validate_unicode_text(self, text, context):
144
"""Verify things like commit messages don't have bogus characters."""
146
raise ValueError('Invalid value for %s: %r' % (context, text))
148
def _validate_revprops(self, revprops):
149
for key, value in revprops.iteritems():
150
# We know that the XML serializers do not round trip '\r'
151
# correctly, so refuse to accept them
152
if not isinstance(value, basestring):
153
raise ValueError('revision property (%s) is not a valid'
154
' (unicode) string: %r' % (key, value))
155
self._validate_unicode_text(value,
156
'revision property (%s)' % (key,))
158
def commit(self, message):
159
"""Make the actual commit.
161
:return: The revision id of the recorded revision.
163
self._validate_unicode_text(message, 'commit message')
164
rev = _mod_revision.Revision(
165
timestamp=self._timestamp,
166
timezone=self._timezone,
167
committer=self._committer,
169
inventory_sha1=self.inv_sha1,
170
revision_id=self._new_revision_id,
171
properties=self._revprops)
172
rev.parent_ids = self.parents
173
self.repository.add_revision(self._new_revision_id, rev,
174
self.new_inventory, self._config)
175
self.repository.commit_write_group()
176
return self._new_revision_id
179
"""Abort the commit that is being built.
181
self.repository.abort_write_group()
183
def revision_tree(self):
184
"""Return the tree that was just committed.
186
After calling commit() this can be called to get a RevisionTree
187
representing the newly committed tree. This is preferred to
188
calling Repository.revision_tree() because that may require
189
deserializing the inventory, while we already have a copy in
192
if self.new_inventory is None:
193
self.new_inventory = self.repository.get_inventory(
194
self._new_revision_id)
195
return RevisionTree(self.repository, self.new_inventory,
196
self._new_revision_id)
198
def finish_inventory(self):
199
"""Tell the builder that the inventory is finished.
201
:return: The inventory id in the repository, which can be used with
202
repository.get_inventory.
204
if self.new_inventory is None:
205
# an inventory delta was accumulated without creating a new
207
basis_id = self.basis_delta_revision
208
self.inv_sha1 = self.repository.add_inventory_by_delta(
209
basis_id, self._basis_delta, self._new_revision_id,
212
if self.new_inventory.root is None:
213
raise AssertionError('Root entry should be supplied to'
214
' record_entry_contents, as of bzr 0.10.')
215
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
216
self.new_inventory.revision_id = self._new_revision_id
217
self.inv_sha1 = self.repository.add_inventory(
218
self._new_revision_id,
222
return self._new_revision_id
224
def _gen_revision_id(self):
225
"""Return new revision-id."""
226
return generate_ids.gen_revision_id(self._config.username(),
229
def _generate_revision_if_needed(self):
230
"""Create a revision id if None was supplied.
232
If the repository can not support user-specified revision ids
233
they should override this function and raise CannotSetRevisionId
234
if _new_revision_id is not None.
236
:raises: CannotSetRevisionId
238
if self._new_revision_id is None:
239
self._new_revision_id = self._gen_revision_id()
240
self.random_revid = True
242
self.random_revid = False
244
def _heads(self, file_id, revision_ids):
245
"""Calculate the graph heads for revision_ids in the graph of file_id.
247
This can use either a per-file graph or a global revision graph as we
248
have an identity relationship between the two graphs.
250
return self.__heads(revision_ids)
252
def _check_root(self, ie, parent_invs, tree):
253
"""Helper for record_entry_contents.
255
:param ie: An entry being added.
256
:param parent_invs: The inventories of the parent revisions of the
258
:param tree: The tree that is being committed.
260
# In this revision format, root entries have no knit or weave When
261
# serializing out to disk and back in root.revision is always
263
ie.revision = self._new_revision_id
265
def _require_root_change(self, tree):
266
"""Enforce an appropriate root object change.
268
This is called once when record_iter_changes is called, if and only if
269
the root was not in the delta calculated by record_iter_changes.
271
:param tree: The tree which is being committed.
273
# NB: if there are no parents then this method is not called, so no
274
# need to guard on parents having length.
275
entry = entry_factory['directory'](tree.path2id(''), '',
277
entry.revision = self._new_revision_id
278
self._basis_delta.append(('', '', entry.file_id, entry))
280
def _get_delta(self, ie, basis_inv, path):
281
"""Get a delta against the basis inventory for ie."""
282
if ie.file_id not in basis_inv:
284
result = (None, path, ie.file_id, ie)
285
self._basis_delta.append(result)
287
elif ie != basis_inv[ie.file_id]:
289
# TODO: avoid tis id2path call.
290
result = (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
291
self._basis_delta.append(result)
297
def get_basis_delta(self):
298
"""Return the complete inventory delta versus the basis inventory.
300
This has been built up with the calls to record_delete and
301
record_entry_contents. The client must have already called
302
will_record_deletes() to indicate that they will be generating a
305
:return: An inventory delta, suitable for use with apply_delta, or
306
Repository.add_inventory_by_delta, etc.
308
if not self._recording_deletes:
309
raise AssertionError("recording deletes not activated.")
310
return self._basis_delta
312
def record_delete(self, path, file_id):
313
"""Record that a delete occured against a basis tree.
315
This is an optional API - when used it adds items to the basis_delta
316
being accumulated by the commit builder. It cannot be called unless the
317
method will_record_deletes() has been called to inform the builder that
318
a delta is being supplied.
320
:param path: The path of the thing deleted.
321
:param file_id: The file id that was deleted.
323
if not self._recording_deletes:
324
raise AssertionError("recording deletes not activated.")
325
delta = (path, None, file_id, None)
326
self._basis_delta.append(delta)
327
self._any_changes = True
330
def will_record_deletes(self):
331
"""Tell the commit builder that deletes are being notified.
333
This enables the accumulation of an inventory delta; for the resulting
334
commit to be valid, deletes against the basis MUST be recorded via
335
builder.record_delete().
337
self._recording_deletes = True
339
basis_id = self.parents[0]
341
basis_id = _mod_revision.NULL_REVISION
342
self.basis_delta_revision = basis_id
344
def record_entry_contents(self, ie, parent_invs, path, tree,
346
"""Record the content of ie from tree into the commit if needed.
348
Side effect: sets ie.revision when unchanged
350
:param ie: An inventory entry present in the commit.
351
:param parent_invs: The inventories of the parent revisions of the
353
:param path: The path the entry is at in the tree.
354
:param tree: The tree which contains this entry and should be used to
356
:param content_summary: Summary data from the tree about the paths
357
content - stat, length, exec, sha/link target. This is only
358
accessed when the entry has a revision of None - that is when it is
359
a candidate to commit.
360
:return: A tuple (change_delta, version_recorded, fs_hash).
361
change_delta is an inventory_delta change for this entry against
362
the basis tree of the commit, or None if no change occured against
364
version_recorded is True if a new version of the entry has been
365
recorded. For instance, committing a merge where a file was only
366
changed on the other side will return (delta, False).
367
fs_hash is either None, or the hash details for the path (currently
368
a tuple of the contents sha1 and the statvalue returned by
369
tree.get_file_with_stat()).
371
if self.new_inventory.root is None:
372
if ie.parent_id is not None:
373
raise errors.RootMissing()
374
self._check_root(ie, parent_invs, tree)
375
if ie.revision is None:
376
kind = content_summary[0]
378
# ie is carried over from a prior commit
380
# XXX: repository specific check for nested tree support goes here - if
381
# the repo doesn't want nested trees we skip it ?
382
if (kind == 'tree-reference' and
383
not self.repository._format.supports_tree_reference):
384
# mismatch between commit builder logic and repository:
385
# this needs the entry creation pushed down into the builder.
386
raise NotImplementedError('Missing repository subtree support.')
387
self.new_inventory.add(ie)
389
# TODO: slow, take it out of the inner loop.
391
basis_inv = parent_invs[0]
393
basis_inv = Inventory(root_id=None)
395
# ie.revision is always None if the InventoryEntry is considered
396
# for committing. We may record the previous parents revision if the
397
# content is actually unchanged against a sole head.
398
if ie.revision is not None:
399
if not self._versioned_root and path == '':
400
# repositories that do not version the root set the root's
401
# revision to the new commit even when no change occurs (more
402
# specifically, they do not record a revision on the root; and
403
# the rev id is assigned to the root during deserialisation -
404
# this masks when a change may have occurred against the basis.
405
# To match this we always issue a delta, because the revision
406
# of the root will always be changing.
407
if ie.file_id in basis_inv:
408
delta = (basis_inv.id2path(ie.file_id), path,
412
delta = (None, path, ie.file_id, ie)
413
self._basis_delta.append(delta)
414
return delta, False, None
416
# we don't need to commit this, because the caller already
417
# determined that an existing revision of this file is
418
# appropriate. If its not being considered for committing then
419
# it and all its parents to the root must be unaltered so
420
# no-change against the basis.
421
if ie.revision == self._new_revision_id:
422
raise AssertionError("Impossible situation, a skipped "
423
"inventory entry (%r) claims to be modified in this "
424
"commit (%r).", (ie, self._new_revision_id))
425
return None, False, None
426
# XXX: Friction: parent_candidates should return a list not a dict
427
# so that we don't have to walk the inventories again.
428
parent_candiate_entries = ie.parent_candidates(parent_invs)
429
head_set = self._heads(ie.file_id, parent_candiate_entries.keys())
431
for inv in parent_invs:
432
if ie.file_id in inv:
433
old_rev = inv[ie.file_id].revision
434
if old_rev in head_set:
435
heads.append(inv[ie.file_id].revision)
436
head_set.remove(inv[ie.file_id].revision)
439
# now we check to see if we need to write a new record to the
441
# We write a new entry unless there is one head to the ancestors, and
442
# the kind-derived content is unchanged.
444
# Cheapest check first: no ancestors, or more the one head in the
445
# ancestors, we write a new node.
449
# There is a single head, look it up for comparison
450
parent_entry = parent_candiate_entries[heads[0]]
451
# if the non-content specific data has changed, we'll be writing a
453
if (parent_entry.parent_id != ie.parent_id or
454
parent_entry.name != ie.name):
456
# now we need to do content specific checks:
458
# if the kind changed the content obviously has
459
if kind != parent_entry.kind:
461
# Stat cache fingerprint feedback for the caller - None as we usually
462
# don't generate one.
465
if content_summary[2] is None:
466
raise ValueError("Files must not have executable = None")
468
if (# if the file length changed we have to store:
469
parent_entry.text_size != content_summary[1] or
470
# if the exec bit has changed we have to store:
471
parent_entry.executable != content_summary[2]):
473
elif parent_entry.text_sha1 == content_summary[3]:
474
# all meta and content is unchanged (using a hash cache
475
# hit to check the sha)
476
ie.revision = parent_entry.revision
477
ie.text_size = parent_entry.text_size
478
ie.text_sha1 = parent_entry.text_sha1
479
ie.executable = parent_entry.executable
480
return self._get_delta(ie, basis_inv, path), False, None
482
# Either there is only a hash change(no hash cache entry,
483
# or same size content change), or there is no change on
485
# Provide the parent's hash to the store layer, so that the
486
# content is unchanged we will not store a new node.
487
nostore_sha = parent_entry.text_sha1
489
# We want to record a new node regardless of the presence or
490
# absence of a content change in the file.
492
ie.executable = content_summary[2]
493
file_obj, stat_value = tree.get_file_with_stat(ie.file_id, path)
495
lines = file_obj.readlines()
499
ie.text_sha1, ie.text_size = self._add_text_to_weave(
500
ie.file_id, lines, heads, nostore_sha)
501
# Let the caller know we generated a stat fingerprint.
502
fingerprint = (ie.text_sha1, stat_value)
503
except errors.ExistingContent:
504
# Turns out that the file content was unchanged, and we were
505
# only going to store a new node if it was changed. Carry over
507
ie.revision = parent_entry.revision
508
ie.text_size = parent_entry.text_size
509
ie.text_sha1 = parent_entry.text_sha1
510
ie.executable = parent_entry.executable
511
return self._get_delta(ie, basis_inv, path), False, None
512
elif kind == 'directory':
514
# all data is meta here, nothing specific to directory, so
516
ie.revision = parent_entry.revision
517
return self._get_delta(ie, basis_inv, path), False, None
519
self._add_text_to_weave(ie.file_id, lines, heads, None)
520
elif kind == 'symlink':
521
current_link_target = content_summary[3]
523
# symlink target is not generic metadata, check if it has
525
if current_link_target != parent_entry.symlink_target:
528
# unchanged, carry over.
529
ie.revision = parent_entry.revision
530
ie.symlink_target = parent_entry.symlink_target
531
return self._get_delta(ie, basis_inv, path), False, None
532
ie.symlink_target = current_link_target
534
self._add_text_to_weave(ie.file_id, lines, heads, None)
535
elif kind == 'tree-reference':
537
if content_summary[3] != parent_entry.reference_revision:
540
# unchanged, carry over.
541
ie.reference_revision = parent_entry.reference_revision
542
ie.revision = parent_entry.revision
543
return self._get_delta(ie, basis_inv, path), False, None
544
ie.reference_revision = content_summary[3]
546
self._add_text_to_weave(ie.file_id, lines, heads, None)
548
raise NotImplementedError('unknown kind')
549
ie.revision = self._new_revision_id
550
self._any_changes = True
551
return self._get_delta(ie, basis_inv, path), True, fingerprint
553
def record_iter_changes(self, tree, basis_revision_id, iter_changes,
554
_entry_factory=entry_factory):
555
"""Record a new tree via iter_changes.
557
:param tree: The tree to obtain text contents from for changed objects.
558
:param basis_revision_id: The revision id of the tree the iter_changes
559
has been generated against. Currently assumed to be the same
560
as self.parents[0] - if it is not, errors may occur.
561
:param iter_changes: An iter_changes iterator with the changes to apply
562
to basis_revision_id. The iterator must not include any items with
563
a current kind of None - missing items must be either filtered out
564
or errored-on beefore record_iter_changes sees the item.
565
:param _entry_factory: Private method to bind entry_factory locally for
567
:return: A generator of (file_id, relpath, fs_hash) tuples for use with
570
# Create an inventory delta based on deltas between all the parents and
571
# deltas between all the parent inventories. We use inventory delta's
572
# between the inventory objects because iter_changes masks
573
# last-changed-field only changes.
575
# file_id -> change map, change is fileid, paths, changed, versioneds,
576
# parents, names, kinds, executables
578
# {file_id -> revision_id -> inventory entry, for entries in parent
579
# trees that are not parents[0]
583
revtrees = list(self.repository.revision_trees(self.parents))
584
except errors.NoSuchRevision:
585
# one or more ghosts, slow path.
587
for revision_id in self.parents:
589
revtrees.append(self.repository.revision_tree(revision_id))
590
except errors.NoSuchRevision:
592
basis_revision_id = _mod_revision.NULL_REVISION
594
revtrees.append(self.repository.revision_tree(
595
_mod_revision.NULL_REVISION))
596
# The basis inventory from a repository
598
basis_inv = revtrees[0].inventory
600
basis_inv = self.repository.revision_tree(
601
_mod_revision.NULL_REVISION).inventory
602
if len(self.parents) > 0:
603
if basis_revision_id != self.parents[0] and not ghost_basis:
605
"arbitrary basis parents not yet supported with merges")
606
for revtree in revtrees[1:]:
607
for change in revtree.inventory._make_delta(basis_inv):
608
if change[1] is None:
609
# Not present in this parent.
611
if change[2] not in merged_ids:
612
if change[0] is not None:
613
basis_entry = basis_inv[change[2]]
614
merged_ids[change[2]] = [
616
basis_entry.revision,
619
parent_entries[change[2]] = {
621
basis_entry.revision:basis_entry,
623
change[3].revision:change[3],
626
merged_ids[change[2]] = [change[3].revision]
627
parent_entries[change[2]] = {change[3].revision:change[3]}
629
merged_ids[change[2]].append(change[3].revision)
630
parent_entries[change[2]][change[3].revision] = change[3]
633
# Setup the changes from the tree:
634
# changes maps file_id -> (change, [parent revision_ids])
636
for change in iter_changes:
637
# This probably looks up in basis_inv way to much.
638
if change[1][0] is not None:
639
head_candidate = [basis_inv[change[0]].revision]
642
changes[change[0]] = change, merged_ids.get(change[0],
644
unchanged_merged = set(merged_ids) - set(changes)
645
# Extend the changes dict with synthetic changes to record merges of
647
for file_id in unchanged_merged:
648
# Record a merged version of these items that did not change vs the
649
# basis. This can be either identical parallel changes, or a revert
650
# of a specific file after a merge. The recorded content will be
651
# that of the current tree (which is the same as the basis), but
652
# the per-file graph will reflect a merge.
653
# NB:XXX: We are reconstructing path information we had, this
654
# should be preserved instead.
655
# inv delta change: (file_id, (path_in_source, path_in_target),
656
# changed_content, versioned, parent, name, kind,
659
basis_entry = basis_inv[file_id]
660
except errors.NoSuchId:
661
# a change from basis->some_parents but file_id isn't in basis
662
# so was new in the merge, which means it must have changed
663
# from basis -> current, and as it hasn't the add was reverted
664
# by the user. So we discard this change.
668
(basis_inv.id2path(file_id), tree.id2path(file_id)),
670
(basis_entry.parent_id, basis_entry.parent_id),
671
(basis_entry.name, basis_entry.name),
672
(basis_entry.kind, basis_entry.kind),
673
(basis_entry.executable, basis_entry.executable))
674
changes[file_id] = (change, merged_ids[file_id])
675
# changes contains tuples with the change and a set of inventory
676
# candidates for the file.
678
# old_path, new_path, file_id, new_inventory_entry
679
seen_root = False # Is the root in the basis delta?
680
inv_delta = self._basis_delta
681
modified_rev = self._new_revision_id
682
for change, head_candidates in changes.values():
683
if change[3][1]: # versioned in target.
684
# Several things may be happening here:
685
# We may have a fork in the per-file graph
686
# - record a change with the content from tree
687
# We may have a change against < all trees
688
# - carry over the tree that hasn't changed
689
# We may have a change against all trees
690
# - record the change with the content from tree
693
entry = _entry_factory[kind](file_id, change[5][1],
695
head_set = self._heads(change[0], set(head_candidates))
698
for head_candidate in head_candidates:
699
if head_candidate in head_set:
700
heads.append(head_candidate)
701
head_set.remove(head_candidate)
704
# Could be a carry-over situation:
705
parent_entry_revs = parent_entries.get(file_id, None)
706
if parent_entry_revs:
707
parent_entry = parent_entry_revs.get(heads[0], None)
710
if parent_entry is None:
711
# The parent iter_changes was called against is the one
712
# that is the per-file head, so any change is relevant
713
# iter_changes is valid.
714
carry_over_possible = False
716
# could be a carry over situation
717
# A change against the basis may just indicate a merge,
718
# we need to check the content against the source of the
719
# merge to determine if it was changed after the merge
721
if (parent_entry.kind != entry.kind or
722
parent_entry.parent_id != entry.parent_id or
723
parent_entry.name != entry.name):
724
# Metadata common to all entries has changed
725
# against per-file parent
726
carry_over_possible = False
728
carry_over_possible = True
729
# per-type checks for changes against the parent_entry
732
# Cannot be a carry-over situation
733
carry_over_possible = False
734
# Populate the entry in the delta
736
# XXX: There is still a small race here: If someone reverts the content of a file
737
# after iter_changes examines and decides it has changed,
738
# we will unconditionally record a new version even if some
739
# other process reverts it while commit is running (with
740
# the revert happening after iter_changes did it's
743
entry.executable = True
745
entry.executable = False
746
if (carry_over_possible and
747
parent_entry.executable == entry.executable):
748
# Check the file length, content hash after reading
750
nostore_sha = parent_entry.text_sha1
753
file_obj, stat_value = tree.get_file_with_stat(file_id, change[1][1])
755
lines = file_obj.readlines()
759
entry.text_sha1, entry.text_size = self._add_text_to_weave(
760
file_id, lines, heads, nostore_sha)
761
yield file_id, change[1][1], (entry.text_sha1, stat_value)
762
except errors.ExistingContent:
763
# No content change against a carry_over parent
764
# Perhaps this should also yield a fs hash update?
766
entry.text_size = parent_entry.text_size
767
entry.text_sha1 = parent_entry.text_sha1
768
elif kind == 'symlink':
770
entry.symlink_target = tree.get_symlink_target(file_id)
771
if (carry_over_possible and
772
parent_entry.symlink_target == entry.symlink_target):
775
self._add_text_to_weave(change[0], [], heads, None)
776
elif kind == 'directory':
777
if carry_over_possible:
780
# Nothing to set on the entry.
781
# XXX: split into the Root and nonRoot versions.
782
if change[1][1] != '' or self.repository.supports_rich_root():
783
self._add_text_to_weave(change[0], [], heads, None)
784
elif kind == 'tree-reference':
785
if not self.repository._format.supports_tree_reference:
786
# This isn't quite sane as an error, but we shouldn't
787
# ever see this code path in practice: tree's don't
788
# permit references when the repo doesn't support tree
790
raise errors.UnsupportedOperation(tree.add_reference,
792
entry.reference_revision = \
793
tree.get_reference_revision(change[0])
794
if (carry_over_possible and
795
parent_entry.reference_revision == reference_revision):
798
self._add_text_to_weave(change[0], [], heads, None)
800
raise AssertionError('unknown kind %r' % kind)
802
entry.revision = modified_rev
804
entry.revision = parent_entry.revision
807
new_path = change[1][1]
808
inv_delta.append((change[1][0], new_path, change[0], entry))
811
self.new_inventory = None
813
self._any_changes = True
815
# housekeeping root entry changes do not affect no-change commits.
816
self._require_root_change(tree)
817
self.basis_delta_revision = basis_revision_id
819
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
820
# Note: as we read the content directly from the tree, we know its not
821
# been turned into unicode or badly split - but a broken tree
822
# implementation could give us bad output from readlines() so this is
823
# not a guarantee of safety. What would be better is always checking
824
# the content during test suite execution. RBC 20070912
825
parent_keys = tuple((file_id, parent) for parent in parents)
826
return self.repository.texts.add_lines(
827
(file_id, self._new_revision_id), parent_keys, new_lines,
828
nostore_sha=nostore_sha, random_id=self.random_revid,
829
check_content=False)[0:2]
832
class RootCommitBuilder(CommitBuilder):
833
"""This commitbuilder actually records the root id"""
835
# the root entry gets versioned properly by this builder.
836
_versioned_root = True
838
def _check_root(self, ie, parent_invs, tree):
839
"""Helper for record_entry_contents.
841
:param ie: An entry being added.
842
:param parent_invs: The inventories of the parent revisions of the
844
:param tree: The tree that is being committed.
847
def _require_root_change(self, tree):
848
"""Enforce an appropriate root object change.
850
This is called once when record_iter_changes is called, if and only if
851
the root was not in the delta calculated by record_iter_changes.
853
:param tree: The tree which is being committed.
855
# versioned roots do not change unless the tree found a change.
858
######################################################################
861
class Repository(object):
862
"""Repository holding history for one or more branches.
864
The repository holds and retrieves historical information including
865
revisions and file history. It's normally accessed only by the Branch,
866
which views a particular line of development through that history.
868
The Repository builds on top of some byte storage facilies (the revisions,
869
signatures, inventories and texts attributes) and a Transport, which
870
respectively provide byte storage and a means to access the (possibly
873
The byte storage facilities are addressed via tuples, which we refer to
874
as 'keys' throughout the code base. Revision_keys, inventory_keys and
875
signature_keys are all 1-tuples: (revision_id,). text_keys are two-tuples:
876
(file_id, revision_id). We use this interface because it allows low
877
friction with the underlying code that implements disk indices, network
878
encoding and other parts of bzrlib.
880
:ivar revisions: A bzrlib.versionedfile.VersionedFiles instance containing
881
the serialised revisions for the repository. This can be used to obtain
882
revision graph information or to access raw serialised revisions.
883
The result of trying to insert data into the repository via this store
884
is undefined: it should be considered read-only except for implementors
886
:ivar signatures: A bzrlib.versionedfile.VersionedFiles instance containing
887
the serialised signatures for the repository. This can be used to
888
obtain access to raw serialised signatures. The result of trying to
889
insert data into the repository via this store is undefined: it should
890
be considered read-only except for implementors of repositories.
891
:ivar inventories: A bzrlib.versionedfile.VersionedFiles instance containing
892
the serialised inventories for the repository. This can be used to
893
obtain unserialised inventories. The result of trying to insert data
894
into the repository via this store is undefined: it should be
895
considered read-only except for implementors of repositories.
896
:ivar texts: A bzrlib.versionedfile.VersionedFiles instance containing the
897
texts of files and directories for the repository. This can be used to
898
obtain file texts or file graphs. Note that Repository.iter_file_bytes
899
is usually a better interface for accessing file texts.
900
The result of trying to insert data into the repository via this store
901
is undefined: it should be considered read-only except for implementors
903
:ivar _transport: Transport for file access to repository, typically
904
pointing to .bzr/repository.
907
# What class to use for a CommitBuilder. Often its simpler to change this
908
# in a Repository class subclass rather than to override
909
# get_commit_builder.
910
_commit_builder_class = CommitBuilder
911
# The search regex used by xml based repositories to determine what things
912
# where changed in a single commit.
913
_file_ids_altered_regex = lazy_regex.lazy_compile(
914
r'file_id="(?P<file_id>[^"]+)"'
915
r'.* revision="(?P<revision_id>[^"]+)"'
918
def abort_write_group(self, suppress_errors=False):
919
"""Commit the contents accrued within the current write group.
921
:param suppress_errors: if true, abort_write_group will catch and log
922
unexpected errors that happen during the abort, rather than
923
allowing them to propagate. Defaults to False.
925
:seealso: start_write_group.
927
if self._write_group is not self.get_transaction():
928
# has an unlock or relock occured ?
929
raise errors.BzrError('mismatched lock context and write group.')
931
self._abort_write_group()
932
except Exception, exc:
933
self._write_group = None
934
if not suppress_errors:
936
mutter('abort_write_group failed')
937
log_exception_quietly()
938
note('bzr: ERROR (ignored): %s', exc)
939
self._write_group = None
941
def _abort_write_group(self):
942
"""Template method for per-repository write group cleanup.
944
This is called during abort before the write group is considered to be
945
finished and should cleanup any internal state accrued during the write
946
group. There is no requirement that data handed to the repository be
947
*not* made available - this is not a rollback - but neither should any
948
attempt be made to ensure that data added is fully commited. Abort is
949
invoked when an error has occured so futher disk or network operations
950
may not be possible or may error and if possible should not be
954
def add_fallback_repository(self, repository):
955
"""Add a repository to use for looking up data not held locally.
957
:param repository: A repository.
959
if not self._format.supports_external_lookups:
960
raise errors.UnstackableRepositoryFormat(self._format, self.base)
961
self._check_fallback_repository(repository)
962
self._fallback_repositories.append(repository)
963
self.texts.add_fallback_versioned_files(repository.texts)
964
self.inventories.add_fallback_versioned_files(repository.inventories)
965
self.revisions.add_fallback_versioned_files(repository.revisions)
966
self.signatures.add_fallback_versioned_files(repository.signatures)
968
def _check_fallback_repository(self, repository):
969
"""Check that this repository can fallback to repository safely.
971
Raise an error if not.
973
:param repository: A repository to fallback to.
975
return InterRepository._assert_same_model(self, repository)
977
def add_inventory(self, revision_id, inv, parents):
978
"""Add the inventory inv to the repository as revision_id.
980
:param parents: The revision ids of the parents that revision_id
981
is known to have and are in the repository already.
983
:returns: The validator(which is a sha1 digest, though what is sha'd is
984
repository format specific) of the serialized inventory.
986
if not self.is_in_write_group():
987
raise AssertionError("%r not in write group" % (self,))
988
_mod_revision.check_not_reserved_id(revision_id)
989
if not (inv.revision_id is None or inv.revision_id == revision_id):
990
raise AssertionError(
991
"Mismatch between inventory revision"
992
" id and insertion revid (%r, %r)"
993
% (inv.revision_id, revision_id))
995
raise AssertionError()
996
inv_lines = self._serialise_inventory_to_lines(inv)
997
return self._inventory_add_lines(revision_id, parents,
998
inv_lines, check_content=False)
1000
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1002
"""Add a new inventory expressed as a delta against another revision.
1004
:param basis_revision_id: The inventory id the delta was created
1005
against. (This does not have to be a direct parent.)
1006
:param delta: The inventory delta (see Inventory.apply_delta for
1008
:param new_revision_id: The revision id that the inventory is being
1010
:param parents: The revision ids of the parents that revision_id is
1011
known to have and are in the repository already. These are supplied
1012
for repositories that depend on the inventory graph for revision
1013
graph access, as well as for those that pun ancestry with delta
1016
:returns: (validator, new_inv)
1017
The validator(which is a sha1 digest, though what is sha'd is
1018
repository format specific) of the serialized inventory, and the
1019
resulting inventory.
1021
if not self.is_in_write_group():
1022
raise AssertionError("%r not in write group" % (self,))
1023
_mod_revision.check_not_reserved_id(new_revision_id)
1024
basis_tree = self.revision_tree(basis_revision_id)
1025
basis_tree.lock_read()
1027
# Note that this mutates the inventory of basis_tree, which not all
1028
# inventory implementations may support: A better idiom would be to
1029
# return a new inventory, but as there is no revision tree cache in
1030
# repository this is safe for now - RBC 20081013
1031
basis_inv = basis_tree.inventory
1032
basis_inv.apply_delta(delta)
1033
basis_inv.revision_id = new_revision_id
1034
return (self.add_inventory(new_revision_id, basis_inv, parents),
1039
def _inventory_add_lines(self, revision_id, parents, lines,
1040
check_content=True):
1041
"""Store lines in inv_vf and return the sha1 of the inventory."""
1042
parents = [(parent,) for parent in parents]
1043
return self.inventories.add_lines((revision_id,), parents, lines,
1044
check_content=check_content)[0]
1046
def add_revision(self, revision_id, rev, inv=None, config=None):
1047
"""Add rev to the revision store as revision_id.
1049
:param revision_id: the revision id to use.
1050
:param rev: The revision object.
1051
:param inv: The inventory for the revision. if None, it will be looked
1052
up in the inventory storer
1053
:param config: If None no digital signature will be created.
1054
If supplied its signature_needed method will be used
1055
to determine if a signature should be made.
1057
# TODO: jam 20070210 Shouldn't we check rev.revision_id and
1059
_mod_revision.check_not_reserved_id(revision_id)
1060
if config is not None and config.signature_needed():
1062
inv = self.get_inventory(revision_id)
1063
plaintext = Testament(rev, inv).as_short_text()
1064
self.store_revision_signature(
1065
gpg.GPGStrategy(config), plaintext, revision_id)
1066
# check inventory present
1067
if not self.inventories.get_parent_map([(revision_id,)]):
1069
raise errors.WeaveRevisionNotPresent(revision_id,
1072
# yes, this is not suitable for adding with ghosts.
1073
rev.inventory_sha1 = self.add_inventory(revision_id, inv,
1076
key = (revision_id,)
1077
rev.inventory_sha1 = self.inventories.get_sha1s([key])[key]
1078
self._add_revision(rev)
1080
def _add_revision(self, revision):
1081
text = self._serializer.write_revision_to_string(revision)
1082
key = (revision.revision_id,)
1083
parents = tuple((parent,) for parent in revision.parent_ids)
1084
self.revisions.add_lines(key, parents, osutils.split_lines(text))
1086
def all_revision_ids(self):
1087
"""Returns a list of all the revision ids in the repository.
1089
This is conceptually deprecated because code should generally work on
1090
the graph reachable from a particular revision, and ignore any other
1091
revisions that might be present. There is no direct replacement
1094
if 'evil' in debug.debug_flags:
1095
mutter_callsite(2, "all_revision_ids is linear with history.")
1096
return self._all_revision_ids()
1098
def _all_revision_ids(self):
1099
"""Returns a list of all the revision ids in the repository.
1101
These are in as much topological order as the underlying store can
1104
raise NotImplementedError(self._all_revision_ids)
1106
def break_lock(self):
1107
"""Break a lock if one is present from another instance.
1109
Uses the ui factory to ask for confirmation if the lock may be from
1112
self.control_files.break_lock()
1115
def _eliminate_revisions_not_present(self, revision_ids):
1116
"""Check every revision id in revision_ids to see if we have it.
1118
Returns a set of the present revisions.
1121
graph = self.get_graph()
1122
parent_map = graph.get_parent_map(revision_ids)
1123
# The old API returned a list, should this actually be a set?
1124
return parent_map.keys()
1127
def create(a_bzrdir):
1128
"""Construct the current default format repository in a_bzrdir."""
1129
return RepositoryFormat.get_default_format().initialize(a_bzrdir)
1131
def __init__(self, _format, a_bzrdir, control_files):
1132
"""instantiate a Repository.
1134
:param _format: The format of the repository on disk.
1135
:param a_bzrdir: The BzrDir of the repository.
1137
In the future we will have a single api for all stores for
1138
getting file texts, inventories and revisions, then
1139
this construct will accept instances of those things.
1141
super(Repository, self).__init__()
1142
self._format = _format
1143
# the following are part of the public API for Repository:
1144
self.bzrdir = a_bzrdir
1145
self.control_files = control_files
1146
self._transport = control_files._transport
1147
self.base = self._transport.base
1149
self._reconcile_does_inventory_gc = True
1150
self._reconcile_fixes_text_parents = False
1151
self._reconcile_backsup_inventory = True
1152
# not right yet - should be more semantically clear ?
1154
# TODO: make sure to construct the right store classes, etc, depending
1155
# on whether escaping is required.
1156
self._warn_if_deprecated()
1157
self._write_group = None
1158
# Additional places to query for data.
1159
self._fallback_repositories = []
1160
# An InventoryEntry cache, used during deserialization
1161
self._inventory_entry_cache = fifo_cache.FIFOCache(10*1024)
1164
return '%s(%r)' % (self.__class__.__name__,
1167
def has_same_location(self, other):
1168
"""Returns a boolean indicating if this repository is at the same
1169
location as another repository.
1171
This might return False even when two repository objects are accessing
1172
the same physical repository via different URLs.
1174
if self.__class__ is not other.__class__:
1176
return (self._transport.base == other._transport.base)
1178
def is_in_write_group(self):
1179
"""Return True if there is an open write group.
1181
:seealso: start_write_group.
1183
return self._write_group is not None
1185
def is_locked(self):
1186
return self.control_files.is_locked()
1188
def is_write_locked(self):
1189
"""Return True if this object is write locked."""
1190
return self.is_locked() and self.control_files._lock_mode == 'w'
1192
def lock_write(self, token=None):
1193
"""Lock this repository for writing.
1195
This causes caching within the repository obejct to start accumlating
1196
data during reads, and allows a 'write_group' to be obtained. Write
1197
groups must be used for actual data insertion.
1199
:param token: if this is already locked, then lock_write will fail
1200
unless the token matches the existing lock.
1201
:returns: a token if this instance supports tokens, otherwise None.
1202
:raises TokenLockingNotSupported: when a token is given but this
1203
instance doesn't support using token locks.
1204
:raises MismatchedToken: if the specified token doesn't match the token
1205
of the existing lock.
1206
:seealso: start_write_group.
1208
A token should be passed in if you know that you have locked the object
1209
some other way, and need to synchronise this object's state with that
1212
XXX: this docstring is duplicated in many places, e.g. lockable_files.py
1214
locked = self.is_locked()
1215
result = self.control_files.lock_write(token=token)
1216
for repo in self._fallback_repositories:
1217
# Writes don't affect fallback repos
1220
self._refresh_data()
1223
def lock_read(self):
1224
locked = self.is_locked()
1225
self.control_files.lock_read()
1226
for repo in self._fallback_repositories:
1229
self._refresh_data()
1231
def get_physical_lock_status(self):
1232
return self.control_files.get_physical_lock_status()
1234
def leave_lock_in_place(self):
1235
"""Tell this repository not to release the physical lock when this
1238
If lock_write doesn't return a token, then this method is not supported.
1240
self.control_files.leave_in_place()
1242
def dont_leave_lock_in_place(self):
1243
"""Tell this repository to release the physical lock when this
1244
object is unlocked, even if it didn't originally acquire it.
1246
If lock_write doesn't return a token, then this method is not supported.
1248
self.control_files.dont_leave_in_place()
1251
def gather_stats(self, revid=None, committers=None):
1252
"""Gather statistics from a revision id.
1254
:param revid: The revision id to gather statistics from, if None, then
1255
no revision specific statistics are gathered.
1256
:param committers: Optional parameter controlling whether to grab
1257
a count of committers from the revision specific statistics.
1258
:return: A dictionary of statistics. Currently this contains:
1259
committers: The number of committers if requested.
1260
firstrev: A tuple with timestamp, timezone for the penultimate left
1261
most ancestor of revid, if revid is not the NULL_REVISION.
1262
latestrev: A tuple with timestamp, timezone for revid, if revid is
1263
not the NULL_REVISION.
1264
revisions: The total revision count in the repository.
1265
size: An estimate disk size of the repository in bytes.
1268
if revid and committers:
1269
result['committers'] = 0
1270
if revid and revid != _mod_revision.NULL_REVISION:
1272
all_committers = set()
1273
revisions = self.get_ancestry(revid)
1274
# pop the leading None
1276
first_revision = None
1278
# ignore the revisions in the middle - just grab first and last
1279
revisions = revisions[0], revisions[-1]
1280
for revision in self.get_revisions(revisions):
1281
if not first_revision:
1282
first_revision = revision
1284
all_committers.add(revision.committer)
1285
last_revision = revision
1287
result['committers'] = len(all_committers)
1288
result['firstrev'] = (first_revision.timestamp,
1289
first_revision.timezone)
1290
result['latestrev'] = (last_revision.timestamp,
1291
last_revision.timezone)
1293
# now gather global repository information
1294
# XXX: This is available for many repos regardless of listability.
1295
if self.bzrdir.root_transport.listable():
1296
# XXX: do we want to __define len__() ?
1297
# Maybe the versionedfiles object should provide a different
1298
# method to get the number of keys.
1299
result['revisions'] = len(self.revisions.keys())
1300
# result['size'] = t
1303
def find_branches(self, using=False):
1304
"""Find branches underneath this repository.
1306
This will include branches inside other branches.
1308
:param using: If True, list only branches using this repository.
1310
if using and not self.is_shared():
1312
return [self.bzrdir.open_branch()]
1313
except errors.NotBranchError:
1315
class Evaluator(object):
1318
self.first_call = True
1320
def __call__(self, bzrdir):
1321
# On the first call, the parameter is always the bzrdir
1322
# containing the current repo.
1323
if not self.first_call:
1325
repository = bzrdir.open_repository()
1326
except errors.NoRepositoryPresent:
1329
return False, (None, repository)
1330
self.first_call = False
1332
value = (bzrdir.open_branch(), None)
1333
except errors.NotBranchError:
1334
value = (None, None)
1338
for branch, repository in bzrdir.BzrDir.find_bzrdirs(
1339
self.bzrdir.root_transport, evaluate=Evaluator()):
1340
if branch is not None:
1341
branches.append(branch)
1342
if not using and repository is not None:
1343
branches.extend(repository.find_branches())
1347
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1348
"""Return the revision ids that other has that this does not.
1350
These are returned in topological order.
1352
revision_id: only return revision ids included by revision_id.
1354
return InterRepository.get(other, self).search_missing_revision_ids(
1355
revision_id, find_ghosts)
1359
"""Open the repository rooted at base.
1361
For instance, if the repository is at URL/.bzr/repository,
1362
Repository.open(URL) -> a Repository instance.
1364
control = bzrdir.BzrDir.open(base)
1365
return control.open_repository()
1367
def copy_content_into(self, destination, revision_id=None):
1368
"""Make a complete copy of the content in self into destination.
1370
This is a destructive operation! Do not use it on existing
1373
return InterRepository.get(self, destination).copy_content(revision_id)
1375
def commit_write_group(self):
1376
"""Commit the contents accrued within the current write group.
1378
:seealso: start_write_group.
1380
if self._write_group is not self.get_transaction():
1381
# has an unlock or relock occured ?
1382
raise errors.BzrError('mismatched lock context %r and '
1384
(self.get_transaction(), self._write_group))
1385
self._commit_write_group()
1386
self._write_group = None
1388
def _commit_write_group(self):
1389
"""Template method for per-repository write group cleanup.
1391
This is called before the write group is considered to be
1392
finished and should ensure that all data handed to the repository
1393
for writing during the write group is safely committed (to the
1394
extent possible considering file system caching etc).
1397
def suspend_write_group(self):
1398
raise errors.UnsuspendableWriteGroup(self)
1400
def refresh_data(self):
1401
"""Re-read any data needed to to synchronise with disk.
1403
This method is intended to be called after another repository instance
1404
(such as one used by a smart server) has inserted data into the
1405
repository. It may not be called during a write group, but may be
1406
called at any other time.
1408
if self.is_in_write_group():
1409
raise errors.InternalBzrError(
1410
"May not refresh_data while in a write group.")
1411
self._refresh_data()
1413
def resume_write_group(self, tokens):
1414
if not self.is_write_locked():
1415
raise errors.NotWriteLocked(self)
1416
if self._write_group:
1417
raise errors.BzrError('already in a write group')
1418
self._resume_write_group(tokens)
1419
# so we can detect unlock/relock - the write group is now entered.
1420
self._write_group = self.get_transaction()
1422
def _resume_write_group(self, tokens):
1423
raise errors.UnsuspendableWriteGroup(self)
1425
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1427
"""Fetch the content required to construct revision_id from source.
1429
If revision_id is None and fetch_spec is None, then all content is
1432
fetch() may not be used when the repository is in a write group -
1433
either finish the current write group before using fetch, or use
1434
fetch before starting the write group.
1436
:param find_ghosts: Find and copy revisions in the source that are
1437
ghosts in the target (and not reachable directly by walking out to
1438
the first-present revision in target from revision_id).
1439
:param revision_id: If specified, all the content needed for this
1440
revision ID will be copied to the target. Fetch will determine for
1441
itself which content needs to be copied.
1442
:param fetch_spec: If specified, a SearchResult or
1443
PendingAncestryResult that describes which revisions to copy. This
1444
allows copying multiple heads at once. Mutually exclusive with
1447
if fetch_spec is not None and revision_id is not None:
1448
raise AssertionError(
1449
"fetch_spec and revision_id are mutually exclusive.")
1450
if self.is_in_write_group():
1451
raise errors.InternalBzrError(
1452
"May not fetch while in a write group.")
1453
# fast path same-url fetch operations
1454
if self.has_same_location(source) and fetch_spec is None:
1455
# check that last_revision is in 'from' and then return a
1457
if (revision_id is not None and
1458
not _mod_revision.is_null(revision_id)):
1459
self.get_revision(revision_id)
1461
# if there is no specific appropriate InterRepository, this will get
1462
# the InterRepository base class, which raises an
1463
# IncompatibleRepositories when asked to fetch.
1464
inter = InterRepository.get(source, self)
1465
return inter.fetch(revision_id=revision_id, pb=pb,
1466
find_ghosts=find_ghosts, fetch_spec=fetch_spec)
1468
def create_bundle(self, target, base, fileobj, format=None):
1469
return serializer.write_bundle(self, target, base, fileobj, format)
1471
def get_commit_builder(self, branch, parents, config, timestamp=None,
1472
timezone=None, committer=None, revprops=None,
1474
"""Obtain a CommitBuilder for this repository.
1476
:param branch: Branch to commit to.
1477
:param parents: Revision ids of the parents of the new revision.
1478
:param config: Configuration to use.
1479
:param timestamp: Optional timestamp recorded for commit.
1480
:param timezone: Optional timezone for timestamp.
1481
:param committer: Optional committer to set for commit.
1482
:param revprops: Optional dictionary of revision properties.
1483
:param revision_id: Optional revision id.
1485
result = self._commit_builder_class(self, parents, config,
1486
timestamp, timezone, committer, revprops, revision_id)
1487
self.start_write_group()
1491
if (self.control_files._lock_count == 1 and
1492
self.control_files._lock_mode == 'w'):
1493
if self._write_group is not None:
1494
self.abort_write_group()
1495
self.control_files.unlock()
1496
raise errors.BzrError(
1497
'Must end write groups before releasing write locks.')
1498
self.control_files.unlock()
1499
if self.control_files._lock_count == 0:
1500
self._inventory_entry_cache.clear()
1501
for repo in self._fallback_repositories:
1505
def clone(self, a_bzrdir, revision_id=None):
1506
"""Clone this repository into a_bzrdir using the current format.
1508
Currently no check is made that the format of this repository and
1509
the bzrdir format are compatible. FIXME RBC 20060201.
1511
:return: The newly created destination repository.
1513
# TODO: deprecate after 0.16; cloning this with all its settings is
1514
# probably not very useful -- mbp 20070423
1515
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
1516
self.copy_content_into(dest_repo, revision_id)
1519
def start_write_group(self):
1520
"""Start a write group in the repository.
1522
Write groups are used by repositories which do not have a 1:1 mapping
1523
between file ids and backend store to manage the insertion of data from
1524
both fetch and commit operations.
1526
A write lock is required around the start_write_group/commit_write_group
1527
for the support of lock-requiring repository formats.
1529
One can only insert data into a repository inside a write group.
1533
if not self.is_write_locked():
1534
raise errors.NotWriteLocked(self)
1535
if self._write_group:
1536
raise errors.BzrError('already in a write group')
1537
self._start_write_group()
1538
# so we can detect unlock/relock - the write group is now entered.
1539
self._write_group = self.get_transaction()
1541
def _start_write_group(self):
1542
"""Template method for per-repository write group startup.
1544
This is called before the write group is considered to be
1549
def sprout(self, to_bzrdir, revision_id=None):
1550
"""Create a descendent repository for new development.
1552
Unlike clone, this does not copy the settings of the repository.
1554
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
1555
dest_repo.fetch(self, revision_id=revision_id)
1558
def _create_sprouting_repo(self, a_bzrdir, shared):
1559
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
1560
# use target default format.
1561
dest_repo = a_bzrdir.create_repository()
1563
# Most control formats need the repository to be specifically
1564
# created, but on some old all-in-one formats it's not needed
1566
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
1567
except errors.UninitializableFormat:
1568
dest_repo = a_bzrdir.open_repository()
1571
def _get_sink(self):
1572
"""Return a sink for streaming into this repository."""
1573
return StreamSink(self)
1575
def _get_source(self, to_format):
1576
"""Return a source for streaming from this repository."""
1577
return StreamSource(self, to_format)
1580
def has_revision(self, revision_id):
1581
"""True if this repository has a copy of the revision."""
1582
return revision_id in self.has_revisions((revision_id,))
1585
def has_revisions(self, revision_ids):
1586
"""Probe to find out the presence of multiple revisions.
1588
:param revision_ids: An iterable of revision_ids.
1589
:return: A set of the revision_ids that were present.
1591
parent_map = self.revisions.get_parent_map(
1592
[(rev_id,) for rev_id in revision_ids])
1594
if _mod_revision.NULL_REVISION in revision_ids:
1595
result.add(_mod_revision.NULL_REVISION)
1596
result.update([key[0] for key in parent_map])
1600
def get_revision(self, revision_id):
1601
"""Return the Revision object for a named revision."""
1602
return self.get_revisions([revision_id])[0]
1605
def get_revision_reconcile(self, revision_id):
1606
"""'reconcile' helper routine that allows access to a revision always.
1608
This variant of get_revision does not cross check the weave graph
1609
against the revision one as get_revision does: but it should only
1610
be used by reconcile, or reconcile-alike commands that are correcting
1611
or testing the revision graph.
1613
return self._get_revisions([revision_id])[0]
1616
def get_revisions(self, revision_ids):
1617
"""Get many revisions at once."""
1618
return self._get_revisions(revision_ids)
1621
def _get_revisions(self, revision_ids):
1622
"""Core work logic to get many revisions without sanity checks."""
1623
for rev_id in revision_ids:
1624
if not rev_id or not isinstance(rev_id, basestring):
1625
raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
1626
keys = [(key,) for key in revision_ids]
1627
stream = self.revisions.get_record_stream(keys, 'unordered', True)
1629
for record in stream:
1630
if record.storage_kind == 'absent':
1631
raise errors.NoSuchRevision(self, record.key[0])
1632
text = record.get_bytes_as('fulltext')
1633
rev = self._serializer.read_revision_from_string(text)
1634
revs[record.key[0]] = rev
1635
return [revs[revid] for revid in revision_ids]
1638
def get_revision_xml(self, revision_id):
1639
# TODO: jam 20070210 This shouldn't be necessary since get_revision
1640
# would have already do it.
1641
# TODO: jam 20070210 Just use _serializer.write_revision_to_string()
1642
rev = self.get_revision(revision_id)
1643
rev_tmp = cStringIO.StringIO()
1644
# the current serializer..
1645
self._serializer.write_revision(rev, rev_tmp)
1647
return rev_tmp.getvalue()
1649
def get_deltas_for_revisions(self, revisions, specific_fileids=None):
1650
"""Produce a generator of revision deltas.
1652
Note that the input is a sequence of REVISIONS, not revision_ids.
1653
Trees will be held in memory until the generator exits.
1654
Each delta is relative to the revision's lefthand predecessor.
1656
:param specific_fileids: if not None, the result is filtered
1657
so that only those file-ids, their parents and their
1658
children are included.
1660
# Get the revision-ids of interest
1661
required_trees = set()
1662
for revision in revisions:
1663
required_trees.add(revision.revision_id)
1664
required_trees.update(revision.parent_ids[:1])
1666
# Get the matching filtered trees. Note that it's more
1667
# efficient to pass filtered trees to changes_from() rather
1668
# than doing the filtering afterwards. changes_from() could
1669
# arguably do the filtering itself but it's path-based, not
1670
# file-id based, so filtering before or afterwards is
1672
if specific_fileids is None:
1673
trees = dict((t.get_revision_id(), t) for
1674
t in self.revision_trees(required_trees))
1676
trees = dict((t.get_revision_id(), t) for
1677
t in self._filtered_revision_trees(required_trees,
1680
# Calculate the deltas
1681
for revision in revisions:
1682
if not revision.parent_ids:
1683
old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
1685
old_tree = trees[revision.parent_ids[0]]
1686
yield trees[revision.revision_id].changes_from(old_tree)
1689
def get_revision_delta(self, revision_id, specific_fileids=None):
1690
"""Return the delta for one revision.
1692
The delta is relative to the left-hand predecessor of the
1695
:param specific_fileids: if not None, the result is filtered
1696
so that only those file-ids, their parents and their
1697
children are included.
1699
r = self.get_revision(revision_id)
1700
return list(self.get_deltas_for_revisions([r],
1701
specific_fileids=specific_fileids))[0]
1704
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1705
signature = gpg_strategy.sign(plaintext)
1706
self.add_signature_text(revision_id, signature)
1709
def add_signature_text(self, revision_id, signature):
1710
self.signatures.add_lines((revision_id,), (),
1711
osutils.split_lines(signature))
1713
def find_text_key_references(self):
1714
"""Find the text key references within the repository.
1716
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1717
to whether they were referred to by the inventory of the
1718
revision_id that they contain. The inventory texts from all present
1719
revision ids are assessed to generate this report.
1721
revision_keys = self.revisions.keys()
1722
w = self.inventories
1723
pb = ui.ui_factory.nested_progress_bar()
1725
return self._find_text_key_references_from_xml_inventory_lines(
1726
w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
1730
def _find_text_key_references_from_xml_inventory_lines(self,
1732
"""Core routine for extracting references to texts from inventories.
1734
This performs the translation of xml lines to revision ids.
1736
:param line_iterator: An iterator of lines, origin_version_id
1737
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1738
to whether they were referred to by the inventory of the
1739
revision_id that they contain. Note that if that revision_id was
1740
not part of the line_iterator's output then False will be given -
1741
even though it may actually refer to that key.
1743
if not self._serializer.support_altered_by_hack:
1744
raise AssertionError(
1745
"_find_text_key_references_from_xml_inventory_lines only "
1746
"supported for branches which store inventory as unnested xml"
1747
", not on %r" % self)
1750
# this code needs to read every new line in every inventory for the
1751
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
1752
# not present in one of those inventories is unnecessary but not
1753
# harmful because we are filtering by the revision id marker in the
1754
# inventory lines : we only select file ids altered in one of those
1755
# revisions. We don't need to see all lines in the inventory because
1756
# only those added in an inventory in rev X can contain a revision=X
1758
unescape_revid_cache = {}
1759
unescape_fileid_cache = {}
1761
# jam 20061218 In a big fetch, this handles hundreds of thousands
1762
# of lines, so it has had a lot of inlining and optimizing done.
1763
# Sorry that it is a little bit messy.
1764
# Move several functions to be local variables, since this is a long
1766
search = self._file_ids_altered_regex.search
1767
unescape = _unescape_xml
1768
setdefault = result.setdefault
1769
for line, line_key in line_iterator:
1770
match = search(line)
1773
# One call to match.group() returning multiple items is quite a
1774
# bit faster than 2 calls to match.group() each returning 1
1775
file_id, revision_id = match.group('file_id', 'revision_id')
1777
# Inlining the cache lookups helps a lot when you make 170,000
1778
# lines and 350k ids, versus 8.4 unique ids.
1779
# Using a cache helps in 2 ways:
1780
# 1) Avoids unnecessary decoding calls
1781
# 2) Re-uses cached strings, which helps in future set and
1783
# (2) is enough that removing encoding entirely along with
1784
# the cache (so we are using plain strings) results in no
1785
# performance improvement.
1787
revision_id = unescape_revid_cache[revision_id]
1789
unescaped = unescape(revision_id)
1790
unescape_revid_cache[revision_id] = unescaped
1791
revision_id = unescaped
1793
# Note that unconditionally unescaping means that we deserialise
1794
# every fileid, which for general 'pull' is not great, but we don't
1795
# really want to have some many fulltexts that this matters anyway.
1798
file_id = unescape_fileid_cache[file_id]
1800
unescaped = unescape(file_id)
1801
unescape_fileid_cache[file_id] = unescaped
1804
key = (file_id, revision_id)
1805
setdefault(key, False)
1806
if revision_id == line_key[-1]:
1810
def _inventory_xml_lines_for_keys(self, keys):
1811
"""Get a line iterator of the sort needed for findind references.
1813
Not relevant for non-xml inventory repositories.
1815
Ghosts in revision_keys are ignored.
1817
:param revision_keys: The revision keys for the inventories to inspect.
1818
:return: An iterator over (inventory line, revid) for the fulltexts of
1819
all of the xml inventories specified by revision_keys.
1821
stream = self.inventories.get_record_stream(keys, 'unordered', True)
1822
for record in stream:
1823
if record.storage_kind != 'absent':
1824
chunks = record.get_bytes_as('chunked')
1825
revid = record.key[-1]
1826
lines = osutils.chunks_to_lines(chunks)
1830
def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1832
"""Helper routine for fileids_altered_by_revision_ids.
1834
This performs the translation of xml lines to revision ids.
1836
:param line_iterator: An iterator of lines, origin_version_id
1837
:param revision_ids: The revision ids to filter for. This should be a
1838
set or other type which supports efficient __contains__ lookups, as
1839
the revision id from each parsed line will be looked up in the
1840
revision_ids filter.
1841
:return: a dictionary mapping altered file-ids to an iterable of
1842
revision_ids. Each altered file-ids has the exact revision_ids that
1843
altered it listed explicitly.
1845
seen = set(self._find_text_key_references_from_xml_inventory_lines(
1846
line_iterator).iterkeys())
1847
# Note that revision_ids are revision keys.
1848
parent_maps = self.revisions.get_parent_map(revision_ids)
1850
map(parents.update, parent_maps.itervalues())
1851
parents.difference_update(revision_ids)
1852
parent_seen = set(self._find_text_key_references_from_xml_inventory_lines(
1853
self._inventory_xml_lines_for_keys(parents)))
1854
new_keys = seen - parent_seen
1856
setdefault = result.setdefault
1857
for key in new_keys:
1858
setdefault(key[0], set()).add(key[-1])
1861
def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
1862
"""Find the file ids and versions affected by revisions.
1864
:param revisions: an iterable containing revision ids.
1865
:param _inv_weave: The inventory weave from this repository or None.
1866
If None, the inventory weave will be opened automatically.
1867
:return: a dictionary mapping altered file-ids to an iterable of
1868
revision_ids. Each altered file-ids has the exact revision_ids that
1869
altered it listed explicitly.
1871
selected_keys = set((revid,) for revid in revision_ids)
1872
w = _inv_weave or self.inventories
1873
pb = ui.ui_factory.nested_progress_bar()
1875
return self._find_file_ids_from_xml_inventory_lines(
1876
w.iter_lines_added_or_present_in_keys(
1877
selected_keys, pb=pb),
1882
def iter_files_bytes(self, desired_files):
1883
"""Iterate through file versions.
1885
Files will not necessarily be returned in the order they occur in
1886
desired_files. No specific order is guaranteed.
1888
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1889
value supplied by the caller as part of desired_files. It should
1890
uniquely identify the file version in the caller's context. (Examples:
1891
an index number or a TreeTransform trans_id.)
1893
bytes_iterator is an iterable of bytestrings for the file. The
1894
kind of iterable and length of the bytestrings are unspecified, but for
1895
this implementation, it is a list of bytes produced by
1896
VersionedFile.get_record_stream().
1898
:param desired_files: a list of (file_id, revision_id, identifier)
1902
for file_id, revision_id, callable_data in desired_files:
1903
text_keys[(file_id, revision_id)] = callable_data
1904
for record in self.texts.get_record_stream(text_keys, 'unordered', True):
1905
if record.storage_kind == 'absent':
1906
raise errors.RevisionNotPresent(record.key, self)
1907
yield text_keys[record.key], record.get_bytes_as('chunked')
1909
def _generate_text_key_index(self, text_key_references=None,
1911
"""Generate a new text key index for the repository.
1913
This is an expensive function that will take considerable time to run.
1915
:return: A dict mapping text keys ((file_id, revision_id) tuples) to a
1916
list of parents, also text keys. When a given key has no parents,
1917
the parents list will be [NULL_REVISION].
1919
# All revisions, to find inventory parents.
1920
if ancestors is None:
1921
graph = self.get_graph()
1922
ancestors = graph.get_parent_map(self.all_revision_ids())
1923
if text_key_references is None:
1924
text_key_references = self.find_text_key_references()
1925
pb = ui.ui_factory.nested_progress_bar()
1927
return self._do_generate_text_key_index(ancestors,
1928
text_key_references, pb)
1932
def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
1933
"""Helper for _generate_text_key_index to avoid deep nesting."""
1934
revision_order = tsort.topo_sort(ancestors)
1935
invalid_keys = set()
1937
for revision_id in revision_order:
1938
revision_keys[revision_id] = set()
1939
text_count = len(text_key_references)
1940
# a cache of the text keys to allow reuse; costs a dict of all the
1941
# keys, but saves a 2-tuple for every child of a given key.
1943
for text_key, valid in text_key_references.iteritems():
1945
invalid_keys.add(text_key)
1947
revision_keys[text_key[1]].add(text_key)
1948
text_key_cache[text_key] = text_key
1949
del text_key_references
1951
text_graph = graph.Graph(graph.DictParentsProvider(text_index))
1952
NULL_REVISION = _mod_revision.NULL_REVISION
1953
# Set a cache with a size of 10 - this suffices for bzr.dev but may be
1954
# too small for large or very branchy trees. However, for 55K path
1955
# trees, it would be easy to use too much memory trivially. Ideally we
1956
# could gauge this by looking at available real memory etc, but this is
1957
# always a tricky proposition.
1958
inventory_cache = lru_cache.LRUCache(10)
1959
batch_size = 10 # should be ~150MB on a 55K path tree
1960
batch_count = len(revision_order) / batch_size + 1
1962
pb.update("Calculating text parents", processed_texts, text_count)
1963
for offset in xrange(batch_count):
1964
to_query = revision_order[offset * batch_size:(offset + 1) *
1968
for rev_tree in self.revision_trees(to_query):
1969
revision_id = rev_tree.get_revision_id()
1970
parent_ids = ancestors[revision_id]
1971
for text_key in revision_keys[revision_id]:
1972
pb.update("Calculating text parents", processed_texts)
1973
processed_texts += 1
1974
candidate_parents = []
1975
for parent_id in parent_ids:
1976
parent_text_key = (text_key[0], parent_id)
1978
check_parent = parent_text_key not in \
1979
revision_keys[parent_id]
1981
# the parent parent_id is a ghost:
1982
check_parent = False
1983
# truncate the derived graph against this ghost.
1984
parent_text_key = None
1986
# look at the parent commit details inventories to
1987
# determine possible candidates in the per file graph.
1990
inv = inventory_cache[parent_id]
1992
inv = self.revision_tree(parent_id).inventory
1993
inventory_cache[parent_id] = inv
1994
parent_entry = inv._byid.get(text_key[0], None)
1995
if parent_entry is not None:
1997
text_key[0], parent_entry.revision)
1999
parent_text_key = None
2000
if parent_text_key is not None:
2001
candidate_parents.append(
2002
text_key_cache[parent_text_key])
2003
parent_heads = text_graph.heads(candidate_parents)
2004
new_parents = list(parent_heads)
2005
new_parents.sort(key=lambda x:candidate_parents.index(x))
2006
if new_parents == []:
2007
new_parents = [NULL_REVISION]
2008
text_index[text_key] = new_parents
2010
for text_key in invalid_keys:
2011
text_index[text_key] = [NULL_REVISION]
2014
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
2015
"""Get an iterable listing the keys of all the data introduced by a set
2018
The keys will be ordered so that the corresponding items can be safely
2019
fetched and inserted in that order.
2021
:returns: An iterable producing tuples of (knit-kind, file-id,
2022
versions). knit-kind is one of 'file', 'inventory', 'signatures',
2023
'revisions'. file-id is None unless knit-kind is 'file'.
2025
# XXX: it's a bit weird to control the inventory weave caching in this
2026
# generator. Ideally the caching would be done in fetch.py I think. Or
2027
# maybe this generator should explicitly have the contract that it
2028
# should not be iterated until the previously yielded item has been
2030
inv_w = self.inventories
2032
# file ids that changed
2033
file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
2035
num_file_ids = len(file_ids)
2036
for file_id, altered_versions in file_ids.iteritems():
2037
if _files_pb is not None:
2038
_files_pb.update("fetch texts", count, num_file_ids)
2040
yield ("file", file_id, altered_versions)
2041
# We're done with the files_pb. Note that it finished by the caller,
2042
# just as it was created by the caller.
2046
yield ("inventory", None, revision_ids)
2049
# XXX: Note ATM no callers actually pay attention to this return
2050
# instead they just use the list of revision ids and ignore
2051
# missing sigs. Consider removing this work entirely
2052
revisions_with_signatures = set(self.signatures.get_parent_map(
2053
[(r,) for r in revision_ids]))
2054
revisions_with_signatures = set(
2055
[r for (r,) in revisions_with_signatures])
2056
revisions_with_signatures.intersection_update(revision_ids)
2057
yield ("signatures", None, revisions_with_signatures)
2060
yield ("revisions", None, revision_ids)
2063
def get_inventory(self, revision_id):
2064
"""Get Inventory object by revision id."""
2065
return self.iter_inventories([revision_id]).next()
2067
def iter_inventories(self, revision_ids):
2068
"""Get many inventories by revision_ids.
2070
This will buffer some or all of the texts used in constructing the
2071
inventories in memory, but will only parse a single inventory at a
2074
:param revision_ids: The expected revision ids of the inventories.
2075
:return: An iterator of inventories.
2077
if ((None in revision_ids)
2078
or (_mod_revision.NULL_REVISION in revision_ids)):
2079
raise ValueError('cannot get null revision inventory')
2080
return self._iter_inventories(revision_ids)
2082
def _iter_inventories(self, revision_ids):
2083
"""single-document based inventory iteration."""
2084
for text, revision_id in self._iter_inventory_xmls(revision_ids):
2085
yield self.deserialise_inventory(revision_id, text)
2087
def _iter_inventory_xmls(self, revision_ids):
2088
keys = [(revision_id,) for revision_id in revision_ids]
2089
stream = self.inventories.get_record_stream(keys, 'unordered', True)
2091
for record in stream:
2092
if record.storage_kind != 'absent':
2093
text_chunks[record.key] = record.get_bytes_as('chunked')
2095
raise errors.NoSuchRevision(self, record.key)
2097
chunks = text_chunks.pop(key)
2098
yield ''.join(chunks), key[-1]
2100
def deserialise_inventory(self, revision_id, xml):
2101
"""Transform the xml into an inventory object.
2103
:param revision_id: The expected revision id of the inventory.
2104
:param xml: A serialised inventory.
2106
result = self._serializer.read_inventory_from_string(xml, revision_id,
2107
entry_cache=self._inventory_entry_cache)
2108
if result.revision_id != revision_id:
2109
raise AssertionError('revision id mismatch %s != %s' % (
2110
result.revision_id, revision_id))
2113
def serialise_inventory(self, inv):
2114
return self._serializer.write_inventory_to_string(inv)
2116
def _serialise_inventory_to_lines(self, inv):
2117
return self._serializer.write_inventory_to_lines(inv)
2119
def get_serializer_format(self):
2120
return self._serializer.format_num
2123
def get_inventory_xml(self, revision_id):
2124
"""Get inventory XML as a file object."""
2125
texts = self._iter_inventory_xmls([revision_id])
2127
text, revision_id = texts.next()
2128
except StopIteration:
2129
raise errors.HistoryMissing(self, 'inventory', revision_id)
2133
def get_inventory_sha1(self, revision_id):
2134
"""Return the sha1 hash of the inventory entry
2136
return self.get_revision(revision_id).inventory_sha1
2138
def iter_reverse_revision_history(self, revision_id):
2139
"""Iterate backwards through revision ids in the lefthand history
2141
:param revision_id: The revision id to start with. All its lefthand
2142
ancestors will be traversed.
2144
graph = self.get_graph()
2145
next_id = revision_id
2147
if next_id in (None, _mod_revision.NULL_REVISION):
2150
# Note: The following line may raise KeyError in the event of
2151
# truncated history. We decided not to have a try:except:raise
2152
# RevisionNotPresent here until we see a use for it, because of the
2153
# cost in an inner loop that is by its very nature O(history).
2154
# Robert Collins 20080326
2155
parents = graph.get_parent_map([next_id])[next_id]
2156
if len(parents) == 0:
2159
next_id = parents[0]
2162
def get_revision_inventory(self, revision_id):
2163
"""Return inventory of a past revision."""
2164
# TODO: Unify this with get_inventory()
2165
# bzr 0.0.6 and later imposes the constraint that the inventory_id
2166
# must be the same as its revision, so this is trivial.
2167
if revision_id is None:
2168
# This does not make sense: if there is no revision,
2169
# then it is the current tree inventory surely ?!
2170
# and thus get_root_id() is something that looks at the last
2171
# commit on the branch, and the get_root_id is an inventory check.
2172
raise NotImplementedError
2173
# return Inventory(self.get_root_id())
2175
return self.get_inventory(revision_id)
2177
def is_shared(self):
2178
"""Return True if this repository is flagged as a shared repository."""
2179
raise NotImplementedError(self.is_shared)
2182
def reconcile(self, other=None, thorough=False):
2183
"""Reconcile this repository."""
2184
from bzrlib.reconcile import RepoReconciler
2185
reconciler = RepoReconciler(self, thorough=thorough)
2186
reconciler.reconcile()
2189
def _refresh_data(self):
2190
"""Helper called from lock_* to ensure coherency with disk.
2192
The default implementation does nothing; it is however possible
2193
for repositories to maintain loaded indices across multiple locks
2194
by checking inside their implementation of this method to see
2195
whether their indices are still valid. This depends of course on
2196
the disk format being validatable in this manner. This method is
2197
also called by the refresh_data() public interface to cause a refresh
2198
to occur while in a write lock so that data inserted by a smart server
2199
push operation is visible on the client's instance of the physical
2204
def revision_tree(self, revision_id):
2205
"""Return Tree for a revision on this branch.
2207
`revision_id` may be NULL_REVISION for the empty tree revision.
2209
revision_id = _mod_revision.ensure_null(revision_id)
2210
# TODO: refactor this to use an existing revision object
2211
# so we don't need to read it in twice.
2212
if revision_id == _mod_revision.NULL_REVISION:
2213
return RevisionTree(self, Inventory(root_id=None),
2214
_mod_revision.NULL_REVISION)
2216
inv = self.get_revision_inventory(revision_id)
2217
return RevisionTree(self, inv, revision_id)
2219
def revision_trees(self, revision_ids):
2220
"""Return Trees for revisions in this repository.
2222
:param revision_ids: a sequence of revision-ids;
2223
a revision-id may not be None or 'null:'
2225
inventories = self.iter_inventories(revision_ids)
2226
for inv in inventories:
2227
yield RevisionTree(self, inv, inv.revision_id)
2229
def _filtered_revision_trees(self, revision_ids, file_ids):
2230
"""Return Tree for a revision on this branch with only some files.
2232
:param revision_ids: a sequence of revision-ids;
2233
a revision-id may not be None or 'null:'
2234
:param file_ids: if not None, the result is filtered
2235
so that only those file-ids, their parents and their
2236
children are included.
2238
inventories = self.iter_inventories(revision_ids)
2239
for inv in inventories:
2240
# Should we introduce a FilteredRevisionTree class rather
2241
# than pre-filter the inventory here?
2242
filtered_inv = inv.filter(file_ids)
2243
yield RevisionTree(self, filtered_inv, filtered_inv.revision_id)
2246
def get_ancestry(self, revision_id, topo_sorted=True):
2247
"""Return a list of revision-ids integrated by a revision.
2249
The first element of the list is always None, indicating the origin
2250
revision. This might change when we have history horizons, or
2251
perhaps we should have a new API.
2253
This is topologically sorted.
2255
if _mod_revision.is_null(revision_id):
2257
if not self.has_revision(revision_id):
2258
raise errors.NoSuchRevision(self, revision_id)
2259
graph = self.get_graph()
2261
search = graph._make_breadth_first_searcher([revision_id])
2264
found, ghosts = search.next_with_ghosts()
2265
except StopIteration:
2268
if _mod_revision.NULL_REVISION in keys:
2269
keys.remove(_mod_revision.NULL_REVISION)
2271
parent_map = graph.get_parent_map(keys)
2272
keys = tsort.topo_sort(parent_map)
2273
return [None] + list(keys)
2276
"""Compress the data within the repository.
2278
This operation only makes sense for some repository types. For other
2279
types it should be a no-op that just returns.
2281
This stub method does not require a lock, but subclasses should use
2282
@needs_write_lock as this is a long running call its reasonable to
2283
implicitly lock for the user.
2286
def get_transaction(self):
2287
return self.control_files.get_transaction()
2289
def get_parent_map(self, revision_ids):
2290
"""See graph._StackedParentsProvider.get_parent_map"""
2291
# revisions index works in keys; this just works in revisions
2292
# therefore wrap and unwrap
2295
for revision_id in revision_ids:
2296
if revision_id == _mod_revision.NULL_REVISION:
2297
result[revision_id] = ()
2298
elif revision_id is None:
2299
raise ValueError('get_parent_map(None) is not valid')
2301
query_keys.append((revision_id ,))
2302
for ((revision_id,), parent_keys) in \
2303
self.revisions.get_parent_map(query_keys).iteritems():
2305
result[revision_id] = tuple(parent_revid
2306
for (parent_revid,) in parent_keys)
2308
result[revision_id] = (_mod_revision.NULL_REVISION,)
2311
def _make_parents_provider(self):
2314
def get_graph(self, other_repository=None):
2315
"""Return the graph walker for this repository format"""
2316
parents_provider = self._make_parents_provider()
2317
if (other_repository is not None and
2318
not self.has_same_location(other_repository)):
2319
parents_provider = graph._StackedParentsProvider(
2320
[parents_provider, other_repository._make_parents_provider()])
2321
return graph.Graph(parents_provider)
2323
def _get_versioned_file_checker(self, text_key_references=None):
2324
"""Return an object suitable for checking versioned files.
2326
:param text_key_references: if non-None, an already built
2327
dictionary mapping text keys ((fileid, revision_id) tuples)
2328
to whether they were referred to by the inventory of the
2329
revision_id that they contain. If None, this will be
2332
return _VersionedFileChecker(self,
2333
text_key_references=text_key_references)
2335
def revision_ids_to_search_result(self, result_set):
2336
"""Convert a set of revision ids to a graph SearchResult."""
2337
result_parents = set()
2338
for parents in self.get_graph().get_parent_map(
2339
result_set).itervalues():
2340
result_parents.update(parents)
2341
included_keys = result_set.intersection(result_parents)
2342
start_keys = result_set.difference(included_keys)
2343
exclude_keys = result_parents.difference(result_set)
2344
result = graph.SearchResult(start_keys, exclude_keys,
2345
len(result_set), result_set)
2349
def set_make_working_trees(self, new_value):
2350
"""Set the policy flag for making working trees when creating branches.
2352
This only applies to branches that use this repository.
2354
The default is 'True'.
2355
:param new_value: True to restore the default, False to disable making
2358
raise NotImplementedError(self.set_make_working_trees)
2360
def make_working_trees(self):
2361
"""Returns the policy for making working trees on new branches."""
2362
raise NotImplementedError(self.make_working_trees)
2365
def sign_revision(self, revision_id, gpg_strategy):
2366
plaintext = Testament.from_revision(self, revision_id).as_short_text()
2367
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
2370
def has_signature_for_revision_id(self, revision_id):
2371
"""Query for a revision signature for revision_id in the repository."""
2372
if not self.has_revision(revision_id):
2373
raise errors.NoSuchRevision(self, revision_id)
2374
sig_present = (1 == len(
2375
self.signatures.get_parent_map([(revision_id,)])))
2379
def get_signature_text(self, revision_id):
2380
"""Return the text for a signature."""
2381
stream = self.signatures.get_record_stream([(revision_id,)],
2383
record = stream.next()
2384
if record.storage_kind == 'absent':
2385
raise errors.NoSuchRevision(self, revision_id)
2386
return record.get_bytes_as('fulltext')
2389
def check(self, revision_ids=None):
2390
"""Check consistency of all history of given revision_ids.
2392
Different repository implementations should override _check().
2394
:param revision_ids: A non-empty list of revision_ids whose ancestry
2395
will be checked. Typically the last revision_id of a branch.
2397
return self._check(revision_ids)
2399
def _check(self, revision_ids):
2400
result = check.Check(self)
2404
def _warn_if_deprecated(self):
2405
global _deprecation_warning_done
2406
if _deprecation_warning_done:
2408
_deprecation_warning_done = True
2409
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
2410
% (self._format, self.bzrdir.transport.base))
2412
def supports_rich_root(self):
2413
return self._format.rich_root_data
2415
def _check_ascii_revisionid(self, revision_id, method):
2416
"""Private helper for ascii-only repositories."""
2417
# weave repositories refuse to store revisionids that are non-ascii.
2418
if revision_id is not None:
2419
# weaves require ascii revision ids.
2420
if isinstance(revision_id, unicode):
2422
revision_id.encode('ascii')
2423
except UnicodeEncodeError:
2424
raise errors.NonAsciiRevisionId(method, self)
2427
revision_id.decode('ascii')
2428
except UnicodeDecodeError:
2429
raise errors.NonAsciiRevisionId(method, self)
2431
def revision_graph_can_have_wrong_parents(self):
2432
"""Is it possible for this repository to have a revision graph with
2435
If True, then this repository must also implement
2436
_find_inconsistent_revision_parents so that check and reconcile can
2437
check for inconsistencies before proceeding with other checks that may
2438
depend on the revision index being consistent.
2440
raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
2443
# remove these delegates a while after bzr 0.15
2444
def __make_delegated(name, from_module):
2445
def _deprecated_repository_forwarder():
2446
symbol_versioning.warn('%s moved to %s in bzr 0.15'
2447
% (name, from_module),
2450
m = __import__(from_module, globals(), locals(), [name])
2452
return getattr(m, name)
2453
except AttributeError:
2454
raise AttributeError('module %s has no name %s'
2456
globals()[name] = _deprecated_repository_forwarder
2459
'AllInOneRepository',
2460
'WeaveMetaDirRepository',
2461
'PreSplitOutRepositoryFormat',
2462
'RepositoryFormat4',
2463
'RepositoryFormat5',
2464
'RepositoryFormat6',
2465
'RepositoryFormat7',
2467
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
2471
'RepositoryFormatKnit',
2472
'RepositoryFormatKnit1',
2474
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
2477
def install_revision(repository, rev, revision_tree):
2478
"""Install all revision data into a repository."""
2479
install_revisions(repository, [(rev, revision_tree, None)])
2482
def install_revisions(repository, iterable, num_revisions=None, pb=None):
2483
"""Install all revision data into a repository.
2485
Accepts an iterable of revision, tree, signature tuples. The signature
2488
repository.start_write_group()
2490
for n, (revision, revision_tree, signature) in enumerate(iterable):
2491
_install_revision(repository, revision, revision_tree, signature)
2493
pb.update('Transferring revisions', n + 1, num_revisions)
2495
repository.abort_write_group()
2498
repository.commit_write_group()
2501
def _install_revision(repository, rev, revision_tree, signature):
2502
"""Install all revision data into a repository."""
2503
present_parents = []
2505
for p_id in rev.parent_ids:
2506
if repository.has_revision(p_id):
2507
present_parents.append(p_id)
2508
parent_trees[p_id] = repository.revision_tree(p_id)
2510
parent_trees[p_id] = repository.revision_tree(
2511
_mod_revision.NULL_REVISION)
2513
inv = revision_tree.inventory
2514
entries = inv.iter_entries()
2515
# backwards compatibility hack: skip the root id.
2516
if not repository.supports_rich_root():
2517
path, root = entries.next()
2518
if root.revision != rev.revision_id:
2519
raise errors.IncompatibleRevision(repr(repository))
2521
for path, ie in entries:
2522
text_keys[(ie.file_id, ie.revision)] = ie
2523
text_parent_map = repository.texts.get_parent_map(text_keys)
2524
missing_texts = set(text_keys) - set(text_parent_map)
2525
# Add the texts that are not already present
2526
for text_key in missing_texts:
2527
ie = text_keys[text_key]
2529
# FIXME: TODO: The following loop overlaps/duplicates that done by
2530
# commit to determine parents. There is a latent/real bug here where
2531
# the parents inserted are not those commit would do - in particular
2532
# they are not filtered by heads(). RBC, AB
2533
for revision, tree in parent_trees.iteritems():
2534
if ie.file_id not in tree:
2536
parent_id = tree.inventory[ie.file_id].revision
2537
if parent_id in text_parents:
2539
text_parents.append((ie.file_id, parent_id))
2540
lines = revision_tree.get_file(ie.file_id).readlines()
2541
repository.texts.add_lines(text_key, text_parents, lines)
2543
# install the inventory
2544
repository.add_inventory(rev.revision_id, inv, present_parents)
2545
except errors.RevisionAlreadyPresent:
2547
if signature is not None:
2548
repository.add_signature_text(rev.revision_id, signature)
2549
repository.add_revision(rev.revision_id, rev, inv)
2552
class MetaDirRepository(Repository):
2553
"""Repositories in the new meta-dir layout.
2555
:ivar _transport: Transport for access to repository control files,
2556
typically pointing to .bzr/repository.
2559
def __init__(self, _format, a_bzrdir, control_files):
2560
super(MetaDirRepository, self).__init__(_format, a_bzrdir, control_files)
2561
self._transport = control_files._transport
2563
def is_shared(self):
2564
"""Return True if this repository is flagged as a shared repository."""
2565
return self._transport.has('shared-storage')
2568
def set_make_working_trees(self, new_value):
2569
"""Set the policy flag for making working trees when creating branches.
2571
This only applies to branches that use this repository.
2573
The default is 'True'.
2574
:param new_value: True to restore the default, False to disable making
2579
self._transport.delete('no-working-trees')
2580
except errors.NoSuchFile:
2583
self._transport.put_bytes('no-working-trees', '',
2584
mode=self.bzrdir._get_file_mode())
2586
def make_working_trees(self):
2587
"""Returns the policy for making working trees on new branches."""
2588
return not self._transport.has('no-working-trees')
2591
class MetaDirVersionedFileRepository(MetaDirRepository):
2592
"""Repositories in a meta-dir, that work via versioned file objects."""
2594
def __init__(self, _format, a_bzrdir, control_files):
2595
super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
2599
network_format_registry = registry.FormatRegistry()
2600
"""Registry of formats indexed by their network name.
2602
The network name for a repository format is an identifier that can be used when
2603
referring to formats with smart server operations. See
2604
RepositoryFormat.network_name() for more detail.
2608
format_registry = registry.FormatRegistry(network_format_registry)
2609
"""Registry of formats, indexed by their BzrDirMetaFormat format string.
2611
This can contain either format instances themselves, or classes/factories that
2612
can be called to obtain one.
2616
#####################################################################
2617
# Repository Formats
2619
class RepositoryFormat(object):
2620
"""A repository format.
2622
Formats provide four things:
2623
* An initialization routine to construct repository data on disk.
2624
* a optional format string which is used when the BzrDir supports
2626
* an open routine which returns a Repository instance.
2627
* A network name for referring to the format in smart server RPC
2630
There is one and only one Format subclass for each on-disk format. But
2631
there can be one Repository subclass that is used for several different
2632
formats. The _format attribute on a Repository instance can be used to
2633
determine the disk format.
2635
Formats are placed in a registry by their format string for reference
2636
during opening. These should be subclasses of RepositoryFormat for
2639
Once a format is deprecated, just deprecate the initialize and open
2640
methods on the format class. Do not deprecate the object, as the
2641
object may be created even when a repository instnace hasn't been
2644
Common instance attributes:
2645
_matchingbzrdir - the bzrdir format that the repository format was
2646
originally written to work with. This can be used if manually
2647
constructing a bzrdir and repository, or more commonly for test suite
2651
# Set to True or False in derived classes. True indicates that the format
2652
# supports ghosts gracefully.
2653
supports_ghosts = None
2654
# Can this repository be given external locations to lookup additional
2655
# data. Set to True or False in derived classes.
2656
supports_external_lookups = None
2657
# What order should fetch operations request streams in?
2658
# The default is unordered as that is the cheapest for an origin to
2660
_fetch_order = 'unordered'
2661
# Does this repository format use deltas that can be fetched as-deltas ?
2662
# (E.g. knits, where the knit deltas can be transplanted intact.
2663
# We default to False, which will ensure that enough data to get
2664
# a full text out of any fetch stream will be grabbed.
2665
_fetch_uses_deltas = False
2666
# Should fetch trigger a reconcile after the fetch? Only needed for
2667
# some repository formats that can suffer internal inconsistencies.
2668
_fetch_reconcile = False
2669
# Does this format have < O(tree_size) delta generation. Used to hint what
2670
# code path for commit, amongst other things.
2674
return "<%s>" % self.__class__.__name__
2676
def __eq__(self, other):
2677
# format objects are generally stateless
2678
return isinstance(other, self.__class__)
2680
def __ne__(self, other):
2681
return not self == other
2684
def find_format(klass, a_bzrdir):
2685
"""Return the format for the repository object in a_bzrdir.
2687
This is used by bzr native formats that have a "format" file in
2688
the repository. Other methods may be used by different types of
2692
transport = a_bzrdir.get_repository_transport(None)
2693
format_string = transport.get("format").read()
2694
return format_registry.get(format_string)
2695
except errors.NoSuchFile:
2696
raise errors.NoRepositoryPresent(a_bzrdir)
2698
raise errors.UnknownFormatError(format=format_string,
2702
def register_format(klass, format):
2703
format_registry.register(format.get_format_string(), format)
2706
def unregister_format(klass, format):
2707
format_registry.remove(format.get_format_string())
2710
def get_default_format(klass):
2711
"""Return the current default format."""
2712
from bzrlib import bzrdir
2713
return bzrdir.format_registry.make_bzrdir('default').repository_format
2715
def get_format_string(self):
2716
"""Return the ASCII format string that identifies this format.
2718
Note that in pre format ?? repositories the format string is
2719
not permitted nor written to disk.
2721
raise NotImplementedError(self.get_format_string)
2723
def get_format_description(self):
2724
"""Return the short description for this format."""
2725
raise NotImplementedError(self.get_format_description)
2727
# TODO: this shouldn't be in the base class, it's specific to things that
2728
# use weaves or knits -- mbp 20070207
2729
def _get_versioned_file_store(self,
2734
versionedfile_class=None,
2735
versionedfile_kwargs={},
2737
if versionedfile_class is None:
2738
versionedfile_class = self._versionedfile_class
2739
weave_transport = control_files._transport.clone(name)
2740
dir_mode = control_files._dir_mode
2741
file_mode = control_files._file_mode
2742
return VersionedFileStore(weave_transport, prefixed=prefixed,
2744
file_mode=file_mode,
2745
versionedfile_class=versionedfile_class,
2746
versionedfile_kwargs=versionedfile_kwargs,
2749
def initialize(self, a_bzrdir, shared=False):
2750
"""Initialize a repository of this format in a_bzrdir.
2752
:param a_bzrdir: The bzrdir to put the new repository in it.
2753
:param shared: The repository should be initialized as a sharable one.
2754
:returns: The new repository object.
2756
This may raise UninitializableFormat if shared repository are not
2757
compatible the a_bzrdir.
2759
raise NotImplementedError(self.initialize)
2761
def is_supported(self):
2762
"""Is this format supported?
2764
Supported formats must be initializable and openable.
2765
Unsupported formats may not support initialization or committing or
2766
some other features depending on the reason for not being supported.
2770
def network_name(self):
2771
"""A simple byte string uniquely identifying this format for RPC calls.
2773
MetaDir repository formats use their disk format string to identify the
2774
repository over the wire. All in one formats such as bzr < 0.8, and
2775
foreign formats like svn/git and hg should use some marker which is
2776
unique and immutable.
2778
raise NotImplementedError(self.network_name)
2780
def check_conversion_target(self, target_format):
2781
raise NotImplementedError(self.check_conversion_target)
2783
def open(self, a_bzrdir, _found=False):
2784
"""Return an instance of this format for the bzrdir a_bzrdir.
2786
_found is a private parameter, do not use it.
2788
raise NotImplementedError(self.open)
2791
class MetaDirRepositoryFormat(RepositoryFormat):
2792
"""Common base class for the new repositories using the metadir layout."""
2794
rich_root_data = False
2795
supports_tree_reference = False
2796
supports_external_lookups = False
2799
def _matchingbzrdir(self):
2800
matching = bzrdir.BzrDirMetaFormat1()
2801
matching.repository_format = self
2805
super(MetaDirRepositoryFormat, self).__init__()
2807
def _create_control_files(self, a_bzrdir):
2808
"""Create the required files and the initial control_files object."""
2809
# FIXME: RBC 20060125 don't peek under the covers
2810
# NB: no need to escape relative paths that are url safe.
2811
repository_transport = a_bzrdir.get_repository_transport(self)
2812
control_files = lockable_files.LockableFiles(repository_transport,
2813
'lock', lockdir.LockDir)
2814
control_files.create_lock()
2815
return control_files
2817
def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
2818
"""Upload the initial blank content."""
2819
control_files = self._create_control_files(a_bzrdir)
2820
control_files.lock_write()
2821
transport = control_files._transport
2823
utf8_files += [('shared-storage', '')]
2825
transport.mkdir_multi(dirs, mode=a_bzrdir._get_dir_mode())
2826
for (filename, content_stream) in files:
2827
transport.put_file(filename, content_stream,
2828
mode=a_bzrdir._get_file_mode())
2829
for (filename, content_bytes) in utf8_files:
2830
transport.put_bytes_non_atomic(filename, content_bytes,
2831
mode=a_bzrdir._get_file_mode())
2833
control_files.unlock()
2835
def network_name(self):
2836
"""Metadir formats have matching disk and network format strings."""
2837
return self.get_format_string()
2840
# Pre-0.8 formats that don't have a disk format string (because they are
2841
# versioned by the matching control directory). We use the control directories
2842
# disk format string as a key for the network_name because they meet the
2843
# constraints (simple string, unique, immmutable).
2844
network_format_registry.register_lazy(
2845
"Bazaar-NG branch, format 5\n",
2846
'bzrlib.repofmt.weaverepo',
2847
'RepositoryFormat5',
2849
network_format_registry.register_lazy(
2850
"Bazaar-NG branch, format 6\n",
2851
'bzrlib.repofmt.weaverepo',
2852
'RepositoryFormat6',
2855
# formats which have no format string are not discoverable or independently
2856
# creatable on disk, so are not registered in format_registry. They're
2857
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
2858
# needed, it's constructed directly by the BzrDir. Non-native formats where
2859
# the repository is not separately opened are similar.
2861
format_registry.register_lazy(
2862
'Bazaar-NG Repository format 7',
2863
'bzrlib.repofmt.weaverepo',
2867
format_registry.register_lazy(
2868
'Bazaar-NG Knit Repository Format 1',
2869
'bzrlib.repofmt.knitrepo',
2870
'RepositoryFormatKnit1',
2873
format_registry.register_lazy(
2874
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
2875
'bzrlib.repofmt.knitrepo',
2876
'RepositoryFormatKnit3',
2879
format_registry.register_lazy(
2880
'Bazaar Knit Repository Format 4 (bzr 1.0)\n',
2881
'bzrlib.repofmt.knitrepo',
2882
'RepositoryFormatKnit4',
2885
# Pack-based formats. There is one format for pre-subtrees, and one for
2886
# post-subtrees to allow ease of testing.
2887
# NOTE: These are experimental in 0.92. Stable in 1.0 and above
2888
format_registry.register_lazy(
2889
'Bazaar pack repository format 1 (needs bzr 0.92)\n',
2890
'bzrlib.repofmt.pack_repo',
2891
'RepositoryFormatKnitPack1',
2893
format_registry.register_lazy(
2894
'Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n',
2895
'bzrlib.repofmt.pack_repo',
2896
'RepositoryFormatKnitPack3',
2898
format_registry.register_lazy(
2899
'Bazaar pack repository format 1 with rich root (needs bzr 1.0)\n',
2900
'bzrlib.repofmt.pack_repo',
2901
'RepositoryFormatKnitPack4',
2903
format_registry.register_lazy(
2904
'Bazaar RepositoryFormatKnitPack5 (bzr 1.6)\n',
2905
'bzrlib.repofmt.pack_repo',
2906
'RepositoryFormatKnitPack5',
2908
format_registry.register_lazy(
2909
'Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6.1)\n',
2910
'bzrlib.repofmt.pack_repo',
2911
'RepositoryFormatKnitPack5RichRoot',
2913
format_registry.register_lazy(
2914
'Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6)\n',
2915
'bzrlib.repofmt.pack_repo',
2916
'RepositoryFormatKnitPack5RichRootBroken',
2918
format_registry.register_lazy(
2919
'Bazaar RepositoryFormatKnitPack6 (bzr 1.9)\n',
2920
'bzrlib.repofmt.pack_repo',
2921
'RepositoryFormatKnitPack6',
2923
format_registry.register_lazy(
2924
'Bazaar RepositoryFormatKnitPack6RichRoot (bzr 1.9)\n',
2925
'bzrlib.repofmt.pack_repo',
2926
'RepositoryFormatKnitPack6RichRoot',
2929
# Development formats.
2930
# 1.7->1.8 go below here
2931
format_registry.register_lazy(
2932
"Bazaar development format 2 (needs bzr.dev from before 1.8)\n",
2933
'bzrlib.repofmt.pack_repo',
2934
'RepositoryFormatPackDevelopment2',
2936
format_registry.register_lazy(
2937
("Bazaar development format 2 with subtree support "
2938
"(needs bzr.dev from before 1.8)\n"),
2939
'bzrlib.repofmt.pack_repo',
2940
'RepositoryFormatPackDevelopment2Subtree',
2944
class InterRepository(InterObject):
2945
"""This class represents operations taking place between two repositories.
2947
Its instances have methods like copy_content and fetch, and contain
2948
references to the source and target repositories these operations can be
2951
Often we will provide convenience methods on 'repository' which carry out
2952
operations with another repository - they will always forward to
2953
InterRepository.get(other).method_name(parameters).
2956
_walk_to_common_revisions_batch_size = 50
2958
"""The available optimised InterRepository types."""
2961
def copy_content(self, revision_id=None):
2962
"""Make a complete copy of the content in self into destination.
2964
This is a destructive operation! Do not use it on existing
2967
:param revision_id: Only copy the content needed to construct
2968
revision_id and its parents.
2971
self.target.set_make_working_trees(self.source.make_working_trees())
2972
except NotImplementedError:
2974
self.target.fetch(self.source, revision_id=revision_id)
2977
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
2979
"""Fetch the content required to construct revision_id.
2981
The content is copied from self.source to self.target.
2983
:param revision_id: if None all content is copied, if NULL_REVISION no
2985
:param pb: optional progress bar to use for progress reports. If not
2986
provided a default one will be created.
2989
from bzrlib.fetch import RepoFetcher
2990
f = RepoFetcher(to_repository=self.target,
2991
from_repository=self.source,
2992
last_revision=revision_id,
2993
fetch_spec=fetch_spec,
2994
pb=pb, find_ghosts=find_ghosts)
2996
def _walk_to_common_revisions(self, revision_ids):
2997
"""Walk out from revision_ids in source to revisions target has.
2999
:param revision_ids: The start point for the search.
3000
:return: A set of revision ids.
3002
target_graph = self.target.get_graph()
3003
revision_ids = frozenset(revision_ids)
3004
# Fast path for the case where all the revisions are already in the
3006
# (Although this does incur an extra round trip for the
3007
# fairly common case where the target doesn't already have the revision
3009
if set(target_graph.get_parent_map(revision_ids)) == revision_ids:
3010
return graph.SearchResult(revision_ids, set(), 0, set())
3011
missing_revs = set()
3012
source_graph = self.source.get_graph()
3013
# ensure we don't pay silly lookup costs.
3014
searcher = source_graph._make_breadth_first_searcher(revision_ids)
3015
null_set = frozenset([_mod_revision.NULL_REVISION])
3016
searcher_exhausted = False
3020
# Iterate the searcher until we have enough next_revs
3021
while len(next_revs) < self._walk_to_common_revisions_batch_size:
3023
next_revs_part, ghosts_part = searcher.next_with_ghosts()
3024
next_revs.update(next_revs_part)
3025
ghosts.update(ghosts_part)
3026
except StopIteration:
3027
searcher_exhausted = True
3029
# If there are ghosts in the source graph, and the caller asked for
3030
# them, make sure that they are present in the target.
3031
# We don't care about other ghosts as we can't fetch them and
3032
# haven't been asked to.
3033
ghosts_to_check = set(revision_ids.intersection(ghosts))
3034
revs_to_get = set(next_revs).union(ghosts_to_check)
3036
have_revs = set(target_graph.get_parent_map(revs_to_get))
3037
# we always have NULL_REVISION present.
3038
have_revs = have_revs.union(null_set)
3039
# Check if the target is missing any ghosts we need.
3040
ghosts_to_check.difference_update(have_revs)
3042
# One of the caller's revision_ids is a ghost in both the
3043
# source and the target.
3044
raise errors.NoSuchRevision(
3045
self.source, ghosts_to_check.pop())
3046
missing_revs.update(next_revs - have_revs)
3047
# Because we may have walked past the original stop point, make
3048
# sure everything is stopped
3049
stop_revs = searcher.find_seen_ancestors(have_revs)
3050
searcher.stop_searching_any(stop_revs)
3051
if searcher_exhausted:
3053
return searcher.get_result()
3056
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
3057
"""Return the revision ids that source has that target does not.
3059
:param revision_id: only return revision ids included by this
3061
:param find_ghosts: If True find missing revisions in deep history
3062
rather than just finding the surface difference.
3063
:return: A bzrlib.graph.SearchResult.
3065
# stop searching at found target revisions.
3066
if not find_ghosts and revision_id is not None:
3067
return self._walk_to_common_revisions([revision_id])
3068
# generic, possibly worst case, slow code path.
3069
target_ids = set(self.target.all_revision_ids())
3070
if revision_id is not None:
3071
source_ids = self.source.get_ancestry(revision_id)
3072
if source_ids[0] is not None:
3073
raise AssertionError()
3076
source_ids = self.source.all_revision_ids()
3077
result_set = set(source_ids).difference(target_ids)
3078
return self.source.revision_ids_to_search_result(result_set)
3081
def _same_model(source, target):
3082
"""True if source and target have the same data representation.
3084
Note: this is always called on the base class; overriding it in a
3085
subclass will have no effect.
3088
InterRepository._assert_same_model(source, target)
3090
except errors.IncompatibleRepositories, e:
3094
def _assert_same_model(source, target):
3095
"""Raise an exception if two repositories do not use the same model.
3097
if source.supports_rich_root() != target.supports_rich_root():
3098
raise errors.IncompatibleRepositories(source, target,
3099
"different rich-root support")
3100
if source._serializer != target._serializer:
3101
raise errors.IncompatibleRepositories(source, target,
3102
"different serializers")
3105
class InterSameDataRepository(InterRepository):
3106
"""Code for converting between repositories that represent the same data.
3108
Data format and model must match for this to work.
3112
def _get_repo_format_to_test(self):
3113
"""Repository format for testing with.
3115
InterSameData can pull from subtree to subtree and from non-subtree to
3116
non-subtree, so we test this with the richest repository format.
3118
from bzrlib.repofmt import knitrepo
3119
return knitrepo.RepositoryFormatKnit3()
3122
def is_compatible(source, target):
3123
return InterRepository._same_model(source, target)
3126
class InterWeaveRepo(InterSameDataRepository):
3127
"""Optimised code paths between Weave based repositories.
3129
This should be in bzrlib/repofmt/weaverepo.py but we have not yet
3130
implemented lazy inter-object optimisation.
3134
def _get_repo_format_to_test(self):
3135
from bzrlib.repofmt import weaverepo
3136
return weaverepo.RepositoryFormat7()
3139
def is_compatible(source, target):
3140
"""Be compatible with known Weave formats.
3142
We don't test for the stores being of specific types because that
3143
could lead to confusing results, and there is no need to be
3146
from bzrlib.repofmt.weaverepo import (
3152
return (isinstance(source._format, (RepositoryFormat5,
3154
RepositoryFormat7)) and
3155
isinstance(target._format, (RepositoryFormat5,
3157
RepositoryFormat7)))
3158
except AttributeError:
3162
def copy_content(self, revision_id=None):
3163
"""See InterRepository.copy_content()."""
3164
# weave specific optimised path:
3166
self.target.set_make_working_trees(self.source.make_working_trees())
3167
except (errors.RepositoryUpgradeRequired, NotImplemented):
3169
# FIXME do not peek!
3170
if self.source._transport.listable():
3171
pb = ui.ui_factory.nested_progress_bar()
3173
self.target.texts.insert_record_stream(
3174
self.source.texts.get_record_stream(
3175
self.source.texts.keys(), 'topological', False))
3176
pb.update('copying inventory', 0, 1)
3177
self.target.inventories.insert_record_stream(
3178
self.source.inventories.get_record_stream(
3179
self.source.inventories.keys(), 'topological', False))
3180
self.target.signatures.insert_record_stream(
3181
self.source.signatures.get_record_stream(
3182
self.source.signatures.keys(),
3184
self.target.revisions.insert_record_stream(
3185
self.source.revisions.get_record_stream(
3186
self.source.revisions.keys(),
3187
'topological', True))
3191
self.target.fetch(self.source, revision_id=revision_id)
3194
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
3195
"""See InterRepository.missing_revision_ids()."""
3196
# we want all revisions to satisfy revision_id in source.
3197
# but we don't want to stat every file here and there.
3198
# we want then, all revisions other needs to satisfy revision_id
3199
# checked, but not those that we have locally.
3200
# so the first thing is to get a subset of the revisions to
3201
# satisfy revision_id in source, and then eliminate those that
3202
# we do already have.
3203
# this is slow on high latency connection to self, but as as this
3204
# disk format scales terribly for push anyway due to rewriting
3205
# inventory.weave, this is considered acceptable.
3207
if revision_id is not None:
3208
source_ids = self.source.get_ancestry(revision_id)
3209
if source_ids[0] is not None:
3210
raise AssertionError()
3213
source_ids = self.source._all_possible_ids()
3214
source_ids_set = set(source_ids)
3215
# source_ids is the worst possible case we may need to pull.
3216
# now we want to filter source_ids against what we actually
3217
# have in target, but don't try to check for existence where we know
3218
# we do not have a revision as that would be pointless.
3219
target_ids = set(self.target._all_possible_ids())
3220
possibly_present_revisions = target_ids.intersection(source_ids_set)
3221
actually_present_revisions = set(
3222
self.target._eliminate_revisions_not_present(possibly_present_revisions))
3223
required_revisions = source_ids_set.difference(actually_present_revisions)
3224
if revision_id is not None:
3225
# we used get_ancestry to determine source_ids then we are assured all
3226
# revisions referenced are present as they are installed in topological order.
3227
# and the tip revision was validated by get_ancestry.
3228
result_set = required_revisions
3230
# if we just grabbed the possibly available ids, then
3231
# we only have an estimate of whats available and need to validate
3232
# that against the revision records.
3234
self.source._eliminate_revisions_not_present(required_revisions))
3235
return self.source.revision_ids_to_search_result(result_set)
3238
class InterKnitRepo(InterSameDataRepository):
3239
"""Optimised code paths between Knit based repositories."""
3242
def _get_repo_format_to_test(self):
3243
from bzrlib.repofmt import knitrepo
3244
return knitrepo.RepositoryFormatKnit1()
3247
def is_compatible(source, target):
3248
"""Be compatible with known Knit formats.
3250
We don't test for the stores being of specific types because that
3251
could lead to confusing results, and there is no need to be
3254
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
3256
are_knits = (isinstance(source._format, RepositoryFormatKnit) and
3257
isinstance(target._format, RepositoryFormatKnit))
3258
except AttributeError:
3260
return are_knits and InterRepository._same_model(source, target)
3263
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
3264
"""See InterRepository.missing_revision_ids()."""
3265
if revision_id is not None:
3266
source_ids = self.source.get_ancestry(revision_id)
3267
if source_ids[0] is not None:
3268
raise AssertionError()
3271
source_ids = self.source.all_revision_ids()
3272
source_ids_set = set(source_ids)
3273
# source_ids is the worst possible case we may need to pull.
3274
# now we want to filter source_ids against what we actually
3275
# have in target, but don't try to check for existence where we know
3276
# we do not have a revision as that would be pointless.
3277
target_ids = set(self.target.all_revision_ids())
3278
possibly_present_revisions = target_ids.intersection(source_ids_set)
3279
actually_present_revisions = set(
3280
self.target._eliminate_revisions_not_present(possibly_present_revisions))
3281
required_revisions = source_ids_set.difference(actually_present_revisions)
3282
if revision_id is not None:
3283
# we used get_ancestry to determine source_ids then we are assured all
3284
# revisions referenced are present as they are installed in topological order.
3285
# and the tip revision was validated by get_ancestry.
3286
result_set = required_revisions
3288
# if we just grabbed the possibly available ids, then
3289
# we only have an estimate of whats available and need to validate
3290
# that against the revision records.
3292
self.source._eliminate_revisions_not_present(required_revisions))
3293
return self.source.revision_ids_to_search_result(result_set)
3296
class InterPackRepo(InterSameDataRepository):
3297
"""Optimised code paths between Pack based repositories."""
3300
def _get_repo_format_to_test(self):
3301
from bzrlib.repofmt import pack_repo
3302
return pack_repo.RepositoryFormatKnitPack1()
3305
def is_compatible(source, target):
3306
"""Be compatible with known Pack formats.
3308
We don't test for the stores being of specific types because that
3309
could lead to confusing results, and there is no need to be
3312
from bzrlib.repofmt.pack_repo import RepositoryFormatPack
3314
are_packs = (isinstance(source._format, RepositoryFormatPack) and
3315
isinstance(target._format, RepositoryFormatPack))
3316
except AttributeError:
3318
return are_packs and InterRepository._same_model(source, target)
3321
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
3323
"""See InterRepository.fetch()."""
3324
if (len(self.source._fallback_repositories) > 0 or
3325
len(self.target._fallback_repositories) > 0):
3326
# The pack layer is not aware of fallback repositories, so when
3327
# fetching from a stacked repository or into a stacked repository
3328
# we use the generic fetch logic which uses the VersionedFiles
3329
# attributes on repository.
3330
from bzrlib.fetch import RepoFetcher
3331
fetcher = RepoFetcher(self.target, self.source, revision_id,
3332
pb, find_ghosts, fetch_spec=fetch_spec)
3333
if fetch_spec is not None:
3334
if len(list(fetch_spec.heads)) != 1:
3335
raise AssertionError(
3336
"InterPackRepo.fetch doesn't support "
3337
"fetching multiple heads yet.")
3338
revision_id = list(fetch_spec.heads)[0]
3340
if revision_id is None:
3342
# everything to do - use pack logic
3343
# to fetch from all packs to one without
3344
# inventory parsing etc, IFF nothing to be copied is in the target.
3346
source_revision_ids = frozenset(self.source.all_revision_ids())
3347
revision_ids = source_revision_ids - \
3348
frozenset(self.target.get_parent_map(source_revision_ids))
3349
revision_keys = [(revid,) for revid in revision_ids]
3350
index = self.target._pack_collection.revision_index.combined_index
3351
present_revision_ids = set(item[1][0] for item in
3352
index.iter_entries(revision_keys))
3353
revision_ids = set(revision_ids) - present_revision_ids
3354
# implementing the TODO will involve:
3355
# - detecting when all of a pack is selected
3356
# - avoiding as much as possible pre-selection, so the
3357
# more-core routines such as create_pack_from_packs can filter in
3358
# a just-in-time fashion. (though having a HEADS list on a
3359
# repository might make this a lot easier, because we could
3360
# sensibly detect 'new revisions' without doing a full index scan.
3361
elif _mod_revision.is_null(revision_id):
3366
revision_ids = self.search_missing_revision_ids(revision_id,
3367
find_ghosts=find_ghosts).get_keys()
3368
except errors.NoSuchRevision:
3369
raise errors.InstallFailed([revision_id])
3370
if len(revision_ids) == 0:
3372
return self._pack(self.source, self.target, revision_ids)
3374
def _pack(self, source, target, revision_ids):
3375
from bzrlib.repofmt.pack_repo import Packer
3376
packs = source._pack_collection.all_packs()
3377
pack = Packer(self.target._pack_collection, packs, '.fetch',
3378
revision_ids).pack()
3379
if pack is not None:
3380
self.target._pack_collection._save_pack_names()
3381
copied_revs = pack.get_revision_count()
3382
# Trigger an autopack. This may duplicate effort as we've just done
3383
# a pack creation, but for now it is simpler to think about as
3384
# 'upload data, then repack if needed'.
3385
self.target._pack_collection.autopack()
3386
return (copied_revs, [])
3391
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
3392
"""See InterRepository.missing_revision_ids().
3394
:param find_ghosts: Find ghosts throughout the ancestry of
3397
if not find_ghosts and revision_id is not None:
3398
return self._walk_to_common_revisions([revision_id])
3399
elif revision_id is not None:
3400
# Find ghosts: search for revisions pointing from one repository to
3401
# the other, and vice versa, anywhere in the history of revision_id.
3402
graph = self.target.get_graph(other_repository=self.source)
3403
searcher = graph._make_breadth_first_searcher([revision_id])
3407
next_revs, ghosts = searcher.next_with_ghosts()
3408
except StopIteration:
3410
if revision_id in ghosts:
3411
raise errors.NoSuchRevision(self.source, revision_id)
3412
found_ids.update(next_revs)
3413
found_ids.update(ghosts)
3414
found_ids = frozenset(found_ids)
3415
# Double query here: should be able to avoid this by changing the
3416
# graph api further.
3417
result_set = found_ids - frozenset(
3418
self.target.get_parent_map(found_ids))
3420
source_ids = self.source.all_revision_ids()
3421
# source_ids is the worst possible case we may need to pull.
3422
# now we want to filter source_ids against what we actually
3423
# have in target, but don't try to check for existence where we know
3424
# we do not have a revision as that would be pointless.
3425
target_ids = set(self.target.all_revision_ids())
3426
result_set = set(source_ids).difference(target_ids)
3427
return self.source.revision_ids_to_search_result(result_set)
3430
class InterDifferingSerializer(InterKnitRepo):
3433
def _get_repo_format_to_test(self):
3437
def is_compatible(source, target):
3438
"""Be compatible with Knit2 source and Knit3 target"""
3439
if source.supports_rich_root() != target.supports_rich_root():
3441
# Ideally, we'd support fetching if the source had no tree references
3442
# even if it supported them...
3443
if (getattr(source, '_format.supports_tree_reference', False) and
3444
not getattr(target, '_format.supports_tree_reference', False)):
3448
def _get_delta_for_revision(self, tree, parent_ids, basis_id, cache):
3449
"""Get the best delta and base for this revision.
3451
:return: (basis_id, delta)
3453
possible_trees = [(parent_id, cache[parent_id])
3454
for parent_id in parent_ids
3455
if parent_id in cache]
3456
if len(possible_trees) == 0:
3457
# There either aren't any parents, or the parents aren't in the
3458
# cache, so just use the last converted tree
3459
possible_trees.append((basis_id, cache[basis_id]))
3461
for basis_id, basis_tree in possible_trees:
3462
delta = tree.inventory._make_delta(basis_tree.inventory)
3463
deltas.append((len(delta), basis_id, delta))
3465
return deltas[0][1:]
3467
def _fetch_batch(self, revision_ids, basis_id, cache):
3468
"""Fetch across a few revisions.
3470
:param revision_ids: The revisions to copy
3471
:param basis_id: The revision_id of a tree that must be in cache, used
3472
as a basis for delta when no other base is available
3473
:param cache: A cache of RevisionTrees that we can use.
3474
:return: The revision_id of the last converted tree. The RevisionTree
3475
for it will be in cache
3477
# Walk though all revisions; get inventory deltas, copy referenced
3478
# texts that delta references, insert the delta, revision and
3482
pending_revisions = []
3483
parent_map = self.source.get_parent_map(revision_ids)
3484
for tree in self.source.revision_trees(revision_ids):
3485
current_revision_id = tree.get_revision_id()
3486
parent_ids = parent_map.get(current_revision_id, ())
3487
basis_id, delta = self._get_delta_for_revision(tree, parent_ids,
3489
# Find text entries that need to be copied
3490
for old_path, new_path, file_id, entry in delta:
3491
if new_path is not None:
3492
if not (new_path or self.target.supports_rich_root()):
3493
# We don't copy the text for the root node unless the
3494
# target supports_rich_root.
3496
text_keys.add((file_id, entry.revision))
3497
revision = self.source.get_revision(current_revision_id)
3498
pending_deltas.append((basis_id, delta,
3499
current_revision_id, revision.parent_ids))
3500
pending_revisions.append(revision)
3501
cache[current_revision_id] = tree
3502
basis_id = current_revision_id
3504
from_texts = self.source.texts
3505
to_texts = self.target.texts
3506
to_texts.insert_record_stream(from_texts.get_record_stream(
3507
text_keys, self.target._format._fetch_order,
3508
not self.target._format._fetch_uses_deltas))
3510
for delta in pending_deltas:
3511
self.target.add_inventory_by_delta(*delta)
3512
# insert signatures and revisions
3513
for revision in pending_revisions:
3515
signature = self.source.get_signature_text(
3516
revision.revision_id)
3517
self.target.add_signature_text(revision.revision_id,
3519
except errors.NoSuchRevision:
3521
self.target.add_revision(revision.revision_id, revision)
3524
def _fetch_all_revisions(self, revision_ids, pb):
3525
"""Fetch everything for the list of revisions.
3527
:param revision_ids: The list of revisions to fetch. Must be in
3529
:param pb: A ProgressBar
3532
basis_id, basis_tree = self._get_basis(revision_ids[0])
3534
cache = lru_cache.LRUCache(100)
3535
cache[basis_id] = basis_tree
3536
del basis_tree # We don't want to hang on to it here
3537
for offset in range(0, len(revision_ids), batch_size):
3538
self.target.start_write_group()
3540
pb.update('Transferring revisions', offset,
3542
batch = revision_ids[offset:offset+batch_size]
3543
basis_id = self._fetch_batch(batch, basis_id, cache)
3545
self.target.abort_write_group()
3548
self.target.commit_write_group()
3549
pb.update('Transferring revisions', len(revision_ids),
3553
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
3555
"""See InterRepository.fetch()."""
3556
if fetch_spec is not None:
3557
raise AssertionError("Not implemented yet...")
3558
revision_ids = self.target.search_missing_revision_ids(self.source,
3559
revision_id, find_ghosts=find_ghosts).get_keys()
3560
if not revision_ids:
3562
revision_ids = tsort.topo_sort(
3563
self.source.get_graph().get_parent_map(revision_ids))
3565
my_pb = ui.ui_factory.nested_progress_bar()
3568
symbol_versioning.warn(
3569
symbol_versioning.deprecated_in((1, 14, 0))
3570
% "pb parameter to fetch()")
3573
self._fetch_all_revisions(revision_ids, pb)
3575
if my_pb is not None:
3577
return len(revision_ids), 0
3579
def _get_basis(self, first_revision_id):
3580
"""Get a revision and tree which exists in the target.
3582
This assumes that first_revision_id is selected for transmission
3583
because all other ancestors are already present. If we can't find an
3584
ancestor we fall back to NULL_REVISION since we know that is safe.
3586
:return: (basis_id, basis_tree)
3588
first_rev = self.source.get_revision(first_revision_id)
3590
basis_id = first_rev.parent_ids[0]
3591
# only valid as a basis if the target has it
3592
self.target.get_revision(basis_id)
3593
# Try to get a basis tree - if its a ghost it will hit the
3594
# NoSuchRevision case.
3595
basis_tree = self.source.revision_tree(basis_id)
3596
except (IndexError, errors.NoSuchRevision):
3597
basis_id = _mod_revision.NULL_REVISION
3598
basis_tree = self.source.revision_tree(basis_id)
3599
return basis_id, basis_tree
3602
InterRepository.register_optimiser(InterDifferingSerializer)
3603
InterRepository.register_optimiser(InterSameDataRepository)
3604
InterRepository.register_optimiser(InterWeaveRepo)
3605
InterRepository.register_optimiser(InterKnitRepo)
3606
InterRepository.register_optimiser(InterPackRepo)
3609
class CopyConverter(object):
3610
"""A repository conversion tool which just performs a copy of the content.
3612
This is slow but quite reliable.
3615
def __init__(self, target_format):
3616
"""Create a CopyConverter.
3618
:param target_format: The format the resulting repository should be.
3620
self.target_format = target_format
3622
def convert(self, repo, pb):
3623
"""Perform the conversion of to_convert, giving feedback via pb.
3625
:param to_convert: The disk object to convert.
3626
:param pb: a progress bar to use for progress information.
3631
# this is only useful with metadir layouts - separated repo content.
3632
# trigger an assertion if not such
3633
repo._format.get_format_string()
3634
self.repo_dir = repo.bzrdir
3635
self.step('Moving repository to repository.backup')
3636
self.repo_dir.transport.move('repository', 'repository.backup')
3637
backup_transport = self.repo_dir.transport.clone('repository.backup')
3638
repo._format.check_conversion_target(self.target_format)
3639
self.source_repo = repo._format.open(self.repo_dir,
3641
_override_transport=backup_transport)
3642
self.step('Creating new repository')
3643
converted = self.target_format.initialize(self.repo_dir,
3644
self.source_repo.is_shared())
3645
converted.lock_write()
3647
self.step('Copying content into repository.')
3648
self.source_repo.copy_content_into(converted)
3651
self.step('Deleting old repository content.')
3652
self.repo_dir.transport.delete_tree('repository.backup')
3653
self.pb.note('repository converted')
3655
def step(self, message):
3656
"""Update the pb by a step."""
3658
self.pb.update(message, self.count, self.total)
3670
def _unescaper(match, _map=_unescape_map):
3671
code = match.group(1)
3675
if not code.startswith('#'):
3677
return unichr(int(code[1:])).encode('utf8')
3683
def _unescape_xml(data):
3684
"""Unescape predefined XML entities in a string of data."""
3686
if _unescape_re is None:
3687
_unescape_re = re.compile('\&([^;]*);')
3688
return _unescape_re.sub(_unescaper, data)
3691
class _VersionedFileChecker(object):
3693
def __init__(self, repository, text_key_references=None):
3694
self.repository = repository
3695
self.text_index = self.repository._generate_text_key_index(
3696
text_key_references=text_key_references)
3698
def calculate_file_version_parents(self, text_key):
3699
"""Calculate the correct parents for a file version according to
3702
parent_keys = self.text_index[text_key]
3703
if parent_keys == [_mod_revision.NULL_REVISION]:
3705
return tuple(parent_keys)
3707
def check_file_version_parents(self, texts, progress_bar=None):
3708
"""Check the parents stored in a versioned file are correct.
3710
It also detects file versions that are not referenced by their
3711
corresponding revision's inventory.
3713
:returns: A tuple of (wrong_parents, dangling_file_versions).
3714
wrong_parents is a dict mapping {revision_id: (stored_parents,
3715
correct_parents)} for each revision_id where the stored parents
3716
are not correct. dangling_file_versions is a set of (file_id,
3717
revision_id) tuples for versions that are present in this versioned
3718
file, but not used by the corresponding inventory.
3721
self.file_ids = set([file_id for file_id, _ in
3722
self.text_index.iterkeys()])
3723
# text keys is now grouped by file_id
3724
n_weaves = len(self.file_ids)
3725
files_in_revisions = {}
3726
revisions_of_files = {}
3727
n_versions = len(self.text_index)
3728
progress_bar.update('loading text store', 0, n_versions)
3729
parent_map = self.repository.texts.get_parent_map(self.text_index)
3730
# On unlistable transports this could well be empty/error...
3731
text_keys = self.repository.texts.keys()
3732
unused_keys = frozenset(text_keys) - set(self.text_index)
3733
for num, key in enumerate(self.text_index.iterkeys()):
3734
if progress_bar is not None:
3735
progress_bar.update('checking text graph', num, n_versions)
3736
correct_parents = self.calculate_file_version_parents(key)
3738
knit_parents = parent_map[key]
3739
except errors.RevisionNotPresent:
3742
if correct_parents != knit_parents:
3743
wrong_parents[key] = (knit_parents, correct_parents)
3744
return wrong_parents, unused_keys
3747
def _old_get_graph(repository, revision_id):
3748
"""DO NOT USE. That is all. I'm serious."""
3749
graph = repository.get_graph()
3750
revision_graph = dict(((key, value) for key, value in
3751
graph.iter_ancestry([revision_id]) if value is not None))
3752
return _strip_NULL_ghosts(revision_graph)
3755
def _strip_NULL_ghosts(revision_graph):
3756
"""Also don't use this. more compatibility code for unmigrated clients."""
3757
# Filter ghosts, and null:
3758
if _mod_revision.NULL_REVISION in revision_graph:
3759
del revision_graph[_mod_revision.NULL_REVISION]
3760
for key, parents in revision_graph.items():
3761
revision_graph[key] = tuple(parent for parent in parents if parent
3763
return revision_graph
3766
class StreamSink(object):
3767
"""An object that can insert a stream into a repository.
3769
This interface handles the complexity of reserialising inventories and
3770
revisions from different formats, and allows unidirectional insertion into
3771
stacked repositories without looking for the missing basis parents
3775
def __init__(self, target_repo):
3776
self.target_repo = target_repo
3778
def insert_stream(self, stream, src_format, resume_tokens):
3779
"""Insert a stream's content into the target repository.
3781
:param src_format: a bzr repository format.
3783
:return: a list of resume tokens and an iterable of keys additional
3784
items required before the insertion can be completed.
3786
self.target_repo.lock_write()
3789
self.target_repo.resume_write_group(resume_tokens)
3791
self.target_repo.start_write_group()
3793
# locked_insert_stream performs a commit|suspend.
3794
return self._locked_insert_stream(stream, src_format)
3796
self.target_repo.abort_write_group(suppress_errors=True)
3799
self.target_repo.unlock()
3801
def _locked_insert_stream(self, stream, src_format):
3802
to_serializer = self.target_repo._format._serializer
3803
src_serializer = src_format._serializer
3804
if to_serializer == src_serializer:
3805
# If serializers match and the target is a pack repository, set the
3806
# write cache size on the new pack. This avoids poor performance
3807
# on transports where append is unbuffered (such as
3808
# RemoteTransport). This is safe to do because nothing should read
3809
# back from the target repository while a stream with matching
3810
# serialization is being inserted.
3811
# The exception is that a delta record from the source that should
3812
# be a fulltext may need to be expanded by the target (see
3813
# test_fetch_revisions_with_deltas_into_pack); but we take care to
3814
# explicitly flush any buffered writes first in that rare case.
3816
new_pack = self.target_repo._pack_collection._new_pack
3817
except AttributeError:
3818
# Not a pack repository
3821
new_pack.set_write_cache_size(1024*1024)
3822
for substream_type, substream in stream:
3823
if substream_type == 'texts':
3824
self.target_repo.texts.insert_record_stream(substream)
3825
elif substream_type == 'inventories':
3826
if src_serializer == to_serializer:
3827
self.target_repo.inventories.insert_record_stream(
3830
self._extract_and_insert_inventories(
3831
substream, src_serializer)
3832
elif substream_type == 'revisions':
3833
# This may fallback to extract-and-insert more often than
3834
# required if the serializers are different only in terms of
3836
if src_serializer == to_serializer:
3837
self.target_repo.revisions.insert_record_stream(
3840
self._extract_and_insert_revisions(substream,
3842
elif substream_type == 'signatures':
3843
self.target_repo.signatures.insert_record_stream(substream)
3845
raise AssertionError('kaboom! %s' % (substream_type,))
3847
missing_keys = set()
3848
for prefix, versioned_file in (
3849
('texts', self.target_repo.texts),
3850
('inventories', self.target_repo.inventories),
3851
('revisions', self.target_repo.revisions),
3852
('signatures', self.target_repo.signatures),
3854
missing_keys.update((prefix,) + key for key in
3855
versioned_file.get_missing_compression_parent_keys())
3856
except NotImplementedError:
3857
# cannot even attempt suspending, and missing would have failed
3858
# during stream insertion.
3859
missing_keys = set()
3862
# suspend the write group and tell the caller what we is
3863
# missing. We know we can suspend or else we would not have
3864
# entered this code path. (All repositories that can handle
3865
# missing keys can handle suspending a write group).
3866
write_group_tokens = self.target_repo.suspend_write_group()
3867
return write_group_tokens, missing_keys
3868
self.target_repo.commit_write_group()
3871
def _extract_and_insert_inventories(self, substream, serializer):
3872
"""Generate a new inventory versionedfile in target, converting data.
3874
The inventory is retrieved from the source, (deserializing it), and
3875
stored in the target (reserializing it in a different format).
3877
for record in substream:
3878
bytes = record.get_bytes_as('fulltext')
3879
revision_id = record.key[0]
3880
inv = serializer.read_inventory_from_string(bytes, revision_id)
3881
parents = [key[0] for key in record.parents]
3882
self.target_repo.add_inventory(revision_id, inv, parents)
3884
def _extract_and_insert_revisions(self, substream, serializer):
3885
for record in substream:
3886
bytes = record.get_bytes_as('fulltext')
3887
revision_id = record.key[0]
3888
rev = serializer.read_revision_from_string(bytes)
3889
if rev.revision_id != revision_id:
3890
raise AssertionError('wtf: %s != %s' % (rev, revision_id))
3891
self.target_repo.add_revision(revision_id, rev)
3894
if self.target_repo._format._fetch_reconcile:
3895
self.target_repo.reconcile()
3898
class StreamSource(object):
3899
"""A source of a stream for fetching between repositories."""
3901
def __init__(self, from_repository, to_format):
3902
"""Create a StreamSource streaming from from_repository."""
3903
self.from_repository = from_repository
3904
self.to_format = to_format
3906
def delta_on_metadata(self):
3907
"""Return True if delta's are permitted on metadata streams.
3909
That is on revisions and signatures.
3911
src_serializer = self.from_repository._format._serializer
3912
target_serializer = self.to_format._serializer
3913
return (self.to_format._fetch_uses_deltas and
3914
src_serializer == target_serializer)
3916
def _fetch_revision_texts(self, revs):
3917
# fetch signatures first and then the revision texts
3918
# may need to be a InterRevisionStore call here.
3919
from_sf = self.from_repository.signatures
3920
# A missing signature is just skipped.
3921
keys = [(rev_id,) for rev_id in revs]
3922
signatures = versionedfile.filter_absent(from_sf.get_record_stream(
3924
self.to_format._fetch_order,
3925
not self.to_format._fetch_uses_deltas))
3926
# If a revision has a delta, this is actually expanded inside the
3927
# insert_record_stream code now, which is an alternate fix for
3929
from_rf = self.from_repository.revisions
3930
revisions = from_rf.get_record_stream(
3932
self.to_format._fetch_order,
3933
not self.delta_on_metadata())
3934
return [('signatures', signatures), ('revisions', revisions)]
3936
def _generate_root_texts(self, revs):
3937
"""This will be called by __fetch between fetching weave texts and
3938
fetching the inventory weave.
3940
Subclasses should override this if they need to generate root texts
3941
after fetching weave texts.
3943
if self._rich_root_upgrade():
3945
return bzrlib.fetch.Inter1and2Helper(
3946
self.from_repository).generate_root_texts(revs)
3950
def get_stream(self, search):
3952
revs = search.get_keys()
3953
graph = self.from_repository.get_graph()
3954
revs = list(graph.iter_topo_order(revs))
3955
data_to_fetch = self.from_repository.item_keys_introduced_by(revs)
3957
for knit_kind, file_id, revisions in data_to_fetch:
3958
if knit_kind != phase:
3960
# Make a new progress bar for this phase
3961
if knit_kind == "file":
3962
# Accumulate file texts
3963
text_keys.extend([(file_id, revision) for revision in
3965
elif knit_kind == "inventory":
3966
# Now copy the file texts.
3967
from_texts = self.from_repository.texts
3968
yield ('texts', from_texts.get_record_stream(
3969
text_keys, self.to_format._fetch_order,
3970
not self.to_format._fetch_uses_deltas))
3971
# Cause an error if a text occurs after we have done the
3974
# Before we process the inventory we generate the root
3975
# texts (if necessary) so that the inventories references
3977
for _ in self._generate_root_texts(revs):
3979
# NB: This currently reopens the inventory weave in source;
3980
# using a single stream interface instead would avoid this.
3981
from_weave = self.from_repository.inventories
3982
# we fetch only the referenced inventories because we do not
3983
# know for unselected inventories whether all their required
3984
# texts are present in the other repository - it could be
3986
yield ('inventories', from_weave.get_record_stream(
3987
[(rev_id,) for rev_id in revs],
3988
self.inventory_fetch_order(),
3989
not self.delta_on_metadata()))
3990
elif knit_kind == "signatures":
3991
# Nothing to do here; this will be taken care of when
3992
# _fetch_revision_texts happens.
3994
elif knit_kind == "revisions":
3995
for record in self._fetch_revision_texts(revs):
3998
raise AssertionError("Unknown knit kind %r" % knit_kind)
4000
def get_stream_for_missing_keys(self, missing_keys):
4001
# missing keys can only occur when we are byte copying and not
4002
# translating (because translation means we don't send
4003
# unreconstructable deltas ever).
4005
keys['texts'] = set()
4006
keys['revisions'] = set()
4007
keys['inventories'] = set()
4008
keys['signatures'] = set()
4009
for key in missing_keys:
4010
keys[key[0]].add(key[1:])
4011
if len(keys['revisions']):
4012
# If we allowed copying revisions at this point, we could end up
4013
# copying a revision without copying its required texts: a
4014
# violation of the requirements for repository integrity.
4015
raise AssertionError(
4016
'cannot copy revisions to fill in missing deltas %s' % (
4017
keys['revisions'],))
4018
for substream_kind, keys in keys.iteritems():
4019
vf = getattr(self.from_repository, substream_kind)
4020
# Ask for full texts always so that we don't need more round trips
4021
# after this stream.
4022
stream = vf.get_record_stream(keys,
4023
self.to_format._fetch_order, True)
4024
yield substream_kind, stream
4026
def inventory_fetch_order(self):
4027
if self._rich_root_upgrade():
4028
return 'topological'
4030
return self.to_format._fetch_order
4032
def _rich_root_upgrade(self):
4033
return (not self.from_repository._format.rich_root_data and
4034
self.to_format.rich_root_data)