~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-15 11:53:48 UTC
  • mto: This revision was merged to the branch mainline in revision 6375.
  • Revision ID: jelmer@samba.org-20111215115348-murs91ipn8jbw6y0
Add tests for default_email behaviour.

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
17
17
"""Server-side branch related request implmentations."""
18
18
 
19
19
 
20
 
from bzrlib import errors
21
 
from bzrlib.bzrdir import BzrDir
 
20
from bzrlib import (
 
21
    bencode,
 
22
    errors,
 
23
    revision as _mod_revision,
 
24
    )
 
25
from bzrlib.controldir import ControlDir
22
26
from bzrlib.smart.request import (
23
27
    FailedSmartServerResponse,
24
28
    SmartServerRequest,
42
46
        :return: A SmartServerResponse from self.do_with_branch().
43
47
        """
44
48
        transport = self.transport_from_client_path(path)
45
 
        bzrdir = BzrDir.open_from_transport(transport)
46
 
        if bzrdir.get_branch_reference() is not None:
 
49
        controldir = ControlDir.open_from_transport(transport)
 
50
        if controldir.get_branch_reference() is not None:
47
51
            raise errors.NotBranchError(transport.base)
48
 
        branch = bzrdir.open_branch(ignore_fallbacks=True)
 
52
        branch = controldir.open_branch(ignore_fallbacks=True)
49
53
        return self.do_with_branch(branch, *args)
50
54
 
51
55
 
73
77
            branch.repository.unlock()
74
78
 
75
79
 
 
80
class SmartServerBranchBreakLock(SmartServerBranchRequest):
 
81
 
 
82
    def do_with_branch(self, branch):
 
83
        """Break a branch lock.
 
84
        """
 
85
        branch.break_lock()
 
86
        return SuccessfulSmartServerResponse(('ok', ), )
 
87
 
 
88
 
76
89
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
77
90
 
78
91
    def do_with_branch(self, branch):
81
94
        The body is not utf8 decoded - its the literal bytestream from disk.
82
95
        """
83
96
        try:
84
 
            content = branch._transport.get_bytes('branch.conf')
 
97
            content = branch.control_transport.get_bytes('branch.conf')
85
98
        except errors.NoSuchFile:
86
99
            content = ''
87
100
        return SuccessfulSmartServerResponse( ('ok', ), content)
88
101
 
89
102
 
 
103
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
 
104
    """Set the configuration data for a branch.
 
105
 
 
106
    New in 2.5.
 
107
    """
 
108
 
 
109
    def do_with_branch(self, branch, branch_token, repo_token):
 
110
        """Set the content of branch.conf.
 
111
 
 
112
        The body is not utf8 decoded - its the literal bytestream for disk.
 
113
        """
 
114
        self._branch = branch
 
115
        self._branch_token = branch_token
 
116
        self._repo_token = repo_token
 
117
        # Signal we want a body
 
118
        return None
 
119
 
 
120
    def do_body(self, body_bytes):
 
121
        self._branch.repository.lock_write(token=self._repo_token)
 
122
        try:
 
123
            self._branch.lock_write(token=self._branch_token)
 
124
            try:
 
125
                self._branch.control_transport.put_bytes(
 
126
                    'branch.conf', body_bytes)
 
127
            finally:
 
128
                self._branch.unlock()
 
129
        finally:
 
130
            self._branch.repository.unlock()
 
131
        return SuccessfulSmartServerResponse(('ok', ))
 
132
 
 
133
 
90
134
class SmartServerBranchGetParent(SmartServerBranchRequest):
91
135
 
92
136
    def do_with_branch(self, branch):
139
183
            self.branch.unlock()
140
184
 
141
185
 
 
186
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
 
187
 
 
188
    def do_with_branch(self, branch):
 
189
        """Return the heads-to-fetch for a Branch as two bencoded lists.
 
190
        
 
191
        See Branch.heads_to_fetch.
 
192
 
 
193
        New in 2.4.
 
194
        """
 
195
        must_fetch, if_present_fetch = branch.heads_to_fetch()
 
196
        return SuccessfulSmartServerResponse(
 
197
            (list(must_fetch), list(if_present_fetch)))
 
198
 
 
199
 
142
200
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
143
201
 
144
202
    def do_with_branch(self, branch):
154
212
        The revision list is returned as the body content,
155
213
        with each revision utf8 encoded and \x00 joined.
156
214
        """
 
215
        branch.lock_read()
 
216
        try:
 
217
            graph = branch.repository.get_graph()
 
218
            stop_revisions = (None, _mod_revision.NULL_REVISION)
 
219
            history = list(graph.iter_lefthand_ancestry(
 
220
                branch.last_revision(), stop_revisions))
 
221
        finally:
 
222
            branch.unlock()
157
223
        return SuccessfulSmartServerResponse(
158
 
            ('ok', ), ('\x00'.join(branch.revision_history())))
 
224
            ('ok', ), ('\x00'.join(reversed(history))))
159
225
 
160
226
 
161
227
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
169
235
        return SuccessfulSmartServerResponse(('ok', str(revno), last_revision))
170
236
 
171
237
 
 
238
class SmartServerBranchRequestRevisionIdToRevno(SmartServerBranchRequest):
 
239
 
 
240
    def do_with_branch(self, branch, revid):
 
241
        """Return branch.revision_id_to_revno().
 
242
 
 
243
        New in 2.5.
 
244
 
 
245
        The revno is encoded in decimal, the revision_id is encoded as utf8.
 
246
        """
 
247
        try:
 
248
            dotted_revno = branch.revision_id_to_dotted_revno(revid)
 
249
        except errors.NoSuchRevision:
 
250
            return FailedSmartServerResponse(('NoSuchRevision', revid))
 
251
        return SuccessfulSmartServerResponse(
 
252
            ('ok', ) + tuple(map(str, dotted_revno)))
 
253
 
 
254
 
172
255
class SmartServerSetTipRequest(SmartServerLockedBranchRequest):
173
256
    """Base class for handling common branch request logic for requests that
174
257
    update the branch tip.
194
277
        return SuccessfulSmartServerResponse(())
195
278
 
196
279
 
 
280
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
 
281
    """Set an option in the branch configuration.
 
282
    
 
283
    New in 2.2.
 
284
    """
 
285
 
 
286
    def do_with_locked_branch(self, branch, value_dict, name, section):
 
287
        utf8_dict = bencode.bdecode(value_dict)
 
288
        value_dict = {}
 
289
        for key, value in utf8_dict.items():
 
290
            value_dict[key.decode('utf8')] = value.decode('utf8')
 
291
        if not section:
 
292
            section = None
 
293
        branch._get_config().set_option(value_dict, name, section)
 
294
        return SuccessfulSmartServerResponse(())
 
295
 
 
296
 
197
297
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
198
298
 
199
299
    def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
200
300
        if new_last_revision_id == 'null:':
201
 
            branch.set_revision_history([])
 
301
            branch._set_revision_history([])
202
302
        else:
203
303
            if not branch.repository.has_revision(new_last_revision_id):
204
304
                return FailedSmartServerResponse(
205
305
                    ('NoSuchRevision', new_last_revision_id))
206
 
            branch.set_revision_history(branch._lefthand_history(
 
306
            branch._set_revision_history(branch._lefthand_history(
207
307
                new_last_revision_id, None, None))
208
308
        return SuccessfulSmartServerResponse(('ok',))
209
309
 
292
392
        if repo_token == '':
293
393
            repo_token = None
294
394
        try:
295
 
            repo_token = branch.repository.lock_write(token=repo_token)
 
395
            repo_token = branch.repository.lock_write(
 
396
                token=repo_token).repository_token
296
397
            try:
297
 
                branch_token = branch.lock_write(token=branch_token)
 
398
                branch_token = branch.lock_write(
 
399
                    token=branch_token).branch_token
298
400
            finally:
299
401
                # this leaves the repository with 1 lock
300
402
                branch.repository.unlock()
332
434
        branch.unlock()
333
435
        return SuccessfulSmartServerResponse(('ok',))
334
436
 
 
437
 
 
438
class SmartServerBranchRequestGetPhysicalLockStatus(SmartServerBranchRequest):
 
439
    """Get the physical lock status for a branch.
 
440
 
 
441
    New in 2.5.
 
442
    """
 
443
 
 
444
    def do_with_branch(self, branch):
 
445
        if branch.get_physical_lock_status():
 
446
            return SuccessfulSmartServerResponse(('yes',))
 
447
        else:
 
448
            return SuccessfulSmartServerResponse(('no',))