~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Server-side branch related request implmentations."""
18
18
 
19
19
 
20
 
from bzrlib import errors
 
20
from bzrlib import (
 
21
    bencode,
 
22
    errors,
 
23
    )
21
24
from bzrlib.bzrdir import BzrDir
22
25
from bzrlib.smart.request import (
23
26
    FailedSmartServerResponse,
32
35
 
33
36
    def do(self, path, *args):
34
37
        """Execute a request for a branch at path.
35
 
    
 
38
 
36
39
        All Branch requests take a path to the branch as their first argument.
37
40
 
38
41
        If the branch is a branch reference, NotBranchError is raised.
45
48
        bzrdir = BzrDir.open_from_transport(transport)
46
49
        if bzrdir.get_branch_reference() is not None:
47
50
            raise errors.NotBranchError(transport.base)
48
 
        branch = bzrdir.open_branch()
 
51
        branch = bzrdir.open_branch(ignore_fallbacks=True)
49
52
        return self.do_with_branch(branch, *args)
50
53
 
51
54
 
74
77
 
75
78
 
76
79
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
77
 
    
 
80
 
78
81
    def do_with_branch(self, branch):
79
82
        """Return the content of branch.conf
80
 
        
 
83
 
81
84
        The body is not utf8 decoded - its the literal bytestream from disk.
82
85
        """
83
 
        # This was at one time called by RemoteBranchLockableFiles
84
 
        # intercepting access to this file; as of 1.5 it is not called by the
85
 
        # client but retained for compatibility.  It may be called again to
86
 
        # allow the client to get the configuration without needing vfs
87
 
        # access.
88
86
        try:
89
87
            content = branch._transport.get_bytes('branch.conf')
90
88
        except errors.NoSuchFile:
92
90
        return SuccessfulSmartServerResponse( ('ok', ), content)
93
91
 
94
92
 
 
93
class SmartServerBranchGetParent(SmartServerBranchRequest):
 
94
 
 
95
    def do_with_branch(self, branch):
 
96
        """Return the parent of branch."""
 
97
        parent = branch._get_parent_location() or ''
 
98
        return SuccessfulSmartServerResponse((parent,))
 
99
 
 
100
 
 
101
class SmartServerBranchGetTagsBytes(SmartServerBranchRequest):
 
102
 
 
103
    def do_with_branch(self, branch):
 
104
        """Return the _get_tags_bytes for a branch."""
 
105
        bytes = branch._get_tags_bytes()
 
106
        return SuccessfulSmartServerResponse((bytes,))
 
107
 
 
108
 
 
109
class SmartServerBranchSetTagsBytes(SmartServerLockedBranchRequest):
 
110
 
 
111
    def __init__(self, backing_transport, root_client_path='/', jail_root=None):
 
112
        SmartServerLockedBranchRequest.__init__(
 
113
            self, backing_transport, root_client_path, jail_root)
 
114
        self.locked = False
 
115
        
 
116
    def do_with_locked_branch(self, branch):
 
117
        """Call _set_tags_bytes for a branch.
 
118
 
 
119
        New in 1.18.
 
120
        """
 
121
        # We need to keep this branch locked until we get a body with the tags
 
122
        # bytes.
 
123
        self.branch = branch
 
124
        self.branch.lock_write()
 
125
        self.locked = True
 
126
 
 
127
    def do_body(self, bytes):
 
128
        self.branch._set_tags_bytes(bytes)
 
129
        return SuccessfulSmartServerResponse(())
 
130
 
 
131
    def do_end(self):
 
132
        # TODO: this request shouldn't have to do this housekeeping manually.
 
133
        # Some of this logic probably belongs in a base class.
 
134
        if not self.locked:
 
135
            # We never acquired the branch successfully in the first place, so
 
136
            # there's nothing more to do.
 
137
            return
 
138
        try:
 
139
            return SmartServerLockedBranchRequest.do_end(self)
 
140
        finally:
 
141
            # Only try unlocking if we locked successfully in the first place
 
142
            self.branch.unlock()
 
143
 
 
144
 
95
145
class SmartServerBranchRequestGetStackedOnURL(SmartServerBranchRequest):
96
146
 
97
147
    def do_with_branch(self, branch):
112
162
 
113
163
 
114
164
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
115
 
    
 
165
 
116
166
    def do_with_branch(self, branch):
117
167
        """Return branch.last_revision_info().
118
 
        
 
168
 
119
169
        The revno is encoded in decimal, the revision_id is encoded as utf8.
120
170
        """
