OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """SCM-specific utility classes.""" | 5 """SCM-specific utility classes.""" |
6 | 6 |
7 import cStringIO | 7 import cStringIO |
8 import glob | 8 import glob |
9 import logging | 9 import logging |
10 import os | 10 import os |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 @staticmethod | 774 @staticmethod |
775 def GenerateDiff(filenames, cwd, full_move, revision): | 775 def GenerateDiff(filenames, cwd, full_move, revision): |
776 """Returns a string containing the diff for the given file list. | 776 """Returns a string containing the diff for the given file list. |
777 | 777 |
778 The files in the list should either be absolute paths or relative to the | 778 The files in the list should either be absolute paths or relative to the |
779 given root. If no root directory is provided, the repository root will be | 779 given root. If no root directory is provided, the repository root will be |
780 used. | 780 used. |
781 The diff will always use relative paths. | 781 The diff will always use relative paths. |
782 """ | 782 """ |
783 assert isinstance(filenames, (list, tuple)) | 783 assert isinstance(filenames, (list, tuple)) |
784 # If the user specified a custom diff command in their svn config file, | |
785 # then it'll be used when we do svn diff, which we don't want to happen | |
786 # since we want the unified diff. | |
787 if SVN.AssertVersion("1.7")[0]: | |
788 # On svn >= 1.7, the "--internal-diff" flag will solve this. | |
789 return SVN._GenerateDiffInternal(filenames, cwd, full_move, revision, | |
790 ["diff", "--internal-diff"]) | |
791 else: | |
792 # On svn < 1.7, the "--internal-diff" flag doesn't exist. Using | |
793 # --diff-cmd=diff doesn't always work, since e.g. Windows cmd users may | |
794 # not have a "diff" executable in their path at all. So we use an empty | |
795 # temporary directory as the config directory, which bypasses any user | |
796 # settings for the diff-cmd. | |
797 bogus_dir = tempfile.mkdtemp() | |
798 try: | |
799 return SVN._GenerateDiffInternal(filenames, cwd, full_move, revision, | |
800 ["diff", "--config_dir", bogus_dir]) | |
801 finally: | |
802 gclient_utils.RemoveDirectory(bogus_dir) | |
803 | |
804 @staticmethod | |
805 def _GenerateDiffInternal(filenames, cwd, full_move, revision, diff_command): | |
806 root = os.path.normcase(os.path.join(cwd, '')) | 784 root = os.path.normcase(os.path.join(cwd, '')) |
807 def RelativePath(path, root): | 785 def RelativePath(path, root): |
808 """We must use relative paths.""" | 786 """We must use relative paths.""" |
809 if os.path.normcase(path).startswith(root): | 787 if os.path.normcase(path).startswith(root): |
810 return path[len(root):] | 788 return path[len(root):] |
811 return path | 789 return path |
812 # Cleanup filenames | 790 # If the user specified a custom diff command in their svn config file, |
813 filenames = [RelativePath(f, root) for f in filenames] | 791 # then it'll be used when we do svn diff, which we don't want to happen |
814 # Get information about the modified items (files and directories) | 792 # since we want the unified diff. Using --diff-cmd=diff doesn't always |
815 data = dict((f, SVN.CaptureLocalInfo([f], root)) for f in filenames) | 793 # work, since e.g. Windows cmd users may not have a "diff" executable in |
816 diffs = [] | 794 # their path at all. So we use an empty temporary directory as the config |
817 if full_move: | 795 # directory, which gets around these problems. |
818 # Eliminate modified files inside moved/copied directory. | 796 bogus_dir = tempfile.mkdtemp() |
819 for (filename, info) in data.iteritems(): | 797 command = ['diff', '--config-dir', bogus_dir] |
820 if SVN.IsMovedInfo(info) and info.get("Node Kind") == "directory": | 798 try: |
821 # Remove files inside the directory. | 799 # Cleanup filenames |
822 filenames = [f for f in filenames | 800 filenames = [RelativePath(f, root) for f in filenames] |
823 if not f.startswith(filename + os.path.sep)] | 801 # Get information about the modified items (files and directories) |
824 for filename in data.keys(): | 802 data = dict((f, SVN.CaptureLocalInfo([f], root)) for f in filenames) |
825 if not filename in filenames: | 803 diffs = [] |
826 # Remove filtered out items. | 804 if full_move: |
827 del data[filename] | 805 # Eliminate modified files inside moved/copied directory. |
828 else: | 806 for (filename, info) in data.iteritems(): |
829 metaheaders = [] | 807 if SVN.IsMovedInfo(info) and info.get("Node Kind") == "directory": |
830 for (filename, info) in data.iteritems(): | 808 # Remove files inside the directory. |
831 if SVN.IsMovedInfo(info): | 809 filenames = [f for f in filenames |
832 # for now, the most common case is a head copy, | 810 if not f.startswith(filename + os.path.sep)] |
833 # so let's just encode that as a straight up cp. | 811 for filename in data.keys(): |
834 srcurl = info.get('Copied From URL') | 812 if not filename in filenames: |
835 file_root = info.get('Repository Root') | 813 # Remove filtered out items. |
836 rev = int(info.get('Copied From Rev')) | 814 del data[filename] |
837 assert srcurl.startswith(file_root) | 815 else: |
838 src = srcurl[len(file_root)+1:] | 816 metaheaders = [] |
839 try: | 817 for (filename, info) in data.iteritems(): |
840 srcinfo = SVN.CaptureRemoteInfo(srcurl) | 818 if SVN.IsMovedInfo(info): |
841 except subprocess2.CalledProcessError, e: | 819 # for now, the most common case is a head copy, |
842 if not 'Not a valid URL' in e.stderr: | 820 # so let's just encode that as a straight up cp. |
843 raise | 821 srcurl = info.get('Copied From URL') |
844 # Assume the file was deleted. No idea how to figure out at which | 822 file_root = info.get('Repository Root') |
845 # revision the file was deleted. | 823 rev = int(info.get('Copied From Rev')) |
846 srcinfo = {'Revision': rev} | 824 assert srcurl.startswith(file_root) |
847 if (srcinfo.get('Revision') != rev and | 825 src = srcurl[len(file_root)+1:] |
848 SVN.Capture(diff_command + ['-r', '%d:head' % rev, srcurl], cwd)): | 826 try: |
849 metaheaders.append("#$ svn cp -r %d %s %s " | 827 srcinfo = SVN.CaptureRemoteInfo(srcurl) |
850 "### WARNING: note non-trunk copy\n" % | 828 except subprocess2.CalledProcessError, e: |
851 (rev, src, filename)) | 829 if not 'Not a valid URL' in e.stderr: |
852 else: | 830 raise |
853 metaheaders.append("#$ cp %s %s\n" % (src, | 831 # Assume the file was deleted. No idea how to figure out at which |
854 filename)) | 832 # revision the file was deleted. |
855 if metaheaders: | 833 srcinfo = {'Revision': rev} |
856 diffs.append("### BEGIN SVN COPY METADATA\n") | 834 if (srcinfo.get('Revision') != rev and |
857 diffs.extend(metaheaders) | 835 SVN.Capture(command + ['-r', '%d:head' % rev, srcurl], cwd)): |
858 diffs.append("### END SVN COPY METADATA\n") | 836 metaheaders.append("#$ svn cp -r %d %s %s " |
859 # Now ready to do the actual diff. | 837 "### WARNING: note non-trunk copy\n" % |
860 for filename in sorted(data): | 838 (rev, src, filename)) |
861 diffs.append(SVN._DiffItemInternal( | 839 else: |
862 filename, cwd, data[filename], diff_command, full_move, revision)) | 840 metaheaders.append("#$ cp %s %s\n" % (src, |
863 # Use StringIO since it can be messy when diffing a directory move with | 841 filename)) |
864 # full_move=True. | 842 |
865 buf = cStringIO.StringIO() | 843 if metaheaders: |
866 for d in filter(None, diffs): | 844 diffs.append("### BEGIN SVN COPY METADATA\n") |
867 buf.write(d) | 845 diffs.extend(metaheaders) |
868 result = buf.getvalue() | 846 diffs.append("### END SVN COPY METADATA\n") |
869 buf.close() | 847 # Now ready to do the actual diff. |
870 return result | 848 for filename in sorted(data): |
| 849 diffs.append(SVN._DiffItemInternal( |
| 850 filename, cwd, data[filename], command, full_move, revision)) |
| 851 # Use StringIO since it can be messy when diffing a directory move with |
| 852 # full_move=True. |
| 853 buf = cStringIO.StringIO() |
| 854 for d in filter(None, diffs): |
| 855 buf.write(d) |
| 856 result = buf.getvalue() |
| 857 buf.close() |
| 858 return result |
| 859 finally: |
| 860 gclient_utils.RemoveDirectory(bogus_dir) |
871 | 861 |
872 @staticmethod | 862 @staticmethod |
873 def _DiffItemInternal(filename, cwd, info, diff_command, full_move, revision): | 863 def _DiffItemInternal(filename, cwd, info, diff_command, full_move, revision): |
874 """Grabs the diff data.""" | 864 """Grabs the diff data.""" |
875 command = diff_command + [filename] | 865 command = diff_command + [filename] |
876 if revision: | 866 if revision: |
877 command.extend(['--revision', revision]) | 867 command.extend(['--revision', revision]) |
878 data = None | 868 data = None |
879 if SVN.IsMovedInfo(info): | 869 if SVN.IsMovedInfo(info): |
880 if full_move: | 870 if full_move: |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 # revert, like for properties. | 1068 # revert, like for properties. |
1079 if not os.path.isdir(cwd): | 1069 if not os.path.isdir(cwd): |
1080 # '.' was deleted. It's not worth continuing. | 1070 # '.' was deleted. It's not worth continuing. |
1081 return | 1071 return |
1082 try: | 1072 try: |
1083 SVN.Capture(['revert', file_status[1]], cwd=cwd) | 1073 SVN.Capture(['revert', file_status[1]], cwd=cwd) |
1084 except subprocess2.CalledProcessError: | 1074 except subprocess2.CalledProcessError: |
1085 if not os.path.exists(file_path): | 1075 if not os.path.exists(file_path): |
1086 continue | 1076 continue |
1087 raise | 1077 raise |
OLD | NEW |