/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2006-2011 Canonical Ltd
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
16
17
"""MutableTree object.
18
19
See MutableTree for more details.
20
"""
21
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
22
from __future__ import absolute_import
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
23
5870.1.16 by Jelmer Vernooij
Keep track of inv_path/ie .
24
import operator
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
25
import os
6672.2.4 by Jelmer Vernooij
Fix imports.
26
from . import (
4721.3.1 by Vincent Ladeuil
Cleanup imports.
27
    errors,
3335.1.2 by Jelmer Vernooij
Add mutabletree hooks class and start_commit hook.
28
    hooks,
4721.3.1 by Vincent Ladeuil
Cleanup imports.
29
    osutils,
30
    trace,
31
    tree,
2568.2.6 by Robert Collins
Review feedback.
32
    )
6670.4.1 by Jelmer Vernooij
Update imports.
33
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
34
from .decorators import needs_read_lock, needs_write_lock
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
35
from .sixish import (
36
    viewvalues,
37
    )
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
38
39
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
40
def needs_tree_write_lock(unbound):
41
    """Decorate unbound to take out and release a tree_write lock."""
42
    def tree_write_locked(self, *args, **kwargs):
43
        self.lock_tree_write()
44
        try:
45
            return unbound(self, *args, **kwargs)
46
        finally:
47
            self.unlock()
48
    tree_write_locked.__doc__ = unbound.__doc__
49
    tree_write_locked.__name__ = unbound.__name__
50
    return tree_write_locked
51
52
5777.4.1 by Jelmer Vernooij
Split inventory-specific code out of MutableTree into MutableInventoryTree.
53
class MutableTree(tree.Tree):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
54
    """A MutableTree is a specialisation of Tree which is able to be mutated.
55
56
    Generally speaking these mutations are only possible within a lock_write
57
    context, and will revert if the lock is broken abnormally - but this cannot
58
    be guaranteed - depending on the exact implementation of the mutable state.
59
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
60
    The most common form of Mutable Tree is WorkingTree, see breezy.workingtree.
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
61
    For tests we also have MemoryTree which is a MutableTree whose contents are
62
    entirely in memory.
63
64
    For now, we are not treating MutableTree as an interface to provide
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
65
    conformance tests for - rather we are testing MemoryTree specifically, and
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
66
    interface testing implementations of WorkingTree.
67
6207.3.3 by jelmer at samba
Fix tests and the like.
68
    A mutable tree always has an associated Branch and ControlDir object - the
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
69
    branch and bzrdir attributes.
70
    """
3794.5.19 by Mark Hammond
MutableTree gets case_sensitive attribute and smart_add takes advantage of it to enforce sane CICP-filesystem semantics
71
    def __init__(self, *args, **kw):
72
        super(MutableTree, self).__init__(*args, **kw)
73
        # Is this tree on a case-insensitive or case-preserving file-system?
74
        # Sub-classes may initialize to False if they detect they are being
75
        # used on media which doesn't differentiate the case of names.
76
        self.case_sensitive = True
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
77
5699.2.1 by Jelmer Vernooij
Move is_control_filename() from Tree to MutableTree.
78
    def is_control_filename(self, filename):
79
        """True if filename is the name of a control file in this tree.
80
81
        :param filename: A filename within the tree. This is a relative path
5891.1.2 by Andrew Bennetts
Fix a bunch of docstring formatting nits, making pydoctor a bit happier.
82
            from the root of this tree.
5699.2.1 by Jelmer Vernooij
Move is_control_filename() from Tree to MutableTree.
83
84
        This is true IF and ONLY IF the filename is part of the meta data
85
        that bzr controls in this tree. I.E. a random .bzr directory placed
86
        on disk will not be a control file for this tree.
87
        """
88
        raise NotImplementedError(self.is_control_filename)
89
2255.7.66 by Robert Collins
MutableTree.add only needs a tree write lock.
90
    @needs_tree_write_lock
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
91
    def add(self, files, ids=None, kinds=None):
92
        """Add paths to the set of versioned paths.
93
94
        Note that the command line normally calls smart_add instead,
95
        which can automatically recurse.
96
97
        This adds the files to the inventory, so that they will be
98
        recorded by the next commit.
99
100
        :param files: List of paths to add, relative to the base of the tree.
101
        :param ids: If set, use these instead of automatically generated ids.
102
            Must be the same length as the list of files, but may
103
            contain None for ids that are to be autogenerated.
104
        :param kinds: Optional parameter to specify the kinds to be used for
105
            each file.
106
107
        TODO: Perhaps callback with the ids and paths as they're added.
108
        """
