1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from binascii import hexlify
18
from copy import deepcopy
19
17
from cStringIO import StringIO
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
22
from unittest import TestSuite
24
import bzrlib.bzrdir as bzrdir
39
revision as _mod_revision,
45
from bzrlib.bundle import serializer
46
from bzrlib.revisiontree import RevisionTree
47
from bzrlib.store.versioned import VersionedFileStore
48
from bzrlib.store.text import TextStore
49
from bzrlib.testament import Testament
50
from bzrlib.util import bencode
25
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
26
import bzrlib.errors as errors
27
from bzrlib.errors import InvalidRevisionId
28
import bzrlib.gpg as gpg
29
from bzrlib.graph import Graph
30
54
from bzrlib.inter import InterObject
31
from bzrlib.inventory import Inventory
32
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
33
from bzrlib.lockable_files import LockableFiles, TransportLock
34
from bzrlib.lockdir import LockDir
35
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date,
37
from bzrlib.revision import NULL_REVISION, Revision
38
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
39
from bzrlib.store.text import TextStore
40
from bzrlib.symbol_versioning import *
41
from bzrlib.trace import mutter, note
42
from bzrlib.tree import RevisionTree
43
from bzrlib.tsort import topo_sort
44
from bzrlib.testament import Testament
45
from bzrlib.tree import EmptyTree
47
from bzrlib.weave import WeaveFile
55
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
56
from bzrlib.symbol_versioning import (
63
from bzrlib.trace import mutter, mutter_callsite, note, warning
66
# Old formats display a warning, but only once
67
_deprecation_warning_done = False
70
class CommitBuilder(object):
71
"""Provides an interface to build up a commit.
73
This allows describing a tree to be committed without needing to
74
know the internals of the format of the repository.
77
# all clients should supply tree roots.
78
record_root_entry = True
79
# the default CommitBuilder does not manage trees whose root is versioned.
80
_versioned_root = False
82
def __init__(self, repository, parents, config, timestamp=None,
83
timezone=None, committer=None, revprops=None,
85
"""Initiate a CommitBuilder.
87
:param repository: Repository to commit to.
88
:param parents: Revision ids of the parents of the new revision.
89
:param config: Configuration to use.
90
:param timestamp: Optional timestamp recorded for commit.
91
:param timezone: Optional timezone for timestamp.
92
:param committer: Optional committer to set for commit.
93
:param revprops: Optional dictionary of revision properties.
94
:param revision_id: Optional revision id.
99
self._committer = self._config.username()
101
self._committer = committer
103
self.new_inventory = Inventory(None)
104
self._new_revision_id = revision_id
105
self.parents = parents
106
self.repository = repository
109
if revprops is not None:
110
self._revprops.update(revprops)
112
if timestamp is None:
113
timestamp = time.time()
114
# Restrict resolution to 1ms
115
self._timestamp = round(timestamp, 3)
118
self._timezone = osutils.local_time_offset()
120
self._timezone = int(timezone)
122
self._generate_revision_if_needed()
123
self.__heads = graph.HeadsCache(repository.get_graph()).heads
125
def commit(self, message):
126
"""Make the actual commit.
128
:return: The revision id of the recorded revision.
130
rev = _mod_revision.Revision(
131
timestamp=self._timestamp,
132
timezone=self._timezone,
133
committer=self._committer,
135
inventory_sha1=self.inv_sha1,
136
revision_id=self._new_revision_id,
137
properties=self._revprops)
138
rev.parent_ids = self.parents
139
self.repository.add_revision(self._new_revision_id, rev,
140
self.new_inventory, self._config)
141
self.repository.commit_write_group()
142
return self._new_revision_id
145
"""Abort the commit that is being built.
147
self.repository.abort_write_group()
149
def revision_tree(self):
150
"""Return the tree that was just committed.
152
After calling commit() this can be called to get a RevisionTree
153
representing the newly committed tree. This is preferred to
154
calling Repository.revision_tree() because that may require
155
deserializing the inventory, while we already have a copy in
158
return RevisionTree(self.repository, self.new_inventory,
159
self._new_revision_id)
161
def finish_inventory(self):
162
"""Tell the builder that the inventory is finished."""
163
if self.new_inventory.root is None:
164
raise AssertionError('Root entry should be supplied to'
165
' record_entry_contents, as of bzr 0.10.',
166
DeprecationWarning, stacklevel=2)
167
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
168
self.new_inventory.revision_id = self._new_revision_id
169
self.inv_sha1 = self.repository.add_inventory(
170
self._new_revision_id,
175
def _gen_revision_id(self):
176
"""Return new revision-id."""
177
return generate_ids.gen_revision_id(self._config.username(),
180
def _generate_revision_if_needed(self):
181
"""Create a revision id if None was supplied.
183
If the repository can not support user-specified revision ids
184
they should override this function and raise CannotSetRevisionId
185
if _new_revision_id is not None.
187
:raises: CannotSetRevisionId
189
if self._new_revision_id is None:
190
self._new_revision_id = self._gen_revision_id()
191
self.random_revid = True
193
self.random_revid = False
195
def _heads(self, file_id, revision_ids):
196
"""Calculate the graph heads for revision_ids in the graph of file_id.
198
This can use either a per-file graph or a global revision graph as we
199
have an identity relationship between the two graphs.
201
return self.__heads(revision_ids)
203
def _check_root(self, ie, parent_invs, tree):
204
"""Helper for record_entry_contents.
206
:param ie: An entry being added.
207
:param parent_invs: The inventories of the parent revisions of the
209
:param tree: The tree that is being committed.
211
# In this revision format, root entries have no knit or weave When
212
# serializing out to disk and back in root.revision is always
214
ie.revision = self._new_revision_id
216
def _get_delta(self, ie, basis_inv, path):
217
"""Get a delta against the basis inventory for ie."""
218
if ie.file_id not in basis_inv:
220
return (None, path, ie.file_id, ie)
221
elif ie != basis_inv[ie.file_id]:
223
# TODO: avoid tis id2path call.
224
return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
229
def record_entry_contents(self, ie, parent_invs, path, tree,
231
"""Record the content of ie from tree into the commit if needed.
233
Side effect: sets ie.revision when unchanged
235
:param ie: An inventory entry present in the commit.
236
:param parent_invs: The inventories of the parent revisions of the
238
:param path: The path the entry is at in the tree.
239
:param tree: The tree which contains this entry and should be used to
241
:param content_summary: Summary data from the tree about the paths
242
content - stat, length, exec, sha/link target. This is only
243
accessed when the entry has a revision of None - that is when it is
244
a candidate to commit.
245
:return: A tuple (change_delta, version_recorded). change_delta is
246
an inventory_delta change for this entry against the basis tree of
247
the commit, or None if no change occured against the basis tree.
248
version_recorded is True if a new version of the entry has been
249
recorded. For instance, committing a merge where a file was only
250
changed on the other side will return (delta, False).
252
if self.new_inventory.root is None:
253
if ie.parent_id is not None:
254
raise errors.RootMissing()
255
self._check_root(ie, parent_invs, tree)
256
if ie.revision is None:
257
kind = content_summary[0]
259
# ie is carried over from a prior commit
261
# XXX: repository specific check for nested tree support goes here - if
262
# the repo doesn't want nested trees we skip it ?
263
if (kind == 'tree-reference' and
264
not self.repository._format.supports_tree_reference):
265
# mismatch between commit builder logic and repository:
266
# this needs the entry creation pushed down into the builder.
267
raise NotImplementedError('Missing repository subtree support.')
268
self.new_inventory.add(ie)
270
# TODO: slow, take it out of the inner loop.
272
basis_inv = parent_invs[0]
274
basis_inv = Inventory(root_id=None)
276
# ie.revision is always None if the InventoryEntry is considered
277
# for committing. We may record the previous parents revision if the
278
# content is actually unchanged against a sole head.
279
if ie.revision is not None:
280
if not self._versioned_root and path == '':
281
# repositories that do not version the root set the root's
282
# revision to the new commit even when no change occurs, and
283
# this masks when a change may have occurred against the basis,
284
# so calculate if one happened.
285
if ie.file_id in basis_inv:
286
delta = (basis_inv.id2path(ie.file_id), path,
290
delta = (None, path, ie.file_id, ie)
293
# we don't need to commit this, because the caller already
294
# determined that an existing revision of this file is
296
return None, (ie.revision == self._new_revision_id)
297
# XXX: Friction: parent_candidates should return a list not a dict
298
# so that we don't have to walk the inventories again.
299
parent_candiate_entries = ie.parent_candidates(parent_invs)
300
head_set = self._heads(ie.file_id, parent_candiate_entries.keys())
302
for inv in parent_invs:
303
if ie.file_id in inv:
304
old_rev = inv[ie.file_id].revision
305
if old_rev in head_set:
306
heads.append(inv[ie.file_id].revision)
307
head_set.remove(inv[ie.file_id].revision)
310
# now we check to see if we need to write a new record to the
312
# We write a new entry unless there is one head to the ancestors, and
313
# the kind-derived content is unchanged.
315
# Cheapest check first: no ancestors, or more the one head in the
316
# ancestors, we write a new node.
320
# There is a single head, look it up for comparison
321
parent_entry = parent_candiate_entries[heads[0]]
322
# if the non-content specific data has changed, we'll be writing a
324
if (parent_entry.parent_id != ie.parent_id or
325
parent_entry.name != ie.name):
327
# now we need to do content specific checks:
329
# if the kind changed the content obviously has
330
if kind != parent_entry.kind:
333
if content_summary[2] is None:
334
raise ValueError("Files must not have executable = None")
336
if (# if the file length changed we have to store:
337
parent_entry.text_size != content_summary[1] or
338
# if the exec bit has changed we have to store:
339
parent_entry.executable != content_summary[2]):
341
elif parent_entry.text_sha1 == content_summary[3]:
342
# all meta and content is unchanged (using a hash cache
343
# hit to check the sha)
344
ie.revision = parent_entry.revision
345
ie.text_size = parent_entry.text_size
346
ie.text_sha1 = parent_entry.text_sha1
347
ie.executable = parent_entry.executable
348
return self._get_delta(ie, basis_inv, path), False
350
# Either there is only a hash change(no hash cache entry,
351
# or same size content change), or there is no change on
353
# Provide the parent's hash to the store layer, so that the
354
# content is unchanged we will not store a new node.
355
nostore_sha = parent_entry.text_sha1
357
# We want to record a new node regardless of the presence or
358
# absence of a content change in the file.
360
ie.executable = content_summary[2]
361
lines = tree.get_file(ie.file_id, path).readlines()
363
ie.text_sha1, ie.text_size = self._add_text_to_weave(
364
ie.file_id, lines, heads, nostore_sha)
365
except errors.ExistingContent:
366
# Turns out that the file content was unchanged, and we were
367
# only going to store a new node if it was changed. Carry over
369
ie.revision = parent_entry.revision
370
ie.text_size = parent_entry.text_size
371
ie.text_sha1 = parent_entry.text_sha1
372
ie.executable = parent_entry.executable
373
return self._get_delta(ie, basis_inv, path), False
374
elif kind == 'directory':
376
# all data is meta here, nothing specific to directory, so
378
ie.revision = parent_entry.revision
379
return self._get_delta(ie, basis_inv, path), False
381
self._add_text_to_weave(ie.file_id, lines, heads, None)
382
elif kind == 'symlink':
383
current_link_target = content_summary[3]
385
# symlink target is not generic metadata, check if it has
387
if current_link_target != parent_entry.symlink_target:
390
# unchanged, carry over.
391
ie.revision = parent_entry.revision
392
ie.symlink_target = parent_entry.symlink_target
393
return self._get_delta(ie, basis_inv, path), False
394
ie.symlink_target = current_link_target
396
self._add_text_to_weave(ie.file_id, lines, heads, None)
397
elif kind == 'tree-reference':
399
if content_summary[3] != parent_entry.reference_revision:
402
# unchanged, carry over.
403
ie.reference_revision = parent_entry.reference_revision
404
ie.revision = parent_entry.revision
405
return self._get_delta(ie, basis_inv, path), False
406
ie.reference_revision = content_summary[3]
408
self._add_text_to_weave(ie.file_id, lines, heads, None)
410
raise NotImplementedError('unknown kind')
411
ie.revision = self._new_revision_id
412
return self._get_delta(ie, basis_inv, path), True
414
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
415
# Note: as we read the content directly from the tree, we know its not
416
# been turned into unicode or badly split - but a broken tree
417
# implementation could give us bad output from readlines() so this is
418
# not a guarantee of safety. What would be better is always checking
419
# the content during test suite execution. RBC 20070912
420
parent_keys = tuple((file_id, parent) for parent in parents)
421
return self.repository.texts.add_lines(
422
(file_id, self._new_revision_id), parent_keys, new_lines,
423
nostore_sha=nostore_sha, random_id=self.random_revid,
424
check_content=False)[0:2]
427
class RootCommitBuilder(CommitBuilder):
428
"""This commitbuilder actually records the root id"""
430
# the root entry gets versioned properly by this builder.
431
_versioned_root = True
433
def _check_root(self, ie, parent_invs, tree):
434
"""Helper for record_entry_contents.
436
:param ie: An entry being added.
437
:param parent_invs: The inventories of the parent revisions of the
439
:param tree: The tree that is being committed.
443
######################################################################
51
446
class Repository(object):
52
447
"""Repository holding history for one or more branches.
55
450
revisions and file history. It's normally accessed only by the Branch,
56
451
which views a particular line of development through that history.
58
The Repository builds on top of Stores and a Transport, which respectively
59
describe the disk data format and the way of accessing the (possibly
453
The Repository builds on top of some byte storage facilies (the revisions,
454
signatures, inventories and texts attributes) and a Transport, which
455
respectively provide byte storage and a means to access the (possibly
458
The byte storage facilities are addressed via tuples, which we refer to
459
as 'keys' throughout the code base. Revision_keys, inventory_keys and
460
signature_keys are all 1-tuples: (revision_id,). text_keys are two-tuples:
461
(file_id, revision_id). We use this interface because it allows low
462
friction with the underlying code that implements disk indices, network
463
encoding and other parts of bzrlib.
465
:ivar revisions: A bzrlib.versionedfile.VersionedFiles instance containing
466
the serialised revisions for the repository. This can be used to obtain
467
revision graph information or to access raw serialised revisions.
468
The result of trying to insert data into the repository via this store
469
is undefined: it should be considered read-only except for implementors
471
:ivar signatures: A bzrlib.versionedfile.VersionedFiles instance containing
472
the serialised signatures for the repository. This can be used to
473
obtain access to raw serialised signatures. The result of trying to
474
insert data into the repository via this store is undefined: it should
475
be considered read-only except for implementors of repositories.
476
:ivar inventories: A bzrlib.versionedfile.VersionedFiles instance containing
477
the serialised inventories for the repository. This can be used to
478
obtain unserialised inventories. The result of trying to insert data
479
into the repository via this store is undefined: it should be
480
considered read-only except for implementors of repositories.
481
:ivar texts: A bzrlib.versionedfile.VersionedFiles instance containing the
482
texts of files and directories for the repository. This can be used to
483
obtain file texts or file graphs. Note that Repository.iter_file_bytes
484
is usually a better interface for accessing file texts.
485
The result of trying to insert data into the repository via this store
486
is undefined: it should be considered read-only except for implementors
488
:ivar _transport: Transport for file access to repository, typically
489
pointing to .bzr/repository.
64
def add_inventory(self, revid, inv, parents):
65
"""Add the inventory inv to the repository as revid.
67
:param parents: The revision ids of the parents that revid
492
# What class to use for a CommitBuilder. Often its simpler to change this
493
# in a Repository class subclass rather than to override
494
# get_commit_builder.
495
_commit_builder_class = CommitBuilder
496
# The search regex used by xml based repositories to determine what things
497
# where changed in a single commit.
498
_file_ids_altered_regex = lazy_regex.lazy_compile(
499
r'file_id="(?P<file_id>[^"]+)"'
500
r'.* revision="(?P<revision_id>[^"]+)"'
503
def abort_write_group(self):
504
"""Commit the contents accrued within the current write group.
506
:seealso: start_write_group.
508
if self._write_group is not self.get_transaction():
509
# has an unlock or relock occured ?
510
raise errors.BzrError('mismatched lock context and write group.')
511
self._abort_write_group()
512
self._write_group = None
514
def _abort_write_group(self):
515
"""Template method for per-repository write group cleanup.
517
This is called during abort before the write group is considered to be
518
finished and should cleanup any internal state accrued during the write
519
group. There is no requirement that data handed to the repository be
520
*not* made available - this is not a rollback - but neither should any
521
attempt be made to ensure that data added is fully commited. Abort is
522
invoked when an error has occured so futher disk or network operations
523
may not be possible or may error and if possible should not be
527
def add_fallback_repository(self, repository):
528
"""Add a repository to use for looking up data not held locally.
530
:param repository: A repository.
532
if not self._format.supports_external_lookups:
533
raise errors.UnstackableRepositoryFormat(self._format, self.base)
534
if not self._add_fallback_repository_check(repository):
535
raise errors.IncompatibleRepositories(self, repository)
536
self._fallback_repositories.append(repository)
537
self.texts.add_fallback_versioned_files(repository.texts)
538
self.inventories.add_fallback_versioned_files(repository.inventories)
539
self.revisions.add_fallback_versioned_files(repository.revisions)
540
self.signatures.add_fallback_versioned_files(repository.signatures)
542
def _add_fallback_repository_check(self, repository):
543
"""Check that this repository can fallback to repository safely.
545
:param repository: A repository to fallback to.
546
:return: True if the repositories can stack ok.
548
return InterRepository._same_model(self, repository)
550
def add_inventory(self, revision_id, inv, parents):
551
"""Add the inventory inv to the repository as revision_id.
553
:param parents: The revision ids of the parents that revision_id
68
554
is known to have and are in the repository already.
70
returns the sha1 of the serialized inventory.
556
:returns: The validator(which is a sha1 digest, though what is sha'd is
557
repository format specific) of the serialized inventory.
72
assert inv.revision_id is None or inv.revision_id == revid, \
73
"Mismatch between inventory revision" \
74
" id and insertion revid (%r, %r)" % (inv.revision_id, revid)
75
inv_text = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
76
inv_sha1 = bzrlib.osutils.sha_string(inv_text)
77
inv_vf = self.control_weaves.get_weave('inventory',
78
self.get_transaction())
79
self._inventory_add_lines(inv_vf, revid, parents, bzrlib.osutils.split_lines(inv_text))
82
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
84
for parent in parents:
86
final_parents.append(parent)
88
inv_vf.add_lines(revid, final_parents, lines)
91
def add_revision(self, rev_id, rev, inv=None, config=None):
92
"""Add rev to the revision store as rev_id.
94
:param rev_id: the revision id to use.
559
if not self.is_in_write_group():
560
raise AssertionError("%r not in write group" % (self,))
561
_mod_revision.check_not_reserved_id(revision_id)
562
if not (inv.revision_id is None or inv.revision_id == revision_id):
563
raise AssertionError(
564
"Mismatch between inventory revision"
565
" id and insertion revid (%r, %r)"
566
% (inv.revision_id, revision_id))
568
raise AssertionError()
569
inv_lines = self._serialise_inventory_to_lines(inv)
570
return self._inventory_add_lines(revision_id, parents,
571
inv_lines, check_content=False)
573
def _inventory_add_lines(self, revision_id, parents, lines,
575
"""Store lines in inv_vf and return the sha1 of the inventory."""
576
parents = [(parent,) for parent in parents]
577
return self.inventories.add_lines((revision_id,), parents, lines,
578
check_content=check_content)[0]
580
def add_revision(self, revision_id, rev, inv=None, config=None):
581
"""Add rev to the revision store as revision_id.
583
:param revision_id: the revision id to use.
95
584
:param rev: The revision object.
96
585
:param inv: The inventory for the revision. if None, it will be looked
97
586
up in the inventory storer
182
677
# the following are part of the public API for Repository:
183
678
self.bzrdir = a_bzrdir
184
679
self.control_files = control_files
185
self._revision_store = _revision_store
186
self.text_store = text_store
187
# backwards compatibility
188
self.weave_store = text_store
680
self._transport = control_files._transport
681
self.base = self._transport.base
683
self._reconcile_does_inventory_gc = True
684
self._reconcile_fixes_text_parents = False
685
self._reconcile_backsup_inventory = True
189
686
# not right yet - should be more semantically clear ?
191
self.control_store = control_store
192
self.control_weaves = control_store
193
688
# TODO: make sure to construct the right store classes, etc, depending
194
689
# on whether escaping is required.
690
self._warn_if_deprecated()
691
self._write_group = None
692
# Additional places to query for data.
693
self._fallback_repositories = []
196
695
def __repr__(self):
197
return '%s(%r)' % (self.__class__.__name__,
198
self.bzrdir.transport.base)
696
return '%s(%r)' % (self.__class__.__name__,
699
def has_same_location(self, other):
700
"""Returns a boolean indicating if this repository is at the same
701
location as another repository.
703
This might return False even when two repository objects are accessing
704
the same physical repository via different URLs.
706
if self.__class__ is not other.__class__:
708
return (self._transport.base == other._transport.base)
710
def is_in_write_group(self):
711
"""Return True if there is an open write group.
713
:seealso: start_write_group.
715
return self._write_group is not None
200
717
def is_locked(self):
201
718
return self.control_files.is_locked()
203
def lock_write(self):
204
self.control_files.lock_write()
720
def is_write_locked(self):
721
"""Return True if this object is write locked."""
722
return self.is_locked() and self.control_files._lock_mode == 'w'
724
def lock_write(self, token=None):
725
"""Lock this repository for writing.
727
This causes caching within the repository obejct to start accumlating
728
data during reads, and allows a 'write_group' to be obtained. Write
729
groups must be used for actual data insertion.
731
:param token: if this is already locked, then lock_write will fail
732
unless the token matches the existing lock.
733
:returns: a token if this instance supports tokens, otherwise None.
734
:raises TokenLockingNotSupported: when a token is given but this
735
instance doesn't support using token locks.
736
:raises MismatchedToken: if the specified token doesn't match the token
737
of the existing lock.
738
:seealso: start_write_group.
740
A token should be passed in if you know that you have locked the object
741
some other way, and need to synchronise this object's state with that
744
XXX: this docstring is duplicated in many places, e.g. lockable_files.py
746
result = self.control_files.lock_write(token=token)
747
for repo in self._fallback_repositories:
748
# Writes don't affect fallback repos
206
753
def lock_read(self):
207
754
self.control_files.lock_read()
755
for repo in self._fallback_repositories:
209
759
def get_physical_lock_status(self):
210
760
return self.control_files.get_physical_lock_status()
213
def missing_revision_ids(self, other, revision_id=None):
214
"""Return the revision ids that other has that this does not.
216
These are returned in topological order.
218
revision_id: only return revision ids included by revision_id.
220
return InterRepository.get(other, self).missing_revision_ids(revision_id)
762
def leave_lock_in_place(self):
763
"""Tell this repository not to release the physical lock when this
766
If lock_write doesn't return a token, then this method is not supported.
768
self.control_files.leave_in_place()
770
def dont_leave_lock_in_place(self):
771
"""Tell this repository to release the physical lock when this
772
object is unlocked, even if it didn't originally acquire it.
774
If lock_write doesn't return a token, then this method is not supported.
776
self.control_files.dont_leave_in_place()
779
def gather_stats(self, revid=None, committers=None):
780
"""Gather statistics from a revision id.
782
:param revid: The revision id to gather statistics from, if None, then
783
no revision specific statistics are gathered.
784
:param committers: Optional parameter controlling whether to grab
785
a count of committers from the revision specific statistics.
786
:return: A dictionary of statistics. Currently this contains:
787
committers: The number of committers if requested.
788
firstrev: A tuple with timestamp, timezone for the penultimate left
789
most ancestor of revid, if revid is not the NULL_REVISION.
790
latestrev: A tuple with timestamp, timezone for revid, if revid is
791
not the NULL_REVISION.
792
revisions: The total revision count in the repository.
793
size: An estimate disk size of the repository in bytes.
796
if revid and committers:
797
result['committers'] = 0
798
if revid and revid != _mod_revision.NULL_REVISION:
800
all_committers = set()
801
revisions = self.get_ancestry(revid)
802
# pop the leading None
804
first_revision = None
806
# ignore the revisions in the middle - just grab first and last
807
revisions = revisions[0], revisions[-1]
808
for revision in self.get_revisions(revisions):
809
if not first_revision:
810
first_revision = revision
812
all_committers.add(revision.committer)
813
last_revision = revision
815
result['committers'] = len(all_committers)
816
result['firstrev'] = (first_revision.timestamp,
817
first_revision.timezone)
818
result['latestrev'] = (last_revision.timestamp,
819
last_revision.timezone)
821
# now gather global repository information
822
# XXX: This is available for many repos regardless of listability.
823
if self.bzrdir.root_transport.listable():
824
# XXX: do we want to __define len__() ?
825
# Maybe the versionedfiles object should provide a different
826
# method to get the number of keys.
827
result['revisions'] = len(self.revisions.keys())
831
def find_branches(self, using=False):
832
"""Find branches underneath this repository.
834
This will include branches inside other branches.
836
:param using: If True, list only branches using this repository.
838
if using and not self.is_shared():
840
return [self.bzrdir.open_branch()]
841
except errors.NotBranchError:
843
class Evaluator(object):
846
self.first_call = True
848
def __call__(self, bzrdir):
849
# On the first call, the parameter is always the bzrdir
850
# containing the current repo.
851
if not self.first_call:
853
repository = bzrdir.open_repository()
854
except errors.NoRepositoryPresent:
857
return False, (None, repository)
858
self.first_call = False
860
value = (bzrdir.open_branch(), None)
861
except errors.NotBranchError:
866
for branch, repository in bzrdir.BzrDir.find_bzrdirs(
867
self.bzrdir.root_transport, evaluate=Evaluator()):
868
if branch is not None:
869
branches.append(branch)
870
if not using and repository is not None:
871
branches.extend(repository.find_branches())
875
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
876
"""Return the revision ids that other has that this does not.
878
These are returned in topological order.
880
revision_id: only return revision ids included by revision_id.
882
return InterRepository.get(other, self).search_missing_revision_ids(
883
revision_id, find_ghosts)
885
@deprecated_method(one_two)
887
def missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
888
"""Return the revision ids that other has that this does not.
890
These are returned in topological order.
892
revision_id: only return revision ids included by revision_id.
894
keys = self.search_missing_revision_ids(
895
other, revision_id, find_ghosts).get_keys()
898
parents = other.get_graph().get_parent_map(keys)
901
return tsort.topo_sort(parents)
226
907
For instance, if the repository is at URL/.bzr/repository,
227
908
Repository.open(URL) -> a Repository instance.
229
control = bzrlib.bzrdir.BzrDir.open(base)
910
control = bzrdir.BzrDir.open(base)
230
911
return control.open_repository()
232
def copy_content_into(self, destination, revision_id=None, basis=None):
913
def copy_content_into(self, destination, revision_id=None):
233
914
"""Make a complete copy of the content in self into destination.
235
916
This is a destructive operation! Do not use it on existing
238
return InterRepository.get(self, destination).copy_content(revision_id, basis)
240
def fetch(self, source, revision_id=None, pb=None):
919
return InterRepository.get(self, destination).copy_content(revision_id)
921
def commit_write_group(self):
922
"""Commit the contents accrued within the current write group.
924
:seealso: start_write_group.
926
if self._write_group is not self.get_transaction():
927
# has an unlock or relock occured ?
928
raise errors.BzrError('mismatched lock context %r and '
930
(self.get_transaction(), self._write_group))
931
self._commit_write_group()
932
self._write_group = None
934
def _commit_write_group(self):
935
"""Template method for per-repository write group cleanup.
937
This is called before the write group is considered to be
938
finished and should ensure that all data handed to the repository
939
for writing during the write group is safely committed (to the
940
extent possible considering file system caching etc).
943
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
241
944
"""Fetch the content required to construct revision_id from source.
243
946
If revision_id is None all content is copied.
947
:param find_ghosts: Find and copy revisions in the source that are
948
ghosts in the target (and not reachable directly by walking out to
949
the first-present revision in target from revision_id).
245
return InterRepository.get(source, self).fetch(revision_id=revision_id,
248
def get_commit_builder(self, branch, parents, config, timestamp=None,
249
timezone=None, committer=None, revprops=None,
951
# fast path same-url fetch operations
952
if self.has_same_location(source):
953
# check that last_revision is in 'from' and then return a
955
if (revision_id is not None and
956
not _mod_revision.is_null(revision_id)):
957
self.get_revision(revision_id)
959
inter = InterRepository.get(source, self)
961
return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
962
except NotImplementedError:
963
raise errors.IncompatibleRepositories(source, self)
965
def create_bundle(self, target, base, fileobj, format=None):
966
return serializer.write_bundle(self, target, base, fileobj, format)
968
def get_commit_builder(self, branch, parents, config, timestamp=None,
969
timezone=None, committer=None, revprops=None,
250
970
revision_id=None):
251
971
"""Obtain a CommitBuilder for this repository.
259
979
:param revprops: Optional dictionary of revision properties.
260
980
:param revision_id: Optional revision id.
262
return CommitBuilder(self, parents, config, timestamp, timezone,
263
committer, revprops, revision_id)
982
result = self._commit_builder_class(self, parents, config,
983
timestamp, timezone, committer, revprops, revision_id)
984
self.start_write_group()
265
987
def unlock(self):
988
if (self.control_files._lock_count == 1 and
989
self.control_files._lock_mode == 'w'):
990
if self._write_group is not None:
991
self.abort_write_group()
992
self.control_files.unlock()
993
raise errors.BzrError(
994
'Must end write groups before releasing write locks.')
266
995
self.control_files.unlock()
996
for repo in self._fallback_repositories:
269
def clone(self, a_bzrdir, revision_id=None, basis=None):
1000
def clone(self, a_bzrdir, revision_id=None):
270
1001
"""Clone this repository into a_bzrdir using the current format.
272
1003
Currently no check is made that the format of this repository and
273
1004
the bzrdir format are compatible. FIXME RBC 20060201.
1006
:return: The newly created destination repository.
1008
# TODO: deprecate after 0.16; cloning this with all its settings is
1009
# probably not very useful -- mbp 20070423
1010
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
1011
self.copy_content_into(dest_repo, revision_id)
1014
def start_write_group(self):
1015
"""Start a write group in the repository.
1017
Write groups are used by repositories which do not have a 1:1 mapping
1018
between file ids and backend store to manage the insertion of data from
1019
both fetch and commit operations.
1021
A write lock is required around the start_write_group/commit_write_group
1022
for the support of lock-requiring repository formats.
1024
One can only insert data into a repository inside a write group.
1028
if not self.is_write_locked():
1029
raise errors.NotWriteLocked(self)
1030
if self._write_group:
1031
raise errors.BzrError('already in a write group')
1032
self._start_write_group()
1033
# so we can detect unlock/relock - the write group is now entered.
1034
self._write_group = self.get_transaction()
1036
def _start_write_group(self):
1037
"""Template method for per-repository write group startup.
1039
This is called before the write group is considered to be
1044
def sprout(self, to_bzrdir, revision_id=None):
1045
"""Create a descendent repository for new development.
1047
Unlike clone, this does not copy the settings of the repository.
1049
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
1050
dest_repo.fetch(self, revision_id=revision_id)
1053
def _create_sprouting_repo(self, a_bzrdir, shared):
275
1054
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
276
1055
# use target default format.
277
result = a_bzrdir.create_repository()
278
# FIXME RBC 20060209 split out the repository type to avoid this check ?
279
elif isinstance(a_bzrdir._format,
280
(bzrlib.bzrdir.BzrDirFormat4,
281
bzrlib.bzrdir.BzrDirFormat5,
282
bzrlib.bzrdir.BzrDirFormat6)):
283
result = a_bzrdir.open_repository()
1056
dest_repo = a_bzrdir.create_repository()
285
result = self._format.initialize(a_bzrdir, shared=self.is_shared())
286
self.copy_content_into(result, revision_id, basis)
1058
# Most control formats need the repository to be specifically
1059
# created, but on some old all-in-one formats it's not needed
1061
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
1062
except errors.UninitializableFormat:
1063
dest_repo = a_bzrdir.open_repository()
289
1066
@needs_read_lock
290
1067
def has_revision(self, revision_id):
291
1068
"""True if this repository has a copy of the revision."""
292
return self._revision_store.has_revision_id(revision_id,
293
self.get_transaction())
1069
return revision_id in self.has_revisions((revision_id,))
1072
def has_revisions(self, revision_ids):
1073
"""Probe to find out the presence of multiple revisions.
1075
:param revision_ids: An iterable of revision_ids.
1076
:return: A set of the revision_ids that were present.
1078
parent_map = self.revisions.get_parent_map(
1079
[(rev_id,) for rev_id in revision_ids])
1081
if _mod_revision.NULL_REVISION in revision_ids:
1082
result.add(_mod_revision.NULL_REVISION)
1083
result.update([key[0] for key in parent_map])
1087
def get_revision(self, revision_id):
1088
"""Return the Revision object for a named revision."""
1089
return self.get_revisions([revision_id])[0]
295
1091
@needs_read_lock
296
1092
def get_revision_reconcile(self, revision_id):
301
1097
be used by reconcile, or reconcile-alike commands that are correcting
302
1098
or testing the revision graph.
304
if not revision_id or not isinstance(revision_id, basestring):
305
raise InvalidRevisionId(revision_id=revision_id, branch=self)
306
return self._revision_store.get_revision(revision_id,
307
self.get_transaction())
1100
return self._get_revisions([revision_id])[0]
1103
def get_revisions(self, revision_ids):
1104
"""Get many revisions at once."""
1105
return self._get_revisions(revision_ids)
1108
def _get_revisions(self, revision_ids):
1109
"""Core work logic to get many revisions without sanity checks."""
1110
for rev_id in revision_ids:
1111
if not rev_id or not isinstance(rev_id, basestring):
1112
raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
1113
keys = [(key,) for key in revision_ids]
1114
stream = self.revisions.get_record_stream(keys, 'unordered', True)
1116
for record in stream:
1117
if record.storage_kind == 'absent':
1118
raise errors.NoSuchRevision(self, record.key[0])
1119
text = record.get_bytes_as('fulltext')
1120
rev = self._serializer.read_revision_from_string(text)
1121
revs[record.key[0]] = rev
1122
return [revs[revid] for revid in revision_ids]
309
1124
@needs_read_lock
310
1125
def get_revision_xml(self, revision_id):
311
rev = self.get_revision(revision_id)
1126
# TODO: jam 20070210 This shouldn't be necessary since get_revision
1127
# would have already do it.
1128
# TODO: jam 20070210 Just use _serializer.write_revision_to_string()
1129
rev = self.get_revision(revision_id)
312
1130
rev_tmp = StringIO()
313
1131
# the current serializer..
314
self._revision_store._serializer.write_revision(rev, rev_tmp)
1132
self._serializer.write_revision(rev, rev_tmp)
316
1134
return rev_tmp.getvalue()
1136
def get_deltas_for_revisions(self, revisions):
1137
"""Produce a generator of revision deltas.
1139
Note that the input is a sequence of REVISIONS, not revision_ids.
1140
Trees will be held in memory until the generator exits.
1141
Each delta is relative to the revision's lefthand predecessor.
1143
required_trees = set()
1144
for revision in revisions:
1145
required_trees.add(revision.revision_id)
1146
required_trees.update(revision.parent_ids[:1])
1147
trees = dict((t.get_revision_id(), t) for
1148
t in self.revision_trees(required_trees))
1149
for revision in revisions:
1150
if not revision.parent_ids:
1151
old_tree = self.revision_tree(None)
1153
old_tree = trees[revision.parent_ids[0]]
1154
yield trees[revision.revision_id].changes_from(old_tree)
318
1156
@needs_read_lock
319
def get_revision(self, revision_id):
320
"""Return the Revision object for a named revision"""
321
r = self.get_revision_reconcile(revision_id)
322
# weave corruption can lead to absent revision markers that should be
324
# the following test is reasonably cheap (it needs a single weave read)
325
# and the weave is cached in read transactions. In write transactions
326
# it is not cached but typically we only read a small number of
327
# revisions. For knits when they are introduced we will probably want
328
# to ensure that caching write transactions are in use.
329
inv = self.get_inventory_weave()
330
self._check_revision_parents(r, inv)
1157
def get_revision_delta(self, revision_id):
1158
"""Return the delta for one revision.
333
def _check_revision_parents(self, revision, inventory):
334
"""Private to Repository and Fetch.
336
This checks the parentage of revision in an inventory weave for
337
consistency and is only applicable to inventory-weave-for-ancestry
338
using repository formats & fetchers.
1160
The delta is relative to the left-hand predecessor of the
340
weave_parents = inventory.get_parents(revision.revision_id)
341
weave_names = inventory.versions()
342
for parent_id in revision.parent_ids:
343
if parent_id in weave_names:
344
# this parent must not be a ghost.
345
if not parent_id in weave_parents:
347
raise errors.CorruptRepository(self)
1163
r = self.get_revision(revision_id)
1164
return list(self.get_deltas_for_revisions([r]))[0]
349
1166
@needs_write_lock
350
1167
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
351
1168
signature = gpg_strategy.sign(plaintext)
352
self._revision_store.add_revision_signature_text(revision_id,
354
self.get_transaction())
356
def fileids_altered_by_revision_ids(self, revision_ids):
357
"""Find the file ids and versions affected by revisions.
359
:param revisions: an iterable containing revision ids.
360
:return: a dictionary mapping altered file-ids to an iterable of
1169
self.add_signature_text(revision_id, signature)
1172
def add_signature_text(self, revision_id, signature):
1173
self.signatures.add_lines((revision_id,), (),
1174
osutils.split_lines(signature))
1176
def find_text_key_references(self):
1177
"""Find the text key references within the repository.
1179
:return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
361
1180
revision_ids. Each altered file-ids has the exact revision_ids that
362
1181
altered it listed explicitly.
364
assert isinstance(self._format, (RepositoryFormat5,
367
RepositoryFormatKnit1)), \
368
("fileids_altered_by_revision_ids only supported for branches "
369
"which store inventory as unnested xml, not on %r" % self)
370
selected_revision_ids = set(revision_ids)
371
w = self.get_inventory_weave()
1182
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1183
to whether they were referred to by the inventory of the
1184
revision_id that they contain. The inventory texts from all present
1185
revision ids are assessed to generate this report.
1187
revision_keys = self.revisions.keys()
1188
w = self.inventories
1189
pb = ui.ui_factory.nested_progress_bar()
1191
return self._find_text_key_references_from_xml_inventory_lines(
1192
w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
1196
def _find_text_key_references_from_xml_inventory_lines(self,
1198
"""Core routine for extracting references to texts from inventories.
1200
This performs the translation of xml lines to revision ids.
1202
:param line_iterator: An iterator of lines, origin_version_id
1203
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1204
to whether they were referred to by the inventory of the
1205
revision_id that they contain. Note that if that revision_id was
1206
not part of the line_iterator's output then False will be given -
1207
even though it may actually refer to that key.
1209
if not self._serializer.support_altered_by_hack:
1210
raise AssertionError(
1211
"_find_text_key_references_from_xml_inventory_lines only "
1212
"supported for branches which store inventory as unnested xml"
1213
", not on %r" % self)
374
1216
# this code needs to read every new line in every inventory for the
379
1221
# revisions. We don't need to see all lines in the inventory because
380
1222
# only those added in an inventory in rev X can contain a revision=X
382
for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
383
start = line.find('file_id="')+9
384
if start < 9: continue
385
end = line.find('"', start)
387
file_id = _unescape_xml(line[start:end])
389
start = line.find('revision="')+10
390
if start < 10: continue
391
end = line.find('"', start)
393
revision_id = _unescape_xml(line[start:end])
394
if revision_id in selected_revision_ids:
395
result.setdefault(file_id, set()).add(revision_id)
399
def get_inventory_weave(self):
400
return self.control_weaves.get_weave('inventory',
401
self.get_transaction())
1224
unescape_revid_cache = {}
1225
unescape_fileid_cache = {}
1227
# jam 20061218 In a big fetch, this handles hundreds of thousands
1228
# of lines, so it has had a lot of inlining and optimizing done.
1229
# Sorry that it is a little bit messy.
1230
# Move several functions to be local variables, since this is a long
1232
search = self._file_ids_altered_regex.search
1233
unescape = _unescape_xml
1234
setdefault = result.setdefault
1235
for line, line_key in line_iterator:
1236
match = search(line)
1239
# One call to match.group() returning multiple items is quite a
1240
# bit faster than 2 calls to match.group() each returning 1
1241
file_id, revision_id = match.group('file_id', 'revision_id')
1243
# Inlining the cache lookups helps a lot when you make 170,000
1244
# lines and 350k ids, versus 8.4 unique ids.
1245
# Using a cache helps in 2 ways:
1246
# 1) Avoids unnecessary decoding calls
1247
# 2) Re-uses cached strings, which helps in future set and
1249
# (2) is enough that removing encoding entirely along with
1250
# the cache (so we are using plain strings) results in no
1251
# performance improvement.
1253
revision_id = unescape_revid_cache[revision_id]
1255
unescaped = unescape(revision_id)
1256
unescape_revid_cache[revision_id] = unescaped
1257
revision_id = unescaped
1259
# Note that unconditionally unescaping means that we deserialise
1260
# every fileid, which for general 'pull' is not great, but we don't
1261
# really want to have some many fulltexts that this matters anyway.
1264
file_id = unescape_fileid_cache[file_id]
1266
unescaped = unescape(file_id)
1267
unescape_fileid_cache[file_id] = unescaped
1270
key = (file_id, revision_id)
1271
setdefault(key, False)
1272
if revision_id == line_key[-1]:
1276
def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1278
"""Helper routine for fileids_altered_by_revision_ids.
1280
This performs the translation of xml lines to revision ids.
1282
:param line_iterator: An iterator of lines, origin_version_id
1283
:param revision_ids: The revision ids to filter for. This should be a
1284
set or other type which supports efficient __contains__ lookups, as
1285
the revision id from each parsed line will be looked up in the
1286
revision_ids filter.
1287
:return: a dictionary mapping altered file-ids to an iterable of
1288
revision_ids. Each altered file-ids has the exact revision_ids that
1289
altered it listed explicitly.
1292
setdefault = result.setdefault
1294
self._find_text_key_references_from_xml_inventory_lines(
1295
line_iterator).iterkeys():
1296
# once data is all ensured-consistent; then this is
1297
# if revision_id == version_id
1298
if key[-1:] in revision_ids:
1299
setdefault(key[0], set()).add(key[-1])
1302
def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
1303
"""Find the file ids and versions affected by revisions.
1305
:param revisions: an iterable containing revision ids.
1306
:param _inv_weave: The inventory weave from this repository or None.
1307
If None, the inventory weave will be opened automatically.
1308
:return: a dictionary mapping altered file-ids to an iterable of
1309
revision_ids. Each altered file-ids has the exact revision_ids that
1310
altered it listed explicitly.
1312
selected_keys = set((revid,) for revid in revision_ids)
1313
w = _inv_weave or self.inventories
1314
pb = ui.ui_factory.nested_progress_bar()
1316
return self._find_file_ids_from_xml_inventory_lines(
1317
w.iter_lines_added_or_present_in_keys(
1318
selected_keys, pb=pb),
1323
def iter_files_bytes(self, desired_files):
1324
"""Iterate through file versions.
1326
Files will not necessarily be returned in the order they occur in
1327
desired_files. No specific order is guaranteed.
1329
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1330
value supplied by the caller as part of desired_files. It should
1331
uniquely identify the file version in the caller's context. (Examples:
1332
an index number or a TreeTransform trans_id.)
1334
bytes_iterator is an iterable of bytestrings for the file. The
1335
kind of iterable and length of the bytestrings are unspecified, but for
1336
this implementation, it is a list of bytes produced by
1337
VersionedFile.get_record_stream().
1339
:param desired_files: a list of (file_id, revision_id, identifier)
1342
transaction = self.get_transaction()
1344
for file_id, revision_id, callable_data in desired_files:
1345
text_keys[(file_id, revision_id)] = callable_data
1346
for record in self.texts.get_record_stream(text_keys, 'unordered', True):
1347
if record.storage_kind == 'absent':
1348
raise errors.RevisionNotPresent(record.key, self)
1349
yield text_keys[record.key], record.get_bytes_as('fulltext')
1351
def _generate_text_key_index(self, text_key_references=None,
1353
"""Generate a new text key index for the repository.
1355
This is an expensive function that will take considerable time to run.
1357
:return: A dict mapping text keys ((file_id, revision_id) tuples) to a
1358
list of parents, also text keys. When a given key has no parents,
1359
the parents list will be [NULL_REVISION].
1361
# All revisions, to find inventory parents.
1362
if ancestors is None:
1363
graph = self.get_graph()
1364
ancestors = graph.get_parent_map(self.all_revision_ids())
1365
if text_key_references is None:
1366
text_key_references = self.find_text_key_references()
1367
pb = ui.ui_factory.nested_progress_bar()
1369
return self._do_generate_text_key_index(ancestors,
1370
text_key_references, pb)
1374
def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
1375
"""Helper for _generate_text_key_index to avoid deep nesting."""
1376
revision_order = tsort.topo_sort(ancestors)
1377
invalid_keys = set()
1379
for revision_id in revision_order:
1380
revision_keys[revision_id] = set()
1381
text_count = len(text_key_references)
1382
# a cache of the text keys to allow reuse; costs a dict of all the
1383
# keys, but saves a 2-tuple for every child of a given key.
1385
for text_key, valid in text_key_references.iteritems():
1387
invalid_keys.add(text_key)
1389
revision_keys[text_key[1]].add(text_key)
1390
text_key_cache[text_key] = text_key
1391
del text_key_references
1393
text_graph = graph.Graph(graph.DictParentsProvider(text_index))
1394
NULL_REVISION = _mod_revision.NULL_REVISION
1395
# Set a cache with a size of 10 - this suffices for bzr.dev but may be
1396
# too small for large or very branchy trees. However, for 55K path
1397
# trees, it would be easy to use too much memory trivially. Ideally we
1398
# could gauge this by looking at available real memory etc, but this is
1399
# always a tricky proposition.
1400
inventory_cache = lru_cache.LRUCache(10)
1401
batch_size = 10 # should be ~150MB on a 55K path tree
1402
batch_count = len(revision_order) / batch_size + 1
1404
pb.update("Calculating text parents.", processed_texts, text_count)
1405
for offset in xrange(batch_count):
1406
to_query = revision_order[offset * batch_size:(offset + 1) *
1410
for rev_tree in self.revision_trees(to_query):
1411
revision_id = rev_tree.get_revision_id()
1412
parent_ids = ancestors[revision_id]
1413
for text_key in revision_keys[revision_id]:
1414
pb.update("Calculating text parents.", processed_texts)
1415
processed_texts += 1
1416
candidate_parents = []
1417
for parent_id in parent_ids:
1418
parent_text_key = (text_key[0], parent_id)
1420
check_parent = parent_text_key not in \
1421
revision_keys[parent_id]
1423
# the parent parent_id is a ghost:
1424
check_parent = False
1425
# truncate the derived graph against this ghost.
1426
parent_text_key = None
1428
# look at the parent commit details inventories to
1429
# determine possible candidates in the per file graph.
1432
inv = inventory_cache[parent_id]
1434
inv = self.revision_tree(parent_id).inventory
1435
inventory_cache[parent_id] = inv
1436
parent_entry = inv._byid.get(text_key[0], None)
1437
if parent_entry is not None:
1439
text_key[0], parent_entry.revision)
1441
parent_text_key = None
1442
if parent_text_key is not None:
1443
candidate_parents.append(
1444
text_key_cache[parent_text_key])
1445
parent_heads = text_graph.heads(candidate_parents)
1446
new_parents = list(parent_heads)
1447
new_parents.sort(key=lambda x:candidate_parents.index(x))
1448
if new_parents == []:
1449
new_parents = [NULL_REVISION]
1450
text_index[text_key] = new_parents
1452
for text_key in invalid_keys:
1453
text_index[text_key] = [NULL_REVISION]
1456
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1457
"""Get an iterable listing the keys of all the data introduced by a set
1460
The keys will be ordered so that the corresponding items can be safely
1461
fetched and inserted in that order.
1463
:returns: An iterable producing tuples of (knit-kind, file-id,
1464
versions). knit-kind is one of 'file', 'inventory', 'signatures',
1465
'revisions'. file-id is None unless knit-kind is 'file'.
1467
# XXX: it's a bit weird to control the inventory weave caching in this
1468
# generator. Ideally the caching would be done in fetch.py I think. Or
1469
# maybe this generator should explicitly have the contract that it
1470
# should not be iterated until the previously yielded item has been
1472
inv_w = self.inventories
1474
# file ids that changed
1475
file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
1477
num_file_ids = len(file_ids)
1478
for file_id, altered_versions in file_ids.iteritems():
1479
if _files_pb is not None:
1480
_files_pb.update("fetch texts", count, num_file_ids)
1482
yield ("file", file_id, altered_versions)
1483
# We're done with the files_pb. Note that it finished by the caller,
1484
# just as it was created by the caller.
1488
yield ("inventory", None, revision_ids)
1491
revisions_with_signatures = set()
1492
for rev_id in revision_ids:
1494
self.get_signature_text(rev_id)
1495
except errors.NoSuchRevision:
1499
revisions_with_signatures.add(rev_id)
1500
yield ("signatures", None, revisions_with_signatures)
1503
yield ("revisions", None, revision_ids)
403
1505
@needs_read_lock
404
1506
def get_inventory(self, revision_id):
405
"""Get Inventory object by hash."""
406
return self.deserialise_inventory(
407
revision_id, self.get_inventory_xml(revision_id))
1507
"""Get Inventory object by revision id."""
1508
return self.iter_inventories([revision_id]).next()
1510
def iter_inventories(self, revision_ids):
1511
"""Get many inventories by revision_ids.
1513
This will buffer some or all of the texts used in constructing the
1514
inventories in memory, but will only parse a single inventory at a
1517
:return: An iterator of inventories.
1519
if ((None in revision_ids)
1520
or (_mod_revision.NULL_REVISION in revision_ids)):
1521
raise ValueError('cannot get null revision inventory')
1522
return self._iter_inventories(revision_ids)
1524
def _iter_inventories(self, revision_ids):
1525
"""single-document based inventory iteration."""
1526
for text, revision_id in self._iter_inventory_xmls(revision_ids):
1527
yield self.deserialise_inventory(revision_id, text)
1529
def _iter_inventory_xmls(self, revision_ids):
1530
keys = [(revision_id,) for revision_id in revision_ids]
1531
stream = self.inventories.get_record_stream(keys, 'unordered', True)
1533
for record in stream:
1534
if record.storage_kind != 'absent':
1535
texts[record.key] = record.get_bytes_as('fulltext')
1537
raise errors.NoSuchRevision(self, record.key)
1539
yield texts[key], key[-1]
409
1541
def deserialise_inventory(self, revision_id, xml):
410
1542
"""Transform the xml into an inventory object.
618
1829
:param revision_ids: A non-empty list of revision_ids whose ancestry
619
1830
will be checked. Typically the last revision_id of a branch.
622
raise ValueError("revision_ids must be non-empty in %s.check"
624
1832
return self._check(revision_ids)
626
1834
def _check(self, revision_ids):
627
result = bzrlib.check.Check(self)
1835
result = check.Check(self)
632
class AllInOneRepository(Repository):
633
"""Legacy support - the repository behaviour for all-in-one branches."""
635
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
636
# we reuse one control files instance.
637
dir_mode = a_bzrdir._control_files._dir_mode
638
file_mode = a_bzrdir._control_files._file_mode
640
def get_store(name, compressed=True, prefixed=False):
641
# FIXME: This approach of assuming stores are all entirely compressed
642
# or entirely uncompressed is tidy, but breaks upgrade from
643
# some existing branches where there's a mixture; we probably
644
# still want the option to look for both.
645
relpath = a_bzrdir._control_files._escape(name)
646
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
647
prefixed=prefixed, compressed=compressed,
650
#if self._transport.should_cache():
651
# cache_path = os.path.join(self.cache_root, name)
652
# os.mkdir(cache_path)
653
# store = bzrlib.store.CachedStore(store, cache_path)
656
# not broken out yet because the controlweaves|inventory_store
657
# and text_store | weave_store bits are still different.
658
if isinstance(_format, RepositoryFormat4):
659
# cannot remove these - there is still no consistent api
660
# which allows access to this old info.
661
self.inventory_store = get_store('inventory-store')
662
text_store = get_store('text-store')
663
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
667
"""AllInOne repositories cannot be shared."""
671
def set_make_working_trees(self, new_value):
672
"""Set the policy flag for making working trees when creating branches.
674
This only applies to branches that use this repository.
676
The default is 'True'.
677
:param new_value: True to restore the default, False to disable making
1839
def _warn_if_deprecated(self):
1840
global _deprecation_warning_done
1841
if _deprecation_warning_done:
1843
_deprecation_warning_done = True
1844
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1845
% (self._format, self.bzrdir.transport.base))
1847
def supports_rich_root(self):
1848
return self._format.rich_root_data
1850
def _check_ascii_revisionid(self, revision_id, method):
1851
"""Private helper for ascii-only repositories."""
1852
# weave repositories refuse to store revisionids that are non-ascii.
1853
if revision_id is not None:
1854
# weaves require ascii revision ids.
1855
if isinstance(revision_id, unicode):
1857
revision_id.encode('ascii')
1858
except UnicodeEncodeError:
1859
raise errors.NonAsciiRevisionId(method, self)
1862
revision_id.decode('ascii')
1863
except UnicodeDecodeError:
1864
raise errors.NonAsciiRevisionId(method, self)
1866
def revision_graph_can_have_wrong_parents(self):
1867
"""Is it possible for this repository to have a revision graph with
1870
If True, then this repository must also implement
1871
_find_inconsistent_revision_parents so that check and reconcile can
1872
check for inconsistencies before proceeding with other checks that may
1873
depend on the revision index being consistent.
680
raise NotImplementedError(self.set_make_working_trees)
682
def make_working_trees(self):
683
"""Returns the policy for making working trees on new branches."""
1875
raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1878
# remove these delegates a while after bzr 0.15
1879
def __make_delegated(name, from_module):
1880
def _deprecated_repository_forwarder():
1881
symbol_versioning.warn('%s moved to %s in bzr 0.15'
1882
% (name, from_module),
1885
m = __import__(from_module, globals(), locals(), [name])
1887
return getattr(m, name)
1888
except AttributeError:
1889
raise AttributeError('module %s has no name %s'
1891
globals()[name] = _deprecated_repository_forwarder
1894
'AllInOneRepository',
1895
'WeaveMetaDirRepository',
1896
'PreSplitOutRepositoryFormat',
1897
'RepositoryFormat4',
1898
'RepositoryFormat5',
1899
'RepositoryFormat6',
1900
'RepositoryFormat7',
1902
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1906
'RepositoryFormatKnit',
1907
'RepositoryFormatKnit1',
1909
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
687
1912
def install_revision(repository, rev, revision_tree):
688
1913
"""Install all revision data into a repository."""
1914
install_revisions(repository, [(rev, revision_tree, None)])
1917
def install_revisions(repository, iterable, num_revisions=None, pb=None):
1918
"""Install all revision data into a repository.
1920
Accepts an iterable of revision, tree, signature tuples. The signature
1923
repository.start_write_group()
1925
for n, (revision, revision_tree, signature) in enumerate(iterable):
1926
_install_revision(repository, revision, revision_tree, signature)
1928
pb.update('Transferring revisions', n + 1, num_revisions)
1930
repository.abort_write_group()
1933
repository.commit_write_group()
1936
def _install_revision(repository, rev, revision_tree, signature):
1937
"""Install all revision data into a repository."""
689
1938
present_parents = []
690
1939
parent_trees = {}
691
1940
for p_id in rev.parent_ids:
693
1942
present_parents.append(p_id)
694
1943
parent_trees[p_id] = repository.revision_tree(p_id)
696
parent_trees[p_id] = EmptyTree()
1945
parent_trees[p_id] = repository.revision_tree(None)
698
1947
inv = revision_tree.inventory
1948
entries = inv.iter_entries()
1949
# backwards compatibility hack: skip the root id.
1950
if not repository.supports_rich_root():
1951
path, root = entries.next()
1952
if root.revision != rev.revision_id:
1953
raise errors.IncompatibleRevision(repr(repository))
1955
for path, ie in entries:
1956
text_keys[(ie.file_id, ie.revision)] = ie
1957
text_parent_map = repository.texts.get_parent_map(text_keys)
1958
missing_texts = set(text_keys) - set(text_parent_map)
700
1959
# Add the texts that are not already present
701
for path, ie in inv.iter_entries():
702
w = repository.weave_store.get_weave_or_empty(ie.file_id,
703
repository.get_transaction())
704
if ie.revision not in w:
706
# FIXME: TODO: The following loop *may* be overlapping/duplicate
707
# with InventoryEntry.find_previous_heads(). if it is, then there
708
# is a latent bug here where the parents may have ancestors of each
710
for revision, tree in parent_trees.iteritems():
711
if ie.file_id not in tree:
713
parent_id = tree.inventory[ie.file_id].revision
714
if parent_id in text_parents:
716
text_parents.append(parent_id)
718
vfile = repository.weave_store.get_weave_or_empty(ie.file_id,
719
repository.get_transaction())
720
lines = revision_tree.get_file(ie.file_id).readlines()
721
vfile.add_lines(rev.revision_id, text_parents, lines)
1960
for text_key in missing_texts:
1961
ie = text_keys[text_key]
1963
# FIXME: TODO: The following loop overlaps/duplicates that done by
1964
# commit to determine parents. There is a latent/real bug here where
1965
# the parents inserted are not those commit would do - in particular
1966
# they are not filtered by heads(). RBC, AB
1967
for revision, tree in parent_trees.iteritems():
1968
if ie.file_id not in tree:
1970
parent_id = tree.inventory[ie.file_id].revision
1971
if parent_id in text_parents:
1973
text_parents.append((ie.file_id, parent_id))
1974
lines = revision_tree.get_file(ie.file_id).readlines()
1975
repository.texts.add_lines(text_key, text_parents, lines)
723
1977
# install the inventory
724
1978
repository.add_inventory(rev.revision_id, inv, present_parents)
725
1979
except errors.RevisionAlreadyPresent:
1981
if signature is not None:
1982
repository.add_signature_text(rev.revision_id, signature)
727
1983
repository.add_revision(rev.revision_id, rev, inv)
730
1986
class MetaDirRepository(Repository):
731
"""Repositories in the new meta-dir layout."""
733
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
734
super(MetaDirRepository, self).__init__(_format,
741
dir_mode = self.control_files._dir_mode
742
file_mode = self.control_files._file_mode
1987
"""Repositories in the new meta-dir layout.
1989
:ivar _transport: Transport for access to repository control files,
1990
typically pointing to .bzr/repository.
1993
def __init__(self, _format, a_bzrdir, control_files):
1994
super(MetaDirRepository, self).__init__(_format, a_bzrdir, control_files)
1995
self._transport = control_files._transport
744
1997
@needs_read_lock
745
1998
def is_shared(self):
746
1999
"""Return True if this repository is flagged as a shared repository."""
747
return self.control_files._transport.has('shared-storage')
2000
return self._transport.has('shared-storage')
749
2002
@needs_write_lock
750
2003
def set_make_working_trees(self, new_value):
761
self.control_files._transport.delete('no-working-trees')
2014
self._transport.delete('no-working-trees')
762
2015
except errors.NoSuchFile:
765
self.control_files.put_utf8('no-working-trees', '')
2018
self._transport.put_bytes('no-working-trees', '',
2019
mode=self.bzrdir._get_file_mode())
767
2021
def make_working_trees(self):
768
2022
"""Returns the policy for making working trees on new branches."""
769
return not self.control_files._transport.has('no-working-trees')
772
class KnitRepository(MetaDirRepository):
773
"""Knit format repository."""
775
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
776
inv_vf.add_lines_with_ghosts(revid, parents, lines)
779
def _all_revision_ids(self):
780
"""See Repository.all_revision_ids()."""
781
# Knits get the revision graph from the index of the revision knit, so
782
# it's always possible even if they're on an unlistable transport.
783
return self._revision_store.all_revision_ids(self.get_transaction())
785
def fileid_involved_between_revs(self, from_revid, to_revid):
786
"""Find file_id(s) which are involved in the changes between revisions.
788
This determines the set of revisions which are involved, and then
789
finds all file ids affected by those revisions.
791
vf = self._get_revision_vf()
792
from_set = set(vf.get_ancestry(from_revid))
793
to_set = set(vf.get_ancestry(to_revid))
794
changed = to_set.difference(from_set)
795
return self._fileid_involved_by_set(changed)
797
def fileid_involved(self, last_revid=None):
798
"""Find all file_ids modified in the ancestry of last_revid.
800
:param last_revid: If None, last_revision() will be used.
803
changed = set(self.all_revision_ids())
805
changed = set(self.get_ancestry(last_revid))
808
return self._fileid_involved_by_set(changed)
811
def get_ancestry(self, revision_id):
812
"""Return a list of revision-ids integrated by a revision.
814
This is topologically sorted.
816
if revision_id is None:
818
vf = self._get_revision_vf()
820
return [None] + vf.get_ancestry(revision_id)
821
except errors.RevisionNotPresent:
822
raise errors.NoSuchRevision(self, revision_id)
825
def get_revision(self, revision_id):
826
"""Return the Revision object for a named revision"""
827
return self.get_revision_reconcile(revision_id)
830
def get_revision_graph(self, revision_id=None):
831
"""Return a dictionary containing the revision graph.
833
:return: a dictionary of revision_id->revision_parents_list.
835
weave = self._get_revision_vf()
836
entire_graph = weave.get_graph()
837
if revision_id is None:
838
return weave.get_graph()
839
elif revision_id not in weave:
840
raise errors.NoSuchRevision(self, revision_id)
842
# add what can be reached from revision_id
844
pending = set([revision_id])
845
while len(pending) > 0:
847
result[node] = weave.get_parents(node)
848
for revision_id in result[node]:
849
if revision_id not in result:
850
pending.add(revision_id)
854
def get_revision_graph_with_ghosts(self, revision_ids=None):
855
"""Return a graph of the revisions with ghosts marked as applicable.
857
:param revision_ids: an iterable of revisions to graph or None for all.
858
:return: a Graph object with the graph reachable from revision_ids.
861
vf = self._get_revision_vf()
862
versions = set(vf.versions())
864
pending = set(self.all_revision_ids())
867
pending = set(revision_ids)
868
required = set(revision_ids)
871
revision_id = pending.pop()
872
if not revision_id in versions:
873
if revision_id in required:
874
raise errors.NoSuchRevision(self, revision_id)
876
result.add_ghost(revision_id)
877
# mark it as done so we don't try for it again.
878
done.add(revision_id)
880
parent_ids = vf.get_parents_with_ghosts(revision_id)
881
for parent_id in parent_ids:
882
# is this queued or done ?
883
if (parent_id not in pending and
884
parent_id not in done):
886
pending.add(parent_id)
887
result.add_node(revision_id, parent_ids)
888
done.add(revision_id)
891
def _get_revision_vf(self):
892
""":return: a versioned file containing the revisions."""
893
vf = self._revision_store.get_revision_file(self.get_transaction())
897
def reconcile(self, other=None, thorough=False):
898
"""Reconcile this repository."""
899
from bzrlib.reconcile import KnitReconciler
900
reconciler = KnitReconciler(self, thorough=thorough)
901
reconciler.reconcile()
2023
return not self._transport.has('no-working-trees')
2026
class MetaDirVersionedFileRepository(MetaDirRepository):
2027
"""Repositories in a meta-dir, that work via versioned file objects."""
2029
def __init__(self, _format, a_bzrdir, control_files):
2030
super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
2034
class RepositoryFormatRegistry(registry.Registry):
2035
"""Registry of RepositoryFormats."""
2037
def get(self, format_string):
2038
r = registry.Registry.get(self, format_string)
904
def revision_parents(self, revid):
905
return self._get_revision_vf().get_parents(rev_id)
2044
format_registry = RepositoryFormatRegistry()
2045
"""Registry of formats, indexed by their identifying format string.
2047
This can contain either format instances themselves, or classes/factories that
2048
can be called to obtain one.
2052
#####################################################################
2053
# Repository Formats
907
2055
class RepositoryFormat(object):
908
2056
"""A repository format.
1037
2196
raise NotImplementedError(self.open)
1040
def register_format(klass, format):
1041
klass._formats[format.get_format_string()] = format
1044
def set_default_format(klass, format):
1045
klass._default_format = format
1048
def unregister_format(klass, format):
1049
assert klass._formats[format.get_format_string()] is format
1050
del klass._formats[format.get_format_string()]
1053
class PreSplitOutRepositoryFormat(RepositoryFormat):
1054
"""Base class for the pre split out repository formats."""
1056
def initialize(self, a_bzrdir, shared=False, _internal=False):
1057
"""Create a weave repository.
1059
TODO: when creating split out bzr branch formats, move this to a common
1060
base for Format5, Format6. or something like that.
1062
from bzrlib.weavefile import write_weave_v5
1063
from bzrlib.weave import Weave
1066
raise errors.IncompatibleFormat(self, a_bzrdir._format)
1069
# always initialized when the bzrdir is.
1070
return self.open(a_bzrdir, _found=True)
1072
# Create an empty weave
1074
bzrlib.weavefile.write_weave_v5(Weave(), sio)
1075
empty_weave = sio.getvalue()
1077
mutter('creating repository in %s.', a_bzrdir.transport.base)
1078
dirs = ['revision-store', 'weaves']
1079
files = [('inventory.weave', StringIO(empty_weave)),
1082
# FIXME: RBC 20060125 don't peek under the covers
1083
# NB: no need to escape relative paths that are url safe.
1084
control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
1086
control_files.create_lock()
1087
control_files.lock_write()
1088
control_files._transport.mkdir_multi(dirs,
1089
mode=control_files._dir_mode)
1091
for file, content in files:
1092
control_files.put(file, content)
1094
control_files.unlock()
1095
return self.open(a_bzrdir, _found=True)
1097
def _get_control_store(self, repo_transport, control_files):
1098
"""Return the control store for this repository."""
1099
return self._get_versioned_file_store('',
1104
def _get_text_store(self, transport, control_files):
1105
"""Get a store for file texts for this format."""
1106
raise NotImplementedError(self._get_text_store)
1108
def open(self, a_bzrdir, _found=False):
1109
"""See RepositoryFormat.open()."""
1111
# we are being called directly and must probe.
1112
raise NotImplementedError
1114
repo_transport = a_bzrdir.get_repository_transport(None)
1115
control_files = a_bzrdir._control_files
1116
text_store = self._get_text_store(repo_transport, control_files)
1117
control_store = self._get_control_store(repo_transport, control_files)
1118
_revision_store = self._get_revision_store(repo_transport, control_files)
1119
return AllInOneRepository(_format=self,
1121
_revision_store=_revision_store,
1122
control_store=control_store,
1123
text_store=text_store)
1126
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1127
"""Bzr repository format 4.
1129
This repository format has:
1131
- TextStores for texts, inventories,revisions.
1133
This format is deprecated: it indexes texts using a text id which is
1134
removed in format 5; initialization and write support for this format
1139
super(RepositoryFormat4, self).__init__()
1140
self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat4()
1142
def get_format_description(self):
1143
"""See RepositoryFormat.get_format_description()."""
1144
return "Repository format 4"
1146
def initialize(self, url, shared=False, _internal=False):
1147
"""Format 4 branches cannot be created."""
1148
raise errors.UninitializableFormat(self)
1150
def is_supported(self):
1151
"""Format 4 is not supported.
1153
It is not supported because the model changed from 4 to 5 and the
1154
conversion logic is expensive - so doing it on the fly was not
1159
def _get_control_store(self, repo_transport, control_files):
1160
"""Format 4 repositories have no formal control store at this point.
1162
This will cause any control-file-needing apis to fail - this is desired.
1166
def _get_revision_store(self, repo_transport, control_files):
1167
"""See RepositoryFormat._get_revision_store()."""
1168
from bzrlib.xml4 import serializer_v4
1169
return self._get_text_rev_store(repo_transport,
1172
serializer=serializer_v4)
1174
def _get_text_store(self, transport, control_files):
1175
"""See RepositoryFormat._get_text_store()."""
1178
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1179
"""Bzr control format 5.
1181
This repository format has:
1182
- weaves for file texts and inventory
1184
- TextStores for revisions and signatures.
1188
super(RepositoryFormat5, self).__init__()
1189
self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat5()
1191
def get_format_description(self):
1192
"""See RepositoryFormat.get_format_description()."""
1193
return "Weave repository format 5"
1195
def _get_revision_store(self, repo_transport, control_files):
1196
"""See RepositoryFormat._get_revision_store()."""
1197
"""Return the revision store object for this a_bzrdir."""
1198
return self._get_text_rev_store(repo_transport,
1203
def _get_text_store(self, transport, control_files):
1204
"""See RepositoryFormat._get_text_store()."""
1205
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1208
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1209
"""Bzr control format 6.
1211
This repository format has:
1212
- weaves for file texts and inventory
1213
- hash subdirectory based stores.
1214
- TextStores for revisions and signatures.
1218
super(RepositoryFormat6, self).__init__()
1219
self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat6()
1221
def get_format_description(self):
1222
"""See RepositoryFormat.get_format_description()."""
1223
return "Weave repository format 6"
1225
def _get_revision_store(self, repo_transport, control_files):
1226
"""See RepositoryFormat._get_revision_store()."""
1227
return self._get_text_rev_store(repo_transport,
1233
def _get_text_store(self, transport, control_files):
1234
"""See RepositoryFormat._get_text_store()."""
1235
return self._get_versioned_file_store('weaves', transport, control_files)
1238
2199
class MetaDirRepositoryFormat(RepositoryFormat):
1239
2200
"""Common base class for the new repositories using the metadir layout."""
2202
rich_root_data = False
2203
supports_tree_reference = False
2204
supports_external_lookups = False
2205
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1241
2207
def __init__(self):
1242
2208
super(MetaDirRepositoryFormat, self).__init__()
1243
self._matchingbzrdir = bzrlib.bzrdir.BzrDirMetaFormat1()
1245
2210
def _create_control_files(self, a_bzrdir):
1246
2211
"""Create the required files and the initial control_files object."""
1247
2212
# FIXME: RBC 20060125 don't peek under the covers
1248
2213
# NB: no need to escape relative paths that are url safe.
1249
2214
repository_transport = a_bzrdir.get_repository_transport(self)
1250
control_files = LockableFiles(repository_transport, 'lock', LockDir)
2215
control_files = lockable_files.LockableFiles(repository_transport,
2216
'lock', lockdir.LockDir)
1251
2217
control_files.create_lock()
1252
2218
return control_files
1255
2221
"""Upload the initial blank content."""
1256
2222
control_files = self._create_control_files(a_bzrdir)
1257
2223
control_files.lock_write()
2224
transport = control_files._transport
2226
utf8_files += [('shared-storage', '')]
1259
control_files._transport.mkdir_multi(dirs,
1260
mode=control_files._dir_mode)
1261
for file, content in files:
1262
control_files.put(file, content)
1263
for file, content in utf8_files:
1264
control_files.put_utf8(file, content)
1266
control_files.put_utf8('shared-storage', '')
2228
transport.mkdir_multi(dirs, mode=a_bzrdir._get_dir_mode())
2229
for (filename, content_stream) in files:
2230
transport.put_file(filename, content_stream,
2231
mode=a_bzrdir._get_file_mode())
2232
for (filename, content_bytes) in utf8_files:
2233
transport.put_bytes_non_atomic(filename, content_bytes,
2234
mode=a_bzrdir._get_file_mode())
1268
2236
control_files.unlock()
1271
class RepositoryFormat7(MetaDirRepositoryFormat):
1272
"""Bzr repository 7.
1274
This repository format has:
1275
- weaves for file texts and inventory
1276
- hash subdirectory based stores.
1277
- TextStores for revisions and signatures.
1278
- a format marker of its own
1279
- an optional 'shared-storage' flag
1280
- an optional 'no-working-trees' flag
1283
def _get_control_store(self, repo_transport, control_files):
1284
"""Return the control store for this repository."""
1285
return self._get_versioned_file_store('',
1290
def get_format_string(self):
1291
"""See RepositoryFormat.get_format_string()."""
1292
return "Bazaar-NG Repository format 7"
1294
def get_format_description(self):
1295
"""See RepositoryFormat.get_format_description()."""
1296
return "Weave repository format 7"
1298
def _get_revision_store(self, repo_transport, control_files):
1299
"""See RepositoryFormat._get_revision_store()."""
1300
return self._get_text_rev_store(repo_transport,
1307
def _get_text_store(self, transport, control_files):
1308
"""See RepositoryFormat._get_text_store()."""
1309
return self._get_versioned_file_store('weaves',
1313
def initialize(self, a_bzrdir, shared=False):
1314
"""Create a weave repository.
1316
:param shared: If true the repository will be initialized as a shared
1319
from bzrlib.weavefile import write_weave_v5
1320
from bzrlib.weave import Weave
1322
# Create an empty weave
1324
bzrlib.weavefile.write_weave_v5(Weave(), sio)
1325
empty_weave = sio.getvalue()
1327
mutter('creating repository in %s.', a_bzrdir.transport.base)
1328
dirs = ['revision-store', 'weaves']
1329
files = [('inventory.weave', StringIO(empty_weave)),
1331
utf8_files = [('format', self.get_format_string())]
1333
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1334
return self.open(a_bzrdir=a_bzrdir, _found=True)
1336
def open(self, a_bzrdir, _found=False, _override_transport=None):
1337
"""See RepositoryFormat.open().
1339
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1340
repository at a slightly different url
1341
than normal. I.e. during 'upgrade'.
1344
format = RepositoryFormat.find_format(a_bzrdir)
1345
assert format.__class__ == self.__class__
1346
if _override_transport is not None:
1347
repo_transport = _override_transport
1349
repo_transport = a_bzrdir.get_repository_transport(None)
1350
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1351
text_store = self._get_text_store(repo_transport, control_files)
1352
control_store = self._get_control_store(repo_transport, control_files)
1353
_revision_store = self._get_revision_store(repo_transport, control_files)
1354
return MetaDirRepository(_format=self,
1356
control_files=control_files,
1357
_revision_store=_revision_store,
1358
control_store=control_store,
1359
text_store=text_store)
1362
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1363
"""Bzr repository knit format 1.
1365
This repository format has:
1366
- knits for file texts and inventory
1367
- hash subdirectory based stores.
1368
- knits for revisions and signatures
1369
- TextStores for revisions and signatures.
1370
- a format marker of its own
1371
- an optional 'shared-storage' flag
1372
- an optional 'no-working-trees' flag
1375
This format was introduced in bzr 0.8.
1378
def _get_control_store(self, repo_transport, control_files):
1379
"""Return the control store for this repository."""
1380
return VersionedFileStore(
1383
file_mode=control_files._file_mode,
1384
versionedfile_class=KnitVersionedFile,
1385
versionedfile_kwargs={'factory':KnitPlainFactory()},
1388
def get_format_string(self):
1389
"""See RepositoryFormat.get_format_string()."""
1390
return "Bazaar-NG Knit Repository Format 1"
1392
def get_format_description(self):
1393
"""See RepositoryFormat.get_format_description()."""
1394
return "Knit repository format 1"
1396
def _get_revision_store(self, repo_transport, control_files):
1397
"""See RepositoryFormat._get_revision_store()."""
1398
from bzrlib.store.revision.knit import KnitRevisionStore
1399
versioned_file_store = VersionedFileStore(
1401
file_mode=control_files._file_mode,
1404
versionedfile_class=KnitVersionedFile,
1405
versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
1408
return KnitRevisionStore(versioned_file_store)
1410
def _get_text_store(self, transport, control_files):
1411
"""See RepositoryFormat._get_text_store()."""
1412
return self._get_versioned_file_store('knits',
1415
versionedfile_class=KnitVersionedFile,
1418
def initialize(self, a_bzrdir, shared=False):
1419
"""Create a knit format 1 repository.
1421
:param a_bzrdir: bzrdir to contain the new repository; must already
1423
:param shared: If true the repository will be initialized as a shared
1426
mutter('creating repository in %s.', a_bzrdir.transport.base)
1427
dirs = ['revision-store', 'knits']
1429
utf8_files = [('format', self.get_format_string())]
1431
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1432
repo_transport = a_bzrdir.get_repository_transport(None)
1433
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1434
control_store = self._get_control_store(repo_transport, control_files)
1435
transaction = bzrlib.transactions.WriteTransaction()
1436
# trigger a write of the inventory store.
1437
control_store.get_weave_or_empty('inventory', transaction)
1438
_revision_store = self._get_revision_store(repo_transport, control_files)
1439
_revision_store.has_revision_id('A', transaction)
1440
_revision_store.get_signature_file(transaction)
1441
return self.open(a_bzrdir=a_bzrdir, _found=True)
1443
def open(self, a_bzrdir, _found=False, _override_transport=None):
1444
"""See RepositoryFormat.open().
1446
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1447
repository at a slightly different url
1448
than normal. I.e. during 'upgrade'.
1451
format = RepositoryFormat.find_format(a_bzrdir)
1452
assert format.__class__ == self.__class__
1453
if _override_transport is not None:
1454
repo_transport = _override_transport
1456
repo_transport = a_bzrdir.get_repository_transport(None)
1457
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1458
text_store = self._get_text_store(repo_transport, control_files)
1459
control_store = self._get_control_store(repo_transport, control_files)
1460
_revision_store = self._get_revision_store(repo_transport, control_files)
1461
return KnitRepository(_format=self,
1463
control_files=control_files,
1464
_revision_store=_revision_store,
1465
control_store=control_store,
1466
text_store=text_store)
1469
2239
# formats which have no format string are not discoverable
1470
# and not independently creatable, so are not registered.
1471
RepositoryFormat.register_format(RepositoryFormat7())
1472
_default_format = RepositoryFormatKnit1()
1473
RepositoryFormat.register_format(_default_format)
1474
RepositoryFormat.set_default_format(_default_format)
1475
_legacy_formats = [RepositoryFormat4(),
1476
RepositoryFormat5(),
1477
RepositoryFormat6()]
2240
# and not independently creatable, so are not registered. They're
2241
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
2242
# needed, it's constructed directly by the BzrDir. Non-native formats where
2243
# the repository is not separately opened are similar.
2245
format_registry.register_lazy(
2246
'Bazaar-NG Repository format 7',
2247
'bzrlib.repofmt.weaverepo',
2251
format_registry.register_lazy(
2252
'Bazaar-NG Knit Repository Format 1',
2253
'bzrlib.repofmt.knitrepo',
2254
'RepositoryFormatKnit1',
2257
format_registry.register_lazy(
2258
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
2259
'bzrlib.repofmt.knitrepo',
2260
'RepositoryFormatKnit3',
2263
format_registry.register_lazy(
2264
'Bazaar Knit Repository Format 4 (bzr 1.0)\n',
2265
'bzrlib.repofmt.knitrepo',
2266
'RepositoryFormatKnit4',
2269
# Pack-based formats. There is one format for pre-subtrees, and one for
2270
# post-subtrees to allow ease of testing.
2271
# NOTE: These are experimental in 0.92. Stable in 1.0 and above
2272
format_registry.register_lazy(
2273
'Bazaar pack repository format 1 (needs bzr 0.92)\n',
2274
'bzrlib.repofmt.pack_repo',
2275
'RepositoryFormatKnitPack1',
2277
format_registry.register_lazy(
2278
'Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n',
2279
'bzrlib.repofmt.pack_repo',
2280
'RepositoryFormatKnitPack3',
2282
format_registry.register_lazy(
2283
'Bazaar pack repository format 1 with rich root (needs bzr 1.0)\n',
2284
'bzrlib.repofmt.pack_repo',
2285
'RepositoryFormatKnitPack4',
2287
# Development formats.
2289
# development 0 - stub to introduce development versioning scheme.
2290
format_registry.register_lazy(
2291
"Bazaar development format 0 (needs bzr.dev from before 1.3)\n",
2292
'bzrlib.repofmt.pack_repo',
2293
'RepositoryFormatPackDevelopment0',
2295
format_registry.register_lazy(
2296
("Bazaar development format 0 with subtree support "
2297
"(needs bzr.dev from before 1.3)\n"),
2298
'bzrlib.repofmt.pack_repo',
2299
'RepositoryFormatPackDevelopment0Subtree',
2301
format_registry.register_lazy(
2302
"Bazaar development format 1 (needs bzr.dev from before 1.6)\n",
2303
'bzrlib.repofmt.pack_repo',
2304
'RepositoryFormatPackDevelopment1',
2306
format_registry.register_lazy(
2307
("Bazaar development format 1 with subtree support "
2308
"(needs bzr.dev from before 1.6)\n"),
2309
'bzrlib.repofmt.pack_repo',
2310
'RepositoryFormatPackDevelopment1Subtree',
2312
# 1.3->1.4 go below here
1480
2315
class InterRepository(InterObject):
1541
2343
Returns the copied revision count and the failed revisions in a tuple:
1542
2344
(copied, failures).
1544
from bzrlib.fetch import GenericRepoFetcher
1545
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1546
self.source, self.source._format, self.target, self.target._format)
1547
f = GenericRepoFetcher(to_repository=self.target,
1548
from_repository=self.source,
1549
last_revision=revision_id,
1551
return f.count_copied, f.failed_revisions
1553
def lock_read(self):
1554
"""Take out a logical read lock.
1556
This will lock the source branch and the target branch. The source gets
1557
a read lock and the target a read lock.
1559
self._double_lock(self.source.lock_read, self.target.lock_read)
1561
def lock_write(self):
1562
"""Take out a logical write lock.
1564
This will lock the source branch and the target branch. The source gets
1565
a read lock and the target a write lock.
1567
self._double_lock(self.source.lock_read, self.target.lock_write)
2346
raise NotImplementedError(self.fetch)
2348
def _walk_to_common_revisions(self, revision_ids):
2349
"""Walk out from revision_ids in source to revisions target has.
2351
:param revision_ids: The start point for the search.
2352
:return: A set of revision ids.
2354
target_graph = self.target.get_graph()
2355
revision_ids = frozenset(revision_ids)
2356
if set(target_graph.get_parent_map(revision_ids)) == revision_ids:
2357
return graph.SearchResult(revision_ids, set(), 0, set())
2358
missing_revs = set()
2359
source_graph = self.source.get_graph()
2360
# ensure we don't pay silly lookup costs.
2361
searcher = source_graph._make_breadth_first_searcher(revision_ids)
2362
null_set = frozenset([_mod_revision.NULL_REVISION])
2365
next_revs, ghosts = searcher.next_with_ghosts()
2366
except StopIteration:
2368
if revision_ids.intersection(ghosts):
2369
absent_ids = set(revision_ids.intersection(ghosts))
2370
# If all absent_ids are present in target, no error is needed.
2371
absent_ids.difference_update(
2372
set(target_graph.get_parent_map(absent_ids)))
2374
raise errors.NoSuchRevision(self.source, absent_ids.pop())
2375
# we don't care about other ghosts as we can't fetch them and
2376
# haven't been asked to.
2377
next_revs = set(next_revs)
2378
# we always have NULL_REVISION present.
2379
have_revs = set(target_graph.get_parent_map(next_revs)).union(null_set)
2380
missing_revs.update(next_revs - have_revs)
2381
searcher.stop_searching_any(have_revs)
2382
return searcher.get_result()
2384
@deprecated_method(one_two)
1569
2385
@needs_read_lock
1570
def missing_revision_ids(self, revision_id=None):
2386
def missing_revision_ids(self, revision_id=None, find_ghosts=True):
1571
2387
"""Return the revision ids that source has that target does not.
1573
2389
These are returned in topological order.
1575
2391
:param revision_id: only return revision ids included by this
2393
:param find_ghosts: If True find missing revisions in deep history
2394
rather than just finding the surface difference.
2396
return list(self.search_missing_revision_ids(
2397
revision_id, find_ghosts).get_keys())
2400
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
2401
"""Return the revision ids that source has that target does not.
2403
:param revision_id: only return revision ids included by this
2405
:param find_ghosts: If True find missing revisions in deep history
2406
rather than just finding the surface difference.
2407
:return: A bzrlib.graph.SearchResult.
2409
# stop searching at found target revisions.
2410
if not find_ghosts and revision_id is not None:
2411
return self._walk_to_common_revisions([revision_id])
1578
2412
# generic, possibly worst case, slow code path.
1579
2413
target_ids = set(self.target.all_revision_ids())
1580
2414
if revision_id is not None:
1581
2415
source_ids = self.source.get_ancestry(revision_id)
1582
assert source_ids[0] == None
2416
if source_ids[0] is not None:
2417
raise AssertionError()
1583
2418
source_ids.pop(0)
1585
2420
source_ids = self.source.all_revision_ids()
1586
2421
result_set = set(source_ids).difference(target_ids)
1587
# this may look like a no-op: its not. It preserves the ordering
1588
# other_ids had while only returning the members from other_ids
1589
# that we've decided we need.
1590
return [rev_id for rev_id in source_ids if rev_id in result_set]
1593
"""Release the locks on source and target."""
2422
return self.source.revision_ids_to_search_result(result_set)
2425
def _same_model(source, target):
2426
"""True if source and target have the same data representation."""
2427
if source.supports_rich_root() != target.supports_rich_root():
2429
if source._serializer != target._serializer:
2434
class InterSameDataRepository(InterRepository):
2435
"""Code for converting between repositories that represent the same data.
2437
Data format and model must match for this to work.
2441
def _get_repo_format_to_test(self):
2442
"""Repository format for testing with.
2444
InterSameData can pull from subtree to subtree and from non-subtree to
2445
non-subtree, so we test this with the richest repository format.
2447
from bzrlib.repofmt import knitrepo
2448
return knitrepo.RepositoryFormatKnit3()
2451
def is_compatible(source, target):
2452
return InterRepository._same_model(source, target)
2455
def copy_content(self, revision_id=None):
2456
"""Make a complete copy of the content in self into destination.
2458
This copies both the repository's revision data, and configuration information
2459
such as the make_working_trees setting.
2461
This is a destructive operation! Do not use it on existing
2464
:param revision_id: Only copy the content needed to construct
2465
revision_id and its parents.
1595
self.target.unlock()
1597
self.source.unlock()
1600
class InterWeaveRepo(InterRepository):
1601
"""Optimised code paths between Weave based repositories."""
1603
_matching_repo_format = RepositoryFormat7()
1604
"""Repository format for testing with."""
2468
self.target.set_make_working_trees(self.source.make_working_trees())
2469
except NotImplementedError:
2471
# but don't bother fetching if we have the needed data now.
2472
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2473
self.target.has_revision(revision_id)):
2475
self.target.fetch(self.source, revision_id=revision_id)
2478
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2479
"""See InterRepository.fetch()."""
2480
from bzrlib.fetch import GenericRepoFetcher
2481
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2482
self.source, self.source._format, self.target,
2483
self.target._format)
2484
f = GenericRepoFetcher(to_repository=self.target,
2485
from_repository=self.source,
2486
last_revision=revision_id,
2487
pb=pb, find_ghosts=find_ghosts)
2488
return f.count_copied, f.failed_revisions
2491
class InterWeaveRepo(InterSameDataRepository):
2492
"""Optimised code paths between Weave based repositories.
2494
This should be in bzrlib/repofmt/weaverepo.py but we have not yet
2495
implemented lazy inter-object optimisation.
2499
def _get_repo_format_to_test(self):
2500
from bzrlib.repofmt import weaverepo
2501
return weaverepo.RepositoryFormat7()
1607
2504
def is_compatible(source, target):
1744
2645
f = KnitRepoFetcher(to_repository=self.target,
1745
2646
from_repository=self.source,
1746
2647
last_revision=revision_id,
2648
pb=pb, find_ghosts=find_ghosts)
1748
2649
return f.count_copied, f.failed_revisions
1750
2651
@needs_read_lock
1751
def missing_revision_ids(self, revision_id=None):
2652
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
1752
2653
"""See InterRepository.missing_revision_ids()."""
1753
2654
if revision_id is not None:
1754
2655
source_ids = self.source.get_ancestry(revision_id)
1755
assert source_ids[0] == None
2656
if source_ids[0] is not None:
2657
raise AssertionError()
1756
2658
source_ids.pop(0)
1758
source_ids = self.source._all_possible_ids()
2660
source_ids = self.source.all_revision_ids()
1759
2661
source_ids_set = set(source_ids)
1760
2662
# source_ids is the worst possible case we may need to pull.
1761
2663
# now we want to filter source_ids against what we actually
1762
2664
# have in target, but don't try to check for existence where we know
1763
2665
# we do not have a revision as that would be pointless.
1764
target_ids = set(self.target._all_possible_ids())
2666
target_ids = set(self.target.all_revision_ids())
1765
2667
possibly_present_revisions = target_ids.intersection(source_ids_set)
1766
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2668
actually_present_revisions = set(
2669
self.target._eliminate_revisions_not_present(possibly_present_revisions))
1767
2670
required_revisions = source_ids_set.difference(actually_present_revisions)
1768
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1769
2671
if revision_id is not None:
1770
2672
# we used get_ancestry to determine source_ids then we are assured all
1771
2673
# revisions referenced are present as they are installed in topological order.
1772
2674
# and the tip revision was validated by get_ancestry.
1773
return required_topo_revisions
2675
result_set = required_revisions
1775
2677
# if we just grabbed the possibly available ids, then
1776
2678
# we only have an estimate of whats available and need to validate
1777
2679
# that against the revision records.
1778
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2681
self.source._eliminate_revisions_not_present(required_revisions))
2682
return self.source.revision_ids_to_search_result(result_set)
2685
class InterPackRepo(InterSameDataRepository):
2686
"""Optimised code paths between Pack based repositories."""
2689
def _get_repo_format_to_test(self):
2690
from bzrlib.repofmt import pack_repo
2691
return pack_repo.RepositoryFormatKnitPack1()
2694
def is_compatible(source, target):
2695
"""Be compatible with known Pack formats.
2697
We don't test for the stores being of specific types because that
2698
could lead to confusing results, and there is no need to be
2701
from bzrlib.repofmt.pack_repo import RepositoryFormatPack
2703
are_packs = (isinstance(source._format, RepositoryFormatPack) and
2704
isinstance(target._format, RepositoryFormatPack))
2705
except AttributeError:
2707
return are_packs and InterRepository._same_model(source, target)
2710
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2711
"""See InterRepository.fetch()."""
2712
if len(self.source._fallback_repositories) > 0:
2713
from bzrlib.fetch import KnitRepoFetcher
2714
fetcher = KnitRepoFetcher(self.target, self.source, revision_id,
2716
return fetcher.count_copied, fetcher.failed_revisions
2717
from bzrlib.repofmt.pack_repo import Packer
2718
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2719
self.source, self.source._format, self.target, self.target._format)
2720
self.count_copied = 0
2721
if revision_id is None:
2723
# everything to do - use pack logic
2724
# to fetch from all packs to one without
2725
# inventory parsing etc, IFF nothing to be copied is in the target.
2727
source_revision_ids = frozenset(self.source.all_revision_ids())
2728
revision_ids = source_revision_ids - \
2729
frozenset(self.target.get_parent_map(source_revision_ids))
2730
revision_keys = [(revid,) for revid in revision_ids]
2731
index = self.target._pack_collection.revision_index.combined_index
2732
present_revision_ids = set(item[1][0] for item in
2733
index.iter_entries(revision_keys))
2734
revision_ids = set(revision_ids) - present_revision_ids
2735
# implementing the TODO will involve:
2736
# - detecting when all of a pack is selected
2737
# - avoiding as much as possible pre-selection, so the
2738
# more-core routines such as create_pack_from_packs can filter in
2739
# a just-in-time fashion. (though having a HEADS list on a
2740
# repository might make this a lot easier, because we could
2741
# sensibly detect 'new revisions' without doing a full index scan.
2742
elif _mod_revision.is_null(revision_id):
2747
revision_ids = self.search_missing_revision_ids(revision_id,
2748
find_ghosts=find_ghosts).get_keys()
2749
except errors.NoSuchRevision:
2750
raise errors.InstallFailed([revision_id])
2751
if len(revision_ids) == 0:
2753
packs = self.source._pack_collection.all_packs()
2754
pack = Packer(self.target._pack_collection, packs, '.fetch',
2755
revision_ids).pack()
2756
if pack is not None:
2757
self.target._pack_collection._save_pack_names()
2758
# Trigger an autopack. This may duplicate effort as we've just done
2759
# a pack creation, but for now it is simpler to think about as
2760
# 'upload data, then repack if needed'.
2761
self.target._pack_collection.autopack()
2762
return (pack.get_revision_count(), [])
2767
def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
2768
"""See InterRepository.missing_revision_ids().
2770
:param find_ghosts: Find ghosts throughout the ancestry of
2773
if not find_ghosts and revision_id is not None:
2774
return self._walk_to_common_revisions([revision_id])
2775
elif revision_id is not None:
2776
# Find ghosts: search for revisions pointing from one repository to
2777
# the other, and vice versa, anywhere in the history of revision_id.
2778
graph = self.target.get_graph(other_repository=self.source)
2779
searcher = graph._make_breadth_first_searcher([revision_id])
2783
next_revs, ghosts = searcher.next_with_ghosts()
2784
except StopIteration:
2786
if revision_id in ghosts:
2787
raise errors.NoSuchRevision(self.source, revision_id)
2788
found_ids.update(next_revs)
2789
found_ids.update(ghosts)
2790
found_ids = frozenset(found_ids)
2791
# Double query here: should be able to avoid this by changing the
2792
# graph api further.
2793
result_set = found_ids - frozenset(
2794
self.target.get_parent_map(found_ids))
2796
source_ids = self.source.all_revision_ids()
2797
# source_ids is the worst possible case we may need to pull.
2798
# now we want to filter source_ids against what we actually
2799
# have in target, but don't try to check for existence where we know
2800
# we do not have a revision as that would be pointless.
2801
target_ids = set(self.target.all_revision_ids())
2802
result_set = set(source_ids).difference(target_ids)
2803
return self.source.revision_ids_to_search_result(result_set)
2806
class InterModel1and2(InterRepository):
2809
def _get_repo_format_to_test(self):
2813
def is_compatible(source, target):
2814
if not source.supports_rich_root() and target.supports_rich_root():
2820
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2821
"""See InterRepository.fetch()."""
2822
from bzrlib.fetch import Model1toKnit2Fetcher
2823
f = Model1toKnit2Fetcher(to_repository=self.target,
2824
from_repository=self.source,
2825
last_revision=revision_id,
2826
pb=pb, find_ghosts=find_ghosts)
2827
return f.count_copied, f.failed_revisions
2830
def copy_content(self, revision_id=None):
2831
"""Make a complete copy of the content in self into destination.
2833
This is a destructive operation! Do not use it on existing
2836
:param revision_id: Only copy the content needed to construct
2837
revision_id and its parents.
2840
self.target.set_make_working_trees(self.source.make_working_trees())
2841
except NotImplementedError:
2843
# but don't bother fetching if we have the needed data now.
2844
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2845
self.target.has_revision(revision_id)):
2847
self.target.fetch(self.source, revision_id=revision_id)
2850
class InterKnit1and2(InterKnitRepo):
2853
def _get_repo_format_to_test(self):
2857
def is_compatible(source, target):
2858
"""Be compatible with Knit1 source and Knit3 target"""
2859
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
2861
from bzrlib.repofmt.knitrepo import (RepositoryFormatKnit1,
2862
RepositoryFormatKnit3)
2863
from bzrlib.repofmt.pack_repo import (
2864
RepositoryFormatKnitPack1,
2865
RepositoryFormatKnitPack3,
2866
RepositoryFormatPackDevelopment0,
2867
RepositoryFormatPackDevelopment0Subtree,
2870
RepositoryFormatKnit1,
2871
RepositoryFormatKnitPack1,
2872
RepositoryFormatPackDevelopment0,
2875
RepositoryFormatKnit3,
2876
RepositoryFormatKnitPack3,
2877
RepositoryFormatPackDevelopment0Subtree,
2879
return (isinstance(source._format, nosubtrees) and
2880
isinstance(target._format, subtrees))
2881
except AttributeError:
2885
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2886
"""See InterRepository.fetch()."""
2887
from bzrlib.fetch import Knit1to2Fetcher
2888
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2889
self.source, self.source._format, self.target,
2890
self.target._format)
2891
f = Knit1to2Fetcher(to_repository=self.target,
2892
from_repository=self.source,
2893
last_revision=revision_id,
2894
pb=pb, find_ghosts=find_ghosts)
2895
return f.count_copied, f.failed_revisions
2898
class InterDifferingSerializer(InterKnitRepo):
2901
def _get_repo_format_to_test(self):
2905
def is_compatible(source, target):
2906
"""Be compatible with Knit2 source and Knit3 target"""
2907
if source.supports_rich_root() != target.supports_rich_root():
2909
# Ideally, we'd support fetching if the source had no tree references
2910
# even if it supported them...
2911
if (getattr(source, '_format.supports_tree_reference', False) and
2912
not getattr(target, '_format.supports_tree_reference', False)):
2917
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2918
"""See InterRepository.fetch()."""
2919
revision_ids = self.target.search_missing_revision_ids(self.source,
2920
revision_id, find_ghosts=find_ghosts).get_keys()
2921
revision_ids = tsort.topo_sort(
2922
self.source.get_graph().get_parent_map(revision_ids))
2923
def revisions_iterator():
2924
for current_revision_id in revision_ids:
2925
revision = self.source.get_revision(current_revision_id)
2926
tree = self.source.revision_tree(current_revision_id)
2928
signature = self.source.get_signature_text(
2929
current_revision_id)
2930
except errors.NoSuchRevision:
2932
yield revision, tree, signature
2934
my_pb = ui.ui_factory.nested_progress_bar()
2939
install_revisions(self.target, revisions_iterator(),
2940
len(revision_ids), pb)
2942
if my_pb is not None:
2944
return len(revision_ids), 0
2947
class InterOtherToRemote(InterRepository):
2949
def __init__(self, source, target):
2950
InterRepository.__init__(self, source, target)
2951
self._real_inter = None
2954
def is_compatible(source, target):
2955
if isinstance(target, remote.RemoteRepository):
2959
def _ensure_real_inter(self):
2960
if self._real_inter is None:
2961
self.target._ensure_real()
2962
real_target = self.target._real_repository
2963
self._real_inter = InterRepository.get(self.source, real_target)
2965
def copy_content(self, revision_id=None):
2966
self._ensure_real_inter()
2967
self._real_inter.copy_content(revision_id=revision_id)
2969
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
2970
self._ensure_real_inter()
2971
return self._real_inter.fetch(revision_id=revision_id, pb=pb,
2972
find_ghosts=find_ghosts)
2975
def _get_repo_format_to_test(self):
2979
class InterRemoteToOther(InterRepository):
2981
def __init__(self, source, target):
2982
InterRepository.__init__(self, source, target)
2983
self._real_inter = None
2986
def is_compatible(source, target):
2987
if not isinstance(source, remote.RemoteRepository):
2989
# Is source's model compatible with target's model?
2990
source._ensure_real()
2991
real_source = source._real_repository
2992
if isinstance(real_source, remote.RemoteRepository):
2993
raise NotImplementedError(
2994
"We don't support remote repos backed by remote repos yet.")
2995
return InterRepository._same_model(real_source, target)
2997
def _ensure_real_inter(self):
2998
if self._real_inter is None:
2999
self.source._ensure_real()
3000
real_source = self.source._real_repository
3001
self._real_inter = InterRepository.get(real_source, self.target)
3003
def fetch(self, revision_id=None, pb=None, find_ghosts=False):
3004
self._ensure_real_inter()
3005
return self._real_inter.fetch(revision_id=revision_id, pb=pb,
3006
find_ghosts=find_ghosts)
3008
def copy_content(self, revision_id=None):
3009
self._ensure_real_inter()
3010
self._real_inter.copy_content(revision_id=revision_id)
3013
def _get_repo_format_to_test(self):
3018
InterRepository.register_optimiser(InterDifferingSerializer)
3019
InterRepository.register_optimiser(InterSameDataRepository)
1780
3020
InterRepository.register_optimiser(InterWeaveRepo)
1781
3021
InterRepository.register_optimiser(InterKnitRepo)
1784
class RepositoryTestProviderAdapter(object):
1785
"""A tool to generate a suite testing multiple repository formats at once.
1787
This is done by copying the test once for each transport and injecting
1788
the transport_server, transport_readonly_server, and bzrdir_format and
1789
repository_format classes into each copy. Each copy is also given a new id()
1790
to make it easy to identify.
1793
def __init__(self, transport_server, transport_readonly_server, formats):
1794
self._transport_server = transport_server
1795
self._transport_readonly_server = transport_readonly_server
1796
self._formats = formats
1798
def adapt(self, test):
1799
result = TestSuite()
1800
for repository_format, bzrdir_format in self._formats:
1801
new_test = deepcopy(test)
1802
new_test.transport_server = self._transport_server
1803
new_test.transport_readonly_server = self._transport_readonly_server
1804
new_test.bzrdir_format = bzrdir_format
1805
new_test.repository_format = repository_format
1806
def make_new_test_id():
1807
new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1808
return lambda: new_id
1809
new_test.id = make_new_test_id()
1810
result.addTest(new_test)
1814
class InterRepositoryTestProviderAdapter(object):
1815
"""A tool to generate a suite testing multiple inter repository formats.
1817
This is done by copying the test once for each interrepo provider and injecting
1818
the transport_server, transport_readonly_server, repository_format and
1819
repository_to_format classes into each copy.
1820
Each copy is also given a new id() to make it easy to identify.
1823
def __init__(self, transport_server, transport_readonly_server, formats):
1824
self._transport_server = transport_server
1825
self._transport_readonly_server = transport_readonly_server
1826
self._formats = formats
1828
def adapt(self, test):
1829
result = TestSuite()
1830
for interrepo_class, repository_format, repository_format_to in self._formats:
1831
new_test = deepcopy(test)
1832
new_test.transport_server = self._transport_server
1833
new_test.transport_readonly_server = self._transport_readonly_server
1834
new_test.interrepo_class = interrepo_class
1835
new_test.repository_format = repository_format
1836
new_test.repository_format_to = repository_format_to
1837
def make_new_test_id():
1838
new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1839
return lambda: new_id
1840
new_test.id = make_new_test_id()
1841
result.addTest(new_test)
1845
def default_test_list():
1846
"""Generate the default list of interrepo permutations to test."""
1848
# test the default InterRepository between format 6 and the current
1850
# XXX: robertc 20060220 reinstate this when there are two supported
1851
# formats which do not have an optimal code path between them.
1852
result.append((InterRepository,
1853
RepositoryFormat6(),
1854
RepositoryFormatKnit1()))
1855
for optimiser in InterRepository._optimisers:
1856
result.append((optimiser,
1857
optimiser._matching_repo_format,
1858
optimiser._matching_repo_format
1860
# if there are specific combinations we want to use, we can add them
3022
InterRepository.register_optimiser(InterModel1and2)
3023
InterRepository.register_optimiser(InterKnit1and2)
3024
InterRepository.register_optimiser(InterPackRepo)
3025
InterRepository.register_optimiser(InterOtherToRemote)
3026
InterRepository.register_optimiser(InterRemoteToOther)
1865
3029
class CopyConverter(object):
1913
3078
self.pb.update(message, self.count, self.total)
1916
class CommitBuilder(object):
1917
"""Provides an interface to build up a commit.
1919
This allows describing a tree to be committed without needing to
1920
know the internals of the format of the repository.
1922
def __init__(self, repository, parents, config, timestamp=None,
1923
timezone=None, committer=None, revprops=None,
1925
"""Initiate a CommitBuilder.
1927
:param repository: Repository to commit to.
1928
:param parents: Revision ids of the parents of the new revision.
1929
:param config: Configuration to use.
1930
:param timestamp: Optional timestamp recorded for commit.
1931
:param timezone: Optional timezone for timestamp.
1932
:param committer: Optional committer to set for commit.
1933
:param revprops: Optional dictionary of revision properties.
1934
:param revision_id: Optional revision id.
1936
self._config = config
1938
if committer is None:
1939
self._committer = self._config.username()
1941
assert isinstance(committer, basestring), type(committer)
1942
self._committer = committer
1944
self.new_inventory = Inventory()
1945
self._new_revision_id = revision_id
1946
self.parents = parents
1947
self.repository = repository
1950
if revprops is not None:
1951
self._revprops.update(revprops)
1953
if timestamp is None:
1954
self._timestamp = time.time()
1956
self._timestamp = long(timestamp)
1958
if timezone is None:
1959
self._timezone = local_time_offset()
1961
self._timezone = int(timezone)
1963
self._generate_revision_if_needed()
1965
def commit(self, message):
1966
"""Make the actual commit.
1968
:return: The revision id of the recorded revision.
1970
rev = Revision(timestamp=self._timestamp,
1971
timezone=self._timezone,
1972
committer=self._committer,
1974
inventory_sha1=self.inv_sha1,
1975
revision_id=self._new_revision_id,
1976
properties=self._revprops)
1977
rev.parent_ids = self.parents
1978
self.repository.add_revision(self._new_revision_id, rev,
1979
self.new_inventory, self._config)
1980
return self._new_revision_id
1982
def finish_inventory(self):
1983
"""Tell the builder that the inventory is finished."""
1984
self.new_inventory.revision_id = self._new_revision_id
1985
self.inv_sha1 = self.repository.add_inventory(
1986
self._new_revision_id,
1991
def _gen_revision_id(self):
1992
"""Return new revision-id."""
1993
s = '%s-%s-' % (self._config.user_email(),
1994
compact_date(self._timestamp))
1995
s += hexlify(rand_bytes(8))
1998
def _generate_revision_if_needed(self):
1999
"""Create a revision id if None was supplied.
2001
If the repository can not support user-specified revision ids
2002
they should override this function and raise UnsupportedOperation
2003
if _new_revision_id is not None.
2005
:raises: UnsupportedOperation
2007
if self._new_revision_id is None:
2008
self._new_revision_id = self._gen_revision_id()
2010
def record_entry_contents(self, ie, parent_invs, path, tree):
2011
"""Record the content of ie from tree into the commit if needed.
2013
:param ie: An inventory entry present in the commit.
2014
:param parent_invs: The inventories of the parent revisions of the
2016
:param path: The path the entry is at in the tree.
2017
:param tree: The tree which contains this entry and should be used to
2020
self.new_inventory.add(ie)
2022
# ie.revision is always None if the InventoryEntry is considered
2023
# for committing. ie.snapshot will record the correct revision
2024
# which may be the sole parent if it is untouched.
2025
if ie.revision is not None:
2027
previous_entries = ie.find_previous_heads(
2029
self.repository.weave_store,
2030
self.repository.get_transaction())
2031
# we are creating a new revision for ie in the history store
2033
ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2035
def modified_directory(self, file_id, file_parents):
2036
"""Record the presence of a symbolic link.
2038
:param file_id: The file_id of the link to record.
2039
:param file_parents: The per-file parent revision ids.
2041
self._add_text_to_weave(file_id, [], file_parents.keys())
2043
def modified_file_text(self, file_id, file_parents,
2044
get_content_byte_lines, text_sha1=None,
2046
"""Record the text of file file_id
2048
:param file_id: The file_id of the file to record the text of.
2049
:param file_parents: The per-file parent revision ids.
2050
:param get_content_byte_lines: A callable which will return the byte
2052
:param text_sha1: Optional SHA1 of the file contents.
2053
:param text_size: Optional size of the file contents.
2055
mutter('storing text of file {%s} in revision {%s} into %r',
2056
file_id, self._new_revision_id, self.repository.weave_store)
2057
# special case to avoid diffing on renames or
2059
if (len(file_parents) == 1
2060
and text_sha1 == file_parents.values()[0].text_sha1
2061
and text_size == file_parents.values()[0].text_size):
2062
previous_ie = file_parents.values()[0]
2063
versionedfile = self.repository.weave_store.get_weave(file_id,
2064
self.repository.get_transaction())
2065
versionedfile.clone_text(self._new_revision_id,
2066
previous_ie.revision, file_parents.keys())
2067
return text_sha1, text_size
2069
new_lines = get_content_byte_lines()
2070
# TODO: Rather than invoking sha_strings here, _add_text_to_weave
2071
# should return the SHA1 and size
2072
self._add_text_to_weave(file_id, new_lines, file_parents.keys())
2073
return bzrlib.osutils.sha_strings(new_lines), \
2074
sum(map(len, new_lines))
2076
def modified_link(self, file_id, file_parents, link_target):
2077
"""Record the presence of a symbolic link.
2079
:param file_id: The file_id of the link to record.
2080
:param file_parents: The per-file parent revision ids.
2081
:param link_target: Target location of this link.
2083
self._add_text_to_weave(file_id, [], file_parents.keys())
2085
def _add_text_to_weave(self, file_id, new_lines, parents):
2086
versionedfile = self.repository.weave_store.get_weave_or_empty(
2087
file_id, self.repository.get_transaction())
2088
versionedfile.add_lines(self._new_revision_id, parents, new_lines)
2089
versionedfile.clear_cache()
2092
# Copied from xml.sax.saxutils
3090
def _unescaper(match, _map=_unescape_map):
3091
code = match.group(1)
3095
if not code.startswith('#'):
3097
return unichr(int(code[1:])).encode('utf8')
2093
3103
def _unescape_xml(data):
2094
"""Unescape &, <, and > in a string of data.
2096
data = data.replace("<", "<")
2097
data = data.replace(">", ">")
2098
# must do ampersand last
2099
return data.replace("&", "&")
3104
"""Unescape predefined XML entities in a string of data."""
3106
if _unescape_re is None:
3107
_unescape_re = re.compile('\&([^;]*);')
3108
return _unescape_re.sub(_unescaper, data)
3111
class _VersionedFileChecker(object):
3113
def __init__(self, repository):
3114
self.repository = repository
3115
self.text_index = self.repository._generate_text_key_index()
3117
def calculate_file_version_parents(self, text_key):
3118
"""Calculate the correct parents for a file version according to
3121
parent_keys = self.text_index[text_key]
3122
if parent_keys == [_mod_revision.NULL_REVISION]:
3124
return tuple(parent_keys)
3126
def check_file_version_parents(self, texts, progress_bar=None):
3127
"""Check the parents stored in a versioned file are correct.
3129
It also detects file versions that are not referenced by their
3130
corresponding revision's inventory.
3132
:returns: A tuple of (wrong_parents, dangling_file_versions).
3133
wrong_parents is a dict mapping {revision_id: (stored_parents,
3134
correct_parents)} for each revision_id where the stored parents
3135
are not correct. dangling_file_versions is a set of (file_id,
3136
revision_id) tuples for versions that are present in this versioned
3137
file, but not used by the corresponding inventory.
3140
self.file_ids = set([file_id for file_id, _ in
3141
self.text_index.iterkeys()])
3142
# text keys is now grouped by file_id
3143
n_weaves = len(self.file_ids)
3144
files_in_revisions = {}
3145
revisions_of_files = {}
3146
n_versions = len(self.text_index)
3147
progress_bar.update('loading text store', 0, n_versions)
3148
parent_map = self.repository.texts.get_parent_map(self.text_index)
3149
# On unlistable transports this could well be empty/error...
3150
text_keys = self.repository.texts.keys()
3151
unused_keys = frozenset(text_keys) - set(self.text_index)
3152
for num, key in enumerate(self.text_index.iterkeys()):
3153
if progress_bar is not None:
3154
progress_bar.update('checking text graph', num, n_versions)
3155
correct_parents = self.calculate_file_version_parents(key)
3157
knit_parents = parent_map[key]
3158
except errors.RevisionNotPresent:
3161
if correct_parents != knit_parents:
3162
wrong_parents[key] = (knit_parents, correct_parents)
3163
return wrong_parents, unused_keys
3166
def _old_get_graph(repository, revision_id):
3167
"""DO NOT USE. That is all. I'm serious."""
3168
graph = repository.get_graph()
3169
revision_graph = dict(((key, value) for key, value in
3170
graph.iter_ancestry([revision_id]) if value is not None))
3171
return _strip_NULL_ghosts(revision_graph)
3174
def _strip_NULL_ghosts(revision_graph):
3175
"""Also don't use this. more compatibility code for unmigrated clients."""
3176
# Filter ghosts, and null:
3177
if _mod_revision.NULL_REVISION in revision_graph:
3178
del revision_graph[_mod_revision.NULL_REVISION]
3179
for key, parents in revision_graph.items():
3180
revision_graph[key] = tuple(parent for parent in parents if parent
3182
return revision_graph