~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/delta.py

  • Committer: Martin Pool
  • Date: 2007-04-04 06:17:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404061731-tt2xrzllqhbodn83
Contents of TODO file moved into bug tracker

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
    osutils,
20
20
    )
21
21
from bzrlib.inventory import InventoryEntry
22
 
from bzrlib.trace import mutter, is_quiet
23
 
from bzrlib.symbol_versioning import deprecated_function
 
22
from bzrlib.trace import mutter
 
23
from bzrlib.symbol_versioning import deprecated_function, zero_nine
24
24
 
25
25
 
26
26
class TreeDelta(object):
27
27
    """Describes changes from one tree to another.
28
28
 
29
 
    Contains seven lists:
 
29
    Contains four lists:
30
30
 
31
31
    added
32
32
        (path, id, kind)
34
34
        (path, id, kind)
35
35
    renamed
36
36
        (oldpath, newpath, id, kind, text_modified, meta_modified)
37
 
    kind_changed
38
 
        (path, id, old_kind, new_kind)
39
37
    modified
40
38
        (path, id, kind, text_modified, meta_modified)
41
39
    unchanged
42
40
        (path, id, kind)
43
41
    unversioned
44
 
        (path, None, kind)
 
42
        (path, kind)
45
43
 
46
44
    Each id is listed only once.
47
45
 
106
104
            if v[1] == file_id:
107
105
                return True
108
106
        return False
109
 
 
 
107
            
110
108
 
111
109
    def show(self, to_file, show_ids=False, show_unchanged=False,
112
 
             short_status=False, indent='',
113
 
             filter=None):
114
 
        """Output this delta in status-like form to to_file.
115
 
 
116
 
        :param to_file: A file-like object where the output is displayed.
117
 
 
118
 
        :param show_ids: Output the file ids if True.
119
 
 
120
 
        :param show_unchanged: Output the unchanged files if True.
121
 
 
122
 
        :param short_status: Single-line status if True.
123
 
 
124
 
        :param indent: Added at the beginning of all output lines (for merged
125
 
            revisions).
126
 
 
127
 
        :param filter: A callable receiving a path and a file id and
128
 
            returning True if the path should be displayed.
