1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""InterRepository operations."""
19
from __future__ import absolute_import
21
from io import BytesIO
24
from dulwich.errors import (
27
from dulwich.object_store import (
28
ObjectStoreGraphWalker,
30
from dulwich.protocol import (
34
from dulwich.refs import SYMREF
35
from dulwich.walk import Walker
37
from ..errors import (
39
FetchLimitUnsupported,
42
NoRoundtrippingSupport,
45
from ..repository import (
48
from ..revision import (
51
from ..sixish import (
66
DetermineWantsRecorder,
68
from .mapping import (
71
from .object_store import (
75
MissingObjectsIterator,
82
from .repository import (
90
from .unpeel_map import (
95
class InterToGitRepository(InterRepository):
96
"""InterRepository that copies into a Git repository."""
98
_matching_repo_format = GitRepositoryFormat()
100
def __init__(self, source, target):
101
super(InterToGitRepository, self).__init__(source, target)
102
self.mapping = self.target.get_mapping()
103
self.source_store = get_object_store(self.source, self.mapping)
106
def _get_repo_format_to_test():
109
def copy_content(self, revision_id=None, pb=None):
110
"""See InterRepository.copy_content."""
111
self.fetch(revision_id, pb, find_ghosts=False)
113
def fetch_refs(self, update_refs, lossy, overwrite=False):
114
"""Fetch possibly roundtripped revisions into the target repository
117
:param update_refs: Generate refs to fetch. Receives dictionary
118
with old refs (git shas), returns dictionary of new names to
120
:param lossy: Whether to roundtrip
121
:return: old refs, new refs
123
raise NotImplementedError(self.fetch_refs)
125
def search_missing_revision_ids(self,
126
find_ghosts=True, revision_ids=None, if_present_ids=None,
128
if limit is not None:
129
raise FetchLimitUnsupported(self)
133
todo.extend(revision_ids)
135
todo.extend(revision_ids)
136
with self.source_store.lock_read():
137
for revid in revision_ids:
138
if revid == NULL_REVISION:
141
git_sha = self.source_store._lookup_revision_sha1(revid)
143
raise NoSuchRevision(revid, self.source)
144
git_shas.append(git_sha)
145
walker = Walker(self.source_store,
146
include=git_shas, exclude=[
147
sha for sha in self.target.controldir.get_refs_container().as_dict().values()
149
missing_revids = set()
151
for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
153
missing_revids.add(type_data[0])
154
return self.source.revision_ids_to_search_result(missing_revids)
156
def _warn_slow(self):
157
if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
159
'Pushing from a Bazaar to a Git repository. '
160
'For better performance, push into a Bazaar repository.')
163
class InterToLocalGitRepository(InterToGitRepository):
164
"""InterBranch implementation between a Bazaar and a Git repository."""
166
def __init__(self, source, target):
167
super(InterToLocalGitRepository, self).__init__(source, target)
168
self.target_store = self.target.controldir._git.object_store
169
self.target_refs = self.target.controldir._git.refs
171
def _commit_needs_fetching(self, sha_id):
173
return (sha_id not in self.target_store)
174
except NoSuchRevision:
178
def _revision_needs_fetching(self, sha_id, revid):
179
if revid == NULL_REVISION:
183
sha_id = self.source_store._lookup_revision_sha1(revid)
186
return self._commit_needs_fetching(sha_id)
188
def missing_revisions(self, stop_revisions):
189
"""Find the revisions that are missing from the target repository.
191
:param stop_revisions: Revisions to check for (tuples with
193
:return: sequence of missing revisions, in topological order
194
:raise: NoSuchRevision if the stop_revisions are not present in
199
for (sha1, revid) in stop_revisions:
200
if sha1 is not None and revid is not None:
201
revid_sha_map[revid] = sha1
202
stop_revids.append(revid)
203
elif sha1 is not None:
204
if self._commit_needs_fetching(sha1):
205
for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
206
revid_sha_map[revid] = sha1
207
stop_revids.append(revid)
211
stop_revids.append(revid)
213
graph = self.source.get_graph()
214
pb = ui.ui_factory.nested_progress_bar()
218
for revid in stop_revids:
219
sha1 = revid_sha_map.get(revid)
220
if (not revid in missing and
221
self._revision_needs_fetching(sha1, revid)):
223
new_stop_revids.append(revid)
225
parent_map = graph.get_parent_map(new_stop_revids)
226
for parent_revids in viewvalues(parent_map):
227
stop_revids.update(parent_revids)
228
pb.update("determining revisions to fetch", len(missing))
231
return graph.iter_topo_order(missing)
233
def _get_target_bzr_refs(self):
234
"""Return a dictionary with references.
236
:return: Dictionary with reference names as keys and tuples
237
with Git SHA, Bazaar revid as values.
241
for k in self.target._git.refs.allkeys():
243
v = self.target._git.refs.read_ref(k)
248
if not v.startswith(SYMREF):
250
for (kind, type_data) in self.source_store.lookup_git_sha(v):
251
if kind == "commit" and self.source.has_revision(type_data[0]):
256
bzr_refs[k] = (v, revid)
259
def fetch_refs(self, update_refs, lossy, overwrite=False):
261
with self.source_store.lock_read():
262
old_refs = self._get_target_bzr_refs()
263
new_refs = update_refs(old_refs)
264
revidmap = self.fetch_objects(
265
[(git_sha, bzr_revid) for (git_sha, bzr_revid) in new_refs.values() if git_sha is None or not git_sha.startswith(SYMREF)], lossy=lossy)
266
for name, (gitid, revid) in viewitems(new_refs):
269
gitid = revidmap[revid][0]
271
gitid = self.source_store._lookup_revision_sha1(revid)
272
if gitid.startswith(SYMREF):
273
self.target_refs.set_symbolic_ref(name, gitid[len(SYMREF):])
276
old_git_id = old_refs[name][0]
278
self.target_refs.add_if_new(name, gitid)
280
self.target_refs.set_if_equals(name, old_git_id, gitid)
281
return revidmap, old_refs, new_refs
283
def fetch_objects(self, revs, lossy, limit=None):
284
if not lossy and not self.mapping.roundtripping:
285
for git_sha, bzr_revid in revs:
286
if bzr_revid is not None and needs_roundtripping(self.source, bzr_revid):
287
raise NoPushSupport(self.source, self.target, self.mapping,
289
with self.source_store.lock_read():
290
todo = list(self.missing_revisions(revs))[:limit]
292
pb = ui.ui_factory.nested_progress_bar()
294
object_generator = MissingObjectsIterator(
295
self.source_store, self.source, pb)
296
for (old_revid, git_sha) in object_generator.import_revisions(
299
new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
301
new_revid = old_revid
303
self.mapping.revision_id_bzr_to_foreign(old_revid)
304
except InvalidRevisionId:
305
refname = self.mapping.revid_as_refname(old_revid)
306
self.target_refs[refname] = git_sha
307
revidmap[old_revid] = (git_sha, new_revid)
308
self.target_store.add_objects(object_generator)
313
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
314
fetch_spec=None, mapped_refs=None):
315
if mapped_refs is not None:
316
stop_revisions = mapped_refs
317
elif revision_id is not None:
318
stop_revisions = [(None, revision_id)]
319
elif fetch_spec is not None:
320
recipe = fetch_spec.get_recipe()
321
if recipe[0] in ("search", "proxy-search"):
322
stop_revisions = [(None, revid) for revid in recipe[1]]
324
raise AssertionError("Unsupported search result type %s" % recipe[0])
326
stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
329
self.fetch_objects(stop_revisions, lossy=False)
330
except NoPushSupport:
331
raise NoRoundtrippingSupport(self.source, self.target)
334
def is_compatible(source, target):
335
"""Be compatible with GitRepository."""
336
return (not isinstance(source, GitRepository) and
337
isinstance(target, LocalGitRepository))
340
class InterToRemoteGitRepository(InterToGitRepository):
342
def fetch_refs(self, update_refs, lossy, overwrite=False):
343
"""Import the gist of the ancestry of a particular revision."""
344
if not lossy and not self.mapping.roundtripping:
345
raise NoPushSupport(self.source, self.target, self.mapping)
346
unpeel_map = UnpeelMap.from_repository(self.source)
348
def determine_wants(old_refs):
350
self.old_refs = dict([(k, (v, None)) for (k, v) in viewitems(old_refs)])
351
self.new_refs = update_refs(self.old_refs)
352
for name, (gitid, revid) in viewitems(self.new_refs):
354
git_sha = self.source_store._lookup_revision_sha1(revid)
355
gitid = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
357
if remote_divergence(old_refs.get(name), gitid, self.source_store):
358
raise DivergedBranches(self.source, self.target)
362
with self.source_store.lock_read():
363
new_refs = self.target.send_pack(determine_wants,
364
self.source_store.generate_lossy_pack_data)
366
return revidmap, self.old_refs, self.new_refs
369
def is_compatible(source, target):
370
"""Be compatible with GitRepository."""
371
return (not isinstance(source, GitRepository) and
372
isinstance(target, RemoteGitRepository))
375
class GitSearchResult(object):
377
def __init__(self, start, exclude, keys):
379
self._exclude = exclude
385
def get_recipe(self):
386
return ('search', self._start, self._exclude, len(self._keys))
389
class InterFromGitRepository(InterRepository):
391
_matching_repo_format = GitRepositoryFormat()
393
def _target_has_shas(self, shas):
394
raise NotImplementedError(self._target_has_shas)
396
def get_determine_wants_heads(self, wants, include_tags=False):
398
def determine_wants(refs):
399
potential = set(wants)
401
for k, unpeeled in viewitems(refs):
402
if k.endswith(b"^{}"):
406
if unpeeled == ZERO_SHA:
408
potential.add(unpeeled)
409
return list(potential - self._target_has_shas(potential))
410
return determine_wants
412
def determine_wants_all(self, refs):
413
raise NotImplementedError(self.determine_wants_all)
416
def _get_repo_format_to_test():
419
def copy_content(self, revision_id=None):
420
"""See InterRepository.copy_content."""
421
self.fetch(revision_id, find_ghosts=False)
423
def search_missing_revision_ids(self,
424
find_ghosts=True, revision_ids=None, if_present_ids=None,
426
if limit is not None:
427
raise FetchLimitUnsupported(self)
428
if revision_ids is None and if_present_ids is None:
429
todo = set(self.source.all_revision_ids())
432
if revision_ids is not None:
433
for revid in revision_ids:
434
if not self.source.has_revision(revid):
435
raise NoSuchRevision(revid, self.source)
436
todo.update(revision_ids)
437
if if_present_ids is not None:
438
todo.update(if_present_ids)
439
result_set = todo.difference(self.target.all_revision_ids())
440
result_parents = set(itertools.chain.from_iterable(viewvalues(
441
self.source.get_graph().get_parent_map(result_set))))
442
included_keys = result_set.intersection(result_parents)
443
start_keys = result_set.difference(included_keys)
444
exclude_keys = result_parents.difference(result_set)
445
return GitSearchResult(start_keys, exclude_keys, result_set)
448
class InterGitNonGitRepository(InterFromGitRepository):
449
"""Base InterRepository that copies revisions from a Git into a non-Git
452
def _target_has_shas(self, shas):
456
revid = self.source.lookup_foreign_revision_id(sha)
457
except NotCommitError:
458
# Commit is definitely not present
462
return set([revids[r] for r in self.target.has_revisions(revids)])
464
def determine_wants_all(self, refs):
466
for k, v in viewitems(refs):
467
# For non-git target repositories, only worry about peeled
470
potential.add(self.source.controldir.get_peeled(k) or v)
471
return list(potential - self._target_has_shas(potential))
473
def get_determine_wants_heads(self, wants, include_tags=False):
475
def determine_wants(refs):
476
potential = set(wants)
478
for k, unpeeled in viewitems(refs):
481
if unpeeled == ZERO_SHA:
483
potential.add(self.source.controldir.get_peeled(k) or unpeeled)
484
return list(potential - self._target_has_shas(potential))
485
return determine_wants
487
def _warn_slow(self):
488
if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
490
'Fetching from Git to Bazaar repository. '
491
'For better performance, fetch into a Git repository.')
493
def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
494
"""Fetch objects from a remote server.
496
:param determine_wants: determine_wants callback
497
:param mapping: BzrGitMapping to use
498
:param limit: Maximum number of commits to import.
499
:return: Tuple with pack hint, last imported revision id and remote refs
501
raise NotImplementedError(self.fetch_objects)
503
def get_determine_wants_revids(self, revids, include_tags=False):
505
for revid in set(revids):
506
if self.target.has_revision(revid):
508
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
510
return self.get_determine_wants_heads(wants, include_tags=include_tags)
512
def fetch(self, revision_id=None, find_ghosts=False,
513
mapping=None, fetch_spec=None, include_tags=False):
515
mapping = self.source.get_mapping()
516
if revision_id is not None:
517
interesting_heads = [revision_id]
518
elif fetch_spec is not None:
519
recipe = fetch_spec.get_recipe()
520
if recipe[0] in ("search", "proxy-search"):
521
interesting_heads = recipe[1]
523
raise AssertionError("Unsupported search result type %s" %
526
interesting_heads = None
528
if interesting_heads is not None:
529
determine_wants = self.get_determine_wants_revids(
530
interesting_heads, include_tags=include_tags)
532
determine_wants = self.determine_wants_all
534
(pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
536
if pack_hint is not None and self.target._format.pack_compresses:
537
self.target.pack(hint=pack_hint)
541
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
542
"""InterRepository that copies revisions from a remote Git into a non-Git
545
def get_target_heads(self):
546
# FIXME: This should be more efficient
547
all_revs = self.target.all_revision_ids()
548
parent_map = self.target.get_parent_map(all_revs)
550
for values in viewvalues(parent_map):
551
all_parents.update(values)
552
return set(all_revs) - all_parents
554
def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
555
"""See `InterGitNonGitRepository`."""
557
store = get_object_store(self.target, mapping)
558
with store.lock_write():
559
heads = self.get_target_heads()
560
graph_walker = ObjectStoreGraphWalker(
561
[store._lookup_revision_sha1(head) for head in heads],
562
lambda sha: store[sha].parents)
563
wants_recorder = DetermineWantsRecorder(determine_wants)
565
pb = ui.ui_factory.nested_progress_bar()
567
objects_iter = self.source.fetch_objects(
568
wants_recorder, graph_walker, store.get_raw)
569
trace.mutter("Importing %d new revisions",
570
len(wants_recorder.wants))
571
(pack_hint, last_rev) = import_git_objects(self.target,
572
mapping, objects_iter, store, wants_recorder.wants, pb,
574
return (pack_hint, last_rev, wants_recorder.remote_refs)
579
def is_compatible(source, target):
580
"""Be compatible with GitRepository."""
581
if not isinstance(source, RemoteGitRepository):
583
if not target.supports_rich_root():
585
if isinstance(target, GitRepository):
587
if not getattr(target._format, "supports_full_versioned_files", True):
592
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
593
"""InterRepository that copies revisions from a local Git into a non-Git
596
def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
597
"""See `InterGitNonGitRepository`."""
599
remote_refs = self.source.controldir.get_refs_container().as_dict()
600
wants = determine_wants(remote_refs)
602
pb = ui.ui_factory.nested_progress_bar()
603
target_git_object_retriever = get_object_store(self.target, mapping)
605
target_git_object_retriever.lock_write()
607
(pack_hint, last_rev) = import_git_objects(self.target,
608
mapping, self.source._git.object_store,
609
target_git_object_retriever, wants, pb, limit)
610
return (pack_hint, last_rev, remote_refs)
612
target_git_object_retriever.unlock()
617
def is_compatible(source, target):
618
"""Be compatible with GitRepository."""
619
if not isinstance(source, LocalGitRepository):
621
if not target.supports_rich_root():
623
if isinstance(target, GitRepository):
625
if not getattr(target._format, "supports_full_versioned_files", True):
630
class InterGitGitRepository(InterFromGitRepository):
631
"""InterRepository that copies between Git repositories."""
633
def fetch_refs(self, update_refs, lossy, overwrite=False):
635
raise LossyPushToSameVCS(self.source, self.target)
636
old_refs = self.target.controldir.get_refs_container()
638
def determine_wants(heads):
639
old_refs = dict([(k, (v, None)) for (k, v) in viewitems(heads.as_dict())])
640
new_refs = update_refs(old_refs)
641
ref_changes.update(new_refs)
642
return [sha1 for (sha1, bzr_revid) in viewvalues(new_refs)]
643
self.fetch_objects(determine_wants, lossy=lossy)
644
for k, (git_sha, bzr_revid) in viewitems(ref_changes):
645
self.target._git.refs[k] = git_sha
646
new_refs = self.target.controldir.get_refs_container()
647
return None, old_refs, new_refs
649
def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
650
raise NotImplementedError(self.fetch_objects)
652
def _target_has_shas(self, shas):
653
return set([sha for sha in shas if sha in self.target._git.object_store])
655
def fetch(self, revision_id=None, find_ghosts=False,
656
mapping=None, fetch_spec=None, branches=None, limit=None, include_tags=False):
658
mapping = self.source.get_mapping()
659
if revision_id is not None:
661
elif fetch_spec is not None:
662
recipe = fetch_spec.get_recipe()
663
if recipe[0] in ("search", "proxy-search"):
666
raise AssertionError(
667
"Unsupported search result type %s" % recipe[0])
669
if branches is not None:
670
def determine_wants(refs):
672
for name, value in viewitems(refs):
673
if value == ZERO_SHA:
676
if name in branches or (include_tags and is_tag(name)):
679
elif fetch_spec is None and revision_id is None:
680
determine_wants = self.determine_wants_all
682
determine_wants = self.get_determine_wants_revids(args, include_tags=include_tags)
683
wants_recorder = DetermineWantsRecorder(determine_wants)
684
self.fetch_objects(wants_recorder, mapping, limit=limit)
685
return wants_recorder.remote_refs
687
def get_determine_wants_revids(self, revids, include_tags=False):
689
for revid in set(revids):
690
if revid == NULL_REVISION:
692
git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
694
return self.get_determine_wants_heads(wants, include_tags=include_tags)
696
def determine_wants_all(self, refs):
697
potential = set([v for v in refs.values() if not v == ZERO_SHA])
698
return list(potential - self._target_has_shas(potential))
701
class InterLocalGitLocalGitRepository(InterGitGitRepository):
703
def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
705
raise LossyPushToSameVCS(self.source, self.target)
706
if limit is not None:
707
raise FetchLimitUnsupported(self)
708
refs = self.source._git.fetch(self.target._git, determine_wants)
709
return (None, None, refs)
712
def is_compatible(source, target):
713
"""Be compatible with GitRepository."""
714
return (isinstance(source, LocalGitRepository) and
715
isinstance(target, LocalGitRepository))
718
class InterRemoteGitLocalGitRepository(InterGitGitRepository):
720
def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
722
raise LossyPushToSameVCS(self.source, self.target)
723
if limit is not None:
724
raise FetchLimitUnsupported(self)
725
graphwalker = self.target._git.get_graph_walker()
726
if CAPABILITY_THIN_PACK in self.source.controldir._client._fetch_capabilities:
727
# TODO(jelmer): Avoid reading entire file into memory and
728
# only processing it after the whole file has been fetched.
734
self.target._git.object_store.move_in_thin_pack(f)
739
f, commit, abort = self.target._git.object_store.add_pack()
741
refs = self.source.controldir.fetch_pack(
742
determine_wants, graphwalker, f.write)
744
return (None, None, refs)
745
except BaseException:
750
def is_compatible(source, target):
751
"""Be compatible with GitRepository."""
752
return (isinstance(source, RemoteGitRepository) and
753
isinstance(target, LocalGitRepository))