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

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 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 ... import (
20
 
    bencode,
21
 
    errors,
22
 
    revision as _mod_revision,
23
 
    )
24
 
from ...controldir import ControlDir
25
 
from .request import (
 
19
 
 
20
from bzrlib import errors
 
21
from bzrlib.bzrdir import BzrDir
 
22
from bzrlib.smart.request import (
26
23
    FailedSmartServerResponse,
27
24
    SmartServerRequest,
28
25
    SuccessfulSmartServerResponse,
45
42
        :return: A SmartServerResponse from self.do_with_branch().
46
43
        """
47
44
        transport = self.transport_from_client_path(path)
48
 
        controldir = ControlDir.open_from_transport(transport)
49
 
        if controldir.get_branch_reference() is not None:
 
45
        bzrdir = BzrDir.open_from_transport(transport)
 
46
        if bzrdir.get_branch_reference() is not None:
50
47
            raise errors.NotBranchError(transport.base)
51
 
        branch = controldir.open_branch(ignore_fallbacks=True)
 
48
        branch = bzrdir.open_branch(ignore_fallbacks=True)
52
49
        return self.do_with_branch(branch, *args)
53
50
 
54
51
 
65
62
        processed.  The physical lock state won't be changed.
66
63
        """
67
64
        # 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)
71
 
 
72
 
 
73
 
class SmartServerBranchBreakLock(SmartServerBranchRequest):
74
 
 
75
 
    def do_with_branch(self, branch):
76
 
        """Break a branch lock.
77
 
        """
78
 
        branch.break_lock()
79
 
        return SuccessfulSmartServerResponse((b'ok', ), )
 
65
        branch.repository.lock_write(token=repo_token)
 
66
        try:
 
67
            branch.lock_write(token=branch_token)
 
68
            try:
 
69
                return self.do_with_locked_branch(branch, *args)
 
70
            finally:
 
71
                branch.unlock()
 
72
        finally:
 
73
            branch.repository.unlock()
80
74
 
81
75
 
82
76
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
87
81
        The body is not utf8 decoded - its the literal bytestream from disk.
88
82
        """
89
83
        try:
90
 
            content = branch.control_transport.get_bytes('branch.conf')
 
84
            content = branch._transport.get_bytes('branch.conf')
91
85
        except errors.NoSuchFile:
92
 
            content = b''
93
 
        return SuccessfulSmartServerResponse((b'ok', ), content)
94
 
 
95
 
 
96
 
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
97
 
    """Set the configuration data for a branch.
98
 
 
99
 
    New in 2.5.
100
 
    """
101
 
 
102
 
    def do_with_branch(self, branch, branch_token, repo_token):
103
 
        """Set the content of branch.conf.
104
 
 
105
 
        The body is not utf8 decoded - its the literal bytestream for disk.
106
 
        """
107
 
        self._branch = branch
108
 
        self._branch_token = branch_token
109
 
        self._repo_token = repo_token
110
 
        # Signal we want a body
111
 
        return None
112
 
 
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', ))
 
86
            content = ''
 
87
        return SuccessfulSmartServerResponse( ('ok', ), content)
119
88
 
120
89
 
121
90
class SmartServerBranchGetParent(SmartServerBranchRequest):
123
92
    def do_with_branch(self, branch):
124
93
        """Return the parent of branch."""
125
94
        parent = branch._get_parent_location() or ''
126
 
        return SuccessfulSmartServerResponse((parent.encode('utf-8'),))
 
95
        return SuccessfulSmartServerResponse((parent,))
127
96
 
128
97
 
129
98
class SmartServerBranchGetTagsBytes(SmartServerBranchRequest):
136
105
 
137
106
class SmartServerBranchSetTagsBytes(SmartServerLockedBranchRequest):
138
107
 
139
 
    def __init__(self, backing_transport, root_client_path='/', jail_root=None):
 
108
    def __init__(self, backing_transport, root_client_path='/'):
140
109
        SmartServerLockedBranchRequest.__init__(
141
 
            self, backing_transport, root_client_path, jail_root)
 
110
            self, backing_transport, root_client_path)
142
111
        self.locked = False
143
 
 
 
112
        
144
113
    def do_with_locked_branch(self, branch):
145
114
        """Call _set_tags_bytes for a branch.
146
115
 
170
139
            self.branch.unlock()
171
140
 
172
141
 
173
 
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
174
 
 
175
 
    def do_with_branch(self, branch):
176
 
        """Return the heads-to-fetch for a Branch as two bencoded lists.
177
 
 
178
 
        See Branch.heads_to_fetch.
179
 
 
180
 
        New in 2.4.
181
 
        """
182
 
        must_fetch, if_present_fetch = branch.heads_to_fetch()
183
 
        return SuccessfulSmartServerResponse(
184
 
            (list(must_fetch), list(if_present_fetch)))
185
 
 
186
 
 
187
142
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
188
143
 
189
144
    def do_with_branch(self, branch):
190
145
        stacked_on_url = branch.get_stacked_on_url()
191
 
        return SuccessfulSmartServerResponse((b'ok', stacked_on_url.encode('ascii')))
 
146
        return SuccessfulSmartServerResponse(('ok', stacked_on_url))
192
147
 
193
148
 
194
149
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
199
154
        The revision list is returned as the body content,
200
155
        with each revision utf8 encoded and \x00 joined.
201
156
        """
202
 
        with branch.lock_read():
203
 
            graph = branch.repository.get_graph()
204
 
            stop_revisions = (None, _mod_revision.NULL_REVISION)
205
 
            history = list(graph.iter_lefthand_ancestry(
206
 
                branch.last_revision(), stop_revisions))
207
157
        return SuccessfulSmartServerResponse(
208
 
            (b'ok', ), (b'\x00'.join(reversed(history))))
 
158
            ('ok', ), ('\x00'.join(branch.revision_history())))
209
159
 
210
160
 
211
161
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
216
166
        The revno is encoded in decimal, the revision_id is encoded as utf8.
217
167
        """
218
168
        revno, last_revision = branch.last_revision_info()
219
 
        return SuccessfulSmartServerResponse(
220
 
            (b'ok', str(revno).encode('ascii'), last_revision))
221
 
 
222
 
 
223
 
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
224
 
 
225
 
    def do_with_branch(self, branch, revid):
226
 
        """Return branch.revision_id_to_revno().
227
 
 
228
 
        New in 2.5.
229
 
 
230
 
        The revno is encoded in decimal, the revision_id is encoded as utf8.
231
 
        """
232
 
        try:
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))
242
170
 
