14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from bzrlib.delta import compare_trees
18
from bzrlib.errors import BzrError
19
from bzrlib.symbol_versioning import *
17
20
from bzrlib.trace import mutter
18
from bzrlib.errors import BzrError
19
from bzrlib.delta import compare_trees
21
22
# TODO: Rather than building a changeset object, we should probably
22
23
# invoke callbacks on an object. That object can either accumulate a
23
24
# list, write them out directly, etc etc.
25
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
26
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file):
28
29
# FIXME: difflib is wrong if there is no trailing newline.
140
142
oldtmpf.close() # and delete
143
def show_diff(b, revision, specific_files, external_diff_options=None,
144
revision2=None, output=None):
146
@deprecated_function(zero_eight)
147
def show_diff(b, from_spec, specific_files, external_diff_options=None,
148
revision2=None, output=None, b2=None):
145
149
"""Shortcut for showing the diff to the working tree.
151
Please use show_diff_trees instead.
151
None for each, or otherwise the old revision to compare against.
157
None for 'basis tree', or otherwise the old revision to compare against.
153
159
The more general form is show_diff_trees(), where the caller
154
160
supplies any two trees.
158
164
output = sys.stdout
161
old_tree = b.basis_tree()
166
if from_spec is None:
167
old_tree = b.bzrdir.open_workingtree()
169
old_tree = old_tree = old_tree.basis_tree()
163
old_tree = b.revision_tree(revision.in_history(b).rev_id)
171
old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
165
173
if revision2 is None:
166
new_tree = b.working_tree()
168
new_tree = b.revision_tree(revision2.in_history(b).rev_id)
170
show_diff_trees(old_tree, new_tree, output, specific_files,
171
external_diff_options)
175
new_tree = b.bzrdir.open_workingtree()
177
new_tree = b2.bzrdir.open_workingtree()
179
new_tree = b.repository.revision_tree(revision2.in_history(b).rev_id)
181
return show_diff_trees(old_tree, new_tree, output, specific_files,
182
external_diff_options)
185
def diff_cmd_helper(tree, specific_files, external_diff_options,
186
old_revision_spec=None, new_revision_spec=None):
187
"""Helper for cmd_diff.
193
The specific files to compare, or None
195
external_diff_options
196
If non-None, run an external diff, and pass it these options
199
If None, use basis tree as old revision, otherwise use the tree for
200
the specified revision.
203
If None, use working tree as new revision, otherwise use the tree for
204
the specified revision.
206
The more general form is show_diff_trees(), where the caller
207
supplies any two trees.
212
revision_id = spec.in_store(tree.branch).rev_id
213
return tree.branch.repository.revision_tree(revision_id)
214
if old_revision_spec is None:
215
old_tree = tree.basis_tree()
217
old_tree = spec_tree(old_revision_spec)
219
if new_revision_spec is None:
222
new_tree = spec_tree(new_revision_spec)
224
return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
225
external_diff_options)
175
228
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
183
236
If set, use an external GNU diff and pass these options.
186
# TODO: Options to control putting on a prefix or suffix, perhaps as a format string
243
return _show_diff_trees(old_tree, new_tree, to_file,
244
specific_files, external_diff_options)
251
def _show_diff_trees(old_tree, new_tree, to_file,
252
specific_files, external_diff_options):
254
# TODO: Options to control putting on a prefix or suffix, perhaps
255
# as a format string?
190
259
DEVNULL = '/dev/null'
191
260
# Windows users, don't panic about this filename -- it is a
207
276
delta = compare_trees(old_tree, new_tree, want_unchanged=False,
208
277
specific_files=specific_files)
210
280
for path, file_id, kind in delta.removed:
211
print >>to_file, '=== removed %s %r' % (kind, path)
282
print >>to_file, '=== removed %s %r' % (kind, old_label + path)
212
283
old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
213
284
DEVNULL, None, None, to_file)
214
285
for path, file_id, kind in delta.added:
215
print >>to_file, '=== added %s %r' % (kind, path)
287
print >>to_file, '=== added %s %r' % (kind, new_label + path)
216
288
new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
217
289
DEVNULL, None, None, to_file,
219
291
for (old_path, new_path, file_id, kind,
220
292
text_modified, meta_modified) in delta.renamed:
221
294
prop_str = get_prop_change(meta_modified)
222
295
print >>to_file, '=== renamed %s %r => %r%s' % (
223
kind, old_path, new_path, prop_str)
296
kind, old_label + old_path, new_label + new_path, prop_str)
224
297
_maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
225
298
new_label, new_path, new_tree,
226
299
text_modified, kind, to_file, diff_file)
227
300
for path, file_id, kind, text_modified, meta_modified in delta.modified:
228
302
prop_str = get_prop_change(meta_modified)
229
print >>to_file, '=== modified %s %r%s' % (kind, path, prop_str)
303
print >>to_file, '=== modified %s %r%s' % (kind, old_label + path,
230
305
if text_modified:
231
306
_maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
232
307
new_label, path, new_tree,
233
308
True, kind, to_file, diff_file)
236
312
def get_prop_change(meta_modified):