1
# Copyright (C) 2007-2011 Canonical Ltd
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.
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.
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
17
"""WorkingTree3 format and implementation.
21
from __future__ import absolute_import
33
revision as _mod_revision,
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 (
43
WorkingTreeFormatMetaDir,
47
class PreDirStateWorkingTree(InventoryWorkingTree):
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
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())
64
# is this scan needed ? it makes things kinda slow.
68
trace.mutter("write hc")
71
def _write_hashcache_if_dirty(self):
72
"""Write out the hashcache if it is dirty."""
73
if self._hashcache.needs_write:
75
self._hashcache.write()
77
if e.errno not in (errno.EPERM, errno.EACCES):
79
# TODO: jam 20061219 Should this be a warning? A single line
80
# warning might be sufficient to let the user know what
82
trace.mutter('Could not write hashcache for %s\nError: %s',
83
self._hashcache.cache_file_name(), e)
85
def get_file_sha1(self, path, file_id=None, stat_value=None):
86
with self.lock_read():
87
return self._hashcache.get_sha1(path, stat_value)
90
class WorkingTree3(PreDirStateWorkingTree):
91
"""This is the Format 3 working tree.
93
This differs from the base WorkingTree by:
94
- having its own file lock
95
- having its own last-revision property.
97
This is new in bzr 0.8
100
def _last_revision(self):
101
"""See Mutable.last_revision."""
102
with self.lock_read():
104
return self._transport.get_bytes('last-revision')
105
except errors.NoSuchFile:
106
return _mod_revision.NULL_REVISION
108
def _change_last_revision(self, revision_id):
109
"""See WorkingTree._change_last_revision."""
110
if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
112
self._transport.delete('last-revision')
113
except errors.NoSuchFile:
117
self._transport.put_bytes('last-revision', revision_id,
118
mode=self.controldir._get_file_mode())
121
def _get_check_refs(self):
122
"""Return the references needed to perform a check of this tree."""
123
return [('trees', self.last_revision())]
126
if self._control_files._lock_count == 1:
127
# do non-implementation specific cleanup
129
# _inventory_is_modified is always False during a read lock.
130
if self._inventory_is_modified:
132
self._write_hashcache_if_dirty()
133
# reverse order of locking.
135
return self._control_files.unlock()
140
class WorkingTreeFormat3(WorkingTreeFormatMetaDir):
141
"""The second working tree format updated to record a format marker.
144
- exists within a metadir controlling .bzr
145
- includes an explicit version marker for the workingtree control
146
files, separate from the ControlDir format
147
- modifies the hash cache format
149
- uses a LockDir to guard access for writes.
152
upgrade_recommended = True
154
missing_parent_conflicts = True
156
supports_versioned_directories = True
159
def get_format_string(cls):
160
"""See WorkingTreeFormat.get_format_string()."""
161
return "Bazaar-NG Working Tree format 3"
163
def get_format_description(self):
164
"""See WorkingTreeFormat.get_format_description()."""
165
return "Working tree format 3"
167
_tree_class = WorkingTree3
169
def __get_matchingcontroldir(self):
170
return bzrdir.BzrDirMetaFormat1()
172
_matchingcontroldir = property(__get_matchingcontroldir)
174
def _open_control_files(self, a_controldir):
175
transport = a_controldir.get_workingtree_transport(None)
176
return LockableFiles(transport, 'lock', LockDir)
178
def initialize(self, a_controldir, revision_id=None, from_branch=None,
179
accelerator_tree=None, hardlink=False):
180
"""See WorkingTreeFormat.initialize().
182
:param revision_id: if supplied, create a working tree at a different
183
revision than the branch is at.
184
:param accelerator_tree: A tree which can be used for retrieving file
185
contents more quickly than the revision tree, i.e. a workingtree.
186
The revision tree will be used for cases where accelerator_tree's
187
content is different.
188
:param hardlink: If true, hard-link files from accelerator_tree,
191
if not isinstance(a_controldir.transport, LocalTransport):
192
raise errors.NotLocalUrl(a_controldir.transport.base)
193
transport = a_controldir.get_workingtree_transport(self)
194
control_files = self._open_control_files(a_controldir)
195
control_files.create_lock()
196
control_files.lock_write()
197
transport.put_bytes('format', self.as_string(),
198
mode=a_controldir._get_file_mode())
199
if from_branch is not None:
202
branch = a_controldir.open_branch()
203
if revision_id is None:
204
revision_id = _mod_revision.ensure_null(branch.last_revision())
205
# WorkingTree3 can handle an inventory which has a unique root id.
206
# as of bzr 0.12. However, bzr 0.11 and earlier fail to handle
207
# those trees. And because there isn't a format bump inbetween, we
208
# are maintaining compatibility with older clients.
209
# inv = Inventory(root_id=gen_root_id())
210
inv = self._initial_inventory()
211
wt = self._tree_class(a_controldir.root_transport.local_abspath('.'),
216
_controldir=a_controldir,
217
_control_files=control_files)
220
basis_tree = branch.repository.revision_tree(revision_id)
221
# only set an explicit root id if there is one to set.
222
if basis_tree.get_root_id() is not None:
223
wt.set_root_id(basis_tree.get_root_id())
224
if revision_id == _mod_revision.NULL_REVISION:
225
wt.set_parent_trees([])
227
wt.set_parent_trees([(revision_id, basis_tree)])
228
transform.build_tree(basis_tree, wt)
229
for hook in MutableTree.hooks['post_build_tree']:
232
# Unlock in this order so that the unlock-triggers-flush in
233
# WorkingTree is given a chance to fire.
234
control_files.unlock()
238
def _initial_inventory(self):
239
return inventory.Inventory()
241
def open(self, a_controldir, _found=False):
242
"""Return the WorkingTree object for a_controldir
244
_found is a private parameter, do not use it. It is used to indicate
245
if format probing has already been done.
248
# we are being called directly and must probe.
249
raise NotImplementedError
250
if not isinstance(a_controldir.transport, LocalTransport):
251
raise errors.NotLocalUrl(a_controldir.transport.base)
252
wt = self._open(a_controldir, self._open_control_files(a_controldir))
255
def _open(self, a_controldir, control_files):
256
"""Open the tree itself.
258
:param a_controldir: the dir for the tree.
259
:param control_files: the control files for the tree.
261
return self._tree_class(a_controldir.root_transport.local_abspath('.'),
264
_controldir=a_controldir,
265
_control_files=control_files)