243
171
 
244
172
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
249
177
    def do_with_locked_branch(self, branch, *args):
250
178
        try:
251
179
            return self.do_tip_change_with_locked_branch(branch, *args)
252
 
        except errors.TipChangeRejected as e:
 
180
        except errors.TipChangeRejected, e:
253
181
            msg = e.msg
254
 
            if isinstance(msg, str):
 
182
            if isinstance(msg, unicode):
255
183
                msg = msg.encode('utf-8')
256
 
            return FailedSmartServerResponse((b'TipChangeRejected', msg))
 
184
            return FailedSmartServerResponse(('TipChangeRejected', msg))
257
185
 
258
186
 
259
187
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
262
190
    def do_with_locked_branch(self, branch, value, name, section):
263
191
        if not section:
264
192
            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)
268
 
        return SuccessfulSmartServerResponse(())
269
 
 
270
 
 
271
 
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
272
 
    """Set an option in the branch configuration.
273
 
 
274
 
    New in 2.2.
275
 
    """
276
 
 
277
 
    def do_with_locked_branch(self, branch, value_dict, name, section):
278
 
        utf8_dict = bencode.bdecode(value_dict)
279
 
        value_dict = {}
280
 
        for key, value in utf8_dict.items():
281
 
            value_dict[key.decode('utf8')] = value.decode('utf8')
282
 
        if not section:
283
 
            section = None
284
 
        else:
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(())
288
195
 
289
196
 
290
197
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
291
198
 
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([])
295
202
        else:
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',))
301
209
 
302
210
 
303
211
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
304
212
 
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.
308
216
 
309
217
        New in 1.6.
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))
349
257
 
350
258
 
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.
354
262
 
355
 
    New in breezy 1.4.
 
263
    New in bzrlib 1.4.
356
264
    """
357
265
 
358
266
    def do_tip_change_with_locked_branch(self, branch, new_revno,
359
 
                                         new_last_revision_id):
 
267
            new_last_revision_id):
360
268
        try:
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',))
366
274
 
367
275
 
368
276
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
369
277
    """Set the parent location for a branch.
370
 
 
 
278
    
371
279
    Takes a location to set, which must be utf8 encoded.
372
280
    """
373
281
 
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(())
377
285
 
378
286
 
379
287
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
380
288
 
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'':
 
292
        if repo_token == '':
385
293
            repo_token = None
386
294
        try:
387
 
            repo_token = branch.repository.lock_write(
388
 
                token=repo_token).repository_token
 
295
            repo_token = branch.repository.lock_write(token=repo_token)
389
296
            try:
390
 
                branch_token = branch.lock_write(
391
 
                    token=branch_token).token
 
297
                branch_token = branch.lock_write(token=branch_token)
392
298
            finally:
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:
405
 
            repo_token = b''
 
310
            repo_token = ''
406
311
        else:
407
312
            branch.repository.leave_lock_in_place()
408
313
        branch.leave_lock_in_place()
409
314
        branch.unlock()
410
 
        return SuccessfulSmartServerResponse((b'ok', branch_token, repo_token))
 
315
        return SuccessfulSmartServerResponse(('ok', branch_token, repo_token))
411
316
 
412
317
 
413
318
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
414
319
 
415
320
    def do_with_branch(self, branch, branch_token, repo_token):
416
321
        try:
417
 
            with branch.repository.lock_write(token=repo_token):
 
322
            branch.repository.lock_write(token=repo_token)
 
323
            try:
418
324
                branch.lock_write(token=branch_token)
 
325
            finally:
 
326
                branch.repository.unlock()
419
327
        except errors.TokenMismatch:
420
 
            return FailedSmartServerResponse((b'TokenMismatch',))
 
328
            return FailedSmartServerResponse(('TokenMismatch',))
421
329
        if repo_token:
422
330
            branch.repository.dont_leave_lock_in_place()
423
331
        branch.dont_leave_lock_in_place()
424
332
        branch.unlock()
425
 
        return SuccessfulSmartServerResponse((b'ok',))
426
 
 
427
 
 
428
 
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
429
 
    """Get the physical lock status for a branch.
430
 
 
431
 
    New in 2.5.
432
 
    """
433
 
 
434
 
    def do_with_branch(self, branch):
435
 
        if branch.get_physical_lock_status():
436
 
            return SuccessfulSmartServerResponse((b'yes',))
437
 
        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)
 
333
        return SuccessfulSmartServerResponse(('ok',))
 
334