87
81
The body is not utf8 decoded - its the literal bytestream from disk.
90
content = branch.control_transport.get_bytes('branch.conf')
84
content = branch._transport.get_bytes('branch.conf')
91
85
except errors.NoSuchFile:
93
return SuccessfulSmartServerResponse((b'ok', ), content)
96
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
97
"""Set the configuration data for a branch.
102
def do_with_branch(self, branch, branch_token, repo_token):
103
"""Set the content of branch.conf.
105
The body is not utf8 decoded - its the literal bytestream for disk.
107
self._branch = branch
108
self._branch_token = branch_token
109
self._repo_token = repo_token
110
# Signal we want a body
113
def do_body(self, body_bytes):
114
with self._branch.repository.lock_write(token=self._repo_token), \
115
self._branch.lock_write(token=self._branch_token):
116
self._branch.control_transport.put_bytes(
117
'branch.conf', body_bytes)
118
return SuccessfulSmartServerResponse((b'ok', ))
87
return SuccessfulSmartServerResponse( ('ok', ), content)
121
90
class SmartServerBranchGetParent(SmartServerBranchRequest):
216
166
The revno is encoded in decimal, the revision_id is encoded as utf8.
218
168
revno, last_revision = branch.last_revision_info()
219
return SuccessfulSmartServerResponse(
220
(b'ok', str(revno).encode('ascii'), last_revision))
223
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
225
def do_with_branch(self, branch, revid):
226
"""Return branch.revision_id_to_revno().
230
The revno is encoded in decimal, the revision_id is encoded as utf8.
233
dotted_revno = branch.revision_id_to_dotted_revno(revid)
234
except errors.NoSuchRevision:
235
return FailedSmartServerResponse((b'NoSuchRevision', revid))
236
except errors.GhostRevisionsHaveNoRevno as e:
237
return FailedSmartServerResponse(
238
(b'GhostRevisionsHaveNoRevno', e.revision_id,
239
e.ghost_revision_id))
240
return SuccessfulSmartServerResponse(
241
(b'ok', ) + tuple([b'%d' % x for x in dotted_revno]))
169
return SuccessfulSmartServerResponse(('ok', str(revno), last_revision))
244
172
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
262
190
def do_with_locked_branch(self, branch, value, name, section):
265
branch._get_config().set_option(
266
value.decode('utf-8'), name.decode('utf-8'),
267
section.decode('utf-8') if section is not None else None)
268
return SuccessfulSmartServerResponse(())
271
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
272
"""Set an option in the branch configuration.
277
def do_with_locked_branch(self, branch, value_dict, name, section):
278
utf8_dict = bencode.bdecode(value_dict)
280
for key, value in utf8_dict.items():
281
value_dict[key.decode('utf8')] = value.decode('utf8')
285
section = section.decode('utf-8')
286
branch._get_config().set_option(value_dict, name.decode('utf-8'), section)
193
branch._get_config().set_option(value.decode('utf8'), name, section)
287
194
return SuccessfulSmartServerResponse(())
290
197
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
292
199
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
293
if new_last_revision_id == b'null:':
294
branch.set_last_revision_info(0, new_last_revision_id)
200
if new_last_revision_id == 'null:':
201
branch.set_revision_history([])
296
203
if not branch.repository.has_revision(new_last_revision_id):
297
204
return FailedSmartServerResponse(
298
(b'NoSuchRevision', new_last_revision_id))
299
branch.generate_revision_history(new_last_revision_id, None, None)
300
return SuccessfulSmartServerResponse((b'ok',))
205
('NoSuchRevision', new_last_revision_id))
206
branch.set_revision_history(branch._lefthand_history(
207
new_last_revision_id, None, None))
208
return SuccessfulSmartServerResponse(('ok',))
303
211
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
305
213
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id,
306
allow_divergence, allow_overwrite_descendant):
214
allow_divergence, allow_overwrite_descendant):
307
215
"""Set the last revision of the branch.
334
242
relation = branch._revision_relations(
335
243
last_rev, new_last_revision_id, graph)
336
244
if relation == 'diverged' and not allow_divergence:
337
return FailedSmartServerResponse((b'Diverged',))
245
return FailedSmartServerResponse(('Diverged',))
338
246
if relation == 'a_descends_from_b' and do_not_overwrite_descendant:
339
247
return SuccessfulSmartServerResponse(
340
(b'ok', last_revno, last_rev))
248
('ok', last_revno, last_rev))
341
249
new_revno = graph.find_distance_to_null(
342
250
new_last_revision_id, [(last_rev, last_revno)])
343
251
branch.set_last_revision_info(new_revno, new_last_revision_id)
344
252
except errors.GhostRevisionsHaveNoRevno:
345
253
return FailedSmartServerResponse(
346
(b'NoSuchRevision', new_last_revision_id))
254
('NoSuchRevision', new_last_revision_id))
347
255
return SuccessfulSmartServerResponse(
348
(b'ok', new_revno, new_last_revision_id))
256
('ok', new_revno, new_last_revision_id))
351
259
class SmartServerBranchRequestSetLastRevisionInfo(SmartServerSetTipRequest):
352
260
"""Branch.set_last_revision_info. Sets the revno and the revision ID of
353
261
the specified branch.
358
266
def do_tip_change_with_locked_branch(self, branch, new_revno,
359
new_last_revision_id):
267
new_last_revision_id):
361
269
branch.set_last_revision_info(int(new_revno), new_last_revision_id)
362
270
except errors.NoSuchRevision:
363
271
return FailedSmartServerResponse(
364
(b'NoSuchRevision', new_last_revision_id))
365
return SuccessfulSmartServerResponse((b'ok',))
272
('NoSuchRevision', new_last_revision_id))
273
return SuccessfulSmartServerResponse(('ok',))
368
276
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
369
277
"""Set the parent location for a branch.
371
279
Takes a location to set, which must be utf8 encoded.
374
282
def do_with_locked_branch(self, branch, location):
375
branch._set_parent_location(location.decode('utf-8'))
283
branch._set_parent_location(location)
376
284
return SuccessfulSmartServerResponse(())
379
287
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
381
def do_with_branch(self, branch, branch_token=b'', repo_token=b''):
382
if branch_token == b'':
289
def do_with_branch(self, branch, branch_token='', repo_token=''):
290
if branch_token == '':
383
291
branch_token = None
384
if repo_token == b'':
385
293
repo_token = None
387
repo_token = branch.repository.lock_write(
388
token=repo_token).repository_token
295
repo_token = branch.repository.lock_write(token=repo_token)
390
branch_token = branch.lock_write(
391
token=branch_token).token
297
branch_token = branch.lock_write(token=branch_token)
393
299
# this leaves the repository with 1 lock
394
300
branch.repository.unlock()
395
301
except errors.LockContention:
396
return FailedSmartServerResponse((b'LockContention',))
302
return FailedSmartServerResponse(('LockContention',))
397
303
except errors.TokenMismatch:
398
return FailedSmartServerResponse((b'TokenMismatch',))
304
return FailedSmartServerResponse(('TokenMismatch',))
399
305
except errors.UnlockableTransport:
400
return FailedSmartServerResponse((b'UnlockableTransport',))
401
except errors.LockFailed as e:
402
return FailedSmartServerResponse((b'LockFailed',
403
str(e.lock).encode('utf-8'), str(e.why).encode('utf-8')))
306
return FailedSmartServerResponse(('UnlockableTransport',))
307
except errors.LockFailed, e:
308
return FailedSmartServerResponse(('LockFailed', str(e.lock), str(e.why)))
404
309
if repo_token is None:
407
312
branch.repository.leave_lock_in_place()
408
313
branch.leave_lock_in_place()
410
return SuccessfulSmartServerResponse((b'ok', branch_token, repo_token))
315
return SuccessfulSmartServerResponse(('ok', branch_token, repo_token))
413
318
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
415
320
def do_with_branch(self, branch, branch_token, repo_token):
417
with branch.repository.lock_write(token=repo_token):
322
branch.repository.lock_write(token=repo_token)
418
324
branch.lock_write(token=branch_token)
326
branch.repository.unlock()
419
327
except errors.TokenMismatch:
420
return FailedSmartServerResponse((b'TokenMismatch',))
328
return FailedSmartServerResponse(('TokenMismatch',))
422
330
branch.repository.dont_leave_lock_in_place()
423
331
branch.dont_leave_lock_in_place()
425
return SuccessfulSmartServerResponse((b'ok',))
428
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
429
"""Get the physical lock status for a branch.
434
def do_with_branch(self, branch):
435
if branch.get_physical_lock_status():
436
return SuccessfulSmartServerResponse((b'yes',))
438
return SuccessfulSmartServerResponse((b'no',))
441
class SmartServerBranchRequestGetAllReferenceInfo(SmartServerBranchRequest):
442
"""Get the reference information.
447
def do_with_branch(self, branch):
448
all_reference_info = branch._get_all_reference_info()
449
content = bencode.bencode([
450
(key, value[0].encode('utf-8'), value[1].encode('utf-8') if value[1] else b'')
451
for (key, value) in all_reference_info.items()])
452
return SuccessfulSmartServerResponse((b'ok', ), content)
333
return SuccessfulSmartServerResponse(('ok',))