/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
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
22
from bzrlib.revision import NULL_REVISION
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
23
from bzrlib.smart.request import SmartServerRequest, SmartServerResponse
24
25
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
26
class SmartServerBranchRequest(SmartServerRequest):
27
    """Base class for handling common branch request logic."""
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
28
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
29
    def do(self, path, *args):
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
30
        """Execute a request for a branch at path.
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
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()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
39
        return self.do_with_branch(branch, *args)
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
40
41
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
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.lock_write(tokens=(branch_token, repo_token))
56
        try:
57
            return self.do_with_locked_branch(branch, *args)
58
        finally:
59
            branch.unlock()
60
61
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
62
class SmartServerBranchGetConfigFile(SmartServerBranchRequest):
63
    
64
    def do_with_branch(self, branch):
65
        """Return the content of branch.control_files.get('branch.conf').
66
        
67
        The body is not utf8 decoded - its the literal bytestream from disk.
68
        """
69
        try:
70
            content = branch.control_files.get('branch.conf').read()
71
        except errors.NoSuchFile:
72
            content = ''
73
        return SmartServerResponse( ('ok', ), content)
74
75
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
76
class SmartServerRequestRevisionHistory(SmartServerBranchRequest):
77
78
    def do_with_branch(self, branch):
79
        """Get the revision history for the branch.
80
81
        The revision list is returned as the body content,
82
        with each revision utf8 encoded and \x00 joined.
83
        """
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
84
        return SmartServerResponse(
85
            ('ok', ), ('\x00'.join(branch.revision_history())))
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
86
87
88
class SmartServerBranchRequestLastRevisionInfo(SmartServerBranchRequest):
89
    
90
    def do_with_branch(self, branch):
91
        """Return branch.last_revision_info().
92
        
93
        The revno is encoded in decimal, the revision_id is encoded as utf8.
94
        """
95
        revno, last_revision = branch.last_revision_info()
96
        if last_revision == NULL_REVISION:
97
            last_revision = ''
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
98
        return SmartServerResponse(('ok', str(revno), last_revision))
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
99
100
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
101
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.
102
    
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
103
    def do_with_locked_branch(self, branch, 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.
104
        if new_last_revision_id == '':
105
            branch.set_revision_history([])
106
        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.
107
            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.
108
                return SmartServerResponse(
109
                    ('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.
110
            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.
111
        return SmartServerResponse(('ok',))
112
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
113
114
class SmartServerBranchRequestLockWrite(SmartServerBranchRequest):
115
    
116
    def do_with_branch(self, branch, branch_token='', repo_token=''):
117
        if branch_token == '':
118
            branch_token = None
119
        if repo_token == '':
120
            repo_token = None
121
        tokens = (branch_token, repo_token)
122
        if tokens == ('', ''):
123
            tokens = None
124
        try:
125
            branch_token, repo_token = branch.lock_write(tokens=tokens)
126
        except errors.LockContention:
127
            return SmartServerResponse(('LockContention',))
128
        except errors.TokenMismatch:
129
            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.
130
        except errors.UnlockableTransport:
131
            return SmartServerResponse(('UnlockableTransport',))
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
132
        branch.repository.leave_lock_in_place()
133
        branch.leave_lock_in_place()
134
        branch.unlock()
135
        return SmartServerResponse(('ok', branch_token, repo_token))
136
137
138
class SmartServerBranchRequestUnlock(SmartServerBranchRequest):
139
140
    def do_with_branch(self, branch, branch_token, repo_token):
141
        tokens = branch_token, repo_token
142
        try:
143
            tokens = branch.lock_write(tokens=tokens)
144
        except errors.TokenMismatch:
145
            return SmartServerResponse(('TokenMismatch',))
146
        branch.repository.dont_leave_lock_in_place()
147
        branch.dont_leave_lock_in_place()
148
        branch.unlock()
149
        return SmartServerResponse(('ok',))
150