~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Aaron Bentley
  • Date: 2005-09-19 02:52:24 UTC
  • mto: (1185.1.29)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050919025224-1cc3c70640086e09
TODO re tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
1
# -*- coding: UTF-8 -*-
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
45
44
    ud = difflib.unified_diff(oldlines, newlines,
46
45
                              fromfile=old_label, tofile=new_label)
47
46
 
 
47
    ud = list(ud)
48
48
    # work-around for difflib being too smart for its own good
49
49
    # if /dev/null is "1,0", patch won't recognize it as /dev/null
50
50
    if not oldlines:
51
 
        ud = list(ud)
52
51
        ud[2] = ud[2].replace('-1,0', '-0,0')
53
52
    elif not newlines:
54
 
        ud = list(ud)
55
53
        ud[2] = ud[2].replace('+1,0', '+0,0')
 
54
    # work around for difflib emitting random spaces after the label
 
55
    ud[0] = ud[0][:-2] + '\n'
 
56
    ud[1] = ud[1][:-2] + '\n'
56
57
 
57
58
    for line in ud:
58
59
        to_file.write(line)
143
144
    
144
145
 
145
146
 
146
 
def show_diff(b, revision, specific_files, external_diff_options=None):
 
147
def show_diff(b, revision, specific_files, external_diff_options=None,
 
148
              revision2=None, output=None):
147
149
    """Shortcut for showing the diff to the working tree.
148
150
 
149
151
    b
155
157
    The more general form is show_diff_trees(), where the caller
156
158
    supplies any two trees.
157
159
    """
158
 
    import sys
 
160
    if output is None:
 
161
        import sys
 
162
        output = sys.stdout
159
163
 
160
 
    if revision == None:
 
164
    if revision is None:
161
165
        old_tree = b.basis_tree()
162
166
    else:
163
 
        old_tree = b.revision_tree(b.lookup_revision(revision))
164
 
        
165
 
    new_tree = b.working_tree()
166
 
 
167
 
    show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
 
167
        old_tree = b.revision_tree(revision.in_history(b).rev_id)
 
168
 
 
169
    if revision2 is None:
 
170
        new_tree = b.working_tree()
 
171
    else:
 
172
        new_tree = b.revision_tree(revision2.in_branch(b).rev_id)
 
173
 
 
174
    show_diff_trees(old_tree, new_tree, output, specific_files,
168
175
                    external_diff_options)
169
176
 
170
177
 
205
212
                          specific_files=specific_files)
206
213
 
207
214
    for path, file_id, kind in delta.removed:
208
 
        print >>to_file, '*** removed %s %r' % (kind, path)
 
215
        print >>to_file, '=== removed %s %r' % (kind, path)
209
216
        if kind == 'file':
210
217
            diff_file(old_label + path,
211
218
                      old_tree.get_file(file_id).readlines(),
214
221
                      to_file)
215
222
 
216
223
    for path, file_id, kind in delta.added:
217
 
        print >>to_file, '*** added %s %r' % (kind, path)
 
224
        print >>to_file, '=== added %s %r' % (kind, path)
218
225
        if kind == 'file':
219
226
            diff_file(DEVNULL,
220
227
                      [],
223
230
                      to_file)
224
231
 
225
232
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
226
 
        print >>to_file, '*** renamed %s %r => %r' % (kind, old_path, new_path)
 
233
        print >>to_file, '=== renamed %s %r => %r' % (kind, old_path, new_path)
227
234
        if text_modified:
228
235
            diff_file(old_label + old_path,
229
236
                      old_tree.get_file(file_id).readlines(),
232
239
                      to_file)
233
240
 
234
241
    for path, file_id, kind in delta.modified:
235
 
        print >>to_file, '*** modified %s %r' % (kind, path)
 
242
        print >>to_file, '=== modified %s %r' % (kind, path)
236
243
        if kind == 'file':
237
244
            diff_file(old_label + path,
238
245
                      old_tree.get_file(file_id).readlines(),