49
52
def __init__(self, to_repository, from_repository, last_revision=None,
50
pb=None, find_ghosts=True, fetch_spec=None):
53
find_ghosts=True, fetch_spec=None):
51
54
"""Create a repo fetcher.
53
56
:param last_revision: If set, try to limit to the data this revision
58
:param fetch_spec: A SearchResult specifying which revisions to fetch.
59
If set, this overrides last_revision.
55
60
:param find_ghosts: If True search the entire history for ghosts.
56
:param _write_group_acquired_callable: Don't use; this parameter only
57
exists to facilitate a hack done in InterPackRepo.fetch. We would
58
like to remove this parameter.
59
:param pb: ProgressBar object to use; deprecated and ignored.
60
This method will just create one on top of the stack.
63
symbol_versioning.warn(
64
symbol_versioning.deprecated_in((1, 14, 0))
65
% "pb parameter to RepoFetcher.__init__")
66
# and for simplicity it is in fact ignored
67
if to_repository.has_same_location(from_repository):
68
# repository.fetch should be taking care of this case.
69
raise errors.BzrError('RepoFetcher run '
70
'between two objects at the same location: '
71
'%r and %r' % (to_repository, from_repository))
62
# repository.fetch has the responsibility for short-circuiting
63
# attempts to copy between a repository and itself.
72
64
self.to_repository = to_repository
73
65
self.from_repository = from_repository
74
66
self.sink = to_repository._get_sink()
159
152
"""Determines the exact revisions needed from self.from_repository to
160
153
install self._last_revision in self.to_repository.
162
If no revisions need to be fetched, then this just returns None.
155
:returns: A SearchResult of some sort. (Possibly a
156
PendingAncestryResult, EmptySearchResult, etc.)
164
158
if self._fetch_spec is not None:
159
# The fetch spec is already a concrete search result.
165
160
return self._fetch_spec
166
mutter('fetch up to rev {%s}', self._last_revision)
167
if self._last_revision is NULL_REVISION:
161
elif self._last_revision == NULL_REVISION:
162
# fetch_spec is None + last_revision is null => empty fetch.
168
163
# explicit limit of no revisions needed
170
if (self._last_revision is not None and
171
self.to_repository.has_revision(self._last_revision)):
174
return self.to_repository.search_missing_revision_ids(
175
self.from_repository, self._last_revision,
176
find_ghosts=self.find_ghosts)
177
except errors.NoSuchRevision, e:
178
raise InstallFailed([self._last_revision])
164
return _mod_graph.EmptySearchResult()
165
elif self._last_revision is not None:
166
return _mod_graph.NotInOtherForRevs(self.to_repository,
167
self.from_repository, [self._last_revision],
168
find_ghosts=self.find_ghosts).execute()
169
else: # self._last_revision is None:
170
return _mod_graph.EverythingNotInOther(self.to_repository,
171
self.from_repository,
172
find_ghosts=self.find_ghosts).execute()
181
175
class Inter1and2Helper(object):
248
242
# yet, and are unlikely to in non-rich-root environments anyway.
249
243
root_id_order.sort(key=operator.itemgetter(0))
250
244
# Create a record stream containing the roots to create.
252
for key in root_id_order:
253
root_id, rev_id = key
254
rev_parents = parent_map[rev_id]
255
# We drop revision parents with different file-ids, because
256
# that represents a rename of the root to a different location
257
# - its not actually a parent for us. (We could look for that
258
# file id in the revision tree at considerably more expense,
259
# but for now this is sufficient (and reconcile will catch and
260
# correct this anyway).
261
# When a parent revision is a ghost, we guess that its root id
262
# was unchanged (rather than trimming it from the parent list).
263
parent_keys = tuple((root_id, parent) for parent in rev_parents
264
if parent != NULL_REVISION and
265
rev_id_to_root_id.get(parent, root_id) == root_id)
266
yield FulltextContentFactory(key, parent_keys, None, '')
267
return [('texts', yield_roots())]
245
if len(revs) > self.known_graph_threshold:
246
graph = self.source.get_known_graph_ancestry(revs)
247
new_roots_stream = _new_root_data_stream(
248
root_id_order, rev_id_to_root_id, parent_map, self.source, graph)
249
return [('texts', new_roots_stream)]
252
def _new_root_data_stream(
253
root_keys_to_create, rev_id_to_root_id_map, parent_map, repo, graph=None):
254
"""Generate a texts substream of synthesised root entries.
256
Used in fetches that do rich-root upgrades.
258
:param root_keys_to_create: iterable of (root_id, rev_id) pairs describing
259
the root entries to create.
260
:param rev_id_to_root_id_map: dict of known rev_id -> root_id mappings for
261
calculating the parents. If a parent rev_id is not found here then it
262
will be recalculated.
263
:param parent_map: a parent map for all the revisions in
265
:param graph: a graph to use instead of repo.get_graph().
267
for root_key in root_keys_to_create:
268
root_id, rev_id = root_key
269
parent_keys = _parent_keys_for_root_version(
270
root_id, rev_id, rev_id_to_root_id_map, parent_map, repo, graph)
271
yield versionedfile.FulltextContentFactory(
272
root_key, parent_keys, None, '')
275
def _parent_keys_for_root_version(
276
root_id, rev_id, rev_id_to_root_id_map, parent_map, repo, graph=None):
277
"""Get the parent keys for a given root id.
279
A helper function for _new_root_data_stream.
281
# Include direct parents of the revision, but only if they used the same
282
# root_id and are heads.
283
rev_parents = parent_map[rev_id]
285
for parent_id in rev_parents:
286
if parent_id == NULL_REVISION:
288
if parent_id not in rev_id_to_root_id_map:
289
# We probably didn't read this revision, go spend the extra effort
292
tree = repo.revision_tree(parent_id)
293
except errors.NoSuchRevision:
294
# Ghost, fill out rev_id_to_root_id in case we encounter this
296
# But set parent_root_id to None since we don't really know
297
parent_root_id = None
299
parent_root_id = tree.get_root_id()
300
rev_id_to_root_id_map[parent_id] = None
302
# rev_id_to_root_id_map[parent_id] = parent_root_id
303
# memory consumption maybe?
305
parent_root_id = rev_id_to_root_id_map[parent_id]
306
if root_id == parent_root_id:
307
# With stacking we _might_ want to refer to a non-local revision,
308
# but this code path only applies when we have the full content
309
# available, so ghosts really are ghosts, not just the edge of
311
parent_ids.append(parent_id)
313
# root_id may be in the parent anyway.
315
tree = repo.revision_tree(parent_id)
316
except errors.NoSuchRevision:
317
# ghost, can't refer to it.
321
parent_ids.append(tree.get_file_revision(root_id))
322
except errors.NoSuchId:
325
# Drop non-head parents
327
graph = repo.get_graph()
328
heads = graph.heads(parent_ids)
330
for parent_id in parent_ids:
331
if parent_id in heads and parent_id not in selected_ids:
332
selected_ids.append(parent_id)
333
parent_keys = [(root_id, parent_id) for parent_id in selected_ids]
337
class TargetRepoKinds(object):
338
"""An enum-like set of constants.
340
They are the possible values of FetchSpecFactory.target_repo_kinds.
343
PREEXISTING = 'preexisting'
348
class FetchSpecFactory(object):
349
"""A helper for building the best fetch spec for a sprout call.
351
Factors that go into determining the sort of fetch to perform:
352
* did the caller specify any revision IDs?
353
* did the caller specify a source branch (need to fetch its
354
heads_to_fetch(), usually the tip + tags)
355
* is there an existing target repo (don't need to refetch revs it
357
* target is stacked? (similar to pre-existing target repo: even if
358
the target itself is new don't want to refetch existing revs)
360
:ivar source_branch: the source branch if one specified, else None.
361
:ivar source_branch_stop_revision_id: fetch up to this revision of
362
source_branch, rather than its tip.
363
:ivar source_repo: the source repository if one found, else None.
364
:ivar target_repo: the target repository acquired by sprout.
365
:ivar target_repo_kind: one of the TargetRepoKinds constants.
369
self._explicit_rev_ids = set()
370
self.source_branch = None
371
self.source_branch_stop_revision_id = None
372
self.source_repo = None
373
self.target_repo = None
374
self.target_repo_kind = None
377
def add_revision_ids(self, revision_ids):
378
"""Add revision_ids to the set of revision_ids to be fetched."""
379
self._explicit_rev_ids.update(revision_ids)
381
def make_fetch_spec(self):
382
"""Build a SearchResult or PendingAncestryResult or etc."""
383
if self.target_repo_kind is None or self.source_repo is None:
384
raise AssertionError(
385
'Incomplete FetchSpecFactory: %r' % (self.__dict__,))
386
if len(self._explicit_rev_ids) == 0 and self.source_branch is None:
387
if self.limit is not None:
388
raise NotImplementedError(
389
"limit is only supported with a source branch set")
390
# Caller hasn't specified any revisions or source branch
391
if self.target_repo_kind == TargetRepoKinds.EMPTY:
392
return _mod_graph.EverythingResult(self.source_repo)
394
# We want everything not already in the target (or target's
396
return _mod_graph.EverythingNotInOther(
397
self.target_repo, self.source_repo).execute()
398
heads_to_fetch = set(self._explicit_rev_ids)
399
if self.source_branch is not None:
400
must_fetch, if_present_fetch = self.source_branch.heads_to_fetch()
401
if self.source_branch_stop_revision_id is not None:
402
# Replace the tip rev from must_fetch with the stop revision
403
# XXX: this might be wrong if the tip rev is also in the
404
# must_fetch set for other reasons (e.g. it's the tip of
405
# multiple loom threads?), but then it's pretty unclear what it
406
# should mean to specify a stop_revision in that case anyway.
407
must_fetch.discard(self.source_branch.last_revision())
408
must_fetch.add(self.source_branch_stop_revision_id)
409
heads_to_fetch.update(must_fetch)
411
if_present_fetch = set()
412
if self.target_repo_kind == TargetRepoKinds.EMPTY:
413
# PendingAncestryResult does not raise errors if a requested head
414
# is absent. Ideally it would support the
415
# required_ids/if_present_ids distinction, but in practice
416
# heads_to_fetch will almost certainly be present so this doesn't
418
all_heads = heads_to_fetch.union(if_present_fetch)
419
ret = _mod_graph.PendingAncestryResult(all_heads, self.source_repo)
420
if self.limit is not None:
421
graph = self.source_repo.get_graph()
422
topo_order = list(graph.iter_topo_order(ret.get_keys()))
423
result_set = topo_order[:self.limit]
424
ret = self.source_repo.revision_ids_to_search_result(result_set)
427
return _mod_graph.NotInOtherForRevs(self.target_repo, self.source_repo,
428
required_ids=heads_to_fetch, if_present_ids=if_present_fetch,
429
limit=self.limit).execute()