~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/delta.py

  • Committer: Martin Pool
  • Date: 2005-11-04 01:46:31 UTC
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051104014631-750e0ad4172c952c
Make biobench directly executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
# -*- coding: UTF-8 -*-
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from bzrlib import (
18
 
    errors,
19
 
    osutils,
20
 
    )
21
17
from bzrlib.inventory import InventoryEntry
22
 
from bzrlib.trace import mutter, is_quiet
23
 
from bzrlib.symbol_versioning import deprecated_function
24
 
 
 
18
from bzrlib.trace import mutter
25
19
 
26
20
class TreeDelta(object):
27
21
    """Describes changes from one tree to another.
38
32
        (path, id, kind, text_modified, meta_modified)
39
33
    unchanged
40
34
        (path, id, kind)
41
 
    unversioned
42
 
        (path, kind)
43
35
 
44
36
    Each id is listed only once.
45
37
 
58
50
        self.added = []
59
51
        self.removed = []
60
52
        self.renamed = []
61
 
        self.kind_changed = []
62
53
        self.modified = []
63
54
        self.unchanged = []
64
 
        self.unversioned = []
65
55
 
66
56
    def __eq__(self, other):
67
57
        if not isinstance(other, TreeDelta):
70
60
               and self.removed == other.removed \
71
61
               and self.renamed == other.renamed \
72
62
               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
 
63
               and self.unchanged == other.unchanged
76
64
 
77
65
    def __ne__(self, other):
78
66
        return not (self == other)
79
67
 
80
68
    def __repr__(self):
81
 
        return "TreeDelta(added=%r, removed=%r, renamed=%r," \
82
 
            " kind_changed=%r, modified=%r, unchanged=%r," \
83
 
            " unversioned=%r)" % (self.added,
84
 
            self.removed, self.renamed, self.kind_changed, self.modified,
85
 
            self.unchanged, self.unversioned)
 
69
        return "TreeDelta(added=%r, removed=%r, renamed=%r, modified=%r," \
 
70
            " unchanged=%r)" % (self.added, self.removed, self.renamed,
 
71
            self.modified, self.unchanged)
86
72
 
87
73
    def has_changed(self):
88
74
        return bool(self.modified
89
75
                    or self.added
90
76
                    or self.removed
91
 
                    or self.renamed
92
 
                    or self.kind_changed)
 
77
                    or self.renamed)
93
78
 
94
79
    def touches_file_id(self, file_id):
95
80
        """Return True if file_id is modified by this delta."""
100
85
        for v in self.renamed:
101
86
            if v[2] == file_id:
102
87
                return True
103
 
        for v in self.kind_changed:
104
 
            if v[1] == file_id:
105
 
                return True
106
88
        return False
107
89
            
108
90
 
109
 
    def show(self, to_file, show_ids=False, show_unchanged=False,
110
 
             short_status=False, indent=''):
111
 
        """output this delta in status-like form to to_file."""
112
 
        def show_list(files, short_status_letter=''):
 
91
    def show(self, to_file, show_ids=False, show_unchanged=False):
 
92
        def show_list(files):
113
93
            for item in files:
114
94
                path, fid, kind = item[:3]
115
95
 
122
102
                    path += '*'
123
103
 
124
104
                if show_ids:
125
 
                    to_file.write(indent + '%s  %-30s %s\n' % (short_status_letter,
126
 
                        path, fid))
 
105
                    print >>to_file, '  %-30s %s' % (path, fid)
127
106
                else:
128
 
                    to_file.write(indent + '%s  %s\n' % (short_status_letter, path))
 
107
                    print >>to_file, ' ', path
129
108
            
130
109
        if self.removed:
131
 
            if not short_status:
132
 
                to_file.write(indent + 'removed:\n')
133
 
                show_list(self.removed)
134
 
            else:
135
 
                show_list(self.removed, 'D')
 
110
            print >>to_file, 'removed:'
 
111
            show_list(self.removed)
136
112
                
137
113
        if self.added:
138
 
            if not short_status:
139
 
                to_file.write(indent + 'added:\n')
140
 
                show_list(self.added)
141
 
            else:
142
 
                show_list(self.added, 'A')
143
 
 
144
 
        extra_modified = []
 
114
            print >>to_file, 'added:'
 
115
            show_list(self.added)
145
116
 
146
117
        if self.renamed:
147
 
            short_status_letter = 'R'
148
 
            if not short_status:
149
 
                to_file.write(indent + 'renamed:\n')
150
 
                short_status_letter = ''
 
118
            print >>to_file, 'renamed:'
151
119
            for (oldpath, newpath, fid, kind,
152
120
                 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
121
                if meta_modified:
157
122
                    newpath += '*'
158
123
                if show_ids:
159
 
                    to_file.write(indent + '%s  %s => %s %s\n' % (
160
 
                        short_status_letter, oldpath, newpath, fid))
161
 
                else:
162
 
                    to_file.write(indent + '%s  %s => %s\n' % (
163
 
                        short_status_letter, oldpath, newpath))
164
 
 
165
 
        if self.kind_changed:
166
 
            if short_status:
167
 
                short_status_letter = 'K'
168
 
            else:
169
 
                to_file.write(indent + 'kind changed:\n')
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
 
                to_file.write(indent + '%s  %s (%s => %s)%s\n' % (
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
 
                to_file.write(indent + 'modified:\n')
183
 
                short_status_letter = ''
184
 
            show_list(self.modified, short_status_letter)
185
 
            show_list(extra_modified, short_status_letter)
 
124
                    print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
 
125
                else:
 
126
                    print >>to_file, '  %s => %s' % (oldpath, newpath)
 
127
                    
 
128
        if self.modified:
 
129
            print >>to_file, 'modified:'
 
130
            show_list(self.modified)
186
131
            
187
132
        if show_unchanged and self.unchanged:
188
 
            if not short_status:
189
 
                to_file.write(indent + 'unchanged:\n')
190
 
                show_list(self.unchanged)
191
 
            else:
192
 
                show_list(self.unchanged, 'S')
193
 
 
194
 
        if self.unversioned:
195
 
            to_file.write(indent + 'unknown:\n')
196
 
            show_list(self.unversioned)
197
 
 
198
 
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
199
 
             short_status=False):
200
 
        import StringIO
201
 
        output = StringIO.StringIO()
202
 
        self.show(output, show_ids, show_unchanged, short_status)
203
 
        return output.getvalue()
204
 
 
205
 
 
206
 
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files,
207
 
                   include_root, extra_trees=None,
208
 
                   require_versioned=False, want_unversioned=False):
209
 
    """Worker function that implements Tree.changes_from."""
 
133
            print >>to_file, 'unchanged:'
 
134
            show_list(self.unchanged)
 
135
 
 
136
 
 
137
 
 
138
def compare_trees(old_tree, new_tree, want_unchanged=False, specific_files=None):
 
139
    """Describe changes from one tree to another.
 
