/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 branch.py

Merge roundtrip support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
    NoSuchRef,
52
52
    )
53
53
from bzrlib.plugins.git.refs import (
 
54
    branch_name_to_ref,
54
55
    ref_to_branch_name,
55
56
    extract_tags,
56
57
    tag_name_to_ref,
97
98
                mutter("Tag %s points at object %r that is not a commit, "
98
99
                       "ignoring", k, obj)
99
100
                continue
100
 
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
101
            ret[k] = self.branch.lookup_foreign_revision_id(v)
101
102
        return ret
102
103
 
103
104
    def _set_tag_dict(self, to_dict):
246
247
        # perhaps should escape this ?
247
248
        if self.head is None:
248
249
            return revision.NULL_REVISION
249
 
        return self.mapping.revision_id_foreign_to_bzr(self.head)
 
250
        return self.lookup_foreign_revision_id(self.head)
250
251
 
251
252
    def _basic_push(self, target, overwrite=False, stop_revision=None):
252
253
        return branch.InterBranch.get(self, target)._basic_push(
253
254
            overwrite, stop_revision)
254
255
 
 
256
    def lookup_foreign_revision_id(self, foreign_revid):
 
257
        return self.repository.lookup_foreign_revision_id(foreign_revid, 
 
258
            self.mapping)
 
259
 
255
260
 
256
261
class LocalGitBranch(GitBranch):
257
262
    """A local Git branch."""
339
344
 
340
345
    def supports_tags(self):
341
346
        return True
342
 
 
 
347
    
343
348
 
344
349
class GitBranchPullResult(branch.PullResult):
345
350
 
 
351
    def __init__(self):
 
352
        super(GitBranchPullResult, self).__init__()
 
353
        self.new_git_head = None
 
354
        self._old_revno = None
 
355
        self._new_revno = None
 
356
 
346
357
    def report(self, to_file):
347
358
        if not is_quiet():
348
359
            if self.old_revid == self.new_revid:
354
365
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
355
366
        self._show_tag_conficts(to_file)
356
367
 
 
368
    def _lookup_revno(self, revid):
 
369
        assert isinstance(revid, str), "was %r" % revid
 
370
        # Try in source branch first, it'll be faster
 
371
        try:
 
372
            return self.source_branch.revision_id_to_revno(revid)
 
373
        except errors.NoSuchRevision:
 
374
            # FIXME: Check using graph.find_distance_to_null() ?
 
375
            return self.target_branch.revision_id_to_revno(revid)
 
376
 
 
377
    def _get_old_revno(self):
 
378
        if self._old_revno is not None:
 
379
            return self._old_revno
 
380
        return self._lookup_revno(self.old_revid)
 
381
 
 
382
    def _set_old_revno(self, revno):
 
383
        self._old_revno = revno
 
384
 
 
385
    old_revno = property(_get_old_revno, _set_old_revno)
 
386
 
 
387
    def _get_new_revno(self):
 
388
        if self._new_revno is not None:
 
389
            return self._new_revno
 
390
        return self._lookup_revno(self.new_revid)
 
391
 
 
392
    def _set_new_revno(self, revno):
 
393
        self._new_revno = revno
 
394
    
 
395
    new_revno = property(_get_new_revno, _set_new_revno)
 
396
 
357
397
 
358
398
class GitBranchPushResult(branch.BranchPushResult):
359
399
 
407
447
                    stop_revision)
408
448
            else:
409
449
                head = heads[self.source.ref]
410
 
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
411
 
                    head)
 
450
                self._last_revid = self.source.lookup_foreign_revision_id(head)
412
451
            if self.target.repository.has_revision(self._last_revid):
413
452
                return []
414
453
            return [head]
415
454
        pack_hint, head = interrepo.fetch_objects(
416
455
            determine_wants, self.source.mapping, limit=limit)
417
 
        if pack_hint is not None and self.target.repository._format.pack_compresses:
 
456
        if (pack_hint is not None and
 
457
            self.target.repository._format.pack_compresses):
418
458
            self.target.repository.pack(hint=pack_hint)
419
459
        if head is not None:
420
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
 
460
            self._last_revid = self.source.lookup_foreign_revision_id(head)
421
461
        if overwrite:
422
462
            prev_last_revid = None
423
463
        else:
517
557
            stop_revision = self.source.last_revision()
518
558
        # FIXME: Check for diverged branches
519
559
        def get_changed_refs(old_refs):
