~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Martin Pool
  • Date: 2005-06-01 07:45:28 UTC
  • Revision ID: mbp@sourcefrog.net-20050601074528-54509f4b1d505f55
- ignore .git files as well

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from errors import BzrError
20
20
 
21
21
 
22
 
# TODO: Rather than building a changeset object, we should probably
23
 
# invoke callbacks on an object.  That object can either accumulate a
24
 
# list, write them out directly, etc etc.
25
 
 
26
22
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
27
23
    import difflib
28
24
    
63
59
        ud = list(ud)
64
60
        ud[2] = ud[2].replace('+1,0', '+0,0')
65
61
 
66
 
    for line in ud:
67
 
        to_file.write(line)
 
62
    to_file.writelines(ud)
68
63
    if nonl:
69
64
        print >>to_file, "\\ No newline at end of file"
70
65
    print >>to_file
153
148
 
154
149
 
155
150
def show_diff(b, revision, specific_files, external_diff_options=None):
156
 
    """Shortcut for showing the diff to the working tree.
157
 
 
158
 
    b
159
 
        Branch.
160
 
 
161
 
    revision
162
 
        None for each, or otherwise the old revision to compare against.
163
 
    
164
 
    The more general form is show_diff_trees(), where the caller
165
 
    supplies any two trees.
166
 
    """
167
151
    import sys
168
152
 
169
153
    if revision == None:
214
198
                          specific_files=specific_files)
215
199
 
216
200
    for path, file_id, kind in delta.removed:
217
 
        print >>to_file, '*** removed %s %r' % (kind, path)
 
201
        print '*** removed %s %r' % (kind, path)
218
202
        if kind == 'file':
219
203
            diff_file(old_label + path,
220
204
                      old_tree.get_file(file_id).readlines(),
223
207
                      to_file)
224
208
 
225
209
    for path, file_id, kind in delta.added:
226
 
        print >>to_file, '*** added %s %r' % (kind, path)
 
210
        print '*** added %s %r' % (kind, path)
227
211
        if kind == 'file':
228
212
            diff_file(DEVNULL,
229
213
                      [],
232
216
                      to_file)
233
217
 
234
218
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
235
 
        print >>to_file, '*** renamed %s %r => %r' % (kind, old_path, new_path)
 
219
        print '*** renamed %s %r => %r' % (kind, old_path, new_path)
236
220
        if text_modified:
237
221
            diff_file(old_label + old_path,
238
222
                      old_tree.get_file(file_id).readlines(),
241
225
                      to_file)
242
226
 
243
227
    for path, file_id, kind in delta.modified:
244
 
        print >>to_file, '*** modified %s %r' % (kind, path)
 
228
        print '*** modified %s %r' % (kind, path)
245
229
        if kind == 'file':
246
230
            diff_file(old_label + path,
247
231
                      old_tree.get_file(file_id).readlines(),
272
256
    Files that are both modified and renamed are listed only in
273
257
    renamed, with the text_modified flag true.
274
258
 
275
 
    Files are only considered renamed if their name has changed or
276
 
    their parent directory has changed.  Renaming a directory
277
 
    does not count as renaming all its contents.
278
 
 
279
259
    The lists are normally sorted when the delta is created.
280
260
    """
281
261
    def __init__(self):
285
265
        self.modified = []
286
266
        self.unchanged = []
287
267
 
288
 
    def __eq__(self, other):
289
 
        if not isinstance(other, TreeDelta):
290
 
            return False
291
 
        return self.added == other.added \
292
 
               and self.removed == other.removed \
293
 
               and self.renamed == other.renamed \
294
 
               and self.modified == other.modified \
295
 
               and self.unchanged == other.unchanged
296
 
 
297
 
    def __ne__(self, other):
298
 
        return not (self == other)
299
 
 
300
 
    def __repr__(self):
301
 
        return "TreeDelta(added=%r, removed=%r, renamed=%r, modified=%r," \
302
 
            " unchanged=%r)" % (self.added, self.removed, self.renamed,
303
 
            self.modified, self.unchanged)
304
 
 
305
 
    def has_changed(self):
306
 
        changes = len(self.added) + len(self.removed) + len(self.renamed)
307
 
        changes += len(self.modified) 
308
 
        return (changes != 0)
309
268
 
310
269
    def touches_file_id(self, file_id):
311
270
        """Return True if file_id is modified by this delta."""
358
317
 
359
318
 
360
319
 
361
 
def compare_trees(old_tree, new_tree, want_unchanged=False, specific_files=None):
 
320
def compare_trees(old_tree, new_tree, want_unchanged, specific_files=None):
362
321
    """Describe changes from one tree to another.
363
322
 
364
323
    Returns a TreeDelta with details of added, modified, renamed, and
401
360
            old_path = old_inv.id2path(file_id)
402
361
            new_path = new_inv.id2path(file_id)
403
362
 
404
 
            old_ie = old_inv[file_id]
405
 
            new_ie = new_inv[file_id]
406
 
 
407
363
            if specific_files:
408
364
                if (not is_inside_any(specific_files, old_path) 
409
365
                    and not is_inside_any(specific_files, new_path)):
422
378
            # the same and the parents are unchanged all the way up.
423
379
            # May not be worthwhile.
424
380
            
425
 
            if (old_ie.name != new_ie.name
426
 
                or old_ie.parent_id != new_ie.parent_id):
 
381
            if old_path != new_path:
427
382
                delta.renamed.append((old_path, new_path, file_id, kind,
428
383
                                      text_modified))
429
384
            elif text_modified: