/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
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
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
16
7479.2.1 by Jelmer Vernooij
Drop python2 support.
17
from io import StringIO
18
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
from breezy import (
2225.1.1 by Aaron Bentley
Added revert change display, with tests
20
    osutils,
6469.1.2 by Parth Malwankar
st now works on windows
21
    trace,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
22
    )
7490.120.3 by Jelmer Vernooij
Split out InventoryTreeChange from TreeChange.
23
from .bzr.inventorytree import InventoryTreeChange
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
24
1732.1.29 by John Arbash Meinel
Update documentation and TODO for compare_trees
25
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
26
class TreeDelta(object):
27
    """Describes changes from one tree to another.
28
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
29
    Contains seven lists with TreeChange objects.
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
30
31
    added
32
    removed
33
    renamed
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
34
    copied
3874.3.3 by Vincent Ladeuil
Finish TreeDelta.show() refactoring.
35
    kind_changed
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
36
    modified
37
    unchanged
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
38
    unversioned
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
39
40
    Each id is listed only once.
41
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
42
    Files that are both modified and renamed or copied are listed only in
43
    renamed or copied, with the text_modified flag true. The text_modified
4775.1.1 by Martin Pool
Remove several 'the the' typos
44
    applies either to the content of the file or the target of the
1092.2.6 by Robert Collins
symlink support updated to work
45
    symbolic link, depending of the kind of file.
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
46
47
    Files are only considered renamed if their name has changed or
48
    their parent directory has changed.  Renaming a directory
49
    does not count as renaming all its contents.
50
51
    The lists are normally sorted when the delta is created.
52
    """
7143.15.2 by Jelmer Vernooij
Run autopep8.
53
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
54
    def __init__(self):
55
        self.added = []
56
        self.removed = []
57
        self.renamed = []
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
58
        self.copied = []
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
59
        self.kind_changed = []
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
60
        self.modified = []
61
        self.unchanged = []
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
62
        self.unversioned = []
5504.5.1 by Rory Yorke
Show missing files in bzr status (bug 134168).
63
        self.missing = []
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
64
65
    def __eq__(self, other):
66
        if not isinstance(other, TreeDelta):
67
            return False
68
        return self.added == other.added \
7143.15.2 by Jelmer Vernooij
Run autopep8.
69
            and self.removed == other.removed \
70
            and self.renamed == other.renamed \
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
71
            and self.copied == other.copied \
7143.15.2 by Jelmer Vernooij
Run autopep8.
72
            and self.modified == other.modified \
73
            and self.unchanged == other.unchanged \
74
            and self.kind_changed == other.kind_changed \
75
            and self.unversioned == other.unversioned
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
76
77
    def __ne__(self, other):
78
        return not (self == other)
79
80
    def __repr__(self):
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
81
        return "TreeDelta(added=%r, removed=%r, renamed=%r," \
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
82
            " copied=%r, kind_changed=%r, modified=%r, unchanged=%r," \
83
            " unversioned=%r)" % (
7358.17.5 by Jelmer Vernooij
Fix more tests.
84
                self.added, self.removed, self.renamed, self.copied,
85
                self.kind_changed, self.modified, self.unchanged,
86
                self.unversioned)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
87
88
    def has_changed(self):
1189 by Martin Pool
- BROKEN: partial support for commit into weave
89
        return bool(self.modified
90
                    or self.added
91
                    or self.removed
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
92
                    or self.renamed
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
93
                    or self.copied
1551.10.6 by Aaron Bentley
Support kind changes in tree deltas
94
                    or self.kind_changed)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
95
2292.1.25 by Marius Kruger
* Add utility method delta.get_changes_as_text to get the output of .show()
96
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
5076.4.3 by Arnaud Jeansen
Port get_changes_as_text to the report callbacks
97
                            short_status=False):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
98
        output = StringIO()
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
99
        report_delta(output, self, short_status, show_ids, show_unchanged)
