/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/bzr/smart/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-04 02:22:58 UTC
  • mto: (7027.4.10 python3-blackbox)
  • mto: This revision was merged to the branch mainline in revision 7038.
  • Revision ID: jelmer@jelmer.uk-20180704022258-0zbrom5bymskh7g3
Fix bisect.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Server-side branch related request implmentations."""
18
18
 
 
19
from __future__ import absolute_import
19
20
 
20
 
from bzrlib import errors
21
 
from bzrlib.bzrdir import BzrDir
22
 
from bzrlib.smart.request import (
 
21
from ... import (
 
22
    bencode,
 
23
    errors,
 
24
    revision as _mod_revision,
 
25
    )
 
26
from ...controldir import ControlDir
 
27
from ...sixish import text_type
 
28
from .request import (
23
29
    FailedSmartServerResponse,
24
30
    SmartServerRequest,
25
31
    SuccessfulSmartServerResponse,
42
48
        :return: A SmartServerResponse from self.do_with_branch().
43
49
        """
44
50
        transport = self.transport_from_client_path(path)
45
 
        bzrdir = BzrDir.open_from_transport(transport)
46
 
        if bzrdir.get_branch_reference() is not None:
 
51
        controldir = ControlDir.open_from_transport(transport)
 
52
        if controldir.get_branch_reference() is not None:
47
53
            raise errors.NotBranchError(transport.base)
48
 
        branch = bzrdir.open_branch(ignore_fallbacks=True)
 
54
        branch = controldir.open_branch(ignore_fallbacks=True)
49
55
        return self.do_with_branch(branch, *args)
50
56
 
51
57
 
62
68
        processed.  The physical lock state won't be changed.
63
69
        """
64
70
        # XXX: write a test for LockContention
65
 
        branch.repository.lock_write(token=repo_token)
66
 
        try:
67
 
            branch.lock_write(token=branch_token)
68
 
            try:
 
71
        with branch.repository.lock_write(token=repo_token), \
 
72
                branch.lock_write(token=branch_token):
69
73
                return self.do_with_locked_branch(branch, *args)
70
 
            finally:
71
 
                branch.unlock()
72
 
        finally:
73
 
            branch.repository.unlock()
 
74
 
 
75
 
 
76
class SmartServerBranchBreakLock(SmartServerBranchRequest):
 
77
 
 
78
    def do_with_branch(self, branch):
 
79
        """Break a branch lock.
 
80
        """
 
81
        branch.break_lock()
 
82
        return SuccessfulSmartServerResponse((b'ok', ), )
74
83
 
75
84
 
76
85
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
81
90
        The body is not utf8 decoded - its the literal bytestream from disk.
82
91
        """
83
92
        try:
84
 
            content = branch._transport.get_bytes('branch.conf')
 
93
            content = branch.control_transport.get_bytes('branch.conf')
85
94
        except errors.NoSuchFile:
86
 
            content = ''
87
 
        return SuccessfulSmartServerResponse( ('ok', ), content)
 
95
            content = b''
 
96
        return SuccessfulSmartServerResponse((b'ok', ), content)
 
97
 
 
98
 
 
99
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
 
100
    """Set the configuration data for a branch.
 
101
 
 
102
    New in 2.5.
 
103
    """
 
104
 
 
105
    def do_with_branch(self, branch, branch_token, repo_token):
 
106
        """Set the content of branch.conf.
 
107
 
 
108
        The body is not utf8 decoded - its the literal bytestream for disk.
 
109
        """
 
110
        self._branch = branch
 
111
        self._branch_token = branch_token
 
112
        self._repo_token = repo_token
 
113
        # Signal we want a body
 
114
        return None
 
115
 
 
116
    def do_body(self, body_bytes):
 
117
        with self._branch.repository.lock_write(token=self._repo_token), \
 
118
             self._branch.lock_write(token=self._branch_token):
 
