/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/smart/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Server-side branch related request implmentations."""
18
18
 
19
 
from ... import (
 
19
from __future__ import absolute_import
 
20
 
 
21
from .. import (
20
22
    bencode,
21
23
    errors,
22
24
    revision as _mod_revision,
23
25
    )
24
 
from ...controldir import ControlDir
 
26
from ..controldir import ControlDir
25
27
from .request import (
26
28
    FailedSmartServerResponse,
27
29
    SmartServerRequest,
65
67
        processed.  The physical lock state won't be changed.
66
68
        """
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)
 
71
        try:
 
72
            branch.lock_write(token=branch_token)
 
73
            try:
 
74
                return self.do_with_locked_branch(branch, *args)
 
75
            finally:
 
76
                branch.unlock()
 
77
        finally:
 
78
            branch.repository.unlock()
71
79
 
72
80
 
73
81
class SmartServerBranchBreakLock(SmartServerBranchRequest):
76
84
        """Break a branch lock.
77
85
        """
78
86
        branch.break_lock()
79
 
        return SuccessfulSmartServerResponse((b'ok', ), )
 
87
        return SuccessfulSmartServerResponse(('ok', ), )
80
88
 
81
89
 
82
90
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
89
97
        try:
90
98
            content = branch.control_transport.get_bytes('branch.conf')
91
99
        except errors.NoSuchFile:
92
 
            content = b''
93
 
        return SuccessfulSmartServerResponse((b'ok', ), content)
 
100
            content = ''
 
101
        return SuccessfulSmartServerResponse( ('ok', ), content)
94
102
 
95
103
 
96
104
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
111
119
        return None
112
120
 
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)
 
123
        try:
 
124
            self._branch.lock_write(token=self._branch_token)
 
125
            try:
 
126
                self._branch.control_transport.put_bytes(
 
127
                    'branch.conf', body_bytes)
 
128
            finally:
 
129
                self._branch.unlock()
 
130
        finally:
 
131
            self._branch.repository.unlock()
 
132
        return SuccessfulSmartServerResponse(('ok', ))
119
133
 
120
134
 
121
135
class SmartServerBranchGetParent(SmartServerBranchRequest):
123
137
    def do_with_branch(self, branch):
124
138
        """Return the parent of branch."""
125
139
        parent = branch._get_parent_location() or ''
126
 
        return SuccessfulSmartServerResponse((parent.encode('utf-8'),))
 
140
        return SuccessfulSmartServerResponse((parent,))
127
141
 
128
142
 
129
143
class SmartServerBranchGetTagsBytes(SmartServerBranchRequest):
140
154
        SmartServerLockedBranchRequest.__init__(
141
155
            self, backing_transport, root_client_path, jail_root)
142
156
        self.locked = False
143
 
 
 
157
        
144
158
    def do_with_locked_branch(self, branch):
145
159
        """Call _set_tags_bytes for a branch.
146
160
 
174
188
 
175
189
    def do_with_branch(self, branch):
176
190
        """Return the heads-to-fetch for a Branch as two bencoded lists.
177
 
 
 
191
        
178
192
        See Branch.heads_to_fetch.
179
193
 
180
194
        New in 2.4.
188
202
 
189
203
    def do_with_branch(self, branch):
190
204
        stacked_on_url = branch.get_stacked_on_url()
191
 
        return SuccessfulSmartServerResponse((b'ok', stacked_on_url.encode('ascii')))
 
205
        return SuccessfulSmartServerResponse(('ok', stacked_on_url))
192
206
 
193
207
 
194
208
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
199
213
        The revision list is returned as the body content,
200
214
        with each revision utf8 encoded and \x00 joined.
201
215
        """
202
 
        with branch.lock_read():
 
216
        branch.lock_read()
 
217
        try:
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))
 
222
        finally:
 
223
            branch.unlock()
207
224
        return SuccessfulSmartServerResponse(
208
 
            (b'ok', ), (b'\x00'.join(reversed(history))))
 
225
            ('ok', ), ('\x00'.join(reversed(history))))
209
226
 
210
227
 
211
228
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
216
233
        The revno is encoded in decimal, the revision_id is encoded as utf8.
217
234
        """
218
235
        revno, last_revision = branch.last_revision_info()
