/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-16 23:15:15 UTC
  • mfrom: (7180 work)
  • mto: This revision was merged to the branch mainline in revision 7183.
  • Revision ID: jelmer@jelmer.uk-20181116231515-zqd2yn6kj8lfydyp
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:
266
269
            old_refs = self._get_target_bzr_refs()
267
270
            new_refs = update_refs(old_refs)
268
271
            revidmap = self.fetch_objects(
269
 
                [(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)
 
272
                [(git_sha, bzr_revid)
 
273
                 for (git_sha, bzr_revid) in new_refs.values()
 
274
                 if git_sha is None or not git_sha.startswith(SYMREF)],
 
275
                lossy=lossy)
270
276
            for name, (gitid, revid) in viewitems(new_refs):
271
277
                if gitid is None:
272
278
                    try:
274
280
                    except KeyError:
275
281
                        gitid = self.source_store._lookup_revision_sha1(revid)
276
282
                if gitid.startswith(SYMREF):
277
 
                    self.target_refs.set_symbolic_ref(name, gitid[len(SYMREF):])
 
283
                    self.target_refs.set_symbolic_ref(
 
284
                        name, gitid[len(SYMREF):])
278
285
                else:
279
286
                    try:
280
287
                        old_git_id = old_refs[name][0]
288
295
    def fetch_objects(self, revs, lossy, limit=None):
289
296
        if not lossy and not self.mapping.roundtripping:
290
297
            for git_sha, bzr_revid in revs:
291
 
                if bzr_revid is not None and needs_roundtripping(self.source, bzr_revid):
 
298
                if (bzr_revid is not None and
 
299
                        needs_roundtripping(self.source, bzr_revid)):
292
300
                    raise NoPushSupport(self.source, self.target, self.mapping,
293
301
                                        bzr_revid)
294
302
        with self.source_store.lock_read():
299
307
                object_generator = MissingObjectsIterator(
300
308
                    self.source_store, self.source, pb)
301
309
                for (old_revid, git_sha) in object_generator.import_revisions(
302
 
                    todo, lossy=lossy):
 
310
                        todo, lossy=lossy):
303
311
                    if lossy:
304
 
                        new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
 
312
                        new_revid = self.mapping.revision_id_foreign_to_bzr(
 
313
                            git_sha)
305
314
                    else:
306
315
                        new_revid = old_revid
307
316
                        try:
316
325
                pb.finished()
317
326
 
318
327
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
319
 
            fetch_spec=None, mapped_refs=None):
 
328
              fetch_spec=None, mapped_refs=None):
320
329
        if mapped_refs is not None:
321
330
            stop_revisions = mapped_refs
322
331
        elif revision_id is not None:
326
335
            if recipe[0] in ("search", "proxy-search"):
327
336
                stop_revisions = [(None, revid) for revid in recipe[1]]
328
337
            else:
329
 
                raise AssertionError("Unsupported search result type %s" % recipe[0])
 
338
                raise AssertionError(
 
339
                    "Unsupported search result type %s" % recipe[0])
330
340
        else:
331
 
            stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
 
341
            stop_revisions = [(None, revid)
 
342
                              for revid in self.source.all_revision_ids()]
332
343
        self._warn_slow()
333
344
        try:
334
345
            self.fetch_objects(stop_revisions, lossy=False)
350
361
            raise NoPushSupport(self.source, self.target, self.mapping)
351
362
        unpeel_map = UnpeelMap.from_repository(self.source)
352
363
        revidmap = {}
 
364
 
353
365
        def git_update_refs(old_refs):
354
366
            ret = {}
355
 
            self.old_refs = dict([(k, (v, None)) for (k, v) in viewitems(old_refs)])
 
367
            self.old_refs = {
 
368
                k: (v, None) for (k, v) in viewitems(old_refs)}
356
369
            new_refs = update_refs(self.old_refs)
357
370
            for name, (gitid, revid) in viewitems(new_refs):
358
371
                if gitid is None:
359
372
                    git_sha = self.source_store._lookup_revision_sha1(revid)
360
 
                    gitid = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
 
373
                    gitid = unpeel_map.re_unpeel_tag(
 
374
                        git_sha, old_refs.get(name))
361
375
                if not overwrite:
362
 
                    if remote_divergence(old_refs.get(name), gitid, self.source_store):
 
376
                    if remote_divergence(
 
377
                            old_refs.get(name), gitid, self.source_store):
363
378
                        raise DivergedBranches(self.source, self.target)
364
379
                ret[name] = gitid
365
380
            return ret
366
381
        self._warn_slow()
367
382
        with self.source_store.lock_read():
