20
20
from bzrlib import (
23
revision as _mod_revision,
24
from bzrlib.bzrdir import BzrDir
25
from bzrlib.controldir import ControlDir
25
26
from bzrlib.smart.request import (
26
27
FailedSmartServerResponse,
27
28
SmartServerRequest,
45
46
:return: A SmartServerResponse from self.do_with_branch().
47
48
transport = self.transport_from_client_path(path)
48
bzrdir = BzrDir.open_from_transport(transport)
49
if bzrdir.get_branch_reference() is not None:
49
controldir = ControlDir.open_from_transport(transport)
50
if controldir.get_branch_reference() is not None:
50
51
raise errors.NotBranchError(transport.base)
51
branch = bzrdir.open_branch(ignore_fallbacks=True)
52
branch = controldir.open_branch(ignore_fallbacks=True)
52
53
return self.do_with_branch(branch, *args)
76
77
branch.repository.unlock()
80
class SmartServerBranchBreakLock(SmartServerBranchRequest):
82
def do_with_branch(self, branch):
83
"""Break a branch lock.
86
return SuccessfulSmartServerResponse(('ok', ), )
79
89
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
81
91
def do_with_branch(self, branch):
84
94
The body is not utf8 decoded - its the literal bytestream from disk.
87
content = branch._transport.get_bytes('branch.conf')
97
content = branch.control_transport.get_bytes('branch.conf')
88
98
except errors.NoSuchFile:
90
100
return SuccessfulSmartServerResponse( ('ok', ), content)
103
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
104
"""Set the configuration data for a branch.
109
def do_with_branch(self, branch, branch_token, repo_token):
110
"""Set the content of branch.conf.
112
The body is not utf8 decoded - its the literal bytestream for disk.
114
self._branch = branch
115
self._branch_token = branch_token
116
self._repo_token = repo_token
117
# Signal we want a body
120
def do_body(self, body_bytes):
121
self._branch.repository.lock_write(token=self._repo_token)
123
self._branch.lock_write(token=self._branch_token)
125
self._branch.control_transport.put_bytes(
126
'branch.conf', body_bytes)
128
self._branch.unlock()
130
self._branch.repository.unlock()
131
return SuccessfulSmartServerResponse(('ok', ))
93
134
class SmartServerBranchGetParent(SmartServerBranchRequest):
95
136
def do_with_branch(self, branch):
142
183
self.branch.unlock()
186
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
188
def do_with_branch(self, branch):
189
"""Return the heads-to-fetch for a Branch as two bencoded lists.
191
See Branch.heads_to_fetch.
195
must_fetch, if_present_fetch = branch.heads_to_fetch()
196
return SuccessfulSmartServerResponse(
197
(list(must_fetch), list(if_present_fetch)))
145
200
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
147
202
def do_with_branch(self, branch):
157
212
The revision list is returned as the body content,
158
213
with each revision utf8 encoded and \x00 joined.
217
graph = branch.repository.get_graph()
218
stop_revisions = (None, _mod_revision.NULL_REVISION)
219
history = list(graph.iter_lefthand_ancestry(
220
branch.last_revision(), stop_revisions))
160
223
return SuccessfulSmartServerResponse(
161
('ok', ), ('\x00'.join(branch.revision_history())))
224
('ok', ), ('\x00'.join(reversed(history))))
164
227
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
172
235
return SuccessfulSmartServerResponse(('ok', str(revno), last_revision))
238
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
240
def do_with_branch(self, branch, revid):
241
"""Return branch.revision_id_to_revno().
245
The revno is encoded in decimal, the revision_id is encoded as utf8.
248
dotted_revno = branch.revision_id_to_dotted_revno(revid)
249
except errors.NoSuchRevision:
250
return FailedSmartServerResponse(('NoSuchRevision', revid))
251
return SuccessfulSmartServerResponse(
252
('ok', ) + tuple(map(str, dotted_revno)))
175
255
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
176
256
"""Base class for handling common branch request logic for requests that
177
257
update the branch tip.
219
299
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
220
300
if new_last_revision_id == 'null:':
221
branch.set_revision_history([])
301
branch._set_revision_history([])
223
303
if not branch.repository.has_revision(new_last_revision_id):
224
304
return FailedSmartServerResponse(
225
305
('NoSuchRevision', new_last_revision_id))
226
branch.set_revision_history(branch._lefthand_history(
306
branch._set_revision_history(branch._lefthand_history(
227
307
new_last_revision_id, None, None))
228
308
return SuccessfulSmartServerResponse(('ok',))
355
435
return SuccessfulSmartServerResponse(('ok',))
438
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
439
"""Get the physical lock status for a branch.
444
def do_with_branch(self, branch):
445
if branch.get_physical_lock_status():
446
return SuccessfulSmartServerResponse(('yes',))
448
return SuccessfulSmartServerResponse(('no',))