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