1
# Copyright (C) 2006 Canonical Ltd
1
# Copyright (C) 2006-2010 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
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Server-side branch related request implmentations."""
20
from bzrlib import errors
21
24
from bzrlib.bzrdir import BzrDir
22
25
from bzrlib.smart.request import (
23
26
FailedSmartServerResponse,
33
36
def do(self, path, *args):
34
37
"""Execute a request for a branch at path.
36
39
All Branch requests take a path to the branch as their first argument.
38
41
If the branch is a branch reference, NotBranchError is raised.
45
48
bzrdir = BzrDir.open_from_transport(transport)
46
49
if bzrdir.get_branch_reference() is not None:
47
50
raise errors.NotBranchError(transport.base)
48
branch = bzrdir.open_branch()
51
branch = bzrdir.open_branch(ignore_fallbacks=True)
49
52
return self.do_with_branch(branch, *args)
76
79
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
78
81
def do_with_branch(self, branch):
79
82
"""Return the content of branch.conf
81
84
The body is not utf8 decoded - its the literal bytestream from disk.
83
# This was at one time called by RemoteBranchLockableFiles
84
# intercepting access to this file; as of 1.5 it is not called by the
85
# client but retained for compatibility. It may be called again to
86
# allow the client to get the configuration without needing vfs
89
87
content = branch._transport.get_bytes('branch.conf')
90
88
except errors.NoSuchFile:
92
90
return SuccessfulSmartServerResponse( ('ok', ), content)
93
class SmartServerBranchGetParent(SmartServerBranchRequest):
95
def do_with_branch(self, branch):
96
"""Return the parent of branch."""
97
parent = branch._get_parent_location() or ''
98
return SuccessfulSmartServerResponse((parent,))
101
class SmartServerBranchGetTagsBytes(SmartServerBranchRequest):
103
def do_with_branch(self, branch):
104
"""Return the _get_tags_bytes for a branch."""
105
bytes = branch._get_tags_bytes()
106
return SuccessfulSmartServerResponse((bytes,))
109
class SmartServerBranchSetTagsBytes(SmartServerLockedBranchRequest):
111
def __init__(self, backing_transport, root_client_path='/', jail_root=None):
112
SmartServerLockedBranchRequest.__init__(
113
self, backing_transport, root_client_path, jail_root)
116
def do_with_locked_branch(self, branch):
117
"""Call _set_tags_bytes for a branch.
121
# We need to keep this branch locked until we get a body with the tags
124
self.branch.lock_write()
127
def do_body(self, bytes):
128
self.branch._set_tags_bytes(bytes)
129
return SuccessfulSmartServerResponse(())
132
# TODO: this request shouldn't have to do this housekeeping manually.
133
# Some of this logic probably belongs in a base class.
135
# We never acquired the branch successfully in the first place, so
136
# there's nothing more to do.
139
return SmartServerLockedBranchRequest.do_end(self)
141
# Only try unlocking if we locked successfully in the first place
95
145
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
97
147
def do_with_branch(self, branch):
114
164
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
116
166
def do_with_branch(self, branch):
117
167
"""Return branch.last_revision_info().
119
169
The revno is encoded in decimal, the revision_id is encoded as utf8.
121
171
revno, last_revision = branch.last_revision_info()
137
187
return FailedSmartServerResponse(('TipChangeRejected', msg))
190
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
191
"""Set an option in the branch configuration."""
193
def do_with_locked_branch(self, branch, value, name, section):
196
branch._get_config().set_option(value.decode('utf8'), name, section)
197
return SuccessfulSmartServerResponse(())
200
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
201
"""Set an option in the branch configuration.
206
def do_with_locked_branch(self, branch, value_dict, name, section):
207
utf8_dict = bencode.bdecode(value_dict)
209
for key, value in utf8_dict.items():
210
value_dict[key.decode('utf8')] = value.decode('utf8')
213
branch._get_config().set_option(value_dict, name, section)
214
return SuccessfulSmartServerResponse(())
140
217
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
142
219
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
143
220
if new_last_revision_id == 'null:':
144
221
branch.set_revision_history([])
146
223
if not branch.repository.has_revision(new_last_revision_id):
147
224
return FailedSmartServerResponse(
148
225
('NoSuchRevision', new_last_revision_id))
149
branch.generate_revision_history(new_last_revision_id)
226
branch.set_revision_history(branch._lefthand_history(
227
new_last_revision_id, None, None))
150
228
return SuccessfulSmartServerResponse(('ok',))
153
231
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
155
233
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id,
156
234
allow_divergence, allow_overwrite_descendant):
157
235
"""Set the last revision of the branch.
161
239
:param new_last_revision_id: the revision ID to set as the last
162
240
revision of the branch.
163
241
:param allow_divergence: A flag. If non-zero, change the revision ID
215
293
return SuccessfulSmartServerResponse(('ok',))
296
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
297
"""Set the parent location for a branch.
299
Takes a location to set, which must be utf8 encoded.
302
def do_with_locked_branch(self, branch, location):
303
branch._set_parent_location(location)
304
return SuccessfulSmartServerResponse(())
218
307
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
220
309
def do_with_branch(self, branch, branch_token='', repo_token=''):
221
310
if branch_token == '':
222
311
branch_token = None
223
312
if repo_token == '':
224
313
repo_token = None
226
repo_token = branch.repository.lock_write(token=repo_token)
315
repo_token = branch.repository.lock_write(
316
token=repo_token).repository_token
228
branch_token = branch.lock_write(token=branch_token)
318
branch_token = branch.lock_write(
319
token=branch_token).branch_token
230
321
# this leaves the repository with 1 lock
231
322
branch.repository.unlock()