129
 
        """
130
 
 
131
 
        def decorate_path(path, kind, meta_modified=None):
132
 
            if kind == 'directory':
133
 
                path += '/'
134
 
            elif kind == 'symlink':
135
 
                path += '@'
136
 
            if meta_modified:
137
 
                path += '*'
138
 
            return path
139
 
 
140
 
        def show_more_renamed(item):
141
 
            (oldpath, file_id, kind,
142
 
             text_modified, meta_modified, newpath) = item
143
 
            dec_new_path = decorate_path(newpath, kind, meta_modified)
144
 
            to_file.write(' => %s' % dec_new_path)
145
 
            if text_modified or meta_modified:
146
 
                extra_modified.append((newpath, file_id, kind,
147
 
                                       text_modified, meta_modified))
148
 
 
149
 
        def show_more_kind_changed(item):
150
 
            (path, file_id, old_kind, new_kind) = item
151
 
            to_file.write(' (%s => %s)' % (old_kind, new_kind))
152
 
 
153
 
        def show_path(path, file_id, kind, meta_modified,
154
 
                      default_format, with_file_id_format):
155
 
            dec_path = decorate_path(path, kind, meta_modified)
156
 
            if show_ids:
157
 
                to_file.write(with_file_id_format % dec_path)
158
 
            else:
159
 
                to_file.write(default_format % dec_path)
160
 
 
161
 
        def show_list(files, long_status_name, short_status_letter,
162
 
                      default_format='%s', with_file_id_format='%-30s',
163
 
                      show_more=None):
164
 
            if files:
165
 
                header_shown = False
166
 
                if short_status:
167
 
                    prefix = short_status_letter
 
110
             short_status=False):
 
111
        """output this delta in status-like form to to_file."""
 
112
        def show_list(files, short_status_letter=''):
 
113
            for item in files:
 
114
                path, fid, kind = item[:3]
 
115
 
 
116
                if kind == 'directory':
 
117
                    path += '/'
 
118
                elif kind == 'symlink':
 
119
                    path += '@'
 
120
 
 
121
                if len(item) == 5 and item[4]:
 
122
                    path += '*'
 
123
 
 
124
                if show_ids:
 
125
                    print >>to_file, '%s  %-30s %s' % (short_status_letter,
 
126
                        path, fid)
168
127
                else:
169
 
                    prefix = ''
170
 
                prefix = indent + prefix + '  '
171
 
 
172
 
                for item in files:
173
 
                    path, file_id, kind = item[:3]
174
 
                    if (filter is not None and not filter(path, file_id)):
175
 
                        continue
176
 
                    if not header_shown and not short_status:
177
 
                        to_file.write(indent + long_status_name + ':\n')
178
 
                        header_shown = True
179
 
                    meta_modified = None
180
 
                    if len(item) == 5:
181
 
                        meta_modified = item[4]
182
 
 
183
 
                    to_file.write(prefix)
184
 
                    show_path(path, file_id, kind, meta_modified,
185
 
                              default_format, with_file_id_format)
186
 
                    if show_more is not None:
187
 
                        show_more(item)
188
 
                    if show_ids:
189
 
                        to_file.write(' %s' % file_id)
190
 
                    to_file.write('\n')
191
 
 
192
 
        show_list(self.removed, 'removed', 'D')#
193
 
        show_list(self.added, 'added', 'A')
 
128
                    print >>to_file, '%s  %s' % (short_status_letter, path)
 
129
            
 
130
        if self.removed:
 
131
            if not short_status:
 
132
                print >>to_file, 'removed:'
 
133
                show_list(self.removed)
 
134
            else:
 
135
                show_list(self.removed, 'D')
 
136
                
 
137
        if self.added:
 
138
            if not short_status:
 
139
                print >>to_file, 'added:'
 
140
                show_list(self.added)
 
141
            else:
 
142
                show_list(self.added, 'A')
 
143
 
194
144
        extra_modified = []
195
 
        # Reorder self.renamed tuples so that all lists share the same
196
 
        # order for their 3 first fields and that they also begin like
197
 
        # the self.modified tuples
198
 
        renamed = [(p, i, k, tm, mm, np)
199
 
                   for  p, np, i, k, tm, mm  in self.renamed]
200
 
        show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
201
 
                  show_more=show_more_renamed)
202
 
        show_list(self.kind_changed, 'kind changed', 'K',
203
 
                  with_file_id_format='%s',
204
 
                  show_more=show_more_kind_changed)
205
 
        show_list(self.modified + extra_modified, 'modified', 'M')
206
 
        if show_unchanged:
207
 
            show_list(self.unchanged, 'unchanged', 'S')
208
 
 
209
 
        show_list(self.unversioned, 'unknown', ' ')
210
 
 
211
 
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
212
 
             short_status=False):
213
 
        import StringIO
214
 
        output = StringIO.StringIO()
215
 
        self.show(output, show_ids, show_unchanged, short_status)
216
 
        return output.getvalue()
 
145
 
 
146
        if self.renamed:
 
147
            short_status_letter = 'R'
 
148
            if not short_status:
 
149
                print >>to_file, 'renamed:'
 
150
                short_status_letter = ''
 
151
            for (oldpath, newpath, fid, kind,
 
152
                 text_modified, meta_modified) in self.renamed:
 
153
                if text_modified or meta_modified:
 
154
                    extra_modified.append((newpath, fid, kind,
 
155
                                           text_modified, meta_modified))
 
156
                if meta_modified:
 
157
                    newpath += '*'
 
158
                if show_ids:
 
159
                    print >>to_file, '%s  %s => %s %s' % (
 
160
                        short_status_letter, oldpath, newpath, fid)
 
161
                else:
 
162
                    print >>to_file, '%s  %s => %s' % (
 
163
                        short_status_letter, oldpath, newpath)
 
164
 
 
165
        if self.kind_changed:
 
166
            if short_status:
 
167
                short_status_letter = 'K'
 
168
            else:
 
169
                print >>to_file, 'kind changed:'
 
170
                short_status_letter = ''
 
171
            for (path, fid, old_kind, new_kind) in self.kind_changed:
 
172
                if show_ids:
 
173
                    suffix = ' '+fid
 
174
                else:
 
175
                    suffix = ''
 
176
                print >>to_file, '%s  %s (%s => %s)%s' % (
 
177
                    short_status_letter, path, old_kind, new_kind, suffix)
 
178
 
 
179
        if self.modified or extra_modified:
 
180
            short_status_letter = 'M'
 
181
            if not short_status:
 
182
                print >>to_file, 'modified:'
 
183
                short_status_letter = ''
 
184
            show_list(self.modified, short_status_letter)
 
185
            show_list(extra_modified, short_status_letter)
 
186
            
 
187
        if show_unchanged and self.unchanged:
 
188
            if not short_status:
 
189
                print >>to_file, 'unchanged:'
 
190
                show_list(self.unchanged)
 
191
            else:
 
192
                show_list(self.unchanged, 'S')
 
193
 
 
194
        if self.unversioned:
 
195
            print >>to_file, 'unknown:'
 
196
            show_list(self.unversioned)
 
197
 
 
198
 
 
199
@deprecated_function(zero_nine)
 
200
def compare_trees(old_tree, new_tree, want_unchanged=False,
 
201
                  specific_files=None, extra_trees=None,
 
202
                  require_versioned=False):
 
203
    """compare_trees was deprecated in 0.10. Please see Tree.changes_from."""
 
204
    return new_tree.changes_from(old_tree,
 
205
        want_unchanged=want_unchanged,
 
206
        specific_files=specific_files,
 
207
        extra_trees=extra_trees,
 
208
        require_versioned=require_versioned,
 
209
        include_root=False)
217
210
 
218
211
 
219
212
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files,
220
213
                   include_root, extra_trees=None,
221
 
                   require_versioned=False, want_unversioned=False):
 
214
                   want_unversioned=False):
222
215
    """Worker function that implements Tree.changes_from."""
223
216
    delta = TreeDelta()
224
217
    # mutter('start compare_trees')
225
218
 
226
219
    for (file_id, path, content_change, versioned, parent_id, name, kind,
227
 
         executable) in new_tree.iter_changes(old_tree, want_unchanged,
 
220
         executable) in new_tree._iter_changes(old_tree, want_unchanged,
228
221
            specific_files, extra_trees=extra_trees,
229
 
            require_versioned=require_versioned,
230
222
            want_unversioned=want_unversioned):
231
223
        if versioned == (False, False):
232
224
            delta.unversioned.append((path[1], None, kind[1]))
239
231
            if fully_present[1] is True:
240
232
                delta.added.append((path[1], file_id, kind[1]))
241
233
            else:
 
234
                assert fully_present[0] is True
242
235
                delta.removed.append((path[0], file_id, kind[0]))
243
236
        elif fully_present[0] is False:
244
237
            continue
254
247
                                  (executable[0] != executable[1])))
255
248
        elif kind[0] != kind[1]:
256
249
            delta.kind_changed.append((path[1], file_id, kind[0], kind[1]))
257
 
        elif content_change or executable[0] != executable[1]:
 
250
        elif content_change is True or executable[0] != executable[1]:
258
251
            delta.modified.append((path[1], file_id, kind[1],
259
252
                                   content_change,
260
253
                                   (executable[0] != executable[1])))
316
309
        """Report one change to a file
