/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/interrepo.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-17 00:47:52 UTC
  • mfrom: (7182 work)
  • mto: This revision was merged to the branch mainline in revision 7305.
  • Revision ID: jelmer@jelmer.uk-20181117004752-6ywampe5pfywlby4
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
    remote_divergence,
80
80
    )
81
81
from .refs import (
82
 
    get_refs_container,
83
82
    is_tag,
84
83
    )
85
84
from .repository import (
126
125
        raise NotImplementedError(self.fetch_refs)
127
126
 
128
127
    def search_missing_revision_ids(self,
129
 
            find_ghosts=True, revision_ids=None, if_present_ids=None,
130
 
            limit=None):
 
128
                                    find_ghosts=True, revision_ids=None,
 
129
                                    if_present_ids=None, limit=None):
131
130
        if limit is not None:
132
131
            raise FetchLimitUnsupported(self)
133
132
        git_shas = []
145
144
                except KeyError:
146
145
                    raise NoSuchRevision(revid, self.source)
147
146
                git_shas.append(git_sha)
148
 
            walker = Walker(self.source_store,
149
 
                include=git_shas, exclude=[
 
147
            walker = Walker(
 
148
                self.source_store,
 
149
                include=git_shas,
 
150
                exclude=[
150
151
                    sha for sha in self.target.controldir.get_refs_container().as_dict().values()
151
152
                    if sha != ZERO_SHA])
152
153
            missing_revids = set()
153
154
            for entry in walker:
154
 
                for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
 
155
                for (kind, type_data) in self.source_store.lookup_git_sha(
 
156
                        entry.commit.id):
155
157
                    if kind == "commit":
156
158
                        missing_revids.add(type_data[0])
157
159
            return self.source.revision_ids_to_search_result(missing_revids)
220
222
                new_stop_revids = []
221
223
                for revid in stop_revids:
222
224
                    sha1 = revid_sha_map.get(revid)
223
 
                    if (not revid in missing and
224
 
                        self._revision_needs_fetching(sha1, revid)):
 
225
                    if (revid not in missing and
 
226
                            self._revision_needs_fetching(sha1, revid)):
225
227
                        missing.add(revid)
226
228
                        new_stop_revids.append(revid)
227
229
                stop_revids = set()
240
242
            with Git SHA, Bazaar revid as values.
241
243
        """
242
244
        bzr_refs = {}
243
 
        refs = {}
244
245
        for k in self.target._git.refs.allkeys():
245
246
            try:
246
247
                v = self.target._git.refs.read_ref(k)
250
251
            revid = None
251
252
            if not v.startswith(SYMREF):
252
253
                try:
253
 
                    for (kind, type_data) in self.source_store.lookup_git_sha(v):
254
 
                        if kind == "commit" and self.source.has_revision(type_data[0]):
 
254
                    for (kind, type_data) in self.source_store.lookup_git_sha(
 
255
                            v):
 
256
                        if kind == "commit" and self.source.has_revision(
 
257
                                type_data[0]):
255
258
                            revid = type_data[0]
256
259
                            break
257
260
                except KeyError:
265
268
            old_refs = self._get_target_bzr_refs()
266
269
            new_refs = update_refs(old_refs)
267
270
            revidmap = self.fetch_objects(
268
 
                [(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)
 
271
                [(git_sha, bzr_revid)
 
272
                 for (git_sha, bzr_revid) in new_refs.values()
 
273
                 if git_sha is None or not git_sha.startswith(SYMREF)],
 
274
                lossy=lossy)
269
275
            for name, (gitid, revid) in viewitems(new_refs):
270
276
                if gitid is None:
271
277
                    try:
273
279
                    except KeyError:
274
280
                        gitid = self.source_store._lookup_revision_sha1(revid)
275
281
                if gitid.startswith(SYMREF):
276
 
                    self.target_refs.set_symbolic_ref(name, gitid[len(SYMREF):])
 
282
                    self.target_refs.set_symbolic_ref(
 
283
                        name, gitid[len(SYMREF):])
277
284
                else:
278
285
                    try:
279
286
                        old_git_id = old_refs[name][0]
286
293
    def fetch_objects(self, revs, lossy, limit=None):
287
294
        if not lossy and not self.mapping.roundtripping:
288
295
            for git_sha, bzr_revid in revs:
289
 
                if bzr_revid is not None and needs_roundtripping(self.source, bzr_revid):
 
296
                if (bzr_revid is not None and
 
297
                        needs_roundtripping(self.source, bzr_revid)):
290
298
                    raise NoPushSupport(self.source, self.target, self.mapping,
291
299
                                        bzr_revid)
292
300
        with self.source_store.lock_read():
297
305
                object_generator = MissingObjectsIterator(
298
306
                    self.source_store, self.source, pb)
299
307
                for (old_revid, git_sha) in object_generator.import_revisions(
300
 
                    todo, lossy=lossy):
 
308
                        todo, lossy=lossy):
301
309
                    if lossy:
302
 
                        new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
 
310
                        new_revid = self.mapping.revision_id_foreign_to_bzr(
 
311
                            git_sha)
303
312
                    else:
304
313
                        new_revid = old_revid
305
314
                        try:
314
323
                pb.finished()
315
324
 
316
325
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
317
 
            fetch_spec=None, mapped_refs=None):
 
326
              fetch_spec=None, mapped_refs=None):
318
327
        if mapped_refs is not None:
319
328
            stop_revisions = mapped_refs
320
329
        elif revision_id is not None:
324
333
            if recipe[0] in ("search", "proxy-search"):
325
334
                stop_revisions = [(None, revid) for revid in recipe[1]]
326
335
            else:
327
 
                raise AssertionError("Unsupported search result type %s" % recipe[0])
 
336
                raise AssertionError(
 
337
                    "Unsupported search result type %s" % recipe[0])
328
338
        else:
329
 
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
339
            stop_revisions = [(None, revid)
 
340
                              for revid in self.source.all_revision_ids()]
330
341
        self._warn_slow()
331
342
        try:
332
343
            self.fetch_objects(stop_revisions, lossy=False)
348
359
            raise NoPushSupport(self.source, self.target, self.mapping)
349
360
        unpeel_map = UnpeelMap.from_repository(self.source)
350
361
        revidmap = {}
 
362
 
351
363
        def git_update_refs(old_refs):
352
364
            ret = {}
353
 
            self.old_refs = dict([(k, (v, None)) for (k, v) in viewitems(old_refs)])
 
365
            self.old_refs = dict([(k, (v, None))
 
366
                                  for (k, v) in viewitems(old_refs)])
354
367
            self.new_refs = update_refs(self.old_refs)
355
368
            for name, (gitid, revid) in viewitems(self.new_refs):
356
369
                if gitid is None:
357
370
                    git_sha = self.source_store._lookup_revision_sha1(revid)
358
 
                    gitid = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
 
371
                    gitid = unpeel_map.re_unpeel_tag(
 
372
                        git_sha, old_refs.get(name))
359
373
                if not overwrite:
360
 
                    if remote_divergence(old_refs.get(name), gitid, self.source_store):
 
374
                    if remote_divergence(
 
375
                            old_refs.get(name), gitid, self.source_store):
361
376
                        raise DivergedBranches(self.source, self.target)
362
377
                ret[name] = gitid
363
378
            return ret
364
379
        self._warn_slow()
365
380
        with self.source_store.lock_read():
366
 
            new_refs = self.target.send_pack(git_update_refs,
367
 
                    self.source_store.generate_lossy_pack_data)
 
381
            new_refs = self.target.send_pack(
 
382
                git_update_refs, self.source_store.generate_lossy_pack_data)
368
383
        # FIXME: revidmap?
369
384
        return revidmap, self.old_refs, self.new_refs
370
385
 
398
413
 
399
414
    def get_determine_wants_heads(self, wants, include_tags=False):
400
415
        wants = set(wants)
 
416
 
401
417
        def determine_wants(refs):
402
 
            potential = set(wants)
 
418
            unpeel_lookup = {}
 
419
            for k, v in viewitems(refs):
 
420
                if k.endswith(ANNOTATED_TAG_SUFFIX):
 
421
                    unpeel_lookup[v] = refs[k[:-len(ANNOTATED_TAG_SUFFIX)]]
 
422
            potential = set([unpeel_lookup.get(w, w) for w in wants])
403
423
            if include_tags:
404
 
                for k, unpeeled in viewitems(refs):
 
424
                for k, sha in viewitems(refs):
405
425
                    if k.endswith(ANNOTATED_TAG_SUFFIX):
406
426
                        continue
407
427
                    if not is_tag(k):
408
428
                        continue
409
 
                    if unpeeled == ZERO_SHA:
 
429
                    if sha == ZERO_SHA:
410
430
                        continue
411
 
                    potential.add(unpeeled)
 
431
                    potential.add(sha)
412
432
            return list(potential - self._target_has_shas(potential))
413
433
        return determine_wants
414
434
 
424
444
        self.fetch(revision_id, find_ghosts=False)
425
445
 
426
446
    def search_missing_revision_ids(self,
427
 
            find_ghosts=True, revision_ids=None, if_present_ids=None,
428
 
            limit=None):
 
447
                                    find_ghosts=True, revision_ids=None,
 
448
                                    if_present_ids=None, limit=None):
429
449
        if limit is not None:
430
450
            raise FetchLimitUnsupported(self)
431
451
        if revision_ids is None and if_present_ids is None:
473
493
            potential.add(self.source.controldir.get_peeled(k) or v)
474
494
        return list(potential - self._target_has_shas(potential))
475
495
 
476
 
    def get_determine_wants_heads(self, wants, include_tags=False):
477
 
        wants = set(wants)
478
 
        def determine_wants(refs):
479
 
            potential = set(wants)
480
 
            if include_tags:
481
 
                for k, unpeeled in viewitems(refs):
482
 
                    if not is_tag(k):
483
 
                        continue
484
 
                    if unpeeled == ZERO_SHA:
485
 
                        continue
486
 
                    potential.add(self.source.controldir.get_peeled(k) or unpeeled)
487
 
            return list(potential - self._target_has_shas(potential))
488
 
        return determine_wants
489
 
 
490
496
    def _warn_slow(self):
491
497
        if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
492
498
            trace.warning(
499
505
        :param determine_wants: determine_wants callback
500
506
        :param mapping: BzrGitMapping to use
501
507
        :param limit: Maximum number of commits to import.
502
 
        :return: Tuple with pack hint, last imported revision id and remote refs
 
508
        :return: Tuple with pack hint, last imported revision id and remote
 
509
            refs
503
510
        """
504
511
        raise NotImplementedError(self.fetch_objects)
505
512
 
524
531
                interesting_heads = recipe[1]
525
532
            else:
526
533
                raise AssertionError("Unsupported search result type %s" %
527
 
                        recipe[0])
 
534
                                     recipe[0])
528
535
        else:
529
536
            interesting_heads = None
530
537
 
535
542
            determine_wants = self.determine_wants_all
536
543
 
537
544
        (pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
538
 
            mapping)
 
545
                                                         mapping)
