/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-06-15 13:39:46 UTC
  • mto: This revision was merged to the branch mainline in revision 7342.
  • Revision ID: jelmer@jelmer.uk-20190615133946-uywh9ix0lfpqw0hy
Install quilt.

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
 
    )
20
 
from bzrlib.trace import is_quiet
 
21
    trace,
 
22
    )
 
23
from .sixish import (
 
24
    StringIO,
 
25
    )
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
135
141
            if fully_present[1] is True:
136
142
                delta.added.append((path[1], file_id, kind[1]))
137
143
            else:
138
 
                delta.removed.append((path[0], file_id, kind[0]))
 
144
                if kind[0] == 'symlink' and not new_tree.supports_symlinks():
 
145
                    trace.warning(
 
146
                        'Ignoring "%s" as symlinks '
 
147
                        'are not supported on this filesystem.' % (path[0],))
 
148
                else:
 
149
                    delta.removed.append((path[0], file_id, kind[0]))
139
150
        elif fully_present[0] is False:
140
 
            continue
 
151
            delta.missing.append((path[1], file_id, kind[1]))
141
152
        elif name[0] != name[1] or parent_id[0] != parent_id[1]:
142
153
            # If the name changes, or the parent_id changes, we have a rename
143
154
            # (if we move a parent, that doesn't count as a rename for the
160
171
    delta.removed.sort()
161
172
    delta.added.sort()
162
173
    delta.renamed.sort()
 
174
 
 
175
    def missing_key(change):
 
176
        return (change[0] or '', change[1])
 
177
    delta.missing.sort(key=missing_key)
163
178
    # TODO: jam 20060529 These lists shouldn't need to be sorted
164
179
    #       since we added them in alphabetical order.
165
180
    delta.modified.sort()
166
181
    delta.unchanged.sort()
 
182
    delta.unversioned.sort()
167
183
 
168
184
    return delta
169
185
 
172
188
    """Report changes between two trees"""
173
189
 
174
190
    def __init__(self, output=None, suppress_root_add=True,
175
 
                 output_file=None, unversioned_filter=None, view_info=None):
 
191
                 output_file=None, unversioned_filter=None, view_info=None,
 
192
                 classify=True):
176
193
        """Constructor
177
194
 
178
195
        :param output: a function with the signature of trace.note, i.e.
187
204
        :param view_info: A tuple of view_name,view_files if only
188
205
            items inside a view are to be reported on, or None for
189
206
            no view filtering.
 
207
        :param classify: Add special symbols to indicate file kind.
190
208
        """
191
209
        if output_file is not None:
192
210
            if output is not None:
193
211
                raise BzrError('Cannot specify both output and output_file')
 
212
 
194
213
            def output(fmt, *args):
195
214
                output_file.write((fmt % args) + '\n')
196
215
        self.output = output
197
216
        if self.output is None:
198
 
            from bzrlib import trace
 
217
            from . import trace
199
218
            self.output = trace.note
200
219
        self.suppress_root_add = suppress_root_add
201
220
        self.modified_map = {'kind changed': 'K',
202
221
                             'unchanged': ' ',
203
222
                             'created': 'N',
204
223
                             '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
 
224
                             'deleted': 'D',
 
225
                             'missing': '!',
 
226
                             }
 
227
        self.versioned_map = {'added': '+',  # versioned target
 
228
                              'unchanged': ' ',  # versioned in both
 
229
                              'removed': '-',  # versioned in source
 
230
                              'unversioned': '?',  # versioned in neither
210
231
                              }
211
232
        self.unversioned_filter = unversioned_filter
 
233
        if classify:
 
234
            self.kind_marker = osutils.kind_marker
 
235
        else:
 
236
            self.kind_marker = lambda kind: ''
212
237
        if view_info is None:
213
238
            self.view_name = None
214
239
            self.view_files = []
233
258
        :param kind: A pair of file kinds, as generated by Tree.iter_changes.
234
259
            None indicates no file present.
235
260
        """
236
 
        if is_quiet():
 
261
        if trace.is_quiet():
237
262
            return
238
263
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
239
264
            return
240
265
        if self.view_files and not osutils.is_inside_any(self.view_files,
241
 
            paths[1]):
 
266
                                                         paths[1]):
242
267
            return
243
268
        if versioned == 'unversioned':
244
269
            # skip ignored unversioned files if needed.
252
277
        # ( the path is different OR
253
278
        #   the kind is different)
254
279
        if (versioned == 'unchanged' and
255
 
            (renamed or modified == 'kind changed')):
 
280
                (renamed or modified == 'kind changed')):
256
281
            if renamed:
257
282
                # on a rename, we show old and new
258
283
                old_path, path = paths
263
288
            # if the file is not missing in the source, we show its kind
264
289
            # when we show two paths.
265
290
            if kind[0] is not None:
266
 
                old_path += osutils.kind_marker(kind[0])
 
291
                old_path += self.kind_marker(kind[0])
267
292
            old_path += " => "
268
293
        elif versioned == 'removed':
269
294
            # not present in target
278
303
            rename = self.versioned_map[versioned]
279
304
        # we show the old kind on the new path when the content is deleted.
280
305
        if modified == 'deleted':
281
 
            path += osutils.kind_marker(kind[0])
 
306
            path += self.kind_marker(kind[0])
282
307
        # otherwise we always show the current kind when there is one
283
308
        elif kind[1] is not None:
284
 
            path += osutils.kind_marker(kind[1])
 
309
            path += self.kind_marker(kind[1])
285
310
        if exe_change:
286
311
            exe = '*'
287
312
        else:
289
314
        self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
290
315
                    old_path, path)
291
316
 
 
317
 
292
318
def report_changes(change_iterator, reporter):
293
319
    """Report the changes from a change iterator.