109
        if isinstance(files, basestring):
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
110
            # XXX: Passing a single string is inconsistent and should be
111
            # deprecated.
112
            if not (ids is None or isinstance(ids, basestring)):
113
                raise AssertionError()
114
            if not (kinds is None or isinstance(kinds, basestring)):
115
                raise AssertionError()
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
116
            files = [files]
117
            if ids is not None:
118
                ids = [ids]
119
            if kinds is not None:
120
                kinds = [kinds]
121
2323.4.2 by Robert Collins
Fix the behaviour of dirstate optimised iter_changes recursing its disk iterator into subtrees inappropriately.
122
        files = [path.strip('/') for path in files]
123
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
124
        if ids is None:
125
            ids = [None] * len(files)
126
        else:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
127
            if not (len(ids) == len(files)):
128
                raise AssertionError()
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
129
        if kinds is None:
130
            kinds = [None] * len(files)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
131
        elif not len(kinds) == len(files):
132
            raise AssertionError()
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
133
        for f in files:
134
            # generic constraint checks:
135
            if self.is_control_filename(f):
136
                raise errors.ForbiddenControlFileError(filename=f)
4721.3.1 by Vincent Ladeuil
Cleanup imports.
137
            fp = osutils.splitpath(f)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
138
        # fill out file kinds for all files [not needed when we stop
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
139
        # caring about the instantaneous file kind within a uncommmitted tree
140
        #
141
        self._gather_kinds(files, kinds)
142
        self._add(files, ids, kinds)
143
2255.6.8 by Aaron Bentley
Merge refactoring of add_reference
144
    def add_reference(self, sub_tree):
145
        """Add a TreeReference to the tree, pointing at sub_tree"""
146
        raise errors.UnsupportedOperation(self.add_reference, self)
147
148
    def _add_reference(self, sub_tree):
149
        """Standard add_reference implementation, for use by subclasses"""
150
        try:
151
            sub_tree_path = self.relpath(sub_tree.basedir)
152
        except errors.PathNotChild:
153
            raise errors.BadReferenceTarget(self, sub_tree,
154
                                            'Target not inside tree.')
155
        sub_tree_id = sub_tree.get_root_id()
156
        if sub_tree_id == self.get_root_id():
157
            raise errors.BadReferenceTarget(self, sub_tree,
158
                                     'Trees have the same root id.')
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
159
        if self.has_id(sub_tree_id):
2255.6.8 by Aaron Bentley
Merge refactoring of add_reference
160
            raise errors.BadReferenceTarget(self, sub_tree,
161
                                            'Root id already present in tree')
162
        self._add([sub_tree_path], [sub_tree_id], ['tree-reference'])
163
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
164
    def _add(self, files, ids, kinds):
2255.6.8 by Aaron Bentley
Merge refactoring of add_reference
165
        """Helper function for add - updates the inventory.
166
167
        :param files: sequence of pathnames, relative to the tree root
168
        :param ids: sequence of suggested ids for the files (may be None)
169
        :param kinds: sequence of  inventory kinds of the files (i.e. may
170
            contain "tree-reference")
171
        """
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
172
        raise NotImplementedError(self._add)
173
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
174
    def apply_inventory_delta(self, changes):
175
        """Apply changes to the inventory as an atomic operation.
176
2865.1.1 by Robert Collins
Create new mutable tree method update_to_one_parent_via_delta for eventual use by commit.
177
        :param changes: An inventory delta to apply to the working tree's
178
            inventory.
179
        :return None:
180
        :seealso Inventory.apply_delta: For details on the changes parameter.
2376.2.9 by Aaron Bentley
Cleanup from review
181
        """
5777.4.1 by Jelmer Vernooij
Split inventory-specific code out of MutableTree into MutableInventoryTree.
182
        raise NotImplementedError(self.apply_inventory_delta)
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
183
184
    @needs_write_lock
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
185
    def commit(self, message=None, revprops=None, *args, **kwargs):
1986.1.3 by Robert Collins
Merge bzr.dev.
186
        # avoid circular imports
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
187
        from breezy import commit
3565.6.9 by Marius Kruger
Jump through hoops not to open multiple connections when committing to a bound branch.
188
        possible_master_transports=[]
