~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/directory_service.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 19:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216191839-eg681lxqibi1qxu1
Fix remaining tests.

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
 
23
from bzrlib import (
 
24
    errors,
 
25
    registry,
 
26
    )
24
27
from bzrlib.lazy_import import lazy_import
25
28
lazy_import(globals(), """
26
 
from bzrlib.branch import Branch
27
 
from bzrlib import urlutils
 
29
from bzrlib import (
 
30
    branch as _mod_branch,
 
31
    urlutils,
 
32
    )
28
33
""")
29
34
 
30
35
 
59
64
 
60
65
directories = DirectoryServiceRegistry()
61
66
 
62
 
 
63
67
class AliasDirectory(object):
64
68
    """Directory lookup for locations associated with a branch.
65
69
 
67
71
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
68
72
    """
69
73
 
 
74
    branch_aliases = registry.Registry()
 
75
    branch_aliases.register('parent', lambda b: b.get_parent(),
 
76
        help="The parent of this branch.")
 
77
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
 
78
        help="The submit branch for this branch.")
 
79
    branch_aliases.register('public', lambda b: b.get_public_branch(),
 
80
        help="The public location of this branch.")
 
81
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
 
82
        help="The branch this branch is bound to, for bound branches.")
 
83
    branch_aliases.register('push', lambda b: b.get_push_location(),
 
84
        help="The saved location used for `bzr push` with no arguments.")
 
85
    branch_aliases.register('this', lambda b: b.base,
 
86
        help="This branch.")
 
87
 
70
88
    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
 
        }
 
89
        branch = _mod_branch.Branch.open_containing('.')[0]
80
90
        parts = url.split('/', 1)
81
91
        if len(parts) == 2:
82
92
            name, extra = parts
84
94
            (name,) = parts
85
95
            extra = None
86
96
        try:
87
 
            method = lookups[name[1:]]
 
97
            method = self.branch_aliases.get(name[1:])
88
98
        except KeyError:
89
99
            raise errors.InvalidLocationAlias(url)
90
100
        else:
91
 
            result = method()
 
101
            result = method(branch)
92
102
        if result is None:
93
103
            raise errors.UnsetLocationAlias(url)
94
104
        if extra is not None:
95
105
            result = urlutils.join(result, extra)
96
106
        return result
97
107
 
 
108
    @classmethod
 
109
    def help_text(cls, topic):
 
110
        alias_lines = []
 
111
        for key in cls.branch_aliases.keys():
 
112
            help = cls.branch_aliases.get_help(key)
 
113
            alias_lines.append("  :%-10s%s\n" % (key, help))
 
114
        return """\
 
115
Location aliases
 
116
================
 
117
 
 
118
Bazaar defines several aliases for locations associated with a branch.  These
 
119
can be used with most commands that expect a location, such as `bzr push`.
 
120
 
 
121
The aliases are::
 
122
 
 
123
%s
 
124
For example, to push to the parent location::
 
125
 
 
126
    bzr push :parent
 
127
""" % "".join(alias_lines)
 
128
 
 
129
 
98
130
directories.register(':', AliasDirectory,
99
131
                     'Easy access to remembered branch locations')