294
320
 
300
326
    :param reporter: The _ChangeReporter that will report the changes.
301
327
    """
302
328
    versioned_change_map = {
303
 
        (True, True)  : 'unchanged',
304
 
        (True, False) : 'removed',
305
 
        (False, True) : 'added',
 
329
        (True, True): 'unchanged',
 
330
        (True, False): 'removed',
 
331
        (False, True): 'added',
306
332
        (False, False): 'unversioned',
307
333
        }
 
334
 
 
335
    def path_key(change):
 
336
        if change[1][0] is not None:
 
337
            path = change[1][0]
 
338
        else:
 
339
            path = change[1][1]
 
340
        return osutils.splitpath(path)
308
341
    for (file_id, path, content_change, versioned, parent_id, name, kind,
309
 
         executable) in change_iterator:
 
342
         executable) in sorted(change_iterator, key=path_key):
310
343
        exe_change = False
311
344
        # files are "renamed" if they are moved or if name changes, as long
312
345
        # as it had a value
313
346
        if None not in name and None not in parent_id and\
314
 
            (name[0] != name[1] or parent_id[0] != parent_id[1]):
 
347
                (name[0] != name[1] or parent_id[0] != parent_id[1]):
315
348
            renamed = True
316
349
        else:
317
350
            renamed = False
325
358
        else:
326
359
            if content_change:
327
360
                modified = "modified"
 
361
            elif kind[0] is None:
 
362
                modified = "missing"
328
363
            else:
329
364
                modified = "unchanged"
330
365
            if kind[1] == "file":
333
368
        reporter.report(file_id, path, versioned_change, renamed, modified,
334
369
                        exe_change, kind)
335
370
 
336
 
def report_delta(to_file, delta, short_status=False, show_ids=False, 
337
 
         show_unchanged=False, indent='', filter=None):
 
371
 
 
372
def report_delta(to_file, delta, short_status=False, show_ids=False,
 
373
                 show_unchanged=False, indent='', predicate=None, classify=True):
338
374
    """Output this delta in status-like form to to_file.
339
375
 
340
376
    :param to_file: A file-like object where the output is displayed.
350
386
    :param indent: Added at the beginning of all output lines (for merged
351
387
        revisions).
352
388
 
353
 
    :param filter: A callable receiving a path and a file id and
 
389
    :param predicate: A callable receiving a path and a file id and
354
390
        returning True if the path should be displayed.
 
391
 
 
392
    :param classify: Add special symbols to indicate file kind.
355
393
    """
356
394
 
357
395
    def decorate_path(path, kind, meta_modified=None):
 
396
        if not classify:
 
397
            return path
358
398
        if kind == 'directory':
359
399
            path += '/'
360
400
        elif kind == 'symlink':
397
437
 
398
438
            for item in files:
399
439
                path, file_id, kind = item[:3]
400
 
                if (filter is not None and not filter(path, file_id)):
 
440
                if (predicate is not None and not predicate(path, file_id)):
401
441
                    continue
402
442
                if not header_shown and not short_status:
403
443
                    to_file.write(indent + long_status_name + ':\n')
412
452
                if show_more is not None:
413
453
                    show_more(item)
414
454
                if show_ids:
415
 
                    to_file.write(' %s' % file_id)
 
455
                    to_file.write(' %s' % file_id.decode('utf-8'))
416
456
                to_file.write('\n')
417
457
 
418
458
    show_list(delta.removed, 'removed', 'D')
419
459
    show_list(delta.added, 'added', 'A')
 
460
    show_list(delta.missing, 'missing', '!')
420
461
    extra_modified = []
421
462
    # Reorder delta.renamed tuples so that all lists share the same
422
463
    # order for their 3 first fields and that they also begin like
423
464
    # the delta.modified tuples
424
465
    renamed = [(p, i, k, tm, mm, np)
425
 
               for  p, np, i, k, tm, mm  in delta.renamed]
 
466
               for p, np, i, k, tm, mm in delta.renamed]
426
467
    show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
427
468
              show_more=show_more_renamed)
428
469
    show_list(delta.kind_changed, 'kind changed', 'K',
433
474
        show_list(delta.unchanged, 'unchanged', 'S')
434
475
 
435
476
    show_list(delta.unversioned, 'unknown', ' ')
436