~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/directory_service.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
21
"""
22
22
 
 
23
from __future__ import absolute_import
 
24
 
23
25
from bzrlib import (
24
26
    errors,
25
27
    registry,
28
30
lazy_import(globals(), """
29
31
from bzrlib import (
30
32
    branch as _mod_branch,
 
33
    controldir as _mod_controldir,
31
34
    urlutils,
32
35
    )
33
36
""")
64
67
 
65
68
directories = DirectoryServiceRegistry()
66
69
 
67
 
 
68
70
class AliasDirectory(object):
69
71
    """Directory lookup for locations associated with a branch.
70
72
 
72
74
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
73
75
    """
74
76
 
 
77
    branch_aliases = registry.Registry()
 
78
    branch_aliases.register('parent', lambda b: b.get_parent(),
 
79
        help="The parent of this branch.")
 
80
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
 
81
        help="The submit branch for this branch.")
 
82
    branch_aliases.register('public', lambda b: b.get_public_branch(),
 
83
        help="The public location of this branch.")
 
84
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
 
85
        help="The branch this branch is bound to, for bound branches.")
 
86
    branch_aliases.register('push', lambda b: b.get_push_location(),
 
87
        help="The saved location used for `bzr push` with no arguments.")
 
88
    branch_aliases.register('this', lambda b: b.base,
 
89
        help="This branch.")
 
90
 
75
91
    def look_up(self, name, url):
76
92
        branch = _mod_branch.Branch.open_containing('.')[0]
77
 
        lookups = {
78
 
            'parent': branch.get_parent,
79
 
            'submit': branch.get_submit_branch,
80
 
            'public': branch.get_public_branch,
81
 
            'bound': branch.get_bound_location,
82
 
            'push': branch.get_push_location,
83
 
            'this': lambda: branch.base
84
 
        }
85
93
        parts = url.split('/', 1)
86
94
        if len(parts) == 2:
87
95
            name, extra = parts
89
97
            (name,) = parts
90
98
            extra = None
91
99
        try:
92
 
            method = lookups[name[1:]]
 
100
            method = self.branch_aliases.get(name[1:])
93
101
        except KeyError:
94
102
            raise errors.InvalidLocationAlias(url)
95
103
        else:
96
 
            result = method()
 
104
            result = method(branch)
97
105
        if result is None:
98
106
            raise errors.UnsetLocationAlias(url)
99
107
        if extra is not None:
100
108
            result = urlutils.join(result, extra)
101
109
        return result
102
110
 
 
111
    @classmethod
 
112
    def help_text(cls, topic):
 
113
        alias_lines = []
 
114
        for key in cls.branch_aliases.keys():
 
115
            help = cls.branch_aliases.get_help(key)
 
116
            alias_lines.append("  :%-10s%s\n" % (key, help))
 
117
        return """\
 
118
Location aliases
 
119
================
 
120
 
 
121
Bazaar defines several aliases for locations associated with a branch.  These
 
122
can be used with most commands that expect a location, such as `bzr push`.
 
123
 
 
124
The aliases are::
 
125
 
 
126
%s
 
127
For example, to push to the parent location::
 
128
 
 
129
    bzr push :parent
 
130
""" % "".join(alias_lines)
 
131
 
 
132
 
103
133
directories.register(':', AliasDirectory,
104
134
                     'Easy access to remembered branch locations')
 
135
 
 
136
 
 
137
class ColocatedDirectory(object):
 
138
    """Directory lookup for colocated branches.
 
139
 
 
140
    co:somename will resolve to the colocated branch with "somename" in
 
141
    the current directory.
 
142
    """
 
143
 
 
144
    def look_up(self, name, url):
 
145
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
 
146
        return urlutils.join_segment_parameters(dir.user_url,
 
147
            {"branch": urlutils.escape(name)})
 
148
 
 
149
 
 
150
directories.register('co:', ColocatedDirectory,
 
151
                     'Easy access to colocated branches')
 
152