/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 breezy/bzr/workingtree_3.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-31 22:36:57 UTC
  • mfrom: (6729.7.2 move-errors-knit)
  • Revision ID: jelmer@jelmer.uk-20170731223657-m1gjn4xvesat87v4
Merge lp:~jelmer/brz/move-errors-knit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2011 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
"""WorkingTree3 format and implementation.
 
18
 
 
19
"""
 
20
 
 
21
from __future__ import absolute_import
 
22
 
 
23
import errno
 
24
 
 
25
from . import (
 
26
    bzrdir,
 
27
    inventory,
 
28
    )
 
29
 
 
30
from .. import (
 
31
    errors,
 
32
    hashcache,
 
33
    revision as _mod_revision,
 
34
    trace,
 
35
    transform,
 
36
    )
 
37
from ..decorators import (
 
38
    needs_read_lock,
 
39
    )
 
40
from ..lockable_files import LockableFiles
 
41
from ..lockdir import LockDir
 
42
from ..mutabletree import MutableTree
 
43
from ..transport.local import LocalTransport
 
44
from .workingtree import (
 
45
    InventoryWorkingTree,
 
46
    WorkingTreeFormatMetaDir,
 
47
    )
 
48
 
 
49
 
 
50
class PreDirStateWorkingTree(InventoryWorkingTree):
 
51
 
 
52
    def __init__(self, basedir='.', *args, **kwargs):
 
53
        super(PreDirStateWorkingTree, self).__init__(basedir, *args, **kwargs)
 
54
        # update the whole cache up front and write to disk if anything changed;
 
55
        # in the future we might want to do this more selectively
 
56
        # two possible ways offer themselves : in self._unlock, write the cache
 
57
        # if needed, or, when the cache sees a change, append it to the hash
 
58
        # cache file, and have the parser take the most recent entry for a
 
59
        # given path only.
 
60
        wt_trans = self.controldir.get_workingtree_transport(None)
 
61
        cache_filename = wt_trans.local_abspath('stat-cache')
 
62
        self._hashcache = hashcache.HashCache(basedir, cache_filename,
 
63
            self.controldir._get_file_mode(),
 
64
            self._content_filter_stack_provider())
 
65
        hc = self._hashcache
 
66
        hc.read()
 
67
        # is this scan needed ? it makes things kinda slow.
 
68
        #hc.scan()
 
69
 
 
70
        if hc.needs_write:
 
71
            trace.mutter("write hc")
 
72
            hc.write()
 
73
 
 
74
    def _write_hashcache_if_dirty(self):
 
75
        """Write out the hashcache if it is dirty."""
 
76
        if self._hashcache.needs_write:
 
77
            try:
 
78
                self._hashcache.write()
 
79
            except OSError as e:
 
80
                if e.errno not in (errno.EPERM, errno.EACCES):
 
81
                    raise
 
82
                # TODO: jam 20061219 Should this be a warning? A single line
 
83
                #       warning might be sufficient to let the user know what
 
84
                #       is going on.
 
85
                trace.mutter('Could not write hashcache for %s\nError: %s',
 
86
                              self._hashcache.cache_file_name(), e)
 
87
 
 
88
    @needs_read_lock
 
89
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
90
        if not path:
 
91
            path = self._inventory.id2path(file_id)
 
92
        return self._hashcache.get_sha1(path, stat_value)
 
93
 
 
94
 
 
95
class WorkingTree3(PreDirStateWorkingTree):
 
96
    """This is the Format 3 working tree.
 
97
 
 
98
    This differs from the base WorkingTree by:
 
99
     - having its own file lock
 
100
     - having its own last-revision property.
 
101
 
 
102
    This is new in bzr 0.8
 
103
    """
 
104
 
 
105
    @needs_read_lock
 
106
    def _last_revision(self):
 
107
        """See Mutable.last_revision."""
 
108
        try:
 
109
            return self._transport.get_bytes('last-revision')
 
110
        except errors.NoSuchFile:
 
111
            return _mod_revision.NULL_REVISION
 
112
 
 
113
    def _change_last_revision(self, revision_id):
 
114
        """See WorkingTree._change_last_revision."""
 
115
        if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
 
116
            try:
 
117
                self._transport.delete('last-revision')
 
118
            except errors.NoSuchFile:
 
119
                pass
 
120
            return False
 
121
        else:
 
122
            self._transport.put_bytes('last-revision', revision_id,
 
123
                mode=self.controldir._get_file_mode())
 
124
            return True
 
125
 
 
126
    def _get_check_refs(self):
 
127
        """Return the references needed to perform a check of this tree."""
 
128
        return [('trees', self.last_revision())]
 
129
 
 
130
    def unlock(self):
 
131
        if self._control_files._lock_count == 1:
 
132
           # do non-implementation specific cleanup
 
133
            self._cleanup()
 
134
            # _inventory_is_modified is always False during a read lock.
 
135
            if self._inventory_is_modified:
 
136
                self.flush()
 
137
            self._write_hashcache_if_dirty()
 
138
        # reverse order of locking.
 
139
        try:
 
140
            return self._control_files.unlock()
 
141
        finally:
 
142
            self.branch.unlock()
 
143
 
 
144
 
 
145
class WorkingTreeFormat3(WorkingTreeFormatMetaDir):
 