140
 
 
141
    Returns a TreeDelta with details of added, modified, renamed, and
 
142
    deleted entries.
 
143
 
 
144
    The root entry is specifically exempt.
 
145
 
 
146
    This only considers versioned files.
 
147
 
 
148
    want_unchanged
 
149
        If true, also list files unchanged from one version to
 
150
        the next.
 
151
 
 
152
    specific_files
 
153
        If true, only check for changes to specified names or
 
154
        files within them.
 
155
    """
 
156
 
 
157
    from osutils import is_inside_any
 
158
    
 
159
    old_inv = old_tree.inventory
 
160
    new_inv = new_tree.inventory
210
161
    delta = TreeDelta()
211
 
    # mutter('start compare_trees')
212
 
 
213
 
    for (file_id, path, content_change, versioned, parent_id, name, kind,
214
 
         executable) in new_tree.iter_changes(old_tree, want_unchanged,
215
 
            specific_files, extra_trees=extra_trees,
216
 
            require_versioned=require_versioned,
217
 
            want_unversioned=want_unversioned):
218
 
        if versioned == (False, False):
219
 
            delta.unversioned.append((path[1], None, kind[1]))
220
 
            continue
221
 
        if not include_root and (None, None) == parent_id:
222
 
            continue
223
 
        fully_present = tuple((versioned[x] and kind[x] is not None) for
224
 
                              x in range(2))
225
 
        if fully_present[0] != fully_present[1]:
226
 
            if fully_present[1] is True:
227
 
                delta.added.append((path[1], file_id, kind[1]))
228
 
            else:
229
 
                assert fully_present[0] is True
230
 
                delta.removed.append((path[0], file_id, kind[0]))
231
 
        elif fully_present[0] is False:
232
 
            continue
233
 
        elif name[0] != name[1] or parent_id[0] != parent_id[1]:
234
 
            # If the name changes, or the parent_id changes, we have a rename
235
 
            # (if we move a parent, that doesn't count as a rename for the
236
 
            # file)
237
 
            delta.renamed.append((path[0],
238
 
                                  path[1],
239
 
                                  file_id,
240
 
                                  kind[1],
241
 
                                  content_change,
242
 
                                  (executable[0] != executable[1])))
243
 
        elif kind[0] != kind[1]:
244
 
            delta.kind_changed.append((path[1], file_id, kind[0], kind[1]))
245
 
        elif content_change is True or executable[0] != executable[1]:
246
 
            delta.modified.append((path[1], file_id, kind[1],
247
 
                                   content_change,
248
 
                                   (executable[0] != executable[1])))
 
162
    mutter('start compare_trees')
 
163
 
 
164
    # TODO: match for specific files can be rather smarter by finding
 
165
    # the IDs of those files up front and then considering only that.
 
166
 
 
167
    for file_id in old_tree:
 
168
        if file_id in new_tree:
 
169
            old_ie = old_inv[file_id]
 
170
            new_ie = new_inv[file_id]
 
171
 
 
172
            kind = old_ie.kind
 
173
            assert kind == new_ie.kind
 
174
            
 
175
            assert kind in InventoryEntry.known_kinds, \
 
176
                   'invalid file kind %r' % kind
 
177
 
 
178
            if kind == 'root_directory':
 
179
                continue
 
180
            
 
181
            if specific_files:
 
182
                if (not is_inside_any(specific_files, old_inv.id2path(file_id)) 
 
183
                    and not is_inside_any(specific_files, new_inv.id2path(file_id))):
 
184
                    continue
 
185
 
 
186
            # temporary hack until all entries are populated before clients 
 
187
            # get them
 
188
            old_path = old_inv.id2path(file_id)
 
189
            new_path = new_inv.id2path(file_id)
 
190
            old_ie._read_tree_state(old_path, old_tree)
 
191
            new_ie._read_tree_state(new_path, new_tree)
 
192
            text_modified, meta_modified = new_ie.detect_changes(old_ie)
 
193
 
 
194
            # TODO: Can possibly avoid calculating path strings if the
 
195
            # two files are unchanged and their names and parents are
 
196
            # the same and the parents are unchanged all the way up.
 
197
            # May not be worthwhile.
 
198
            
 
199
            if (old_ie.name != new_ie.name
 
200
                or old_ie.parent_id != new_ie.parent_id):
 
201
                delta.renamed.append((old_path,
 
202
                                      new_path,
 
203
                                      file_id, kind,
 
204
                                      text_modified, meta_modified))
 
205
            elif text_modified or meta_modified:
 
206
                delta.modified.append((new_path, file_id, kind,
 
207
                                       text_modified, meta_modified))
 
208
            elif want_unchanged:
 
209
                delta.unchanged.append((new_path, file_id, kind))
249
210
        else:
250
 
            delta.unchanged.append((path[1], file_id, kind[1]))
 
211
            kind = old_inv.get_file_kind(file_id)
 
212
            if kind == 'root_directory':
 
213
                continue
 
214
            old_path = old_inv.id2path(file_id)
 
215
            if specific_files:
 
216
                if not is_inside_any(specific_files, old_path):
 
217
                    continue
 
218
            delta.removed.append((old_path, file_id, kind))
251
219
 
 
220
    mutter('start looking for new files')
 
221
    for file_id in new_inv:
 
222
        if file_id in old_inv:
 
223
            continue
 
224
        kind = new_inv.get_file_kind(file_id)
 
225
        if kind == 'root_directory':
 
226
            continue
 
227
        new_path = new_inv.id2path(file_id)
 
228
        if specific_files:
 
229
            if not is_inside_any(specific_files, new_path):
 
230
                continue
 
231
        delta.added.append((new_path, file_id, kind))
 
232
            
252
233
    delta.removed.sort()
253
234
    delta.added.sort()
254
235
    delta.renamed.sort()
255
 
    # TODO: jam 20060529 These lists shouldn't need to be sorted
256
 
    #       since we added them in alphabetical order.
257
236
    delta.modified.sort()
258
237
    delta.unchanged.sort()
259
238
 
260
239
    return delta
261
 
 
262
 
 
263
 
class _ChangeReporter(object):
264
 
    """Report changes between two trees"""
265
 
 
266
 
    def __init__(self, output=None, suppress_root_add=True,
267
 
                 output_file=None, unversioned_filter=None):
268
 
        """Constructor
