/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
1
# Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Weave-era branch implementations."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
21
from bzrlib import (
22
    errors,
23
    lockable_files,
24
    )
25
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
26
from bzrlib.decorators import (
27
    needs_read_lock,
28
    needs_write_lock,
29
    only_raises,
30
    )
31
from bzrlib.lock import LogicalLockResult
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
32
from bzrlib.trace import mutter
33
34
from bzrlib.branch import (
35
    BranchFormat,
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
36
    BranchWriteLockResult,
5718.8.1 by Jelmer Vernooij
Add FullHistoryBzrBranch.
37
    FullHistoryBzrBranch,
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
38
    )
39
40
5718.8.1 by Jelmer Vernooij
Add FullHistoryBzrBranch.
41
class BzrBranch4(FullHistoryBzrBranch):
5718.8.2 by Jelmer Vernooij
Split out full history branch code.
42
    """Branch format 4."""
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
43
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
44
    def lock_write(self, token=None):
45
        """Lock the branch for write operations.
46
47
        :param token: A token to permit reacquiring a previously held and
48
            preserved lock.
49
        :return: A BranchWriteLockResult.
50
        """
51
        if not self.is_locked():
52
            self._note_lock('w')
6531.2.2 by Vincent Ladeuil
Better version separating the lock methods between branch and branch format 4 and removing the duplication.
53
            # All-in-one needs to always unlock/lock.
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
54
            self.repository._warn_if_deprecated(self)
55
            self.repository.lock_write()
56
            took_lock = True
57
        else:
58
            took_lock = False
59
        try:
60
            return BranchWriteLockResult(self.unlock,
61
                self.control_files.lock_write(token=token))
62
        except:
63
            if took_lock:
64
                self.repository.unlock()
65
            raise
66
67
    def lock_read(self):
68
        """Lock the branch for read operations.
69
70
        :return: A bzrlib.lock.LogicalLockResult.
71
        """
72
        if not self.is_locked():
73
            self._note_lock('r')
6531.2.2 by Vincent Ladeuil
Better version separating the lock methods between branch and branch format 4 and removing the duplication.
74
            # All-in-one needs to always unlock/lock.
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
75
            self.repository._warn_if_deprecated(self)
76
            self.repository.lock_read()
77
            took_lock = True
78
        else:
79
            took_lock = False
80
        try:
81
            self.control_files.lock_read()
82
            return LogicalLockResult(self.unlock)
83
        except:
84
            if took_lock:
85
                self.repository.unlock()
86
            raise
87
88
    @only_raises(errors.LockNotHeld, errors.LockBroken)
89
    def unlock(self):
90
        if self.control_files._lock_count == 2 and self.conf_store is not None:
91
            self.conf_store.save_changes()
92
        try:
93
            self.control_files.unlock()
94
        finally:
95
            # All-in-one needs to always unlock/lock.
6531.2.2 by Vincent Ladeuil
Better version separating the lock methods between branch and branch format 4 and removing the duplication.
96
            self.repository.unlock()
6531.2.1 by Vincent Ladeuil
Add a failing test and a brain-dead fix (duplicating the existing code).
97
            if not self.control_files.is_locked():
98
                # we just released the lock
99
                self._clear_cached_state()
100
6127.1.9 by Jelmer Vernooij
Add lightweight option to _get_checkout_format().
101
    def _get_checkout_format(self, lightweight=False):
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
102
        """Return the most suitable metadir for a checkout of this branch.
103
        """
5582.10.83 by Jelmer Vernooij
merge weave-branch branch.
104
        from bzrlib.plugins.weave_fmt.repository import RepositoryFormat7
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
105
        from bzrlib.bzrdir import BzrDirMetaFormat1
106
        format = BzrDirMetaFormat1()
6127.1.11 by Jelmer Vernooij
Fix weave format test.
107
        if lightweight:
108
            format.set_branch_format(self._format)
109
            format.repository_format = self.bzrdir._format.repository_format
110
        else:
111
            format.repository_format = RepositoryFormat7()
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
112
        return format
113
5718.8.2 by Jelmer Vernooij
Split out full history branch code.
114
    def unbind(self):
115
        raise errors.UpgradeRequired(self.user_url)
116
117
    def bind(self, other):
118
        raise errors.UpgradeRequired(self.user_url)
119
120
    def set_bound_location(self, location):
121
        raise NotImplementedError(self.set_bound_location)
122
123
    def get_bound_location(self):
124
        return None
125
126
    def update(self):
127
        return None
128
129
    def get_master_branch(self, possible_transports=None):
130
        return None
131
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
132
133
class BzrBranchFormat4(BranchFormat):
134
    """Bzr branch format 4.
135
136
    This format has:
137
     - a revision-history file.
138
     - a branch-lock lock file [ to be shared with the bzrdir ]
5718.8.2 by Jelmer Vernooij
Split out full history branch code.
139
140
    It does not support binding.
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
141
    """
