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

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-03-04 06:21:20 UTC
  • mfrom: (7141.8.7 git-config-branches)
  • Revision ID: breezy.the.bot@gmail.com-20190304062120-pmcmhza3xro5euy7
Support reading related branches from git configs.

Merged from https://code.launchpad.net/~jelmer/brz/git-config-branches/+merge/358140

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
from .unpeel_map import (
77
77
    UnpeelMap,
78
78
    )
79
 
from .urls import git_url_to_bzr_url
 
79
from .urls import (
 
80
    git_url_to_bzr_url,
 
81
    bzr_url_to_git_url,
 
82
    )
80
83
 
81
84
 
82
85
class GitPullResult(branch.PullResult):
464
467
        # Git doesn't do stacking (yet...)
465
468
        raise branch.UnstackableBranchFormat(self._format, self.base)
466
469
 
 
470
    def _get_push_origin(self, cs):
 
471
        """Get the name for the push origin.
 
472
 
 
473
        The exact behaviour is documented in the git-config(1) manpage.
 
474
        """
 
475
        try:
 
476
            return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
 
477
        except KeyError:
 
478
            try:
 
479
                return cs.get((b'branch', ), b'remote')
 
480
            except KeyError:
 
481
                try:
 
482
                    return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
 
483
                except KeyError:
 
484
                    return b'origin'
 
485
 
 
486
    def _get_origin(self, cs):
 
487
        try:
 
488
            return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
 
489
        except KeyError:
 
490
            return b'origin'
 
491
 
 
492
    def _get_related_push_branch(self, cs):
 
493
        remote = self._get_push_origin(cs)
 
494
        try:
 
495
            location = cs.get((b"remote", remote), b"url")
 
496
        except KeyError:
 
497
            return None
 
498
 
 
499
        return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
 
500
 
 
501
    def _get_related_merge_branch(self, cs):
 
502
        remote = self._get_origin(cs)
 
503
        try:
 
504
            location = cs.get((b"remote", remote), b"url")
 
505
        except KeyError:
 
506
            return None
 
507
 
 
508
        try:
 
509
            ref = cs.get((b"branch", remote), b"merge")
 
510
        except KeyError:
 
511
            ref = self.ref
 
512
 
 
513
        return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
 
514
 
467
515
    def _get_parent_location(self):
468
516
        """See Branch.get_parent()."""
469
 
        # FIXME: Set "origin" url from .git/config ?
470
517
        cs = self.repository._git.get_config_stack()
471
 
        try:
472
 
            location = cs.get((b"remote", b'origin'), b"url")
473
 
        except KeyError:
474
 
            return None
475
 
 
476
 
        params = {}
477
 
        try:
478
 
            ref = cs.get((b"remote", b"origin"), b"merge")
479
 
        except KeyError:
480
 
            pass
481
 
        else:
482
 
            if ref != b'HEAD':
483
 
                try:
484
 
                    params['branch'] = urlutils.escape(ref_to_branch_name(ref))
485
 
                except ValueError:
486
 
                    params['ref'] = urlutils.quote_from_bytes(ref)
487
 
 
488
 
        url = git_url_to_bzr_url(location.decode('utf-8'))
489
 
        return urlutils.join_segment_parameters(url, params)
 
518
        return self._get_related_merge_branch(cs)
 
519
 
 
520
    def _write_git_config(self, cs):
 
521
        f = BytesIO()
 
522
        cs.write_to_file(f)
 
523
        self.repository._git._put_named_file('config', f.getvalue())
490
524
 
491
525
    def set_parent(self, location):
492
 
        # FIXME: Set "origin" url in .git/config ?
493
526
        cs = self.repository._git.get_config()
 
527
        remote = self._get_origin(cs)
494
528
        this_url = urlutils.split_segment_parameters(self.user_url)[0]
495
 
        target_url, target_params = urlutils.split_segment_parameters(location)
 
529
        target_url, branch, ref = bzr_url_to_git_url(location)
496
530
        location = urlutils.relative_url(this_url, target_url)
497
 
        cs.set((b"remote", b"origin"), b"url", location)
498
 
        if 'branch' in target_params:
499
 
            cs.set((b"remote", b"origin"), b"merge",
500
 
                   branch_name_to_ref(target_params['branch']))
501
 
        elif 'ref' in target_params:
502
 
            cs.set((b"remote", b"origin"), b"merge",
503
 
                   target_params['ref'])
 
531
        cs.set((b"remote", remote), b"url", location)
 
532
        if branch:
 
533
            cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
 
534
        elif ref:
 
535
            cs.set((b"branch", remote), b"merge", ref)
504
536
        else:
505
537
            # TODO(jelmer): Maybe unset rather than setting to HEAD?
506
 
            cs.set((b"remote", b"origin"), b"merge", 'HEAD')
507
 
        f = BytesIO()
508
 
        cs.write_to_file(f)
509
 
        self.repository._git._put_named_file('config', f.getvalue())
 
538
            cs.set((b"branch", remote), b"merge", b'HEAD')
 
539
        self._write_git_config(cs)
510
540
 
511
541
    def break_lock(self):
512
542
        raise NotImplementedError(self.break_lock)
720
750
    def get_push_location(self):
721
751
        """See Branch.get_push_location."""
722
752
        push_loc = self.get_config_stack().get('push_location')
723
 
        return push_loc
 
753
        if push_loc is not None:
 
754
            return push_loc
 
755
        cs = self.repository._git.get_config_stack()
 
756
        return self._get_related_push_branch(cs)
724
757
 
725
758
    def set_push_location(self, location):
726
759
        """See Branch.set_push_location."""