/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
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.smart.request import SmartServerRequest, SmartServerResponse
23
24
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
25
class SmartServerBranchRequest(SmartServerRequest):
26
    """Base class for handling common branch request logic."""
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
27
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
28
    def do(self, path, *args):
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
29
        """Execute a request for a branch at path.
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
30
31
        If the branch is a branch reference, NotBranchError is raised.
32
        """
33
        transport = self._backing_transport.clone(path)
34
        bzrdir = BzrDir.open_from_transport(transport)
35
        if bzrdir.get_branch_reference() is not None:
36
            raise errors.NotBranchError(transport.base)
37
        branch = bzrdir.open_branch()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
38
        return self.do_with_branch(branch, *args)
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
39
40
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
41
class SmartServerLockedBranchRequest(SmartServerBranchRequest):
42
    """Base class for handling common branch request logic for requests that
43
    need a write lock.
44
    """
45
46
    def do_with_branch(self, branch, branch_token, repo_token, *args):
47
        """Execute a request for a branch.
48
49
        A write lock will be acquired with the given tokens for the branch and
50
        repository locks.  The lock will be released once the request is
51
        processed.  The physical lock state won't be changed.
52
        """
53
        # XXX: write a test for LockContention
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
54
        branch.repository.lock_write(token=repo_token)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
55
        try:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
56
            branch.lock_write(token=branch_token)
57
            try:
58
                return self.do_with_locked_branch(branch, *args)
59
            finally:
60
                branch.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
61
        finally:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
62
            branch.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
63
64
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
65
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
66
    
67
    def do_with_branch(self, branch):
68
        """Return the content of branch.control_files.get('branch.conf').
69
        
70
        The body is not utf8 decoded - its the literal bytestream from disk.
71
        """
72
        try:
73
            content = branch.control_files.get('branch.conf').read()
74
        except errors.NoSuchFile:
75
            content = ''
76
        return SmartServerResponse( ('ok', ), content)
77
78
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
79
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
80
81
    def do_with_branch(self, branch):
82
        """Get the revision history for the branch.
83
84
        The revision list is returned as the body content,
85
        with each revision utf8 encoded and \x00 joined.
86
        """
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
87
        return SmartServerResponse(
88
            ('ok', ), ('\x00'.join(branch.revision_history())))
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
89
90
91
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
92
    
93
    def do_with_branch(self, branch):
94
        """Return branch.last_revision_info().
95
        
96
        The revno is encoded in decimal, the revision_id is encoded as utf8.
97
        """
98
        revno, last_revision = branch.last_revision_info()
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
99
        return SmartServerResponse(('ok', str(revno), last_revision))
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
100
101
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
102
class SmartServerBranchRequestSetLastRevision(SmartServerLockedBranchRequest):
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
103
    
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
104
    def do_with_locked_branch(self, branch, new_last_revision_id):
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
105
        if new_last_revision_id == 'null:':
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
106
            branch.set_revision_history([])
107
        else:
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
108
            if not branch.repository.has_revision(new_last_revision_id):
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
109
                return SmartServerResponse(
110
                    ('NoSuchRevision', new_last_revision_id))
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
111
            branch.generate_revision_history(new_last_revision_id)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
112
        return SmartServerResponse(('ok',))
113
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
114
115
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
116
    
117
    def do_with_branch(self, branch, branch_token='', repo_token=''):
118
        if branch_token == '':
119
            branch_token = None
120
        if repo_token == '':
121
            repo_token = None
122
        try:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
123
            repo_token = branch.repository.lock_write(token=repo_token)
124
            try:
125
                branch_token = branch.lock_write(token=branch_token)
126
            finally:
127
                branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
128
        except errors.LockContention:
129
            return SmartServerResponse(('LockContention',))
130
        except errors.TokenMismatch:
131
            return SmartServerResponse(('TokenMismatch',))
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
132
        except errors.UnlockableTransport:
133
            return SmartServerResponse(('UnlockableTransport',))
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
134
        branch.repository.leave_lock_in_place()
135
        branch.leave_lock_in_place()
136
        branch.unlock()
137
        return SmartServerResponse(('ok', branch_token, repo_token))
138
139
140
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
141
142
    def do_with_branch(self, branch, branch_token, repo_token):
143
        try:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
144
            branch.repository.lock_write(token=repo_token)
145
            try:
146
                branch.lock_write(token=branch_token)
147
            finally:
148
                branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
149
        except errors.TokenMismatch:
150
            return SmartServerResponse(('TokenMismatch',))
151
        branch.repository.dont_leave_lock_in_place()
152
        branch.dont_leave_lock_in_place()
153
        branch.unlock()
154
        return SmartServerResponse(('ok',))
155