368
 
            new_refs = self.target.send_pack(git_update_refs,
369
 
                    self.source_store.generate_lossy_pack_data)
 
383
            new_refs = self.target.send_pack(
 
384
                git_update_refs, self.source_store.generate_lossy_pack_data)
370
385
        # FIXME: revidmap?
371
386
        return revidmap, self.old_refs, new_refs
372
387
 
400
415
 
401
416
    def get_determine_wants_heads(self, wants, include_tags=False):
402
417
        wants = set(wants)
 
418
 
403
419
        def determine_wants(refs):
404
 
            potential = set(wants)
 
420
            unpeel_lookup = {}
 
421
            for k, v in viewitems(refs):
 
422
                if k.endswith(ANNOTATED_TAG_SUFFIX):
 
423
                    unpeel_lookup[v] = refs[k[:-len(ANNOTATED_TAG_SUFFIX)]]
 
424
            potential = set([unpeel_lookup.get(w, w) for w in wants])
405
425
            if include_tags:
406
 
                for k, unpeeled in viewitems(refs):
 
426
                for k, sha in viewitems(refs):
407
427
                    if k.endswith(ANNOTATED_TAG_SUFFIX):
408
428
                        continue
409
429
                    if not is_tag(k):
410
430
                        continue
411
 
                    if unpeeled == ZERO_SHA:
 
431
                    if sha == ZERO_SHA:
412
432
                        continue
413
 
                    potential.add(unpeeled)
 
433
                    potential.add(sha)
414
434
            return list(potential - self._target_has_shas(potential))
415
435
        return determine_wants
416
436
 
426
446
        self.fetch(revision_id, find_ghosts=False)
427
447
 
428
448
    def search_missing_revision_ids(self,
429
 
            find_ghosts=True, revision_ids=None, if_present_ids=None,
430
 
            limit=None):
 
449
                                    find_ghosts=True, revision_ids=None,
 
450
                                    if_present_ids=None, limit=None):
431
451
        if limit is not None:
432
452
            raise FetchLimitUnsupported(self)
433
453
        if revision_ids is None and if_present_ids is None:
475
495
            potential.add(self.source.controldir.get_peeled(k) or v)
476
496
        return list(potential - self._target_has_shas(potential))
477
497
 
478
 
    def get_determine_wants_heads(self, wants, include_tags=False):
479
 
        wants = set(wants)
480
 
        def determine_wants(refs):
481
 
            potential = set(wants)
482
 
            if include_tags:
483
 
                for k, unpeeled in viewitems(refs):
484
 
                    if not is_tag(k):
485
 
                        continue
486
 
                    if unpeeled == ZERO_SHA:
487
 
                        continue
488
 
                    potential.add(self.source.controldir.get_peeled(k) or unpeeled)
489
 
            return list(potential - self._target_has_shas(potential))
490
 
        return determine_wants
491
 
 
492
498
    def _warn_slow(self):
493
499
        if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