121
171
        revno, last_revision = branch.last_revision_info()
137
187
            return FailedSmartServerResponse(('TipChangeRejected', msg))
138
188
 
139
189
 
 
190
class SmartServerBranchRequestSetConfigOption(SmartServerLockedBranchRequest):
 
191
    """Set an option in the branch configuration."""
 
192
 
 
193
    def do_with_locked_branch(self, branch, value, name, section):
 
194
        if not section:
 
195
            section = None
 
196
        branch._get_config().set_option(value.decode('utf8'), name, section)
 
197
        return SuccessfulSmartServerResponse(())
 
198
 
 
199
 
 
200
class SmartServerBranchRequestSetConfigOptionDict(SmartServerLockedBranchRequest):
 
201
    """Set an option in the branch configuration.
 
202
    
 
203
    New in 2.2.
 
204
    """
 
205
 
 
206
    def do_with_locked_branch(self, branch, value_dict, name, section):
 
207
        utf8_dict = bencode.bdecode(value_dict)
 
208
        value_dict = {}
 
209
        for key, value in utf8_dict.items():
 
210
            value_dict[key.decode('utf8')] = value.decode('utf8')
 
211
        if not section:
 
212
            section = None
 
213
        branch._get_config().set_option(value_dict, name, section)
 
214
        return SuccessfulSmartServerResponse(())
 
215
 
 
216
 
140
217
class SmartServerBranchRequestSetLastRevision(SmartServerSetTipRequest):
141
 
    
 
218
 
142
219
    def do_tip_change_with_locked_branch(self, branch, new_last_revision_id):
143
220
        if new_last_revision_id == 'null:':
144
221
            branch.set_revision_history([])
146
223
            if not branch.repository.has_revision(new_last_revision_id):
147
224
                return FailedSmartServerResponse(
148
225
                    ('NoSuchRevision', new_last_revision_id))
149
 
            branch.generate_revision_history(new_last_revision_id)
 
226
            branch.set_revision_history(branch._lefthand_history(
 
227
                new_last_revision_id, None, None))
150
228
        return SuccessfulSmartServerResponse(('ok',))
151
229
 
152
230
 
153
231
class SmartServerBranchRequestSetLastRevisionEx(SmartServerSetTipRequest):
154
 
    
 
232
 
155
233
    def do_tip_change_with_locked_branch(self, branch, new_last_revision_id,
156
234
            allow_divergence, allow_overwrite_descendant):
157
235
        """Set the last revision of the branch.
158
236
 
159
237
        New in 1.6.
160
 
        
 
238
 
161
239
        :param new_last_revision_id: the revision ID to set as the last
162
240
            revision of the branch.
163
241
        :param allow_divergence: A flag.  If non-zero, change the revision ID
204
282
 
205
283
    New in bzrlib 1.4.
206
284
    """
207
 
    
 
285
 
208
286
    def do_tip_change_with_locked_branch(self, branch, new_revno,
209
287
            new_last_revision_id):
210
288
        try:
215
293
        return SuccessfulSmartServerResponse(('ok',))
216
294
 
217
295
 
 
296
class SmartServerBranchRequestSetParentLocation(SmartServerLockedBranchRequest):
 
297
    """Set the parent location for a branch.
 
298
    
 
299
    Takes a location to set, which must be utf8 encoded.
 
300
    """
 
301
 
 
302
    def do_with_locked_branch(self, branch, location):
 
303
        branch._set_parent_location(location)
 
304
        return SuccessfulSmartServerResponse(())
 
305
 
 
306
 
218
307
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
219
 
    
 
308
 
220
309
    def do_with_branch(self, branch, branch_token='', repo_token=''):
221
310
        if branch_token == '':
222
311
            branch_token = None
223
312
        if repo_token == '':
224
313
            repo_token = None
225
314
        try:
226
 
            repo_token = branch.repository.lock_write(token=repo_token)
 
315
            repo_token = branch.repository.lock_write(
 
316
                token=repo_token).repository_token
227
317
            try:
228
 
                branch_token = branch.lock_write(token=branch_token)
 
318
                branch_token = branch.lock_write(
 
319
                    token=branch_token).branch_token
229
320
            finally:
230
321
                # this leaves the repository with 1 lock
231
322
                branch.repository.unlock()
262
353
        branch.dont_leave_lock_in_place()
263
354
        branch.unlock()
264
355
        return SuccessfulSmartServerResponse(('ok',))
265
 
        
 
356