539
546
        if pack_hint is not None and self.target._format.pack_compresses:
540
547
            self.target.pack(hint=pack_hint)
541
548
        return remote_refs
571
578
                    wants_recorder, graph_walker, store.get_raw)
572
579
                trace.mutter("Importing %d new revisions",
573
580
                             len(wants_recorder.wants))
574
 
                (pack_hint, last_rev) = import_git_objects(self.target,
575
 
                    mapping, objects_iter, store, wants_recorder.wants, pb,
576
 
                    limit)
 
581
                (pack_hint, last_rev) = import_git_objects(
 
582
                    self.target, mapping, objects_iter, store,
 
583
                    wants_recorder.wants, pb, limit)
577
584
                return (pack_hint, last_rev, wants_recorder.remote_refs)
578
585
            finally:
579
586
                pb.finished()
601
608
        self._warn_slow()
602
609
        remote_refs = self.source.controldir.get_refs_container().as_dict()
603
610
        wants = determine_wants(remote_refs)
604
 
        create_pb = None
605
611
        pb = ui.ui_factory.nested_progress_bar()
606
612
        target_git_object_retriever = get_object_store(self.target, mapping)
607
613
        try:
608
614
            target_git_object_retriever.lock_write()
609
615
            try:
610
 
                (pack_hint, last_rev) = import_git_objects(self.target,
611
 
                    mapping, self.source._git.object_store,
 
616
                (pack_hint, last_rev) = import_git_objects(
 
617
                    self.target, mapping, self.source._git.object_store,
612
618
                    target_git_object_retriever, wants, pb, limit)
613
619
                return (pack_hint, last_rev, remote_refs)
614
620
            finally:
638
644
            raise LossyPushToSameVCS(self.source, self.target)
639
645
        old_refs = self.target.controldir.get_refs_container()
640
646
        ref_changes = {}
 
647
 
641
648
        def determine_wants(heads):
642
 
            old_refs = dict([(k, (v, None)) for (k, v) in viewitems(heads.as_dict())])
 
649
            old_refs = dict([(k, (v, None))
 
650
                             for (k, v) in viewitems(heads.as_dict())])
643
651
            new_refs = update_refs(old_refs)
644
652
            ref_changes.update(new_refs)
645
653
            return [sha1 for (sha1, bzr_revid) in viewvalues(new_refs)]
649
657
        new_refs = self.target.controldir.get_refs_container()
650
658
        return None, old_refs, new_refs
651
659
 
652
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
660
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
661
                      lossy=False):
