15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from sets import Set, ImmutableSet
18
20
from trace import mutter
19
21
from errors import BzrError
22
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
25
def _diff_one(oldlines, newlines, to_file, **kw):
25
28
# FIXME: difflib is wrong if there is no trailing newline.
47
50
newlines[-1] += '\n'
50
ud = difflib.unified_diff(oldlines, newlines,
51
fromfile=old_label, tofile=new_label)
53
ud = difflib.unified_diff(oldlines, newlines, **kw)
53
55
# work-around for difflib being too smart for its own good
54
56
# 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
oldtmpf.writelines(oldlines)
93
newtmpf.writelines(newlines)
98
system('diff -u --label %s %s --label %s %s' % (old_label, oldtmpf.name, new_label, newtmpf.name))
100
oldtmpf.close() # and delete
105
def diff_file(old_label, oldlines, new_label, newlines, to_file):
107
differ = external_diff
109
differ = internal_diff
111
differ(old_label, oldlines, new_label, newlines, to_file)
115
70
def show_diff(b, revision, specific_files):
123
78
new_tree = b.working_tree()
125
show_diff_trees(old_tree, new_tree, sys.stdout, specific_files)
129
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None):
130
"""Show in text form the changes from one tree to another.
133
If set, include only changes to these files.
136
80
# TODO: Options to control putting on a prefix or suffix, perhaps as a format string
151
95
for path, file_id, kind in delta.removed:
152
96
print '*** removed %s %r' % (kind, path)
153
97
if kind == 'file':
154
diff_file(old_label + path,
155
old_tree.get_file(file_id).readlines(),
98
_diff_one(old_tree.get_file(file_id).readlines(),
101
fromfile=old_label + path,
160
104
for path, file_id, kind in delta.added:
161
105
print '*** added %s %r' % (kind, path)
162
106
if kind == 'file':
166
new_tree.get_file(file_id).readlines(),
108
new_tree.get_file(file_id).readlines(),
111
tofile=new_label + path)
169
113
for old_path, new_path, file_id, kind, text_modified in delta.renamed:
170
114
print '*** renamed %s %r => %r' % (kind, old_path, new_path)
171
115
if text_modified:
172
diff_file(old_label + old_path,
173
old_tree.get_file(file_id).readlines(),
174
new_label + new_path,
175
new_tree.get_file(file_id).readlines(),
116
_diff_one(old_tree.get_file(file_id).readlines(),
117
new_tree.get_file(file_id).readlines(),
119
fromfile=old_label + old_path,
120
tofile=new_label + new_path)
178
122
for path, file_id, kind in delta.modified:
179
123
print '*** modified %s %r' % (kind, path)
180
124
if kind == 'file':
181
diff_file(old_label + path,
182
old_tree.get_file(file_id).readlines(),
184
new_tree.get_file(file_id).readlines(),
189
class TreeDelta(object):
125
_diff_one(old_tree.get_file(file_id).readlines(),
126
new_tree.get_file(file_id).readlines(),
128
fromfile=old_label + path,
129
tofile=new_label + path)
190
134
"""Describes changes from one tree to another.
192
136
Contains four lists:
216
160
self.modified = []
217
161
self.unchanged = []
220
def touches_file_id(self, file_id):
221
"""Return True if file_id is modified by this delta."""
222
for l in self.added, self.removed, self.modified:
226
for v in self.renamed:
232
163
def show(self, to_file, show_ids=False, show_unchanged=False):
233
164
def show_list(files):
234
165
for path, fid, kind in files:
337
268
elif want_unchanged:
338
269
delta.unchanged.append((new_path, file_id, kind))
340
kind = old_inv.get_file_kind(file_id)
341
271
old_path = old_inv.id2path(file_id)
342
272
if specific_files:
343
273
if not is_inside_any(specific_files, old_path):