520
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
 
560
            result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
521
561
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
522
562
            result.new_revid = stop_revision
523
563
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
550
590
 
551
591
    def update_tags(self, refs):
552
592
        for name, v in extract_tags(refs).iteritems():
553
 
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
 
593
            revid = self.target.lookup_foreign_revision_id(v)
554
594
            self.target.tags.set_tag(name, revid)
555
595
 
556
596
    def update_refs(self, stop_revision=None):
558
598
            self.target.repository)
559
599
        if stop_revision is None:
560
600
            refs = interrepo.fetch_refs(branches=["HEAD"])
561
 
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
 
601
            stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
562
602
        else:
563
603
            refs = interrepo.fetch_refs(revision_id=stop_revision)
564
604
        return refs, stop_revision
594
634
    def update_revisions(self, *args, **kwargs):
595
635
        raise NoPushSupport()
596
636
 
597
 
    def push(self, overwrite=True, stop_revision=None,
598
 
             _override_hook_source_branch=None):
599
 
        raise NoPushSupport()
600
 
 
601
 
    def lossy_push(self, stop_revision=None):
602
 
        from dulwich.protocol import ZERO_SHA
603
 
        result = GitBranchPushResult()
604
 
        result.source_branch = self.source
605
 
        result.target_branch = self.target
 
637
    def _get_new_refs(self, stop_revision=None):
606
638
        if stop_revision is None:
607
639
            stop_revision = self.source.last_revision()
608
 
        # FIXME: Check for diverged branches
609
 
        refs = { self.target.ref: stop_revision }
 
640
        refs = {
 
641
            branch_name_to_ref(self.target.name, "refs/heads/master"): stop_revision }
610
642
        for name, revid in self.source.tags.get_tag_dict().iteritems():
611
643
            if self.source.repository.has_revision(revid):
612
644
                refs[tag_name_to_ref(name)] = revid
613
 
        revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
 
645
        return refs
 
646
 
 
647
    def pull(self, overwrite=False, stop_revision=None, local=False,
 
648
             possible_transports=None):
 
649
        from dulwich.protocol import ZERO_SHA
 
650
        result = GitBranchPullResult()
 
651
        result.source_branch = self.source
 
652
        result.target_branch = self.target
 
653
        # FIXME: Check for diverged branches
 
654
        old_refs = self.target.repository._git.get_refs()
 
655
        refs = dict(old_refs)
 
656
        refs.update(self._get_new_refs(stop_revision))
 
657
        self.target.repository.fetch_refs(self.source.repository, refs)
 
658
        result.old_revid = self.target.lookup_foreign_revision_id(
 
659
            old_refs.get(self.target.ref, ZERO_SHA))
 
660
        result.new_revid = refs[self.target.ref]
 
661
        return result
 
662
 
 
663
    def push(self, overwrite=False, stop_revision=None,
 
664
             _override_hook_source_branch=None):
 
665
        from dulwich.protocol import ZERO_SHA
 
666
        result = GitBranchPushResult()
 
667
        result.source_branch = self.source
 
668
        result.target_branch = self.target
 
669
        # FIXME: Check for diverged branches
 
670
        old_refs = self.target.repository._git.get_refs()
 
671
        refs = dict(old_refs)
 
672
        refs.update(self._get_new_refs(stop_revision))
 
673
        self.target.repository.fetch_refs(self.source.repository, refs)
 
674
        result.old_revid = self.target.lookup_foreign_revision_id(
 
675
            old_refs.get(self.target.ref, ZERO_SHA))
 
676
        result.new_revid = refs[self.target.ref]
 
677
        return result
 
678
 
 
679
    def lossy_push(self, stop_revision=None):
 
680
        from dulwich.protocol import ZERO_SHA
 
681
        result = GitBranchPushResult()
 
682
        result.source_branch = self.source
 
683
        result.target_branch = self.target
 
684
        # FIXME: Check for diverged branches
 
685
        refs = self._get_new_refs(stop_revision)
 
686
        result.revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
614
687
            self.source.repository, refs)
615
 
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
688
        result.old_revid = self.target.lookup_foreign_revision_id(
616
689
            old_refs.get(self.target.ref, ZERO_SHA))
617
 
        result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
690
        result.new_revid = self.target.lookup_foreign_revision_id(
618
691
            new_refs[self.target.ref])
619
 
        result.revidmap = revidmap
620
692
        return result
621
693
 
622
694