146
    """The second working tree format updated to record a format marker.
 
147
 
 
148
    This format:
 
149
        - exists within a metadir controlling .bzr
 
150
        - includes an explicit version marker for the workingtree control
 
151
          files, separate from the ControlDir format
 
152
        - modifies the hash cache format
 
153
        - is new in bzr 0.8
 
154
        - uses a LockDir to guard access for writes.
 
155
    """
 
156
 
 
157
    upgrade_recommended = True
 
158
 
 
159
    missing_parent_conflicts = True
 
160
 
 
161
    supports_versioned_directories = True
 
162
 
 
163
    @classmethod
 
164
    def get_format_string(cls):
 
165
        """See WorkingTreeFormat.get_format_string()."""
 
166
        return "Bazaar-NG Working Tree format 3"
 
167
 
 
168
    def get_format_description(self):
 
169
        """See WorkingTreeFormat.get_format_description()."""
 
170
        return "Working tree format 3"
 
171
 
 
172
    _tree_class = WorkingTree3
 
173
 
 
174
    def __get_matchingbzrdir(self):
 
175
        return bzrdir.BzrDirMetaFormat1()
 
176
 
 
177
    _matchingbzrdir = property(__get_matchingbzrdir)
 
178
 
 
179
    def _open_control_files(self, a_controldir):
 
180
        transport = a_controldir.get_workingtree_transport(None)
 
181
        return LockableFiles(transport, 'lock', LockDir)
 
182
 
 
183
    def initialize(self, a_controldir, revision_id=None, from_branch=None,
 
184
                   accelerator_tree=None, hardlink=False):
 
185
        """See WorkingTreeFormat.initialize().
 
186
 
 
187
        :param revision_id: if supplied, create a working tree at a different
 
188
            revision than the branch is at.
 
189
        :param accelerator_tree: A tree which can be used for retrieving file
 
190
            contents more quickly than the revision tree, i.e. a workingtree.
 
191
            The revision tree will be used for cases where accelerator_tree's
 
192
            content is different.
 
193
        :param hardlink: If true, hard-link files from accelerator_tree,
 
194
            where possible.
 
195
        """
 
196
        if not isinstance(a_controldir.transport, LocalTransport):
 
197
            raise errors.NotLocalUrl(a_controldir.transport.base)
 
198
        transport = a_controldir.get_workingtree_transport(self)
 
199
        control_files = self._open_control_files(a_controldir)
 
200
        control_files.create_lock()
 
201
        control_files.lock_write()
 
202
        transport.put_bytes('format', self.as_string(),
 
203
            mode=a_controldir._get_file_mode())
 
204
        if from_branch is not None:
 
205
            branch = from_branch
 
206
        else:
 
207
            branch = a_controldir.open_branch()
 
208
        if revision_id is None:
 
209
            revision_id = _mod_revision.ensure_null(branch.last_revision())
 
210
        # WorkingTree3 can handle an inventory which has a unique root id.
 
211
        # as of bzr 0.12. However, bzr 0.11 and earlier fail to handle
 
212
        # those trees. And because there isn't a format bump inbetween, we
 
213
        # are maintaining compatibility with older clients.
 
214
        # inv = Inventory(root_id=gen_root_id())
 
215
        inv = self._initial_inventory()
 
216
        wt = self._tree_class(a_controldir.root_transport.local_abspath('.'),
 
217
                         branch,
 
218
                         inv,
 
219
                         _internal=True,
 
220
                         _format=self,
 
221
                         _controldir=a_controldir,
 
222
                         _control_files=control_files)
 
223
        wt.lock_tree_write()
 
224
        try:
 
225
            basis_tree = branch.repository.revision_tree(revision_id)
 
226
            # only set an explicit root id if there is one to set.
 
227
            if basis_tree.get_root_id() is not None:
 
228
                wt.set_root_id(basis_tree.get_root_id())
 
229
            if revision_id == _mod_revision.NULL_REVISION:
 
230
                wt.set_parent_trees([])
 
231
            else:
 
232
                wt.set_parent_trees([(revision_id, basis_tree)])
 
233
            transform.build_tree(basis_tree, wt)
 
234
            for hook in MutableTree.hooks['post_build_tree']:
 
235
                hook(wt)
 
236
        finally:
 
237
            # Unlock in this order so that the unlock-triggers-flush in
 
238
            # WorkingTree is given a chance to fire.
 
239
            control_files.unlock()
 
240
            wt.unlock()
 
241
        return wt
 
242
 
 
243
    def _initial_inventory(self):
 
244
        return inventory.Inventory()
 
245
 
 
246
    def open(self, a_controldir, _found=False):
 
247
        """Return the WorkingTree object for a_controldir
 
248
 
 
249
        _found is a private parameter, do not use it. It is used to indicate
 
250
               if format probing has already been done.
 
251
        """
 
252
        if not _found:
 
253
            # we are being called directly and must probe.
 
254
            raise NotImplementedError
 
255
        if not isinstance(a_controldir.transport, LocalTransport):
 
256
            raise errors.NotLocalUrl(a_controldir.transport.base)
 
257
        wt = self._open(a_controldir, self._open_control_files(a_controldir))
 
258
        return wt
 
259
 
 
260
    def _open(self, a_controldir, control_files):
 
261
        """Open the tree itself.
 
262
 
 
263
        :param a_controldir: the dir for the tree.
 
264
        :param control_files: the control files for the tree.
 
265
        """
 
266
        return self._tree_class(a_controldir.root_transport.local_abspath('.'),
 
267
                                _internal=True,
 
268
                                _format=self,
 
269
                                _controldir=a_controldir,
 
270
                                _control_files=control_files)