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

  • Committer: Jelmer Vernooij
  • Date: 2019-01-01 21:38:07 UTC
  • mfrom: (7228 work)
  • mto: This revision was merged to the branch mainline in revision 7233.
  • Revision ID: jelmer@jelmer.uk-20190101213807-ay6uqghz0nnrgjvx
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
21
"""
22
22
 
23
 
from bzrlib import errors, registry
24
 
from bzrlib.lazy_import import lazy_import
 
23
from __future__ import absolute_import
 
24
 
 
25
from . import (
 
26
    errors,
 
27
    registry,
 
28
    )
 
29
from .lazy_import import lazy_import
25
30
lazy_import(globals(), """
26
 
from bzrlib.branch import Branch
27
 
from bzrlib import urlutils
 
31
from breezy import (
 
32
    branch as _mod_branch,
 
33
    controldir as _mod_controldir,
 
34
    urlutils,
 
35
    )
28
36
""")
29
37
 
30
38
 
 
39
class DirectoryLookupFailure(errors.BzrError):
 
40
    """Base type for lookup errors."""
 
41
 
 
42
 
 
43
class InvalidLocationAlias(DirectoryLookupFailure):
 
44
 
 
45
    _fmt = '"%(alias_name)s" is not a valid location alias.'
 
46
 
 
47
    def __init__(self, alias_name):
 
48
        DirectoryLookupFailure.__init__(self, alias_name=alias_name)
 
49
 
 
50
 
 
51
class UnsetLocationAlias(DirectoryLookupFailure):
 
52
 
 
53
    _fmt = 'No %(alias_name)s location assigned.'
 
54
 
 
55
    def __init__(self, alias_name):
 
56
        DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
 
57
 
 
58
 
31
59
class DirectoryServiceRegistry(registry.Registry):
32
60
    """This object maintains and uses a list of directory services.
33
61
 
57
85
        service, name = match
58
86
        return service().look_up(name, url)
59
87
 
 
88
 
60
89
directories = DirectoryServiceRegistry()
61
90
 
62
91
 
67
96
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
68
97
    """
69
98
 
 
99
    branch_aliases = registry.Registry()
 
100
    branch_aliases.register('parent', lambda b: b.get_parent(),
 
101
                            help="The parent of this branch.")
 
102
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
 
103
                            help="The submit branch for this branch.")
 
104
    branch_aliases.register('public', lambda b: b.get_public_branch(),
 
105
                            help="The public location of this branch.")
 
106
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
 
107
                            help="The branch this branch is bound to, for bound branches.")
 
108
    branch_aliases.register('push', lambda b: b.get_push_location(),
 
109
                            help="The saved location used for `brz push` with no arguments.")
 
110
    branch_aliases.register('this', lambda b: b.base,
 
111
                            help="This branch.")
 
112
 
70
113
    def look_up(self, name, url):
71
 
        branch = Branch.open_containing('.')[0]
72
 
        lookups = {
73
 
            'parent': branch.get_parent,
74
 
            'submit': branch.get_submit_branch,
75
 
            'public': branch.get_public_branch,
76
 
            'bound': branch.get_bound_location,
77
 
            'push': branch.get_push_location,
78
 
            'this': lambda: branch.base
79
 
        }
 
114
        branch = _mod_branch.Branch.open_containing('.')[0]
80
115
        parts = url.split('/', 1)
81
116
        if len(parts) == 2:
82
117
            name, extra = parts
84
119
            (name,) = parts
85
120
            extra = None
86
121
        try:
87
 
            method = lookups[name[1:]]
 
122
            method = self.branch_aliases.get(name[1:])
88
123
        except KeyError:
89
 
            raise errors.InvalidLocationAlias(url)
 
124
            raise InvalidLocationAlias(url)
90
125
        else:
91
 
            result = method()
 
126
            result = method(branch)
92
127
        if result is None:
93
 
            raise errors.UnsetLocationAlias(url)
 
128
            raise UnsetLocationAlias(url)
94
129
        if extra is not None:
95
130
            result = urlutils.join(result, extra)
96
131
        return result
97
132
 
 
133
    @classmethod
 
134
    def help_text(cls, topic):
 
135
        alias_lines = []
 
136
        for key in cls.branch_aliases.keys():
 
137
            help = cls.branch_aliases.get_help(key)
 
138
            alias_lines.append("  :%-10s%s\n" % (key, help))
 
139
        return """\
 
140
Location aliases
 
141
================
 
142
 
 
143
Bazaar defines several aliases for locations associated with a branch.  These
 
144
can be used with most commands that expect a location, such as `brz push`.
 
145
 
 
146
The aliases are::
 
147
 
 
148
%s
 
149
For example, to push to the parent location::
 
150
 
 
151
    brz push :parent
 
152
""" % "".join(alias_lines)
 
153
 
 
154
 
98
155
directories.register(':', AliasDirectory,
99
156
                     'Easy access to remembered branch locations')
 
157
 
 
158
 
 
159
class ColocatedDirectory(object):
 
160
    """Directory lookup for colocated branches.
 
161
 
 
162
    co:somename will resolve to the colocated branch with "somename" in
 
163
    the current directory.
 
164
    """
 
165
 
 
166
    def look_up(self, name, url):
 
167
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
 
168
        return urlutils.join_segment_parameters(dir.user_url,
 
169
                                                {"branch": urlutils.escape(name)})
 
170
 
 
171
 
 
172
directories.register('co:', ColocatedDirectory,
 
173
                     'Easy access to colocated branches')