219
 
        return SuccessfulSmartServerResponse(
220
 
            (b'ok', str(revno).encode('ascii'), last_revision))
 
236
        return SuccessfulSmartServerResponse(('ok', str(revno), last_revision))
221
237
 
222
238
 
223
239
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
232
248
        try:
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)))
242
254
 
243
255
 
244
256
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
251
263
            return self.do_tip_change_with_locked_branch(branch, *args)
252
264
        except errors.TipChangeRejected as e:
253
265
            msg = e.msg
254
 
            if isinstance(msg, str):
 
266
            if isinstance(msg, unicode):
255
267
                msg = msg.encode('utf-8')
256
 
            return FailedSmartServerResponse((b'TipChangeRejected', msg))
 
268
            return FailedSmartServerResponse(('TipChangeRejected', msg))
257
269
 
258
270
 
259
271
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
262
274
    def do_with_locked_branch(self, branch, value, name, section):
263
275
        if not section:
264
276
            section = None
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(())
269
279
 
270
280
 
271
281
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
272
282
    """Set an option in the branch configuration.
273
 
 
 
283
    
274
284
    New in 2.2.
275
285
    """
276
286
 
281
291
            value_dict[key.decode('utf8')] = value.decode('utf8')
282
292
        if not section:
283
293
            section = None
284
 
        else:
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(())
288
296
 
289
297
 
290
298
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
291
299
 
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)
295
303
        else:
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',))
301
309
 
302
310
 
303
311
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
304
312
 
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.
308
316
 
309
317
        New in 1.6.
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))
349
357
 
350
358
 
351
359
class SmartServerBranchRequestSetLastRevisionInfo(SmartServerSetTipRequest):
356
364
    """
357
365
 
358
366
    def do_tip_change_with_locked_branch(self, branch, new_revno,
359
 
                                         new_last_revision_id):
 
367
            new_last_revision_id):
360
368
        try:
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',))
366
374
 
367
375
 
368
376
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
369
377
    """Set the parent location for a branch.
370
 
 
 
378
    
371
379
    Takes a location to set, which must be utf8 encoded.
372
380
    """
373
381
 
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(())
377
385
 
378
386
 
379
387
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
380
388
 
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'':
 
392
        if repo_token == '':
385
393
            repo_token = None
386
394
        try:
387
395
            repo_token = branch.repository.lock_write(
388
396
                token=repo_token).repository_token
389
397
            try:
390
398
                branch_token = branch.lock_write(
391
 
                    token=branch_token).token
 
399
                    token=branch_token).branch_token
392
400
            finally:
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:
405
 
            repo_token = b''
 
412
            repo_token = ''
406
413
        else:
407
414
            branch.repository.leave_lock_in_place()
408
415
        branch.leave_lock_in_place()
409
416
        branch.unlock()
410
 
        return SuccessfulSmartServerResponse((b'ok', branch_token, repo_token))
 
417
        return SuccessfulSmartServerResponse(('ok', branch_token, repo_token))
411
418
 
412
419
 
413
420
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
414
421
 
415
422
    def do_with_branch(self, branch, branch_token, repo_token):
416
423
        try:
417
 
            with branch.repository.lock_write(token=repo_token):
 
424
            branch.repository.lock_write(token=repo_token)
 
425
            try:
418
426
                branch.lock_write(token=branch_token)
 
427
            finally:
 
428
                branch.repository.unlock()
419
429
        except errors.TokenMismatch:
420
 
            return FailedSmartServerResponse((b'TokenMismatch',))
 
430
            return FailedSmartServerResponse(('TokenMismatch',))
421
431
        if repo_token:
422
432
            branch.repository.dont_leave_lock_in_place()
423
433
        branch.dont_leave_lock_in_place()
424
434
        branch.unlock()
425
 
        return SuccessfulSmartServerResponse((b'ok',))
 
435
        return SuccessfulSmartServerResponse(('ok',))
426
436
 
427
437
 
428
438
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
433
443
 
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',))
437
447
        else:
438
 
            return SuccessfulSmartServerResponse((b'no',))
439
 
 
440
 
 
441
 
class SmartServerBranchRequestGetAllReferenceInfo(SmartServerBranchRequest):
442
 
    """Get the reference information.
443
 
 
444
 
    New in 3.1.
445
 
    """
446
 
 
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',))