653
662
        raise NotImplementedError(self.fetch_objects)
654
663
 
655
664
    def _target_has_shas(self, shas):
656
 
        return set([sha for sha in shas if sha in self.target._git.object_store])
 
665
        return set(
 
666
            [sha for sha in shas if sha in self.target._git.object_store])
657
667
 
658
668
    def fetch(self, revision_id=None, find_ghosts=False,
659
 
              mapping=None, fetch_spec=None, branches=None, limit=None, include_tags=False):
 
669
              mapping=None, fetch_spec=None, branches=None, limit=None,
 
670
              include_tags=False):
660
671
        if mapping is None:
661
672
            mapping = self.source.get_mapping()
662
673
        if revision_id is not None:
682
693
        elif fetch_spec is None and revision_id is None:
683
694
            determine_wants = self.determine_wants_all
684
695
        else:
685
 
            determine_wants = self.get_determine_wants_revids(args, include_tags=include_tags)
 
696
            determine_wants = self.get_determine_wants_revids(
 
697
                args, include_tags=include_tags)
686
698
        wants_recorder = DetermineWantsRecorder(determine_wants)
687
699
        self.fetch_objects(wants_recorder, mapping, limit=limit)
688
700
        return wants_recorder.remote_refs
705
717
 
706
718
class InterLocalGitLocalGitRepository(InterGitGitRepository):
707
719
 