2292.1.25 by Marius Kruger
* Add utility method delta.get_changes_as_text to get the output of .show()
100
        return output.getvalue()
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
101
1731.1.33 by Aaron Bentley
Revert no-special-root changes
102
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
103
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files,
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
104
                   include_root, extra_trees=None,
2655.2.1 by Marius Kruger
InterTree.compare and delta._compare_trees did not pass its
105
                   require_versioned=False, want_unversioned=False):
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
106
    """Worker function that implements Tree.changes_from."""
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
107
    delta = TreeDelta()
1908.3.1 by Carl Friedrich Bolz
Clean up some mutter() calls.
108
    # mutter('start compare_trees')
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
109
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
110
    for change in new_tree.iter_changes(
111
            old_tree, want_unchanged, specific_files, extra_trees=extra_trees,
112
            require_versioned=require_versioned,
113
            want_unversioned=want_unversioned):
114
        if change.versioned == (False, False):
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
115
            delta.unversioned.append(change)
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
116
            continue
117
        if not include_root and (None, None) == change.parent_id:
118
            continue
119
        fully_present = tuple(
120
            (change.versioned[x] and change.kind[x] is not None)
121
            for x in range(2))
2012.1.5 by Aaron Bentley
Implement specific file id and dangling id handling
122
        if fully_present[0] != fully_present[1]:
123
            if fully_present[1] is True:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
124
                delta.added.append(change)
2012.1.5 by Aaron Bentley
Implement specific file id and dangling id handling
125
            else:
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
126
                if change.kind[0] == 'symlink' and not new_tree.supports_symlinks():
7122.6.9 by Jelmer Vernooij
Fix tests on python 2.
127
                    trace.warning(
128
                        'Ignoring "%s" as symlinks '
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
129
                        'are not supported on this filesystem.' % (change.path[0],))
6469.1.2 by Parth Malwankar
st now works on windows
130
                else:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
131
                    delta.removed.append(change)
2012.1.5 by Aaron Bentley
Implement specific file id and dangling id handling
132
        elif fully_present[0] is False:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
133
            delta.missing.append(change)
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
134
        elif change.name[0] != change.name[1] or change.parent_id[0] != change.parent_id[1]:
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
135
            # If the name changes, or the parent_id changes, we have a rename or copy
2012.1.4 by Aaron Bentley
Simplify add/remove code
136
            # (if we move a parent, that doesn't count as a rename for the
137
            # file)
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
138
            if change.copied:
139
                delta.copied.append(change)
140
            else:
141
                delta.renamed.append(change)
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
142
        elif change.kind[0] != change.kind[1]:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
143
            delta.kind_changed.append(change)
7322.1.6 by Jelmer Vernooij
Use the new attributes on TreeChange.
144
        elif change.changed_content or change.executable[0] != change.executable[1]:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
145
            delta.modified.append(change)
146
        else:
147
            delta.unchanged.append(change)
148
149
    def change_key(change):
150
        if change.path[0] is None:
151
            path = change.path[1]
152
        else:
153
            path = change.path[0]
154
        return (path, change.file_id)
155
156
    delta.removed.sort(key=change_key)
157
    delta.added.sort(key=change_key)
158
    delta.renamed.sort(key=change_key)
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
159
    delta.copied.sort(key=change_key)
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
160
    delta.missing.sort(key=change_key)
1732.1.29 by John Arbash Meinel
Update documentation and TODO for compare_trees
161
    # TODO: jam 20060529 These lists shouldn't need to be sorted
162
    #       since we added them in alphabetical order.
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
163
    delta.modified.sort(key=change_key)
164
    delta.unchanged.sort(key=change_key)
165
    delta.unversioned.sort(key=change_key)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
166
167
    return delta
2225.1.1 by Aaron Bentley
Added revert change display, with tests
168
169
1551.10.25 by Aaron Bentley
Make ChangeReporter private
170
class _ChangeReporter(object):
2225.1.1 by Aaron Bentley
Added revert change display, with tests
171
    """Report changes between two trees"""