269
 
 
270
 
        :param output: a function with the signature of trace.note, i.e.
271
 
            accepts a format and parameters.
272
 
        :param supress_root_add: If true, adding the root will be ignored
273
 
            (i.e. when a tree has just been initted)
274
 
        :param output_file: If supplied, a file-like object to write to.
275
 
            Only one of output and output_file may be supplied.
276
 
        :param unversioned_filter: A filter function to be called on 
277
 
            unversioned files. This should return True to ignore a path.
278
 
            By default, no filtering takes place.
279
 
        """
280
 
        if output_file is not None:
281
 
            if output is not None:
282
 
                raise BzrError('Cannot specify both output and output_file')
283
 
            def output(fmt, *args):
284
 
                output_file.write((fmt % args) + '\n')
285
 
        self.output = output
286
 
        if self.output is None:
287
 
            from bzrlib import trace
288
 
            self.output = trace.note
289
 
        self.suppress_root_add = suppress_root_add
290
 
        self.modified_map = {'kind changed': 'K',
291
 
                             'unchanged': ' ',
292
 
                             'created': 'N',
293
 
                             'modified': 'M',
294
 
                             'deleted': 'D'}
295
 
        self.versioned_map = {'added': '+', # versioned target
296
 
                              'unchanged': ' ', # versioned in both
297
 
                              'removed': '-', # versioned in source
298
 
                              'unversioned': '?', # versioned in neither
299
 
                              }
300
 
        self.unversioned_filter = unversioned_filter
301
 
 
302
 
    def report(self, file_id, paths, versioned, renamed, modified, exe_change,
303
 
               kind):
304
 
        """Report one change to a file