708
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
720
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
721
                      lossy=False):
709
722
        if lossy:
710
723
            raise LossyPushToSameVCS(self.source, self.target)
711
724
        if limit is not None:
715
728
        progress = DefaultProgressReporter(pb).progress
716
729
        try:
717
730
            refs = self.source._git.fetch(
718
 
                    self.target._git, determine_wants,
719
 
                    progress=progress)
 
731
                self.target._git, determine_wants,
 
732
                progress=progress)
720
733
        finally:
721
734
            pb.finished()
722
735
        return (None, None, refs)
730
743
 
731
744
class InterRemoteGitLocalGitRepository(InterGitGitRepository):
732
745
 
733
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
746
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
747
                      lossy=False):
734
748
        if lossy:
735
749
            raise LossyPushToSameVCS(self.source, self.target)
736
750
        if limit is not None:
737
751
            raise FetchLimitUnsupported(self)
738
752
        graphwalker = self.target._git.get_graph_walker()
739
 
        if CAPABILITY_THIN_PACK in self.source.controldir._client._fetch_capabilities:
 
753
        if (CAPABILITY_THIN_PACK in
 
754
                self.source.controldir._client._fetch_capabilities):
740
755
            # TODO(jelmer): Avoid reading entire file into memory and
741
756
            # only processing it after the whole file has been fetched.
742
757
            f = BytesIO()