172
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
173
    def __init__(self, output=None, suppress_root_add=True,
5945.2.1 by Martin von Gagern
Make kind markers optional.
174
                 output_file=None, unversioned_filter=None, view_info=None,
5945.2.2 by Martin von Gagern
Change from no_decorate to classify as name for the argument.
175
                 classify=True):
1551.10.7 by Aaron Bentley
Use new-style output for status
176
        """Constructor
177
178
        :param output: a function with the signature of trace.note, i.e.
179
            accepts a format and parameters.
180
        :param supress_root_add: If true, adding the root will be ignored
181
            (i.e. when a tree has just been initted)
1551.10.8 by Aaron Bentley
Make ChangeReporter interface nicer
182
        :param output_file: If supplied, a file-like object to write to.
183
            Only one of output and output_file may be supplied.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
184
        :param unversioned_filter: A filter function to be called on
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
185
            unversioned files. This should return True to ignore a path.
186
            By default, no filtering takes place.
3586.1.30 by Ian Clatworthy
add view support to change reporting
187
        :param view_info: A tuple of view_name,view_files if only
188
            items inside a view are to be reported on, or None for
189
            no view filtering.
5945.2.2 by Martin von Gagern
Change from no_decorate to classify as name for the argument.
190
        :param classify: Add special symbols to indicate file kind.
1551.10.7 by Aaron Bentley
Use new-style output for status
191
        """
1551.10.8 by Aaron Bentley
Make ChangeReporter interface nicer
192
        if output_file is not None:
193
            if output is not None:
194
                raise BzrError('Cannot specify both output and output_file')
7143.15.2 by Jelmer Vernooij
Run autopep8.
195
1551.10.8 by Aaron Bentley
Make ChangeReporter interface nicer
196
            def output(fmt, *args):
197
                output_file.write((fmt % args) + '\n')
2225.1.1 by Aaron Bentley
Added revert change display, with tests
198
        self.output = output
199
        if self.output is None:
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
200
            from . import trace
2225.1.1 by Aaron Bentley
Added revert change display, with tests
201
            self.output = trace.note
1551.10.7 by Aaron Bentley
Use new-style output for status
202
        self.suppress_root_add = suppress_root_add
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
203
        self.modified_map = {'kind changed': 'K',
204
                             'unchanged': ' ',
205
                             'created': 'N',
206
                             'modified': 'M',
5504.5.1 by Rory Yorke
Show missing files in bzr status (bug 134168).
207
                             'deleted': 'D',
208
                             'missing': '!',
209
                             }
7143.15.2 by Jelmer Vernooij
Run autopep8.
210
        self.versioned_map = {'added': '+',  # versioned target
211
                              'unchanged': ' ',  # versioned in both
212
                              'removed': '-',  # versioned in source
213
                              'unversioned': '?',  # versioned in neither
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
214
                              }
215
        self.unversioned_filter = unversioned_filter
5945.2.2 by Martin von Gagern
Change from no_decorate to classify as name for the argument.
216
        if classify:
217
            self.kind_marker = osutils.kind_marker
218
        else:
219
            self.kind_marker = lambda kind: ''
3586.1.30 by Ian Clatworthy
add view support to change reporting
220
        if view_info is None:
221
            self.view_name = None
222
            self.view_files = []
223
        else:
224
            self.view_name = view_info[0]
225
            self.view_files = view_info[1]
226
            self.output("Operating on whole tree but only reporting on "
227
                        "'%s' view." % (self.view_name,))
2225.1.1 by Aaron Bentley
Added revert change display, with tests
228
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
229
    def report(self, paths, versioned, renamed, copied, modified, exe_change,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
230
               kind):
