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
import bzrlib.errors as errors
20
from bzrlib.patiencediff import unified_diff
21
import bzrlib.patiencediff
22
from bzrlib.symbol_versioning import *
23
from bzrlib.textfile import check_text_lines
17
24
from bzrlib.trace import mutter
18
from bzrlib.errors import BzrError
19
from bzrlib.delta import compare_trees
21
27
# TODO: Rather than building a changeset object, we should probably
22
28
# invoke callbacks on an object. That object can either accumulate a
23
29
# list, write them out directly, etc etc.
25
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
31
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
32
allow_binary=False, sequence_matcher=None):
28
33
# FIXME: difflib is wrong if there is no trailing newline.
29
34
# The syntax used by patch seems to be "\ No newline at
30
35
# end of file" following the last diff line from that
40
45
# both sequences are empty.
41
46
if not oldlines and not newlines:
49
if allow_binary is False:
50
check_text_lines(oldlines)
51
check_text_lines(newlines)
44
ud = difflib.unified_diff(oldlines, newlines,
45
fromfile=old_label, tofile=new_label)
53
if sequence_matcher is None:
54
sequence_matcher = bzrlib.patiencediff.PatienceSequenceMatcher
55
ud = unified_diff(oldlines, newlines,
56
fromfile=old_filename+'\t',
57
tofile=new_filename+'\t',
58
sequencematcher=sequence_matcher)
48
61
# work-around for difflib being too smart for its own good
158
175
output = sys.stdout
160
177
if from_spec is None:
161
old_tree = b.basis_tree()
178
old_tree = b.bzrdir.open_workingtree()
180
old_tree = old_tree = old_tree.basis_tree()
163
old_tree = b.revision_tree(from_spec.in_history(b).rev_id)
182
old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
165
184
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)
186
new_tree = b.bzrdir.open_workingtree()
188
new_tree = b2.bzrdir.open_workingtree()
190
new_tree = b.repository.revision_tree(revision2.in_history(b).rev_id)
192
return show_diff_trees(old_tree, new_tree, output, specific_files,
193
external_diff_options)
196
def diff_cmd_helper(tree, specific_files, external_diff_options,
197
old_revision_spec=None, new_revision_spec=None,
198
old_label='a/', new_label='b/'):
199
"""Helper for cmd_diff.
205
The specific files to compare, or None
207
external_diff_options
208
If non-None, run an external diff, and pass it these options
211
If None, use basis tree as old revision, otherwise use the tree for
212
the specified revision.
215
If None, use working tree as new revision, otherwise use the tree for
216
the specified revision.
218
The more general form is show_diff_trees(), where the caller
219
supplies any two trees.
224
revision_id = spec.in_store(tree.branch).rev_id
225
return tree.branch.repository.revision_tree(revision_id)
226
if old_revision_spec is None:
227
old_tree = tree.basis_tree()
229
old_tree = spec_tree(old_revision_spec)
231
if new_revision_spec is None:
234
new_tree = spec_tree(new_revision_spec)
236
return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
237
external_diff_options,
238
old_label=old_label, new_label=new_label)
175
241
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
176
external_diff_options=None):
242
external_diff_options=None,
243
old_label='a/', new_label='b/'):
177
244
"""Show in text form the changes from one tree to another.
182
249
external_diff_options
183
250
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
256
return _show_diff_trees(old_tree, new_tree, to_file,
257
specific_files, external_diff_options,
258
old_label=old_label, new_label=new_label)
265
def _show_diff_trees(old_tree, new_tree, to_file,
266
specific_files, external_diff_options,
267
old_label='a/', new_label='b/' ):
190
269
DEVNULL = '/dev/null'
191
270
# Windows users, don't panic about this filename -- it is a
204
285
diff_file = internal_diff
207
287
delta = compare_trees(old_tree, new_tree, want_unchanged=False,
208
288
specific_files=specific_files)
210
291
for path, file_id, kind in delta.removed:
211
293
print >>to_file, '=== removed %s %r' % (kind, path)
212
294
old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
213
295
DEVNULL, None, None, to_file)
214
296
for path, file_id, kind in delta.added:
215
298
print >>to_file, '=== added %s %r' % (kind, path)
216
299
new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
217
300
DEVNULL, None, None, to_file,
219
302
for (old_path, new_path, file_id, kind,
220
303
text_modified, meta_modified) in delta.renamed:
221
305
prop_str = get_prop_change(meta_modified)
222
306
print >>to_file, '=== renamed %s %r => %r%s' % (
223
kind, old_path, new_path, prop_str)
307
kind, old_path, new_path, prop_str)
224
308
_maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
225
309
new_label, new_path, new_tree,
226
310
text_modified, kind, to_file, diff_file)
227
311
for path, file_id, kind, text_modified, meta_modified in delta.modified:
228
313
prop_str = get_prop_change(meta_modified)
229
314
print >>to_file, '=== modified %s %r%s' % (kind, path, prop_str)
230
315
if text_modified:
231
316
_maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
232
317
new_label, path, new_tree,
233
318
True, kind, to_file, diff_file)
323
def _raise_if_doubly_unversioned(specific_files, old_tree, new_tree):
324
"""Complain if paths are not versioned in either tree."""
325
if not specific_files:
327
old_unversioned = old_tree.filter_unversioned_files(specific_files)
328
new_unversioned = new_tree.filter_unversioned_files(specific_files)
329
unversioned = old_unversioned.intersection(new_unversioned)
331
raise errors.PathsNotVersionedError(sorted(unversioned))
334
def _raise_if_nonexistent(paths, old_tree, new_tree):
335
"""Complain if paths are not in either inventory or tree.
337
It's OK with the files exist in either tree's inventory, or
338
if they exist in the tree but are not versioned.
340
This can be used by operations such as bzr status that can accept
341
unknown or ignored files.
343
mutter("check paths: %r", paths)
346
s = old_tree.filter_unversioned_files(paths)
347
s = new_tree.filter_unversioned_files(s)
348
s = [path for path in s if not new_tree.has_filename(path)]
350
raise errors.PathsDoNotExist(sorted(s))
236
353
def get_prop_change(meta_modified):
237
354
if meta_modified: