~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/weave_fmt/branch.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
"""Weave-era branch implementations."""
18
 
 
19
 
from __future__ import absolute_import
20
 
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    lockable_files,
24
 
    )
25
 
 
26
 
from bzrlib.decorators import (
27
 
    needs_read_lock,
28
 
    needs_write_lock,
29
 
    only_raises,
30
 
    )
31
 
from bzrlib.lock import LogicalLockResult
32
 
from bzrlib.trace import mutter
33
 
 
34
 
from bzrlib.branch import (
35
 
    BranchFormat,
36
 
    BranchWriteLockResult,
37
 
    )
38
 
from bzrlib.branchfmt.fullhistory import (
39
 
    FullHistoryBzrBranch,
40
 
    )
41
 
 
42
 
 
43
 
class BzrBranch4(FullHistoryBzrBranch):
44
 
    """Branch format 4."""
45
 
 
46
 
    def lock_write(self, token=None):
47
 
        """Lock the branch for write operations.
48
 
 
49
 
        :param token: A token to permit reacquiring a previously held and
50
 
            preserved lock.
51
 
        :return: A BranchWriteLockResult.
52
 
        """
53
 
        if not self.is_locked():
54
 
            self._note_lock('w')
55
 
        # All-in-one needs to always unlock/lock.
56
 
        self.repository._warn_if_deprecated(self)
57
 
        self.repository.lock_write()
58
 
        try:
59
 
            return BranchWriteLockResult(self.unlock,
60
 
                self.control_files.lock_write(token=token))
61
 
        except:
62
 
            self.repository.unlock()
63
 
            raise
64
 
 
65
 
    def lock_read(self):
66
 
        """Lock the branch for read operations.
67
 
 
68
 
        :return: A bzrlib.lock.LogicalLockResult.
69
 
        """
70
 
        if not self.is_locked():
71
 
            self._note_lock('r')
72
 
        # All-in-one needs to always unlock/lock.
73
 
        self.repository._warn_if_deprecated(self)
74
 
        self.repository.lock_read()
75
 
        try:
76
 
            self.control_files.lock_read()
77
 
            return LogicalLockResult(self.unlock)
78
 
        except:
79
 
            self.repository.unlock()
80
 
            raise
81
 
 
82
 
    @only_raises(errors.LockNotHeld, errors.LockBroken)
83
 
    def unlock(self):
84
 
        if self.control_files._lock_count == 2 and self.conf_store is not None:
85
 
            self.conf_store.save_changes()
86
 
        try:
87
 
            self.control_files.unlock()
88
 
        finally:
89
 
            # All-in-one needs to always unlock/lock.
90
 
            self.repository.unlock()
91
 
            if not self.control_files.is_locked():
92
 
                # we just released the lock
93
 
                self._clear_cached_state()
94
 
 
95
 
    def _get_checkout_format(self, lightweight=False):
96
 
        """Return the most suitable metadir for a checkout of this branch.
97
 
        """
98
 
        from bzrlib.plugins.weave_fmt.repository import RepositoryFormat7
99
 
        from bzrlib.bzrdir import BzrDirMetaFormat1
100
 
        format = BzrDirMetaFormat1()
101
 
        if lightweight:
102
 
            format.set_branch_format(self._format)
103
 
            format.repository_format = self.bzrdir._format.repository_format
104
 
        else:
105
 
            format.repository_format = RepositoryFormat7()
106
 
        return format
107
 
 
108
 
    def unbind(self):
109
 
        raise errors.UpgradeRequired(self.user_url)
110
 
 
111
 
    def bind(self, other):
112
 
        raise errors.UpgradeRequired(self.user_url)
113
 
 
114
 
    def set_bound_location(self, location):
115
 
        raise NotImplementedError(self.set_bound_location)
116
 
 
117
 
    def get_bound_location(self):
118
 
        return None
119
 
 
120
 
    def update(self):
121
 
        return None
122
 
 
123
 
    def get_master_branch(self, possible_transports=None):
124
 
        return None
125
 
 
126
 
 
127
 
class BzrBranchFormat4(BranchFormat):
128
 
    """Bzr branch format 4.
129
 
 
130
 
    This format has:
131
 
     - a revision-history file.
132
 
     - a branch-lock lock file [ to be shared with the bzrdir ]
133
 
 
134
 
    It does not support binding.
135
 
    """
136
 
 
137
 
    def initialize(self, a_bzrdir, name=None, repository=None,
138
 
                   append_revisions_only=None):
139
 
        """Create a branch of this format in a_bzrdir.
140
 
 
141
 
        :param a_bzrdir: The bzrdir to initialize the branch in
142
 
        :param name: Name of colocated branch to create, if any
143
 
        :param repository: Repository for this branch (unused)
144
 
        """
145
 
        if append_revisions_only:
146
 
            raise errors.UpgradeRequired(a_bzrdir.user_url)
147
 
        if repository is not None:
148
 
            raise NotImplementedError(
149
 
                "initialize(repository=<not None>) on %r" % (self,))
150
 
        if not [isinstance(a_bzrdir._format, format) for format in
151
 
                self._compatible_bzrdirs]:
152
 
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
153
 
        utf8_files = [('revision-history', ''),
154
 
                      ('branch-name', ''),
155
 
                      ]
156
 
        mutter('creating branch %r in %s', self, a_bzrdir.user_url)
157
 
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
158
 
        control_files = lockable_files.LockableFiles(branch_transport,
159
 
            'branch-lock', lockable_files.TransportLock)
160
 
        control_files.create_lock()
161
 
        try:
162
 
            control_files.lock_write()
163
 
        except errors.LockContention:
164
 
            lock_taken = False
165
 
        else:
166
 
            lock_taken = True
167
 
        try:
168
 
            for (filename, content) in utf8_files:
169
 
                branch_transport.put_bytes(
170
 
                    filename, content,
171
 
                    mode=a_bzrdir._get_file_mode())
172
 
        finally:
173
 
            if lock_taken:
174
 
                control_files.unlock()
175
 
        branch = self.open(a_bzrdir, name, _found=True,
176
 
                found_repository=None)
177
 
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
178
 
        return branch
179
 
 
180
 
    def __init__(self):
181
 
        super(BzrBranchFormat4, self).__init__()
182
 
        from bzrlib.plugins.weave_fmt.bzrdir import (
183
 
            BzrDirFormat4, BzrDirFormat5, BzrDirFormat6,
184
 
            )
185
 
        self._matchingbzrdir = BzrDirFormat6()
186
 
        self._compatible_bzrdirs = [BzrDirFormat4, BzrDirFormat5,
187
 
            BzrDirFormat6]
188
 
 
189
 
    def network_name(self):
190
 
        """The network name for this format is the control dirs disk label."""
191
 
        return self._matchingbzrdir.get_format_string()
192
 
 
193
 
    def get_format_description(self):
194
 
        return "Branch format 4"
195
 
 
196
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
197
 
            found_repository=None, possible_transports=None):
198
 
        """See BranchFormat.open()."""
199
 
        if name is None:
200
 
            name = a_bzrdir._get_selected_branch()
201
 
        if name != "":
202
 
            raise errors.NoColocatedBranchSupport(self)
203
 
        if not _found:
204
 
            # we are being called directly and must probe.
205
 
            raise NotImplementedError
206
 
        if found_repository is None:
207
 
            found_repository = a_bzrdir.open_repository()
208
 
        return BzrBranch4(_format=self,
209
 
                         _control_files=a_bzrdir._control_files,
210
 
                         a_bzrdir=a_bzrdir,
211
 
                         name=name,
212
 
                         _repository=found_repository,
213
 
                         possible_transports=possible_transports)
214
 
 
215
 
    def __str__(self):
216
 
        return "Bazaar-NG branch format 4"
217
 
 
218
 
    def supports_leaving_lock(self):
219
 
        return False