5162.4.1 by Aaron Bentley
TreeTransform supports normal commit parameters and includes branch nick.
189
        revprops = commit.Commit.update_revprops(
190
                revprops,
191
                self.branch,
192
                kwargs.pop('authors', None),
3815.3.4 by Marius Kruger
When doing a `commit --local`, don't try to connect to the master branch.
193
                kwargs.get('local', False),
3565.6.9 by Marius Kruger
Jump through hoops not to open multiple connections when committing to a bound branch.
194
                possible_master_transports)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
195
        # args for wt.commit start at message from the Commit.commit method,
2367.2.1 by Robert Collins
Remove bzrlib 0.8 compatability where it was making the code unclear or messy. (Robert Collins)
196
        args = (message, ) + args
3335.1.2 by Jelmer Vernooij
Add mutabletree hooks class and start_commit hook.
197
        for hook in MutableTree.hooks['start_commit']:
198
            hook(self)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
199
        committed_id = commit.Commit().commit(working_tree=self,
3565.6.9 by Marius Kruger
Jump through hoops not to open multiple connections when committing to a bound branch.
200
            revprops=revprops,
201
            possible_master_transports=possible_master_transports,
202
            *args, **kwargs)
4634.33.3 by Ian Clatworthy
review feedback from Robert: rename finish_commit to post_commit
203
        post_hook_params = PostCommitHookParams(self)
204
        for hook in MutableTree.hooks['post_commit']:
205
            hook(post_hook_params)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
206
        return committed_id
207
208
    def _gather_kinds(self, files, kinds):
209
        """Helper function for add - sets the entries of kinds."""
210
        raise NotImplementedError(self._gather_kinds)
211
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
212
    @needs_read_lock
4721.3.2 by Vincent Ladeuil
Simplify mutable_tree.has_changes() and update call sites.
213
    def has_changes(self, _from_tree=None):
214
        """Quickly check that the tree contains at least one commitable change.
215
216
        :param _from_tree: tree to compare against to find changes (default to
217
            the basis tree and is intended to be used by tests).
4503.1.3 by Vincent Ladeuil
Take review comments into account.
218
219
        :return: True if a change is found. False otherwise
220
        """
4721.3.2 by Vincent Ladeuil
Simplify mutable_tree.has_changes() and update call sites.
221
        # Check pending merges
222
        if len(self.get_parent_ids()) > 1:
223
            return True
224
        if _from_tree is None:
225
            _from_tree = self.basis_tree()
226
        changes = self.iter_changes(_from_tree)
4503.1.3 by Vincent Ladeuil
Take review comments into account.
227
        try:
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
228
            change = next(changes)
4503.1.3 by Vincent Ladeuil
Take review comments into account.
229
            # Exclude root (talk about black magic... --vila 20090629)
230
            if change[4] == (None, None):
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
231
                change = next(changes)
4503.1.3 by Vincent Ladeuil
Take review comments into account.
232
            return True
233
        except StopIteration:
234
            # No changes
235
            return False
236
237
    @needs_read_lock
5171.2.2 by Vincent Ladeuil
Explain that the uncommitted changes are not processed when
238
    def check_changed_or_out_of_date(self, strict, opt_name,
239
                                     more_error, more_warning):
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
240
        """Check the tree for uncommitted changes and branch synchronization.
241
242
        If strict is None and not set in the config files, a warning is issued.
243
        If strict is True, an error is raised.
244
        If strict is False, no checks are done and no warning is issued.
245
246
        :param strict: True, False or None, searched in branch config if None.
247
248
        :param opt_name: strict option name to search in config file.
249
5171.2.2 by Vincent Ladeuil
Explain that the uncommitted changes are not processed when
250
        :param more_error: Details about how to avoid the check.
251
252
        :param more_warning: Details about what is happening.
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
253
        """
254
        if strict is None:
6155.2.1 by Vincent Ladeuil
Migrate dpush_strict, push_strict and send_strict options to the stack based config design, introducing get_config_stack for branches.
255
            strict = self.branch.get_config_stack().get(opt_name)
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
256
        if strict is not False:
5171.2.1 by Vincent Ladeuil
Fix the warning message for unclean trees.
257
            err_class = None
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
258
            if (self.has_changes()):
5171.2.1 by Vincent Ladeuil
Fix the warning message for unclean trees.
259
                err_class = errors.UncommittedChanges
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
260
            elif self.last_revision() != self.branch.last_revision():
261
                # The tree has lost sync with its branch, there is little
