/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: Andrew Bennetts
  • Date: 2007-04-19 14:55:20 UTC
  • mfrom: (2404.1.1 faster-tests)
  • mto: This revision was merged to the branch mainline in revision 2435.
  • Revision ID: andrew.bennetts@canonical.com-20070419145520-3j94u46y5zfh0ybq
Disconnect RemoteTransports in some tests to avoid tripping up test_strace with leftover threads from previous tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Server-side branch related request implmentations."""
 
18
 
 
19
 
 
20
from bzrlib import errors
 
21
from bzrlib.bzrdir import BzrDir
 
22
from bzrlib.revision import NULL_REVISION
 
23
from bzrlib.smart.request import SmartServerRequest, SmartServerResponse
 
24
 
 
25
 
 
26
class SmartServerBranchRequest(SmartServerRequest):
 
27
    """Base class for handling common branch request logic."""
 
28
 
 
29
    def do(self, path, *args):
 
30
        """Execute a request for a branch at path.
 
31
 
 
32
        If the branch is a branch reference, NotBranchError is raised.
 
33
        """
 
34
        transport = self._backing_transport.clone(path)
 
35
        bzrdir = BzrDir.open_from_transport(transport)
 
36
        if bzrdir.get_branch_reference() is not None:
 
37
            raise errors.NotBranchError(transport.base)
 
38
        branch = bzrdir.open_branch()
 
39
        return self.do_with_branch(branch, *args)
 
40
 
 
41
 
 
42
class SmartServerLockedBranchRequest(SmartServerBranchRequest):
 
43
    """Base class for handling common branch request logic for requests that
 
44
    need a write lock.
 
45
    """
 
46
 
 
47
    def do_with_branch(self, branch, branch_token, repo_token, *args):
 
48
        """Execute a request for a branch.
 
49
 
 
50
        A write lock will be acquired with the given tokens for the branch and
 
51
        repository locks.  The lock will be released once the request is
 
52
        processed.  The physical lock state won't be changed.
 
53
        """
 
54
        # XXX: write a test for LockContention
 
55
        branch.repository.lock_write(token=repo_token)
 
56
        try:
 
57
            branch.lock_write(token=branch_token)
 
58
            try:
 
59
                return self.do_with_locked_branch(branch, *args)
 
60
            finally:
 
61
                branch.unlock()
 
62
        finally:
 
63
            branch.repository.unlock()
 
64
 
 
65
 
 
66
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
 
67
    
 
68
    def do_with_branch(self, branch):
 
69
        """Return the content of branch.control_files.get('branch.conf').
 
70
        
 
71
        The body is not utf8 decoded - its the literal bytestream from disk.
 
72
        """
 
73
        try:
 
74
            content = branch.control_files.get('branch.conf').read()
 
75
        except errors.NoSuchFile:
 
76
            content = ''
 
77
        return SmartServerResponse( ('ok', ), content)
 
78
 
 
79
 
 
80
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
 
81
 
 
82
    def do_with_branch(self, branch):
 
83
        """Get the revision history for the branch.
 
84
 
 
85
        The revision list is returned as the body content,
 
86
        with each revision utf8 encoded and \x00 joined.
 
87
        """
 
88
        return SmartServerResponse(
 
89
            ('ok', ), ('\x00'.join(branch.revision_history())))
 
90
 
 
91
 
 
92
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
 
93
    
 
94
    def do_with_branch(self, branch):
 
95
        """Return branch.last_revision_info().
 
96
        
 
97
        The revno is encoded in decimal, the revision_id is encoded as utf8.
 
98
        """
 
99
        revno, last_revision = branch.last_revision_info()
 
100
        return SmartServerResponse(('ok', str(revno), last_revision))
 
101
 
 
102
 
 
103
class SmartServerBranchRequestSetLastRevision(SmartServerLockedBranchRequest):
 
104
    
 
105
    def do_with_locked_branch(self, branch, new_last_revision_id):
 
106
        if new_last_revision_id == 'null:':
 
107
            branch.set_revision_history([])
 
108
        else:
 
109
            if not branch.repository.has_revision(new_last_revision_id):
 
110
                return SmartServerResponse(
 
111
                    ('NoSuchRevision', new_last_revision_id))
 
112
            branch.generate_revision_history(new_last_revision_id)
 
113
        return SmartServerResponse(('ok',))
 
114
 
 
115
 
 
116
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
 
117
    
 
118
    def do_with_branch(self, branch, branch_token='', repo_token=''):
 
119
        if branch_token == '':
 
120
            branch_token = None
 
121
        if repo_token == '':
 
122
            repo_token = None
 
123
        try:
 
124
            repo_token = branch.repository.lock_write(token=repo_token)
 
125
            try:
 
126
                branch_token = branch.lock_write(token=branch_token)
 
127
            finally:
 
128
                branch.repository.unlock()
 
129
        except errors.LockContention:
 
130
            return SmartServerResponse(('LockContention',))
 
131
        except errors.TokenMismatch:
 
132
            return SmartServerResponse(('TokenMismatch',))
 
133
        except errors.UnlockableTransport:
 
134
            return SmartServerResponse(('UnlockableTransport',))
 
135
        branch.repository.leave_lock_in_place()
 
136
        branch.leave_lock_in_place()
 
137
        branch.unlock()
 
138
        return SmartServerResponse(('ok', branch_token, repo_token))
 
139
 
 
140
 
 
141
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
 
142
 
 
143
    def do_with_branch(self, branch, branch_token, repo_token):
 
144
        try:
 
145
            branch.repository.lock_write(token=repo_token)
 
146
            try:
 
147
                branch.lock_write(token=branch_token)
 
148
            finally:
 
149
                branch.repository.unlock()
 
150
        except errors.TokenMismatch:
 
151
            return SmartServerResponse(('TokenMismatch',))
 
152
        branch.repository.dont_leave_lock_in_place()
 
153
        branch.dont_leave_lock_in_place()
 
154
        branch.unlock()
 
155
        return SmartServerResponse(('ok',))
 
156