/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: Patch Queue Manager
  • Date: 2011-11-24 13:15:51 UTC
  • mfrom: (6240.4.5 rmbranch-colo)
  • Revision ID: pqm@pqm.ubuntu.com-20111124131551-u7xzxgto1p1yfz57
(gz) Support removing colocated branches in 'bzr rmbranch' (Martin Packman)

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
 
81
85
        The body is not utf8 decoded - its the literal bytestream from disk.
82
86
        """
83
87
        try:
84
 
            content = branch._transport.get_bytes('branch.conf')
 
88
            content = branch.control_transport.get_bytes('branch.conf')
85
89
        except errors.NoSuchFile:
86
90
            content = ''
87
91
        return SuccessfulSmartServerResponse( ('ok', ), content)
88
92
 
89
93
 
 
94
class SmartServerBranchPutConfigFile(SmartServerBranchRequest):
 
95
    """Set the configuration data for a branch.
 
96
 
 
97
    New in 2.5.
 
98
    """
 
99
 
 
100
    def do_with_branch(self, branch, branch_token, repo_token):
 
101
        """Set the content of branch.conf.
 
102
 
 
103
        The body is not utf8 decoded - its the literal bytestream for disk.
 
104
        """
 
105
        self._branch = branch
 
106
        self._branch_token = branch_token
 
107
        self._repo_token = repo_token
 
108
        # Signal we want a body
 
109
        return None
 
110
 
 
111
    def do_body(self, body_bytes):
 
112
        self._branch.repository.lock_write(token=self._repo_token)
 
113
        try:
 
114
            self._branch.lock_write(token=self._branch_token)
 
115
            try:
 
116
                self._branch.control_transport.put_bytes(
 
117
                    'branch.conf', body_bytes)
 
118
            finally:
 
119
                self._branch.unlock()
 
120
        finally:
 
121
            self._branch.repository.unlock()
 
122
        return SuccessfulSmartServerResponse(('ok', ))
 
123
 
 
124
 
90
125
class SmartServerBranchGetParent(SmartServerBranchRequest):
91
126
 
92
127
    def do_with_branch(self, branch):
139
174
            self.branch.unlock()
140
175
 
141
176
 
 
177
class SmartServerBranchHeadsToFetch(SmartServerBranchRequest):
 
178
 
 
179
    def do_with_branch(self, branch):
 
180
        """Return the heads-to-fetch for a Branch as two bencoded lists.
 
181
        
 
182
        See Branch.heads_to_fetch.
 
183
 
 
184
        New in 2.4.
 
185
        """
 
186
        must_fetch, if_present_fetch = branch.heads_to_fetch()
 
187
        return SuccessfulSmartServerResponse(
 
188
            (list(must_fetch), list(if_present_fetch)))
 
189
 
 
190
 
142
191
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
143
192
 
144
193
    def do_with_branch(self, branch):
154
203
        The revision list is returned as the body content,
155
204
        with each revision utf8 encoded and \x00 joined.
156
205
        """
 
206
        branch.lock_read()
 
207
        try:
 
208
            graph = branch.repository.get_graph()
 
209
            stop_revisions = (None, _mod_revision.NULL_REVISION)
 
210
            history = list(graph.iter_lefthand_ancestry(
 
211
                branch.last_revision(), stop_revisions))
 
212
        finally:
 
213
            branch.unlock()
157
214
        return SuccessfulSmartServerResponse(
158
 
            ('ok', ), ('\x00'.join(branch.revision_history())))
 
215
            ('ok', ), ('\x00'.join(reversed(history))))
159
216
 
160
217
 
161
218
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
194
251
        return SuccessfulSmartServerResponse(())
195
252
 
196
253
 
 
254
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
 
255
    """Set an option in the branch configuration.
 
256
    
 
257
    New in 2.2.
 
258
    """
 
259
 
 
260
    def do_with_locked_branch(self, branch, value_dict, name, section):
 
261
        utf8_dict = bencode.bdecode(value_dict)
 
262
        value_dict = {}
 
263
        for key, value in utf8_dict.items():
 
264
            value_dict[key.decode('utf8')] = value.decode('utf8')
 
265
        if not section:
 
266
            section = None
 
267
        branch._get_config().set_option(value_dict, name, section)
 
268
        return SuccessfulSmartServerResponse(())
 
269
 
 
270
 
197
271
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
198
272
 
199
273
    def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
200
274
        if new_last_revision_id == 'null:':
201
 
            branch.set_revision_history([])
 
275
            branch._set_revision_history([])
202
276
        else:
203
277
            if not branch.repository.has_revision(new_last_revision_id):
204
278
                return FailedSmartServerResponse(
205
279
                    ('NoSuchRevision', new_last_revision_id))
206
 
            branch.set_revision_history(branch._lefthand_history(
 
280
            branch._set_revision_history(branch._lefthand_history(
207
281
                new_last_revision_id, None, None))
208
282
        return SuccessfulSmartServerResponse(('ok',))
209
283
 
292
366
        if repo_token == '':
293
367
            repo_token = None
294
368
        try:
295
 
            repo_token = branch.repository.lock_write(token=repo_token)
 
369
            repo_token = branch.repository.lock_write(
 
370
                token=repo_token).repository_token
296
371
            try:
297
 
                branch_token = branch.lock_write(token=branch_token)
 
372
                branch_token = branch.lock_write(
 
373
                    token=branch_token).branch_token
298
374
            finally:
299
375
                # this leaves the repository with 1 lock
300
376
                branch.repository.unlock()