262
                # chance that the user is aware of it but he can still force
263
                # the action with --no-strict
5171.2.1 by Vincent Ladeuil
Fix the warning message for unclean trees.
264
                err_class = errors.OutOfDateTree
265
            if err_class is not None:
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
266
                if strict is None:
5171.2.2 by Vincent Ladeuil
Explain that the uncommitted changes are not processed when
267
                    err = err_class(self, more=more_warning)
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
268
                    # We don't want to interrupt the user if he expressed no
269
                    # preference about strict.
5158.1.1 by Vincent Ladeuil
Use the proper argument for the warning.
270
                    trace.warning('%s', err._format())
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
271
                else:
5171.2.2 by Vincent Ladeuil
Explain that the uncommitted changes are not processed when
272
                    err = err_class(self, more=more_error)
5147.2.2 by Vincent Ladeuil
Fix bug #519319 by defaulting to a warning for dirty trees.
273
                    raise err
274
275
    @needs_read_lock
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
276
    def last_revision(self):
1986.1.7 by Robert Collins
Merge bzr.dev.
277
        """Return the revision id of the last commit performed in this tree.
278
279
        In early tree formats the result of last_revision is the same as the
280
        branch last_revision, but that is no longer the case for modern tree
281
        formats.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
282
1986.1.7 by Robert Collins
Merge bzr.dev.
283
        last_revision returns the left most parent id, or None if there are no
284
        parents.
285
286
        last_revision was deprecated as of 0.11. Please use get_parent_ids
287
        instead.
288
        """
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
289
        raise NotImplementedError(self.last_revision)
290
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
291
    def lock_tree_write(self):
292
        """Lock the working tree for write, and the branch for read.
293
294
        This is useful for operations which only need to mutate the working
295
        tree. Taking out branch write locks is a relatively expensive process
296
        and may fail if the branch is on read only media. So branch write locks
297
        should only be taken out when we are modifying branch data - such as in
298
        operations like commit, pull, uncommit and update.
299
        """
300
        raise NotImplementedError(self.lock_tree_write)
301
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
302
    def lock_write(self):
303
        """Lock the tree and its branch. This allows mutating calls to be made.
304
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
305
        Some mutating methods will take out implicit write locks, but in
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
306
        general you should always obtain a write lock before calling mutating
307
        methods on a tree.
308
        """
309
        raise NotImplementedError(self.lock_write)
310
311
    @needs_write_lock
312
    def mkdir(self, path, file_id=None):
313
        """Create a directory in the tree. if file_id is None, one is assigned.
314
315
        :param path: A unicode file path.
316
        :param file_id: An optional file-id.
317
        :return: the file id of the new directory.
318
        """
319
        raise NotImplementedError(self.mkdir)
320
6621.20.1 by Martin
Run 2to3 tuple_params fixer and tidy up.
321
    def _observed_sha1(self, file_id, path, sha_and_stat):
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
322
        """Tell the tree we have observed a paths sha1.
323
324
        The intent of this function is to allow trees that have a hashcache to
3709.3.2 by Robert Collins
Race-free stat-fingerprint updating during commit via a new method get_file_with_stat.
325
        update the hashcache during commit. If the observed file is too new
326
        (based on the stat_value) to be safely hash-cached the tree will ignore
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
327
        it.
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
328
329
        The default implementation does nothing.
330
331
        :param file_id: The file id
332
        :param path: The file path
6621.20.1 by Martin
Run 2to3 tuple_params fixer and tidy up.
333
        :param sha_and_stat: The sha 1 and stat result observed.
3709.3.1 by Robert Collins
First cut - make it work - at updating the tree stat cache during commit.
334
        :return: None
335
        """
336
3394.2.1 by Ian Clatworthy
add docstring for put_file_bytes_non_atomic
337
    @needs_write_lock
338
    def put_file_bytes_non_atomic(self, file_id, bytes):
339
        """Update the content of a file in the tree.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
340
3394.2.1 by Ian Clatworthy
add docstring for put_file_bytes_non_atomic
341
        Note that the file is written in-place rather than being
342
        written to a temporary location and renamed. As a consequence,
343
        readers can potentially see the file half-written.
344
345
        :param file_id: file-id of the file
346
        :param bytes: the new file contents
347
        """
348
        raise NotImplementedError(self.put_file_bytes_non_atomic)
349
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
350
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
351
        """Set the parents ids of the working tree.
352
353
        :param revision_ids: A list of revision_ids.
354
        """