119
                self._branch.control_transport.put_bytes(
 
120
                    'branch.conf', body_bytes)
 
121
        return SuccessfulSmartServerResponse((b'ok', ))
88
122
 
89
123
 
90
124
class SmartServerBranchGetParent(SmartServerBranchRequest):
92
126
    def do_with_branch(self, branch):
93
127
        """Return the parent of branch."""
94
128
        parent = branch._get_parent_location() or ''
95
 
        return SuccessfulSmartServerResponse((parent,))
 
129
        return SuccessfulSmartServerResponse((parent.encode('utf-8'),))
96
130
 
97
131
 
98
132
class SmartServerBranchGetTagsBytes(SmartServerBranchRequest):
139
173
            self.branch.unlock()
140
174
 
141
175
 
 
176
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
 
177
 
 
178
    def do_with_branch(self, branch):
 
179
        """Return the heads-to-fetch for a Branch as two bencoded lists.
 
180
        
 
181
        See Branch.heads_to_fetch.
 
182
 
 
183
        New in 2.4.
 
184
        """
 
185
        must_fetch, if_present_fetch = branch.heads_to_fetch()
 
186
        return SuccessfulSmartServerResponse(
 
187
            (list(must_fetch), list(if_present_fetch)))
 
188
 
 
189
 
142
190
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
143
191
 
144
192
    def do_with_branch(self, branch):
145
193
        stacked_on_url = branch.get_stacked_on_url()
146
 
        return SuccessfulSmartServerResponse(('ok', stacked_on_url))
 
194
        return SuccessfulSmartServerResponse((b'ok', stacked_on_url.encode('ascii')))
147
195
 
148
196
 
149
197
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
154
202
        The revision list is returned as the body content,
155
203
        with each revision utf8 encoded and \x00 joined.
156
204
        """
 
205
        with branch.lock_read():
 
206
            graph = branch.repository.get_graph()
 
207
            stop_revisions = (None, _mod_revision.NULL_REVISION)
 
208
            history = list(graph.iter_lefthand_ancestry(
 
209
                branch.last_revision(), stop_revisions))
157
210
        return SuccessfulSmartServerResponse(
158
 
            ('ok', ), ('\x00'.join(branch.revision_history())))
 
211
            (b'ok', ), (b'\x00'.join(reversed(history))))
159
212
 
160
213
 
161
214
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
166
219
        The revno is encoded in decimal, the revision_id is encoded as utf8.
167
220
        """
168
221
        revno, last_revision = branch.last_revision_info()
169
 
        return SuccessfulSmartServerResponse(('ok', str(revno), last_revision))
 
222
        return SuccessfulSmartServerResponse(
 
223
                (b'ok', str(revno).encode('ascii'), last_revision))
 
224
 
 
225
 
 
226
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
 
227
 
 
228
    def do_with_branch(self, branch, revid):
 
229
        """Return branch.revision_id_to_revno().
 
230
 
 
231
        New in 2.5.
 
232
 
 
233
        The revno is encoded in decimal, the revision_id is encoded as utf8.
 
234
        """
 
235
        try:
 
236
            dotted_revno = branch.revision_id_to_dotted_revno(revid)
 
237
        except errors.NoSuchRevision:
 
238
            return FailedSmartServerResponse((b'NoSuchRevision', revid))
 
239
        return SuccessfulSmartServerResponse(
 
240
            (b'ok', ) + tuple([b'%d' % x for x in dotted_revno]))
170
241
 
171
242
 
172
243
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
177
248
    def do_with_locked_branch(self, branch, *args):
178
249
        try:
179
250
            return self.do_tip_change_with_locked_branch(branch, *args)
180
 
        except errors.TipChangeRejected, e:
 
251
        except errors.TipChangeRejected as e:
181
252
            msg = e.msg
182
 
            if isinstance(msg, unicode):
 
253
            if isinstance(msg, text_type):
183
254
                msg = msg.encode('utf-8')
