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
17
19
from cStringIO import StringIO
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
22
from unittest import TestSuite
39
revision as _mod_revision,
44
from bzrlib.bundle import serializer
24
from bzrlib import bzrdir, check, delta, gpg, errors, xml5, ui, transactions, osutils
25
from bzrlib.decorators import needs_read_lock, needs_write_lock
26
from bzrlib.errors import InvalidRevisionId
27
from bzrlib.graph import Graph
28
from bzrlib.inter import InterObject
29
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
30
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
31
from bzrlib.lockable_files import LockableFiles, TransportLock
32
from bzrlib.lockdir import LockDir
33
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date,
35
from bzrlib.revision import NULL_REVISION, Revision
45
36
from bzrlib.revisiontree import RevisionTree
46
from bzrlib.store.versioned import VersionedFileStore
37
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
47
38
from bzrlib.store.text import TextStore
39
from bzrlib import symbol_versioning
40
from bzrlib.symbol_versioning import (deprecated_method,
48
43
from bzrlib.testament import Testament
49
from bzrlib.util import bencode
52
from bzrlib.decorators import needs_read_lock, needs_write_lock
53
from bzrlib.inter import InterObject
54
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
55
from bzrlib.symbol_versioning import (
58
from bzrlib.trace import mutter, mutter_callsite, note, warning
44
from bzrlib.trace import mutter, note, warning
45
from bzrlib.tsort import topo_sort
46
from bzrlib.weave import WeaveFile
61
49
# Old formats display a warning, but only once
62
50
_deprecation_warning_done = False
65
class CommitBuilder(object):
66
"""Provides an interface to build up a commit.
68
This allows describing a tree to be committed without needing to
69
know the internals of the format of the repository.
72
# all clients should supply tree roots.
73
record_root_entry = True
74
# the default CommitBuilder does not manage trees whose root is versioned.
75
_versioned_root = False
77
def __init__(self, repository, parents, config, timestamp=None,
78
timezone=None, committer=None, revprops=None,
80
"""Initiate a CommitBuilder.
82
:param repository: Repository to commit to.
83
:param parents: Revision ids of the parents of the new revision.
84
:param config: Configuration to use.
85
:param timestamp: Optional timestamp recorded for commit.
86
:param timezone: Optional timezone for timestamp.
87
:param committer: Optional committer to set for commit.
88
:param revprops: Optional dictionary of revision properties.
89
:param revision_id: Optional revision id.
94
self._committer = self._config.username()
96
assert isinstance(committer, basestring), type(committer)
97
self._committer = committer
99
self.new_inventory = Inventory(None)
100
self._new_revision_id = revision_id
101
self.parents = parents
102
self.repository = repository
105
if revprops is not None:
106
self._revprops.update(revprops)
108
if timestamp is None:
109
timestamp = time.time()
110
# Restrict resolution to 1ms
111
self._timestamp = round(timestamp, 3)
114
self._timezone = osutils.local_time_offset()
116
self._timezone = int(timezone)
118
self._generate_revision_if_needed()
119
self._heads = graph.HeadsCache(repository.get_graph()).heads
121
def commit(self, message):
122
"""Make the actual commit.
124
:return: The revision id of the recorded revision.
126
rev = _mod_revision.Revision(
127
timestamp=self._timestamp,
128
timezone=self._timezone,
129
committer=self._committer,
131
inventory_sha1=self.inv_sha1,
132
revision_id=self._new_revision_id,
133
properties=self._revprops)
134
rev.parent_ids = self.parents
135
self.repository.add_revision(self._new_revision_id, rev,
136
self.new_inventory, self._config)
137
self.repository.commit_write_group()
138
return self._new_revision_id
141
"""Abort the commit that is being built.
143
self.repository.abort_write_group()
145
def revision_tree(self):
146
"""Return the tree that was just committed.
148
After calling commit() this can be called to get a RevisionTree
149
representing the newly committed tree. This is preferred to
150
calling Repository.revision_tree() because that may require
151
deserializing the inventory, while we already have a copy in
154
return RevisionTree(self.repository, self.new_inventory,
155
self._new_revision_id)
157
def finish_inventory(self):
158
"""Tell the builder that the inventory is finished."""
159
if self.new_inventory.root is None:
160
raise AssertionError('Root entry should be supplied to'
161
' record_entry_contents, as of bzr 0.10.',
162
DeprecationWarning, stacklevel=2)
163
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
164
self.new_inventory.revision_id = self._new_revision_id
165
self.inv_sha1 = self.repository.add_inventory(
166
self._new_revision_id,
171
def _gen_revision_id(self):
172
"""Return new revision-id."""
173
return generate_ids.gen_revision_id(self._config.username(),
176
def _generate_revision_if_needed(self):
177
"""Create a revision id if None was supplied.
179
If the repository can not support user-specified revision ids
180
they should override this function and raise CannotSetRevisionId
181
if _new_revision_id is not None.
183
:raises: CannotSetRevisionId
185
if self._new_revision_id is None:
186
self._new_revision_id = self._gen_revision_id()
187
self.random_revid = True
189
self.random_revid = False
191
def _check_root(self, ie, parent_invs, tree):
192
"""Helper for record_entry_contents.
194
:param ie: An entry being added.
195
:param parent_invs: The inventories of the parent revisions of the
197
:param tree: The tree that is being committed.
199
# In this revision format, root entries have no knit or weave When
200
# serializing out to disk and back in root.revision is always
202
ie.revision = self._new_revision_id
204
def _get_delta(self, ie, basis_inv, path):
205
"""Get a delta against the basis inventory for ie."""
206
if ie.file_id not in basis_inv:
208
return (None, path, ie.file_id, ie)
209
elif ie != basis_inv[ie.file_id]:
211
# TODO: avoid tis id2path call.
212
return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
217
def record_entry_contents(self, ie, parent_invs, path, tree,
219
"""Record the content of ie from tree into the commit if needed.
221
Side effect: sets ie.revision when unchanged
223
:param ie: An inventory entry present in the commit.
224
:param parent_invs: The inventories of the parent revisions of the
226
:param path: The path the entry is at in the tree.
227
:param tree: The tree which contains this entry and should be used to
229
:param content_summary: Summary data from the tree about the paths
230
content - stat, length, exec, sha/link target. This is only
231
accessed when the entry has a revision of None - that is when it is
232
a candidate to commit.
233
:return: A tuple (change_delta, version_recorded). change_delta is
234
an inventory_delta change for this entry against the basis tree of
235
the commit, or None if no change occured against the basis tree.
236
version_recorded is True if a new version of the entry has been
237
recorded. For instance, committing a merge where a file was only
238
changed on the other side will return (delta, False).
240
if self.new_inventory.root is None:
241
if ie.parent_id is not None:
242
raise errors.RootMissing()
243
self._check_root(ie, parent_invs, tree)
244
if ie.revision is None:
245
kind = content_summary[0]
247
# ie is carried over from a prior commit
249
# XXX: repository specific check for nested tree support goes here - if
250
# the repo doesn't want nested trees we skip it ?
251
if (kind == 'tree-reference' and
252
not self.repository._format.supports_tree_reference):
253
# mismatch between commit builder logic and repository:
254
# this needs the entry creation pushed down into the builder.
255
raise NotImplementedError('Missing repository subtree support.')
256
self.new_inventory.add(ie)
258
# TODO: slow, take it out of the inner loop.
260
basis_inv = parent_invs[0]
262
basis_inv = Inventory(root_id=None)
264
# ie.revision is always None if the InventoryEntry is considered
265
# for committing. We may record the previous parents revision if the
266
# content is actually unchanged against a sole head.
267
if ie.revision is not None:
268
if not self._versioned_root and path == '':
269
# repositories that do not version the root set the root's
270
# revision to the new commit even when no change occurs, and
271
# this masks when a change may have occurred against the basis,
272
# so calculate if one happened.
273
if ie.file_id in basis_inv:
274
delta = (basis_inv.id2path(ie.file_id), path,
278
delta = (None, path, ie.file_id, ie)
281
# we don't need to commit this, because the caller already
282
# determined that an existing revision of this file is
284
return None, (ie.revision == self._new_revision_id)
285
# XXX: Friction: parent_candidates should return a list not a dict
286
# so that we don't have to walk the inventories again.
287
parent_candiate_entries = ie.parent_candidates(parent_invs)
288
head_set = self._heads(parent_candiate_entries.keys())
290
for inv in parent_invs:
291
if ie.file_id in inv:
292
old_rev = inv[ie.file_id].revision
293
if old_rev in head_set:
294
heads.append(inv[ie.file_id].revision)
295
head_set.remove(inv[ie.file_id].revision)
298
# now we check to see if we need to write a new record to the
300
# We write a new entry unless there is one head to the ancestors, and
301
# the kind-derived content is unchanged.
303
# Cheapest check first: no ancestors, or more the one head in the
304
# ancestors, we write a new node.
308
# There is a single head, look it up for comparison
309
parent_entry = parent_candiate_entries[heads[0]]
310
# if the non-content specific data has changed, we'll be writing a
312
if (parent_entry.parent_id != ie.parent_id or
313
parent_entry.name != ie.name):
315
# now we need to do content specific checks:
317
# if the kind changed the content obviously has
318
if kind != parent_entry.kind:
321
assert content_summary[2] is not None, \
322
"Files must not have executable = None"
324
if (# if the file length changed we have to store:
325
parent_entry.text_size != content_summary[1] or
326
# if the exec bit has changed we have to store:
327
parent_entry.executable != content_summary[2]):
329
elif parent_entry.text_sha1 == content_summary[3]:
330
# all meta and content is unchanged (using a hash cache
331
# hit to check the sha)
332
ie.revision = parent_entry.revision
333
ie.text_size = parent_entry.text_size
334
ie.text_sha1 = parent_entry.text_sha1
335
ie.executable = parent_entry.executable
336
return self._get_delta(ie, basis_inv, path), False
338
# Either there is only a hash change(no hash cache entry,
339
# or same size content change), or there is no change on
341
# Provide the parent's hash to the store layer, so that the
342
# content is unchanged we will not store a new node.
343
nostore_sha = parent_entry.text_sha1
345
# We want to record a new node regardless of the presence or
346
# absence of a content change in the file.
348
ie.executable = content_summary[2]
349
lines = tree.get_file(ie.file_id, path).readlines()
351
ie.text_sha1, ie.text_size = self._add_text_to_weave(
352
ie.file_id, lines, heads, nostore_sha)
353
except errors.ExistingContent:
354
# Turns out that the file content was unchanged, and we were
355
# only going to store a new node if it was changed. Carry over
357
ie.revision = parent_entry.revision
358
ie.text_size = parent_entry.text_size
359
ie.text_sha1 = parent_entry.text_sha1
360
ie.executable = parent_entry.executable
361
return self._get_delta(ie, basis_inv, path), False
362
elif kind == 'directory':
364
# all data is meta here, nothing specific to directory, so
366
ie.revision = parent_entry.revision
367
return self._get_delta(ie, basis_inv, path), False
369
self._add_text_to_weave(ie.file_id, lines, heads, None)
370
elif kind == 'symlink':
371
current_link_target = content_summary[3]
373
# symlink target is not generic metadata, check if it has
375
if current_link_target != parent_entry.symlink_target:
378
# unchanged, carry over.
379
ie.revision = parent_entry.revision
380
ie.symlink_target = parent_entry.symlink_target
381
return self._get_delta(ie, basis_inv, path), False
382
ie.symlink_target = current_link_target
384
self._add_text_to_weave(ie.file_id, lines, heads, None)
385
elif kind == 'tree-reference':
387
if content_summary[3] != parent_entry.reference_revision:
390
# unchanged, carry over.
391
ie.reference_revision = parent_entry.reference_revision
392
ie.revision = parent_entry.revision
393
return self._get_delta(ie, basis_inv, path), False
394
ie.reference_revision = content_summary[3]
396
self._add_text_to_weave(ie.file_id, lines, heads, None)
398
raise NotImplementedError('unknown kind')
399
ie.revision = self._new_revision_id
400
return self._get_delta(ie, basis_inv, path), True
402
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
403
versionedfile = self.repository.weave_store.get_weave_or_empty(
404
file_id, self.repository.get_transaction())
405
# Don't change this to add_lines - add_lines_with_ghosts is cheaper
406
# than add_lines, and allows committing when a parent is ghosted for
408
# Note: as we read the content directly from the tree, we know its not
409
# been turned into unicode or badly split - but a broken tree
410
# implementation could give us bad output from readlines() so this is
411
# not a guarantee of safety. What would be better is always checking
412
# the content during test suite execution. RBC 20070912
414
return versionedfile.add_lines_with_ghosts(
415
self._new_revision_id, parents, new_lines,
416
nostore_sha=nostore_sha, random_id=self.random_revid,
417
check_content=False)[0:2]
419
versionedfile.clear_cache()
422
class RootCommitBuilder(CommitBuilder):
423
"""This commitbuilder actually records the root id"""
425
# the root entry gets versioned properly by this builder.
426
_versioned_root = True
428
def _check_root(self, ie, parent_invs, tree):
429
"""Helper for record_entry_contents.
431
:param ie: An entry being added.
432
:param parent_invs: The inventories of the parent revisions of the
434
:param tree: The tree that is being committed.
438
######################################################################
441
53
class Repository(object):
442
54
"""Repository holding history for one or more branches.
453
# What class to use for a CommitBuilder. Often its simpler to change this
454
# in a Repository class subclass rather than to override
455
# get_commit_builder.
456
_commit_builder_class = CommitBuilder
457
# The search regex used by xml based repositories to determine what things
458
# where changed in a single commit.
459
_file_ids_altered_regex = lazy_regex.lazy_compile(
460
r'file_id="(?P<file_id>[^"]+)"'
461
r'.* revision="(?P<revision_id>[^"]+)"'
464
def abort_write_group(self):
465
"""Commit the contents accrued within the current write group.
467
:seealso: start_write_group.
469
if self._write_group is not self.get_transaction():
470
# has an unlock or relock occured ?
471
raise errors.BzrError('mismatched lock context and write group.')
472
self._abort_write_group()
473
self._write_group = None
475
def _abort_write_group(self):
476
"""Template method for per-repository write group cleanup.
478
This is called during abort before the write group is considered to be
479
finished and should cleanup any internal state accrued during the write
480
group. There is no requirement that data handed to the repository be
481
*not* made available - this is not a rollback - but neither should any
482
attempt be made to ensure that data added is fully commited. Abort is
483
invoked when an error has occured so futher disk or network operations
484
may not be possible or may error and if possible should not be
489
def add_inventory(self, revision_id, inv, parents):
490
"""Add the inventory inv to the repository as revision_id.
66
def add_inventory(self, revid, inv, parents):
67
"""Add the inventory inv to the repository as revid.
492
:param parents: The revision ids of the parents that revision_id
69
:param parents: The revision ids of the parents that revid
493
70
is known to have and are in the repository already.
495
72
returns the sha1 of the serialized inventory.
497
assert self.is_in_write_group()
498
_mod_revision.check_not_reserved_id(revision_id)
499
assert inv.revision_id is None or inv.revision_id == revision_id, \
74
assert inv.revision_id is None or inv.revision_id == revid, \
500
75
"Mismatch between inventory revision" \
501
" id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
76
" id and insertion revid (%r, %r)" % (inv.revision_id, revid)
502
77
assert inv.root is not None
503
inv_lines = self._serialise_inventory_to_lines(inv)
504
inv_vf = self.get_inventory_weave()
505
return self._inventory_add_lines(inv_vf, revision_id, parents,
506
inv_lines, check_content=False)
78
inv_text = xml5.serializer_v5.write_inventory_to_string(inv)
79
inv_sha1 = osutils.sha_string(inv_text)
80
inv_vf = self.control_weaves.get_weave('inventory',
81
self.get_transaction())
82
self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
508
def _inventory_add_lines(self, inv_vf, revision_id, parents, lines,
510
"""Store lines in inv_vf and return the sha1 of the inventory."""
85
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
511
86
final_parents = []
512
87
for parent in parents:
513
88
if parent in inv_vf:
514
89
final_parents.append(parent)
515
return inv_vf.add_lines(revision_id, final_parents, lines,
516
check_content=check_content)[0]
91
inv_vf.add_lines(revid, final_parents, lines)
519
def add_revision(self, revision_id, rev, inv=None, config=None):
520
"""Add rev to the revision store as revision_id.
94
def add_revision(self, rev_id, rev, inv=None, config=None):
95
"""Add rev to the revision store as rev_id.
522
:param revision_id: the revision id to use.
97
:param rev_id: the revision id to use.
523
98
:param rev: The revision object.
524
99
:param inv: The inventory for the revision. if None, it will be looked
525
100
up in the inventory storer
623
195
# TODO: make sure to construct the right store classes, etc, depending
624
196
# on whether escaping is required.
625
197
self._warn_if_deprecated()
626
self._write_group = None
627
self.base = control_files._transport.base
629
199
def __repr__(self):
630
return '%s(%r)' % (self.__class__.__name__,
633
def has_same_location(self, other):
634
"""Returns a boolean indicating if this repository is at the same
635
location as another repository.
637
This might return False even when two repository objects are accessing
638
the same physical repository via different URLs.
640
if self.__class__ is not other.__class__:
642
return (self.control_files._transport.base ==
643
other.control_files._transport.base)
645
def is_in_write_group(self):
646
"""Return True if there is an open write group.
648
:seealso: start_write_group.
650
return self._write_group is not None
200
return '%s(%r)' % (self.__class__.__name__,
201
self.bzrdir.transport.base)
652
203
def is_locked(self):
653
204
return self.control_files.is_locked()
655
def is_write_locked(self):
656
"""Return True if this object is write locked."""
657
return self.is_locked() and self.control_files._lock_mode == 'w'
659
def lock_write(self, token=None):
660
"""Lock this repository for writing.
662
This causes caching within the repository obejct to start accumlating
663
data during reads, and allows a 'write_group' to be obtained. Write
664
groups must be used for actual data insertion.
666
:param token: if this is already locked, then lock_write will fail
667
unless the token matches the existing lock.
668
:returns: a token if this instance supports tokens, otherwise None.
669
:raises TokenLockingNotSupported: when a token is given but this
670
instance doesn't support using token locks.
671
:raises MismatchedToken: if the specified token doesn't match the token
672
of the existing lock.
673
:seealso: start_write_group.
675
A token should be passed in if you know that you have locked the object
676
some other way, and need to synchronise this object's state with that
679
XXX: this docstring is duplicated in many places, e.g. lockable_files.py
681
result = self.control_files.lock_write(token=token)
206
def lock_write(self):
207
self.control_files.lock_write()
685
209
def lock_read(self):
686
210
self.control_files.lock_read()
689
212
def get_physical_lock_status(self):
690
213
return self.control_files.get_physical_lock_status()
692
def leave_lock_in_place(self):
693
"""Tell this repository not to release the physical lock when this
696
If lock_write doesn't return a token, then this method is not supported.
698
self.control_files.leave_in_place()
700
def dont_leave_lock_in_place(self):
701
"""Tell this repository to release the physical lock when this
702
object is unlocked, even if it didn't originally acquire it.
704
If lock_write doesn't return a token, then this method is not supported.
706
self.control_files.dont_leave_in_place()
709
def gather_stats(self, revid=None, committers=None):
710
"""Gather statistics from a revision id.
712
:param revid: The revision id to gather statistics from, if None, then
713
no revision specific statistics are gathered.
714
:param committers: Optional parameter controlling whether to grab
715
a count of committers from the revision specific statistics.
716
:return: A dictionary of statistics. Currently this contains:
717
committers: The number of committers if requested.
718
firstrev: A tuple with timestamp, timezone for the penultimate left
719
most ancestor of revid, if revid is not the NULL_REVISION.
720
latestrev: A tuple with timestamp, timezone for revid, if revid is
721
not the NULL_REVISION.
722
revisions: The total revision count in the repository.
723
size: An estimate disk size of the repository in bytes.
726
if revid and committers:
727
result['committers'] = 0
728
if revid and revid != _mod_revision.NULL_REVISION:
730
all_committers = set()
731
revisions = self.get_ancestry(revid)
732
# pop the leading None
734
first_revision = None
736
# ignore the revisions in the middle - just grab first and last
737
revisions = revisions[0], revisions[-1]
738
for revision in self.get_revisions(revisions):
739
if not first_revision:
740
first_revision = revision
742
all_committers.add(revision.committer)
743
last_revision = revision
745
result['committers'] = len(all_committers)
746
result['firstrev'] = (first_revision.timestamp,
747
first_revision.timezone)
748
result['latestrev'] = (last_revision.timestamp,
749
last_revision.timezone)
751
# now gather global repository information
752
if self.bzrdir.root_transport.listable():
753
c, t = self._revision_store.total_size(self.get_transaction())
754
result['revisions'] = c
758
def get_data_stream(self, revision_ids):
759
raise NotImplementedError(self.get_data_stream)
761
def insert_data_stream(self, stream):
762
"""XXX What does this really do?
764
Is it a substitute for fetch?
765
Should it manage its own write group ?
767
for item_key, bytes in stream:
768
if item_key[0] == 'file':
769
(file_id,) = item_key[1:]
770
knit = self.weave_store.get_weave_or_empty(
771
file_id, self.get_transaction())
772
elif item_key == ('inventory',):
773
knit = self.get_inventory_weave()
774
elif item_key == ('revisions',):
775
knit = self._revision_store.get_revision_file(
776
self.get_transaction())
777
elif item_key == ('signatures',):
778
knit = self._revision_store.get_signature_file(
779
self.get_transaction())
781
raise RepositoryDataStreamError(
782
"Unrecognised data stream key '%s'" % (item_key,))
783
decoded_list = bencode.bdecode(bytes)
784
format = decoded_list.pop(0)
787
for version, options, parents, some_bytes in decoded_list:
788
data_list.append((version, options, len(some_bytes), parents))
789
knit_bytes += some_bytes
790
knit.insert_data_stream(
791
(format, data_list, StringIO(knit_bytes).read))
794
216
def missing_revision_ids(self, other, revision_id=None):
795
217
"""Return the revision ids that other has that this does not.
876
262
:param revprops: Optional dictionary of revision properties.
877
263
:param revision_id: Optional revision id.
879
result = self._commit_builder_class(self, parents, config,
880
timestamp, timezone, committer, revprops, revision_id)
881
self.start_write_group()
265
return _CommitBuilder(self, parents, config, timestamp, timezone,
266
committer, revprops, revision_id)
884
268
def unlock(self):
885
if (self.control_files._lock_count == 1 and
886
self.control_files._lock_mode == 'w'):
887
if self._write_group is not None:
888
raise errors.BzrError(
889
'Must end write groups before releasing write locks.')
890
269
self.control_files.unlock()
893
def clone(self, a_bzrdir, revision_id=None):
272
def clone(self, a_bzrdir, revision_id=None, basis=None):
894
273
"""Clone this repository into a_bzrdir using the current format.
896
275
Currently no check is made that the format of this repository and
897
276
the bzrdir format are compatible. FIXME RBC 20060201.
899
:return: The newly created destination repository.
901
# TODO: deprecate after 0.16; cloning this with all its settings is
902
# probably not very useful -- mbp 20070423
903
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
904
self.copy_content_into(dest_repo, revision_id)
907
def start_write_group(self):
908
"""Start a write group in the repository.
910
Write groups are used by repositories which do not have a 1:1 mapping
911
between file ids and backend store to manage the insertion of data from
912
both fetch and commit operations.
914
A write lock is required around the start_write_group/commit_write_group
915
for the support of lock-requiring repository formats.
917
One can only insert data into a repository inside a write group.
921
if not self.is_write_locked():
922
raise errors.NotWriteLocked(self)
923
if self._write_group:
924
raise errors.BzrError('already in a write group')
925
self._start_write_group()
926
# so we can detect unlock/relock - the write group is now entered.
927
self._write_group = self.get_transaction()
929
def _start_write_group(self):
930
"""Template method for per-repository write group startup.
932
This is called before the write group is considered to be
937
def sprout(self, to_bzrdir, revision_id=None):
938
"""Create a descendent repository for new development.
940
Unlike clone, this does not copy the settings of the repository.
942
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
943
dest_repo.fetch(self, revision_id=revision_id)
946
def _create_sprouting_repo(self, a_bzrdir, shared):
947
278
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
948
279
# use target default format.
949
dest_repo = a_bzrdir.create_repository()
280
result = a_bzrdir.create_repository()
281
# FIXME RBC 20060209 split out the repository type to avoid this check ?
282
elif isinstance(a_bzrdir._format,
283
(bzrdir.BzrDirFormat4,
284
bzrdir.BzrDirFormat5,
285
bzrdir.BzrDirFormat6)):
286
result = a_bzrdir.open_repository()
951
# Most control formats need the repository to be specifically
952
# created, but on some old all-in-one formats it's not needed
954
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
955
except errors.UninitializableFormat:
956
dest_repo = a_bzrdir.open_repository()
288
result = self._format.initialize(a_bzrdir, shared=self.is_shared())
289
self.copy_content_into(result, revision_id, basis)
960
293
def has_revision(self, revision_id):
961
294
"""True if this repository has a copy of the revision."""
962
if 'evil' in debug.debug_flags:
963
mutter_callsite(3, "has_revision is a LBYL symptom.")
964
295
return self._revision_store.has_revision_id(revision_id,
965
296
self.get_transaction())
968
def get_revision(self, revision_id):
969
"""Return the Revision object for a named revision."""
970
return self.get_revisions([revision_id])[0]
973
299
def get_revision_reconcile(self, revision_id):
974
300
"""'reconcile' helper routine that allows access to a revision always.
1071
417
# revisions. We don't need to see all lines in the inventory because
1072
418
# only those added in an inventory in rev X can contain a revision=X
1074
unescape_revid_cache = {}
1075
unescape_fileid_cache = {}
1077
# jam 20061218 In a big fetch, this handles hundreds of thousands
1078
# of lines, so it has had a lot of inlining and optimizing done.
1079
# Sorry that it is a little bit messy.
1080
# Move several functions to be local variables, since this is a long
1082
search = self._file_ids_altered_regex.search
1083
unescape = _unescape_xml
1084
setdefault = result.setdefault
1085
for line in line_iterator:
1086
match = search(line)
1089
# One call to match.group() returning multiple items is quite a
1090
# bit faster than 2 calls to match.group() each returning 1
1091
file_id, revision_id = match.group('file_id', 'revision_id')
1093
# Inlining the cache lookups helps a lot when you make 170,000
1094
# lines and 350k ids, versus 8.4 unique ids.
1095
# Using a cache helps in 2 ways:
1096
# 1) Avoids unnecessary decoding calls
1097
# 2) Re-uses cached strings, which helps in future set and
1099
# (2) is enough that removing encoding entirely along with
1100
# the cache (so we are using plain strings) results in no
1101
# performance improvement.
1103
revision_id = unescape_revid_cache[revision_id]
1105
unescaped = unescape(revision_id)
1106
unescape_revid_cache[revision_id] = unescaped
1107
revision_id = unescaped
1109
if revision_id in revision_ids:
1111
file_id = unescape_fileid_cache[file_id]
1113
unescaped = unescape(file_id)
1114
unescape_fileid_cache[file_id] = unescaped
1116
setdefault(file_id, set()).add(revision_id)
420
for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
421
start = line.find('file_id="')+9
422
if start < 9: continue
423
end = line.find('"', start)
425
file_id = _unescape_xml(line[start:end])
427
start = line.find('revision="')+10
428
if start < 10: continue
429
end = line.find('"', start)
431
revision_id = _unescape_xml(line[start:end])
432
if revision_id in selected_revision_ids:
433
result.setdefault(file_id, set()).add(revision_id)
1119
def fileids_altered_by_revision_ids(self, revision_ids):
1120
"""Find the file ids and versions affected by revisions.
1122
:param revisions: an iterable containing revision ids.
1123
:return: a dictionary mapping altered file-ids to an iterable of
1124
revision_ids. Each altered file-ids has the exact revision_ids that
1125
altered it listed explicitly.
1127
assert self._serializer.support_altered_by_hack, \
1128
("fileids_altered_by_revision_ids only supported for branches "
1129
"which store inventory as unnested xml, not on %r" % self)
1130
selected_revision_ids = set(revision_ids)
1131
w = self.get_inventory_weave()
1132
pb = ui.ui_factory.nested_progress_bar()
1134
return self._find_file_ids_from_xml_inventory_lines(
1135
w.iter_lines_added_or_present_in_versions(
1136
selected_revision_ids, pb=pb),
1137
selected_revision_ids)
1141
def iter_files_bytes(self, desired_files):
1142
"""Iterate through file versions.
1144
Files will not necessarily be returned in the order they occur in
1145
desired_files. No specific order is guaranteed.
1147
Yields pairs of identifier, bytes_iterator. identifier is an opaque
1148
value supplied by the caller as part of desired_files. It should
1149
uniquely identify the file version in the caller's context. (Examples:
1150
an index number or a TreeTransform trans_id.)
1152
bytes_iterator is an iterable of bytestrings for the file. The
1153
kind of iterable and length of the bytestrings are unspecified, but for
1154
this implementation, it is a list of lines produced by
1155
VersionedFile.get_lines().
1157
:param desired_files: a list of (file_id, revision_id, identifier)
1160
transaction = self.get_transaction()
1161
for file_id, revision_id, callable_data in desired_files:
1163
weave = self.weave_store.get_weave(file_id, transaction)
1164
except errors.NoSuchFile:
1165
raise errors.NoSuchIdInRepository(self, file_id)
1166
yield callable_data, weave.get_lines(revision_id)
1168
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1169
"""Get an iterable listing the keys of all the data introduced by a set
1172
The keys will be ordered so that the corresponding items can be safely
1173
fetched and inserted in that order.
1175
:returns: An iterable producing tuples of (knit-kind, file-id,
1176
versions). knit-kind is one of 'file', 'inventory', 'signatures',
1177
'revisions'. file-id is None unless knit-kind is 'file'.
1179
# XXX: it's a bit weird to control the inventory weave caching in this
1180
# generator. Ideally the caching would be done in fetch.py I think. Or
1181
# maybe this generator should explicitly have the contract that it
1182
# should not be iterated until the previously yielded item has been
1185
inv_w = self.get_inventory_weave()
1186
inv_w.enable_cache()
1188
# file ids that changed
1189
file_ids = self.fileids_altered_by_revision_ids(revision_ids)
1191
num_file_ids = len(file_ids)
1192
for file_id, altered_versions in file_ids.iteritems():
1193
if _files_pb is not None:
1194
_files_pb.update("fetch texts", count, num_file_ids)
1196
yield ("file", file_id, altered_versions)
1197
# We're done with the files_pb. Note that it finished by the caller,
1198
# just as it was created by the caller.
1202
yield ("inventory", None, revision_ids)
1206
revisions_with_signatures = set()
1207
for rev_id in revision_ids:
1209
self.get_signature_text(rev_id)
1210
except errors.NoSuchRevision:
1214
revisions_with_signatures.add(rev_id)
1216
yield ("signatures", None, revisions_with_signatures)
1219
yield ("revisions", None, revision_ids)
1221
436
@needs_read_lock
1222
437
def get_inventory_weave(self):
1223
438
return self.control_weaves.get_weave('inventory',
1551
697
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1552
698
% (self._format, self.bzrdir.transport.base))
1554
def supports_rich_root(self):
1555
return self._format.rich_root_data
1557
def _check_ascii_revisionid(self, revision_id, method):
1558
"""Private helper for ascii-only repositories."""
1559
# weave repositories refuse to store revisionids that are non-ascii.
1560
if revision_id is not None:
1561
# weaves require ascii revision ids.
1562
if isinstance(revision_id, unicode):
1564
revision_id.encode('ascii')
1565
except UnicodeEncodeError:
1566
raise errors.NonAsciiRevisionId(method, self)
1569
revision_id.decode('ascii')
1570
except UnicodeDecodeError:
1571
raise errors.NonAsciiRevisionId(method, self)
701
class AllInOneRepository(Repository):
702
"""Legacy support - the repository behaviour for all-in-one branches."""
704
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
705
# we reuse one control files instance.
706
dir_mode = a_bzrdir._control_files._dir_mode
707
file_mode = a_bzrdir._control_files._file_mode
709
def get_store(name, compressed=True, prefixed=False):
710
# FIXME: This approach of assuming stores are all entirely compressed
711
# or entirely uncompressed is tidy, but breaks upgrade from
712
# some existing branches where there's a mixture; we probably
713
# still want the option to look for both.
714
relpath = a_bzrdir._control_files._escape(name)
715
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
716
prefixed=prefixed, compressed=compressed,
719
#if self._transport.should_cache():
720
# cache_path = os.path.join(self.cache_root, name)
721
# os.mkdir(cache_path)
722
# store = bzrlib.store.CachedStore(store, cache_path)
725
# not broken out yet because the controlweaves|inventory_store
726
# and text_store | weave_store bits are still different.
727
if isinstance(_format, RepositoryFormat4):
728
# cannot remove these - there is still no consistent api
729
# which allows access to this old info.
730
self.inventory_store = get_store('inventory-store')
731
text_store = get_store('text-store')
732
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
736
"""AllInOne repositories cannot be shared."""
740
def set_make_working_trees(self, new_value):
741
"""Set the policy flag for making working trees when creating branches.
743
This only applies to branches that use this repository.
745
The default is 'True'.
746
:param new_value: True to restore the default, False to disable making
749
raise NotImplementedError(self.set_make_working_trees)
1573
def revision_graph_can_have_wrong_parents(self):
1574
"""Is it possible for this repository to have a revision graph with
1577
If True, then this repository must also implement
1578
_find_inconsistent_revision_parents so that check and reconcile can
1579
check for inconsistencies before proceeding with other checks that may
1580
depend on the revision index being consistent.
1582
raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1584
# remove these delegates a while after bzr 0.15
1585
def __make_delegated(name, from_module):
1586
def _deprecated_repository_forwarder():
1587
symbol_versioning.warn('%s moved to %s in bzr 0.15'
1588
% (name, from_module),
1591
m = __import__(from_module, globals(), locals(), [name])
1593
return getattr(m, name)
1594
except AttributeError:
1595
raise AttributeError('module %s has no name %s'
1597
globals()[name] = _deprecated_repository_forwarder
1600
'AllInOneRepository',
1601
'WeaveMetaDirRepository',
1602
'PreSplitOutRepositoryFormat',
1603
'RepositoryFormat4',
1604
'RepositoryFormat5',
1605
'RepositoryFormat6',
1606
'RepositoryFormat7',
1608
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1612
'RepositoryFormatKnit',
1613
'RepositoryFormatKnit1',
1615
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
751
def make_working_trees(self):
752
"""Returns the policy for making working trees on new branches."""
1618
756
def install_revision(repository, rev, revision_tree):
1619
757
"""Install all revision data into a repository."""
1620
repository.start_write_group()
1622
_install_revision(repository, rev, revision_tree)
1624
repository.abort_write_group()
1627
repository.commit_write_group()
1630
def _install_revision(repository, rev, revision_tree):
1631
"""Install all revision data into a repository."""
1632
758
present_parents = []
1633
759
parent_trees = {}
1634
760
for p_id in rev.parent_ids:
1716
840
return not self.control_files._transport.has('no-working-trees')
1719
class RepositoryFormatRegistry(registry.Registry):
1720
"""Registry of RepositoryFormats."""
1722
def get(self, format_string):
1723
r = registry.Registry.get(self, format_string)
843
class KnitRepository(MetaDirRepository):
844
"""Knit format repository."""
846
def _warn_if_deprecated(self):
847
# This class isn't deprecated
850
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
851
inv_vf.add_lines_with_ghosts(revid, parents, lines)
854
def _all_revision_ids(self):
855
"""See Repository.all_revision_ids()."""
856
# Knits get the revision graph from the index of the revision knit, so
857
# it's always possible even if they're on an unlistable transport.
858
return self._revision_store.all_revision_ids(self.get_transaction())
860
def fileid_involved_between_revs(self, from_revid, to_revid):
861
"""Find file_id(s) which are involved in the changes between revisions.
863
This determines the set of revisions which are involved, and then
864
finds all file ids affected by those revisions.
866
vf = self._get_revision_vf()
867
from_set = set(vf.get_ancestry(from_revid))
868
to_set = set(vf.get_ancestry(to_revid))
869
changed = to_set.difference(from_set)
870
return self._fileid_involved_by_set(changed)
872
def fileid_involved(self, last_revid=None):
873
"""Find all file_ids modified in the ancestry of last_revid.
875
:param last_revid: If None, last_revision() will be used.
878
changed = set(self.all_revision_ids())
880
changed = set(self.get_ancestry(last_revid))
883
return self._fileid_involved_by_set(changed)
886
def get_ancestry(self, revision_id):
887
"""Return a list of revision-ids integrated by a revision.
889
This is topologically sorted.
891
if revision_id is None:
893
vf = self._get_revision_vf()
895
return [None] + vf.get_ancestry(revision_id)
896
except errors.RevisionNotPresent:
897
raise errors.NoSuchRevision(self, revision_id)
900
def get_revision(self, revision_id):
901
"""Return the Revision object for a named revision"""
902
return self.get_revision_reconcile(revision_id)
905
def get_revision_graph(self, revision_id=None):
906
"""Return a dictionary containing the revision graph.
908
:param revision_id: The revision_id to get a graph from. If None, then
909
the entire revision graph is returned. This is a deprecated mode of
910
operation and will be removed in the future.
911
:return: a dictionary of revision_id->revision_parents_list.
913
# special case NULL_REVISION
914
if revision_id == NULL_REVISION:
916
weave = self._get_revision_vf()
917
entire_graph = weave.get_graph()
918
if revision_id is None:
919
return weave.get_graph()
920
elif revision_id not in weave:
921
raise errors.NoSuchRevision(self, revision_id)
923
# add what can be reached from revision_id
925
pending = set([revision_id])
926
while len(pending) > 0:
928
result[node] = weave.get_parents(node)
929
for revision_id in result[node]:
930
if revision_id not in result:
931
pending.add(revision_id)
935
def get_revision_graph_with_ghosts(self, revision_ids=None):
936
"""Return a graph of the revisions with ghosts marked as applicable.
938
:param revision_ids: an iterable of revisions to graph or None for all.
939
:return: a Graph object with the graph reachable from revision_ids.
942
vf = self._get_revision_vf()
943
versions = set(vf.versions())
945
pending = set(self.all_revision_ids())
948
pending = set(revision_ids)
949
# special case NULL_REVISION
950
if NULL_REVISION in pending:
951
pending.remove(NULL_REVISION)
952
required = set(pending)
955
revision_id = pending.pop()
956
if not revision_id in versions:
957
if revision_id in required:
958
raise errors.NoSuchRevision(self, revision_id)
960
result.add_ghost(revision_id)
961
# mark it as done so we don't try for it again.
962
done.add(revision_id)
964
parent_ids = vf.get_parents_with_ghosts(revision_id)
965
for parent_id in parent_ids:
966
# is this queued or done ?
967
if (parent_id not in pending and
968
parent_id not in done):
970
pending.add(parent_id)
971
result.add_node(revision_id, parent_ids)
972
done.add(revision_id)
975
def _get_revision_vf(self):
976
""":return: a versioned file containing the revisions."""
977
vf = self._revision_store.get_revision_file(self.get_transaction())
981
def reconcile(self, other=None, thorough=False):
982
"""Reconcile this repository."""
983
from bzrlib.reconcile import KnitReconciler
984
reconciler = KnitReconciler(self, thorough=thorough)
985
reconciler.reconcile()
1729
format_registry = RepositoryFormatRegistry()
1730
"""Registry of formats, indexed by their identifying format string.
1732
This can contain either format instances themselves, or classes/factories that
1733
can be called to obtain one.
1737
#####################################################################
1738
# Repository Formats
988
def revision_parents(self, revision_id):
989
return self._get_revision_vf().get_parents(revision_id)
1740
992
class RepositoryFormat(object):
1741
993
"""A repository format.
1904
1125
raise NotImplementedError(self.open)
1128
def register_format(klass, format):
1129
klass._formats[format.get_format_string()] = format
1132
def set_default_format(klass, format):
1133
klass._default_format = format
1136
def unregister_format(klass, format):
1137
assert klass._formats[format.get_format_string()] is format
1138
del klass._formats[format.get_format_string()]
1141
class PreSplitOutRepositoryFormat(RepositoryFormat):
1142
"""Base class for the pre split out repository formats."""
1144
def initialize(self, a_bzrdir, shared=False, _internal=False):
1145
"""Create a weave repository.
1147
TODO: when creating split out bzr branch formats, move this to a common
1148
base for Format5, Format6. or something like that.
1150
from bzrlib.weavefile import write_weave_v5
1151
from bzrlib.weave import Weave
1154
raise errors.IncompatibleFormat(self, a_bzrdir._format)
1157
# always initialized when the bzrdir is.
1158
return self.open(a_bzrdir, _found=True)
1160
# Create an empty weave
1162
write_weave_v5(Weave(), sio)
1163
empty_weave = sio.getvalue()
1165
mutter('creating repository in %s.', a_bzrdir.transport.base)
1166
dirs = ['revision-store', 'weaves']
1167
files = [('inventory.weave', StringIO(empty_weave)),
1170
# FIXME: RBC 20060125 don't peek under the covers
1171
# NB: no need to escape relative paths that are url safe.
1172
control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
1174
control_files.create_lock()
1175
control_files.lock_write()
1176
control_files._transport.mkdir_multi(dirs,
1177
mode=control_files._dir_mode)
1179
for file, content in files:
1180
control_files.put(file, content)
1182
control_files.unlock()
1183
return self.open(a_bzrdir, _found=True)
1185
def _get_control_store(self, repo_transport, control_files):
1186
"""Return the control store for this repository."""
1187
return self._get_versioned_file_store('',
1192
def _get_text_store(self, transport, control_files):
1193
"""Get a store for file texts for this format."""
1194
raise NotImplementedError(self._get_text_store)
1196
def open(self, a_bzrdir, _found=False):
1197
"""See RepositoryFormat.open()."""
1199
# we are being called directly and must probe.
1200
raise NotImplementedError
1202
repo_transport = a_bzrdir.get_repository_transport(None)
1203
control_files = a_bzrdir._control_files
1204
text_store = self._get_text_store(repo_transport, control_files)
1205
control_store = self._get_control_store(repo_transport, control_files)
1206
_revision_store = self._get_revision_store(repo_transport, control_files)
1207
return AllInOneRepository(_format=self,
1209
_revision_store=_revision_store,
1210
control_store=control_store,
1211
text_store=text_store)
1214
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1215
"""Bzr repository format 4.
1217
This repository format has:
1219
- TextStores for texts, inventories,revisions.
1221
This format is deprecated: it indexes texts using a text id which is
1222
removed in format 5; initialization and write support for this format
1227
super(RepositoryFormat4, self).__init__()
1228
self._matchingbzrdir = bzrdir.BzrDirFormat4()
1230
def get_format_description(self):
1231
"""See RepositoryFormat.get_format_description()."""
1232
return "Repository format 4"
1234
def initialize(self, url, shared=False, _internal=False):
1235
"""Format 4 branches cannot be created."""
1236
raise errors.UninitializableFormat(self)
1238
def is_supported(self):
1239
"""Format 4 is not supported.
1241
It is not supported because the model changed from 4 to 5 and the
1242
conversion logic is expensive - so doing it on the fly was not
1247
def _get_control_store(self, repo_transport, control_files):
1248
"""Format 4 repositories have no formal control store at this point.
1250
This will cause any control-file-needing apis to fail - this is desired.
1254
def _get_revision_store(self, repo_transport, control_files):
1255
"""See RepositoryFormat._get_revision_store()."""
1256
from bzrlib.xml4 import serializer_v4
1257
return self._get_text_rev_store(repo_transport,
1260
serializer=serializer_v4)
1262
def _get_text_store(self, transport, control_files):
1263
"""See RepositoryFormat._get_text_store()."""
1266
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1267
"""Bzr control format 5.
1269
This repository format has:
1270
- weaves for file texts and inventory
1272
- TextStores for revisions and signatures.
1276
super(RepositoryFormat5, self).__init__()
1277
self._matchingbzrdir = bzrdir.BzrDirFormat5()
1279
def get_format_description(self):
1280
"""See RepositoryFormat.get_format_description()."""
1281
return "Weave repository format 5"
1283
def _get_revision_store(self, repo_transport, control_files):
1284
"""See RepositoryFormat._get_revision_store()."""
1285
"""Return the revision store object for this a_bzrdir."""
1286
return self._get_text_rev_store(repo_transport,
1291
def _get_text_store(self, transport, control_files):
1292
"""See RepositoryFormat._get_text_store()."""
1293
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1296
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1297
"""Bzr control format 6.
1299
This repository format has:
1300
- weaves for file texts and inventory
1301
- hash subdirectory based stores.
1302
- TextStores for revisions and signatures.
1306
super(RepositoryFormat6, self).__init__()
1307
self._matchingbzrdir = bzrdir.BzrDirFormat6()
1309
def get_format_description(self):
1310
"""See RepositoryFormat.get_format_description()."""
1311
return "Weave repository format 6"
1313
def _get_revision_store(self, repo_transport, control_files):
1314
"""See RepositoryFormat._get_revision_store()."""
1315
return self._get_text_rev_store(repo_transport,
1321
def _get_text_store(self, transport, control_files):
1322
"""See RepositoryFormat._get_text_store()."""
1323
return self._get_versioned_file_store('weaves', transport, control_files)
1907
1326
class MetaDirRepositoryFormat(RepositoryFormat):
1908
1327
"""Common base class for the new repositories using the metadir layout."""
1910
rich_root_data = False
1911
supports_tree_reference = False
1912
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1914
1329
def __init__(self):
1915
1330
super(MetaDirRepositoryFormat, self).__init__()
1331
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1917
1333
def _create_control_files(self, a_bzrdir):
1918
1334
"""Create the required files and the initial control_files object."""
1919
1335
# FIXME: RBC 20060125 don't peek under the covers
1920
1336
# NB: no need to escape relative paths that are url safe.
1921
1337
repository_transport = a_bzrdir.get_repository_transport(self)
1922
control_files = lockable_files.LockableFiles(repository_transport,
1923
'lock', lockdir.LockDir)
1338
control_files = LockableFiles(repository_transport, 'lock', LockDir)
1924
1339
control_files.create_lock()
1925
1340
return control_files
1941
1356
control_files.unlock()
1359
class RepositoryFormat7(MetaDirRepositoryFormat):
1360
"""Bzr repository 7.
1362
This repository format has:
1363
- weaves for file texts and inventory
1364
- hash subdirectory based stores.
1365
- TextStores for revisions and signatures.
1366
- a format marker of its own
1367
- an optional 'shared-storage' flag
1368
- an optional 'no-working-trees' flag
1371
def _get_control_store(self, repo_transport, control_files):
1372
"""Return the control store for this repository."""
1373
return self._get_versioned_file_store('',
1378
def get_format_string(self):
1379
"""See RepositoryFormat.get_format_string()."""
1380
return "Bazaar-NG Repository format 7"
1382
def get_format_description(self):
1383
"""See RepositoryFormat.get_format_description()."""
1384
return "Weave repository format 7"
1386
def _get_revision_store(self, repo_transport, control_files):
1387
"""See RepositoryFormat._get_revision_store()."""
1388
return self._get_text_rev_store(repo_transport,
1395
def _get_text_store(self, transport, control_files):
1396
"""See RepositoryFormat._get_text_store()."""
1397
return self._get_versioned_file_store('weaves',
1401
def initialize(self, a_bzrdir, shared=False):
1402
"""Create a weave repository.
1404
:param shared: If true the repository will be initialized as a shared
1407
from bzrlib.weavefile import write_weave_v5
1408
from bzrlib.weave import Weave
1410
# Create an empty weave
1412
write_weave_v5(Weave(), sio)
1413
empty_weave = sio.getvalue()
1415
mutter('creating repository in %s.', a_bzrdir.transport.base)
1416
dirs = ['revision-store', 'weaves']
1417
files = [('inventory.weave', StringIO(empty_weave)),
1419
utf8_files = [('format', self.get_format_string())]
1421
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1422
return self.open(a_bzrdir=a_bzrdir, _found=True)
1424
def open(self, a_bzrdir, _found=False, _override_transport=None):
1425
"""See RepositoryFormat.open().
1427
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1428
repository at a slightly different url
1429
than normal. I.e. during 'upgrade'.
1432
format = RepositoryFormat.find_format(a_bzrdir)
1433
assert format.__class__ == self.__class__
1434
if _override_transport is not None:
1435
repo_transport = _override_transport
1437
repo_transport = a_bzrdir.get_repository_transport(None)
1438
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1439
text_store = self._get_text_store(repo_transport, control_files)
1440
control_store = self._get_control_store(repo_transport, control_files)
1441
_revision_store = self._get_revision_store(repo_transport, control_files)
1442
return MetaDirRepository(_format=self,
1444
control_files=control_files,
1445
_revision_store=_revision_store,
1446
control_store=control_store,
1447
text_store=text_store)
1450
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1451
"""Bzr repository knit format 1.
1453
This repository format has:
1454
- knits for file texts and inventory
1455
- hash subdirectory based stores.
1456
- knits for revisions and signatures
1457
- TextStores for revisions and signatures.
1458
- a format marker of its own
1459
- an optional 'shared-storage' flag
1460
- an optional 'no-working-trees' flag
1463
This format was introduced in bzr 0.8.
1466
def _get_control_store(self, repo_transport, control_files):
1467
"""Return the control store for this repository."""
1468
return VersionedFileStore(
1471
file_mode=control_files._file_mode,
1472
versionedfile_class=KnitVersionedFile,
1473
versionedfile_kwargs={'factory':KnitPlainFactory()},
1476
def get_format_string(self):
1477
"""See RepositoryFormat.get_format_string()."""
1478
return "Bazaar-NG Knit Repository Format 1"
1480
def get_format_description(self):
1481
"""See RepositoryFormat.get_format_description()."""
1482
return "Knit repository format 1"
1484
def _get_revision_store(self, repo_transport, control_files):
1485
"""See RepositoryFormat._get_revision_store()."""
1486
from bzrlib.store.revision.knit import KnitRevisionStore
1487
versioned_file_store = VersionedFileStore(
1489
file_mode=control_files._file_mode,
1492
versionedfile_class=KnitVersionedFile,
1493
versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
1496
return KnitRevisionStore(versioned_file_store)
1498
def _get_text_store(self, transport, control_files):
1499
"""See RepositoryFormat._get_text_store()."""
1500
return self._get_versioned_file_store('knits',
1503
versionedfile_class=KnitVersionedFile,
1506
def initialize(self, a_bzrdir, shared=False):
1507
"""Create a knit format 1 repository.
1509
:param a_bzrdir: bzrdir to contain the new repository; must already
1511
:param shared: If true the repository will be initialized as a shared
1514
mutter('creating repository in %s.', a_bzrdir.transport.base)
1515
dirs = ['revision-store', 'knits']
1517
utf8_files = [('format', self.get_format_string())]
1519
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1520
repo_transport = a_bzrdir.get_repository_transport(None)
1521
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1522
control_store = self._get_control_store(repo_transport, control_files)
1523
transaction = transactions.WriteTransaction()
1524
# trigger a write of the inventory store.
1525
control_store.get_weave_or_empty('inventory', transaction)
1526
_revision_store = self._get_revision_store(repo_transport, control_files)
1527
_revision_store.has_revision_id('A', transaction)
1528
_revision_store.get_signature_file(transaction)
1529
return self.open(a_bzrdir=a_bzrdir, _found=True)
1531
def open(self, a_bzrdir, _found=False, _override_transport=None):
1532
"""See RepositoryFormat.open().
1534
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1535
repository at a slightly different url
1536
than normal. I.e. during 'upgrade'.
1539
format = RepositoryFormat.find_format(a_bzrdir)
1540
assert format.__class__ == self.__class__
1541
if _override_transport is not None:
1542
repo_transport = _override_transport
1544
repo_transport = a_bzrdir.get_repository_transport(None)
1545
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1546
text_store = self._get_text_store(repo_transport, control_files)
1547
control_store = self._get_control_store(repo_transport, control_files)
1548
_revision_store = self._get_revision_store(repo_transport, control_files)
1549
return KnitRepository(_format=self,
1551
control_files=control_files,
1552
_revision_store=_revision_store,
1553
control_store=control_store,
1554
text_store=text_store)
1944
1557
# formats which have no format string are not discoverable
1945
# and not independently creatable, so are not registered. They're
1946
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
1947
# needed, it's constructed directly by the BzrDir. Non-native formats where
1948
# the repository is not separately opened are similar.
1950
format_registry.register_lazy(
1951
'Bazaar-NG Repository format 7',
1952
'bzrlib.repofmt.weaverepo',
1956
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1957
# default control directory format
1958
format_registry.register_lazy(
1959
'Bazaar-NG Knit Repository Format 1',
1960
'bzrlib.repofmt.knitrepo',
1961
'RepositoryFormatKnit1',
1963
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
1965
format_registry.register_lazy(
1966
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
1967
'bzrlib.repofmt.knitrepo',
1968
'RepositoryFormatKnit3',
1971
# Experimental formats. These make no guarantee about data stability.
1972
# There is one format for pre-subtrees, and one for post-subtrees to
1973
# allow ease of testing.
1974
format_registry.register_lazy(
1975
'Bazaar Experimental no-subtrees\n',
1976
'bzrlib.repofmt.pack_repo',
1977
'RepositoryFormatKnitPack1',
1979
format_registry.register_lazy(
1980
'Bazaar Experimental subtrees\n',
1981
'bzrlib.repofmt.pack_repo',
1982
'RepositoryFormatKnitPack3',
1558
# and not independently creatable, so are not registered.
1559
RepositoryFormat.register_format(RepositoryFormat7())
1560
_default_format = RepositoryFormatKnit1()
1561
RepositoryFormat.register_format(_default_format)
1562
RepositoryFormat.set_default_format(_default_format)
1563
_legacy_formats = [RepositoryFormat4(),
1564
RepositoryFormat5(),
1565
RepositoryFormat6()]
1986
1568
class InterRepository(InterObject):
2291
1831
# that against the revision records.
2292
1832
return self.source._eliminate_revisions_not_present(required_topo_revisions)
2295
class InterPackRepo(InterSameDataRepository):
2296
"""Optimised code paths between Pack based repositories."""
2299
def _get_repo_format_to_test(self):
2300
from bzrlib.repofmt import pack_repo
2301
return pack_repo.RepositoryFormatKnitPack1()
2304
def is_compatible(source, target):
2305
"""Be compatible with known Pack formats.
2307
We don't test for the stores being of specific types because that
2308
could lead to confusing results, and there is no need to be
2311
from bzrlib.repofmt.pack_repo import RepositoryFormatPack
2313
are_packs = (isinstance(source._format, RepositoryFormatPack) and
2314
isinstance(target._format, RepositoryFormatPack))
2315
except AttributeError:
2317
return are_packs and InterRepository._same_model(source, target)
2320
def fetch(self, revision_id=None, pb=None):
2321
"""See InterRepository.fetch()."""
2322
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2323
self.source, self.source._format, self.target, self.target._format)
2324
self.count_copied = 0
2325
if revision_id is None:
2327
# everything to do - use pack logic
2328
# to fetch from all packs to one without
2329
# inventory parsing etc, IFF nothing to be copied is in the target.
2331
revision_ids = self.source.all_revision_ids()
2332
# implementing the TODO will involve:
2333
# - detecting when all of a pack is selected
2334
# - avoiding as much as possible pre-selection, so the
2335
# more-core routines such as create_pack_from_packs can filter in
2336
# a just-in-time fashion. (though having a HEADS list on a
2337
# repository might make this a lot easier, because we could
2338
# sensibly detect 'new revisions' without doing a full index scan.
2339
elif _mod_revision.is_null(revision_id):
2344
revision_ids = self.missing_revision_ids(revision_id)
2345
except errors.NoSuchRevision:
2346
raise errors.InstallFailed([revision_id])
2347
packs = self.source._pack_collection.all_packs()
2348
pack = self.target._pack_collection.create_pack_from_packs(
2349
packs, '.fetch', revision_ids,
2351
if pack is not None:
2352
self.target._pack_collection._save_pack_names()
2353
# Trigger an autopack. This may duplicate effort as we've just done
2354
# a pack creation, but for now it is simpler to think about as
2355
# 'upload data, then repack if needed'.
2356
self.target._pack_collection.autopack()
2357
return pack.get_revision_count()
2362
def missing_revision_ids(self, revision_id=None):
2363
"""See InterRepository.missing_revision_ids()."""
2364
if revision_id is not None:
2365
source_ids = self.source.get_ancestry(revision_id)
2366
assert source_ids[0] is None
2369
source_ids = self.source.all_revision_ids()
2370
# source_ids is the worst possible case we may need to pull.
2371
# now we want to filter source_ids against what we actually
2372
# have in target, but don't try to check for existence where we know
2373
# we do not have a revision as that would be pointless.
2374
target_ids = set(self.target.all_revision_ids())
2375
return [r for r in source_ids if (r not in target_ids)]
2378
class InterModel1and2(InterRepository):
2381
def _get_repo_format_to_test(self):
2385
def is_compatible(source, target):
2386
if not source.supports_rich_root() and target.supports_rich_root():
2392
def fetch(self, revision_id=None, pb=None):
2393
"""See InterRepository.fetch()."""
2394
from bzrlib.fetch import Model1toKnit2Fetcher
2395
f = Model1toKnit2Fetcher(to_repository=self.target,
2396
from_repository=self.source,
2397
last_revision=revision_id,
2399
return f.count_copied, f.failed_revisions
2402
def copy_content(self, revision_id=None):
2403
"""Make a complete copy of the content in self into destination.
2405
This is a destructive operation! Do not use it on existing
2408
:param revision_id: Only copy the content needed to construct
2409
revision_id and its parents.
2412
self.target.set_make_working_trees(self.source.make_working_trees())
2413
except NotImplementedError:
2415
# but don't bother fetching if we have the needed data now.
2416
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
2417
self.target.has_revision(revision_id)):
2419
self.target.fetch(self.source, revision_id=revision_id)
2422
class InterKnit1and2(InterKnitRepo):
2425
def _get_repo_format_to_test(self):
2429
def is_compatible(source, target):
2430
"""Be compatible with Knit1 source and Knit3 target"""
2431
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
2433
from bzrlib.repofmt.knitrepo import (RepositoryFormatKnit1,
2434
RepositoryFormatKnit3)
2435
from bzrlib.repofmt.pack_repo import (RepositoryFormatKnitPack1,
2436
RepositoryFormatKnitPack3)
2437
return (isinstance(source._format,
2438
(RepositoryFormatKnit1, RepositoryFormatKnitPack1)) and
2439
isinstance(target._format,
2440
(RepositoryFormatKnit3, RepositoryFormatKnitPack3))
2442
except AttributeError:
2446
def fetch(self, revision_id=None, pb=None):
2447
"""See InterRepository.fetch()."""
2448
from bzrlib.fetch import Knit1to2Fetcher
2449
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2450
self.source, self.source._format, self.target,
2451
self.target._format)
2452
f = Knit1to2Fetcher(to_repository=self.target,
2453
from_repository=self.source,
2454
last_revision=revision_id,
2456
return f.count_copied, f.failed_revisions
2459
class InterRemoteToOther(InterRepository):
2461
def __init__(self, source, target):
2462
InterRepository.__init__(self, source, target)
2463
self._real_inter = None
2466
def is_compatible(source, target):
2467
if not isinstance(source, remote.RemoteRepository):
2469
source._ensure_real()
2470
real_source = source._real_repository
2471
# Is source's model compatible with target's model, and are they the
2472
# same format? Currently we can only optimise fetching from an
2473
# identical model & format repo.
2474
assert not isinstance(real_source, remote.RemoteRepository), (
2475
"We don't support remote repos backed by remote repos yet.")
2476
return real_source._format == target._format
2479
def fetch(self, revision_id=None, pb=None):
2480
"""See InterRepository.fetch()."""
2481
from bzrlib.fetch import RemoteToOtherFetcher
2482
mutter("Using fetch logic to copy between %s(remote) and %s(%s)",
2483
self.source, self.target, self.target._format)
2484
# TODO: jam 20070210 This should be an assert, not a translate
2485
revision_id = osutils.safe_revision_id(revision_id)
2486
f = RemoteToOtherFetcher(to_repository=self.target,
2487
from_repository=self.source,
2488
last_revision=revision_id,
2490
return f.count_copied, f.failed_revisions
2493
def _get_repo_format_to_test(self):
2497
class InterOtherToRemote(InterRepository):
2499
def __init__(self, source, target):
2500
InterRepository.__init__(self, source, target)
2501
self._real_inter = None
2504
def is_compatible(source, target):
2505
if isinstance(target, remote.RemoteRepository):
2509
def _ensure_real_inter(self):
2510
if self._real_inter is None:
2511
self.target._ensure_real()
2512
real_target = self.target._real_repository
2513
self._real_inter = InterRepository.get(self.source, real_target)
2515
def copy_content(self, revision_id=None):
2516
self._ensure_real_inter()
2517
self._real_inter.copy_content(revision_id=revision_id)
2519
def fetch(self, revision_id=None, pb=None):
2520
self._ensure_real_inter()
2521
self._real_inter.fetch(revision_id=revision_id, pb=pb)
2524
def _get_repo_format_to_test(self):
2528
InterRepository.register_optimiser(InterSameDataRepository)
2529
1834
InterRepository.register_optimiser(InterWeaveRepo)
2530
1835
InterRepository.register_optimiser(InterKnitRepo)
2531
InterRepository.register_optimiser(InterModel1and2)
2532
InterRepository.register_optimiser(InterKnit1and2)
2533
InterRepository.register_optimiser(InterPackRepo)
2534
InterRepository.register_optimiser(InterRemoteToOther)
2535
InterRepository.register_optimiser(InterOtherToRemote)
1838
class RepositoryTestProviderAdapter(object):
1839
"""A tool to generate a suite testing multiple repository formats at once.
1841
This is done by copying the test once for each transport and injecting
1842
the transport_server, transport_readonly_server, and bzrdir_format and
1843
repository_format classes into each copy. Each copy is also given a new id()
1844
to make it easy to identify.
1847
def __init__(self, transport_server, transport_readonly_server, formats):
1848
self._transport_server = transport_server
1849
self._transport_readonly_server = transport_readonly_server
1850
self._formats = formats
1852
def adapt(self, test):
1853
result = TestSuite()
1854
for repository_format, bzrdir_format in self._formats:
1855
new_test = deepcopy(test)
1856
new_test.transport_server = self._transport_server
1857
new_test.transport_readonly_server = self._transport_readonly_server
1858
new_test.bzrdir_format = bzrdir_format
1859
new_test.repository_format = repository_format
1860
def make_new_test_id():
1861
new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1862
return lambda: new_id
1863
new_test.id = make_new_test_id()
1864
result.addTest(new_test)
1868
class InterRepositoryTestProviderAdapter(object):
1869
"""A tool to generate a suite testing multiple inter repository formats.
1871
This is done by copying the test once for each interrepo provider and injecting
1872
the transport_server, transport_readonly_server, repository_format and
1873
repository_to_format classes into each copy.
1874
Each copy is also given a new id() to make it easy to identify.
1877
def __init__(self, transport_server, transport_readonly_server, formats):
1878
self._transport_server = transport_server
1879
self._transport_readonly_server = transport_readonly_server
1880
self._formats = formats
1882
def adapt(self, test):
1883
result = TestSuite()
1884
for interrepo_class, repository_format, repository_format_to in self._formats:
1885
new_test = deepcopy(test)
1886
new_test.transport_server = self._transport_server
1887
new_test.transport_readonly_server = self._transport_readonly_server
1888
new_test.interrepo_class = interrepo_class
1889
new_test.repository_format = repository_format
1890
new_test.repository_format_to = repository_format_to
1891
def make_new_test_id():
1892
new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1893
return lambda: new_id
1894
new_test.id = make_new_test_id()
1895
result.addTest(new_test)
1899
def default_test_list():
1900
"""Generate the default list of interrepo permutations to test."""
1902
# test the default InterRepository between format 6 and the current
1904
# XXX: robertc 20060220 reinstate this when there are two supported
1905
# formats which do not have an optimal code path between them.
1906
result.append((InterRepository,
1907
RepositoryFormat6(),
1908
RepositoryFormatKnit1()))
1909
for optimiser in InterRepository._optimisers:
1910
result.append((optimiser,
1911
optimiser._matching_repo_format,
1912
optimiser._matching_repo_format
1914
# if there are specific combinations we want to use, we can add them
2538
1919
class CopyConverter(object):
2587
1967
self.pb.update(message, self.count, self.total)
1970
class CommitBuilder(object):
1971
"""Provides an interface to build up a commit.
1973
This allows describing a tree to be committed without needing to
1974
know the internals of the format of the repository.
1977
record_root_entry = False
1978
def __init__(self, repository, parents, config, timestamp=None,
1979
timezone=None, committer=None, revprops=None,
1981
"""Initiate a CommitBuilder.
1983
:param repository: Repository to commit to.
1984
:param parents: Revision ids of the parents of the new revision.
1985
:param config: Configuration to use.
1986
:param timestamp: Optional timestamp recorded for commit.
1987
:param timezone: Optional timezone for timestamp.
1988
:param committer: Optional committer to set for commit.
1989
:param revprops: Optional dictionary of revision properties.
1990
:param revision_id: Optional revision id.
1992
self._config = config
1994
if committer is None:
1995
self._committer = self._config.username()
1997
assert isinstance(committer, basestring), type(committer)
1998
self._committer = committer
2000
self.new_inventory = Inventory(None)
2001
self._new_revision_id = revision_id
2002
self.parents = parents
2003
self.repository = repository
2006
if revprops is not None:
2007
self._revprops.update(revprops)
2009
if timestamp is None:
2010
timestamp = time.time()
2011
# Restrict resolution to 1ms
2012
self._timestamp = round(timestamp, 3)
2014
if timezone is None:
2015
self._timezone = local_time_offset()
2017
self._timezone = int(timezone)
2019
self._generate_revision_if_needed()
2021
def commit(self, message):
2022
"""Make the actual commit.
2024
:return: The revision id of the recorded revision.
2026
rev = Revision(timestamp=self._timestamp,
2027
timezone=self._timezone,
2028
committer=self._committer,
2030
inventory_sha1=self.inv_sha1,
2031
revision_id=self._new_revision_id,
2032
properties=self._revprops)
2033
rev.parent_ids = self.parents
2034
self.repository.add_revision(self._new_revision_id, rev,
2035
self.new_inventory, self._config)
2036
return self._new_revision_id
2038
def finish_inventory(self):
2039
"""Tell the builder that the inventory is finished."""
2040
if self.new_inventory.root is None:
2041
symbol_versioning.warn('Root entry should be supplied to'
2042
' record_entry_contents, as of bzr 0.10.',
2043
DeprecationWarning, stacklevel=2)
2044
self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
2045
self.new_inventory.revision_id = self._new_revision_id
2046
self.inv_sha1 = self.repository.add_inventory(
2047
self._new_revision_id,
2052
def _gen_revision_id(self):
2053
"""Return new revision-id."""
2054
s = '%s-%s-' % (self._config.user_email(),
2055
compact_date(self._timestamp))
2056
s += hexlify(rand_bytes(8))
2059
def _generate_revision_if_needed(self):
2060
"""Create a revision id if None was supplied.
2062
If the repository can not support user-specified revision ids
2063
they should override this function and raise UnsupportedOperation
2064
if _new_revision_id is not None.
2066
:raises: UnsupportedOperation
2068
if self._new_revision_id is None:
2069
self._new_revision_id = self._gen_revision_id()
2071
def record_entry_contents(self, ie, parent_invs, path, tree):
2072
"""Record the content of ie from tree into the commit if needed.
2074
Side effect: sets ie.revision when unchanged
2076
:param ie: An inventory entry present in the commit.
2077
:param parent_invs: The inventories of the parent revisions of the
2079
:param path: The path the entry is at in the tree.
2080
:param tree: The tree which contains this entry and should be used to
2083
if self.new_inventory.root is None and ie.parent_id is not None:
2084
symbol_versioning.warn('Root entry should be supplied to'
2085
' record_entry_contents, as of bzr 0.10.',
2086
DeprecationWarning, stacklevel=2)
2087
self.record_entry_contents(tree.inventory.root.copy(), parent_invs,
2089
self.new_inventory.add(ie)
2091
# ie.revision is always None if the InventoryEntry is considered
2092
# for committing. ie.snapshot will record the correct revision
2093
# which may be the sole parent if it is untouched.
2094
if ie.revision is not None:
2097
# In this revision format, root entries have no knit or weave
2098
if ie is self.new_inventory.root:
2099
if len(parent_invs):
2100
ie.revision = parent_invs[0].root.revision
2104
previous_entries = ie.find_previous_heads(
2106
self.repository.weave_store,
2107
self.repository.get_transaction())
2108
# we are creating a new revision for ie in the history store
2110
ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2112
def modified_directory(self, file_id, file_parents):
2113
"""Record the presence of a symbolic link.
2115
:param file_id: The file_id of the link to record.
2116
:param file_parents: The per-file parent revision ids.
2118
self._add_text_to_weave(file_id, [], file_parents.keys())
2120
def modified_file_text(self, file_id, file_parents,
2121
get_content_byte_lines, text_sha1=None,
2123
"""Record the text of file file_id
2125
:param file_id: The file_id of the file to record the text of.
2126
:param file_parents: The per-file parent revision ids.
2127
:param get_content_byte_lines: A callable which will return the byte
2129
:param text_sha1: Optional SHA1 of the file contents.
2130
:param text_size: Optional size of the file contents.
2132
# mutter('storing text of file {%s} in revision {%s} into %r',
2133
# file_id, self._new_revision_id, self.repository.weave_store)
2134
# special case to avoid diffing on renames or
2136
if (len(file_parents) == 1
2137
and text_sha1 == file_parents.values()[0].text_sha1
2138
and text_size == file_parents.values()[0].text_size):
2139
previous_ie = file_parents.values()[0]
2140
versionedfile = self.repository.weave_store.get_weave(file_id,
2141
self.repository.get_transaction())
2142
versionedfile.clone_text(self._new_revision_id,
2143
previous_ie.revision, file_parents.keys())
2144
return text_sha1, text_size
2146
new_lines = get_content_byte_lines()
2147
# TODO: Rather than invoking sha_strings here, _add_text_to_weave
2148
# should return the SHA1 and size
2149
self._add_text_to_weave(file_id, new_lines, file_parents.keys())
2150
return osutils.sha_strings(new_lines), \
2151
sum(map(len, new_lines))
2153
def modified_link(self, file_id, file_parents, link_target):
2154
"""Record the presence of a symbolic link.
2156
:param file_id: The file_id of the link to record.
2157
:param file_parents: The per-file parent revision ids.
2158
:param link_target: Target location of this link.
2160
self._add_text_to_weave(file_id, [], file_parents.keys())
2162
def _add_text_to_weave(self, file_id, new_lines, parents):
2163
versionedfile = self.repository.weave_store.get_weave_or_empty(
2164
file_id, self.repository.get_transaction())
2165
versionedfile.add_lines(self._new_revision_id, parents, new_lines)
2166
versionedfile.clear_cache()
2169
class _CommitBuilder(CommitBuilder):
2170
"""Temporary class so old CommitBuilders are detected properly
2172
Note: CommitBuilder works whether or not root entry is recorded.
2175
record_root_entry = True
2590
2178
_unescape_map = {
2613
2195
"""Unescape predefined XML entities in a string of data."""
2614
2196
global _unescape_re
2615
2197
if _unescape_re is None:
2616
_unescape_re = re.compile('\&([^;]*);')
2198
_unescape_re = re.compile('\&([^;]*);')
2617
2199
return _unescape_re.sub(_unescaper, data)
2620
class _RevisionTextVersionCache(object):
2621
"""A cache of the versionedfile versions for revision and file-id."""
2623
def __init__(self, repository):
2624
self.repository = repository
2625
self.revision_versions = {}
2626
self.revision_parents = {}
2627
self.repo_graph = self.repository.get_graph()
2628
# XXX: RBC: I haven't tracked down what uses this, but it would be
2629
# better to use the headscache directly I think.
2630
self.heads = graph.HeadsCache(self.repo_graph).heads
2632
def add_revision_text_versions(self, tree):
2633
"""Cache text version data from the supplied revision tree"""
2635
for path, entry in tree.iter_entries_by_dir():
2636
inv_revisions[entry.file_id] = entry.revision
2637
self.revision_versions[tree.get_revision_id()] = inv_revisions
2638
return inv_revisions
2640
def get_text_version(self, file_id, revision_id):
2641
"""Determine the text version for a given file-id and revision-id"""
2643
inv_revisions = self.revision_versions[revision_id]
2646
tree = self.repository.revision_tree(revision_id)
2647
except errors.RevisionNotPresent:
2648
self.revision_versions[revision_id] = inv_revisions = {}
2650
inv_revisions = self.add_revision_text_versions(tree)
2651
return inv_revisions.get(file_id)
2653
def prepopulate_revs(self, revision_ids):
2654
# Filter out versions that we don't have an inventory for, so that the
2655
# revision_trees() call won't fail.
2656
inv_weave = self.repository.get_inventory_weave()
2657
revs = [r for r in revision_ids if inv_weave.has_version(r)]
2658
# XXX: this loop is very similar to
2659
# bzrlib.fetch.Inter1and2Helper.iter_rev_trees.
2661
for tree in self.repository.revision_trees(revs[:100]):
2662
if tree.inventory.revision_id is None:
2663
tree.inventory.revision_id = tree.get_revision_id()
2664
self.add_revision_text_versions(tree)
2667
def get_parents(self, revision_id):
2669
return self.revision_parents[revision_id]
2671
parents = self.repository.get_parents([revision_id])[0]
2672
self.revision_parents[revision_id] = parents
2676
class VersionedFileChecker(object):
2678
def __init__(self, planned_revisions, revision_versions, repository):
2679
self.planned_revisions = planned_revisions
2680
self.revision_versions = revision_versions
2681
self.repository = repository
2683
def calculate_file_version_parents(self, revision_id, file_id):
2684
text_revision = self.revision_versions.get_text_version(
2685
file_id, revision_id)
2686
if text_revision is None:
2688
parents_of_text_revision = self.revision_versions.get_parents(
2690
parents_from_inventories = []
2691
for parent in parents_of_text_revision:
2692
if parent == _mod_revision.NULL_REVISION:
2694
introduced_in = self.revision_versions.get_text_version(file_id,
2696
if introduced_in is not None:
2697
parents_from_inventories.append(introduced_in)
2698
heads = set(self.revision_versions.heads(parents_from_inventories))
2700
for parent in parents_from_inventories:
2701
if parent in heads and parent not in new_parents:
2702
new_parents.append(parent)
2703
return tuple(new_parents)
2705
def check_file_version_parents(self, weave, file_id):
2707
for num, revision_id in enumerate(self.planned_revisions):
2708
correct_parents = self.calculate_file_version_parents(
2709
revision_id, file_id)
2710
if correct_parents is None:
2712
text_revision = self.revision_versions.get_text_version(
2713
file_id, revision_id)
2714
knit_parents = tuple(weave.get_parents(text_revision))
2715
if correct_parents != knit_parents:
2716
result[revision_id] = (knit_parents, correct_parents)