1
# Copyright (C) 2005-2011 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
"""Repository formats built around versioned files."""
20
from bzrlib.lazy_import import lazy_import
21
lazy_import(globals(), """
26
config as _mod_config,
35
revision as _mod_revision,
36
serializer as _mod_serializer,
45
from bzrlib.recordcounter import RecordCounter
46
from bzrlib.revisiontree import InventoryRevisionTree
47
from bzrlib.testament import Testament
48
from bzrlib.i18n import gettext
54
from bzrlib.decorators import (
59
from bzrlib.inventory import (
66
from bzrlib.repository import (
70
RepositoryFormatMetaDir,
75
from bzrlib.trace import (
80
class VersionedFileRepositoryFormat(RepositoryFormat):
81
"""Base class for all repository formats that are VersionedFiles-based."""
83
supports_full_versioned_files = True
84
supports_versioned_directories = True
85
supports_unreferenced_revisions = True
87
# Should commit add an inventory, or an inventory delta to the repository.
88
_commit_inv_deltas = True
89
# What order should fetch operations request streams in?
90
# The default is unordered as that is the cheapest for an origin to
92
_fetch_order = 'unordered'
93
# Does this repository format use deltas that can be fetched as-deltas ?
94
# (E.g. knits, where the knit deltas can be transplanted intact.
95
# We default to False, which will ensure that enough data to get
96
# a full text out of any fetch stream will be grabbed.
97
_fetch_uses_deltas = False
100
class VersionedFileCommitBuilder(CommitBuilder):
101
"""Commit builder implementation for versioned files based repositories.
104
# this commit builder supports the record_entry_contents interface
105
supports_record_entry_contents = True
107
# the default CommitBuilder does not manage trees whose root is versioned.
108
_versioned_root = False
110
def __init__(self, repository, parents, config_stack, timestamp=None,
111
timezone=None, committer=None, revprops=None,
112
revision_id=None, lossy=False):
113
super(VersionedFileCommitBuilder, self).__init__(repository,
114
parents, config_stack, timestamp, timezone, committer, revprops,
117
basis_id = self.parents[0]
119
basis_id = _mod_revision.NULL_REVISION
120
self.basis_delta_revision = basis_id
121
self.new_inventory = Inventory(None)
122
self._basis_delta = []
123
self.__heads = graph.HeadsCache(repository.get_graph()).heads
124
# memo'd check for no-op commits.
125
self._any_changes = False
126
# API compatibility, older code that used CommitBuilder did not call
127
# .record_delete(), which means the delta that is computed would not be
128
# valid. Callers that will call record_delete() should call
129
# .will_record_deletes() to indicate that.
130
self._recording_deletes = False
132
def will_record_deletes(self):
133
"""Tell the commit builder that deletes are being notified.
135
This enables the accumulation of an inventory delta; for the resulting
136
commit to be valid, deletes against the basis MUST be recorded via
137
builder.record_delete().
139
self._recording_deletes = True
141
def any_changes(self):
142
"""Return True if any entries were changed.
144
This includes merge-only changes. It is the core for the --unchanged
147
:return: True if any changes have occured.
149
return self._any_changes
151
def _ensure_fallback_inventories(self):
152
"""Ensure that appropriate inventories are available.
154
This only applies to repositories that are stacked, and is about
155
enusring the stacking invariants. Namely, that for any revision that is
156
present, we either have all of the file content, or we have the parent
157
inventory and the delta file content.
159
if not self.repository._fallback_repositories:
161
if not self.repository._format.supports_chks:
162
raise errors.BzrError("Cannot commit directly to a stacked branch"
163
" in pre-2a formats. See "
164
"https://bugs.launchpad.net/bzr/+bug/375013 for details.")
165
# This is a stacked repo, we need to make sure we have the parent
166
# inventories for the parents.
167
parent_keys = [(p,) for p in self.parents]
168
parent_map = self.repository.inventories._index.get_parent_map(parent_keys)
169
missing_parent_keys = set([pk for pk in parent_keys
170
if pk not in parent_map])
171
fallback_repos = list(reversed(self.repository._fallback_repositories))
172
missing_keys = [('inventories', pk[0])
173
for pk in missing_parent_keys]
175
while missing_keys and fallback_repos:
176
fallback_repo = fallback_repos.pop()
177
source = fallback_repo._get_source(self.repository._format)
178
sink = self.repository._get_sink()
179
stream = source.get_stream_for_missing_keys(missing_keys)
180
missing_keys = sink.insert_stream_without_locking(stream,
181
self.repository._format)
183
raise errors.BzrError('Unable to fill in parent inventories for a'
186
def commit(self, message):
187
"""Make the actual commit.
189
:return: The revision id of the recorded revision.
191
self._validate_unicode_text(message, 'commit message')
192
rev = _mod_revision.Revision(
193
timestamp=self._timestamp,
194
timezone=self._timezone,
195
committer=self._committer,
197
inventory_sha1=self.inv_sha1,
198
revision_id=self._new_revision_id,
199
properties=self._revprops)
200
rev.parent_ids = self.parents
201
self.repository.add_revision(self._new_revision_id, rev,
202
self.new_inventory, self._config_stack)
203
self._ensure_fallback_inventories()
204
self.repository.commit_write_group()
205
return self._new_revision_id
208
"""Abort the commit that is being built.
210
self.repository.abort_write_group()
212
def revision_tree(self):
213
"""Return the tree that was just committed.
215
After calling commit() this can be called to get a
216
RevisionTree representing the newly committed tree. This is
217
preferred to calling Repository.revision_tree() because that may
218
require deserializing the inventory, while we already have a copy in
221
if self.new_inventory is None:
222
self.new_inventory = self.repository.get_inventory(
223
self._new_revision_id)
224
return InventoryRevisionTree(self.repository, self.new_inventory,
225
self._new_revision_id)
227
def finish_inventory(self):
228
"""Tell the builder that the inventory is finished.
230
:return: The inventory id in the repository, which can be used with
231
repository.get_inventory.
233
if self.new_inventory is None:
234
# an inventory delta was accumulated without creating a new
236
basis_id = self.basis_delta_revision
237
# We ignore the 'inventory' returned by add_inventory_by_delta
238
# because self.new_inventory is used to hint to the rest of the
239
# system what code path was taken
240
self.inv_sha1, _ = self.repository.add_inventory_by_delta(
241
basis_id, self._basis_delta, self._new_revision_id,
244
if self.new_inventory.root is None:
245
raise AssertionError('Root entry should be supplied to'
246
' record_entry_contents, as of bzr 0.10.')
247
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
248
self.new_inventory.revision_id = self._new_revision_id
249
self.inv_sha1 = self.repository.add_inventory(
250
self._new_revision_id,
254
return self._new_revision_id
256
def _check_root(self, ie, parent_invs, tree):
257
"""Helper for record_entry_contents.
259
:param ie: An entry being added.
260
:param parent_invs: The inventories of the parent revisions of the
262
:param tree: The tree that is being committed.
264
# In this revision format, root entries have no knit or weave When
265
# serializing out to disk and back in root.revision is always
267
ie.revision = self._new_revision_id
269
def _require_root_change(self, tree):
270
"""Enforce an appropriate root object change.
272
This is called once when record_iter_changes is called, if and only if
273
the root was not in the delta calculated by record_iter_changes.
275
:param tree: The tree which is being committed.
277
if len(self.parents) == 0:
278
raise errors.RootMissing()
279
entry = entry_factory['directory'](tree.path2id(''), '',
281
entry.revision = self._new_revision_id
282
self._basis_delta.append(('', '', entry.file_id, entry))
284
def _get_delta(self, ie, basis_inv, path):
285
"""Get a delta against the basis inventory for ie."""
286
if not basis_inv.has_id(ie.file_id):
288
result = (None, path, ie.file_id, ie)
289
self._basis_delta.append(result)
291
elif ie != basis_inv[ie.file_id]:
293
# TODO: avoid tis id2path call.
294
result = (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
295
self._basis_delta.append(result)
301
def _heads(self, file_id, revision_ids):
302
"""Calculate the graph heads for revision_ids in the graph of file_id.
304
This can use either a per-file graph or a global revision graph as we
305
have an identity relationship between the two graphs.
307
return self.__heads(revision_ids)
309
def get_basis_delta(self):
310
"""Return the complete inventory delta versus the basis inventory.
312
This has been built up with the calls to record_delete and
313
record_entry_contents. The client must have already called
314
will_record_deletes() to indicate that they will be generating a
317
:return: An inventory delta, suitable for use with apply_delta, or
318
Repository.add_inventory_by_delta, etc.
320
if not self._recording_deletes:
321
raise AssertionError("recording deletes not activated.")
322
return self._basis_delta
324
def record_delete(self, path, file_id):
325
"""Record that a delete occured against a basis tree.
327
This is an optional API - when used it adds items to the basis_delta
328
being accumulated by the commit builder. It cannot be called unless the
329
method will_record_deletes() has been called to inform the builder that
330
a delta is being supplied.
332
:param path: The path of the thing deleted.
333
:param file_id: The file id that was deleted.
335
if not self._recording_deletes:
336
raise AssertionError("recording deletes not activated.")
337
delta = (path, None, file_id, None)
338
self._basis_delta.append(delta)
339
self._any_changes = True
342
def record_entry_contents(self, ie, parent_invs, path, tree,
344
"""Record the content of ie from tree into the commit if needed.
346
Side effect: sets ie.revision when unchanged
348
:param ie: An inventory entry present in the commit.
349
:param parent_invs: The inventories of the parent revisions of the
351
:param path: The path the entry is at in the tree.
352
:param tree: The tree which contains this entry and should be used to
354
:param content_summary: Summary data from the tree about the paths
355
content - stat, length, exec, sha/link target. This is only
356
accessed when the entry has a revision of None - that is when it is
357
a candidate to commit.
358
:return: A tuple (change_delta, version_recorded, fs_hash).
359
change_delta is an inventory_delta change for this entry against
360
the basis tree of the commit, or None if no change occured against
362
version_recorded is True if a new version of the entry has been
363
recorded. For instance, committing a merge where a file was only
364
changed on the other side will return (delta, False).
365
fs_hash is either None, or the hash details for the path (currently
366
a tuple of the contents sha1 and the statvalue returned by
367
tree.get_file_with_stat()).
369
if self.new_inventory.root is None:
370
if ie.parent_id is not None:
371
raise errors.RootMissing()
372
self._check_root(ie, parent_invs, tree)
373
if ie.revision is None:
374
kind = content_summary[0]
376
# ie is carried over from a prior commit
378
# XXX: repository specific check for nested tree support goes here - if
379
# the repo doesn't want nested trees we skip it ?
380
if (kind == 'tree-reference' and
381
not self.repository._format.supports_tree_reference):
382
# mismatch between commit builder logic and repository:
383
# this needs the entry creation pushed down into the builder.
384
raise NotImplementedError('Missing repository subtree support.')
385
self.new_inventory.add(ie)
387
# TODO: slow, take it out of the inner loop.
389
basis_inv = parent_invs[0]
391
basis_inv = Inventory(root_id=None)
393
# ie.revision is always None if the InventoryEntry is considered
394
# for committing. We may record the previous parents revision if the
395
# content is actually unchanged against a sole head.
396
if ie.revision is not None:
397
if not self._versioned_root and path == '':
398
# repositories that do not version the root set the root's
399
# revision to the new commit even when no change occurs (more
400
# specifically, they do not record a revision on the root; and
401
# the rev id is assigned to the root during deserialisation -
402
# this masks when a change may have occurred against the basis.
403
# To match this we always issue a delta, because the revision
404
# of the root will always be changing.
405
if basis_inv.has_id(ie.file_id):
406
delta = (basis_inv.id2path(ie.file_id), path,
410
delta = (None, path, ie.file_id, ie)
411
self._basis_delta.append(delta)
412
return delta, False, None
414
# we don't need to commit this, because the caller already
415
# determined that an existing revision of this file is
416
# appropriate. If it's not being considered for committing then
417
# it and all its parents to the root must be unaltered so
418
# no-change against the basis.
419
if ie.revision == self._new_revision_id:
420
raise AssertionError("Impossible situation, a skipped "
421
"inventory entry (%r) claims to be modified in this "
422
"commit (%r).", (ie, self._new_revision_id))
423
return None, False, None
424
# XXX: Friction: parent_candidates should return a list not a dict
425
# so that we don't have to walk the inventories again.
426
parent_candidate_entries = ie.parent_candidates(parent_invs)
427
head_set = self._heads(ie.file_id, parent_candidate_entries.keys())
429
for inv in parent_invs:
430
if inv.has_id(ie.file_id):
431
old_rev = inv[ie.file_id].revision
432
if old_rev in head_set:
433
heads.append(inv[ie.file_id].revision)
434
head_set.remove(inv[ie.file_id].revision)
437
# now we check to see if we need to write a new record to the
439
# We write a new entry unless there is one head to the ancestors, and
440
# the kind-derived content is unchanged.
442
# Cheapest check first: no ancestors, or more the one head in the
443
# ancestors, we write a new node.
447
# There is a single head, look it up for comparison
448
parent_entry = parent_candidate_entries[heads[0]]
449
# if the non-content specific data has changed, we'll be writing a
451
if (parent_entry.parent_id != ie.parent_id or
452
parent_entry.name != ie.name):
454
# now we need to do content specific checks:
456
# if the kind changed the content obviously has
457
if kind != parent_entry.kind:
459
# Stat cache fingerprint feedback for the caller - None as we usually
460
# don't generate one.
463
if content_summary[2] is None:
464
raise ValueError("Files must not have executable = None")
466
# We can't trust a check of the file length because of content
468
if (# if the exec bit has changed we have to store:
469
parent_entry.executable != content_summary[2]):
471
elif parent_entry.text_sha1 == content_summary[3]:
472
# all meta and content is unchanged (using a hash cache
473
# hit to check the sha)
474
ie.revision = parent_entry.revision
475
ie.text_size = parent_entry.text_size
476
ie.text_sha1 = parent_entry.text_sha1
477
ie.executable = parent_entry.executable
478
return self._get_delta(ie, basis_inv, path), False, None
480
# Either there is only a hash change(no hash cache entry,
481
# or same size content change), or there is no change on
483
# Provide the parent's hash to the store layer, so that the
484
# content is unchanged we will not store a new node.
485
nostore_sha = parent_entry.text_sha1
487
# We want to record a new node regardless of the presence or
488
# absence of a content change in the file.
490
ie.executable = content_summary[2]
491
file_obj, stat_value = tree.get_file_with_stat(ie.file_id, path)
493
text = file_obj.read()
497
ie.text_sha1, ie.text_size = self._add_text_to_weave(
498
ie.file_id, text, heads, nostore_sha)
499
# Let the caller know we generated a stat fingerprint.
500
fingerprint = (ie.text_sha1, stat_value)
501
except errors.ExistingContent:
502
# Turns out that the file content was unchanged, and we were
503
# only going to store a new node if it was changed. Carry over
505
ie.revision = parent_entry.revision
506
ie.text_size = parent_entry.text_size
507
ie.text_sha1 = parent_entry.text_sha1
508
ie.executable = parent_entry.executable
509
return self._get_delta(ie, basis_inv, path), False, None
510
elif kind == 'directory':
512
# all data is meta here, nothing specific to directory, so
514
ie.revision = parent_entry.revision
515
return self._get_delta(ie, basis_inv, path), False, None
516
self._add_text_to_weave(ie.file_id, '', heads, None)
517
elif kind == 'symlink':
518
current_link_target = content_summary[3]
520
# symlink target is not generic metadata, check if it has
522
if current_link_target != parent_entry.symlink_target:
525
# unchanged, carry over.
526
ie.revision = parent_entry.revision
527
ie.symlink_target = parent_entry.symlink_target
528
return self._get_delta(ie, basis_inv, path), False, None
529
ie.symlink_target = current_link_target
530
self._add_text_to_weave(ie.file_id, '', heads, None)
531
elif kind == 'tree-reference':
533
if content_summary[3] != parent_entry.reference_revision:
536
# unchanged, carry over.
537
ie.reference_revision = parent_entry.reference_revision
538
ie.revision = parent_entry.revision
539
return self._get_delta(ie, basis_inv, path), False, None
540
ie.reference_revision = content_summary[3]
541
if ie.reference_revision is None:
542
raise AssertionError("invalid content_summary for nested tree: %r"
543
% (content_summary,))
544
self._add_text_to_weave(ie.file_id, '', heads, None)
546
raise NotImplementedError('unknown kind')
547
ie.revision = self._new_revision_id
548
# The initial commit adds a root directory, but this in itself is not
549
# a worthwhile commit.
550
if (self.basis_delta_revision != _mod_revision.NULL_REVISION or
552
self._any_changes = True
553
return self._get_delta(ie, basis_inv, path), True, fingerprint
555
def record_iter_changes(self, tree, basis_revision_id, iter_changes,
556
_entry_factory=entry_factory):
557
"""Record a new tree via iter_changes.
559
:param tree: The tree to obtain text contents from for changed objects.
560
:param basis_revision_id: The revision id of the tree the iter_changes
561
has been generated against. Currently assumed to be the same
562
as self.parents[0] - if it is not, errors may occur.
563
:param iter_changes: An iter_changes iterator with the changes to apply
564
to basis_revision_id. The iterator must not include any items with
565
a current kind of None - missing items must be either filtered out
566
or errored-on before record_iter_changes sees the item.
567
:param _entry_factory: Private method to bind entry_factory locally for
569
:return: A generator of (file_id, relpath, fs_hash) tuples for use with
572
# Create an inventory delta based on deltas between all the parents and
573
# deltas between all the parent inventories. We use inventory delta's
574
# between the inventory objects because iter_changes masks
575
# last-changed-field only changes.
577
# file_id -> change map, change is fileid, paths, changed, versioneds,
578
# parents, names, kinds, executables
580
# {file_id -> revision_id -> inventory entry, for entries in parent
581
# trees that are not parents[0]
585
revtrees = list(self.repository.revision_trees(self.parents))
586
except errors.NoSuchRevision:
587
# one or more ghosts, slow path.
589
for revision_id in self.parents:
591
revtrees.append(self.repository.revision_tree(revision_id))
592
except errors.NoSuchRevision:
594
basis_revision_id = _mod_revision.NULL_REVISION
596
revtrees.append(self.repository.revision_tree(
597
_mod_revision.NULL_REVISION))
598
# The basis inventory from a repository
600
basis_inv = revtrees[0].inventory
602
basis_inv = self.repository.revision_tree(
603
_mod_revision.NULL_REVISION).inventory
604
if len(self.parents) > 0:
605
if basis_revision_id != self.parents[0] and not ghost_basis:
607
"arbitrary basis parents not yet supported with merges")
608
for revtree in revtrees[1:]:
609
for change in revtree.inventory._make_delta(basis_inv):
610
if change[1] is None:
611
# Not present in this parent.
613
if change[2] not in merged_ids:
614
if change[0] is not None:
615
basis_entry = basis_inv[change[2]]
616
merged_ids[change[2]] = [
618
basis_entry.revision,
621
parent_entries[change[2]] = {
623
basis_entry.revision:basis_entry,
625
change[3].revision:change[3],
628
merged_ids[change[2]] = [change[3].revision]
629
parent_entries[change[2]] = {change[3].revision:change[3]}
631
merged_ids[change[2]].append(change[3].revision)
632
parent_entries[change[2]][change[3].revision] = change[3]
635
# Setup the changes from the tree:
636
# changes maps file_id -> (change, [parent revision_ids])
638
for change in iter_changes:
639
# This probably looks up in basis_inv way to much.
640
if change[1][0] is not None:
641
head_candidate = [basis_inv[change[0]].revision]
644
changes[change[0]] = change, merged_ids.get(change[0],
646
unchanged_merged = set(merged_ids) - set(changes)
647
# Extend the changes dict with synthetic changes to record merges of
649
for file_id in unchanged_merged:
650
# Record a merged version of these items that did not change vs the
651
# basis. This can be either identical parallel changes, or a revert
652
# of a specific file after a merge. The recorded content will be
653
# that of the current tree (which is the same as the basis), but
654
# the per-file graph will reflect a merge.
655
# NB:XXX: We are reconstructing path information we had, this
656
# should be preserved instead.
657
# inv delta change: (file_id, (path_in_source, path_in_target),
658
# changed_content, versioned, parent, name, kind,
661
basis_entry = basis_inv[file_id]
662
except errors.NoSuchId:
663
# a change from basis->some_parents but file_id isn't in basis
664
# so was new in the merge, which means it must have changed
665
# from basis -> current, and as it hasn't the add was reverted
666
# by the user. So we discard this change.
670
(basis_inv.id2path(file_id), tree.id2path(file_id)),
672
(basis_entry.parent_id, basis_entry.parent_id),
673
(basis_entry.name, basis_entry.name),
674
(basis_entry.kind, basis_entry.kind),
675
(basis_entry.executable, basis_entry.executable))
676
changes[file_id] = (change, merged_ids[file_id])
677
# changes contains tuples with the change and a set of inventory
678
# candidates for the file.
680
# old_path, new_path, file_id, new_inventory_entry
681
seen_root = False # Is the root in the basis delta?
682
inv_delta = self._basis_delta
683
modified_rev = self._new_revision_id
684
for change, head_candidates in changes.values():
685
if change[3][1]: # versioned in target.
686
# Several things may be happening here:
687
# We may have a fork in the per-file graph
688
# - record a change with the content from tree
689
# We may have a change against < all trees
690
# - carry over the tree that hasn't changed
691
# We may have a change against all trees
692
# - record the change with the content from tree
695
entry = _entry_factory[kind](file_id, change[5][1],
697
head_set = self._heads(change[0], set(head_candidates))
700
for head_candidate in head_candidates:
701
if head_candidate in head_set:
702
heads.append(head_candidate)
703
head_set.remove(head_candidate)
706
# Could be a carry-over situation:
707
parent_entry_revs = parent_entries.get(file_id, None)
708
if parent_entry_revs:
709
parent_entry = parent_entry_revs.get(heads[0], None)
712
if parent_entry is None:
713
# The parent iter_changes was called against is the one
714
# that is the per-file head, so any change is relevant
715
# iter_changes is valid.
716
carry_over_possible = False
718
# could be a carry over situation
719
# A change against the basis may just indicate a merge,
720
# we need to check the content against the source of the
721
# merge to determine if it was changed after the merge
723
if (parent_entry.kind != entry.kind or
724
parent_entry.parent_id != entry.parent_id or
725
parent_entry.name != entry.name):
726
# Metadata common to all entries has changed
727
# against per-file parent
728
carry_over_possible = False
730
carry_over_possible = True
731
# per-type checks for changes against the parent_entry
734
# Cannot be a carry-over situation
735
carry_over_possible = False
736
# Populate the entry in the delta
738
# XXX: There is still a small race here: If someone reverts the content of a file
739
# after iter_changes examines and decides it has changed,
740
# we will unconditionally record a new version even if some
741
# other process reverts it while commit is running (with
742
# the revert happening after iter_changes did its
745
entry.executable = True
747
entry.executable = False
748
if (carry_over_possible and
749
parent_entry.executable == entry.executable):
750
# Check the file length, content hash after reading
752
nostore_sha = parent_entry.text_sha1
755
file_obj, stat_value = tree.get_file_with_stat(file_id, change[1][1])
757
text = file_obj.read()
761
entry.text_sha1, entry.text_size = self._add_text_to_weave(
762
file_id, text, heads, nostore_sha)
763
yield file_id, change[1][1], (entry.text_sha1, stat_value)
764
except errors.ExistingContent:
765
# No content change against a carry_over parent
766
# Perhaps this should also yield a fs hash update?
768
entry.text_size = parent_entry.text_size
769
entry.text_sha1 = parent_entry.text_sha1
770
elif kind == 'symlink':
772
entry.symlink_target = tree.get_symlink_target(file_id)
773
if (carry_over_possible and
774
parent_entry.symlink_target == entry.symlink_target):
777
self._add_text_to_weave(change[0], '', heads, None)
778
elif kind == 'directory':
779
if carry_over_possible:
782
# Nothing to set on the entry.
783
# XXX: split into the Root and nonRoot versions.
784
if change[1][1] != '' or self.repository.supports_rich_root():
785
self._add_text_to_weave(change[0], '', heads, None)
786
elif kind == 'tree-reference':
787
if not self.repository._format.supports_tree_reference:
788
# This isn't quite sane as an error, but we shouldn't
789
# ever see this code path in practice: tree's don't
790
# permit references when the repo doesn't support tree
792
raise errors.UnsupportedOperation(tree.add_reference,
794
reference_revision = tree.get_reference_revision(change[0])
795
entry.reference_revision = reference_revision
796
if (carry_over_possible and
797
parent_entry.reference_revision == reference_revision):
800
self._add_text_to_weave(change[0], '', heads, None)
802
raise AssertionError('unknown kind %r' % kind)
804
entry.revision = modified_rev
806
entry.revision = parent_entry.revision
809
new_path = change[1][1]
810
inv_delta.append((change[1][0], new_path, change[0], entry))
813
self.new_inventory = None
814
# The initial commit adds a root directory, but this in itself is not
815
# a worthwhile commit.
816
if ((len(inv_delta) > 0 and basis_revision_id != _mod_revision.NULL_REVISION) or
817
(len(inv_delta) > 1 and basis_revision_id == _mod_revision.NULL_REVISION)):
818
# This should perhaps be guarded by a check that the basis we
819
# commit against is the basis for the commit and if not do a delta
821
self._any_changes = True
823
# housekeeping root entry changes do not affect no-change commits.
824
self._require_root_change(tree)
825
self.basis_delta_revision = basis_revision_id
827
def _add_text_to_weave(self, file_id, new_text, parents, nostore_sha):
828
parent_keys = tuple([(file_id, parent) for parent in parents])
829
return self.repository.texts._add_text(
830
(file_id, self._new_revision_id), parent_keys, new_text,
831
nostore_sha=nostore_sha, random_id=self.random_revid)[0:2]
834
class VersionedFileRootCommitBuilder(VersionedFileCommitBuilder):
835
"""This commitbuilder actually records the root id"""
837
# the root entry gets versioned properly by this builder.
838
_versioned_root = True
840
def _check_root(self, ie, parent_invs, tree):
841
"""Helper for record_entry_contents.
843
:param ie: An entry being added.
844
:param parent_invs: The inventories of the parent revisions of the
846
:param tree: The tree that is being committed.
849
def _require_root_change(self, tree):
850
"""Enforce an appropriate root object change.
852
This is called once when record_iter_changes is called, if and only if
853
the root was not in the delta calculated by record_iter_changes.
855
:param tree: The tree which is being committed.
857
# versioned roots do not change unless the tree found a change.
860
class VersionedFileRepository(Repository):
861
"""Repository holding history for one or more branches.
863
The repository holds and retrieves historical information including
864
revisions and file history. It's normally accessed only by the Branch,
865
which views a particular line of development through that history.
867
The Repository builds on top of some byte storage facilies (the revisions,
868
signatures, inventories, texts and chk_bytes attributes) and a Transport,
869
which respectively provide byte storage and a means to access the (possibly
872
The byte storage facilities are addressed via tuples, which we refer to
873
as 'keys' throughout the code base. Revision_keys, inventory_keys and
874
signature_keys are all 1-tuples: (revision_id,). text_keys are two-tuples:
875
(file_id, revision_id). chk_bytes uses CHK keys - a 1-tuple with a single
876
byte string made up of a hash identifier and a hash value.
877
We use this interface because it allows low friction with the underlying
878
code that implements disk indices, network encoding and other parts of
881
:ivar revisions: A bzrlib.versionedfile.VersionedFiles instance containing
882
the serialised revisions for the repository. This can be used to obtain
883
revision graph information or to access raw serialised revisions.
884
The result of trying to insert data into the repository via this store
885
is undefined: it should be considered read-only except for implementors
887
:ivar signatures: A bzrlib.versionedfile.VersionedFiles instance containing
888
the serialised signatures for the repository. This can be used to
889
obtain access to raw serialised signatures. The result of trying to
890
insert data into the repository via this store is undefined: it should
891
be considered read-only except for implementors of repositories.
892
:ivar inventories: A bzrlib.versionedfile.VersionedFiles instance containing
893
the serialised inventories for the repository. This can be used to
894
obtain unserialised inventories. The result of trying to insert data
895
into the repository via this store is undefined: it should be
896
considered read-only except for implementors of repositories.
897
:ivar texts: A bzrlib.versionedfile.VersionedFiles instance containing the
898
texts of files and directories for the repository. This can be used to
899
obtain file texts or file graphs. Note that Repository.iter_file_bytes
900
is usually a better interface for accessing file texts.
901
The result of trying to insert data into the repository via this store
902
is undefined: it should be considered read-only except for implementors
904
:ivar chk_bytes: A bzrlib.versionedfile.VersionedFiles instance containing
905
any data the repository chooses to store or have indexed by its hash.
906
The result of trying to insert data into the repository via this store
907
is undefined: it should be considered read-only except for implementors
909
:ivar _transport: Transport for file access to repository, typically
910
pointing to .bzr/repository.
913
# What class to use for a CommitBuilder. Often it's simpler to change this
914
# in a Repository class subclass rather than to override
915
# get_commit_builder.
916
_commit_builder_class = VersionedFileCommitBuilder
918
def add_fallback_repository(self, repository):
919
"""Add a repository to use for looking up data not held locally.
921
:param repository: A repository.
923
if not self._format.supports_external_lookups:
924
raise errors.UnstackableRepositoryFormat(self._format, self.base)
925
# This can raise an exception, so should be done before we lock the
926
# fallback repository.
927
self._check_fallback_repository(repository)
929
# This repository will call fallback.unlock() when we transition to
930
# the unlocked state, so we make sure to increment the lock count
931
repository.lock_read()
932
self._fallback_repositories.append(repository)
933
self.texts.add_fallback_versioned_files(repository.texts)
934
self.inventories.add_fallback_versioned_files(repository.inventories)
935
self.revisions.add_fallback_versioned_files(repository.revisions)
936
self.signatures.add_fallback_versioned_files(repository.signatures)
937
if self.chk_bytes is not None:
938
self.chk_bytes.add_fallback_versioned_files(repository.chk_bytes)
940
@only_raises(errors.LockNotHeld, errors.LockBroken)
942
super(VersionedFileRepository, self).unlock()
943
if self.control_files._lock_count == 0:
944
self._inventory_entry_cache.clear()
946
def add_inventory(self, revision_id, inv, parents):
947
"""Add the inventory inv to the repository as revision_id.
949
:param parents: The revision ids of the parents that revision_id
950
is known to have and are in the repository already.
952
:returns: The validator(which is a sha1 digest, though what is sha'd is
953
repository format specific) of the serialized inventory.
955
if not self.is_in_write_group():
956
raise AssertionError("%r not in write group" % (self,))
957
_mod_revision.check_not_reserved_id(revision_id)
958
if not (inv.revision_id is None or inv.revision_id == revision_id):
959
raise AssertionError(
960
"Mismatch between inventory revision"
961
" id and insertion revid (%r, %r)"
962
% (inv.revision_id, revision_id))
964
raise errors.RootMissing()
965
return self._add_inventory_checked(revision_id, inv, parents)
967
def _add_inventory_checked(self, revision_id, inv, parents):
968
"""Add inv to the repository after checking the inputs.
970
This function can be overridden to allow different inventory styles.
972
:seealso: add_inventory, for the contract.
974
inv_lines = self._serializer.write_inventory_to_lines(inv)
975
return self._inventory_add_lines(revision_id, parents,
976
inv_lines, check_content=False)
978
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
979
parents, basis_inv=None, propagate_caches=False):
980
"""Add a new inventory expressed as a delta against another revision.
982
See the inventory developers documentation for the theory behind
985
:param basis_revision_id: The inventory id the delta was created
986
against. (This does not have to be a direct parent.)
987
:param delta: The inventory delta (see Inventory.apply_delta for
989
:param new_revision_id: The revision id that the inventory is being
991
:param parents: The revision ids of the parents that revision_id is
992
known to have and are in the repository already. These are supplied
993
for repositories that depend on the inventory graph for revision
994
graph access, as well as for those that pun ancestry with delta
996
:param basis_inv: The basis inventory if it is already known,
998
:param propagate_caches: If True, the caches for this inventory are
999
copied to and updated for the result if possible.
1001
:returns: (validator, new_inv)
1002
The validator(which is a sha1 digest, though what is sha'd is
1003
repository format specific) of the serialized inventory, and the
1004
resulting inventory.
1006
if not self.is_in_write_group():
1007
raise AssertionError("%r not in write group" % (self,))
1008
_mod_revision.check_not_reserved_id(new_revision_id)
1009
basis_tree = self.revision_tree(basis_revision_id)
1010
basis_tree.lock_read()
1012
# Note that this mutates the inventory of basis_tree, which not all
1013
# inventory implementations may support: A better idiom would be to
1014
# return a new inventory, but as there is no revision tree cache in
1015
# repository this is safe for now - RBC 20081013
1016
if basis_inv is None:
1017
basis_inv = basis_tree.inventory
1018
basis_inv.apply_delta(delta)
1019
basis_inv.revision_id = new_revision_id
1020
return (self.add_inventory(new_revision_id, basis_inv, parents),
1025
def _inventory_add_lines(self, revision_id, parents, lines,
1026
check_content=True):
1027
"""Store lines in inv_vf and return the sha1 of the inventory."""
1028
parents = [(parent,) for parent in parents]
1029
result = self.inventories.add_lines((revision_id,), parents, lines,
1030
check_content=check_content)[0]
1031
self.inventories._access.flush()
1034
def add_revision(self, revision_id, rev, inv=None, config=None):
1035
"""Add rev to the revision store as revision_id.
1037
:param revision_id: the revision id to use.
1038
:param rev: The revision object.
1039
:param inv: The inventory for the revision. if None, it will be looked
1040
up in the inventory storer
1041
:param config: If None no digital signature will be created.
1042
If supplied its signature_needed method will be used
1043
to determine if a signature should be made.
1045
# TODO: jam 20070210 Shouldn't we check rev.revision_id and
1047
_mod_revision.check_not_reserved_id(revision_id)
1048
if (config is not None and
1049
config.get('create_signatures') == _mod_config.SIGN_ALWAYS):
1051
inv = self.get_inventory(revision_id)
1052
tree = InventoryRevisionTree(self, inv, revision_id)
1053
testament = Testament(rev, tree)
1054
plaintext = testament.as_short_text()
1055
self.store_revision_signature(
1056
gpg.GPGStrategy(config), plaintext, revision_id)
1057
# check inventory present
1058
if not self.inventories.get_parent_map([(revision_id,)]):
1060
raise errors.WeaveRevisionNotPresent(revision_id,
1063
# yes, this is not suitable for adding with ghosts.
1064
rev.inventory_sha1 = self.add_inventory(revision_id, inv,
1067
key = (revision_id,)
1068
rev.inventory_sha1 = self.inventories.get_sha1s([key])[key]
1069
self._add_revision(rev)
1071
def _add_revision(self, revision):
1072
text = self._serializer.write_revision_to_string(revision)
1073
key = (revision.revision_id,)
1074
parents = tuple((parent,) for parent in revision.parent_ids)
1075
self.revisions.add_lines(key, parents, osutils.split_lines(text))
1077
def _check_inventories(self, checker):
1078
"""Check the inventories found from the revision scan.
1080
This is responsible for verifying the sha1 of inventories and
1081
creating a pending_keys set that covers data referenced by inventories.
1083
bar = ui.ui_factory.nested_progress_bar()
1085
self._do_check_inventories(checker, bar)
1089
def _do_check_inventories(self, checker, bar):
1090
"""Helper for _check_inventories."""
1092
keys = {'chk_bytes':set(), 'inventories':set(), 'texts':set()}
1093
kinds = ['chk_bytes', 'texts']
1094
count = len(checker.pending_keys)
1095
bar.update(gettext("inventories"), 0, 2)
1096
current_keys = checker.pending_keys
1097
checker.pending_keys = {}
1098
# Accumulate current checks.
1099
for key in current_keys:
1100
if key[0] != 'inventories' and key[0] not in kinds:
1101
checker._report_items.append('unknown key type %r' % (key,))
1102
keys[key[0]].add(key[1:])
1103
if keys['inventories']:
1104
# NB: output order *should* be roughly sorted - topo or
1105
# inverse topo depending on repository - either way decent
1106
# to just delta against. However, pre-CHK formats didn't
1107
# try to optimise inventory layout on disk. As such the
1108
# pre-CHK code path does not use inventory deltas.
1110
for record in self.inventories.check(keys=keys['inventories']):
1111
if record.storage_kind == 'absent':
1112
checker._report_items.append(
1113
'Missing inventory {%s}' % (record.key,))
1115
last_object = self._check_record('inventories', record,
1116
checker, last_object,
1117
current_keys[('inventories',) + record.key])
1118
del keys['inventories']
1121
bar.update(gettext("texts"), 1)
1122
while (checker.pending_keys or keys['chk_bytes']
1124
# Something to check.
1125
current_keys = checker.pending_keys
1126
checker.pending_keys = {}
1127
# Accumulate current checks.
1128
for key in current_keys:
1129
if key[0] not in kinds:
1130
checker._report_items.append('unknown key type %r' % (key,))
1131
keys[key[0]].add(key[1:])
1132
# Check the outermost kind only - inventories || chk_bytes || texts
1136
for record in getattr(self, kind).check(keys=keys[kind]):
1137
if record.storage_kind == 'absent':
1138
checker._report_items.append(
1139
'Missing %s {%s}' % (kind, record.key,))
1141
last_object = self._check_record(kind, record,
1142
checker, last_object, current_keys[(kind,) + record.key])
1146
def _check_record(self, kind, record, checker, last_object, item_data):
1147
"""Check a single text from this repository."""
1148
if kind == 'inventories':
1149
rev_id = record.key[0]
1150
inv = self._deserialise_inventory(rev_id,
1151
record.get_bytes_as('fulltext'))
1152
if last_object is not None:
1153
delta = inv._make_delta(last_object)
1154
for old_path, path, file_id, ie in delta:
1157
ie.check(checker, rev_id, inv)
1159
for path, ie in inv.iter_entries():
1160
ie.check(checker, rev_id, inv)
1161
if self._format.fast_deltas:
1163
elif kind == 'chk_bytes':
1164
# No code written to check chk_bytes for this repo format.
1165
checker._report_items.append(
1166
'unsupported key type chk_bytes for %s' % (record.key,))
1167
elif kind == 'texts':
1168
self._check_text(record, checker, item_data)
1170
checker._report_items.append(
1171
'unknown key type %s for %s' % (kind, record.key))
1173
def _check_text(self, record, checker, item_data):
1174
"""Check a single text."""
1175
# Check it is extractable.
1176
# TODO: check length.
1177
if record.storage_kind == 'chunked':
1178
chunks = record.get_bytes_as(record.storage_kind)
1179
sha1 = osutils.sha_strings(chunks)
1180
length = sum(map(len, chunks))
1182
content = record.get_bytes_as('fulltext')
1183
sha1 = osutils.sha_string(content)
1184
length = len(content)
1185
if item_data and sha1 != item_data[1]:
1186
checker._report_items.append(
1187
'sha1 mismatch: %s has sha1 %s expected %s referenced by %s' %
1188
(record.key, sha1, item_data[1], item_data[2]))
1191
def _eliminate_revisions_not_present(self, revision_ids):
1192
"""Check every revision id in revision_ids to see if we have it.
1194
Returns a set of the present revisions.
1197
graph = self.get_graph()
1198
parent_map = graph.get_parent_map(revision_ids)
1199
# The old API returned a list, should this actually be a set?
1200
return parent_map.keys()
1202
def __init__(self, _format, a_bzrdir, control_files):
1203
"""Instantiate a VersionedFileRepository.
1205
:param _format: The format of the repository on disk.
1206
:param controldir: The ControlDir of the repository.
1207
:param control_files: Control files to use for locking, etc.
1209
# In the future we will have a single api for all stores for
1210
# getting file texts, inventories and revisions, then
1211
# this construct will accept instances of those things.
1212
super(VersionedFileRepository, self).__init__(_format, a_bzrdir,
1214
self._transport = control_files._transport
1215
self.base = self._transport.base
1217
self._reconcile_does_inventory_gc = True
1218
self._reconcile_fixes_text_parents = False
1219
self._reconcile_backsup_inventory = True
1220
# An InventoryEntry cache, used during deserialization
1221
self._inventory_entry_cache = fifo_cache.FIFOCache(10*1024)
1222
# Is it safe to return inventory entries directly from the entry cache,
1223
# rather copying them?
1224
self._safe_to_return_from_cache = False
1226
def fetch(self, source, revision_id=None, find_ghosts=False,
1228
"""Fetch the content required to construct revision_id from source.
1230
If revision_id is None and fetch_spec is None, then all content is
1233
fetch() may not be used when the repository is in a write group -
1234
either finish the current write group before using fetch, or use
1235
fetch before starting the write group.
1237
:param find_ghosts: Find and copy revisions in the source that are
1238
ghosts in the target (and not reachable directly by walking out to
1239
the first-present revision in target from revision_id).
1240
:param revision_id: If specified, all the content needed for this
1241
revision ID will be copied to the target. Fetch will determine for
1242
itself which content needs to be copied.
1243
:param fetch_spec: If specified, a SearchResult or
1244
PendingAncestryResult that describes which revisions to copy. This
1245
allows copying multiple heads at once. Mutually exclusive with
1248
if fetch_spec is not None and revision_id is not None:
1249
raise AssertionError(
1250
"fetch_spec and revision_id are mutually exclusive.")
1251
if self.is_in_write_group():
1252
raise errors.InternalBzrError(
1253
"May not fetch while in a write group.")
1254
# fast path same-url fetch operations
1255
# TODO: lift out to somewhere common with RemoteRepository
1256
# <https://bugs.launchpad.net/bzr/+bug/401646>
1257
if (self.has_same_location(source)
1258
and fetch_spec is None
1259
and self._has_same_fallbacks(source)):
1260
# check that last_revision is in 'from' and then return a
1262
if (revision_id is not None and
1263
not _mod_revision.is_null(revision_id)):
1264
self.get_revision(revision_id)
1266
inter = InterRepository.get(source, self)
1267
if (fetch_spec is not None and
1268
not getattr(inter, "supports_fetch_spec", False)):
1269
raise errors.UnsupportedOperation(
1270
"fetch_spec not supported for %r" % inter)
1271
return inter.fetch(revision_id=revision_id,
1272
find_ghosts=find_ghosts, fetch_spec=fetch_spec)
1275
def gather_stats(self, revid=None, committers=None):
1276
"""See Repository.gather_stats()."""
1277
result = super(VersionedFileRepository, self).gather_stats(revid, committers)
1278
# now gather global repository information
1279
# XXX: This is available for many repos regardless of listability.
1280
if self.user_transport.listable():
1281
# XXX: do we want to __define len__() ?
1282
# Maybe the versionedfiles object should provide a different
1283
# method to get the number of keys.
1284
result['revisions'] = len(self.revisions.keys())
1285
# result['size'] = t
1288
def get_commit_builder(self, branch, parents, config_stack, timestamp=None,
1289
timezone=None, committer=None, revprops=None,
1290
revision_id=None, lossy=False):
1291
"""Obtain a CommitBuilder for this repository.
1293
:param branch: Branch to commit to.
1294
:param parents: Revision ids of the parents of the new revision.
1295
:param config_stack: Configuration stack to use.
1296
:param timestamp: Optional timestamp recorded for commit.
1297
:param timezone: Optional timezone for timestamp.
1298
:param committer: Optional committer to set for commit.
1299
:param revprops: Optional dictionary of revision properties.
1300
:param revision_id: Optional revision id.
1301
:param lossy: Whether to discard data that can not be natively
1302
represented, when pushing to a foreign VCS
1304
if self._fallback_repositories and not self._format.supports_chks:
1305
raise errors.BzrError("Cannot commit directly to a stacked branch"
1306
" in pre-2a formats. See "
1307
"https://bugs.launchpad.net/bzr/+bug/375013 for details.")
1308
result = self._commit_builder_class(self, parents, config_stack,
1309
timestamp, timezone, committer, revprops, revision_id,
1311
self.start_write_group()
1314
def get_missing_parent_inventories(self, check_for_missing_texts=True):
1315
"""Return the keys of missing inventory parents for revisions added in
1318
A revision is not complete if the inventory delta for that revision
1319
cannot be calculated. Therefore if the parent inventories of a
1320
revision are not present, the revision is incomplete, and e.g. cannot
1321
be streamed by a smart server. This method finds missing inventory
1322
parents for revisions added in this write group.
1324
if not self._format.supports_external_lookups:
1325
# This is only an issue for stacked repositories
1327
if not self.is_in_write_group():
1328
raise AssertionError('not in a write group')
1330
# XXX: We assume that every added revision already has its
1331
# corresponding inventory, so we only check for parent inventories that
1332
# might be missing, rather than all inventories.
1333
parents = set(self.revisions._index.get_missing_parents())
1334
parents.discard(_mod_revision.NULL_REVISION)
1335
unstacked_inventories = self.inventories._index
1336
present_inventories = unstacked_inventories.get_parent_map(
1337
key[-1:] for key in parents)
1338
parents.difference_update(present_inventories)
1339
if len(parents) == 0:
1340
# No missing parent inventories.
1342
if not check_for_missing_texts:
1343
return set(('inventories', rev_id) for (rev_id,) in parents)
1344
# Ok, now we have a list of missing inventories. But these only matter
1345
# if the inventories that reference them are missing some texts they
1346
# appear to introduce.
1347
# XXX: Texts referenced by all added inventories need to be present,
1348
# but at the moment we're only checking for texts referenced by
1349
# inventories at the graph's edge.
1350
key_deps = self.revisions._index._key_dependencies
1351
key_deps.satisfy_refs_for_keys(present_inventories)
1352
referrers = frozenset(r[0] for r in key_deps.get_referrers())
1353
file_ids = self.fileids_altered_by_revision_ids(referrers)
1354
missing_texts = set()
1355
for file_id, version_ids in file_ids.iteritems():
1356
missing_texts.update(
1357
(file_id, version_id) for version_id in version_ids)
1358
present_texts = self.texts.get_parent_map(missing_texts)
1359
missing_texts.difference_update(present_texts)
1360
if not missing_texts:
1361
# No texts are missing, so all revisions and their deltas are
1364
# Alternatively the text versions could be returned as the missing
1365
# keys, but this is likely to be less data.
1366
missing_keys = set(('inventories', rev_id) for (rev_id,) in parents)
1370
def has_revisions(self, revision_ids):
1371
"""Probe to find out the presence of multiple revisions.
1373
:param revision_ids: An iterable of revision_ids.
1374
:return: A set of the revision_ids that were present.
1376
parent_map = self.revisions.get_parent_map(
1377
[(rev_id,) for rev_id in revision_ids])
1379
if _mod_revision.NULL_REVISION in revision_ids:
1380
result.add(_mod_revision.NULL_REVISION)
1381
result.update([key[0] for key in parent_map])
1385
def get_revision_reconcile(self, revision_id):
1386
"""'reconcile' helper routine that allows access to a revision always.
1388
This variant of get_revision does not cross check the weave graph
1389
against the revision one as get_revision does: but it should only
1390
be used by reconcile, or reconcile-alike commands that are correcting
1391
or testing the revision graph.
1393
return self._get_revisions([revision_id])[0]
1396
def get_revisions(self, revision_ids):
1397
"""Get many revisions at once.
1399
Repositories that need to check data on every revision read should
1400
subclass this method.
1402
return self._get_revisions(revision_ids)
1405
def _get_revisions(self, revision_ids):
1406
"""Core work logic to get many revisions without sanity checks."""
1408
for revid, rev in self._iter_revisions(revision_ids):
1410
raise errors.NoSuchRevision(self, revid)
1412
return [revs[revid] for revid in revision_ids]
1414
def _iter_revisions(self, revision_ids):
1415
"""Iterate over revision objects.
1417
:param revision_ids: An iterable of revisions to examine. None may be
1418
passed to request all revisions known to the repository. Note that
1419
not all repositories can find unreferenced revisions; for those
1420
repositories only referenced ones will be returned.
1421
:return: An iterator of (revid, revision) tuples. Absent revisions (
1422
those asked for but not available) are returned as (revid, None).
1424
if revision_ids is None:
1425
revision_ids = self.all_revision_ids()
1427
for rev_id in revision_ids:
1428
if not rev_id or not isinstance(rev_id, basestring):
1429
raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
1430
keys = [(key,) for key in revision_ids]
1431
stream = self.revisions.get_record_stream(keys, 'unordered', True)
1432
for record in stream:
1433
revid = record.key[0]
1434
if record.storage_kind == 'absent':
1437
text = record.get_bytes_as('fulltext')
1438
rev = self._serializer.read_revision_from_string(text)
1442
def add_signature_text(self, revision_id, signature):
1443
"""Store a signature text for a revision.
1445
:param revision_id: Revision id of the revision
1446
:param signature: Signature text.
1448
self.signatures.add_lines((revision_id,), (),
1449
osutils.split_lines(signature))
1451
def find_text_key_references(self):
1452
"""Find the text key references within the repository.
1454
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1455
to whether they were referred to by the inventory of the
1456
revision_id that they contain. The inventory texts from all present
1457
revision ids are assessed to generate this report.
1459
revision_keys = self.revisions.keys()
1460
w = self.inventories
1461
pb = ui.ui_factory.nested_progress_bar()
1463
return self._serializer._find_text_key_references(
1464
w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
1468
def _inventory_xml_lines_for_keys(self, keys):
1469
"""Get a line iterator of the sort needed for findind references.
1471
Not relevant for non-xml inventory repositories.
1473
Ghosts in revision_keys are ignored.
1475
:param revision_keys: The revision keys for the inventories to inspect.
1476
:return: An iterator over (inventory line, revid) for the fulltexts of
1477
all of the xml inventories specified by revision_keys.
1479
stream = self.inventories.get_record_stream(keys, 'unordered', True)
1480
for record in stream:
1481
if record.storage_kind != 'absent':
1482
chunks = record.get_bytes_as('chunked')
1483
revid = record.key[-1]
1484
lines = osutils.chunks_to_lines(chunks)
1488
def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1490
"""Helper routine for fileids_altered_by_revision_ids.
1492
This performs the translation of xml lines to revision ids.
1494
:param line_iterator: An iterator of lines, origin_version_id
1495
:param revision_keys: The revision ids to filter for. This should be a
1496
set or other type which supports efficient __contains__ lookups, as
1497
the revision key from each parsed line will be looked up in the
1498
revision_keys filter.
1499
:return: a dictionary mapping altered file-ids to an iterable of
1500
revision_ids. Each altered file-ids has the exact revision_ids that
1501
altered it listed explicitly.
1503
seen = set(self._serializer._find_text_key_references(
1504
line_iterator).iterkeys())
1505
parent_keys = self._find_parent_keys_of_revisions(revision_keys)
1506
parent_seen = set(self._serializer._find_text_key_references(
1507
self._inventory_xml_lines_for_keys(parent_keys)))
1508
new_keys = seen - parent_seen
1510
setdefault = result.setdefault
1511
for key in new_keys:
1512
setdefault(key[0], set()).add(key[-1])
1515
def _find_parent_keys_of_revisions(self, revision_keys):
1516
"""Similar to _find_parent_ids_of_revisions, but used with keys.
1518
:param revision_keys: An iterable of revision_keys.
1519
:return: The parents of all revision_keys that are not already in
1522
parent_map = self.revisions.get_parent_map(revision_keys)
1524
map(parent_keys.update, parent_map.itervalues())
1525
parent_keys.difference_update(revision_keys)
1526
parent_keys.discard(_mod_revision.NULL_REVISION)
1529
def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
1530
"""Find the file ids and versions affected by revisions.
1532
:param revisions: an iterable containing revision ids.
1533
:param _inv_weave: The inventory weave from this repository or None.
1534
If None, the inventory weave will be opened automatically.
1535
:return: a dictionary mapping altered file-ids to an iterable of
1536
revision_ids. Each altered file-ids has the exact revision_ids that
1537
altered it listed explicitly.
1539
selected_keys = set((revid,) for revid in revision_ids)
1540
w = _inv_weave or self.inventories
1541
return self._find_file_ids_from_xml_inventory_lines(
1542
w.iter_lines_added_or_present_in_keys(
1543
selected_keys, pb=None),
1546
def iter_files_bytes(self, desired_files):
1547
"""Iterate through file versions.
1549
Files will not necessarily be returned in the order they occur in
1550
desired_files. No specific order is guaranteed.
1552
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1553
value supplied by the caller as part of desired_files. It should
1554
uniquely identify the file version in the caller's context. (Examples:
1555
an index number or a TreeTransform trans_id.)
1557
bytes_iterator is an iterable of bytestrings for the file. The
1558
kind of iterable and length of the bytestrings are unspecified, but for
1559
this implementation, it is a list of bytes produced by
1560
VersionedFile.get_record_stream().
1562
:param desired_files: a list of (file_id, revision_id, identifier)
1566
for file_id, revision_id, callable_data in desired_files:
1567
text_keys[(file_id, revision_id)] = callable_data
1568
for record in self.texts.get_record_stream(text_keys, 'unordered', True):
1569
if record.storage_kind == 'absent':
1570
raise errors.RevisionNotPresent(record.key[1], record.key[0])
1571
yield text_keys[record.key], record.get_bytes_as('chunked')
1573
def _generate_text_key_index(self, text_key_references=None,
1575
"""Generate a new text key index for the repository.
1577
This is an expensive function that will take considerable time to run.
1579
:return: A dict mapping text keys ((file_id, revision_id) tuples) to a
1580
list of parents, also text keys. When a given key has no parents,
1581
the parents list will be [NULL_REVISION].
1583
# All revisions, to find inventory parents.
1584
if ancestors is None:
1585
graph = self.get_graph()
1586
ancestors = graph.get_parent_map(self.all_revision_ids())
1587
if text_key_references is None:
1588
text_key_references = self.find_text_key_references()
1589
pb = ui.ui_factory.nested_progress_bar()
1591
return self._do_generate_text_key_index(ancestors,
1592
text_key_references, pb)
1596
def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
1597
"""Helper for _generate_text_key_index to avoid deep nesting."""
1598
revision_order = tsort.topo_sort(ancestors)
1599
invalid_keys = set()
1601
for revision_id in revision_order:
1602
revision_keys[revision_id] = set()
1603
text_count = len(text_key_references)
1604
# a cache of the text keys to allow reuse; costs a dict of all the
1605
# keys, but saves a 2-tuple for every child of a given key.
1607
for text_key, valid in text_key_references.iteritems():
1609
invalid_keys.add(text_key)
1611
revision_keys[text_key[1]].add(text_key)
1612
text_key_cache[text_key] = text_key
1613
del text_key_references
1615
text_graph = graph.Graph(graph.DictParentsProvider(text_index))
1616
NULL_REVISION = _mod_revision.NULL_REVISION
1617
# Set a cache with a size of 10 - this suffices for bzr.dev but may be
1618
# too small for large or very branchy trees. However, for 55K path
1619
# trees, it would be easy to use too much memory trivially. Ideally we
1620
# could gauge this by looking at available real memory etc, but this is
1621
# always a tricky proposition.
1622
inventory_cache = lru_cache.LRUCache(10)
1623
batch_size = 10 # should be ~150MB on a 55K path tree
1624
batch_count = len(revision_order) / batch_size + 1
1626
pb.update(gettext("Calculating text parents"), processed_texts, text_count)
1627
for offset in xrange(batch_count):
1628
to_query = revision_order[offset * batch_size:(offset + 1) *
1632
for revision_id in to_query:
1633
parent_ids = ancestors[revision_id]
1634
for text_key in revision_keys[revision_id]:
1635
pb.update(gettext("Calculating text parents"), processed_texts)
1636
processed_texts += 1
1637
candidate_parents = []
1638
for parent_id in parent_ids:
1639
parent_text_key = (text_key[0], parent_id)
1641
check_parent = parent_text_key not in \
1642
revision_keys[parent_id]
1644
# the parent parent_id is a ghost:
1645
check_parent = False
1646
# truncate the derived graph against this ghost.
1647
parent_text_key = None
1649
# look at the parent commit details inventories to
1650
# determine possible candidates in the per file graph.
1653
inv = inventory_cache[parent_id]
1655
inv = self.revision_tree(parent_id).inventory
1656
inventory_cache[parent_id] = inv
1658
parent_entry = inv[text_key[0]]
1659
except (KeyError, errors.NoSuchId):
1661
if parent_entry is not None:
1663
text_key[0], parent_entry.revision)
1665
parent_text_key = None
1666
if parent_text_key is not None:
1667
candidate_parents.append(
1668
text_key_cache[parent_text_key])
1669
parent_heads = text_graph.heads(candidate_parents)
1670
new_parents = list(parent_heads)
1671
new_parents.sort(key=lambda x:candidate_parents.index(x))
1672
if new_parents == []:
1673
new_parents = [NULL_REVISION]
1674
text_index[text_key] = new_parents
1676
for text_key in invalid_keys:
1677
text_index[text_key] = [NULL_REVISION]
1680
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1681
"""Get an iterable listing the keys of all the data introduced by a set
1684
The keys will be ordered so that the corresponding items can be safely
1685
fetched and inserted in that order.
1687
:returns: An iterable producing tuples of (knit-kind, file-id,
1688
versions). knit-kind is one of 'file', 'inventory', 'signatures',
1689
'revisions'. file-id is None unless knit-kind is 'file'.
1691
for result in self._find_file_keys_to_fetch(revision_ids, _files_pb):
1694
for result in self._find_non_file_keys_to_fetch(revision_ids):
1697
def _find_file_keys_to_fetch(self, revision_ids, pb):
1698
# XXX: it's a bit weird to control the inventory weave caching in this
1699
# generator. Ideally the caching would be done in fetch.py I think. Or
1700
# maybe this generator should explicitly have the contract that it
1701
# should not be iterated until the previously yielded item has been
1703
inv_w = self.inventories
1705
# file ids that changed
1706
file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
1708
num_file_ids = len(file_ids)
1709
for file_id, altered_versions in file_ids.iteritems():
1711
pb.update(gettext("Fetch texts"), count, num_file_ids)
1713
yield ("file", file_id, altered_versions)
1715
def _find_non_file_keys_to_fetch(self, revision_ids):
1717
yield ("inventory", None, revision_ids)
1720
# XXX: Note ATM no callers actually pay attention to this return
1721
# instead they just use the list of revision ids and ignore
1722
# missing sigs. Consider removing this work entirely
1723
revisions_with_signatures = set(self.signatures.get_parent_map(
1724
[(r,) for r in revision_ids]))
1725
revisions_with_signatures = set(
1726
[r for (r,) in revisions_with_signatures])
1727
revisions_with_signatures.intersection_update(revision_ids)
1728
yield ("signatures", None, revisions_with_signatures)
1731
yield ("revisions", None, revision_ids)
1734
def get_inventory(self, revision_id):
1735
"""Get Inventory object by revision id."""
1736
return self.iter_inventories([revision_id]).next()
1738
def iter_inventories(self, revision_ids, ordering=None):
1739
"""Get many inventories by revision_ids.
1741
This will buffer some or all of the texts used in constructing the
1742
inventories in memory, but will only parse a single inventory at a
1745
:param revision_ids: The expected revision ids of the inventories.
1746
:param ordering: optional ordering, e.g. 'topological'. If not
1747
specified, the order of revision_ids will be preserved (by
1748
buffering if necessary).
1749
:return: An iterator of inventories.
1751
if ((None in revision_ids)
1752
or (_mod_revision.NULL_REVISION in revision_ids)):
1753
raise ValueError('cannot get null revision inventory')
1754
for inv, revid in self._iter_inventories(revision_ids, ordering):
1756
raise errors.NoSuchRevision(self, revid)
1759
def _iter_inventories(self, revision_ids, ordering):
1760
"""single-document based inventory iteration."""
1761
inv_xmls = self._iter_inventory_xmls(revision_ids, ordering)
1762
for text, revision_id in inv_xmls:
1764
yield None, revision_id
1766
yield self._deserialise_inventory(revision_id, text), revision_id
1768
def _iter_inventory_xmls(self, revision_ids, ordering):
1769
if ordering is None:
1770
order_as_requested = True
1771
ordering = 'unordered'
1773
order_as_requested = False
1774
keys = [(revision_id,) for revision_id in revision_ids]
1777
if order_as_requested:
1778
key_iter = iter(keys)
1779
next_key = key_iter.next()
1780
stream = self.inventories.get_record_stream(keys, ordering, True)
1782
for record in stream:
1783
if record.storage_kind != 'absent':
1784
chunks = record.get_bytes_as('chunked')
1785
if order_as_requested:
1786
text_chunks[record.key] = chunks
1788
yield ''.join(chunks), record.key[-1]
1790
yield None, record.key[-1]
1791
if order_as_requested:
1792
# Yield as many results as we can while preserving order.
1793
while next_key in text_chunks:
1794
chunks = text_chunks.pop(next_key)
1795
yield ''.join(chunks), next_key[-1]
1797
next_key = key_iter.next()
1798
except StopIteration:
1799
# We still want to fully consume the get_record_stream,
1800
# just in case it is not actually finished at this point
1804
def _deserialise_inventory(self, revision_id, xml):
1805
"""Transform the xml into an inventory object.
1807
:param revision_id: The expected revision id of the inventory.
1808
:param xml: A serialised inventory.
1810
result = self._serializer.read_inventory_from_string(xml, revision_id,
1811
entry_cache=self._inventory_entry_cache,
1812
return_from_cache=self._safe_to_return_from_cache)
1813
if result.revision_id != revision_id:
1814
raise AssertionError('revision id mismatch %s != %s' % (
1815
result.revision_id, revision_id))
1818
def get_serializer_format(self):
1819
return self._serializer.format_num
1822
def _get_inventory_xml(self, revision_id):
1823
"""Get serialized inventory as a string."""
1824
texts = self._iter_inventory_xmls([revision_id], 'unordered')
1825
text, revision_id = texts.next()
1827
raise errors.NoSuchRevision(self, revision_id)
1831
def revision_tree(self, revision_id):
1832
"""Return Tree for a revision on this branch.
1834
`revision_id` may be NULL_REVISION for the empty tree revision.
1836
revision_id = _mod_revision.ensure_null(revision_id)
1837
# TODO: refactor this to use an existing revision object
1838
# so we don't need to read it in twice.
1839
if revision_id == _mod_revision.NULL_REVISION:
1840
return InventoryRevisionTree(self,
1841
Inventory(root_id=None), _mod_revision.NULL_REVISION)
1843
inv = self.get_inventory(revision_id)
1844
return InventoryRevisionTree(self, inv, revision_id)
1846
def revision_trees(self, revision_ids):
1847
"""Return Trees for revisions in this repository.
1849
:param revision_ids: a sequence of revision-ids;
1850
a revision-id may not be None or 'null:'
1852
inventories = self.iter_inventories(revision_ids)
1853
for inv in inventories:
1854
yield InventoryRevisionTree(self, inv, inv.revision_id)
1856
def _filtered_revision_trees(self, revision_ids, file_ids):
1857
"""Return Tree for a revision on this branch with only some files.
1859
:param revision_ids: a sequence of revision-ids;
1860
a revision-id may not be None or 'null:'
1861
:param file_ids: if not None, the result is filtered
1862
so that only those file-ids, their parents and their
1863
children are included.
1865
inventories = self.iter_inventories(revision_ids)
1866
for inv in inventories:
1867
# Should we introduce a FilteredRevisionTree class rather
1868
# than pre-filter the inventory here?
1869
filtered_inv = inv.filter(file_ids)
1870
yield InventoryRevisionTree(self, filtered_inv, filtered_inv.revision_id)
1872
def get_parent_map(self, revision_ids):
1873
"""See graph.StackedParentsProvider.get_parent_map"""
1874
# revisions index works in keys; this just works in revisions
1875
# therefore wrap and unwrap
1878
for revision_id in revision_ids:
1879
if revision_id == _mod_revision.NULL_REVISION:
1880
result[revision_id] = ()
1881
elif revision_id is None:
1882
raise ValueError('get_parent_map(None) is not valid')
1884
query_keys.append((revision_id ,))
1885
for ((revision_id,), parent_keys) in \
1886
self.revisions.get_parent_map(query_keys).iteritems():
1888
result[revision_id] = tuple([parent_revid
1889
for (parent_revid,) in parent_keys])
1891
result[revision_id] = (_mod_revision.NULL_REVISION,)
1895
def get_known_graph_ancestry(self, revision_ids):
1896
"""Return the known graph for a set of revision ids and their ancestors.
1898
st = static_tuple.StaticTuple
1899
revision_keys = [st(r_id).intern() for r_id in revision_ids]
1900
known_graph = self.revisions.get_known_graph_ancestry(revision_keys)
1901
return graph.GraphThunkIdsToKeys(known_graph)
1904
def get_file_graph(self):
1905
"""Return the graph walker for text revisions."""
1906
return graph.Graph(self.texts)
1908
def revision_ids_to_search_result(self, result_set):
1909
"""Convert a set of revision ids to a graph SearchResult."""
1910
result_parents = set()
1911
for parents in self.get_graph().get_parent_map(
1912
result_set).itervalues():
1913
result_parents.update(parents)
1914
included_keys = result_set.intersection(result_parents)
1915
start_keys = result_set.difference(included_keys)
1916
exclude_keys = result_parents.difference(result_set)
1917
result = vf_search.SearchResult(start_keys, exclude_keys,
1918
len(result_set), result_set)
1921
def _get_versioned_file_checker(self, text_key_references=None,
1923
"""Return an object suitable for checking versioned files.
1925
:param text_key_references: if non-None, an already built
1926
dictionary mapping text keys ((fileid, revision_id) tuples)
1927
to whether they were referred to by the inventory of the
1928
revision_id that they contain. If None, this will be
1930
:param ancestors: Optional result from
1931
self.get_graph().get_parent_map(self.all_revision_ids()) if already
1934
return _VersionedFileChecker(self,
1935
text_key_references=text_key_references, ancestors=ancestors)
1938
def has_signature_for_revision_id(self, revision_id):
1939
"""Query for a revision signature for revision_id in the repository."""
1940
if not self.has_revision(revision_id):
1941
raise errors.NoSuchRevision(self, revision_id)
1942
sig_present = (1 == len(
1943
self.signatures.get_parent_map([(revision_id,)])))
1947
def get_signature_text(self, revision_id):
1948
"""Return the text for a signature."""
1949
stream = self.signatures.get_record_stream([(revision_id,)],
1951
record = stream.next()
1952
if record.storage_kind == 'absent':
1953
raise errors.NoSuchRevision(self, revision_id)
1954
return record.get_bytes_as('fulltext')
1957
def _check(self, revision_ids, callback_refs, check_repo):
1958
result = check.VersionedFileCheck(self, check_repo=check_repo)
1959
result.check(callback_refs)
1962
def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1963
"""Find revisions with different parent lists in the revision object
1964
and in the index graph.
1966
:param revisions_iterator: None, or an iterator of (revid,
1967
Revision-or-None). This iterator controls the revisions checked.
1968
:returns: an iterator yielding tuples of (revison-id, parents-in-index,
1969
parents-in-revision).
1971
if not self.is_locked():
1972
raise AssertionError()
1974
if revisions_iterator is None:
1975
revisions_iterator = self._iter_revisions(None)
1976
for revid, revision in revisions_iterator:
1977
if revision is None:
1979
parent_map = vf.get_parent_map([(revid,)])
1980
parents_according_to_index = tuple(parent[-1] for parent in
1981
parent_map[(revid,)])
1982
parents_according_to_revision = tuple(revision.parent_ids)
1983
if parents_according_to_index != parents_according_to_revision:
1984
yield (revid, parents_according_to_index,
1985
parents_according_to_revision)
1987
def _check_for_inconsistent_revision_parents(self):
1988
inconsistencies = list(self._find_inconsistent_revision_parents())
1990
raise errors.BzrCheckError(
1991
"Revision knit has inconsistent parents.")
1993
def _get_sink(self):
1994
"""Return a sink for streaming into this repository."""
1995
return StreamSink(self)
1997
def _get_source(self, to_format):
1998
"""Return a source for streaming from this repository."""
1999
return StreamSource(self, to_format)
2002
class MetaDirVersionedFileRepository(MetaDirRepository,
2003
VersionedFileRepository):
2004
"""Repositories in a meta-dir, that work via versioned file objects."""
2006
def __init__(self, _format, a_bzrdir, control_files):
2007
super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
2011
class MetaDirVersionedFileRepositoryFormat(RepositoryFormatMetaDir,
2012
VersionedFileRepositoryFormat):
2013
"""Base class for repository formats using versioned files in metadirs."""
2016
class StreamSink(object):
2017
"""An object that can insert a stream into a repository.
2019
This interface handles the complexity of reserialising inventories and
2020
revisions from different formats, and allows unidirectional insertion into
2021
stacked repositories without looking for the missing basis parents
2025
def __init__(self, target_repo):
2026
self.target_repo = target_repo
2028
def insert_stream(self, stream, src_format, resume_tokens):
2029
"""Insert a stream's content into the target repository.
2031
:param src_format: a bzr repository format.
2033
:return: a list of resume tokens and an iterable of keys additional
2034
items required before the insertion can be completed.
2036
self.target_repo.lock_write()
2039
self.target_repo.resume_write_group(resume_tokens)
2042
self.target_repo.start_write_group()
2045
# locked_insert_stream performs a commit|suspend.
2046
missing_keys = self.insert_stream_without_locking(stream,
2047
src_format, is_resume)
2049
# suspend the write group and tell the caller what we is
2050
# missing. We know we can suspend or else we would not have
2051
# entered this code path. (All repositories that can handle
2052
# missing keys can handle suspending a write group).
2053
write_group_tokens = self.target_repo.suspend_write_group()
2054
return write_group_tokens, missing_keys
2055
hint = self.target_repo.commit_write_group()
2056
to_serializer = self.target_repo._format._serializer
2057
src_serializer = src_format._serializer
2058
if (to_serializer != src_serializer and
2059
self.target_repo._format.pack_compresses):
2060
self.target_repo.pack(hint=hint)
2063
self.target_repo.abort_write_group(suppress_errors=True)
2066
self.target_repo.unlock()
2068
def insert_stream_without_locking(self, stream, src_format,
2070
"""Insert a stream's content into the target repository.
2072
This assumes that you already have a locked repository and an active
2075
:param src_format: a bzr repository format.
2076
:param is_resume: Passed down to get_missing_parent_inventories to
2077
indicate if we should be checking for missing texts at the same
2080
:return: A set of keys that are missing.
2082
if not self.target_repo.is_write_locked():
2083
raise errors.ObjectNotLocked(self)
2084
if not self.target_repo.is_in_write_group():
2085
raise errors.BzrError('you must already be in a write group')
2086
to_serializer = self.target_repo._format._serializer
2087
src_serializer = src_format._serializer
2089
if to_serializer == src_serializer:
2090
# If serializers match and the target is a pack repository, set the
2091
# write cache size on the new pack. This avoids poor performance
2092
# on transports where append is unbuffered (such as
2093
# RemoteTransport). This is safe to do because nothing should read
2094
# back from the target repository while a stream with matching
2095
# serialization is being inserted.
2096
# The exception is that a delta record from the source that should
2097
# be a fulltext may need to be expanded by the target (see
2098
# test_fetch_revisions_with_deltas_into_pack); but we take care to
2099
# explicitly flush any buffered writes first in that rare case.
2101
new_pack = self.target_repo._pack_collection._new_pack
2102
except AttributeError:
2103
# Not a pack repository
2106
new_pack.set_write_cache_size(1024*1024)
2107
for substream_type, substream in stream:
2108
if 'stream' in debug.debug_flags:
2109
mutter('inserting substream: %s', substream_type)
2110
if substream_type == 'texts':
2111
self.target_repo.texts.insert_record_stream(substream)
2112
elif substream_type == 'inventories':
2113
if src_serializer == to_serializer:
2114
self.target_repo.inventories.insert_record_stream(
2117
self._extract_and_insert_inventories(
2118
substream, src_serializer)
2119
elif substream_type == 'inventory-deltas':
2120
self._extract_and_insert_inventory_deltas(
2121
substream, src_serializer)
2122
elif substream_type == 'chk_bytes':
2123
# XXX: This doesn't support conversions, as it assumes the
2124
# conversion was done in the fetch code.
2125
self.target_repo.chk_bytes.insert_record_stream(substream)
2126
elif substream_type == 'revisions':
2127
# This may fallback to extract-and-insert more often than
2128
# required if the serializers are different only in terms of
2130
if src_serializer == to_serializer:
2131
self.target_repo.revisions.insert_record_stream(substream)
2133
self._extract_and_insert_revisions(substream,
2135
elif substream_type == 'signatures':
2136
self.target_repo.signatures.insert_record_stream(substream)
2138
raise AssertionError('kaboom! %s' % (substream_type,))
2139
# Done inserting data, and the missing_keys calculations will try to
2140
# read back from the inserted data, so flush the writes to the new pack
2141
# (if this is pack format).
2142
if new_pack is not None:
2143
new_pack._write_data('', flush=True)
2144
# Find all the new revisions (including ones from resume_tokens)
2145
missing_keys = self.target_repo.get_missing_parent_inventories(
2146
check_for_missing_texts=is_resume)
2148
for prefix, versioned_file in (
2149
('texts', self.target_repo.texts),
2150
('inventories', self.target_repo.inventories),
2151
('revisions', self.target_repo.revisions),
2152
('signatures', self.target_repo.signatures),
2153
('chk_bytes', self.target_repo.chk_bytes),
2155
if versioned_file is None:
2157
# TODO: key is often going to be a StaticTuple object
2158
# I don't believe we can define a method by which
2159
# (prefix,) + StaticTuple will work, though we could
2160
# define a StaticTuple.sq_concat that would allow you to
2161
# pass in either a tuple or a StaticTuple as the second
2162
# object, so instead we could have:
2163
# StaticTuple(prefix) + key here...
2164
missing_keys.update((prefix,) + key for key in
2165
versioned_file.get_missing_compression_parent_keys())
2166
except NotImplementedError:
2167
# cannot even attempt suspending, and missing would have failed
2168
# during stream insertion.
2169
missing_keys = set()
2172
def _extract_and_insert_inventory_deltas(self, substream, serializer):
2173
target_rich_root = self.target_repo._format.rich_root_data
2174
target_tree_refs = self.target_repo._format.supports_tree_reference
2175
for record in substream:
2176
# Insert the delta directly
2177
inventory_delta_bytes = record.get_bytes_as('fulltext')
2178
deserialiser = inventory_delta.InventoryDeltaDeserializer()
2180
parse_result = deserialiser.parse_text_bytes(
2181
inventory_delta_bytes)
2182
except inventory_delta.IncompatibleInventoryDelta, err:
2183
mutter("Incompatible delta: %s", err.msg)
2184
raise errors.IncompatibleRevision(self.target_repo._format)
2185
basis_id, new_id, rich_root, tree_refs, inv_delta = parse_result
2186
revision_id = new_id
2187
parents = [key[0] for key in record.parents]
2188
self.target_repo.add_inventory_by_delta(
2189
basis_id, inv_delta, revision_id, parents)
2191
def _extract_and_insert_inventories(self, substream, serializer,
2193
"""Generate a new inventory versionedfile in target, converting data.
2195
The inventory is retrieved from the source, (deserializing it), and
2196
stored in the target (reserializing it in a different format).
2198
target_rich_root = self.target_repo._format.rich_root_data
2199
target_tree_refs = self.target_repo._format.supports_tree_reference
2200
for record in substream:
2201
# It's not a delta, so it must be a fulltext in the source
2202
# serializer's format.
2203
bytes = record.get_bytes_as('fulltext')
2204
revision_id = record.key[0]
2205
inv = serializer.read_inventory_from_string(bytes, revision_id)
2206
parents = [key[0] for key in record.parents]
2207
self.target_repo.add_inventory(revision_id, inv, parents)
2208
# No need to keep holding this full inv in memory when the rest of
2209
# the substream is likely to be all deltas.
2212
def _extract_and_insert_revisions(self, substream, serializer):
2213
for record in substream:
2214
bytes = record.get_bytes_as('fulltext')
2215
revision_id = record.key[0]
2216
rev = serializer.read_revision_from_string(bytes)
2217
if rev.revision_id != revision_id:
2218
raise AssertionError('wtf: %s != %s' % (rev, revision_id))
2219
self.target_repo.add_revision(revision_id, rev)
2222
if self.target_repo._format._fetch_reconcile:
2223
self.target_repo.reconcile()
2226
class StreamSource(object):
2227
"""A source of a stream for fetching between repositories."""
2229
def __init__(self, from_repository, to_format):
2230
"""Create a StreamSource streaming from from_repository."""
2231
self.from_repository = from_repository
2232
self.to_format = to_format
2233
self._record_counter = RecordCounter()
2235
def delta_on_metadata(self):
2236
"""Return True if delta's are permitted on metadata streams.
2238
That is on revisions and signatures.
2240
src_serializer = self.from_repository._format._serializer
2241
target_serializer = self.to_format._serializer
2242
return (self.to_format._fetch_uses_deltas and
2243
src_serializer == target_serializer)
2245
def _fetch_revision_texts(self, revs):
2246
# fetch signatures first and then the revision texts
2247
# may need to be a InterRevisionStore call here.
2248
from_sf = self.from_repository.signatures
2249
# A missing signature is just skipped.
2250
keys = [(rev_id,) for rev_id in revs]
2251
signatures = versionedfile.filter_absent(from_sf.get_record_stream(
2253
self.to_format._fetch_order,
2254
not self.to_format._fetch_uses_deltas))
2255
# If a revision has a delta, this is actually expanded inside the
2256
# insert_record_stream code now, which is an alternate fix for
2258
from_rf = self.from_repository.revisions
2259
revisions = from_rf.get_record_stream(
2261
self.to_format._fetch_order,
2262
not self.delta_on_metadata())
2263
return [('signatures', signatures), ('revisions', revisions)]
2265
def _generate_root_texts(self, revs):
2266
"""This will be called by get_stream between fetching weave texts and
2267
fetching the inventory weave.
2269
if self._rich_root_upgrade():
2270
return _mod_fetch.Inter1and2Helper(
2271
self.from_repository).generate_root_texts(revs)
2275
def get_stream(self, search):
2277
revs = search.get_keys()
2278
graph = self.from_repository.get_graph()
2279
revs = tsort.topo_sort(graph.get_parent_map(revs))
2280
data_to_fetch = self.from_repository.item_keys_introduced_by(revs)
2282
for knit_kind, file_id, revisions in data_to_fetch:
2283
if knit_kind != phase:
2285
# Make a new progress bar for this phase
2286
if knit_kind == "file":
2287
# Accumulate file texts
2288
text_keys.extend([(file_id, revision) for revision in
2290
elif knit_kind == "inventory":
2291
# Now copy the file texts.
2292
from_texts = self.from_repository.texts
2293
yield ('texts', from_texts.get_record_stream(
2294
text_keys, self.to_format._fetch_order,
2295
not self.to_format._fetch_uses_deltas))
2296
# Cause an error if a text occurs after we have done the
2299
# Before we process the inventory we generate the root
2300
# texts (if necessary) so that the inventories references
2302
for _ in self._generate_root_texts(revs):
2304
# we fetch only the referenced inventories because we do not
2305
# know for unselected inventories whether all their required
2306
# texts are present in the other repository - it could be
2308
for info in self._get_inventory_stream(revs):
2310
elif knit_kind == "signatures":
2311
# Nothing to do here; this will be taken care of when
2312
# _fetch_revision_texts happens.
2314
elif knit_kind == "revisions":
2315
for record in self._fetch_revision_texts(revs):
2318
raise AssertionError("Unknown knit kind %r" % knit_kind)
2320
def get_stream_for_missing_keys(self, missing_keys):
2321
# missing keys can only occur when we are byte copying and not
2322
# translating (because translation means we don't send
2323
# unreconstructable deltas ever).
2325
keys['texts'] = set()
2326
keys['revisions'] = set()
2327
keys['inventories'] = set()
2328
keys['chk_bytes'] = set()
2329
keys['signatures'] = set()
2330
for key in missing_keys:
2331
keys[key[0]].add(key[1:])
2332
if len(keys['revisions']):
2333
# If we allowed copying revisions at this point, we could end up
2334
# copying a revision without copying its required texts: a
2335
# violation of the requirements for repository integrity.
2336
raise AssertionError(
2337
'cannot copy revisions to fill in missing deltas %s' % (
2338
keys['revisions'],))
2339
for substream_kind, keys in keys.iteritems():
2340
vf = getattr(self.from_repository, substream_kind)
2341
if vf is None and keys:
2342
raise AssertionError(
2343
"cannot fill in keys for a versioned file we don't"
2344
" have: %s needs %s" % (substream_kind, keys))
2346
# No need to stream something we don't have
2348
if substream_kind == 'inventories':
2349
# Some missing keys are genuinely ghosts, filter those out.
2350
present = self.from_repository.inventories.get_parent_map(keys)
2351
revs = [key[0] for key in present]
2352
# Get the inventory stream more-or-less as we do for the
2353
# original stream; there's no reason to assume that records
2354
# direct from the source will be suitable for the sink. (Think
2355
# e.g. 2a -> 1.9-rich-root).
2356
for info in self._get_inventory_stream(revs, missing=True):
2360
# Ask for full texts always so that we don't need more round trips
2361
# after this stream.
2362
# Some of the missing keys are genuinely ghosts, so filter absent
2363
# records. The Sink is responsible for doing another check to
2364
# ensure that ghosts don't introduce missing data for future
2366
stream = versionedfile.filter_absent(vf.get_record_stream(keys,
2367
self.to_format._fetch_order, True))
2368
yield substream_kind, stream
2370
def inventory_fetch_order(self):
2371
if self._rich_root_upgrade():
2372
return 'topological'
2374
return self.to_format._fetch_order
2376
def _rich_root_upgrade(self):
2377
return (not self.from_repository._format.rich_root_data and
2378
self.to_format.rich_root_data)
2380
def _get_inventory_stream(self, revision_ids, missing=False):
2381
from_format = self.from_repository._format
2382
if (from_format.supports_chks and self.to_format.supports_chks and
2383
from_format.network_name() == self.to_format.network_name()):
2384
raise AssertionError(
2385
"this case should be handled by GroupCHKStreamSource")
2386
elif 'forceinvdeltas' in debug.debug_flags:
2387
return self._get_convertable_inventory_stream(revision_ids,
2388
delta_versus_null=missing)
2389
elif from_format.network_name() == self.to_format.network_name():
2391
return self._get_simple_inventory_stream(revision_ids,
2393
elif (not from_format.supports_chks and not self.to_format.supports_chks
2394
and from_format._serializer == self.to_format._serializer):
2395
# Essentially the same format.
2396
return self._get_simple_inventory_stream(revision_ids,
2399
# Any time we switch serializations, we want to use an
2400
# inventory-delta based approach.
2401
return self._get_convertable_inventory_stream(revision_ids,
2402
delta_versus_null=missing)
2404
def _get_simple_inventory_stream(self, revision_ids, missing=False):
2405
# NB: This currently reopens the inventory weave in source;
2406
# using a single stream interface instead would avoid this.
2407
from_weave = self.from_repository.inventories
2409
delta_closure = True
2411
delta_closure = not self.delta_on_metadata()
2412
yield ('inventories', from_weave.get_record_stream(
2413
[(rev_id,) for rev_id in revision_ids],
2414
self.inventory_fetch_order(), delta_closure))
2416
def _get_convertable_inventory_stream(self, revision_ids,
2417
delta_versus_null=False):
2418
# The two formats are sufficiently different that there is no fast
2419
# path, so we need to send just inventorydeltas, which any
2420
# sufficiently modern client can insert into any repository.
2421
# The StreamSink code expects to be able to
2422
# convert on the target, so we need to put bytes-on-the-wire that can
2423
# be converted. That means inventory deltas (if the remote is <1.19,
2424
# RemoteStreamSink will fallback to VFS to insert the deltas).
2425
yield ('inventory-deltas',
2426
self._stream_invs_as_deltas(revision_ids,
2427
delta_versus_null=delta_versus_null))
2429
def _stream_invs_as_deltas(self, revision_ids, delta_versus_null=False):
2430
"""Return a stream of inventory-deltas for the given rev ids.
2432
:param revision_ids: The list of inventories to transmit
2433
:param delta_versus_null: Don't try to find a minimal delta for this
2434
entry, instead compute the delta versus the NULL_REVISION. This
2435
effectively streams a complete inventory. Used for stuff like
2436
filling in missing parents, etc.
2438
from_repo = self.from_repository
2439
revision_keys = [(rev_id,) for rev_id in revision_ids]
2440
parent_map = from_repo.inventories.get_parent_map(revision_keys)
2441
# XXX: possibly repos could implement a more efficient iter_inv_deltas
2443
inventories = self.from_repository.iter_inventories(
2444
revision_ids, 'topological')
2445
format = from_repo._format
2446
invs_sent_so_far = set([_mod_revision.NULL_REVISION])
2447
inventory_cache = lru_cache.LRUCache(50)
2448
null_inventory = from_repo.revision_tree(
2449
_mod_revision.NULL_REVISION).inventory
2450
# XXX: ideally the rich-root/tree-refs flags would be per-revision, not
2451
# per-repo (e.g. streaming a non-rich-root revision out of a rich-root
2452
# repo back into a non-rich-root repo ought to be allowed)
2453
serializer = inventory_delta.InventoryDeltaSerializer(
2454
versioned_root=format.rich_root_data,
2455
tree_references=format.supports_tree_reference)
2456
for inv in inventories:
2457
key = (inv.revision_id,)
2458
parent_keys = parent_map.get(key, ())
2460
if not delta_versus_null and parent_keys:
2461
# The caller did not ask for complete inventories and we have
2462
# some parents that we can delta against. Make a delta against
2463
# each parent so that we can find the smallest.
2464
parent_ids = [parent_key[0] for parent_key in parent_keys]
2465
for parent_id in parent_ids:
2466
if parent_id not in invs_sent_so_far:
2467
# We don't know that the remote side has this basis, so
2470
if parent_id == _mod_revision.NULL_REVISION:
2471
parent_inv = null_inventory
2473
parent_inv = inventory_cache.get(parent_id, None)
2474
if parent_inv is None:
2475
parent_inv = from_repo.get_inventory(parent_id)
2476
candidate_delta = inv._make_delta(parent_inv)
2477
if (delta is None or
2478
len(delta) > len(candidate_delta)):
2479
delta = candidate_delta
2480
basis_id = parent_id
2482
# Either none of the parents ended up being suitable, or we
2483
# were asked to delta against NULL
2484
basis_id = _mod_revision.NULL_REVISION
2485
delta = inv._make_delta(null_inventory)
2486
invs_sent_so_far.add(inv.revision_id)
2487
inventory_cache[inv.revision_id] = inv
2488
delta_serialized = ''.join(
2489
serializer.delta_to_lines(basis_id, key[-1], delta))
2490
yield versionedfile.FulltextContentFactory(
2491
key, parent_keys, None, delta_serialized)
2494
class _VersionedFileChecker(object):
2496
def __init__(self, repository, text_key_references=None, ancestors=None):
2497
self.repository = repository
2498
self.text_index = self.repository._generate_text_key_index(
2499
text_key_references=text_key_references, ancestors=ancestors)
2501
def calculate_file_version_parents(self, text_key):
2502
"""Calculate the correct parents for a file version according to
2505
parent_keys = self.text_index[text_key]
2506
if parent_keys == [_mod_revision.NULL_REVISION]:
2508
return tuple(parent_keys)
2510
def check_file_version_parents(self, texts, progress_bar=None):
2511
"""Check the parents stored in a versioned file are correct.
2513
It also detects file versions that are not referenced by their
2514
corresponding revision's inventory.
2516
:returns: A tuple of (wrong_parents, dangling_file_versions).
2517
wrong_parents is a dict mapping {revision_id: (stored_parents,
2518
correct_parents)} for each revision_id where the stored parents
2519
are not correct. dangling_file_versions is a set of (file_id,
2520
revision_id) tuples for versions that are present in this versioned
2521
file, but not used by the corresponding inventory.
2523
local_progress = None
2524
if progress_bar is None:
2525
local_progress = ui.ui_factory.nested_progress_bar()
2526
progress_bar = local_progress
2528
return self._check_file_version_parents(texts, progress_bar)
2531
local_progress.finished()
2533
def _check_file_version_parents(self, texts, progress_bar):
2534
"""See check_file_version_parents."""
2536
self.file_ids = set([file_id for file_id, _ in
2537
self.text_index.iterkeys()])
2538
# text keys is now grouped by file_id
2539
n_versions = len(self.text_index)
2540
progress_bar.update(gettext('loading text store'), 0, n_versions)
2541
parent_map = self.repository.texts.get_parent_map(self.text_index)
2542
# On unlistable transports this could well be empty/error...
2543
text_keys = self.repository.texts.keys()
2544
unused_keys = frozenset(text_keys) - set(self.text_index)
2545
for num, key in enumerate(self.text_index.iterkeys()):
2546
progress_bar.update(gettext('checking text graph'), num, n_versions)
2547
correct_parents = self.calculate_file_version_parents(key)
2549
knit_parents = parent_map[key]
2550
except errors.RevisionNotPresent:
2553
if correct_parents != knit_parents:
2554
wrong_parents[key] = (knit_parents, correct_parents)
2555
return wrong_parents, unused_keys
2558
class InterVersionedFileRepository(InterRepository):
2560
_walk_to_common_revisions_batch_size = 50
2562
supports_fetch_spec = True
2565
def fetch(self, revision_id=None, find_ghosts=False,
2567
"""Fetch the content required to construct revision_id.
2569
The content is copied from self.source to self.target.
2571
:param revision_id: if None all content is copied, if NULL_REVISION no
2575
if self.target._format.experimental:
2576
ui.ui_factory.show_user_warning('experimental_format_fetch',
2577
from_format=self.source._format,
2578
to_format=self.target._format)
2579
from bzrlib.fetch import RepoFetcher
2580
# See <https://launchpad.net/bugs/456077> asking for a warning here
2581
if self.source._format.network_name() != self.target._format.network_name():
2582
ui.ui_factory.show_user_warning('cross_format_fetch',
2583
from_format=self.source._format,
2584
to_format=self.target._format)
2585
f = RepoFetcher(to_repository=self.target,
2586
from_repository=self.source,
2587
last_revision=revision_id,
2588
fetch_spec=fetch_spec,
2589
find_ghosts=find_ghosts)
2591
def _walk_to_common_revisions(self, revision_ids, if_present_ids=None):
2592
"""Walk out from revision_ids in source to revisions target has.
2594
:param revision_ids: The start point for the search.
2595
:return: A set of revision ids.
2597
target_graph = self.target.get_graph()
2598
revision_ids = frozenset(revision_ids)
2600
all_wanted_revs = revision_ids.union(if_present_ids)
2602
all_wanted_revs = revision_ids
2603
missing_revs = set()
2604
source_graph = self.source.get_graph()
2605
# ensure we don't pay silly lookup costs.
2606
searcher = source_graph._make_breadth_first_searcher(all_wanted_revs)
2607
null_set = frozenset([_mod_revision.NULL_REVISION])
2608
searcher_exhausted = False
2612
# Iterate the searcher until we have enough next_revs
2613
while len(next_revs) < self._walk_to_common_revisions_batch_size:
2615
next_revs_part, ghosts_part = searcher.next_with_ghosts()
2616
next_revs.update(next_revs_part)
2617
ghosts.update(ghosts_part)
2618
except StopIteration:
2619
searcher_exhausted = True
2621
# If there are ghosts in the source graph, and the caller asked for
2622
# them, make sure that they are present in the target.
2623
# We don't care about other ghosts as we can't fetch them and
2624
# haven't been asked to.
2625
ghosts_to_check = set(revision_ids.intersection(ghosts))
2626
revs_to_get = set(next_revs).union(ghosts_to_check)
2628
have_revs = set(target_graph.get_parent_map(revs_to_get))
2629
# we always have NULL_REVISION present.
2630
have_revs = have_revs.union(null_set)
2631
# Check if the target is missing any ghosts we need.
2632
ghosts_to_check.difference_update(have_revs)
2634
# One of the caller's revision_ids is a ghost in both the
2635
# source and the target.
2636
raise errors.NoSuchRevision(
2637
self.source, ghosts_to_check.pop())
2638
missing_revs.update(next_revs - have_revs)
2639
# Because we may have walked past the original stop point, make
2640
# sure everything is stopped
2641
stop_revs = searcher.find_seen_ancestors(have_revs)
2642
searcher.stop_searching_any(stop_revs)
2643
if searcher_exhausted:
2645
(started_keys, excludes, included_keys) = searcher.get_state()
2646
return vf_search.SearchResult(started_keys, excludes,
2647
len(included_keys), included_keys)
2650
def search_missing_revision_ids(self,
2651
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
2652
find_ghosts=True, revision_ids=None, if_present_ids=None,
2654
"""Return the revision ids that source has that target does not.
2656
:param revision_id: only return revision ids included by this
2658
:param revision_ids: return revision ids included by these
2659
revision_ids. NoSuchRevision will be raised if any of these
2660
revisions are not present.
2661
:param if_present_ids: like revision_ids, but will not cause
2662
NoSuchRevision if any of these are absent, instead they will simply
2663
not be in the result. This is useful for e.g. finding revisions
2664
to fetch for tags, which may reference absent revisions.
2665
:param find_ghosts: If True find missing revisions in deep history
2666
rather than just finding the surface difference.
2667
:return: A bzrlib.graph.SearchResult.
2669
if symbol_versioning.deprecated_passed(revision_id):
2670
symbol_versioning.warn(
2671
'search_missing_revision_ids(revision_id=...) was '
2672
'deprecated in 2.4. Use revision_ids=[...] instead.',
2673
DeprecationWarning, stacklevel=2)
2674
if revision_ids is not None:
2675
raise AssertionError(
2676
'revision_ids is mutually exclusive with revision_id')
2677
if revision_id is not None:
2678
revision_ids = [revision_id]
2680
# stop searching at found target revisions.
2681
if not find_ghosts and (revision_ids is not None or if_present_ids is
2683
result = self._walk_to_common_revisions(revision_ids,
2684
if_present_ids=if_present_ids)
2687
result_set = result.get_keys()
2689
# generic, possibly worst case, slow code path.
2690
target_ids = set(self.target.all_revision_ids())
2691
source_ids = self._present_source_revisions_for(
2692
revision_ids, if_present_ids)
2693
result_set = set(source_ids).difference(target_ids)
2694
if limit is not None:
2695
topo_ordered = self.source.get_graph().iter_topo_order(result_set)
2696
result_set = set(itertools.islice(topo_ordered, limit))
2697
return self.source.revision_ids_to_search_result(result_set)
2699
def _present_source_revisions_for(self, revision_ids, if_present_ids=None):
2700
"""Returns set of all revisions in ancestry of revision_ids present in
2703
:param revision_ids: if None, all revisions in source are returned.
2704
:param if_present_ids: like revision_ids, but if any/all of these are
2705
absent no error is raised.
2707
if revision_ids is not None or if_present_ids is not None:
2708
# First, ensure all specified revisions exist. Callers expect
2709
# NoSuchRevision when they pass absent revision_ids here.
2710
if revision_ids is None:
2711
revision_ids = set()
2712
if if_present_ids is None:
2713
if_present_ids = set()
2714
revision_ids = set(revision_ids)
2715
if_present_ids = set(if_present_ids)
2716
all_wanted_ids = revision_ids.union(if_present_ids)
2717
graph = self.source.get_graph()
2718
present_revs = set(graph.get_parent_map(all_wanted_ids))
2719
missing = revision_ids.difference(present_revs)
2721
raise errors.NoSuchRevision(self.source, missing.pop())
2722
found_ids = all_wanted_ids.intersection(present_revs)
2723
source_ids = [rev_id for (rev_id, parents) in
2724
graph.iter_ancestry(found_ids)
2725
if rev_id != _mod_revision.NULL_REVISION
2726
and parents is not None]
2728
source_ids = self.source.all_revision_ids()
2729
return set(source_ids)
2732
def _get_repo_format_to_test(self):
2736
def is_compatible(cls, source, target):
2737
# The default implementation is compatible with everything
2738
return (source._format.supports_full_versioned_files and
2739
target._format.supports_full_versioned_files)
2742
class InterDifferingSerializer(InterVersionedFileRepository):
2745
def _get_repo_format_to_test(self):
2749
def is_compatible(source, target):
2750
if not source._format.supports_full_versioned_files:
2752
if not target._format.supports_full_versioned_files:
2754
# This is redundant with format.check_conversion_target(), however that
2755
# raises an exception, and we just want to say "False" as in we won't
2756
# support converting between these formats.
2757
if 'IDS_never' in debug.debug_flags:
2759
if source.supports_rich_root() and not target.supports_rich_root():
2761
if (source._format.supports_tree_reference
2762
and not target._format.supports_tree_reference):
2764
if target._fallback_repositories and target._format.supports_chks:
2765
# IDS doesn't know how to copy CHKs for the parent inventories it
2766
# adds to stacked repos.
2768
if 'IDS_always' in debug.debug_flags:
2770
# Only use this code path for local source and target. IDS does far
2771
# too much IO (both bandwidth and roundtrips) over a network.
2772
if not source.bzrdir.transport.base.startswith('file:///'):
2774
if not target.bzrdir.transport.base.startswith('file:///'):
2778
def _get_trees(self, revision_ids, cache):
2780
for rev_id in revision_ids:
2782
possible_trees.append((rev_id, cache[rev_id]))
2784
# Not cached, but inventory might be present anyway.
2786
tree = self.source.revision_tree(rev_id)
2787
except errors.NoSuchRevision:
2788
# Nope, parent is ghost.
2791
cache[rev_id] = tree
2792
possible_trees.append((rev_id, tree))
2793
return possible_trees
2795
def _get_delta_for_revision(self, tree, parent_ids, possible_trees):
2796
"""Get the best delta and base for this revision.
2798
:return: (basis_id, delta)
2801
# Generate deltas against each tree, to find the shortest.
2802
texts_possibly_new_in_tree = set()
2803
for basis_id, basis_tree in possible_trees:
2804
delta = tree.inventory._make_delta(basis_tree.inventory)
2805
for old_path, new_path, file_id, new_entry in delta:
2806
if new_path is None:
2807
# This file_id isn't present in the new rev, so we don't
2811
# Rich roots are handled elsewhere...
2813
kind = new_entry.kind
2814
if kind != 'directory' and kind != 'file':
2815
# No text record associated with this inventory entry.
2817
# This is a directory or file that has changed somehow.
2818
texts_possibly_new_in_tree.add((file_id, new_entry.revision))
2819
deltas.append((len(delta), basis_id, delta))
2821
return deltas[0][1:]
2823
def _fetch_parent_invs_for_stacking(self, parent_map, cache):
2824
"""Find all parent revisions that are absent, but for which the
2825
inventory is present, and copy those inventories.
2827
This is necessary to preserve correctness when the source is stacked
2828
without fallbacks configured. (Note that in cases like upgrade the
2829
source may be not have _fallback_repositories even though it is
2833
for parents in parent_map.values():
2834
parent_revs.update(parents)
2835
present_parents = self.source.get_parent_map(parent_revs)
2836
absent_parents = set(parent_revs).difference(present_parents)
2837
parent_invs_keys_for_stacking = self.source.inventories.get_parent_map(
2838
(rev_id,) for rev_id in absent_parents)
2839
parent_inv_ids = [key[-1] for key in parent_invs_keys_for_stacking]
2840
for parent_tree in self.source.revision_trees(parent_inv_ids):
2841
current_revision_id = parent_tree.get_revision_id()
2842
parents_parents_keys = parent_invs_keys_for_stacking[
2843
(current_revision_id,)]
2844
parents_parents = [key[-1] for key in parents_parents_keys]
2845
basis_id = _mod_revision.NULL_REVISION
2846
basis_tree = self.source.revision_tree(basis_id)
2847
delta = parent_tree.inventory._make_delta(basis_tree.inventory)
2848
self.target.add_inventory_by_delta(
2849
basis_id, delta, current_revision_id, parents_parents)
2850
cache[current_revision_id] = parent_tree
2852
def _fetch_batch(self, revision_ids, basis_id, cache):
2853
"""Fetch across a few revisions.
2855
:param revision_ids: The revisions to copy
2856
:param basis_id: The revision_id of a tree that must be in cache, used
2857
as a basis for delta when no other base is available
2858
:param cache: A cache of RevisionTrees that we can use.
2859
:return: The revision_id of the last converted tree. The RevisionTree
2860
for it will be in cache
2862
# Walk though all revisions; get inventory deltas, copy referenced
2863
# texts that delta references, insert the delta, revision and
2865
root_keys_to_create = set()
2868
pending_revisions = []
2869
parent_map = self.source.get_parent_map(revision_ids)
2870
self._fetch_parent_invs_for_stacking(parent_map, cache)
2871
self.source._safe_to_return_from_cache = True
2872
for tree in self.source.revision_trees(revision_ids):
2873
# Find a inventory delta for this revision.
2874
# Find text entries that need to be copied, too.
2875
current_revision_id = tree.get_revision_id()
2876
parent_ids = parent_map.get(current_revision_id, ())
2877
parent_trees = self._get_trees(parent_ids, cache)
2878
possible_trees = list(parent_trees)
2879
if len(possible_trees) == 0:
2880
# There either aren't any parents, or the parents are ghosts,
2881
# so just use the last converted tree.
2882
possible_trees.append((basis_id, cache[basis_id]))
2883
basis_id, delta = self._get_delta_for_revision(tree, parent_ids,
2885
revision = self.source.get_revision(current_revision_id)
2886
pending_deltas.append((basis_id, delta,
2887
current_revision_id, revision.parent_ids))
2888
if self._converting_to_rich_root:
2889
self._revision_id_to_root_id[current_revision_id] = \
2891
# Determine which texts are in present in this revision but not in
2892
# any of the available parents.
2893
texts_possibly_new_in_tree = set()
2894
for old_path, new_path, file_id, entry in delta:
2895
if new_path is None:
2896
# This file_id isn't present in the new rev
2900
if not self.target.supports_rich_root():
2901
# The target doesn't support rich root, so we don't
2904
if self._converting_to_rich_root:
2905
# This can't be copied normally, we have to insert
2907
root_keys_to_create.add((file_id, entry.revision))
2910
texts_possibly_new_in_tree.add((file_id, entry.revision))
2911
for basis_id, basis_tree in possible_trees:
2912
basis_inv = basis_tree.inventory
2913
for file_key in list(texts_possibly_new_in_tree):
2914
file_id, file_revision = file_key
2916
entry = basis_inv[file_id]
2917
except errors.NoSuchId:
2919
if entry.revision == file_revision:
2920
texts_possibly_new_in_tree.remove(file_key)
2921
text_keys.update(texts_possibly_new_in_tree)
2922
pending_revisions.append(revision)
2923
cache[current_revision_id] = tree
2924
basis_id = current_revision_id
2925
self.source._safe_to_return_from_cache = False
2927
from_texts = self.source.texts
2928
to_texts = self.target.texts
2929
if root_keys_to_create:
2930
root_stream = _mod_fetch._new_root_data_stream(
2931
root_keys_to_create, self._revision_id_to_root_id, parent_map,
2933
to_texts.insert_record_stream(root_stream)
2934
to_texts.insert_record_stream(from_texts.get_record_stream(
2935
text_keys, self.target._format._fetch_order,
2936
not self.target._format._fetch_uses_deltas))
2937
# insert inventory deltas
2938
for delta in pending_deltas:
2939
self.target.add_inventory_by_delta(*delta)
2940
if self.target._fallback_repositories:
2941
# Make sure this stacked repository has all the parent inventories
2942
# for the new revisions that we are about to insert. We do this
2943
# before adding the revisions so that no revision is added until
2944
# all the inventories it may depend on are added.
2945
# Note that this is overzealous, as we may have fetched these in an
2948
revision_ids = set()
2949
for revision in pending_revisions:
2950
revision_ids.add(revision.revision_id)
2951
parent_ids.update(revision.parent_ids)
2952
parent_ids.difference_update(revision_ids)
2953
parent_ids.discard(_mod_revision.NULL_REVISION)
2954
parent_map = self.source.get_parent_map(parent_ids)
2955
# we iterate over parent_map and not parent_ids because we don't
2956
# want to try copying any revision which is a ghost
2957
for parent_tree in self.source.revision_trees(parent_map):
2958
current_revision_id = parent_tree.get_revision_id()
2959
parents_parents = parent_map[current_revision_id]
2960
possible_trees = self._get_trees(parents_parents, cache)
2961
if len(possible_trees) == 0:
2962
# There either aren't any parents, or the parents are
2963
# ghosts, so just use the last converted tree.
2964
possible_trees.append((basis_id, cache[basis_id]))
2965
basis_id, delta = self._get_delta_for_revision(parent_tree,
2966
parents_parents, possible_trees)
2967
self.target.add_inventory_by_delta(
2968
basis_id, delta, current_revision_id, parents_parents)
2969
# insert signatures and revisions
2970
for revision in pending_revisions:
2972
signature = self.source.get_signature_text(
2973
revision.revision_id)
2974
self.target.add_signature_text(revision.revision_id,
2976
except errors.NoSuchRevision:
2978
self.target.add_revision(revision.revision_id, revision)
2981
def _fetch_all_revisions(self, revision_ids, pb):
2982
"""Fetch everything for the list of revisions.
2984
:param revision_ids: The list of revisions to fetch. Must be in
2986
:param pb: A ProgressTask
2989
basis_id, basis_tree = self._get_basis(revision_ids[0])
2991
cache = lru_cache.LRUCache(100)
2992
cache[basis_id] = basis_tree
2993
del basis_tree # We don't want to hang on to it here
2997
for offset in range(0, len(revision_ids), batch_size):
2998
self.target.start_write_group()
3000
pb.update(gettext('Transferring revisions'), offset,
3002
batch = revision_ids[offset:offset+batch_size]
3003
basis_id = self._fetch_batch(batch, basis_id, cache)
3005
self.source._safe_to_return_from_cache = False
3006
self.target.abort_write_group()
3009
hint = self.target.commit_write_group()
3012
if hints and self.target._format.pack_compresses:
3013
self.target.pack(hint=hints)
3014
pb.update(gettext('Transferring revisions'), len(revision_ids),
3018
def fetch(self, revision_id=None, find_ghosts=False,
3020
"""See InterRepository.fetch()."""
3021
if fetch_spec is not None:
3022
revision_ids = fetch_spec.get_keys()
3025
if self.source._format.experimental:
3026
ui.ui_factory.show_user_warning('experimental_format_fetch',
3027
from_format=self.source._format,
3028
to_format=self.target._format)
3029
if (not self.source.supports_rich_root()
3030
and self.target.supports_rich_root()):
3031
self._converting_to_rich_root = True
3032
self._revision_id_to_root_id = {}
3034
self._converting_to_rich_root = False
3035
# See <https://launchpad.net/bugs/456077> asking for a warning here
3036
if self.source._format.network_name() != self.target._format.network_name():
3037
ui.ui_factory.show_user_warning('cross_format_fetch',
3038
from_format=self.source._format,
3039
to_format=self.target._format)
3040
if revision_ids is None:
3042
search_revision_ids = [revision_id]
3044
search_revision_ids = None
3045
revision_ids = self.target.search_missing_revision_ids(self.source,
3046
revision_ids=search_revision_ids,
3047
find_ghosts=find_ghosts).get_keys()
3048
if not revision_ids:
3050
revision_ids = tsort.topo_sort(
3051
self.source.get_graph().get_parent_map(revision_ids))
3052
if not revision_ids:
3054
# Walk though all revisions; get inventory deltas, copy referenced
3055
# texts that delta references, insert the delta, revision and
3057
pb = ui.ui_factory.nested_progress_bar()
3059
self._fetch_all_revisions(revision_ids, pb)
3062
return len(revision_ids), 0
3064
def _get_basis(self, first_revision_id):
3065
"""Get a revision and tree which exists in the target.
3067
This assumes that first_revision_id is selected for transmission
3068
because all other ancestors are already present. If we can't find an
3069
ancestor we fall back to NULL_REVISION since we know that is safe.
3071
:return: (basis_id, basis_tree)
3073
first_rev = self.source.get_revision(first_revision_id)
3075
basis_id = first_rev.parent_ids[0]
3076
# only valid as a basis if the target has it
3077
self.target.get_revision(basis_id)
3078
# Try to get a basis tree - if it's a ghost it will hit the
3079
# NoSuchRevision case.
3080
basis_tree = self.source.revision_tree(basis_id)
3081
except (IndexError, errors.NoSuchRevision):
3082
basis_id = _mod_revision.NULL_REVISION
3083
basis_tree = self.source.revision_tree(basis_id)
3084
return basis_id, basis_tree
3087
class InterSameDataRepository(InterVersionedFileRepository):
3088
"""Code for converting between repositories that represent the same data.
3090
Data format and model must match for this to work.
3094
def _get_repo_format_to_test(self):
3095
"""Repository format for testing with.
3097
InterSameData can pull from subtree to subtree and from non-subtree to
3098
non-subtree, so we test this with the richest repository format.
3100
from bzrlib.repofmt import knitrepo
3101
return knitrepo.RepositoryFormatKnit3()
3104
def is_compatible(source, target):
3106
InterRepository._same_model(source, target) and
3107
source._format.supports_full_versioned_files and
3108
target._format.supports_full_versioned_files)
3111
InterRepository.register_optimiser(InterVersionedFileRepository)
3112
InterRepository.register_optimiser(InterDifferingSerializer)
3113
InterRepository.register_optimiser(InterSameDataRepository)
3116
def install_revisions(repository, iterable, num_revisions=None, pb=None):
3117
"""Install all revision data into a repository.
3119
Accepts an iterable of revision, tree, signature tuples. The signature
3122
repository.start_write_group()
3124
inventory_cache = lru_cache.LRUCache(10)
3125
for n, (revision, revision_tree, signature) in enumerate(iterable):
3126
_install_revision(repository, revision, revision_tree, signature,
3129
pb.update(gettext('Transferring revisions'), n + 1, num_revisions)
3131
repository.abort_write_group()
3134
repository.commit_write_group()
3137
def _install_revision(repository, rev, revision_tree, signature,
3139
"""Install all revision data into a repository."""
3140
present_parents = []
3142
for p_id in rev.parent_ids:
3143
if repository.has_revision(p_id):
3144
present_parents.append(p_id)
3145
parent_trees[p_id] = repository.revision_tree(p_id)
3147
parent_trees[p_id] = repository.revision_tree(
3148
_mod_revision.NULL_REVISION)
3150
inv = revision_tree.inventory
3151
entries = inv.iter_entries()
3152
# backwards compatibility hack: skip the root id.
3153
if not repository.supports_rich_root():
3154
path, root = entries.next()
3155
if root.revision != rev.revision_id:
3156
raise errors.IncompatibleRevision(repr(repository))
3158
for path, ie in entries:
3159
text_keys[(ie.file_id, ie.revision)] = ie
3160
text_parent_map = repository.texts.get_parent_map(text_keys)
3161
missing_texts = set(text_keys) - set(text_parent_map)
3162
# Add the texts that are not already present
3163
for text_key in missing_texts:
3164
ie = text_keys[text_key]
3166
# FIXME: TODO: The following loop overlaps/duplicates that done by
3167
# commit to determine parents. There is a latent/real bug here where
3168
# the parents inserted are not those commit would do - in particular
3169
# they are not filtered by heads(). RBC, AB
3170
for revision, tree in parent_trees.iteritems():
3171
if not tree.has_id(ie.file_id):
3173
parent_id = tree.get_file_revision(ie.file_id)
3174
if parent_id in text_parents:
3176
text_parents.append((ie.file_id, parent_id))
3177
lines = revision_tree.get_file(ie.file_id).readlines()
3178
repository.texts.add_lines(text_key, text_parents, lines)
3180
# install the inventory
3181
if repository._format._commit_inv_deltas and len(rev.parent_ids):
3182
# Cache this inventory
3183
inventory_cache[rev.revision_id] = inv
3185
basis_inv = inventory_cache[rev.parent_ids[0]]
3187
repository.add_inventory(rev.revision_id, inv, present_parents)
3189
delta = inv._make_delta(basis_inv)
3190
repository.add_inventory_by_delta(rev.parent_ids[0], delta,
3191
rev.revision_id, present_parents)
3193
repository.add_inventory(rev.revision_id, inv, present_parents)
3194
except errors.RevisionAlreadyPresent:
3196
if signature is not None:
3197
repository.add_signature_text(rev.revision_id, signature)
3198
repository.add_revision(rev.revision_id, rev, inv)
3201
def install_revision(repository, rev, revision_tree):
3202
"""Install all revision data into a repository."""
3203
install_revisions(repository, [(rev, revision_tree, None)])