231
        """Report one change to a file
2225.1.5 by Aaron Bentley
Clean up whitespace changes
232
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
233
        :param path: The old and new paths as generated by Tree.iter_changes.
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
234
        :param versioned: may be 'added', 'removed', 'unchanged', or
235
            'unversioned.
2225.1.1 by Aaron Bentley
Added revert change display, with tests
236
        :param renamed: may be True or False
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
237
        :param copied: may be True or False
2225.1.2 by Aaron Bentley
Ensure that changes are detected correctly
238
        :param modified: may be 'created', 'deleted', 'kind changed',
2225.1.1 by Aaron Bentley
Added revert change display, with tests
239
            'modified' or 'unchanged'.
240
        :param exe_change: True if the execute bit has changed
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
241
        :param kind: A pair of file kinds, as generated by Tree.iter_changes.
2225.1.1 by Aaron Bentley
Added revert change display, with tests
242
            None indicates no file present.
243
        """
6469.1.2 by Parth Malwankar
st now works on windows
244
        if trace.is_quiet():
3200.1.1 by James Westby
Make pull --quiet more quiet. Fixes #185907.
245
            return
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
246
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
1551.10.7 by Aaron Bentley
Use new-style output for status
247
            return
3586.1.30 by Ian Clatworthy
add view support to change reporting
248
        if self.view_files and not osutils.is_inside_any(self.view_files,
7143.15.2 by Jelmer Vernooij
Run autopep8.
249
                                                         paths[1]):
3586.1.30 by Ian Clatworthy
add view support to change reporting
250
            return
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
251
        if versioned == 'unversioned':
252
            # skip ignored unversioned files if needed.
253
            if self.unversioned_filter is not None:
254
                if self.unversioned_filter(paths[1]):
255
                    return
256
            # dont show a content change in the output.
257
            modified = 'unchanged'
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
258
        # we show both paths in the following situations:
259
        # the file versioning is unchanged AND
260
        # ( the path is different OR
261
        #   the kind is different)
262
        if (versioned == 'unchanged' and
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
263
                (renamed or copied or modified == 'kind changed')):
264
            if renamed or copied:
265
                # on a rename or copy, we show old and new
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
266
                old_path, path = paths
267
            else:
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
268
                # if it's not renamed or copied, we're showing both for kind
269
                # changes so only show the new path
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
270
                old_path, path = paths[1], paths[1]
271
            # if the file is not missing in the source, we show its kind
272
            # when we show two paths.
273
            if kind[0] is not None:
5945.2.1 by Martin von Gagern
Make kind markers optional.
274
                old_path += self.kind_marker(kind[0])
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
275
            old_path += " => "
2255.2.231 by Robert Collins
Add test showing reverts UI working as Aaron intended it to.
276
        elif versioned == 'removed':
277
            # not present in target
278
            old_path = ""
279
            path = paths[0]
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
280
        else:
281
            old_path = ""
282
            path = paths[1]
2225.1.1 by Aaron Bentley
Added revert change display, with tests
283
        if renamed:
284
            rename = "R"
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
285
        elif copied:
286
            rename = "C"
2225.1.1 by Aaron Bentley
Added revert change display, with tests
287
        else:
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
288
            rename = self.versioned_map[versioned]
289
        # we show the old kind on the new path when the content is deleted.
2225.1.1 by Aaron Bentley
Added revert change display, with tests
290
        if modified == 'deleted':
5945.2.1 by Martin von Gagern
Make kind markers optional.
291
            path += self.kind_marker(kind[0])
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
292
        # otherwise we always show the current kind when there is one
1551.10.11 by Aaron Bentley
Handle case where file-id only is added
293
        elif kind[1] is not None:
5945.2.1 by Martin von Gagern
Make kind markers optional.
294
            path += self.kind_marker(kind[1])
2225.1.1 by Aaron Bentley
Added revert change display, with tests
295
        if exe_change:
296
            exe = '*'
297
        else:
298
            exe = ' '
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
299
        self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
2225.1.1 by Aaron Bentley
Added revert change display, with tests
300
                    old_path, path)
