~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Martin Pool
  • Date: 2005-09-22 12:12:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050922121253-eae2a3240ea5e493
- upgrade can no longer be done in current version branches
  so don't test it

Show diffs side-by-side

added added

removed removed

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