/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 bzrlib/mutabletree.py

Various changes to allow non-workingtree specific tests to run entirely
from MemoryTransports:
 * Create MemoryTree and pull up common code for it from WorkingTree to
   a new common base class MutableTree.
 * Add MutableTree.mkdir().
 * Add MutableTree.put_file_bytes_nonatomic().
 * New test helper make_branch_and_memory_tree().
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""MutableTree object.
 
18
 
 
19
See MutableTree for more details.
 
20
"""
 
21
 
 
22
 
 
23
from bzrlib import tree
 
24
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
25
from bzrlib.osutils import splitpath
 
26
from bzrlib.symbol_versioning import DEPRECATED_PARAMETER
 
27
 
 
28
 
 
29
class MutableTree(tree.Tree):
 
30
    """A MutableTree is a specialisation of Tree which is able to be mutated.
 
31
 
 
32
    Generally speaking these mutations are only possible within a lock_write
 
33
    context, and will revert if the lock is broken abnormally - but this cannot
 
34
    be guaranteed - depending on the exact implementation of the mutable state.
 
35
 
 
36
    The most common form of Mutable Tree is WorkingTree, see bzrlib.workingtree.
 
37
    For tests we also have MemoryTree which is a MutableTree whose contents are
 
38
    entirely in memory.
 
39
 
 
40
    For now, we are not treating MutableTree as an interface to provide
 
41
    conformance tests for - rather we are testing MemoryTree specifically, and 
 
42
    interface testing implementations of WorkingTree.
 
43
 
 
44
    A mutable tree always has an associated Branch and BzrDir object - the
 
45
    branch and bzrdir attributes.
 
46
    """
 
47
 
 
48
    @needs_write_lock
 
49
    def add(self, files, ids=None, kinds=None):
 
50
        """Add paths to the set of versioned paths.
 
51
 
 
52
        Note that the command line normally calls smart_add instead,
 
53
        which can automatically recurse.
 
54
 
 
55
        This adds the files to the inventory, so that they will be
 
56
        recorded by the next commit.
 
57
 
 
58
        :param files: List of paths to add, relative to the base of the tree.
 
59
        :param ids: If set, use these instead of automatically generated ids.
 
60
            Must be the same length as the list of files, but may
 
61
            contain None for ids that are to be autogenerated.
 
62
        :param kinds: Optional parameter to specify the kinds to be used for
 
63
            each file.
 
64
 
 
65
        TODO: Perhaps callback with the ids and paths as they're added.
 
66
        """
 
67
        if isinstance(files, basestring):
 
68
            assert(ids is None or isinstance(ids, basestring))
 
69
            assert(kinds is None or isinstance(kinds, basestring))
 
70
            files = [files]
 
71
            if ids is not None:
 
72
                ids = [ids]
 
73
            if kinds is not None:
 
74
                kinds = [kinds]
 
75
 
 
76
        if ids is None:
 
77
            ids = [None] * len(files)
 
78
        else:
 
79
            assert(len(ids) == len(files))
 
80
 
 
81
        if kinds is None:
 
82
            kinds = [None] * len(files)
 
83
        else:
 
84
            assert(len(kinds) == len(files))
 
85
        for f in files:
 
86
            # generic constraint checks:
 
87
            if self.is_control_filename(f):
 
88
                raise errors.ForbiddenControlFileError(filename=f)
 
89
            fp = splitpath(f)
 
90
            if len(fp) == 0:
 
91
                raise BzrError("cannot add top-level %r" % f)
 
92
        # fill out file kinds for all files [not needed when we stop 
 
93
        # caring about the instantaneous file kind within a uncommmitted tree
 
94
        #
 
95
        self._gather_kinds(files, kinds)
 
96
        self._add(files, ids, kinds)
 
97
 
 
98
    def _add(self, files, ids, kinds):
 
99
        """Helper function for add - updates the inventory."""
 
100
        raise NotImplementedError(self._add)
 
101
 
 
102
    @needs_write_lock
 
103
    def commit(self, message=None, revprops=None, *args, **kwargs):
 
104
        """Perform a commit from the tree into the trees branch."""
 
105
        # avoid circular imports:
 
106
        from bzrlib import commit
 
107
        if revprops is None:
 
108
            revprops = {}
 
109
        if not 'branch-nick' in revprops:
 
110
            revprops['branch-nick'] = self.branch.nick
 
111
        # args for wt.commit start at message from the Commit.commit method,
 
112
        # but with branch a kwarg now, passing in args as is results in the
 
113
        #message being used for the branch
 
114
        args = (DEPRECATED_PARAMETER, message, ) + args
 
115
        committed_id = commit.Commit().commit(working_tree=self,
 
116
            revprops=revprops,
 
117
            *args, **kwargs)
 
118
        #self._set_inventory(self.read_working_inventory())
 
119
        return committed_id
 
120
 
 
121
    def _gather_kinds(self, files, kinds):
 
122
        """Helper function for add - sets the entries of kinds."""
 
123
        raise NotImplementedError(self._gather_kinds)
 
124
 
 
125
    def lock_write(self):
 
126
        """Lock the tree and its branch. This allows mutating calls to be made.
 
127
 
 
128
        Some mutating methods will take out implicit write locks, but in 
 
129
        general you should always obtain a write lock before calling mutating
 
130
        methods on a tree.
 
131
        """
 
132
        raise NotImplementedError(self.lock_write)
 
133
 
 
134
    @needs_write_lock
 
135
    def mkdir(self, path, file_id=None):
 
136
        """Create a directory in the tree. if file_id is None, one is assigned.
 
137
 
 
138
        :param path: A unicode file path.
 
139
        :param file_id: An optional file-id.
 
140
        :return: the file id of the new directory.
 
141
        """
 
142
        raise NotImplementedError(self.mkdir)
 
143
 
 
144
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
145
        """Set the parents of the working tree.
 
146
 
 
147
        :param parents_list: A list of (revision_id, tree) tuples. 
 
148
            If tree is None, then that element is treated as an unreachable
 
149
            parent tree - i.e. a ghost.
 
150
        """
 
151
        raise NotImplementedError(self.set_parent_trees)