301
7058.3.1 by Jelmer Vernooij
Some more sortin'.
302
2225.1.1 by Aaron Bentley
Added revert change display, with tests
303
def report_changes(change_iterator, reporter):
304
    """Report the changes from a change iterator.
305
306
    This is essentially a translation from low-level to medium-level changes.
307
    Further processing may be required to produce a human-readable output.
308
    Unfortunately, some tree-changing operations are very complex
309
    :change_iterator: an iterator or sequence of changes in the format
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
310
        generated by Tree.iter_changes
1551.10.25 by Aaron Bentley
Make ChangeReporter private
311
    :param reporter: The _ChangeReporter that will report the changes.
2225.1.1 by Aaron Bentley
Added revert change display, with tests
312
    """
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
313
    versioned_change_map = {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
314
        (True, True): 'unchanged',
315
        (True, False): 'removed',
316
        (False, True): 'added',
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
317
        (False, False): 'unversioned',
318
        }
7143.15.2 by Jelmer Vernooij
Run autopep8.
319
7058.3.2 by Jelmer Vernooij
More consistent output.
320
    def path_key(change):
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
321
        if change.path[0] is not None:
322
            path = change.path[0]
7058.3.2 by Jelmer Vernooij
More consistent output.
323
        else:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
324
            path = change.path[1]
7058.3.2 by Jelmer Vernooij
More consistent output.
325
        return osutils.splitpath(path)
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
326
    for change in sorted(change_iterator, key=path_key):
2225.1.1 by Aaron Bentley
Added revert change display, with tests
327
        exe_change = False
328
        # files are "renamed" if they are moved or if name changes, as long
329
        # as it had a value
7490.134.1 by Jelmer Vernooij
Fix merging of copies.
330
        if change.copied:
331
            copied = True
332
            renamed = False
333
        elif change.renamed:
334
            renamed = True
335
            copied = False
2225.1.1 by Aaron Bentley
Added revert change display, with tests
336
        else:
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
337
            copied = False
2225.1.1 by Aaron Bentley
Added revert change display, with tests
338
            renamed = False
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
339
        if change.kind[0] != change.kind[1]:
340
            if change.kind[0] is None:
2225.1.1 by Aaron Bentley
Added revert change display, with tests
341
                modified = "created"
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
342
            elif change.kind[1] is None:
2225.1.1 by Aaron Bentley
Added revert change display, with tests
343
                modified = "deleted"
344
            else:
345
                modified = "kind changed"
346
        else:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
347
            if change.changed_content:
2225.1.1 by Aaron Bentley
Added revert change display, with tests
348
                modified = "modified"
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
349
            elif change.kind[0] is None:
5504.5.1 by Rory Yorke
Show missing files in bzr status (bug 134168).
350
                modified = "missing"
2225.1.1 by Aaron Bentley
Added revert change display, with tests
351
            else:
352
                modified = "unchanged"
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
353
            if change.kind[1] == "file":
354
                exe_change = (change.executable[0] != change.executable[1])
355
        versioned_change = versioned_change_map[change.versioned]
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
356
        reporter.report(change.path, versioned_change, renamed, copied, modified,
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
357
                        exe_change, change.kind)
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
358
6631.4.1 by Martin
Rename report_delta param from filter to predicate with tests and release notes
359
360
def report_delta(to_file, delta, short_status=False, show_ids=False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
361
                 show_unchanged=False, indent='', predicate=None, classify=True):
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
362
    """Output this delta in status-like form to to_file.
363
364
    :param to_file: A file-like object where the output is displayed.
365
366
    :param delta: A TreeDelta containing the changes to be displayed
367
368
    :param short_status: Single-line status if True.
369
370
    :param show_ids: Output the file ids if True.
371
372
    :param show_unchanged: Output the unchanged files if True.
373
374
    :param indent: Added at the beginning of all output lines (for merged
375
        revisions).
376
7358.11.2 by Jelmer Vernooij
Drop file id argument from predicate.
377
    :param predicate: A callable receiving a path returning True if the path
378
        should be displayed.
5945.2.1 by Martin von Gagern
Make kind markers optional.
379
5945.2.2 by Martin von Gagern
Change from no_decorate to classify as name for the argument.
380
    :param classify: Add special symbols to indicate file kind.
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
381
    """
382
383
    def decorate_path(path, kind, meta_modified=None):