494
500
            trace.warning(
501
507
        :param determine_wants: determine_wants callback
502
508
        :param mapping: BzrGitMapping to use
503
509
        :param limit: Maximum number of commits to import.
504
 
        :return: Tuple with pack hint, last imported revision id and remote refs
 
510
        :return: Tuple with pack hint, last imported revision id and remote
 
511
            refs
505
512
        """
506
513
        raise NotImplementedError(self.fetch_objects)
507
514
 
526
533
                interesting_heads = recipe[1]
527
534
            else:
528
535
                raise AssertionError("Unsupported search result type %s" %
529
 
                        recipe[0])
 
536
                                     recipe[0])
530
537
        else:
531
538
            interesting_heads = None
532
539
 
537
544
            determine_wants = self.determine_wants_all
538
545
 
539
546
        (pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
540
 
            mapping)
 
547
                                                         mapping)
541
548
        if pack_hint is not None and self.target._format.pack_compresses:
542
549
            self.target.pack(hint=pack_hint)
543
550
        return remote_refs
573
580
                    wants_recorder, graph_walker, store.get_raw)
574
581
                trace.mutter("Importing %d new revisions",
575
582
                             len(wants_recorder.wants))
576
 
                (pack_hint, last_rev) = import_git_objects(self.target,
577
 
                    mapping, objects_iter, store, wants_recorder.wants, pb,
578
 
                    limit)
 
583
                (pack_hint, last_rev) = import_git_objects(
 
584
                    self.target, mapping, objects_iter, store,
 
585
                    wants_recorder.wants, pb, limit)
579
586
                return (pack_hint, last_rev, wants_recorder.remote_refs)
580
587
            finally:
581
588
                pb.finished()
603
610
        self._warn_slow()
604
611
        remote_refs = self.source.controldir.get_refs_container().as_dict()
605
612
        wants = determine_wants(remote_refs)
606
 
        create_pb = None
607
613
        pb = ui.ui_factory.nested_progress_bar()
608
614
        target_git_object_retriever = get_object_store(self.target, mapping)
609
615
        try:
610
616
            target_git_object_retriever.lock_write()
611
617
            try:
612
 
                (pack_hint, last_rev) = import_git_objects(self.target,
613
 
                    mapping, self.source._git.object_store,
 
618
                (pack_hint, last_rev) = import_git_objects(
 
619
                    self.target, mapping, self.source._git.object_store,
614
620
                    target_git_object_retriever, wants, pb, limit)
615
621
                return (pack_hint, last_rev, remote_refs)
616
622
            finally:
640
646
            raise LossyPushToSameVCS(self.source, self.target)
641
647
        old_refs = self.target.controldir.get_refs_container()
642
648
        ref_changes = {}
 
649
 
643
650
        def determine_wants(heads):
644
 
            old_refs = dict([(k, (v, None)) for (k, v) in viewitems(heads.as_dict())])
 
651
            old_refs = dict([(k, (v, None))
 
652
                             for (k, v) in viewitems(heads.as_dict())])
645
653
            new_refs = update_refs(old_refs)
646
654
            ref_changes.update(new_refs)
647
655
            return [sha1 for (sha1, bzr_revid) in viewvalues(new_refs)]
651
659
        new_refs = self.target.controldir.get_refs_container()
652
660
        return None, old_refs, new_refs
653
661
 
654
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
662
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
663
                      lossy=False):
655
664
        raise NotImplementedError(self.fetch_objects)
656
665
 
657
666
    def _target_has_shas(self, shas):
658
 
        return set([sha for sha in shas if sha in self.target._git.object_store])
 
667
        return set(
 
668
            [sha for sha in shas if sha in self.target._git.object_store])
659
669
 
660
670
    def fetch(self, revision_id=None, find_ghosts=False,
661
 
              mapping=None, fetch_spec=None, branches=None, limit=None, include_tags=False):
 
671
              mapping=None, fetch_spec=None, branches=None, limit=None,
 
672
              include_tags=False):
662
673
        if mapping is None:
663
674
            mapping = self.source.get_mapping()
664
675
        if revision_id is not None:
684
695
        elif fetch_spec is None and revision_id is None:
685
696
            determine_wants = self.determine_wants_all
686
697
        else:
687
 
            determine_wants = self.get_determine_wants_revids(args, include_tags=include_tags)
 
698
            determine_wants = self.get_determine_wants_revids(
 
699
                args, include_tags=include_tags)
688
700
        wants_recorder = DetermineWantsRecorder(determine_wants)
689
701
        self.fetch_objects(wants_recorder, mapping, limit=limit)
690
702
        return wants_recorder.remote_refs
707
719
 
708
720
class InterLocalGitLocalGitRepository(InterGitGitRepository):
709
721
 
710
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
722
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
723
                      lossy=False):
711
724
        if lossy:
712
725
            raise LossyPushToSameVCS(self.source, self.target)
713
726
        if limit is not None:
714
727
            raise FetchLimitUnsupported(self)
715
 
        refs = self.source._git.fetch(self.target._git, determine_wants)
 
728
        from .remote import DefaultProgressReporter
 
729
        pb = ui.ui_factory.nested_progress_bar()
 
730
        progress = DefaultProgressReporter(pb).progress
 
731
        try:
 
732
            refs = self.source._git.fetch(
 
733
                self.target._git, determine_wants,
 
734
                progress=progress)
 
735
        finally:
 
736
            pb.finished()
716
737
        return (None, None, refs)
717
738
 
718
739
    @staticmethod
724
745
 
725
746
class InterRemoteGitLocalGitRepository(InterGitGitRepository):
726
747
 
727
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None, lossy=False):
 
748
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
 
749
                      lossy=False):
728
750
        if lossy:
729
751
            raise LossyPushToSameVCS(self.source, self.target)
730
752
        if limit is not None:
731
753
            raise FetchLimitUnsupported(self)
732
754
        graphwalker = self.target._git.get_graph_walker()
733
 
        if CAPABILITY_THIN_PACK in self.source.controldir._client._fetch_capabilities:
 
755
        if (CAPABILITY_THIN_PACK in
 
756
                self.source.controldir._client._fetch_capabilities):
734
757
            # TODO(jelmer): Avoid reading entire file into memory and
735
758
            # only processing it after the whole file has been fetched.
736
759
            f = BytesIO()