/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
1
# Copyright (C) 2005-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 working tree objects."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from ... import (
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
22
    conflicts as _mod_conflicts,
23
    errors,
6754.8.9 by Jelmer Vernooij
Fix more tests.
24
    lock,
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
25
    osutils,
26
    revision as _mod_revision,
27
    transform,
6670.4.1 by Jelmer Vernooij
Update imports.
28
    )
29
from ...bzr import (
30
    inventory,
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
31
    xml5,
32
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
33
from ...decorators import needs_read_lock
34
from ...mutabletree import MutableTree
35
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
36
    BytesIO,
37
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
38
from ...transport.local import LocalTransport
39
from ...workingtree import (
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
40
    WorkingTreeFormat,
41
    )
6670.4.1 by Jelmer Vernooij
Update imports.
42
from ...bzr.workingtree_3 import (
6110.4.4 by Jelmer Vernooij
Add common base class.
43
    PreDirStateWorkingTree,
44
    )
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
45
46
47
def get_conflicted_stem(path):
48
    for suffix in _mod_conflicts.CONFLICT_SUFFIXES:
49
        if path.endswith(suffix):
50
            return path[:-len(suffix)]
51
52
53
class WorkingTreeFormat2(WorkingTreeFormat):
54
    """The second working tree format.
55
56
    This format modified the hash cache from the format 1 hash cache.
57
    """
58
59
    upgrade_recommended = True
60
61
    requires_normalized_unicode_filenames = True
62
63
    case_sensitive_filename = "Branch-FoRMaT"
64
65
    missing_parent_conflicts = False
66
5993.3.1 by Jelmer Vernooij
Add WorkingTreeFormat.supports_versioned_directories attribute.
67
    supports_versioned_directories = True
68
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
69
    def get_format_description(self):
70
        """See WorkingTreeFormat.get_format_description()."""
71
        return "Working tree format 2"
72
73
    def _stub_initialize_on_transport(self, transport, file_mode):
74
        """Workaround: create control files for a remote working tree.
75
76
        This ensures that it can later be updated and dealt with locally,
77
        since BzrDirFormat6 and BzrDirFormat5 cannot represent dirs with
78
        no working tree.  (See bug #43064).
79
        """
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
80
        sio = BytesIO()
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
81
        inv = inventory.Inventory()
82
        xml5.serializer_v5.write_inventory(inv, sio, working=True)
83
        sio.seek(0)
84
        transport.put_file('inventory', sio, file_mode)
85
        transport.put_bytes('pending-merges', '', file_mode)
86
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
87
    def initialize(self, a_controldir, revision_id=None, from_branch=None,
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
88
                   accelerator_tree=None, hardlink=False):
89
        """See WorkingTreeFormat.initialize()."""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
90
        if not isinstance(a_controldir.transport, LocalTransport):
91
            raise errors.NotLocalUrl(a_controldir.transport.base)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
92
        if from_branch is not None:
93
            branch = from_branch
94
        else:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
95
            branch = a_controldir.open_branch()
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
96
        if revision_id is None:
97
            revision_id = _mod_revision.ensure_null(branch.last_revision())
98
        branch.lock_write()
99
        try:
100
            branch.generate_revision_history(revision_id)
101
        finally:
102
            branch.unlock()
103
        inv = inventory.Inventory()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
104
        wt = WorkingTree2(a_controldir.root_transport.local_abspath('.'),
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
105
                         branch,
106
                         inv,
107
                         _internal=True,
108
                         _format=self,
6681.2.4 by Jelmer Vernooij
More renames.
109
                         _controldir=a_controldir,
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
110
                         _control_files=branch.control_files)
111
        basis_tree = branch.repository.revision_tree(revision_id)
6405.2.6 by Jelmer Vernooij
Lots of test fixes.
112
        if basis_tree.get_root_id() is not None:
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
113
            wt.set_root_id(basis_tree.get_root_id())
114
        # set the parent list and cache the basis tree.
115
        if _mod_revision.is_null(revision_id):
116
            parent_trees = []
117
        else:
118
            parent_trees = [(revision_id, basis_tree)]
119
        wt.set_parent_trees(parent_trees)
120
        transform.build_tree(basis_tree, wt)
6435.1.1 by Jelmer Vernooij
Add post_build_tree hook.
121
        for hook in MutableTree.hooks['post_build_tree']:
122
            hook(wt)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
123
        return wt
124
125
    def __init__(self):
126
        super(WorkingTreeFormat2, self).__init__()
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
127
        from breezy.plugins.weave_fmt.bzrdir import BzrDirFormat6
6746.2.1 by Jelmer Vernooij
Rename matchingbzrdir to matchingcontroldir.
128
        self._matchingcontroldir = BzrDirFormat6()
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
129
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
130
    def open(self, a_controldir, _found=False):
131
        """Return the WorkingTree object for a_controldir
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
132
133
        _found is a private parameter, do not use it. It is used to indicate
134
               if format probing has already been done.
135
        """
136
        if not _found:
137
            # we are being called directly and must probe.
138
            raise NotImplementedError
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
139
        if not isinstance(a_controldir.transport, LocalTransport):
140
            raise errors.NotLocalUrl(a_controldir.transport.base)
141
        wt = WorkingTree2(a_controldir.root_transport.local_abspath('.'),
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
142
                           _internal=True,
143
                           _format=self,
6681.2.4 by Jelmer Vernooij
More renames.
144
                           _controldir=a_controldir,
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
145
                           _control_files=a_controldir.open_branch().control_files)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
146
        return wt
147
148
6110.4.4 by Jelmer Vernooij
Add common base class.
149
class WorkingTree2(PreDirStateWorkingTree):
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
150
    """This is the Format 2 working tree.
151
152
    This was the first weave based working tree.
153
     - uses os locks for locking.
154
     - uses the branch last-revision.
155
    """
156
6110.4.2 by Jelmer Vernooij
Move hashcache use to bzrlib.workingtree_3 and bzrlib.plugins.weave_fmt.workingtree.
157
    def __init__(self, basedir, *args, **kwargs):
158
        super(WorkingTree2, self).__init__(basedir, *args, **kwargs)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
159
        # WorkingTree2 has more of a constraint that self._inventory must
160
        # exist. Because this is an older format, we don't mind the overhead
161
        # caused by the extra computation here.
162
163
        # Newer WorkingTree's should only have self._inventory set when they
164
        # have a read lock.
165
        if self._inventory is None:
166
            self.read_working_inventory()
167
168
    def _get_check_refs(self):
169
        """Return the references needed to perform a check of this tree."""
170
        return [('trees', self.last_revision())]
171
6110.4.2 by Jelmer Vernooij
Move hashcache use to bzrlib.workingtree_3 and bzrlib.plugins.weave_fmt.workingtree.
172
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
173
    def lock_tree_write(self):
174
        """See WorkingTree.lock_tree_write().
175
176
        In Format2 WorkingTrees we have a single lock for the branch and tree
177
        so lock_tree_write() degrades to lock_write().
178
179
        :return: An object with an unlock method which will release the lock
180
            obtained.
181
        """
182
        self.branch.lock_write()
183
        try:
6754.8.9 by Jelmer Vernooij
Fix more tests.
184
            token = self._control_files.lock_write()
185
            return lock.LogicalLockResult(self.unlock, token)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
186
        except:
187
            self.branch.unlock()
188
            raise
189
190
    def unlock(self):
191
        # we share control files:
192
        if self._control_files._lock_count == 3:
5900.1.2 by Jelmer Vernooij
Fix weave plugin.
193
            # do non-implementation specific cleanup
194
            self._cleanup()
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
195
            # _inventory_is_modified is always False during a read lock.
196
            if self._inventory_is_modified:
197
                self.flush()
198
            self._write_hashcache_if_dirty()
199
200
        # reverse order of locking.
201
        try:
202
            return self._control_files.unlock()
203
        finally:
204
            self.branch.unlock()
205
206
    def _iter_conflicts(self):
207
        conflicted = set()
208
        for info in self.list_files():
209
            path = info[0]
210
            stem = get_conflicted_stem(path)
211
            if stem is None:
212
                continue
213
            if stem not in conflicted:
214
                conflicted.add(stem)
215
                yield stem
216
217
    @needs_read_lock
218
    def conflicts(self):
219
        conflicts = _mod_conflicts.ConflictList()
220
        for conflicted in self._iter_conflicts():
221
            text = True
222
            try:
223
                if osutils.file_kind(self.abspath(conflicted)) != "file":
224
                    text = False
225
            except errors.NoSuchFile:
226
                text = False
227
            if text is True:
228
                for suffix in ('.THIS', '.OTHER'):
229
                    try:
230
                        kind = osutils.file_kind(self.abspath(conflicted+suffix))
231
                        if kind != "file":
232
                            text = False
233
                    except errors.NoSuchFile:
234
                        text = False
235
                    if text == False:
236
                        break
237
            ctype = {True: 'text conflict', False: 'contents conflict'}[text]
238
            conflicts.append(_mod_conflicts.Conflict.factory(ctype,
239
                             path=conflicted,
240
                             file_id=self.path2id(conflicted)))
241
        return conflicts
242
5816.5.5 by Jelmer Vernooij
Fix conflict handling for wt2.
243
    def set_conflicts(self, arg):
244
        raise errors.UnsupportedOperation(self.set_conflicts, self)
5697.1.1 by Jelmer Vernooij
Move WorkingTreeFormat2 to a separate file.
245
5816.5.5 by Jelmer Vernooij
Fix conflict handling for wt2.
246
    def add_conflicts(self, arg):
247
        raise errors.UnsupportedOperation(self.add_conflicts, self)