305
 
 
306
 
        :param file_id: The file_id of the file
307
 
        :param path: The old and new paths as generated by Tree.iter_changes.
308
 
        :param versioned: may be 'added', 'removed', 'unchanged', or
309
 
            'unversioned.
310
 
        :param renamed: may be True or False
311
 
        :param modified: may be 'created', 'deleted', 'kind changed',
312
 
            'modified' or 'unchanged'.
313
 
        :param exe_change: True if the execute bit has changed
314
 
        :param kind: A pair of file kinds, as generated by Tree.iter_changes.
315
 
            None indicates no file present.
316
 
        """
317
 
        if is_quiet():
318
 
            return
319
 
        if paths[1] == '' and versioned == 'added' and self.suppress_root_add:
320
 
            return
321
 
        if versioned == 'unversioned':
322
 
            # skip ignored unversioned files if needed.
323
 
            if self.unversioned_filter is not None:
324
 
                if self.unversioned_filter(paths[1]):
325
 
                    return
326
 
            # dont show a content change in the output.
327
 
            modified = 'unchanged'
328
 
        # we show both paths in the following situations:
329
 
        # the file versioning is unchanged AND
330
 
        # ( the path is different OR
331
 
        #   the kind is different)
332
 
        if (versioned == 'unchanged' and
333
 
            (renamed or modified == 'kind changed')):
334
 
            if renamed:
335
 
                # on a rename, we show old and new
336
 
                old_path, path = paths
337
 
            else:
338
 
                # if it's not renamed, we're showing both for kind changes
339
 
                # so only show the new path
340
 
                old_path, path = paths[1], paths[1]
341
 
            # if the file is not missing in the source, we show its kind
342
 
            # when we show two paths.
343
 
            if kind[0] is not None:
344
 
                old_path += osutils.kind_marker(kind[0])
345
 
            old_path += " => "
346
 
        elif versioned == 'removed':
347
 
            # not present in target
348
 
            old_path = ""
349
 
            path = paths[0]
350
 
        else:
351
 
            old_path = ""
352
 
            path = paths[1]
353
 
        if renamed:
354
 
            rename = "R"
355
 
        else:
356
 
            rename = self.versioned_map[versioned]
357
 
        # we show the old kind on the new path when the content is deleted.
358
 
        if modified == 'deleted':
359
 
            path += osutils.kind_marker(kind[0])
360
 
        # otherwise we always show the current kind when there is one
361
 
        elif kind[1] is not None:
362
 
            path += osutils.kind_marker(kind[1])
363
 
        if exe_change:
364
 
            exe = '*'
365
 
        else:
366
 
            exe = ' '
367
 
        self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
368
 
                    old_path, path)
369
 
 
370
 
 
371
 
def report_changes(change_iterator, reporter):
372
 
    """Report the changes from a change iterator.
