293
@deprecated_function(deprecated_in((2, 2, 0)))
294
def get_trees_and_branches_to_diff(path_list, revision_specs, old_url, new_url,
296
"""Get the trees and specific files to diff given a list of paths.
298
This method works out the trees to be diff'ed and the files of
299
interest within those trees.
302
the list of arguments passed to the diff command
303
:param revision_specs:
304
Zero, one or two RevisionSpecs from the diff command line,
305
saying what revisions to compare.
307
The url of the old branch or tree. If None, the tree to use is
308
taken from the first path, if any, or the current working tree.
310
The url of the new branch or tree. If None, the tree to use is
311
taken from the first path, if any, or the current working tree.
313
if True and a view is set, apply the view or check that the paths
316
a tuple of (old_tree, new_tree, old_branch, new_branch,
317
specific_files, extra_trees) where extra_trees is a sequence of
318
additional trees to search in for file-ids. The trees and branches
321
op = cleanup.OperationWithCleanups(get_trees_and_branches_to_diff_locked)
322
return op.run_simple(path_list, revision_specs, old_url, new_url,
323
op.add_cleanup, apply_view=apply_view)
275
@deprecated_function(one_zero)
276
def diff_cmd_helper(tree, specific_files, external_diff_options,
277
old_revision_spec=None, new_revision_spec=None,
279
old_label='a/', new_label='b/'):
280
"""Helper for cmd_diff.
285
:param specific_files:
286
The specific files to compare, or None
288
:param external_diff_options:
289
If non-None, run an external diff, and pass it these options
291
:param old_revision_spec:
292
If None, use basis tree as old revision, otherwise use the tree for
293
the specified revision.
295
:param new_revision_spec:
296
If None, use working tree as new revision, otherwise use the tree for
297
the specified revision.
326
def get_trees_and_branches_to_diff_locked(
327
path_list, revision_specs, old_url, new_url, add_cleanup, apply_view=True):
299
:param revision_specs:
300
Zero, one or two RevisionSpecs from the command line, saying what revisions
301
to compare. This can be passed as an alternative to the old_revision_spec
302
and new_revision_spec parameters.
304
The more general form is show_diff_trees(), where the caller
305
supplies any two trees.
308
# TODO: perhaps remove the old parameters old_revision_spec and
309
# new_revision_spec, since this is only really for use from cmd_diff and
310
# it now always passes through a sequence of revision_specs -- mbp
315
revision = spec.in_store(tree.branch)
317
revision = spec.in_store(None)
318
revision_id = revision.rev_id
319
branch = revision.branch
320
return branch.repository.revision_tree(revision_id)
322
if revision_specs is not None:
323
assert (old_revision_spec is None
324
and new_revision_spec is None)
325
if len(revision_specs) > 0:
326
old_revision_spec = revision_specs[0]
327
if len(revision_specs) > 1:
328
new_revision_spec = revision_specs[1]
330
if old_revision_spec is None:
331
old_tree = tree.basis_tree()
333
old_tree = spec_tree(old_revision_spec)
335
if (new_revision_spec is None
336
or new_revision_spec.spec is None):
339
new_tree = spec_tree(new_revision_spec)
341
if new_tree is not tree:
342
extra_trees = (tree,)
346
return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
347
external_diff_options,
348
old_label=old_label, new_label=new_label,
349
extra_trees=extra_trees)
352
def _get_trees_to_diff(path_list, revision_specs, old_url, new_url):
328
353
"""Get the trees and specific files to diff given a list of paths.
330
355
This method works out the trees to be diff'ed and the files of
409
412
if new_url != old_url:
410
413
working_tree, branch, relpath = \
411
414
bzrdir.BzrDir.open_containing_tree_or_branch(new_url)
412
lock_tree_or_branch(working_tree, branch)
413
if consider_relpath and relpath != '':
414
if working_tree is not None and apply_view:
415
views.check_path_in_view(working_tree, relpath)
416
416
specific_files.append(relpath)
417
417
new_tree = _get_tree_to_diff(new_revision_spec, working_tree, branch,
418
418
basis_is_default=working_tree is None)
421
420
# Get the specific files (all files is None, no files is [])
422
421
if make_paths_wt_relative and working_tree is not None:
424
from bzrlib.builtins import safe_relpath_files
425
other_paths = safe_relpath_files(working_tree, other_paths,
426
apply_view=apply_view)
427
except errors.FileInWrongBranch:
428
raise errors.BzrCommandError("Files are in different branches")
422
other_paths = _relative_paths_in_tree(working_tree, other_paths)
429
423
specific_files.extend(other_paths)
430
424
if len(specific_files) == 0:
431
425
specific_files = None
432
if (working_tree is not None and working_tree.supports_views()
434
view_files = working_tree.views.lookup_view()
436
specific_files = view_files
437
view_str = views.view_display_str(view_files)
438
note("*** Ignoring files outside view. View is %s" % view_str)
440
427
# Get extra trees that ought to be searched for file-ids
441
428
extra_trees = None
442
429
if working_tree is not None and working_tree not in (old_tree, new_tree):
443
430
extra_trees = (working_tree,)
444
return old_tree, new_tree, old_branch, new_branch, specific_files, extra_trees
431
return old_tree, new_tree, specific_files, extra_trees
447
434
def _get_tree_to_diff(spec, tree=None, branch=None, basis_is_default=True):
455
442
return branch.basis_tree()
458
return spec.as_tree(branch)
445
revision = spec.in_store(branch)
446
revision_id = revision.rev_id
447
rev_branch = revision.branch
448
return rev_branch.repository.revision_tree(revision_id)
451
def _relative_paths_in_tree(tree, paths):
452
"""Get the relative paths within a working tree.
454
Each path may be either an absolute path or a path relative to the
455
current working directory.
458
for filename in paths:
460
result.append(tree.relpath(osutils.dereference_path(filename)))
461
except errors.PathNotChild:
462
raise errors.BzrCommandError("Files are in different branches")
461
466
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
462
467
external_diff_options=None,
463
468
old_label='a/', new_label='b/',
464
469
extra_trees=None,
465
path_encoding='utf8',
470
path_encoding='utf8'):
468
471
"""Show in text form the changes from one tree to another.
470
:param to_file: The output stream.
471
:param specific_files:Include only changes to these files - None for all
473
:param external_diff_options: If set, use an external GNU diff and pass
475
:param extra_trees: If set, more Trees to use for looking up file ids
476
:param path_encoding: If set, the path will be encoded as specified,
477
otherwise is supposed to be utf8
478
:param format_cls: Formatter class (DiffTree subclass)
477
Include only changes to these files - None for all changes.
479
external_diff_options
480
If set, use an external GNU diff and pass these options.
483
If set, more Trees to use for looking up file ids
486
If set, the path will be encoded as specified, otherwise is supposed
480
if format_cls is None:
481
format_cls = DiffTree
482
489
old_tree.lock_read()
484
491
if extra_trees is not None:
503
510
def _patch_header_date(tree, file_id, path):
504
511
"""Returns a timestamp suitable for use in a patch header."""
506
mtime = tree.get_file_mtime(file_id, path)
507
except errors.FileTimestampUnavailable:
512
mtime = tree.get_file_mtime(file_id, path)
513
assert mtime is not None, \
514
"got an mtime of None for file-id %s, path %s in tree %s" % (
509
516
return timestamp.format_patch_date(mtime)
512
def get_executable_change(old_is_x, new_is_x):
513
descr = { True:"+x", False:"-x", None:"??" }
514
if old_is_x != new_is_x:
515
return ["%s to %s" % (descr[old_is_x], descr[new_is_x],)]
519
def _raise_if_nonexistent(paths, old_tree, new_tree):
520
"""Complain if paths are not in either inventory or tree.
522
It's OK with the files exist in either tree's inventory, or
523
if they exist in the tree but are not versioned.
525
This can be used by operations such as bzr status that can accept
526
unknown or ignored files.
528
mutter("check paths: %r", paths)
531
s = old_tree.filter_unversioned_files(paths)
532
s = new_tree.filter_unversioned_files(s)
533
s = [path for path in s if not new_tree.has_filename(path)]
535
raise errors.PathsDoNotExist(sorted(s))
538
def get_prop_change(meta_modified):
540
return " (properties changed)"
520
545
class DiffPath(object):
701
718
:param to_file_id: The id of the file in the to tree. This may refer
702
719
to a different file from from_file_id. If None,
703
720
the file is not present in the to tree.
704
:param from_path: The path in the from tree or None if unknown.
705
:param to_path: The path in the to tree or None if unknown.
707
def _get_text(tree, file_id, path):
722
def _get_text(tree, file_id):
708
723
if file_id is not None:
709
return tree.get_file(file_id, path).readlines()
724
return tree.get_file(file_id).readlines()
713
from_text = _get_text(self.old_tree, from_file_id, from_path)
714
to_text = _get_text(self.new_tree, to_file_id, to_path)
728
from_text = _get_text(self.old_tree, from_file_id)
729
to_text = _get_text(self.new_tree, to_file_id)
715
730
self.text_differ(from_label, from_text, to_label, to_text,
717
732
except errors.BinaryFile:
721
736
return self.CHANGED
724
class DiffFromTool(DiffPath):
726
def __init__(self, command_template, old_tree, new_tree, to_file,
727
path_encoding='utf-8'):
728
DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
729
self.command_template = command_template
730
self._root = osutils.mkdtemp(prefix='bzr-diff-')
733
def from_string(klass, command_string, old_tree, new_tree, to_file,
734
path_encoding='utf-8'):
735
command_template = cmdline.split(command_string)
736
if '@' not in command_string:
737
command_template.extend(['@old_path', '@new_path'])
738
return klass(command_template, old_tree, new_tree, to_file,
742
def make_from_diff_tree(klass, command_string):
743
def from_diff_tree(diff_tree):
744
return klass.from_string(command_string, diff_tree.old_tree,
745
diff_tree.new_tree, diff_tree.to_file)
746
return from_diff_tree
748
def _get_command(self, old_path, new_path):
749
my_map = {'old_path': old_path, 'new_path': new_path}
750
return [AtTemplate(t).substitute(my_map) for t in
751
self.command_template]
753
def _execute(self, old_path, new_path):
754
command = self._get_command(old_path, new_path)
756
proc = subprocess.Popen(command, stdout=subprocess.PIPE,
759
if e.errno == errno.ENOENT:
760
raise errors.ExecutableMissing(command[0])
763
self.to_file.write(proc.stdout.read())
766
def _try_symlink_root(self, tree, prefix):
767
if (getattr(tree, 'abspath', None) is None
768
or not osutils.host_os_dereferences_symlinks()):
771
os.symlink(tree.abspath(''), osutils.pathjoin(self._root, prefix))
773
if e.errno != errno.EEXIST:
777
def _write_file(self, file_id, tree, prefix, relpath, force_temp=False,
779
if not force_temp and isinstance(tree, WorkingTree):
780
return tree.abspath(tree.id2path(file_id))
782
full_path = osutils.pathjoin(self._root, prefix, relpath)
783
if not force_temp and self._try_symlink_root(tree, prefix):
785
parent_dir = osutils.dirname(full_path)
787
os.makedirs(parent_dir)
789
if e.errno != errno.EEXIST:
791
source = tree.get_file(file_id, relpath)
793
target = open(full_path, 'wb')
795
osutils.pumpfile(source, target)
801
mtime = tree.get_file_mtime(file_id)
802
except errors.FileTimestampUnavailable:
805
os.utime(full_path, (mtime, mtime))
807
osutils.make_readonly(full_path)
810
def _prepare_files(self, file_id, old_path, new_path, force_temp=False,
811
allow_write_new=False):
812
old_disk_path = self._write_file(file_id, self.old_tree, 'old',
813
old_path, force_temp)
814
new_disk_path = self._write_file(file_id, self.new_tree, 'new',
815
new_path, force_temp,
816
allow_write=allow_write_new)
817
return old_disk_path, new_disk_path
821
osutils.rmtree(self._root)
823
if e.errno != errno.ENOENT:
824
mutter("The temporary directory \"%s\" was not "
825
"cleanly removed: %s." % (self._root, e))
827
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
828
if (old_kind, new_kind) != ('file', 'file'):
829
return DiffPath.CANNOT_DIFF
830
(old_disk_path, new_disk_path) = self._prepare_files(
831
file_id, old_path, new_path)
832
self._execute(old_disk_path, new_disk_path)
834
def edit_file(self, file_id):
835
"""Use this tool to edit a file.
837
A temporary copy will be edited, and the new contents will be
840
:param file_id: The id of the file to edit.
841
:return: The new contents of the file.
843
old_path = self.old_tree.id2path(file_id)
844
new_path = self.new_tree.id2path(file_id)
845
new_abs_path = self._prepare_files(file_id, old_path, new_path,
846
allow_write_new=True,
848
command = self._get_command(osutils.pathjoin('old', old_path),
849
osutils.pathjoin('new', new_path))
850
subprocess.call(command, cwd=self._root)
851
new_file = open(new_abs_path, 'r')
853
return new_file.read()
858
739
class DiffTree(object):
859
740
"""Provides textual representations of the difference between two trees.
926
803
diff_file = internal_diff
927
804
diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
928
805
old_label, new_label, diff_file)
929
return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
806
return klass(old_tree, new_tree, to_file, path_encoding, diff_text)
932
808
def show_diff(self, specific_files, extra_trees=None):
933
809
"""Write tree diff to self.to_file
935
:param specific_files: the specific files to compare (recursive)
811
:param sepecific_files: the specific files to compare (recursive)
936
812
:param extra_trees: extra trees to use for mapping paths to file_ids
939
return self._show_diff(specific_files, extra_trees)
941
for differ in self.differs:
944
def _show_diff(self, specific_files, extra_trees):
945
814
# TODO: Generation of pseudo-diffs for added/deleted files could
946
815
# be usefully made into a much faster special case.
947
iterator = self.new_tree.iter_changes(self.old_tree,
948
specific_files=specific_files,
949
extra_trees=extra_trees,
950
require_versioned=True)
817
delta = self.new_tree.changes_from(self.old_tree,
818
specific_files=specific_files,
819
extra_trees=extra_trees, require_versioned=True)
952
def changes_key(change):
953
old_path, new_path = change[1]
958
def get_encoded_path(path):
960
return path.encode(self.path_encoding, "replace")
961
for (file_id, paths, changed_content, versioned, parent, name, kind,
962
executable) in sorted(iterator, key=changes_key):
963
# The root does not get diffed, and items with no known kind (that
964
# is, missing) in both trees are skipped as well.
965
if parent == (None, None) or kind == (None, None):
967
oldpath, newpath = paths
968
oldpath_encoded = get_encoded_path(paths[0])
969
newpath_encoded = get_encoded_path(paths[1])
970
old_present = (kind[0] is not None and versioned[0])
971
new_present = (kind[1] is not None and versioned[1])
972
renamed = (parent[0], name[0]) != (parent[1], name[1])
974
properties_changed = []
975
properties_changed.extend(get_executable_change(executable[0], executable[1]))
977
if properties_changed:
978
prop_str = " (properties changed: %s)" % (", ".join(properties_changed),)
982
if (old_present, new_present) == (True, False):
983
self.to_file.write("=== removed %s '%s'\n" %
984
(kind[0], oldpath_encoded))
986
elif (old_present, new_present) == (False, True):
987
self.to_file.write("=== added %s '%s'\n" %
988
(kind[1], newpath_encoded))
991
self.to_file.write("=== renamed %s '%s' => '%s'%s\n" %
992
(kind[0], oldpath_encoded, newpath_encoded, prop_str))
994
# if it was produced by iter_changes, it must be
995
# modified *somehow*, either content or execute bit.
996
self.to_file.write("=== modified %s '%s'%s\n" % (kind[0],
997
newpath_encoded, prop_str))
999
self._diff(file_id, oldpath, newpath, kind[0], kind[1])
822
for path, file_id, kind in delta.removed:
824
path_encoded = path.encode(self.path_encoding, "replace")
825
self.to_file.write("=== removed %s '%s'\n" % (kind, path_encoded))
826
self.diff(file_id, path, path)
828
for path, file_id, kind in delta.added:
830
path_encoded = path.encode(self.path_encoding, "replace")
831
self.to_file.write("=== added %s '%s'\n" % (kind, path_encoded))
832
self.diff(file_id, path, path)
833
for (old_path, new_path, file_id, kind,
834
text_modified, meta_modified) in delta.renamed:
836
prop_str = get_prop_change(meta_modified)
837
oldpath_encoded = old_path.encode(self.path_encoding, "replace")
838
newpath_encoded = new_path.encode(self.path_encoding, "replace")
839
self.to_file.write("=== renamed %s '%s' => '%s'%s\n" % (kind,
840
oldpath_encoded, newpath_encoded, prop_str))
842
self.diff(file_id, old_path, new_path)
843
for path, file_id, kind, text_modified, meta_modified in\
846
prop_str = get_prop_change(meta_modified)
847
path_encoded = path.encode(self.path_encoding, "replace")
848
self.to_file.write("=== modified %s '%s'%s\n" % (kind,
849
path_encoded, prop_str))
850
# The file may be in a different location in the old tree (because
851
# the containing dir was renamed, but the file itself was not)
853
old_path = self.old_tree.id2path(file_id)
854
self.diff(file_id, old_path, path)
1003
855
return has_changes
1005
857
def diff(self, file_id, old_path, new_path):