~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Patch Queue Manager
  • Date: 2012-01-20 15:48:39 UTC
  • mfrom: (6436.2.2 sibling-branches)
  • Revision ID: pqm@pqm.ubuntu.com-20120120154839-y6yh1m1dzknmub8f
(jelmer) Factor out finding of sibling branches from 'bzr switch'. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
    )
84
84
 
85
85
 
 
86
def _get_branch_location(control_dir):
 
87
    """Return location of branch for this control dir."""
 
88
    try:
 
89
        this_branch = control_dir.open_branch()
 
90
        # This may be a heavy checkout, where we want the master branch
 
91
        master_location = this_branch.get_bound_location()
 
92
        if master_location is not None:
 
93
            return master_location
 
94
        # If not, use a local sibling
 
95
        return this_branch.base
 
96
    except errors.NotBranchError:
 
97
        format = control_dir.find_branch_format()
 
98
        if getattr(format, 'get_reference', None) is not None:
 
99
            return format.get_reference(control_dir)
 
100
        else:
 
101
            return control_dir.root_transport.base
 
102
 
 
103
 
 
104
def lookup_new_sibling_branch(control_dir, location):
 
105
    """Lookup the location for a new sibling branch.
 
106
 
 
107
    :param control_dir: Control directory relative to which to look up
 
108
        the name.
 
109
    :param location: Name of the new branch
 
110
    :return: Full location to the new branch
 
111
    """
 
112
    location = directory_service.directories.dereference(location)
 
113
    if '/' not in location and '\\' not in location:
 
114
        # This path is meant to be relative to the existing branch
 
115
        this_url = _get_branch_location(control_dir)
 
116
        # Perhaps the target control dir supports colocated branches?
 
117
        try:
 
118
            root = controldir.ControlDir.open(this_url,
 
119
                possible_transports=[control_dir.user_transport])
 
120
        except errors.NotBranchError:
 
121
            colocated = False
 
122
        else:
 
123
            colocated = root._format.colocated_branches
 
124
 
 
125
        if colocated:
 
126
            return urlutils.join_segment_parameters(this_url,
 
127
                {"branch": urlutils.escape(location)})
 
128
        else:
 
129
            return urlutils.join(this_url, '..', urlutils.escape(location))
 
130
    return location
 
131
 
 
132
 
 
133
def lookup_sibling_branch(control_dir, location):
 
134
    """Lookup sibling branch.
 
135
    
 
136
    :param control_dir: Control directory relative to which to lookup the
 
137
        location.
 
138
    :param location: Location to look up
 
139
    :return: branch to open
 
140
    """
 
141
    try:
 
142
        # Perhaps it's a colocated branch?
 
143
        return control_dir.open_branch(location)
 
144
    except (errors.NotBranchError, errors.NoColocatedBranchSupport):
 
145
        try:
 
146
            return Branch.open(location)
 
147
        except errors.NotBranchError:
 
148
            this_url = _get_branch_location(control_dir)
 
149
            return Branch.open(
 
150
                urlutils.join(
 
151
                    this_url, '..', urlutils.escape(location)))
 
152
 
 
153
 
86
154
@symbol_versioning.deprecated_function(symbol_versioning.deprecated_in((2, 3, 0)))
87
155
def tree_files(file_list, default_branch=u'.', canonicalize=True,
88
156
    apply_view=True):
6147
6215
            had_explicit_nick = False
6148
6216
        if create_branch:
6149
6217
            if branch is None:
6150
 
                raise errors.BzrCommandError(gettext('cannot create branch without'
6151
 
                                             ' source branch'))
6152
 
            to_location = directory_service.directories.dereference(
6153
 
                              to_location)
6154
 
            if '/' not in to_location and '\\' not in to_location:
6155
 
                # This path is meant to be relative to the existing branch
6156
 
                this_url = self._get_branch_location(control_dir)
6157
 
                # Perhaps the target control dir supports colocated branches?
6158
 
                try:
6159
 
                    root = controldir.ControlDir.open(this_url,
6160
 
                        possible_transports=[control_dir.user_transport])
6161
 
                except errors.NotBranchError:
6162
 
                    colocated = False
6163
 
                else:
6164
 
                    colocated = root._format.colocated_branches
6165
 
                if colocated:
6166
 
                    to_location = urlutils.join_segment_parameters(this_url,
6167
 
                        {"branch": urlutils.escape(to_location)})
6168
 
                else:
6169
 
                    to_location = urlutils.join(
6170
 
                        this_url, '..', urlutils.escape(to_location))
 
6218
                raise errors.BzrCommandError(
 
6219
                    gettext('cannot create branch without source branch'))
 
6220
            to_location = lookup_new_sibling_branch(control_dir, to_location)
6171
6221
            to_branch = branch.bzrdir.sprout(to_location,
6172
 
                                 possible_transports=[branch.bzrdir.root_transport],
6173
 
                                 source_branch=branch).open_branch()
 
6222
                 possible_transports=[branch.bzrdir.root_transport],
 
6223
                 source_branch=branch).open_branch()
6174
6224
        else:
6175
 
            # Perhaps it's a colocated branch?
6176
 
            try:
6177
 
                to_branch = control_dir.open_branch(to_location)
6178
 
            except (errors.NotBranchError, errors.NoColocatedBranchSupport):
6179
 
                try:
6180
 
                    to_branch = Branch.open(to_location)
6181
 
                except errors.NotBranchError:
6182
 
                    this_url = self._get_branch_location(control_dir)
6183
 
                    to_branch = Branch.open(
6184
 
                        urlutils.join(
6185
 
                            this_url, '..', urlutils.escape(to_location)))
 
6225
            to_branch = lookup_sibling_branch(control_dir, to_location)
6186
6226
        if revision is not None:
6187
6227
            revision = revision.as_revision_id(to_branch)
6188
6228
        switch.switch(control_dir, to_branch, force, revision_id=revision)
6192
6232
        note(gettext('Switched to branch: %s'),
6193
6233
            urlutils.unescape_for_display(to_branch.base, 'utf-8'))
6194
6234
 
6195
 
    def _get_branch_location(self, control_dir):
6196
 
        """Return location of branch for this control dir."""
6197
 
        try:
6198
 
            this_branch = control_dir.open_branch()
6199
 
            # This may be a heavy checkout, where we want the master branch
6200
 
            master_location = this_branch.get_bound_location()
6201
 
            if master_location is not None:
6202
 
                return master_location
6203
 
            # If not, use a local sibling
6204
 
            return this_branch.base
6205
 
        except errors.NotBranchError:
6206
 
            format = control_dir.find_branch_format()
6207
 
            if getattr(format, 'get_reference', None) is not None:
6208
 
                return format.get_reference(control_dir)
6209
 
            else:
6210
 
                return control_dir.root_transport.base
6211
6235
 
6212
6236
 
6213
6237
class cmd_view(Command):