/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 breezy/delta.py

  • Committer: Jelmer Vernooij
  • Date: 2019-02-04 01:01:24 UTC
  • mto: This revision was merged to the branch mainline in revision 7268.
  • Revision ID: jelmer@jelmer.uk-20190204010124-ni0i4qc6f5tnbvux
Fix source tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from bzrlib import (
 
17
from __future__ import absolute_import
 
18
 
 
19
from breezy import (
18
20
    osutils,
19
21
    )
20
 
from bzrlib.trace import is_quiet
 
22
from .sixish import (
 
23
    StringIO,
 
24
    )
 
25
from .trace import is_quiet
21
26
 
22
27
 
23
28
class TreeDelta(object):
53
58
 
54
59
    The lists are normally sorted when the delta is created.
55
60
    """
 
61
 
56
62
    def __init__(self):
57
63
        self.added = []
58
64
        self.removed = []
61
67
        self.modified = []
62
68
        self.unchanged = []
63
69
        self.unversioned = []
 
70
        self.missing = []
64
71
 
65
72
    def __eq__(self, other):
66
73
        if not isinstance(other, TreeDelta):
67
74
            return False
68
75
        return self.added == other.added \
69
 
               and self.removed == other.removed \
70
 
               and self.renamed == other.renamed \
71
 
               and self.modified == other.modified \
72
 
               and self.unchanged == other.unchanged \
73
 
               and self.kind_changed == other.kind_changed \
74
 
               and self.unversioned == other.unversioned
 
76
            and self.removed == other.removed \
 
77
            and self.renamed == other.renamed \
 
78
            and self.modified == other.modified \
 
79
            and self.unchanged == other.unchanged \
 
80
            and self.kind_changed == other.kind_changed \
 
81
            and self.unversioned == other.unversioned
75
82
 
76
83
    def __ne__(self, other):
77
84
        return not (self == other)
80
87
        return "TreeDelta(added=%r, removed=%r, renamed=%r," \
81
88
            " kind_changed=%r, modified=%r, unchanged=%r," \
82
89
            " unversioned=%r)" % (self.added,
83
 
            self.removed, self.renamed, self.kind_changed, self.modified,
84
 
            self.unchanged, self.unversioned)
 
90
                                  self.removed, self.renamed, self.kind_changed, self.modified,
 
91
                                  self.unchanged, self.unversioned)
85
92
 
86
93
    def has_changed(self):
87
94
        return bool(self.modified
106
113
 
107
114
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
108
115
                            short_status=False):
109
 
        import StringIO
110
 
        output = StringIO.StringIO()
 
116
        output = StringIO()
111
117
        report_delta(output, self, short_status, show_ids, show_unchanged)
112
118
        return output.getvalue()
113
119
 
121
127
 
122
128
    for (file_id, path, content_change, versioned, parent_id, name, kind,
123
129
         executable) in new_tree.iter_changes(old_tree, want_unchanged,
124
 
            specific_files, extra_trees=extra_trees,
125
 
            require_versioned=require_versioned,
126
 
            want_unversioned=want_unversioned):
 
130
                                              specific_files, extra_trees=extra_trees,
 
131
                                              require_versioned=require_versioned,
 
132
                                              want_unversioned=want_unversioned):
127
133
        if versioned == (False, False):
128
134
            delta.unversioned.append((path[1], None, kind[1]))
129
135
            continue
137
143
            else:
138
144
                delta.removed.append((path[0], file_id, kind[0]))
139
145
        elif fully_present[0] is False:
140
 
            continue
 
146
            delta.missing.append((path[1], file_id, kind[1]))
141
147
        elif name[0] != name[1] or parent_id[0] != parent_id[1]:
142
148
            # If the name changes, or the parent_id changes, we have a rename
143
149
            # (if we move a parent, that doesn't count as a rename for the
160
166
    delta.removed.sort()
161
167
    delta.added.sort()
162
168
    delta.renamed.sort()
 
169
 
 
170
    def missing_key(change):
 
171
        return (change[0] or '', change[1])
 
172
    delta.missing.sort(key=missing_key)
163
173
    # TODO: jam 20060529 These lists shouldn't need to be sorted
164
174
    #       since we added them in alphabetical order.
165
175
    delta.modified.sort()
166
176
    delta.unchanged.sort()
 
177
    delta.unversioned.sort()
167
178
 
168
179
    return delta
169
180
 
172
183
    """Report changes between two trees"""
173
184
 
174
185
    def __init__(self, output=None, suppress_root_add=True,
175
 
                 output_file=None, unversioned_filter=None, view_info=None):
 
186
                 output_file=None, unversioned_filter=None, view_info=None,
 
187
                 classify=True):
176
188
        """Constructor
177
189
 
178
190
        :param output: a function with the signature of trace.note, i.e.
187
199
        :param view_info: A tuple of view_name,view_files if only
188
200
            items inside a view are to be reported on, or None for
189
201
            no view filtering.
 
202
        :param classify: Add special symbols to indicate file kind.
190
203
        """
191
204
        if output_file is not None:
192
205
            if output is not None:
193
206
                raise BzrError('Cannot specify both output and output_file')
 
207
 
194
208
            def output(fmt, *args):
195
209
                output_file.write((fmt % args) + '\n')
196
210
        self.output = output
197
211
        if self.output is None:
198
 
            from bzrlib import trace
 
212
            from . import trace
199
213
            self.output = trace.note
200
214
        self.suppress_root_add = suppress_root_add
201
215
        self.modified_map = {'kind changed': 'K',
202
216
                             'unchanged': ' ',
203
217
                             'created': 'N',
204
218
                             'modified': 'M',
205
 
                             'deleted': 'D'}
206
 
        self.versioned_map = {'added': '+', # versioned target
207
 
                              'unchanged': ' ', # versioned in both
208
 
                              'removed': '-', # versioned in source
209
 
                              'unversioned': '?', # versioned in neither
 
219
                             'deleted': 'D',
 
220
                             'missing': '!',
 
221
                             }
 
222
        self.versioned_map = {'added': '+',  # versioned target
 
223
                              'unchanged': ' ',  # versioned in both
 
224
                              'removed': '-',  # versioned in source
 
225
                              'unversioned': '?',  # versioned in neither
210
226
                              }
211
227
        self.unversioned_filter = unversioned_filter
 
228
        if classify:
 
229
            self.kind_marker = osutils.kind_marker
 
230
        else:
 
231
            self.kind_marker = lambda kind: ''
212
232
        if view_info is None:
213
233
            self.view_name = None
214
234
            self.view_files = []
238
258
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
239
259
            return
240
260
        if self.view_files and not osutils.is_inside_any(self.view_files,
241
 
            paths[1]):
 
261
                                                         paths[1]):
242
262
            return
243
263
        if versioned == 'unversioned':
244
264
            # skip ignored unversioned files if needed.
252
272
        # ( the path is different OR
253
273
        #   the kind is different)
254
274
        if (versioned == 'unchanged' and
255
 
            (renamed or modified == 'kind changed')):
 
275
                (renamed or modified == 'kind changed')):
256
276
            if renamed:
257
277
                # on a rename, we show old and new
258
278
                old_path, path = paths
263
283
            # if the file is not missing in the source, we show its kind
264
284
            # when we show two paths.
265
285
            if kind[0] is not None:
266
 
                old_path += osutils.kind_marker(kind[0])
 
286
                old_path += self.kind_marker(kind[0])
267
287
            old_path += " => "
268
288
        elif versioned == 'removed':
269
289
            # not present in target
278
298
            rename = self.versioned_map[versioned]
279
299
        # we show the old kind on the new path when the content is deleted.
280
300
        if modified == 'deleted':
281
 
            path += osutils.kind_marker(kind[0])
 
301
            path += self.kind_marker(kind[0])
282
302
        # otherwise we always show the current kind when there is one
283
303
        elif kind[1] is not None:
284
 
            path += osutils.kind_marker(kind[1])
 
304
            path += self.kind_marker(kind[1])
285
305
        if exe_change:
286
306
            exe = '*'
287
307
        else:
289
309
        self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
290
310
                    old_path, path)
291
311
 
 
312
 
292
313
def report_changes(change_iterator, reporter):
293
314
    """Report the changes from a change iterator.
294
315
 
300
321
    :param reporter: The _ChangeReporter that will report the changes.
301
322
    """
302
323
    versioned_change_map = {
303
 
        (True, True)  : 'unchanged',
304
 
        (True, False) : 'removed',
305
 
        (False, True) : 'added',
 
324
        (True, True): 'unchanged',
 
325
        (True, False): 'removed',
 
326
        (False, True): 'added',
306
327
        (False, False): 'unversioned',
307
328
        }
 
329
 
 
330
    def path_key(change):
 
331
        if change[1][0] is not None:
 
332
            path = change[1][0]
 
333
        else:
 
334
            path = change[1][1]
 
335
        return osutils.splitpath(path)
308
336
    for (file_id, path, content_change, versioned, parent_id, name, kind,
309
 
         executable) in change_iterator:
 
337
         executable) in sorted(change_iterator, key=path_key):
310
338
        exe_change = False
311
339
        # files are "renamed" if they are moved or if name changes, as long
312
340
        # as it had a value
313
341
        if None not in name and None not in parent_id and\
314
 
            (name[0] != name[1] or parent_id[0] != parent_id[1]):
 
342
                (name[0] != name[1] or parent_id[0] != parent_id[1]):
315
343
            renamed = True
316
344
        else:
317
345
            renamed = False
325
353
        else:
326
354
            if content_change:
327
355
                modified = "modified"
 
356
            elif kind[0] is None:
 
357
                modified = "missing"
328
358
            else:
329
359
                modified = "unchanged"
330
360
            if kind[1] == "file":
333
363
        reporter.report(file_id, path, versioned_change, renamed, modified,
334
364
                        exe_change, kind)
335
365
 
336
 
def report_delta(to_file, delta, short_status=False, show_ids=False, 
337
 
         show_unchanged=False, indent='', filter=None):
 
366
 
 
367
def report_delta(to_file, delta, short_status=False, show_ids=False,
 
368
                 show_unchanged=False, indent='', predicate=None, classify=True):
338
369
    """Output this delta in status-like form to to_file.
339
370
 
340
371
    :param to_file: A file-like object where the output is displayed.
350
381
    :param indent: Added at the beginning of all output lines (for merged
351
382
        revisions).
352
383
 
353
 
    :param filter: A callable receiving a path and a file id and
 
384
    :param predicate: A callable receiving a path and a file id and
354
385
        returning True if the path should be displayed.
 
386
 
 
387
    :param classify: Add special symbols to indicate file kind.
355
388
    """
356
389
 
357
390
    def decorate_path(path, kind, meta_modified=None):
 
391
        if not classify:
 
392
            return path
358
393
        if kind == 'directory':
359
394
            path += '/'
360
395
        elif kind == 'symlink':
397
432
 
398
433
            for item in files:
399
434
                path, file_id, kind = item[:3]
400
 
                if (filter is not None and not filter(path, file_id)):
 
435
                if (predicate is not None and not predicate(path, file_id)):
401
436
                    continue
402
437
                if not header_shown and not short_status:
403
438
                    to_file.write(indent + long_status_name + ':\n')
412
447
                if show_more is not None:
413
448
                    show_more(item)
414
449
                if show_ids:
415
 
                    to_file.write(' %s' % file_id)
 
450
                    to_file.write(' %s' % file_id.decode('utf-8'))
416
451
                to_file.write('\n')
417
452
 
418
453
    show_list(delta.removed, 'removed', 'D')
419
454
    show_list(delta.added, 'added', 'A')
 
455
    show_list(delta.missing, 'missing', '!')
420
456
    extra_modified = []
421
457
    # Reorder delta.renamed tuples so that all lists share the same
422
458
    # order for their 3 first fields and that they also begin like
423
459
    # the delta.modified tuples
424
460
    renamed = [(p, i, k, tm, mm, np)
425
 
               for  p, np, i, k, tm, mm  in delta.renamed]
 
461
               for p, np, i, k, tm, mm in delta.renamed]
426
462
    show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
427
463
              show_more=show_more_renamed)
428
464
    show_list(delta.kind_changed, 'kind changed', 'K',
433
469
        show_list(delta.unchanged, 'unchanged', 'S')
434
470
 
435
471
    show_list(delta.unversioned, 'unknown', ' ')
436