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 """Gclient-specific SCM-specific operations.""" | 5 """Gclient-specific SCM-specific operations.""" |
6 | 6 |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import posixpath | 9 import posixpath |
10 import re | 10 import re |
11 import sys | 11 import sys |
12 import time | 12 import time |
13 | 13 |
14 import gclient_utils | 14 import gclient_utils |
15 import scm | 15 import scm |
16 import subprocess2 | 16 import subprocess2 |
17 | 17 |
18 | 18 |
19 class DiffFilterer(object): | 19 class DiffFiltererWrapper(object): |
20 """Simple class which tracks which file is being diffed and | 20 """Simple base class which tracks which file is being diffed and |
21 replaces instances of its file name in the original and | 21 replaces instances of its file name in the original and |
22 working copy lines of the svn/git diff output.""" | 22 working copy lines of the svn/git diff output.""" |
23 index_string = "Index: " | 23 index_string = None |
24 original_prefix = "--- " | 24 original_prefix = "--- " |
25 working_prefix = "+++ " | 25 working_prefix = "+++ " |
26 | 26 |
27 def __init__(self, relpath): | 27 def __init__(self, relpath): |
28 # Note that we always use '/' as the path separator to be | 28 # Note that we always use '/' as the path separator to be |
29 # consistent with svn's cygwin-style output on Windows | 29 # consistent with svn's cygwin-style output on Windows |
30 self._relpath = relpath.replace("\\", "/") | 30 self._relpath = relpath.replace("\\", "/") |
31 self._current_file = "" | 31 self._current_file = None |
32 self._replacement_file = "" | |
33 | 32 |
34 def SetCurrentFile(self, current_file): | 33 def SetCurrentFile(self, current_file): |
35 self._current_file = current_file | 34 self._current_file = current_file |
36 # Note that we always use '/' as the path separator to be | 35 |
37 # consistent with svn's cygwin-style output on Windows | 36 @property |
38 self._replacement_file = posixpath.join(self._relpath, current_file) | 37 def _replacement_file(self): |
| 38 return posixpath.join(self._relpath, self._current_file) |
39 | 39 |
40 def _Replace(self, line): | 40 def _Replace(self, line): |
41 return line.replace(self._current_file, self._replacement_file) | 41 return line.replace(self._current_file, self._replacement_file) |
42 | 42 |
43 def Filter(self, line): | 43 def Filter(self, line): |
44 if (line.startswith(self.index_string)): | 44 if (line.startswith(self.index_string)): |
45 self.SetCurrentFile(line[len(self.index_string):]) | 45 self.SetCurrentFile(line[len(self.index_string):]) |
46 line = self._Replace(line) | 46 line = self._Replace(line) |
47 else: | 47 else: |
48 if (line.startswith(self.original_prefix) or | 48 if (line.startswith(self.original_prefix) or |
49 line.startswith(self.working_prefix)): | 49 line.startswith(self.working_prefix)): |
50 line = self._Replace(line) | 50 line = self._Replace(line) |
51 print(line) | 51 print(line) |
52 | 52 |
53 | 53 |
| 54 class SvnDiffFilterer(DiffFiltererWrapper): |
| 55 index_string = "Index: " |
| 56 |
| 57 |
| 58 class GitDiffFilterer(DiffFiltererWrapper): |
| 59 index_string = "diff --git " |
| 60 |
| 61 def SetCurrentFile(self, current_file): |
| 62 # Get filename by parsing "a/<filename> b/<filename>" |
| 63 self._current_file = current_file[:(len(current_file)/2)][2:] |
| 64 |
| 65 def _Replace(self, line): |
| 66 return re.sub("[a|b]/" + self._current_file, self._replacement_file, line) |
| 67 |
| 68 |
54 def ask_for_data(prompt): | 69 def ask_for_data(prompt): |
55 try: | 70 try: |
56 return raw_input(prompt) | 71 return raw_input(prompt) |
57 except KeyboardInterrupt: | 72 except KeyboardInterrupt: |
58 # Hide the exception. | 73 # Hide the exception. |
59 sys.exit(1) | 74 sys.exit(1) |
60 | 75 |
61 | 76 |
62 ### SCM abstraction layer | 77 ### SCM abstraction layer |
63 | 78 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 """Generates a patch file which can be applied to the root of the | 185 """Generates a patch file which can be applied to the root of the |
171 repository. | 186 repository. |
172 | 187 |
173 The patch file is generated from a diff of the merge base of HEAD and | 188 The patch file is generated from a diff of the merge base of HEAD and |
174 its upstream branch. | 189 its upstream branch. |
175 """ | 190 """ |
176 merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) | 191 merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) |
177 gclient_utils.CheckCallAndFilter( | 192 gclient_utils.CheckCallAndFilter( |
178 ['git', 'diff', merge_base], | 193 ['git', 'diff', merge_base], |
179 cwd=self.checkout_path, | 194 cwd=self.checkout_path, |
180 filter_fn=DiffFilterer(self.relpath).Filter) | 195 filter_fn=GitDiffFilterer(self.relpath).Filter) |
181 | 196 |
182 def update(self, options, args, file_list): | 197 def update(self, options, args, file_list): |
183 """Runs git to update or transparently checkout the working copy. | 198 """Runs git to update or transparently checkout the working copy. |
184 | 199 |
185 All updated files will be appended to file_list. | 200 All updated files will be appended to file_list. |
186 | 201 |
187 Raises: | 202 Raises: |
188 Error: if can't get URL for relative path. | 203 Error: if can't get URL for relative path. |
189 """ | 204 """ |
190 if args: | 205 if args: |
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
823 def pack(self, options, args, file_list): | 838 def pack(self, options, args, file_list): |
824 """Generates a patch file which can be applied to the root of the | 839 """Generates a patch file which can be applied to the root of the |
825 repository.""" | 840 repository.""" |
826 if not os.path.isdir(self.checkout_path): | 841 if not os.path.isdir(self.checkout_path): |
827 raise gclient_utils.Error('Directory %s is not present.' % | 842 raise gclient_utils.Error('Directory %s is not present.' % |
828 self.checkout_path) | 843 self.checkout_path) |
829 gclient_utils.CheckCallAndFilter( | 844 gclient_utils.CheckCallAndFilter( |
830 ['svn', 'diff', '-x', '--ignore-eol-style'] + args, | 845 ['svn', 'diff', '-x', '--ignore-eol-style'] + args, |
831 cwd=self.checkout_path, | 846 cwd=self.checkout_path, |
832 print_stdout=False, | 847 print_stdout=False, |
833 filter_fn=DiffFilterer(self.relpath).Filter) | 848 filter_fn=SvnDiffFilterer(self.relpath).Filter) |
834 | 849 |
835 def update(self, options, args, file_list): | 850 def update(self, options, args, file_list): |
836 """Runs svn to update or transparently checkout the working copy. | 851 """Runs svn to update or transparently checkout the working copy. |
837 | 852 |
838 All updated files will be appended to file_list. | 853 All updated files will be appended to file_list. |
839 | 854 |
840 Raises: | 855 Raises: |
841 Error: if can't get URL for relative path. | 856 Error: if can't get URL for relative path. |
842 """ | 857 """ |
843 # Only update if git or hg is not controlling the directory. | 858 # Only update if git or hg is not controlling the directory. |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1155 new_command.append('--force') | 1170 new_command.append('--force') |
1156 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1171 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1157 new_command.extend(('--accept', 'theirs-conflict')) | 1172 new_command.extend(('--accept', 'theirs-conflict')) |
1158 elif options.manually_grab_svn_rev: | 1173 elif options.manually_grab_svn_rev: |
1159 new_command.append('--force') | 1174 new_command.append('--force') |
1160 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1175 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1161 new_command.extend(('--accept', 'postpone')) | 1176 new_command.extend(('--accept', 'postpone')) |
1162 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1177 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1163 new_command.extend(('--accept', 'postpone')) | 1178 new_command.extend(('--accept', 'postpone')) |
1164 return new_command | 1179 return new_command |
OLD | NEW |