184
 
            return FailedSmartServerResponse(('TipChangeRejected', msg))
 
255
            return FailedSmartServerResponse((b'TipChangeRejected', msg))
185
256
 
186
257
 
187
258
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
190
261
    def do_with_locked_branch(self, branch, value, name, section):
191
262
        if not section:
192
263
            section = None
193
 
        branch._get_config().set_option(value.decode('utf8'), name, section)
 
264
        branch._get_config().set_option(
 
265
                value.decode('utf-8'), name.decode('utf-8'),
 
266
                section.decode('utf-8') if section is not None else None)
 
267
        return SuccessfulSmartServerResponse(())
 
268
 
 
269
 
 
270
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
 
271
    """Set an option in the branch configuration.
 
272
    
 
273
    New in 2.2.
 
274
    """
 
275
 
 
276
    def do_with_locked_branch(self, branch, value_dict, name, section):
 
277
        utf8_dict = bencode.bdecode(value_dict)
 
278
        value_dict = {}
 
279
        for key, value in utf8_dict.items():
 
280
            value_dict[key.decode('utf8')] = value.decode('utf8')
 
281
        if not section:
 
282
            section = None
 
283
        else:
 
284
            section = section.decode('utf-8')
 
285
        branch._get_config().set_option(value_dict, name.decode('utf-8'), section)
194
286
        return SuccessfulSmartServerResponse(())
195
287
 
196
288
 
197
289
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
198
290
 
199
291
    def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
200
 
        if new_last_revision_id == 'null:':
201
 
            branch.set_revision_history([])
 
292
        if new_last_revision_id == b'null:':
 
293
            branch.set_last_revision_info(0, new_last_revision_id)
202
294
        else:
203
295
            if not branch.repository.has_revision(new_last_revision_id):
204
296
                return FailedSmartServerResponse(
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',))
 
297
                    (b'NoSuchRevision', new_last_revision_id))
 
298
            branch.generate_revision_history(new_last_revision_id, None, None)
 
299
        return SuccessfulSmartServerResponse((b'ok',))
209
300
 
210
301
 
211
302
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
242
333
                relation = branch._revision_relations(
243
334
                    last_rev, new_last_revision_id, graph)
244
335
                if relation == 'diverged' and not allow_divergence:
245
 
                    return FailedSmartServerResponse(('Diverged',))
 
336
                    return FailedSmartServerResponse((b'Diverged',))
246
337
                if relation == 'a_descends_from_b' and do_not_overwrite_descendant:
247
338
                    return SuccessfulSmartServerResponse(
248
 
                        ('ok', last_revno, last_rev))
 
339
                        (b'ok', last_revno, last_rev))
249
340
            new_revno = graph.find_distance_to_null(
250
341
                new_last_revision_id, [(last_rev, last_revno)])
251
342
            branch.set_last_revision_info(new_revno, new_last_revision_id)
252
343
        except errors.GhostRevisionsHaveNoRevno:
253
344
            return FailedSmartServerResponse(
254
 
                ('NoSuchRevision', new_last_revision_id))
 
345
                (b'NoSuchRevision', new_last_revision_id))
255
346
        return SuccessfulSmartServerResponse(
256
 
            ('ok', new_revno, new_last_revision_id))
 
347
            (b'ok', new_revno, new_last_revision_id))
257
348
 
258
349
 
259
350
class SmartServerBranchRequestSetLastRevisionInfo(SmartServerSetTipRequest):
260
351
    """Branch.set_last_revision_info.  Sets the revno and the revision ID of
261
352
    the specified branch.
262
353
 
263
 
    New in bzrlib 1.4.
 
354
    New in breezy 1.4.
264
355
    """
265
356
 
266
357
    def do_tip_change_with_locked_branch(self, branch, new_revno,
269
360
            branch.set_last_revision_info(int(new_revno), new_last_revision_id)
270
361
        except errors.NoSuchRevision:
271
362
            return FailedSmartServerResponse(
272
 
                ('NoSuchRevision', new_last_revision_id))
273
 
        return SuccessfulSmartServerResponse(('ok',))
 
363
                (b'NoSuchRevision', new_last_revision_id))
 
