~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-19 10:58:39 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219105839-uji05ck4rkm1mj4j
Merge bzr.dev.

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