142
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
143
    def initialize(self, a_bzrdir, name=None, repository=None,
144
                   append_revisions_only=None):
5705.2.1 by Jelmer Vernooij
Inline _initialize_helper.
145
        """Create a branch of this format in a_bzrdir.
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
146
147
        :param a_bzrdir: The bzrdir to initialize the branch in
148
        :param name: Name of colocated branch to create, if any
5705.2.1 by Jelmer Vernooij
Inline _initialize_helper.
149
        :param repository: Repository for this branch (unused)
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
150
        """
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
151
        if append_revisions_only:
152
            raise errors.UpgradeRequired(a_bzrdir.user_url)
5705.2.1 by Jelmer Vernooij
Inline _initialize_helper.
153
        if repository is not None:
154
            raise NotImplementedError(
155
                "initialize(repository=<not None>) on %r" % (self,))
5705.2.2 by Jelmer Vernooij
Only support BzrBranchFormat4 in pre-metadir bzr dirs.
156
        if not [isinstance(a_bzrdir._format, format) for format in
157
                self._compatible_bzrdirs]:
158
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
5705.2.1 by Jelmer Vernooij
Inline _initialize_helper.
159
        utf8_files = [('revision-history', ''),
160
                      ('branch-name', ''),
161
                      ]
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
162
        mutter('creating branch %r in %s', self, a_bzrdir.user_url)
163
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
164
        control_files = lockable_files.LockableFiles(branch_transport,
165
            'branch-lock', lockable_files.TransportLock)
166
        control_files.create_lock()
167
        try:
168
            control_files.lock_write()
169
        except errors.LockContention:
170
            lock_taken = False
171
        else:
172
            lock_taken = True
173
        try:
174
            for (filename, content) in utf8_files:
175
                branch_transport.put_bytes(
176
                    filename, content,
177
                    mode=a_bzrdir._get_file_mode())
178
        finally:
179
            if lock_taken:
180
                control_files.unlock()
181
        branch = self.open(a_bzrdir, name, _found=True,
182
                found_repository=None)
183
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
184
        return branch
185
186
    def __init__(self):
187
        super(BzrBranchFormat4, self).__init__()
5582.10.87 by Jelmer Vernooij
Merge bzr.dev.
188
        from bzrlib.plugins.weave_fmt.bzrdir import (
189
            BzrDirFormat4, BzrDirFormat5, BzrDirFormat6,
190
            )
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
191
        self._matchingbzrdir = BzrDirFormat6()
5705.2.2 by Jelmer Vernooij
Only support BzrBranchFormat4 in pre-metadir bzr dirs.
192
        self._compatible_bzrdirs = [BzrDirFormat4, BzrDirFormat5,
193
            BzrDirFormat6]
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
194
195
    def network_name(self):
196
        """The network name for this format is the control dirs disk label."""
197
        return self._matchingbzrdir.get_format_string()
198
5705.2.4 by Jelmer Vernooij
Add BzrBranchFormat4.get_format_description().
199
    def get_format_description(self):
5705.2.6 by Jelmer Vernooij
Fix case sensitivity.
200
        return "Branch format 4"
5705.2.4 by Jelmer Vernooij
Add BzrBranchFormat4.get_format_description().
201
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
202
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
6305.3.3 by Jelmer Vernooij
Fix use of possible_transports.
203
            found_repository=None, possible_transports=None):
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
204
        """See BranchFormat.open()."""
6436.1.2 by Jelmer Vernooij
Fix some tests.
205
        if name is None:
206
            name = a_bzrdir._get_selected_branch()
207
        if name != "":
5699.4.8 by Jelmer Vernooij
Properly raise NoColocatedBranchSupport.
208
            raise errors.NoColocatedBranchSupport(self)
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
209
        if not _found:
210
            # we are being called directly and must probe.
211
            raise NotImplementedError
212
        if found_repository is None:
213
            found_repository = a_bzrdir.open_repository()
5718.8.1 by Jelmer Vernooij
Add FullHistoryBzrBranch.
214
        return BzrBranch4(_format=self,
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
215
                         _control_files=a_bzrdir._control_files,
216
                         a_bzrdir=a_bzrdir,
217
                         name=name,
6305.3.3 by Jelmer Vernooij
Fix use of possible_transports.
218
                         _repository=found_repository,
219
                         possible_transports=possible_transports)
5697.2.1 by Jelmer Vernooij
Move weave branch to bzrlib.branch_weave.
220
221
    def __str__(self):
222
        return "Bazaar-NG branch format 4"
223
224
    def supports_leaving_lock(self):
225
        return False