364
        return SuccessfulSmartServerResponse((b'ok',))
274
365
 
275
366
 
276
367
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
286
377
 
287
378
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
288
379
 
289
 
    def do_with_branch(self, branch, branch_token='', repo_token=''):
290
 
        if branch_token == '':
 
380
    def do_with_branch(self, branch, branch_token=b'', repo_token=b''):
 
381
        if branch_token == b'':
291
382
            branch_token = None
292
 
        if repo_token == '':
 
383
        if repo_token == b'':
293
384
            repo_token = None
294
385
        try:
295
 
            repo_token = branch.repository.lock_write(token=repo_token)
 
386
            repo_token = branch.repository.lock_write(
 
387
                token=repo_token).repository_token
296
388
            try:
297
 
                branch_token = branch.lock_write(token=branch_token)
 
389
                branch_token = branch.lock_write(
 
390
                    token=branch_token).token
298
391
            finally:
299
392
                # this leaves the repository with 1 lock
300
393
                branch.repository.unlock()
301
394
        except errors.LockContention:
302
 
            return FailedSmartServerResponse(('LockContention',))
 
395
            return FailedSmartServerResponse((b'LockContention',))
303
396
        except errors.TokenMismatch:
304
 
            return FailedSmartServerResponse(('TokenMismatch',))
 
397
            return FailedSmartServerResponse((b'TokenMismatch',))
305
398
        except errors.UnlockableTransport:
306
 
            return FailedSmartServerResponse(('UnlockableTransport',))
307
 
        except errors.LockFailed, e:
308
 
            return FailedSmartServerResponse(('LockFailed', str(e.lock), str(e.why)))
 
399
            return FailedSmartServerResponse((b'UnlockableTransport',))
 
400
        except errors.LockFailed as e:
 
401
            return FailedSmartServerResponse((b'LockFailed', str(e.lock), str(e.why)))
309
402
        if repo_token is None:
310
 
            repo_token = ''
 
403
            repo_token = b''
311
404
        else:
312
405
            branch.repository.leave_lock_in_place()
313
406
        branch.leave_lock_in_place()
314
407
        branch.unlock()
315
 
        return SuccessfulSmartServerResponse(('ok', branch_token, repo_token))
 
408
        return SuccessfulSmartServerResponse((b'ok', branch_token, repo_token))
316
409
 
317
410
 
318
411
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
319
412
 
320
413
    def do_with_branch(self, branch, branch_token, repo_token):
321
414
        try:
322
 
            branch.repository.lock_write(token=repo_token)
323
 
            try:
 
415
            with branch.repository.lock_write(token=repo_token):
324
416
                branch.lock_write(token=branch_token)
325
 
            finally:
326
 
                branch.repository.unlock()
327
417
        except errors.TokenMismatch:
328
 
            return FailedSmartServerResponse(('TokenMismatch',))
 
418
            return FailedSmartServerResponse((b'TokenMismatch',))
329
419
        if repo_token:
330
420
            branch.repository.dont_leave_lock_in_place()
331
421
        branch.dont_leave_lock_in_place()
332
422
        branch.unlock()
333
 
        return SuccessfulSmartServerResponse(('ok',))
334
 
 
 
423
        return SuccessfulSmartServerResponse((b'ok',))
 
424
 
 
425
 
 
426
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
 
427
    """Get the physical lock status for a branch.
 
428
 
 
429
    New in 2.5.
 
430
    """
 
431
 
 
432
    def do_with_branch(self, branch):
 
433
        if branch.get_physical_lock_status():
 
434
            return SuccessfulSmartServerResponse((b'yes',))
 
435
        else:
 
436
            return SuccessfulSmartServerResponse((b'no',))