1
# Copyright (C) 2008 Canonical Ltd
1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
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.
23
from bzrlib import errors, registry
24
from bzrlib.lazy_import import lazy_import
23
from __future__ import absolute_import
29
from .lazy_import import lazy_import
25
30
lazy_import(globals(), """
26
from bzrlib.branch import Branch
27
from bzrlib import urlutils
32
branch as _mod_branch,
33
controldir as _mod_controldir,
39
class DirectoryLookupFailure(errors.BzrError):
40
"""Base type for lookup errors."""
43
class InvalidLocationAlias(DirectoryLookupFailure):
45
_fmt = '"%(alias_name)s" is not a valid location alias.'
47
def __init__(self, alias_name):
48
DirectoryLookupFailure.__init__(self, alias_name=alias_name)
51
class UnsetLocationAlias(DirectoryLookupFailure):
53
_fmt = 'No %(alias_name)s location assigned.'
55
def __init__(self, alias_name):
56
DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
31
59
class DirectoryServiceRegistry(registry.Registry):
32
60
"""This object maintains and uses a list of directory services.
67
94
supported. On error, a subclass of DirectoryLookupFailure will be raised.
97
branch_aliases = registry.Registry()
98
branch_aliases.register('parent', lambda b: b.get_parent(),
99
help="The parent of this branch.")
100
branch_aliases.register('submit', lambda b: b.get_submit_branch(),
101
help="The submit branch for this branch.")
102
branch_aliases.register('public', lambda b: b.get_public_branch(),
103
help="The public location of this branch.")
104
branch_aliases.register('bound', lambda b: b.get_bound_location(),
105
help="The branch this branch is bound to, for bound branches.")
106
branch_aliases.register('push', lambda b: b.get_push_location(),
107
help="The saved location used for `brz push` with no arguments.")
108
branch_aliases.register('this', lambda b: b.base,
70
111
def look_up(self, name, url):
71
branch = Branch.open_containing('.')[0]
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
112
branch = _mod_branch.Branch.open_containing('.')[0]
80
113
parts = url.split('/', 1)
81
114
if len(parts) == 2:
82
115
name, extra = parts
87
method = lookups[name[1:]]
120
method = self.branch_aliases.get(name[1:])
89
raise errors.InvalidLocationAlias(url)
122
raise InvalidLocationAlias(url)
124
result = method(branch)
92
125
if result is None:
93
raise errors.UnsetLocationAlias(url)
126
raise UnsetLocationAlias(url)
94
127
if extra is not None:
95
128
result = urlutils.join(result, extra)
132
def help_text(cls, topic):
134
for key in cls.branch_aliases.keys():
135
help = cls.branch_aliases.get_help(key)
136
alias_lines.append(" :%-10s%s\n" % (key, help))
141
Bazaar defines several aliases for locations associated with a branch. These
142
can be used with most commands that expect a location, such as `brz push`.
147
For example, to push to the parent location::
150
""" % "".join(alias_lines)
98
153
directories.register(':', AliasDirectory,
99
154
'Easy access to remembered branch locations')
157
class ColocatedDirectory(object):
158
"""Directory lookup for colocated branches.
160
co:somename will resolve to the colocated branch with "somename" in
161
the current directory.
164
def look_up(self, name, url):
165
dir = _mod_controldir.ControlDir.open_containing('.')[0]
166
return urlutils.join_segment_parameters(dir.user_url,
167
{"branch": urlutils.escape(name)})
170
directories.register('co:', ColocatedDirectory,
171
'Easy access to colocated branches')