317
310
 
318
311
        :param file_id: The file_id of the file
319
 
        :param path: The old and new paths as generated by Tree.iter_changes.
 
312
        :param path: The old and new paths as generated by Tree._iter_changes.
320
313
        :param versioned: may be 'added', 'removed', 'unchanged', or
321
314
            'unversioned.
322
315
        :param renamed: may be True or False
323
316
        :param modified: may be 'created', 'deleted', 'kind changed',
324
317
            'modified' or 'unchanged'.
325
318
        :param exe_change: True if the execute bit has changed
326
 
        :param kind: A pair of file kinds, as generated by Tree.iter_changes.
 
319
        :param kind: A pair of file kinds, as generated by Tree._iter_changes.
327
320
            None indicates no file present.
328
321
        """
329
 
        if is_quiet():
330
 
            return
331
322
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
332
323
            return
333
324
        if versioned == 'unversioned':
347
338
                # on a rename, we show old and new
348
339
                old_path, path = paths
349
340
            else:
350
 
                # if it's not renamed, we're showing both for kind changes
 
341
                # if its not renamed, we're showing both for kind changes
351
342
                # so only show the new path
352
343
                old_path, path = paths[1], paths[1]
353
344
            # if the file is not missing in the source, we show its kind
387
378
    Further processing may be required to produce a human-readable output.
388
379
    Unfortunately, some tree-changing operations are very complex
389
380
    :change_iterator: an iterator or sequence of changes in the format
390
 
        generated by Tree.iter_changes
 
381
        generated by Tree._iter_changes
391
382
    :param reporter: The _ChangeReporter that will report the changes.
392
383
    """
393
384
    versioned_change_map = {