1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
"""Copying of history from one branch to another.
21
21
that has merged into it. As the first step of a merge, pull, or
22
22
branch operation we copy history from the source into the destination
25
The copying is done in a slightly complicated order. We don't want to
26
add a revision to the store until everything it refers to is also
27
stored, so that if a revision is present we can totally recreate it.
28
However, we can't know what files are included in a revision until we
29
read its inventory. So we query the inventory store of the source for
30
the ids we need, and then pull those ids and then return to the inventories.
36
import bzrlib.errors as errors
37
from bzrlib.errors import InstallFailed
38
from bzrlib.progress import ProgressPhase
39
33
from bzrlib.revision import NULL_REVISION
40
34
from bzrlib.tsort import topo_sort
41
35
from bzrlib.trace import mutter
43
37
from bzrlib.versionedfile import FulltextContentFactory
45
# TODO: Avoid repeatedly opening weaves so many times.
47
# XXX: This doesn't handle ghost (not present in branch) revisions at
48
# all yet. I'm not sure they really should be supported.
50
# NOTE: This doesn't copy revisions which may be present but not
51
# merged into the last revision. I'm not sure we want to do that.
53
# - get a list of revisions that need to be pulled in
54
# - for each one, pull in that revision file
55
# and get the inventory, and store the inventory with right
57
# - and get the ancestry, and store that with right parents too
58
# - and keep a note of all file ids and version seen
59
# - then go through all files; for each one get the weave,
60
# and add in all file versions
63
40
class RepoFetcher(object):
64
41
"""Pull revisions and texts from one repository to another.
67
if set, try to limit to the data this revision references.
69
43
This should not be used directly, it's essential a object to encapsulate
70
44
the logic in InterRepository.fetch().
74
48
pb=None, find_ghosts=True, fetch_spec=None):
75
49
"""Create a repo fetcher.
51
:param last_revision: If set, try to limit to the data this revision
77
53
:param find_ghosts: If True search the entire history for ghosts.
78
54
:param _write_group_acquired_callable: Don't use; this parameter only
79
55
exists to facilitate a hack done in InterPackRepo.fetch. We would
80
56
like to remove this parameter.
57
:param pb: ProgressBar object to use; deprecated and ignored.
58
This method will just create one on top of the stack.
61
symbol_versioning.warn(
62
symbol_versioning.deprecated_in((1, 14, 0))
63
% "pb parameter to RepoFetcher.__init__")
64
# and for simplicity it is in fact ignored
82
65
if to_repository.has_same_location(from_repository):
83
66
# repository.fetch should be taking care of this case.
84
67
raise errors.BzrError('RepoFetcher run '
91
74
self._last_revision = last_revision
92
75
self._fetch_spec = fetch_spec
93
76
self.find_ghosts = find_ghosts
95
self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
96
self.nested_pb = self.pb
100
77
self.from_repository.lock_read()
78
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
79
self.from_repository, self.from_repository._format,
80
self.to_repository, self.to_repository._format)
105
if self.nested_pb is not None:
106
self.nested_pb.finished()
108
84
self.from_repository.unlock()
121
97
# assert not missing
122
98
self.count_total = 0
123
99
self.file_ids_names = {}
124
pp = ProgressPhase('Transferring', 4, self.pb)
100
pb = bzrlib.ui.ui_factory.nested_progress_bar()
101
pb.show_pct = pb.show_count = False
103
pb.update("Finding revisions", 0, 2)
127
104
search = self._revids_to_fetch()
128
105
if search is None:
130
self._fetch_everything_for_search(search, pp)
107
pb.update("Fetching revisions", 1, 2)
108
self._fetch_everything_for_search(search)
134
def _fetch_everything_for_search(self, search, pp):
112
def _fetch_everything_for_search(self, search):
135
113
"""Fetch all data for the given set of revisions."""
136
114
# The first phase is "file". We pass the progress bar for it directly
137
115
# into item_keys_introduced_by, which has more information about how
146
124
raise errors.IncompatibleRepositories(
147
125
self.from_repository, self.to_repository,
148
126
"different rich-root support")
149
self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
127
pb = bzrlib.ui.ui_factory.nested_progress_bar()
129
pb.update("Get stream source")
151
130
source = self.from_repository._get_source(
152
131
self.to_repository._format)
153
132
stream = source.get_stream(search)
154
133
from_format = self.from_repository._format
134
pb.update("Inserting stream")
155
135
resume_tokens, missing_keys = self.sink.insert_stream(
156
136
stream, from_format, [])
137
if self.to_repository._fallback_repositories:
139
self._parent_inventories(search.get_keys()))
141
pb.update("Missing keys")
158
142
stream = source.get_stream_for_missing_keys(missing_keys)
143
pb.update("Inserting missing keys")
159
144
resume_tokens, missing_keys = self.sink.insert_stream(
160
145
stream, from_format, resume_tokens)
183
168
if self._last_revision is NULL_REVISION:
184
169
# explicit limit of no revisions needed
186
if (self._last_revision is not None and
187
self.to_repository.has_revision(self._last_revision)):
190
return self.to_repository.search_missing_revision_ids(
191
self.from_repository, self._last_revision,
192
find_ghosts=self.find_ghosts)
193
except errors.NoSuchRevision, e:
194
raise InstallFailed([self._last_revision])
171
return self.to_repository.search_missing_revision_ids(
172
self.from_repository, self._last_revision,
173
find_ghosts=self.find_ghosts)
175
def _parent_inventories(self, revision_ids):
176
# Find all the parent revisions referenced by the stream, but
177
# not present in the stream, and make sure we send their
179
parent_maps = self.to_repository.get_parent_map(revision_ids)
181
map(parents.update, parent_maps.itervalues())
182
parents.discard(NULL_REVISION)
183
parents.difference_update(revision_ids)
184
missing_keys = set(('inventories', rev_id) for rev_id in parents)
197
188
class Inter1and2Helper(object):