19
19
from errors import BzrError
23
def _diff_one(oldlines, newlines, to_file, **kw):
22
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
26
25
# FIXME: difflib is wrong if there is no trailing newline.
48
47
newlines[-1] += '\n'
51
ud = difflib.unified_diff(oldlines, newlines, **kw)
50
ud = difflib.unified_diff(oldlines, newlines,
51
fromfile=old_label, tofile=new_label)
53
53
# work-around for difflib being too smart for its own good
54
54
# if /dev/null is "1,0", patch won't recognize it as /dev/null
70
def external_diff(old_label, oldlines, new_label, newlines, to_file):
71
"""Display a diff by calling out to the external diff program."""
74
if to_file != sys.stdout:
75
raise NotImplementedError("sorry, can't send external diff other than to stdout yet",
78
from tempfile import NamedTemporaryFile
81
oldtmpf = NamedTemporaryFile()
82
newtmpf = NamedTemporaryFile()
85
# TODO: perhaps a special case for comparing to or from the empty
86
# sequence; can just use /dev/null on Unix
88
# TODO: if either of the files being compared already exists as a
89
# regular named file (e.g. in the working directory) then we can
90
# compare directly to that, rather than copying it.
92
# TODO: Set the labels appropriately
94
oldtmpf.writelines(oldlines)
95
newtmpf.writelines(newlines)
100
system('diff -u --label %s %s --label %s %s' % (old_label, oldtmpf.name, new_label, newtmpf.name))
102
oldtmpf.close() # and delete
107
def diff_file(old_label, oldlines, new_label, newlines, to_file):
109
differ = external_diff
111
differ = internal_diff
113
differ(old_label, oldlines, new_label, newlines, to_file)
69
117
def show_diff(b, revision, specific_files):
105
153
for path, file_id, kind in delta.removed:
106
154
print '*** removed %s %r' % (kind, path)
107
155
if kind == 'file':
108
_diff_one(old_tree.get_file(file_id).readlines(),
111
fromfile=old_label + path,
156
diff_file(old_label + path,
157
old_tree.get_file(file_id).readlines(),
114
162
for path, file_id, kind in delta.added:
115
163
print '*** added %s %r' % (kind, path)
116
164
if kind == 'file':
118
new_tree.get_file(file_id).readlines(),
121
tofile=new_label + path)
168
new_tree.get_file(file_id).readlines(),
123
171
for old_path, new_path, file_id, kind, text_modified in delta.renamed:
124
172
print '*** renamed %s %r => %r' % (kind, old_path, new_path)
125
173
if text_modified:
126
_diff_one(old_tree.get_file(file_id).readlines(),
127
new_tree.get_file(file_id).readlines(),
129
fromfile=old_label + old_path,
130
tofile=new_label + new_path)
174
diff_file(old_label + old_path,
175
old_tree.get_file(file_id).readlines(),
176
new_label + new_path,
177
new_tree.get_file(file_id).readlines(),
132
180
for path, file_id, kind in delta.modified:
133
181
print '*** modified %s %r' % (kind, path)
134
182
if kind == 'file':
135
_diff_one(old_tree.get_file(file_id).readlines(),
136
new_tree.get_file(file_id).readlines(),
138
fromfile=old_label + path,
139
tofile=new_label + path)
183
diff_file(old_label + path,
184
old_tree.get_file(file_id).readlines(),
186
new_tree.get_file(file_id).readlines(),