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,
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
145
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
147
def do_with_branch(self, branch):
148
"""Return the heads-to-fetch for a Branch as two bencoded lists.
150
See Branch.heads_to_fetch.
154
must_fetch, if_present_fetch = branch.heads_to_fetch()
155
return SuccessfulSmartServerResponse(
156
(list(must_fetch), list(if_present_fetch)))
95
159
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
97
161
def do_with_branch(self, branch):
114
178
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
116
180
def do_with_branch(self, branch):
117
181
"""Return branch.last_revision_info().
119
183
The revno is encoded in decimal, the revision_id is encoded as utf8.
121
185
revno, last_revision = branch.last_revision_info()
137
201
return FailedSmartServerResponse(('TipChangeRejected', msg))
204
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
205
"""Set an option in the branch configuration."""
207
def do_with_locked_branch(self, branch, value, name, section):
210
branch._get_config().set_option(value.decode('utf8'), name, section)
211
return SuccessfulSmartServerResponse(())
214
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
215
"""Set an option in the branch configuration.
220
def do_with_locked_branch(self, branch, value_dict, name, section):
221
utf8_dict = bencode.bdecode(value_dict)
223
for key, value in utf8_dict.items():
224
value_dict[key.decode('utf8')] = value.decode('utf8')
227
branch._get_config().set_option(value_dict, name, section)
228
return SuccessfulSmartServerResponse(())
140
231
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
142
233
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
143
234
if new_last_revision_id == 'null:':
144
branch.set_revision_history([])
235
branch._set_revision_history([])
146
237
if not branch.repository.has_revision(new_last_revision_id):
147
238
return FailedSmartServerResponse(
148
239
('NoSuchRevision', new_last_revision_id))
149
branch.generate_revision_history(new_last_revision_id)
240
branch._set_revision_history(branch._lefthand_history(
241
new_last_revision_id, None, None))
150
242
return SuccessfulSmartServerResponse(('ok',))
153
245
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
155
247
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id,
156
248
allow_divergence, allow_overwrite_descendant):
157
249
"""Set the last revision of the branch.
161
253
:param new_last_revision_id: the revision ID to set as the last
162
254
revision of the branch.
163
255
:param allow_divergence: A flag. If non-zero, change the revision ID
215
307
return SuccessfulSmartServerResponse(('ok',))
310
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
311
"""Set the parent location for a branch.
313
Takes a location to set, which must be utf8 encoded.
316
def do_with_locked_branch(self, branch, location):
317
branch._set_parent_location(location)
318
return SuccessfulSmartServerResponse(())
218
321
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
220
323
def do_with_branch(self, branch, branch_token='', repo_token=''):
221
324
if branch_token == '':
222
325
branch_token = None
223
326
if repo_token == '':
224
327
repo_token = None
226
repo_token = branch.repository.lock_write(token=repo_token)
329
repo_token = branch.repository.lock_write(
330
token=repo_token).repository_token
228
branch_token = branch.lock_write(token=branch_token)
332
branch_token = branch.lock_write(
333
token=branch_token).branch_token
230
335
# this leaves the repository with 1 lock
231
336
branch.repository.unlock()