5945.2.2 by Martin von Gagern
Change from no_decorate to classify as name for the argument.
384
        if not classify:
5945.2.1 by Martin von Gagern
Make kind markers optional.
385
            return path
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
386
        if kind == 'directory':
387
            path += '/'
388
        elif kind == 'symlink':
389
            path += '@'
390
        if meta_modified:
391
            path += '*'
392
        return path
393
394
    def show_more_renamed(item):
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
395
        dec_new_path = decorate_path(item.path[1], item.kind[1], item.meta_modified())
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
396
        to_file.write(' => %s' % dec_new_path)
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
397
        if item.changed_content or item.meta_modified():
7490.120.3 by Jelmer Vernooij
Split out InventoryTreeChange from TreeChange.
398
            extra_modified.append(InventoryTreeChange(
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
399
                item.file_id, (item.path[1], item.path[1]),
400
                item.changed_content,
401
                item.versioned,
402
                (item.parent_id[1], item.parent_id[1]),
403
                (item.name[1], item.name[1]),
404
                (item.kind[1], item.kind[1]),
405
                item.executable))
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
406
407
    def show_more_kind_changed(item):
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
408
        to_file.write(' (%s => %s)' % (item.kind[0], item.kind[1]))
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
409
7358.11.1 by Jelmer Vernooij
Drop file_id argument.
410
    def show_path(path, kind, meta_modified,
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
411
                  default_format, with_file_id_format):
412
        dec_path = decorate_path(path, kind, meta_modified)
413
        if show_ids:
414
            to_file.write(with_file_id_format % dec_path)
415
        else:
416
            to_file.write(default_format % dec_path)
417
418
    def show_list(files, long_status_name, short_status_letter,
419
                  default_format='%s', with_file_id_format='%-30s',
420
                  show_more=None):
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
421
        if files:
422
            header_shown = False
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
423
            if short_status:
424
                prefix = short_status_letter
425
            else:
426
                prefix = ''
427
            prefix = indent + prefix + '  '
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
428
429
            for item in files:
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
430
                if item.path[0] is None:
431
                    path = item.path[1]
432
                    kind = item.kind[1]
433
                else:
434
                    path = item.path[0]
435
                    kind = item.kind[0]
436
                if predicate is not None and not predicate(path):
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
437
                    continue
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
438
                if not header_shown and not short_status:
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
439
                    to_file.write(indent + long_status_name + ':\n')
440
                    header_shown = True
441
                to_file.write(prefix)
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
442
                show_path(path, kind, item.meta_modified(),
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
443
                          default_format, with_file_id_format)
444
                if show_more is not None:
445
                    show_more(item)
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
446
                if show_ids and getattr(item, 'file_id', None):
447
                    to_file.write(' %s' % item.file_id.decode('utf-8'))
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
448
                to_file.write('\n')
449
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
450
    show_list(delta.removed, 'removed', 'D')
451
    show_list(delta.added, 'added', 'A')
5504.5.1 by Rory Yorke
Show missing files in bzr status (bug 134168).
452
    show_list(delta.missing, 'missing', '!')
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
453
    extra_modified = []
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
454
    show_list(delta.renamed, 'renamed', 'R', with_file_id_format='%s',
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
455
              show_more=show_more_renamed)
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
456
    show_list(delta.copied, 'copied', 'C', with_file_id_format='%s',
457
              show_more=show_more_renamed)
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
458
    show_list(delta.kind_changed, 'kind changed', 'K',
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
459
              with_file_id_format='%s',
460
              show_more=show_more_kind_changed)
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
461
    show_list(delta.modified + extra_modified, 'modified', 'M')
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
462
    if show_unchanged:
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
463
        show_list(delta.unchanged, 'unchanged', 'S')
5076.4.2 by Arnaud Jeansen
Create a short show callback using the previously removed short code (it was not dead, only not used by status). Port log to directly call the callbacks.
464
5076.4.6 by Arnaud Jeansen
Go back to unified report_delta method (i.e. former TreeDelta.show())
465
    show_list(delta.unversioned, 'unknown', ' ')