1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from sets import Set, ImmutableSet
from trace import mutter
from errors import BzrError
def _diff_one(oldlines, newlines, to_file, **kw):
import difflib
# FIXME: difflib is wrong if there is no trailing newline.
# The syntax used by patch seems to be "\ No newline at
# end of file" following the last diff line from that
# file. This is not trivial to insert into the
# unified_diff output and it might be better to just fix
# or replace that function.
# In the meantime we at least make sure the patch isn't
# mangled.
# Special workaround for Python2.3, where difflib fails if
# both sequences are empty.
if not oldlines and not newlines:
return
nonl = False
if oldlines and (oldlines[-1][-1] != '\n'):
oldlines[-1] += '\n'
nonl = True
if newlines and (newlines[-1][-1] != '\n'):
newlines[-1] += '\n'
nonl = True
ud = difflib.unified_diff(oldlines, newlines, **kw)
# work-around for difflib being too smart for its own good
# if /dev/null is "1,0", patch won't recognize it as /dev/null
if not oldlines:
ud = list(ud)
ud[2] = ud[2].replace('-1,0', '-0,0')
elif not newlines:
ud = list(ud)
ud[2] = ud[2].replace('+1,0', '+0,0')
to_file.writelines(ud)
if nonl:
print >>to_file, "\\ No newline at end of file"
print >>to_file
def show_diff(b, revision, specific_files):
import sys
if revision == None:
old_tree = b.basis_tree()
else:
old_tree = b.revision_tree(b.lookup_revision(revision))
new_tree = b.working_tree()
# TODO: Options to control putting on a prefix or suffix, perhaps as a format string
old_label = ''
new_label = ''
DEVNULL = '/dev/null'
# Windows users, don't panic about this filename -- it is a
# special signal to GNU patch that the file should be created or
# deleted respectively.
# TODO: Generation of pseudo-diffs for added/deleted files could
# be usefully made into a much faster special case.
delta = compare_trees(old_tree, new_tree, want_unchanged=False,
specific_files=specific_files)
for path, file_id, kind in delta.removed:
print '*** removed %s %r' % (kind, path)
if kind == 'file':
_diff_one(old_tree.get_file(file_id).readlines(),
[],
sys.stdout,
fromfile=old_label + path,
tofile=DEVNULL)
for path, file_id, kind in delta.added:
print '*** added %s %r' % (kind, path)
if kind == 'file':
_diff_one([],
new_tree.get_file(file_id).readlines(),
sys.stdout,
fromfile=DEVNULL,
tofile=new_label + path)
for old_path, new_path, file_id, kind, text_modified in delta.renamed:
print '*** renamed %s %r => %r' % (kind, old_path, new_path)
if text_modified:
_diff_one(old_tree.get_file(file_id).readlines(),
new_tree.get_file(file_id).readlines(),
sys.stdout,
fromfile=old_label + old_path,
tofile=new_label + new_path)
for path, file_id, kind in delta.modified:
print '*** modified %s %r' % (kind, path)
if kind == 'file':
_diff_one(old_tree.get_file(file_id).readlines(),
new_tree.get_file(file_id).readlines(),
sys.stdout,
fromfile=old_label + path,
tofile=new_label + path)
class TreeDelta:
"""Describes changes from one tree to another.
Contains four lists:
added
(path, id, kind)
removed
(path, id, kind)
renamed
(oldpath, newpath, id, kind, text_modified)
modified
(path, id, kind)
unchanged
(path, id, kind)
Each id is listed only once.
Files that are both modified and renamed are listed only in
renamed, with the text_modified flag true.
The lists are normally sorted when the delta is created.
"""
def __init__(self):
self.added = []
self.removed = []
self.renamed = []
self.modified = []
self.unchanged = []
def show(self, to_file, show_ids=False, show_unchanged=False):
def show_list(files):
for path, fid, kind in files:
if kind == 'directory':
path += '/'
elif kind == 'symlink':
path += '@'
if show_ids:
print >>to_file, ' %-30s %s' % (path, fid)
else:
print >>to_file, ' ', path
if self.removed:
print >>to_file, 'removed:'
show_list(self.removed)
if self.added:
print >>to_file, 'added:'
show_list(self.added)
if self.renamed:
print >>to_file, 'renamed:'
for oldpath, newpath, fid, kind, text_modified in self.renamed:
if show_ids:
print >>to_file, ' %s => %s %s' % (oldpath, newpath, fid)
else:
print >>to_file, ' %s => %s' % (oldpath, newpath)
if self.modified:
print >>to_file, 'modified:'
show_list(self.modified)
if show_unchanged and self.unchanged:
print >>to_file, 'unchanged:'
show_list(self.unchanged)
def compare_trees(old_tree, new_tree, want_unchanged, specific_files=None):
"""Describe changes from one tree to another.
Returns a TreeDelta with details of added, modified, renamed, and
deleted entries.
The root entry is specifically exempt.
This only considers versioned files.
want_unchanged
If true, also list files unchanged from one version to the next.
specific_files
If true, only check for changes to specified files.
"""
old_inv = old_tree.inventory
new_inv = new_tree.inventory
delta = TreeDelta()
mutter('start compare_trees')
if specific_files:
specific_files = ImmutableSet(specific_files)
for file_id in old_tree:
if file_id in new_tree:
kind = old_inv.get_file_kind(file_id)
assert kind == new_inv.get_file_kind(file_id)
assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
'invalid file kind %r' % kind
if kind == 'root_directory':
continue
old_path = old_inv.id2path(file_id)
new_path = new_inv.id2path(file_id)
if specific_files:
if (old_path not in specific_files
and new_path not in specific_files):
continue
if kind == 'file':
old_sha1 = old_tree.get_file_sha1(file_id)
new_sha1 = new_tree.get_file_sha1(file_id)
text_modified = (old_sha1 != new_sha1)
else:
## mutter("no text to check for %r %r" % (file_id, kind))
text_modified = False
# TODO: Can possibly avoid calculating path strings if the
# two files are unchanged and their names and parents are
# the same and the parents are unchanged all the way up.
# May not be worthwhile.
if old_path != new_path:
delta.renamed.append((old_path, new_path, file_id, kind,
text_modified))
elif text_modified:
delta.modified.append((new_path, file_id, kind))
elif want_unchanged:
delta.unchanged.append((new_path, file_id, kind))
else:
delta.removed.append((old_inv.id2path(file_id), file_id, kind))
mutter('start looking for new files')
for file_id in new_inv:
if file_id in old_inv:
continue
new_path = new_inv.id2path(file_id)
if specific_files:
if new_path not in specific_files:
continue
kind = new_inv.get_file_kind(file_id)
delta.added.append((new_path, file_id, kind))
delta.removed.sort()
delta.added.sort()
delta.renamed.sort()
delta.modified.sort()
delta.unchanged.sort()
return delta
|