373
 
 
374
 
    This is essentially a translation from low-level to medium-level changes.
375
 
    Further processing may be required to produce a human-readable output.
376
 
    Unfortunately, some tree-changing operations are very complex
377
 
    :change_iterator: an iterator or sequence of changes in the format
378
 
        generated by Tree.iter_changes
379
 
    :param reporter: The _ChangeReporter that will report the changes.
380
 
    """
381
 
    versioned_change_map = {
382
 
        (True, True)  : 'unchanged',
383
 
        (True, False) : 'removed',
384
 
        (False, True) : 'added',
385
 
        (False, False): 'unversioned',
386
 
        }
387
 
    for (file_id, path, content_change, versioned, parent_id, name, kind,
388
 
         executable) in change_iterator:
389
 
        exe_change = False
390
 
        # files are "renamed" if they are moved or if name changes, as long
391
 
        # as it had a value
392
 
        if None not in name and None not in parent_id and\
393
 
            (name[0] != name[1] or parent_id[0] != parent_id[1]):
394
 
            renamed = True
395
 
        else:
396
 
            renamed = False
397
 
        if kind[0] != kind[1]:
398
 
            if kind[0] is None:
399
 
                modified = "created"
400
 
            elif kind[1] is None:
401
 
                modified = "deleted"
402
 
            else:
403
 
                modified = "kind changed"
404
 
        else:
405
 
            if content_change:
406
 
                modified = "modified"
407
 
            else:
408
 
                modified = "unchanged"
409
 
            if kind[1] == "file":
410
 
                exe_change = (executable[0] != executable[1])
411
 
        versioned_change = versioned_change_map[versioned]
412
 
        reporter.report(file_id, path, versioned_change, renamed, modified,
413
 
                        exe_change, kind)