1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from binascii import hexlify
18
from copy import deepcopy
19
from cStringIO import StringIO
22
from unittest import TestSuite
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
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
36
from bzrlib.revisiontree import RevisionTree
37
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
38
from bzrlib.store.text import TextStore
39
from bzrlib.symbol_versioning import (deprecated_method,
42
from bzrlib.testament import Testament
43
from bzrlib.trace import mutter, note
44
from bzrlib.tsort import topo_sort
45
from bzrlib.weave import WeaveFile
48
class Repository(object):
49
"""Repository holding history for one or more branches.
51
The repository holds and retrieves historical information including
52
revisions and file history. It's normally accessed only by the Branch,
53
which views a particular line of development through that history.
55
The Repository builds on top of Stores and a Transport, which respectively
56
describe the disk data format and the way of accessing the (possibly
61
def add_inventory(self, revid, inv, parents):
62
"""Add the inventory inv to the repository as revid.
64
:param parents: The revision ids of the parents that revid
65
is known to have and are in the repository already.
67
returns the sha1 of the serialized inventory.
69
assert inv.revision_id is None or inv.revision_id == revid, \
70
"Mismatch between inventory revision" \
71
" id and insertion revid (%r, %r)" % (inv.revision_id, revid)
72
inv_text = xml5.serializer_v5.write_inventory_to_string(inv)
73
inv_sha1 = osutils.sha_string(inv_text)
74
inv_vf = self.control_weaves.get_weave('inventory',
75
self.get_transaction())
76
self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
79
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
81
for parent in parents:
83
final_parents.append(parent)
85
inv_vf.add_lines(revid, final_parents, lines)
88
def add_revision(self, rev_id, rev, inv=None, config=None):
89
"""Add rev to the revision store as rev_id.
91
:param rev_id: the revision id to use.
92
:param rev: The revision object.
93
:param inv: The inventory for the revision. if None, it will be looked
94
up in the inventory storer
95
:param config: If None no digital signature will be created.
96
If supplied its signature_needed method will be used
97
to determine if a signature should be made.
99
if config is not None and config.signature_needed():
101
inv = self.get_inventory(rev_id)
102
plaintext = Testament(rev, inv).as_short_text()
103
self.store_revision_signature(
104
gpg.GPGStrategy(config), plaintext, rev_id)
105
if not rev_id in self.get_inventory_weave():
107
raise errors.WeaveRevisionNotPresent(rev_id,
108
self.get_inventory_weave())
110
# yes, this is not suitable for adding with ghosts.
111
self.add_inventory(rev_id, inv, rev.parent_ids)
112
self._revision_store.add_revision(rev, self.get_transaction())
115
def _all_possible_ids(self):
116
"""Return all the possible revisions that we could find."""
117
return self.get_inventory_weave().versions()
119
def all_revision_ids(self):
120
"""Returns a list of all the revision ids in the repository.
122
This is deprecated because code should generally work on the graph
123
reachable from a particular revision, and ignore any other revisions
124
that might be present. There is no direct replacement method.
126
return self._all_revision_ids()
129
def _all_revision_ids(self):
130
"""Returns a list of all the revision ids in the repository.
132
These are in as much topological order as the underlying store can
133
present: for weaves ghosts may lead to a lack of correctness until
134
the reweave updates the parents list.
136
if self._revision_store.text_store.listable():
137
return self._revision_store.all_revision_ids(self.get_transaction())
138
result = self._all_possible_ids()
139
return self._eliminate_revisions_not_present(result)
141
def break_lock(self):
142
"""Break a lock if one is present from another instance.
144
Uses the ui factory to ask for confirmation if the lock may be from
147
self.control_files.break_lock()
150
def _eliminate_revisions_not_present(self, revision_ids):
151
"""Check every revision id in revision_ids to see if we have it.
153
Returns a set of the present revisions.
156
for id in revision_ids:
157
if self.has_revision(id):
162
def create(a_bzrdir):
163
"""Construct the current default format repository in a_bzrdir."""
164
return RepositoryFormat.get_default_format().initialize(a_bzrdir)
166
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
167
"""instantiate a Repository.
169
:param _format: The format of the repository on disk.
170
:param a_bzrdir: The BzrDir of the repository.
172
In the future we will have a single api for all stores for
173
getting file texts, inventories and revisions, then
174
this construct will accept instances of those things.
176
super(Repository, self).__init__()
177
self._format = _format
178
# the following are part of the public API for Repository:
179
self.bzrdir = a_bzrdir
180
self.control_files = control_files
181
self._revision_store = _revision_store
182
self.text_store = text_store
183
# backwards compatibility
184
self.weave_store = text_store
185
# not right yet - should be more semantically clear ?
187
self.control_store = control_store
188
self.control_weaves = control_store
189
# TODO: make sure to construct the right store classes, etc, depending
190
# on whether escaping is required.
193
return '%s(%r)' % (self.__class__.__name__,
194
self.bzrdir.transport.base)
197
return self.control_files.is_locked()
199
def lock_write(self):
200
self.control_files.lock_write()
203
self.control_files.lock_read()
205
def get_physical_lock_status(self):
206
return self.control_files.get_physical_lock_status()
209
def missing_revision_ids(self, other, revision_id=None):
210
"""Return the revision ids that other has that this does not.
212
These are returned in topological order.
214
revision_id: only return revision ids included by revision_id.
216
return InterRepository.get(other, self).missing_revision_ids(revision_id)
220
"""Open the repository rooted at base.
222
For instance, if the repository is at URL/.bzr/repository,
223
Repository.open(URL) -> a Repository instance.
225
control = bzrdir.BzrDir.open(base)
226
return control.open_repository()
228
def copy_content_into(self, destination, revision_id=None, basis=None):
229
"""Make a complete copy of the content in self into destination.
231
This is a destructive operation! Do not use it on existing
234
return InterRepository.get(self, destination).copy_content(revision_id, basis)
236
def fetch(self, source, revision_id=None, pb=None):
237
"""Fetch the content required to construct revision_id from source.
239
If revision_id is None all content is copied.
241
return InterRepository.get(source, self).fetch(revision_id=revision_id,
244
def get_commit_builder(self, branch, parents, config, timestamp=None,
245
timezone=None, committer=None, revprops=None,
247
"""Obtain a CommitBuilder for this repository.
249
:param branch: Branch to commit to.
250
:param parents: Revision ids of the parents of the new revision.
251
:param config: Configuration to use.
252
:param timestamp: Optional timestamp recorded for commit.
253
:param timezone: Optional timezone for timestamp.
254
:param committer: Optional committer to set for commit.
255
:param revprops: Optional dictionary of revision properties.
256
:param revision_id: Optional revision id.
258
return CommitBuilder(self, parents, config, timestamp, timezone,
259
committer, revprops, revision_id)
262
self.control_files.unlock()
265
def clone(self, a_bzrdir, revision_id=None, basis=None):
266
"""Clone this repository into a_bzrdir using the current format.
268
Currently no check is made that the format of this repository and
269
the bzrdir format are compatible. FIXME RBC 20060201.
271
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
272
# use target default format.
273
result = a_bzrdir.create_repository()
274
# FIXME RBC 20060209 split out the repository type to avoid this check ?
275
elif isinstance(a_bzrdir._format,
276
(bzrdir.BzrDirFormat4,
277
bzrdir.BzrDirFormat5,
278
bzrdir.BzrDirFormat6)):
279
result = a_bzrdir.open_repository()
281
result = self._format.initialize(a_bzrdir, shared=self.is_shared())
282
self.copy_content_into(result, revision_id, basis)
286
def has_revision(self, revision_id):
287
"""True if this repository has a copy of the revision."""
288
return self._revision_store.has_revision_id(revision_id,
289
self.get_transaction())
292
def get_revision_reconcile(self, revision_id):
293
"""'reconcile' helper routine that allows access to a revision always.
295
This variant of get_revision does not cross check the weave graph
296
against the revision one as get_revision does: but it should only
297
be used by reconcile, or reconcile-alike commands that are correcting
298
or testing the revision graph.
300
if not revision_id or not isinstance(revision_id, basestring):
301
raise InvalidRevisionId(revision_id=revision_id, branch=self)
302
return self._revision_store.get_revisions([revision_id],
303
self.get_transaction())[0]
305
def get_revisions(self, revision_ids):
306
return self._revision_store.get_revisions(revision_ids,
307
self.get_transaction())
310
def get_revision_xml(self, revision_id):
311
rev = self.get_revision(revision_id)
313
# the current serializer..
314
self._revision_store._serializer.write_revision(rev, rev_tmp)
316
return rev_tmp.getvalue()
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)
334
def get_deltas_for_revisions(self, revisions):
335
"""Produce a generator of revision deltas.
337
Note that the input is a sequence of REVISIONS, not revision_ids.
338
Trees will be held in memory until the generator exits.
339
Each delta is relative to the revision's lefthand predecessor.
341
required_trees = set()
342
for revision in revisions:
343
required_trees.add(revision.revision_id)
344
required_trees.update(revision.parent_ids[:1])
345
trees = dict((t.get_revision_id(), t) for
346
t in self.revision_trees(required_trees))
347
for revision in revisions:
348
if not revision.parent_ids:
349
old_tree = self.revision_tree(None)
351
old_tree = trees[revision.parent_ids[0]]
352
yield trees[revision.revision_id].changes_from(old_tree)
355
def get_revision_delta(self, revision_id):
356
"""Return the delta for one revision.
358
The delta is relative to the left-hand predecessor of the
361
r = self.get_revision(revision_id)
362
return list(self.get_deltas_for_revisions([r]))[0]
364
def _check_revision_parents(self, revision, inventory):
365
"""Private to Repository and Fetch.
367
This checks the parentage of revision in an inventory weave for
368
consistency and is only applicable to inventory-weave-for-ancestry
369
using repository formats & fetchers.
371
weave_parents = inventory.get_parents(revision.revision_id)
372
weave_names = inventory.versions()
373
for parent_id in revision.parent_ids:
374
if parent_id in weave_names:
375
# this parent must not be a ghost.
376
if not parent_id in weave_parents:
378
raise errors.CorruptRepository(self)
381
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
382
signature = gpg_strategy.sign(plaintext)
383
self._revision_store.add_revision_signature_text(revision_id,
385
self.get_transaction())
387
def fileids_altered_by_revision_ids(self, revision_ids):
388
"""Find the file ids and versions affected by revisions.
390
:param revisions: an iterable containing revision ids.
391
:return: a dictionary mapping altered file-ids to an iterable of
392
revision_ids. Each altered file-ids has the exact revision_ids that
393
altered it listed explicitly.
395
assert isinstance(self._format, (RepositoryFormat5,
398
RepositoryFormatKnit1)), \
399
("fileids_altered_by_revision_ids only supported for branches "
400
"which store inventory as unnested xml, not on %r" % self)
401
selected_revision_ids = set(revision_ids)
402
w = self.get_inventory_weave()
405
# this code needs to read every new line in every inventory for the
406
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
407
# not present in one of those inventories is unnecessary but not
408
# harmful because we are filtering by the revision id marker in the
409
# inventory lines : we only select file ids altered in one of those
410
# revisions. We don't need to see all lines in the inventory because
411
# only those added in an inventory in rev X can contain a revision=X
413
for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
414
start = line.find('file_id="')+9
415
if start < 9: continue
416
end = line.find('"', start)
418
file_id = _unescape_xml(line[start:end])
420
start = line.find('revision="')+10
421
if start < 10: continue
422
end = line.find('"', start)
424
revision_id = _unescape_xml(line[start:end])
425
if revision_id in selected_revision_ids:
426
result.setdefault(file_id, set()).add(revision_id)
430
def get_inventory_weave(self):
431
return self.control_weaves.get_weave('inventory',
432
self.get_transaction())
435
def get_inventory(self, revision_id):
436
"""Get Inventory object by hash."""
437
return self.deserialise_inventory(
438
revision_id, self.get_inventory_xml(revision_id))
440
def deserialise_inventory(self, revision_id, xml):
441
"""Transform the xml into an inventory object.
443
:param revision_id: The expected revision id of the inventory.
444
:param xml: A serialised inventory.
446
result = xml5.serializer_v5.read_inventory_from_string(xml)
447
result.root.revision = revision_id
451
def get_inventory_xml(self, revision_id):
452
"""Get inventory XML as a file object."""
454
assert isinstance(revision_id, basestring), type(revision_id)
455
iw = self.get_inventory_weave()
456
return iw.get_text(revision_id)
458
raise errors.HistoryMissing(self, 'inventory', revision_id)
461
def get_inventory_sha1(self, revision_id):
462
"""Return the sha1 hash of the inventory entry
464
return self.get_revision(revision_id).inventory_sha1
467
def get_revision_graph(self, revision_id=None):
468
"""Return a dictionary containing the revision graph.
470
:param revision_id: The revision_id to get a graph from. If None, then
471
the entire revision graph is returned. This is a deprecated mode of
472
operation and will be removed in the future.
473
:return: a dictionary of revision_id->revision_parents_list.
475
# special case NULL_REVISION
476
if revision_id == NULL_REVISION:
478
weave = self.get_inventory_weave()
479
all_revisions = self._eliminate_revisions_not_present(weave.versions())
480
entire_graph = dict([(node, weave.get_parents(node)) for
481
node in all_revisions])
482
if revision_id is None:
484
elif revision_id not in entire_graph:
485
raise errors.NoSuchRevision(self, revision_id)
487
# add what can be reached from revision_id
489
pending = set([revision_id])
490
while len(pending) > 0:
492
result[node] = entire_graph[node]
493
for revision_id in result[node]:
494
if revision_id not in result:
495
pending.add(revision_id)
499
def get_revision_graph_with_ghosts(self, revision_ids=None):
500
"""Return a graph of the revisions with ghosts marked as applicable.
502
:param revision_ids: an iterable of revisions to graph or None for all.
503
:return: a Graph object with the graph reachable from revision_ids.
507
pending = set(self.all_revision_ids())
510
pending = set(revision_ids)
511
# special case NULL_REVISION
512
if NULL_REVISION in pending:
513
pending.remove(NULL_REVISION)
514
required = set(pending)
517
revision_id = pending.pop()
519
rev = self.get_revision(revision_id)
520
except errors.NoSuchRevision:
521
if revision_id in required:
524
result.add_ghost(revision_id)
526
for parent_id in rev.parent_ids:
527
# is this queued or done ?
528
if (parent_id not in pending and
529
parent_id not in done):
531
pending.add(parent_id)
532
result.add_node(revision_id, rev.parent_ids)
533
done.add(revision_id)
537
def get_revision_inventory(self, revision_id):
538
"""Return inventory of a past revision."""
539
# TODO: Unify this with get_inventory()
540
# bzr 0.0.6 and later imposes the constraint that the inventory_id
541
# must be the same as its revision, so this is trivial.
542
if revision_id is None:
543
# This does not make sense: if there is no revision,
544
# then it is the current tree inventory surely ?!
545
# and thus get_root_id() is something that looks at the last
546
# commit on the branch, and the get_root_id is an inventory check.
547
raise NotImplementedError
548
# return Inventory(self.get_root_id())
550
return self.get_inventory(revision_id)
554
"""Return True if this repository is flagged as a shared repository."""
555
raise NotImplementedError(self.is_shared)
558
def reconcile(self, other=None, thorough=False):
559
"""Reconcile this repository."""
560
from bzrlib.reconcile import RepoReconciler
561
reconciler = RepoReconciler(self, thorough=thorough)
562
reconciler.reconcile()
566
def revision_tree(self, revision_id):
567
"""Return Tree for a revision on this branch.
569
`revision_id` may be None for the empty tree revision.
571
# TODO: refactor this to use an existing revision object
572
# so we don't need to read it in twice.
573
if revision_id is None or revision_id == NULL_REVISION:
574
return RevisionTree(self, Inventory(), NULL_REVISION)
576
inv = self.get_revision_inventory(revision_id)
577
return RevisionTree(self, inv, revision_id)
580
def revision_trees(self, revision_ids):
581
"""Return Tree for a revision on this branch.
583
`revision_id` may not be None or 'null:'"""
584
assert None not in revision_ids
585
assert NULL_REVISION not in revision_ids
586
texts = self.get_inventory_weave().get_texts(revision_ids)
587
for text, revision_id in zip(texts, revision_ids):
588
inv = self.deserialise_inventory(revision_id, text)
589
yield RevisionTree(self, inv, revision_id)
592
def get_ancestry(self, revision_id):
593
"""Return a list of revision-ids integrated by a revision.
595
The first element of the list is always None, indicating the origin
596
revision. This might change when we have history horizons, or
597
perhaps we should have a new API.
599
This is topologically sorted.
601
if revision_id is None:
603
if not self.has_revision(revision_id):
604
raise errors.NoSuchRevision(self, revision_id)
605
w = self.get_inventory_weave()
606
candidates = w.get_ancestry(revision_id)
607
return [None] + candidates # self._eliminate_revisions_not_present(candidates)
610
def print_file(self, file, revision_id):
611
"""Print `file` to stdout.
613
FIXME RBC 20060125 as John Meinel points out this is a bad api
614
- it writes to stdout, it assumes that that is valid etc. Fix
615
by creating a new more flexible convenience function.
617
tree = self.revision_tree(revision_id)
618
# use inventory as it was in that revision
619
file_id = tree.inventory.path2id(file)
621
# TODO: jam 20060427 Write a test for this code path
622
# it had a bug in it, and was raising the wrong
624
raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
625
tree.print_file(file_id)
627
def get_transaction(self):
628
return self.control_files.get_transaction()
630
def revision_parents(self, revid):
631
return self.get_inventory_weave().parent_names(revid)
634
def set_make_working_trees(self, new_value):
635
"""Set the policy flag for making working trees when creating branches.
637
This only applies to branches that use this repository.
639
The default is 'True'.
640
:param new_value: True to restore the default, False to disable making
643
raise NotImplementedError(self.set_make_working_trees)
645
def make_working_trees(self):
646
"""Returns the policy for making working trees on new branches."""
647
raise NotImplementedError(self.make_working_trees)
650
def sign_revision(self, revision_id, gpg_strategy):
651
plaintext = Testament.from_revision(self, revision_id).as_short_text()
652
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
655
def has_signature_for_revision_id(self, revision_id):
656
"""Query for a revision signature for revision_id in the repository."""
657
return self._revision_store.has_signature(revision_id,
658
self.get_transaction())
661
def get_signature_text(self, revision_id):
662
"""Return the text for a signature."""
663
return self._revision_store.get_signature_text(revision_id,
664
self.get_transaction())
667
def check(self, revision_ids):
668
"""Check consistency of all history of given revision_ids.
670
Different repository implementations should override _check().
672
:param revision_ids: A non-empty list of revision_ids whose ancestry
673
will be checked. Typically the last revision_id of a branch.
676
raise ValueError("revision_ids must be non-empty in %s.check"
678
return self._check(revision_ids)
680
def _check(self, revision_ids):
681
result = check.Check(self)
686
class AllInOneRepository(Repository):
687
"""Legacy support - the repository behaviour for all-in-one branches."""
689
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
690
# we reuse one control files instance.
691
dir_mode = a_bzrdir._control_files._dir_mode
692
file_mode = a_bzrdir._control_files._file_mode
694
def get_store(name, compressed=True, prefixed=False):
695
# FIXME: This approach of assuming stores are all entirely compressed
696
# or entirely uncompressed is tidy, but breaks upgrade from
697
# some existing branches where there's a mixture; we probably
698
# still want the option to look for both.
699
relpath = a_bzrdir._control_files._escape(name)
700
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
701
prefixed=prefixed, compressed=compressed,
704
#if self._transport.should_cache():
705
# cache_path = os.path.join(self.cache_root, name)
706
# os.mkdir(cache_path)
707
# store = bzrlib.store.CachedStore(store, cache_path)
710
# not broken out yet because the controlweaves|inventory_store
711
# and text_store | weave_store bits are still different.
712
if isinstance(_format, RepositoryFormat4):
713
# cannot remove these - there is still no consistent api
714
# which allows access to this old info.
715
self.inventory_store = get_store('inventory-store')
716
text_store = get_store('text-store')
717
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
721
"""AllInOne repositories cannot be shared."""
725
def set_make_working_trees(self, new_value):
726
"""Set the policy flag for making working trees when creating branches.
728
This only applies to branches that use this repository.
730
The default is 'True'.
731
:param new_value: True to restore the default, False to disable making
734
raise NotImplementedError(self.set_make_working_trees)
736
def make_working_trees(self):
737
"""Returns the policy for making working trees on new branches."""
741
def install_revision(repository, rev, revision_tree):
742
"""Install all revision data into a repository."""
745
for p_id in rev.parent_ids:
746
if repository.has_revision(p_id):
747
present_parents.append(p_id)
748
parent_trees[p_id] = repository.revision_tree(p_id)
750
parent_trees[p_id] = repository.revision_tree(None)
752
inv = revision_tree.inventory
754
# backwards compatability hack: skip the root id.
755
entries = inv.iter_entries()
757
# Add the texts that are not already present
758
for path, ie in entries:
759
w = repository.weave_store.get_weave_or_empty(ie.file_id,
760
repository.get_transaction())
761
if ie.revision not in w:
763
# FIXME: TODO: The following loop *may* be overlapping/duplicate
764
# with InventoryEntry.find_previous_heads(). if it is, then there
765
# is a latent bug here where the parents may have ancestors of each
767
for revision, tree in parent_trees.iteritems():
768
if ie.file_id not in tree:
770
parent_id = tree.inventory[ie.file_id].revision
771
if parent_id in text_parents:
773
text_parents.append(parent_id)
775
vfile = repository.weave_store.get_weave_or_empty(ie.file_id,
776
repository.get_transaction())
777
lines = revision_tree.get_file(ie.file_id).readlines()
778
vfile.add_lines(rev.revision_id, text_parents, lines)
780
# install the inventory
781
repository.add_inventory(rev.revision_id, inv, present_parents)
782
except errors.RevisionAlreadyPresent:
784
repository.add_revision(rev.revision_id, rev, inv)
787
class MetaDirRepository(Repository):
788
"""Repositories in the new meta-dir layout."""
790
def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
791
super(MetaDirRepository, self).__init__(_format,
798
dir_mode = self.control_files._dir_mode
799
file_mode = self.control_files._file_mode
803
"""Return True if this repository is flagged as a shared repository."""
804
return self.control_files._transport.has('shared-storage')
807
def set_make_working_trees(self, new_value):
808
"""Set the policy flag for making working trees when creating branches.
810
This only applies to branches that use this repository.
812
The default is 'True'.
813
:param new_value: True to restore the default, False to disable making
818
self.control_files._transport.delete('no-working-trees')
819
except errors.NoSuchFile:
822
self.control_files.put_utf8('no-working-trees', '')
824
def make_working_trees(self):
825
"""Returns the policy for making working trees on new branches."""
826
return not self.control_files._transport.has('no-working-trees')
829
class KnitRepository(MetaDirRepository):
830
"""Knit format repository."""
832
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
833
inv_vf.add_lines_with_ghosts(revid, parents, lines)
836
def _all_revision_ids(self):
837
"""See Repository.all_revision_ids()."""
838
# Knits get the revision graph from the index of the revision knit, so
839
# it's always possible even if they're on an unlistable transport.
840
return self._revision_store.all_revision_ids(self.get_transaction())
842
def fileid_involved_between_revs(self, from_revid, to_revid):
843
"""Find file_id(s) which are involved in the changes between revisions.
845
This determines the set of revisions which are involved, and then
846
finds all file ids affected by those revisions.
848
vf = self._get_revision_vf()
849
from_set = set(vf.get_ancestry(from_revid))
850
to_set = set(vf.get_ancestry(to_revid))
851
changed = to_set.difference(from_set)
852
return self._fileid_involved_by_set(changed)
854
def fileid_involved(self, last_revid=None):
855
"""Find all file_ids modified in the ancestry of last_revid.
857
:param last_revid: If None, last_revision() will be used.
860
changed = set(self.all_revision_ids())
862
changed = set(self.get_ancestry(last_revid))
865
return self._fileid_involved_by_set(changed)
868
def get_ancestry(self, revision_id):
869
"""Return a list of revision-ids integrated by a revision.
871
This is topologically sorted.
873
if revision_id is None:
875
vf = self._get_revision_vf()
877
return [None] + vf.get_ancestry(revision_id)
878
except errors.RevisionNotPresent:
879
raise errors.NoSuchRevision(self, revision_id)
882
def get_revision(self, revision_id):
883
"""Return the Revision object for a named revision"""
884
return self.get_revision_reconcile(revision_id)
887
def get_revision_graph(self, revision_id=None):
888
"""Return a dictionary containing the revision graph.
890
:param revision_id: The revision_id to get a graph from. If None, then
891
the entire revision graph is returned. This is a deprecated mode of
892
operation and will be removed in the future.
893
:return: a dictionary of revision_id->revision_parents_list.
895
# special case NULL_REVISION
896
if revision_id == NULL_REVISION:
898
weave = self._get_revision_vf()
899
entire_graph = weave.get_graph()
900
if revision_id is None:
901
return weave.get_graph()
902
elif revision_id not in weave:
903
raise errors.NoSuchRevision(self, revision_id)
905
# add what can be reached from revision_id
907
pending = set([revision_id])
908
while len(pending) > 0:
910
result[node] = weave.get_parents(node)
911
for revision_id in result[node]:
912
if revision_id not in result:
913
pending.add(revision_id)
917
def get_revision_graph_with_ghosts(self, revision_ids=None):
918
"""Return a graph of the revisions with ghosts marked as applicable.
920
:param revision_ids: an iterable of revisions to graph or None for all.
921
:return: a Graph object with the graph reachable from revision_ids.
924
vf = self._get_revision_vf()
925
versions = set(vf.versions())
927
pending = set(self.all_revision_ids())
930
pending = set(revision_ids)
931
# special case NULL_REVISION
932
if NULL_REVISION in pending:
933
pending.remove(NULL_REVISION)
934
required = set(pending)
937
revision_id = pending.pop()
938
if not revision_id in versions:
939
if revision_id in required:
940
raise errors.NoSuchRevision(self, revision_id)
942
result.add_ghost(revision_id)
943
# mark it as done so we don't try for it again.
944
done.add(revision_id)
946
parent_ids = vf.get_parents_with_ghosts(revision_id)
947
for parent_id in parent_ids:
948
# is this queued or done ?
949
if (parent_id not in pending and
950
parent_id not in done):
952
pending.add(parent_id)
953
result.add_node(revision_id, parent_ids)
954
done.add(revision_id)
957
def _get_revision_vf(self):
958
""":return: a versioned file containing the revisions."""
959
vf = self._revision_store.get_revision_file(self.get_transaction())
963
def reconcile(self, other=None, thorough=False):
964
"""Reconcile this repository."""
965
from bzrlib.reconcile import KnitReconciler
966
reconciler = KnitReconciler(self, thorough=thorough)
967
reconciler.reconcile()
970
def revision_parents(self, revision_id):
971
return self._get_revision_vf().get_parents(revision_id)
974
class RepositoryFormat(object):
975
"""A repository format.
977
Formats provide three things:
978
* An initialization routine to construct repository data on disk.
979
* a format string which is used when the BzrDir supports versioned
981
* an open routine which returns a Repository instance.
983
Formats are placed in an dict by their format string for reference
984
during opening. These should be subclasses of RepositoryFormat
987
Once a format is deprecated, just deprecate the initialize and open
988
methods on the format class. Do not deprecate the object, as the
989
object will be created every system load.
991
Common instance attributes:
992
_matchingbzrdir - the bzrdir format that the repository format was
993
originally written to work with. This can be used if manually
994
constructing a bzrdir and repository, or more commonly for test suite
998
_default_format = None
999
"""The default format used for new repositories."""
1002
"""The known formats."""
1005
def find_format(klass, a_bzrdir):
1006
"""Return the format for the repository object in a_bzrdir."""
1008
transport = a_bzrdir.get_repository_transport(None)
1009
format_string = transport.get("format").read()
1010
return klass._formats[format_string]
1011
except errors.NoSuchFile:
1012
raise errors.NoRepositoryPresent(a_bzrdir)
1014
raise errors.UnknownFormatError(format=format_string)
1016
def _get_control_store(self, repo_transport, control_files):
1017
"""Return the control store for this repository."""
1018
raise NotImplementedError(self._get_control_store)
1021
def get_default_format(klass):
1022
"""Return the current default format."""
1023
return klass._default_format
1025
def get_format_string(self):
1026
"""Return the ASCII format string that identifies this format.
1028
Note that in pre format ?? repositories the format string is
1029
not permitted nor written to disk.
1031
raise NotImplementedError(self.get_format_string)
1033
def get_format_description(self):
1034
"""Return the short description for this format."""
1035
raise NotImplementedError(self.get_format_description)
1037
def _get_revision_store(self, repo_transport, control_files):
1038
"""Return the revision store object for this a_bzrdir."""
1039
raise NotImplementedError(self._get_revision_store)
1041
def _get_text_rev_store(self,
1048
"""Common logic for getting a revision store for a repository.
1050
see self._get_revision_store for the subclass-overridable method to
1051
get the store for a repository.
1053
from bzrlib.store.revision.text import TextRevisionStore
1054
dir_mode = control_files._dir_mode
1055
file_mode = control_files._file_mode
1056
text_store =TextStore(transport.clone(name),
1058
compressed=compressed,
1060
file_mode=file_mode)
1061
_revision_store = TextRevisionStore(text_store, serializer)
1062
return _revision_store
1064
def _get_versioned_file_store(self,
1069
versionedfile_class=WeaveFile,
1071
weave_transport = control_files._transport.clone(name)
1072
dir_mode = control_files._dir_mode
1073
file_mode = control_files._file_mode
1074
return VersionedFileStore(weave_transport, prefixed=prefixed,
1076
file_mode=file_mode,
1077
versionedfile_class=versionedfile_class,
1080
def initialize(self, a_bzrdir, shared=False):
1081
"""Initialize a repository of this format in a_bzrdir.
1083
:param a_bzrdir: The bzrdir to put the new repository in it.
1084
:param shared: The repository should be initialized as a sharable one.
1086
This may raise UninitializableFormat if shared repository are not
1087
compatible the a_bzrdir.
1090
def is_supported(self):
1091
"""Is this format supported?
1093
Supported formats must be initializable and openable.
1094
Unsupported formats may not support initialization or committing or
1095
some other features depending on the reason for not being supported.
1099
def open(self, a_bzrdir, _found=False):
1100
"""Return an instance of this format for the bzrdir a_bzrdir.
1102
_found is a private parameter, do not use it.
1104
raise NotImplementedError(self.open)
1107
def register_format(klass, format):
1108
klass._formats[format.get_format_string()] = format
1111
def set_default_format(klass, format):
1112
klass._default_format = format
1115
def unregister_format(klass, format):
1116
assert klass._formats[format.get_format_string()] is format
1117
del klass._formats[format.get_format_string()]
1120
class PreSplitOutRepositoryFormat(RepositoryFormat):
1121
"""Base class for the pre split out repository formats."""
1123
def initialize(self, a_bzrdir, shared=False, _internal=False):
1124
"""Create a weave repository.
1126
TODO: when creating split out bzr branch formats, move this to a common
1127
base for Format5, Format6. or something like that.
1129
from bzrlib.weavefile import write_weave_v5
1130
from bzrlib.weave import Weave
1133
raise errors.IncompatibleFormat(self, a_bzrdir._format)
1136
# always initialized when the bzrdir is.
1137
return self.open(a_bzrdir, _found=True)
1139
# Create an empty weave
1141
write_weave_v5(Weave(), sio)
1142
empty_weave = sio.getvalue()
1144
mutter('creating repository in %s.', a_bzrdir.transport.base)
1145
dirs = ['revision-store', 'weaves']
1146
files = [('inventory.weave', StringIO(empty_weave)),
1149
# FIXME: RBC 20060125 don't peek under the covers
1150
# NB: no need to escape relative paths that are url safe.
1151
control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
1153
control_files.create_lock()
1154
control_files.lock_write()
1155
control_files._transport.mkdir_multi(dirs,
1156
mode=control_files._dir_mode)
1158
for file, content in files:
1159
control_files.put(file, content)
1161
control_files.unlock()
1162
return self.open(a_bzrdir, _found=True)
1164
def _get_control_store(self, repo_transport, control_files):
1165
"""Return the control store for this repository."""
1166
return self._get_versioned_file_store('',
1171
def _get_text_store(self, transport, control_files):
1172
"""Get a store for file texts for this format."""
1173
raise NotImplementedError(self._get_text_store)
1175
def open(self, a_bzrdir, _found=False):
1176
"""See RepositoryFormat.open()."""
1178
# we are being called directly and must probe.
1179
raise NotImplementedError
1181
repo_transport = a_bzrdir.get_repository_transport(None)
1182
control_files = a_bzrdir._control_files
1183
text_store = self._get_text_store(repo_transport, control_files)
1184
control_store = self._get_control_store(repo_transport, control_files)
1185
_revision_store = self._get_revision_store(repo_transport, control_files)
1186
return AllInOneRepository(_format=self,
1188
_revision_store=_revision_store,
1189
control_store=control_store,
1190
text_store=text_store)
1193
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1194
"""Bzr repository format 4.
1196
This repository format has:
1198
- TextStores for texts, inventories,revisions.
1200
This format is deprecated: it indexes texts using a text id which is
1201
removed in format 5; initialization and write support for this format
1206
super(RepositoryFormat4, self).__init__()
1207
self._matchingbzrdir = bzrdir.BzrDirFormat4()
1209
def get_format_description(self):
1210
"""See RepositoryFormat.get_format_description()."""
1211
return "Repository format 4"
1213
def initialize(self, url, shared=False, _internal=False):
1214
"""Format 4 branches cannot be created."""
1215
raise errors.UninitializableFormat(self)
1217
def is_supported(self):
1218
"""Format 4 is not supported.
1220
It is not supported because the model changed from 4 to 5 and the
1221
conversion logic is expensive - so doing it on the fly was not
1226
def _get_control_store(self, repo_transport, control_files):
1227
"""Format 4 repositories have no formal control store at this point.
1229
This will cause any control-file-needing apis to fail - this is desired.
1233
def _get_revision_store(self, repo_transport, control_files):
1234
"""See RepositoryFormat._get_revision_store()."""
1235
from bzrlib.xml4 import serializer_v4
1236
return self._get_text_rev_store(repo_transport,
1239
serializer=serializer_v4)
1241
def _get_text_store(self, transport, control_files):
1242
"""See RepositoryFormat._get_text_store()."""
1245
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1246
"""Bzr control format 5.
1248
This repository format has:
1249
- weaves for file texts and inventory
1251
- TextStores for revisions and signatures.
1255
super(RepositoryFormat5, self).__init__()
1256
self._matchingbzrdir = bzrdir.BzrDirFormat5()
1258
def get_format_description(self):
1259
"""See RepositoryFormat.get_format_description()."""
1260
return "Weave repository format 5"
1262
def _get_revision_store(self, repo_transport, control_files):
1263
"""See RepositoryFormat._get_revision_store()."""
1264
"""Return the revision store object for this a_bzrdir."""
1265
return self._get_text_rev_store(repo_transport,
1270
def _get_text_store(self, transport, control_files):
1271
"""See RepositoryFormat._get_text_store()."""
1272
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1275
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1276
"""Bzr control format 6.
1278
This repository format has:
1279
- weaves for file texts and inventory
1280
- hash subdirectory based stores.
1281
- TextStores for revisions and signatures.
1285
super(RepositoryFormat6, self).__init__()
1286
self._matchingbzrdir = bzrdir.BzrDirFormat6()
1288
def get_format_description(self):
1289
"""See RepositoryFormat.get_format_description()."""
1290
return "Weave repository format 6"
1292
def _get_revision_store(self, repo_transport, control_files):
1293
"""See RepositoryFormat._get_revision_store()."""
1294
return self._get_text_rev_store(repo_transport,
1300
def _get_text_store(self, transport, control_files):
1301
"""See RepositoryFormat._get_text_store()."""
1302
return self._get_versioned_file_store('weaves', transport, control_files)
1305
class MetaDirRepositoryFormat(RepositoryFormat):
1306
"""Common base class for the new repositories using the metadir layout."""
1309
super(MetaDirRepositoryFormat, self).__init__()
1310
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1312
def _create_control_files(self, a_bzrdir):
1313
"""Create the required files and the initial control_files object."""
1314
# FIXME: RBC 20060125 don't peek under the covers
1315
# NB: no need to escape relative paths that are url safe.
1316
repository_transport = a_bzrdir.get_repository_transport(self)
1317
control_files = LockableFiles(repository_transport, 'lock', LockDir)
1318
control_files.create_lock()
1319
return control_files
1321
def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1322
"""Upload the initial blank content."""
1323
control_files = self._create_control_files(a_bzrdir)
1324
control_files.lock_write()
1326
control_files._transport.mkdir_multi(dirs,
1327
mode=control_files._dir_mode)
1328
for file, content in files:
1329
control_files.put(file, content)
1330
for file, content in utf8_files:
1331
control_files.put_utf8(file, content)
1333
control_files.put_utf8('shared-storage', '')
1335
control_files.unlock()
1338
class RepositoryFormat7(MetaDirRepositoryFormat):
1339
"""Bzr repository 7.
1341
This repository format has:
1342
- weaves for file texts and inventory
1343
- hash subdirectory based stores.
1344
- TextStores for revisions and signatures.
1345
- a format marker of its own
1346
- an optional 'shared-storage' flag
1347
- an optional 'no-working-trees' flag
1350
def _get_control_store(self, repo_transport, control_files):
1351
"""Return the control store for this repository."""
1352
return self._get_versioned_file_store('',
1357
def get_format_string(self):
1358
"""See RepositoryFormat.get_format_string()."""
1359
return "Bazaar-NG Repository format 7"
1361
def get_format_description(self):
1362
"""See RepositoryFormat.get_format_description()."""
1363
return "Weave repository format 7"
1365
def _get_revision_store(self, repo_transport, control_files):
1366
"""See RepositoryFormat._get_revision_store()."""
1367
return self._get_text_rev_store(repo_transport,
1374
def _get_text_store(self, transport, control_files):
1375
"""See RepositoryFormat._get_text_store()."""
1376
return self._get_versioned_file_store('weaves',
1380
def initialize(self, a_bzrdir, shared=False):
1381
"""Create a weave repository.
1383
:param shared: If true the repository will be initialized as a shared
1386
from bzrlib.weavefile import write_weave_v5
1387
from bzrlib.weave import Weave
1389
# Create an empty weave
1391
write_weave_v5(Weave(), sio)
1392
empty_weave = sio.getvalue()
1394
mutter('creating repository in %s.', a_bzrdir.transport.base)
1395
dirs = ['revision-store', 'weaves']
1396
files = [('inventory.weave', StringIO(empty_weave)),
1398
utf8_files = [('format', self.get_format_string())]
1400
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1401
return self.open(a_bzrdir=a_bzrdir, _found=True)
1403
def open(self, a_bzrdir, _found=False, _override_transport=None):
1404
"""See RepositoryFormat.open().
1406
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1407
repository at a slightly different url
1408
than normal. I.e. during 'upgrade'.
1411
format = RepositoryFormat.find_format(a_bzrdir)
1412
assert format.__class__ == self.__class__
1413
if _override_transport is not None:
1414
repo_transport = _override_transport
1416
repo_transport = a_bzrdir.get_repository_transport(None)
1417
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1418
text_store = self._get_text_store(repo_transport, control_files)
1419
control_store = self._get_control_store(repo_transport, control_files)
1420
_revision_store = self._get_revision_store(repo_transport, control_files)
1421
return MetaDirRepository(_format=self,
1423
control_files=control_files,
1424
_revision_store=_revision_store,
1425
control_store=control_store,
1426
text_store=text_store)
1429
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1430
"""Bzr repository knit format 1.
1432
This repository format has:
1433
- knits for file texts and inventory
1434
- hash subdirectory based stores.
1435
- knits for revisions and signatures
1436
- TextStores for revisions and signatures.
1437
- a format marker of its own
1438
- an optional 'shared-storage' flag
1439
- an optional 'no-working-trees' flag
1442
This format was introduced in bzr 0.8.
1445
def _get_control_store(self, repo_transport, control_files):
1446
"""Return the control store for this repository."""
1447
return VersionedFileStore(
1450
file_mode=control_files._file_mode,
1451
versionedfile_class=KnitVersionedFile,
1452
versionedfile_kwargs={'factory':KnitPlainFactory()},
1455
def get_format_string(self):
1456
"""See RepositoryFormat.get_format_string()."""
1457
return "Bazaar-NG Knit Repository Format 1"
1459
def get_format_description(self):
1460
"""See RepositoryFormat.get_format_description()."""
1461
return "Knit repository format 1"
1463
def _get_revision_store(self, repo_transport, control_files):
1464
"""See RepositoryFormat._get_revision_store()."""
1465
from bzrlib.store.revision.knit import KnitRevisionStore
1466
versioned_file_store = VersionedFileStore(
1468
file_mode=control_files._file_mode,
1471
versionedfile_class=KnitVersionedFile,
1472
versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
1475
return KnitRevisionStore(versioned_file_store)
1477
def _get_text_store(self, transport, control_files):
1478
"""See RepositoryFormat._get_text_store()."""
1479
return self._get_versioned_file_store('knits',
1482
versionedfile_class=KnitVersionedFile,
1485
def initialize(self, a_bzrdir, shared=False):
1486
"""Create a knit format 1 repository.
1488
:param a_bzrdir: bzrdir to contain the new repository; must already
1490
:param shared: If true the repository will be initialized as a shared
1493
mutter('creating repository in %s.', a_bzrdir.transport.base)
1494
dirs = ['revision-store', 'knits']
1496
utf8_files = [('format', self.get_format_string())]
1498
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1499
repo_transport = a_bzrdir.get_repository_transport(None)
1500
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1501
control_store = self._get_control_store(repo_transport, control_files)
1502
transaction = transactions.WriteTransaction()
1503
# trigger a write of the inventory store.
1504
control_store.get_weave_or_empty('inventory', transaction)
1505
_revision_store = self._get_revision_store(repo_transport, control_files)
1506
_revision_store.has_revision_id('A', transaction)
1507
_revision_store.get_signature_file(transaction)
1508
return self.open(a_bzrdir=a_bzrdir, _found=True)
1510
def open(self, a_bzrdir, _found=False, _override_transport=None):
1511
"""See RepositoryFormat.open().
1513
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1514
repository at a slightly different url
1515
than normal. I.e. during 'upgrade'.
1518
format = RepositoryFormat.find_format(a_bzrdir)
1519
assert format.__class__ == self.__class__
1520
if _override_transport is not None:
1521
repo_transport = _override_transport
1523
repo_transport = a_bzrdir.get_repository_transport(None)
1524
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1525
text_store = self._get_text_store(repo_transport, control_files)
1526
control_store = self._get_control_store(repo_transport, control_files)
1527
_revision_store = self._get_revision_store(repo_transport, control_files)
1528
return KnitRepository(_format=self,
1530
control_files=control_files,
1531
_revision_store=_revision_store,
1532
control_store=control_store,
1533
text_store=text_store)
1536
# formats which have no format string are not discoverable
1537
# and not independently creatable, so are not registered.
1538
RepositoryFormat.register_format(RepositoryFormat7())
1539
_default_format = RepositoryFormatKnit1()
1540
RepositoryFormat.register_format(_default_format)
1541
RepositoryFormat.set_default_format(_default_format)
1542
_legacy_formats = [RepositoryFormat4(),
1543
RepositoryFormat5(),
1544
RepositoryFormat6()]
1547
class InterRepository(InterObject):
1548
"""This class represents operations taking place between two repositories.
1550
Its instances have methods like copy_content and fetch, and contain
1551
references to the source and target repositories these operations can be
1554
Often we will provide convenience methods on 'repository' which carry out
1555
operations with another repository - they will always forward to
1556
InterRepository.get(other).method_name(parameters).
1560
"""The available optimised InterRepository types."""
1563
def copy_content(self, revision_id=None, basis=None):
1564
"""Make a complete copy of the content in self into destination.
1566
This is a destructive operation! Do not use it on existing
1569
:param revision_id: Only copy the content needed to construct
1570
revision_id and its parents.
1571
:param basis: Copy the needed data preferentially from basis.
1574
self.target.set_make_working_trees(self.source.make_working_trees())
1575
except NotImplementedError:
1577
# grab the basis available data
1578
if basis is not None:
1579
self.target.fetch(basis, revision_id=revision_id)
1580
# but don't bother fetching if we have the needed data now.
1581
if (revision_id not in (None, NULL_REVISION) and
1582
self.target.has_revision(revision_id)):
1584
self.target.fetch(self.source, revision_id=revision_id)
1587
def fetch(self, revision_id=None, pb=None):
1588
"""Fetch the content required to construct revision_id.
1590
The content is copied from source to target.
1592
:param revision_id: if None all content is copied, if NULL_REVISION no
1594
:param pb: optional progress bar to use for progress reports. If not
1595
provided a default one will be created.
1597
Returns the copied revision count and the failed revisions in a tuple:
1600
from bzrlib.fetch import GenericRepoFetcher
1601
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1602
self.source, self.source._format, self.target, self.target._format)
1603
f = GenericRepoFetcher(to_repository=self.target,
1604
from_repository=self.source,
1605
last_revision=revision_id,
1607
return f.count_copied, f.failed_revisions
1610
def missing_revision_ids(self, revision_id=None):
1611
"""Return the revision ids that source has that target does not.
1613
These are returned in topological order.
1615
:param revision_id: only return revision ids included by this
1618
# generic, possibly worst case, slow code path.
1619
target_ids = set(self.target.all_revision_ids())
1620
if revision_id is not None:
1621
source_ids = self.source.get_ancestry(revision_id)
1622
assert source_ids[0] == None
1625
source_ids = self.source.all_revision_ids()
1626
result_set = set(source_ids).difference(target_ids)
1627
# this may look like a no-op: its not. It preserves the ordering
1628
# other_ids had while only returning the members from other_ids
1629
# that we've decided we need.
1630
return [rev_id for rev_id in source_ids if rev_id in result_set]
1633
class InterWeaveRepo(InterRepository):
1634
"""Optimised code paths between Weave based repositories."""
1636
_matching_repo_format = RepositoryFormat7()
1637
"""Repository format for testing with."""
1640
def is_compatible(source, target):
1641
"""Be compatible with known Weave formats.
1643
We don't test for the stores being of specific types because that
1644
could lead to confusing results, and there is no need to be
1648
return (isinstance(source._format, (RepositoryFormat5,
1650
RepositoryFormat7)) and
1651
isinstance(target._format, (RepositoryFormat5,
1653
RepositoryFormat7)))
1654
except AttributeError:
1658
def copy_content(self, revision_id=None, basis=None):
1659
"""See InterRepository.copy_content()."""
1660
# weave specific optimised path:
1661
if basis is not None:
1662
# copy the basis in, then fetch remaining data.
1663
basis.copy_content_into(self.target, revision_id)
1664
# the basis copy_content_into could miss-set this.
1666
self.target.set_make_working_trees(self.source.make_working_trees())
1667
except NotImplementedError:
1669
self.target.fetch(self.source, revision_id=revision_id)
1672
self.target.set_make_working_trees(self.source.make_working_trees())
1673
except NotImplementedError:
1675
# FIXME do not peek!
1676
if self.source.control_files._transport.listable():
1677
pb = ui.ui_factory.nested_progress_bar()
1679
self.target.weave_store.copy_all_ids(
1680
self.source.weave_store,
1682
from_transaction=self.source.get_transaction(),
1683
to_transaction=self.target.get_transaction())
1684
pb.update('copying inventory', 0, 1)
1685
self.target.control_weaves.copy_multi(
1686
self.source.control_weaves, ['inventory'],
1687
from_transaction=self.source.get_transaction(),
1688
to_transaction=self.target.get_transaction())
1689
self.target._revision_store.text_store.copy_all_ids(
1690
self.source._revision_store.text_store,
1695
self.target.fetch(self.source, revision_id=revision_id)
1698
def fetch(self, revision_id=None, pb=None):
1699
"""See InterRepository.fetch()."""
1700
from bzrlib.fetch import GenericRepoFetcher
1701
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1702
self.source, self.source._format, self.target, self.target._format)
1703
f = GenericRepoFetcher(to_repository=self.target,
1704
from_repository=self.source,
1705
last_revision=revision_id,
1707
return f.count_copied, f.failed_revisions
1710
def missing_revision_ids(self, revision_id=None):
1711
"""See InterRepository.missing_revision_ids()."""
1712
# we want all revisions to satisfy revision_id in source.
1713
# but we don't want to stat every file here and there.
1714
# we want then, all revisions other needs to satisfy revision_id
1715
# checked, but not those that we have locally.
1716
# so the first thing is to get a subset of the revisions to
1717
# satisfy revision_id in source, and then eliminate those that
1718
# we do already have.
1719
# this is slow on high latency connection to self, but as as this
1720
# disk format scales terribly for push anyway due to rewriting
1721
# inventory.weave, this is considered acceptable.
1723
if revision_id is not None:
1724
source_ids = self.source.get_ancestry(revision_id)
1725
assert source_ids[0] == None
1728
source_ids = self.source._all_possible_ids()
1729
source_ids_set = set(source_ids)
1730
# source_ids is the worst possible case we may need to pull.
1731
# now we want to filter source_ids against what we actually
1732
# have in target, but don't try to check for existence where we know
1733
# we do not have a revision as that would be pointless.
1734
target_ids = set(self.target._all_possible_ids())
1735
possibly_present_revisions = target_ids.intersection(source_ids_set)
1736
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1737
required_revisions = source_ids_set.difference(actually_present_revisions)
1738
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1739
if revision_id is not None:
1740
# we used get_ancestry to determine source_ids then we are assured all
1741
# revisions referenced are present as they are installed in topological order.
1742
# and the tip revision was validated by get_ancestry.
1743
return required_topo_revisions
1745
# if we just grabbed the possibly available ids, then
1746
# we only have an estimate of whats available and need to validate
1747
# that against the revision records.
1748
return self.source._eliminate_revisions_not_present(required_topo_revisions)
1751
class InterKnitRepo(InterRepository):
1752
"""Optimised code paths between Knit based repositories."""
1754
_matching_repo_format = RepositoryFormatKnit1()
1755
"""Repository format for testing with."""
1758
def is_compatible(source, target):
1759
"""Be compatible with known Knit formats.
1761
We don't test for the stores being of specific types because that
1762
could lead to confusing results, and there is no need to be
1766
return (isinstance(source._format, (RepositoryFormatKnit1)) and
1767
isinstance(target._format, (RepositoryFormatKnit1)))
1768
except AttributeError:
1772
def fetch(self, revision_id=None, pb=None):
1773
"""See InterRepository.fetch()."""
1774
from bzrlib.fetch import KnitRepoFetcher
1775
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1776
self.source, self.source._format, self.target, self.target._format)
1777
f = KnitRepoFetcher(to_repository=self.target,
1778
from_repository=self.source,
1779
last_revision=revision_id,
1781
return f.count_copied, f.failed_revisions
1784
def missing_revision_ids(self, revision_id=None):
1785
"""See InterRepository.missing_revision_ids()."""
1786
if revision_id is not None:
1787
source_ids = self.source.get_ancestry(revision_id)
1788
assert source_ids[0] == None
1791
source_ids = self.source._all_possible_ids()
1792
source_ids_set = set(source_ids)
1793
# source_ids is the worst possible case we may need to pull.
1794
# now we want to filter source_ids against what we actually
1795
# have in target, but don't try to check for existence where we know
1796
# we do not have a revision as that would be pointless.
1797
target_ids = set(self.target._all_possible_ids())
1798
possibly_present_revisions = target_ids.intersection(source_ids_set)
1799
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1800
required_revisions = source_ids_set.difference(actually_present_revisions)
1801
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1802
if revision_id is not None:
1803
# we used get_ancestry to determine source_ids then we are assured all
1804
# revisions referenced are present as they are installed in topological order.
1805
# and the tip revision was validated by get_ancestry.
1806
return required_topo_revisions
1808
# if we just grabbed the possibly available ids, then
1809
# we only have an estimate of whats available and need to validate
1810
# that against the revision records.
1811
return self.source._eliminate_revisions_not_present(required_topo_revisions)
1813
InterRepository.register_optimiser(InterWeaveRepo)
1814
InterRepository.register_optimiser(InterKnitRepo)
1817
class RepositoryTestProviderAdapter(object):
1818
"""A tool to generate a suite testing multiple repository formats at once.
1820
This is done by copying the test once for each transport and injecting
1821
the transport_server, transport_readonly_server, and bzrdir_format and
1822
repository_format classes into each copy. Each copy is also given a new id()
1823
to make it easy to identify.
1826
def __init__(self, transport_server, transport_readonly_server, formats):
1827
self._transport_server = transport_server
1828
self._transport_readonly_server = transport_readonly_server
1829
self._formats = formats
1831
def adapt(self, test):
1832
result = TestSuite()
1833
for repository_format, bzrdir_format in self._formats:
1834
new_test = deepcopy(test)
1835
new_test.transport_server = self._transport_server
1836
new_test.transport_readonly_server = self._transport_readonly_server
1837
new_test.bzrdir_format = bzrdir_format
1838
new_test.repository_format = repository_format
1839
def make_new_test_id():
1840
new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1841
return lambda: new_id
1842
new_test.id = make_new_test_id()
1843
result.addTest(new_test)
1847
class InterRepositoryTestProviderAdapter(object):
1848
"""A tool to generate a suite testing multiple inter repository formats.
1850
This is done by copying the test once for each interrepo provider and injecting
1851
the transport_server, transport_readonly_server, repository_format and
1852
repository_to_format classes into each copy.
1853
Each copy is also given a new id() to make it easy to identify.
1856
def __init__(self, transport_server, transport_readonly_server, formats):
1857
self._transport_server = transport_server
1858
self._transport_readonly_server = transport_readonly_server
1859
self._formats = formats
1861
def adapt(self, test):
1862
result = TestSuite()
1863
for interrepo_class, repository_format, repository_format_to in self._formats:
1864
new_test = deepcopy(test)
1865
new_test.transport_server = self._transport_server
1866
new_test.transport_readonly_server = self._transport_readonly_server
1867
new_test.interrepo_class = interrepo_class
1868
new_test.repository_format = repository_format
1869
new_test.repository_format_to = repository_format_to
1870
def make_new_test_id():
1871
new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1872
return lambda: new_id
1873
new_test.id = make_new_test_id()
1874
result.addTest(new_test)
1878
def default_test_list():
1879
"""Generate the default list of interrepo permutations to test."""
1881
# test the default InterRepository between format 6 and the current
1883
# XXX: robertc 20060220 reinstate this when there are two supported
1884
# formats which do not have an optimal code path between them.
1885
result.append((InterRepository,
1886
RepositoryFormat6(),
1887
RepositoryFormatKnit1()))
1888
for optimiser in InterRepository._optimisers:
1889
result.append((optimiser,
1890
optimiser._matching_repo_format,
1891
optimiser._matching_repo_format
1893
# if there are specific combinations we want to use, we can add them
1898
class CopyConverter(object):
1899
"""A repository conversion tool which just performs a copy of the content.
1901
This is slow but quite reliable.
1904
def __init__(self, target_format):
1905
"""Create a CopyConverter.
1907
:param target_format: The format the resulting repository should be.
1909
self.target_format = target_format
1911
def convert(self, repo, pb):
1912
"""Perform the conversion of to_convert, giving feedback via pb.
1914
:param to_convert: The disk object to convert.
1915
:param pb: a progress bar to use for progress information.
1920
# this is only useful with metadir layouts - separated repo content.
1921
# trigger an assertion if not such
1922
repo._format.get_format_string()
1923
self.repo_dir = repo.bzrdir
1924
self.step('Moving repository to repository.backup')
1925
self.repo_dir.transport.move('repository', 'repository.backup')
1926
backup_transport = self.repo_dir.transport.clone('repository.backup')
1927
self.source_repo = repo._format.open(self.repo_dir,
1929
_override_transport=backup_transport)
1930
self.step('Creating new repository')
1931
converted = self.target_format.initialize(self.repo_dir,
1932
self.source_repo.is_shared())
1933
converted.lock_write()
1935
self.step('Copying content into repository.')
1936
self.source_repo.copy_content_into(converted)
1939
self.step('Deleting old repository content.')
1940
self.repo_dir.transport.delete_tree('repository.backup')
1941
self.pb.note('repository converted')
1943
def step(self, message):
1944
"""Update the pb by a step."""
1946
self.pb.update(message, self.count, self.total)
1949
class CommitBuilder(object):
1950
"""Provides an interface to build up a commit.
1952
This allows describing a tree to be committed without needing to
1953
know the internals of the format of the repository.
1955
def __init__(self, repository, parents, config, timestamp=None,
1956
timezone=None, committer=None, revprops=None,
1958
"""Initiate a CommitBuilder.
1960
:param repository: Repository to commit to.
1961
:param parents: Revision ids of the parents of the new revision.
1962
:param config: Configuration to use.
1963
:param timestamp: Optional timestamp recorded for commit.
1964
:param timezone: Optional timezone for timestamp.
1965
:param committer: Optional committer to set for commit.
1966
:param revprops: Optional dictionary of revision properties.
1967
:param revision_id: Optional revision id.
1969
self._config = config
1971
if committer is None:
1972
self._committer = self._config.username()
1974
assert isinstance(committer, basestring), type(committer)
1975
self._committer = committer
1977
self.new_inventory = Inventory()
1978
self._new_revision_id = revision_id
1979
self.parents = parents
1980
self.repository = repository
1983
if revprops is not None:
1984
self._revprops.update(revprops)
1986
if timestamp is None:
1987
timestamp = time.time()
1988
# Restrict resolution to 1ms
1989
self._timestamp = round(timestamp, 3)
1991
if timezone is None:
1992
self._timezone = local_time_offset()
1994
self._timezone = int(timezone)
1996
self._generate_revision_if_needed()
1998
def commit(self, message):
1999
"""Make the actual commit.
2001
:return: The revision id of the recorded revision.
2003
rev = Revision(timestamp=self._timestamp,
2004
timezone=self._timezone,
2005
committer=self._committer,
2007
inventory_sha1=self.inv_sha1,
2008
revision_id=self._new_revision_id,
2009
properties=self._revprops)
2010
rev.parent_ids = self.parents
2011
self.repository.add_revision(self._new_revision_id, rev,
2012
self.new_inventory, self._config)
2013
return self._new_revision_id
2015
def finish_inventory(self):
2016
"""Tell the builder that the inventory is finished."""
2017
self.new_inventory.revision_id = self._new_revision_id
2018
self.inv_sha1 = self.repository.add_inventory(
2019
self._new_revision_id,
2024
def _gen_revision_id(self):
2025
"""Return new revision-id."""
2026
s = '%s-%s-' % (self._config.user_email(),
2027
compact_date(self._timestamp))
2028
s += hexlify(rand_bytes(8))
2031
def _generate_revision_if_needed(self):
2032
"""Create a revision id if None was supplied.
2034
If the repository can not support user-specified revision ids
2035
they should override this function and raise UnsupportedOperation
2036
if _new_revision_id is not None.
2038
:raises: UnsupportedOperation
2040
if self._new_revision_id is None:
2041
self._new_revision_id = self._gen_revision_id()
2043
def record_entry_contents(self, ie, parent_invs, path, tree):
2044
"""Record the content of ie from tree into the commit if needed.
2046
:param ie: An inventory entry present in the commit.
2047
:param parent_invs: The inventories of the parent revisions of the
2049
:param path: The path the entry is at in the tree.
2050
:param tree: The tree which contains this entry and should be used to
2053
self.new_inventory.add(ie)
2055
# ie.revision is always None if the InventoryEntry is considered
2056
# for committing. ie.snapshot will record the correct revision
2057
# which may be the sole parent if it is untouched.
2058
if ie.revision is not None:
2060
previous_entries = ie.find_previous_heads(
2062
self.repository.weave_store,
2063
self.repository.get_transaction())
2064
# we are creating a new revision for ie in the history store
2066
ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2068
def modified_directory(self, file_id, file_parents):
2069
"""Record the presence of a symbolic link.
2071
:param file_id: The file_id of the link to record.
2072
:param file_parents: The per-file parent revision ids.
2074
self._add_text_to_weave(file_id, [], file_parents.keys())
2076
def modified_file_text(self, file_id, file_parents,
2077
get_content_byte_lines, text_sha1=None,
2079
"""Record the text of file file_id
2081
:param file_id: The file_id of the file to record the text of.
2082
:param file_parents: The per-file parent revision ids.
2083
:param get_content_byte_lines: A callable which will return the byte
2085
:param text_sha1: Optional SHA1 of the file contents.
2086
:param text_size: Optional size of the file contents.
2088
# mutter('storing text of file {%s} in revision {%s} into %r',
2089
# file_id, self._new_revision_id, self.repository.weave_store)
2090
# special case to avoid diffing on renames or
2092
if (len(file_parents) == 1
2093
and text_sha1 == file_parents.values()[0].text_sha1
2094
and text_size == file_parents.values()[0].text_size):
2095
previous_ie = file_parents.values()[0]
2096
versionedfile = self.repository.weave_store.get_weave(file_id,
2097
self.repository.get_transaction())
2098
versionedfile.clone_text(self._new_revision_id,
2099
previous_ie.revision, file_parents.keys())
2100
return text_sha1, text_size
2102
new_lines = get_content_byte_lines()
2103
# TODO: Rather than invoking sha_strings here, _add_text_to_weave
2104
# should return the SHA1 and size
2105
self._add_text_to_weave(file_id, new_lines, file_parents.keys())
2106
return osutils.sha_strings(new_lines), \
2107
sum(map(len, new_lines))
2109
def modified_link(self, file_id, file_parents, link_target):
2110
"""Record the presence of a symbolic link.
2112
:param file_id: The file_id of the link to record.
2113
:param file_parents: The per-file parent revision ids.
2114
:param link_target: Target location of this link.
2116
self._add_text_to_weave(file_id, [], file_parents.keys())
2118
def _add_text_to_weave(self, file_id, new_lines, parents):
2119
versionedfile = self.repository.weave_store.get_weave_or_empty(
2120
file_id, self.repository.get_transaction())
2121
versionedfile.add_lines(self._new_revision_id, parents, new_lines)
2122
versionedfile.clear_cache()
2134
def _unescaper(match, _map=_unescape_map):
2135
return _map[match.group(1)]
2141
def _unescape_xml(data):
2142
"""Unescape predefined XML entities in a string of data."""
2144
if _unescape_re is None:
2145
_unescape_re = re.compile('\&([^;]*);')
2146
return _unescape_re.sub(_unescaper, data)