~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Aaron Bentley
  • Date: 2009-08-14 16:12:47 UTC
  • mto: (4603.1.22 shelve-editor)
  • mto: This revision was merged to the branch mainline in revision 4795.
  • Revision ID: aaron@aaronbentley.com-20090814161247-33pga4kq3qdzjn0k
Implement DiffFromTool.edit_file

Show diffs side-by-side

added added

removed removed

Lines of Context:
664
664
        DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
665
665
        self.command_template = command_template
666
666
        self._root = osutils.mkdtemp(prefix='bzr-diff-')
667
 
        self.force_temp = False
668
 
        self.allow_write = False
669
667
 
670
668
    @classmethod
671
669
    def from_string(klass, command_string, old_tree, new_tree, to_file,
710
708
                raise
711
709
        return True
712
710
 
713
 
    def _write_file(self, file_id, tree, prefix, relpath):
 
711
    def _write_file(self, file_id, tree, prefix, relpath, force_temp=False,
 
712
                    allow_write=False):
714
713
        full_path = osutils.pathjoin(self._root, prefix, relpath)
715
 
        if not self.force_temp and self._try_symlink_root(tree, prefix):
 
714
        if not force_temp and self._try_symlink_root(tree, prefix):
716
715
            return full_path
717
716
        parent_dir = osutils.dirname(full_path)
718
717
        try:
729
728
                target.close()
730
729
        finally:
731
730
            source.close()
732
 
        if not self.allow_write:
 
731
        if not allow_write:
733
732
            osutils.make_readonly(full_path)
734
733
        mtime = tree.get_file_mtime(file_id)
735
734
        os.utime(full_path, (mtime, mtime))
736
735
        return full_path
737
736
 
738
 
    def _prepare_files(self, file_id, old_path, new_path):
 
737
    def _prepare_files(self, file_id, old_path, new_path, force_temp=False,
 
738
                       allow_write_new=False):
739
739
        old_disk_path = self._write_file(file_id, self.old_tree, 'old',
740
 
                                         old_path)
 
740
                                         old_path, force_temp)
741
741
        new_disk_path = self._write_file(file_id, self.new_tree, 'new',
742
 
                                         new_path)
 
742
                                         new_path, force_temp,
 
743
                                         allow_write=allow_write_new)
743
744
        return old_disk_path, new_disk_path
744
745
 
745
746
    def finish(self):
757
758
        self._execute(osutils.pathjoin('old', old_path),
758
759
                      self.get_new_path(new_path))
759
760
 
 
761
    def edit_file(self, file_id):
 
762
        """Use this tool to edit a file.
 
763
 
 
764
        A temporary copy will be edited, and the new contents will be
 
765
        returned.
 
766
 
 
767
        :param file_id: The id of the file to edit.
 
768
        :return: The new contents of the file.
 
769
        """
 
770
        old_path = self.old_tree.id2path(file_id)
 
771
        new_path = self.new_tree.id2path(file_id)
 
772
        new_abs_path = self._prepare_files(file_id, old_path, new_path,
 
773
                                           allow_write_new=True,
 
774
                                           force_temp=True)[1]
 
775
        self._execute(osutils.pathjoin('old', old_path),
 
776
                      self.get_new_path(new_path))
 
777
        new_file = open(new_abs_path, 'r')
 
778
        try:
 
779
            return new_file.read()
 
780
        finally:
 
781
            new_file.close()
 
782
 
760
783
    def get_new_path(self, new_path, abspath=False):
761
784
        if abspath:
762
785
            components = [self._root]
765
788
        components.extend(['new', new_path])
766
789
        return osutils.pathjoin(*components)
767
790
 
768
 
    def read_new_file(self, new_path):
769
 
        new_abs_path = self.get_new_path(new_path, abspath=True)
770
 
        new_file = open(new_abs_path, 'r')
771
 
        try:
772
 
            return new_file.read()
773
 
        finally:
774
 
            new_file.close()
775
 
 
776
791
 
777
792
class DiffTree(object):
778
793
    """Provides textual representations of the difference between two trees.