/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: 2018-05-14 20:32:14 UTC
  • mto: This revision was merged to the branch mainline in revision 6967.
  • Revision ID: jelmer@jelmer.uk-20180514203214-i59tdreu9dnc1qp0
Add basic support for horizoned history.

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