65
67
processed. The physical lock state won't be changed.
67
69
# XXX: write a test for LockContention
68
with branch.repository.lock_write(token=repo_token), \
69
branch.lock_write(token=branch_token):
70
return self.do_with_locked_branch(branch, *args)
70
branch.repository.lock_write(token=repo_token)
72
branch.lock_write(token=branch_token)
74
return self.do_with_locked_branch(branch, *args)
78
branch.repository.unlock()
73
81
class SmartServerBranchBreakLock(SmartServerBranchRequest):
113
121
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', ))
122
self._branch.repository.lock_write(token=self._repo_token)
124
self._branch.lock_write(token=self._branch_token)
126
self._branch.control_transport.put_bytes(
127
'branch.conf', body_bytes)
129
self._branch.unlock()
131
self._branch.repository.unlock()
132
return SuccessfulSmartServerResponse(('ok', ))
121
135
class SmartServerBranchGetParent(SmartServerBranchRequest):
199
213
The revision list is returned as the body content,
200
214
with each revision utf8 encoded and \x00 joined.
202
with branch.lock_read():
203
218
graph = branch.repository.get_graph()
204
219
stop_revisions = (None, _mod_revision.NULL_REVISION)
205
220
history = list(graph.iter_lefthand_ancestry(
206
221
branch.last_revision(), stop_revisions))
207
224
return SuccessfulSmartServerResponse(
208
(b'ok', ), (b'\x00'.join(reversed(history))))
225
('ok', ), ('\x00'.join(reversed(history))))
211
228
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
233
249
dotted_revno = branch.revision_id_to_dotted_revno(revid)
234
250
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))
251
return FailedSmartServerResponse(('NoSuchRevision', revid))
240
252
return SuccessfulSmartServerResponse(
241
(b'ok', ) + tuple([b'%d' % x for x in dotted_revno]))
253
('ok', ) + tuple(map(str, dotted_revno)))
244
256
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
262
274
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)
277
branch._get_config().set_option(value.decode('utf8'), name, section)
268
278
return SuccessfulSmartServerResponse(())
271
281
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
272
282
"""Set an option in the branch configuration.
281
291
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)
294
branch._get_config().set_option(value_dict, name, section)
287
295
return SuccessfulSmartServerResponse(())
290
298
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
292
300
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
293
if new_last_revision_id == b'null:':
301
if new_last_revision_id == 'null:':
294
302
branch.set_last_revision_info(0, new_last_revision_id)
296
304
if not branch.repository.has_revision(new_last_revision_id):
297
305
return FailedSmartServerResponse(
298
(b'NoSuchRevision', new_last_revision_id))
306
('NoSuchRevision', new_last_revision_id))
299
307
branch.generate_revision_history(new_last_revision_id, None, None)
300
return SuccessfulSmartServerResponse((b'ok',))
308
return SuccessfulSmartServerResponse(('ok',))
303
311
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
305
313
def do_tip_change_with_locked_branch(self, branch, new_last_revision_id,
306
allow_divergence, allow_overwrite_descendant):
314
allow_divergence, allow_overwrite_descendant):
307
315
"""Set the last revision of the branch.
334
342
relation = branch._revision_relations(
335
343
last_rev, new_last_revision_id, graph)
336
344
if relation == 'diverged' and not allow_divergence:
337
return FailedSmartServerResponse((b'Diverged',))
345
return FailedSmartServerResponse(('Diverged',))
338
346
if relation == 'a_descends_from_b' and do_not_overwrite_descendant:
339
347
return SuccessfulSmartServerResponse(
340
(b'ok', last_revno, last_rev))
348
('ok', last_revno, last_rev))
341
349
new_revno = graph.find_distance_to_null(
342
350
new_last_revision_id, [(last_rev, last_revno)])
343
351
branch.set_last_revision_info(new_revno, new_last_revision_id)
344
352
except errors.GhostRevisionsHaveNoRevno:
345
353
return FailedSmartServerResponse(
346
(b'NoSuchRevision', new_last_revision_id))
354
('NoSuchRevision', new_last_revision_id))
347
355
return SuccessfulSmartServerResponse(
348
(b'ok', new_revno, new_last_revision_id))
356
('ok', new_revno, new_last_revision_id))
351
359
class SmartServerBranchRequestSetLastRevisionInfo(SmartServerSetTipRequest):
358
366
def do_tip_change_with_locked_branch(self, branch, new_revno,
359
new_last_revision_id):
367
new_last_revision_id):
361
369
branch.set_last_revision_info(int(new_revno), new_last_revision_id)
362
370
except errors.NoSuchRevision:
363
371
return FailedSmartServerResponse(
364
(b'NoSuchRevision', new_last_revision_id))
365
return SuccessfulSmartServerResponse((b'ok',))
372
('NoSuchRevision', new_last_revision_id))
373
return SuccessfulSmartServerResponse(('ok',))
368
376
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
369
377
"""Set the parent location for a branch.
371
379
Takes a location to set, which must be utf8 encoded.
374
382
def do_with_locked_branch(self, branch, location):
375
branch._set_parent_location(location.decode('utf-8'))
383
branch._set_parent_location(location)
376
384
return SuccessfulSmartServerResponse(())
379
387
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
381
def do_with_branch(self, branch, branch_token=b'', repo_token=b''):
382
if branch_token == b'':
389
def do_with_branch(self, branch, branch_token='', repo_token=''):
390
if branch_token == '':
383
391
branch_token = None
384
if repo_token == b'':
385
393
repo_token = None
387
395
repo_token = branch.repository.lock_write(
388
396
token=repo_token).repository_token
390
398
branch_token = branch.lock_write(
391
token=branch_token).token
399
token=branch_token).branch_token
393
401
# this leaves the repository with 1 lock
394
402
branch.repository.unlock()
395
403
except errors.LockContention:
396
return FailedSmartServerResponse((b'LockContention',))
404
return FailedSmartServerResponse(('LockContention',))
397
405
except errors.TokenMismatch:
398
return FailedSmartServerResponse((b'TokenMismatch',))
406
return FailedSmartServerResponse(('TokenMismatch',))
399
407
except errors.UnlockableTransport:
400
return FailedSmartServerResponse((b'UnlockableTransport',))
408
return FailedSmartServerResponse(('UnlockableTransport',))
401
409
except errors.LockFailed as e:
402
return FailedSmartServerResponse((b'LockFailed',
403
str(e.lock).encode('utf-8'), str(e.why).encode('utf-8')))
410
return FailedSmartServerResponse(('LockFailed', str(e.lock), str(e.why)))
404
411
if repo_token is None:
407
414
branch.repository.leave_lock_in_place()
408
415
branch.leave_lock_in_place()
410
return SuccessfulSmartServerResponse((b'ok', branch_token, repo_token))
417
return SuccessfulSmartServerResponse(('ok', branch_token, repo_token))
413
420
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
415
422
def do_with_branch(self, branch, branch_token, repo_token):
417
with branch.repository.lock_write(token=repo_token):
424
branch.repository.lock_write(token=repo_token)
418
426
branch.lock_write(token=branch_token)
428
branch.repository.unlock()
419
429
except errors.TokenMismatch:
420
return FailedSmartServerResponse((b'TokenMismatch',))
430
return FailedSmartServerResponse(('TokenMismatch',))
422
432
branch.repository.dont_leave_lock_in_place()
423
433
branch.dont_leave_lock_in_place()
425
return SuccessfulSmartServerResponse((b'ok',))
435
return SuccessfulSmartServerResponse(('ok',))
428
438
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
434
444
def do_with_branch(self, branch):
435
445
if branch.get_physical_lock_status():
436
return SuccessfulSmartServerResponse((b'yes',))
446
return SuccessfulSmartServerResponse(('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)
448
return SuccessfulSmartServerResponse(('no',))