355
        raise NotImplementedError(self.set_parent_ids)
356
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
357
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
358
        """Set the parents of the working tree.
359
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
360
        :param parents_list: A list of (revision_id, tree) tuples.
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
361
            If tree is None, then that element is treated as an unreachable
362
            parent tree - i.e. a ghost.
363
        """
364
        raise NotImplementedError(self.set_parent_trees)
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
365
5777.4.1 by Jelmer Vernooij
Split inventory-specific code out of MutableTree into MutableInventoryTree.
366
    def smart_add(self, file_list, recurse=True, action=None, save=True):
367
        """Version file_list, optionally recursing into directories.
368
369
        This is designed more towards DWIM for humans than API clarity.
370
        For the specific behaviour see the help for cmd_add().
371
372
        :param file_list: List of zero or more paths.  *NB: these are 
373
            interpreted relative to the process cwd, not relative to the 
374
            tree.*  (Add and most other tree methods use tree-relative
375
            paths.)
376
        :param action: A reporter to be called with the inventory, parent_ie,
377
            path and kind of the path being added. It may return a file_id if
378
            a specific one should be used.
379
        :param save: Save the inventory after completing the adds. If False
380
            this provides dry-run functionality by doing the add and not saving
381
            the inventory.
382
        :return: A tuple - files_added, ignored_files. files_added is the count
383
            of added files, and ignored_files is a dict mapping files that were
384
            ignored to the rule that caused them to be ignored.
385
        """
386
        raise NotImplementedError(self.smart_add)
387
5870.1.42 by Jelmer Vernooij
Review feedback from John.
388
5870.1.31 by Jelmer Vernooij
Move smart_add into a helper.
389
class MutableTreeHooks(hooks.Hooks):
390
    """A dictionary mapping a hook name to a list of callables for mutabletree
391
    hooks.
392
    """
393
394
    def __init__(self):
395
        """Create the default hooks.
396
397
        """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
398
        hooks.Hooks.__init__(self, "breezy.mutabletree", "MutableTree.hooks")
5870.1.31 by Jelmer Vernooij
Move smart_add into a helper.
399
        self.add_hook('start_commit',
400
            "Called before a commit is performed on a tree. The start commit "
401
            "hook is able to change the tree before the commit takes place. "
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
402
            "start_commit is called with the breezy.mutabletree.MutableTree "
5870.1.31 by Jelmer Vernooij
Move smart_add into a helper.
403
            "that the commit is being performed on.", (1, 4))
404
        self.add_hook('post_commit',
405
            "Called after a commit is performed on a tree. The hook is "
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
406
            "called with a breezy.mutabletree.PostCommitHookParams object. "
5870.1.31 by Jelmer Vernooij
Move smart_add into a helper.
407
            "The mutable tree the commit was performed on is available via "
408
            "the mutable_tree attribute of that object.", (2, 0))
6434.2.1 by Jelmer Vernooij
Add pre_transform/post_transform hooks.
409
        self.add_hook('pre_transform',
410
            "Called before a tree transform on this tree. The hook is called "
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
411
            "with the tree that is being transformed and the transform.",
412
            (2, 5))
6435.1.1 by Jelmer Vernooij
Add post_build_tree hook.
413
        self.add_hook('post_build_tree',
414
            "Called after a completely new tree is built. The hook is "
415
            "called with the tree as its only argument.", (2, 5))
6434.2.1 by Jelmer Vernooij
Add pre_transform/post_transform hooks.
416
        self.add_hook('post_transform',
417
            "Called after a tree transform has been performed on a tree. "
6434.2.2 by Jelmer Vernooij
Add hooks for tree transforms.
418
            "The hook is called with the tree that is being transformed and "
419
            "the transform.",
6434.2.1 by Jelmer Vernooij
Add pre_transform/post_transform hooks.
420
            (2, 5))
5870.1.31 by Jelmer Vernooij
Move smart_add into a helper.
421
422
# install the default hooks into the MutableTree class.
423
MutableTree.hooks = MutableTreeHooks()
424
425
426
class PostCommitHookParams(object):
427
    """Parameters for the post_commit hook.
428
429
    To access the parameters, use the following attributes:
430
431
    * mutable_tree - the MutableTree object
432
    """
433
434
    def __init__(self, mutable_tree):
435
        """Create the parameters for the post_commit hook."""
436
        self.mutable_tree = mutable_tree