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 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 = "" |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
index_string = None
so the code would fail if not
haitao.feng
2012/05/03 09:15:18
Done.
| |
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 = "" |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
None
haitao.feng
2012/05/03 09:15:18
Done.
| |
32 self._replacement_file = "" | 32 self._replacement_file = "" |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
self._replacement_file is not really needed, you c
haitao.feng
2012/05/03 09:15:18
Done.
| |
33 | 33 |
34 def SetCurrentFile(self, current_file): | 34 def SetCurrentFile(self, current_file): |
35 self._current_file = current_file | 35 pass |
36 # Note that we always use '/' as the path separator to be | |
37 # consistent with svn's cygwin-style output on Windows | |
38 self._replacement_file = posixpath.join(self._relpath, current_file) | |
39 | 36 |
40 def _Replace(self, line): | 37 def _Replace(self, line): |
41 return line.replace(self._current_file, self._replacement_file) | 38 pass |
42 | 39 |
43 def Filter(self, line): | 40 def Filter(self, line): |
44 if (line.startswith(self.index_string)): | 41 if (line.startswith(self.index_string)): |
45 self.SetCurrentFile(line[len(self.index_string):]) | 42 self.SetCurrentFile(line[len(self.index_string):]) |
46 line = self._Replace(line) | 43 line = self._Replace(line) |
47 else: | 44 else: |
48 if (line.startswith(self.original_prefix) or | 45 if (line.startswith(self.original_prefix) or |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
Note that this will fail to process git diff with
haitao.feng
2012/05/03 09:15:18
Thanks for the note. Do I need to submit a bug for
M-A Ruel
2012/05/03 13:37:07
It's used in the commit queue, which is in a separ
| |
49 line.startswith(self.working_prefix)): | 46 line.startswith(self.working_prefix)): |
50 line = self._Replace(line) | 47 line = self._Replace(line) |
51 print(line) | 48 print(line) |
52 | 49 |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
2 lines
haitao.feng
2012/05/03 09:15:18
Done.
| |
50 class SvnDiffFilterer(DiffFiltererWrapper): | |
51 """Simple class which tracks which file is being diffed and | |
52 replaces instances of its file name in the original and | |
53 working copy lines of the svn diff output.""" | |
54 index_string = "Index: " | |
55 | |
56 def __init__(self, relpath): | |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
You can remove this constructor entirely. The clas
haitao.feng
2012/05/03 09:15:18
Done.
| |
57 DiffFiltererWrapper.__init__(self, relpath) | |
58 | |
59 def SetCurrentFile(self, current_file): | |
60 self._current_file = current_file | |
61 self._replacement_file = posixpath.join(self._relpath, current_file) | |
62 | |
63 def _Replace(self, line): | |
64 return line.replace(self._current_file, self._replacement_file) | |
65 | |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
2 lines
haitao.feng
2012/05/03 09:15:18
Done.
| |
66 class GitDiffFilterer(DiffFiltererWrapper): | |
67 """Simple class which tracks which file is being diffed and | |
68 replaces instances of its file name in the original and | |
69 working copy lines of the git diff output.""" | |
70 index_string = "diff --git " | |
71 | |
72 def __init__(self, relpath): | |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
Same, remove
haitao.feng
2012/05/03 09:15:18
Done.
| |
73 DiffFiltererWrapper.__init__(self, relpath) | |
74 | |
75 def SetCurrentFile(self, current_file): | |
76 self._current_file = current_file.split()[0][2:] | |
77 self._replacement_file = posixpath.join(self._relpath, self._current_file) | |
78 | |
79 def _Replace(self, line): | |
80 result = line.replace("a/" + self._current_file, self._replacement_file) | |
Marc-Antoine Ruel (Google)
2012/05/03 01:30:53
that's a bit tricky though, if the file name is in
haitao.feng
2012/05/03 09:15:18
Replace with re.sub("[a|b]/" + self._current_file,
| |
81 return result.replace("b/" + self._current_file, self._replacement_file) | |
82 | |
53 | 83 |
54 def ask_for_data(prompt): | 84 def ask_for_data(prompt): |
55 try: | 85 try: |
56 return raw_input(prompt) | 86 return raw_input(prompt) |
57 except KeyboardInterrupt: | 87 except KeyboardInterrupt: |
58 # Hide the exception. | 88 # Hide the exception. |
59 sys.exit(1) | 89 sys.exit(1) |
60 | 90 |
61 | 91 |
62 ### SCM abstraction layer | 92 ### SCM abstraction layer |
(...skipping 107 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 | 200 """Generates a patch file which can be applied to the root of the |
171 repository. | 201 repository. |
172 | 202 |
173 The patch file is generated from a diff of the merge base of HEAD and | 203 The patch file is generated from a diff of the merge base of HEAD and |
174 its upstream branch. | 204 its upstream branch. |
175 """ | 205 """ |
176 merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) | 206 merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) |
177 gclient_utils.CheckCallAndFilter( | 207 gclient_utils.CheckCallAndFilter( |
178 ['git', 'diff', merge_base], | 208 ['git', 'diff', merge_base], |
179 cwd=self.checkout_path, | 209 cwd=self.checkout_path, |
180 filter_fn=DiffFilterer(self.relpath).Filter) | 210 filter_fn=GitDiffFilterer(self.relpath).Filter) |
181 | 211 |
182 def update(self, options, args, file_list): | 212 def update(self, options, args, file_list): |
183 """Runs git to update or transparently checkout the working copy. | 213 """Runs git to update or transparently checkout the working copy. |
184 | 214 |
185 All updated files will be appended to file_list. | 215 All updated files will be appended to file_list. |
186 | 216 |
187 Raises: | 217 Raises: |
188 Error: if can't get URL for relative path. | 218 Error: if can't get URL for relative path. |
189 """ | 219 """ |
190 if args: | 220 if args: |
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
823 def pack(self, options, args, file_list): | 853 def pack(self, options, args, file_list): |
824 """Generates a patch file which can be applied to the root of the | 854 """Generates a patch file which can be applied to the root of the |
825 repository.""" | 855 repository.""" |
826 if not os.path.isdir(self.checkout_path): | 856 if not os.path.isdir(self.checkout_path): |
827 raise gclient_utils.Error('Directory %s is not present.' % | 857 raise gclient_utils.Error('Directory %s is not present.' % |
828 self.checkout_path) | 858 self.checkout_path) |
829 gclient_utils.CheckCallAndFilter( | 859 gclient_utils.CheckCallAndFilter( |
830 ['svn', 'diff', '-x', '--ignore-eol-style'] + args, | 860 ['svn', 'diff', '-x', '--ignore-eol-style'] + args, |
831 cwd=self.checkout_path, | 861 cwd=self.checkout_path, |
832 print_stdout=False, | 862 print_stdout=False, |
833 filter_fn=DiffFilterer(self.relpath).Filter) | 863 filter_fn=SvnDiffFilterer(self.relpath).Filter) |
834 | 864 |
835 def update(self, options, args, file_list): | 865 def update(self, options, args, file_list): |
836 """Runs svn to update or transparently checkout the working copy. | 866 """Runs svn to update or transparently checkout the working copy. |
837 | 867 |
838 All updated files will be appended to file_list. | 868 All updated files will be appended to file_list. |
839 | 869 |
840 Raises: | 870 Raises: |
841 Error: if can't get URL for relative path. | 871 Error: if can't get URL for relative path. |
842 """ | 872 """ |
843 # Only update if git or hg is not controlling the directory. | 873 # 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') | 1185 new_command.append('--force') |
1156 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1186 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1157 new_command.extend(('--accept', 'theirs-conflict')) | 1187 new_command.extend(('--accept', 'theirs-conflict')) |
1158 elif options.manually_grab_svn_rev: | 1188 elif options.manually_grab_svn_rev: |
1159 new_command.append('--force') | 1189 new_command.append('--force') |
1160 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1190 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1161 new_command.extend(('--accept', 'postpone')) | 1191 new_command.extend(('--accept', 'postpone')) |
1162 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1192 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
1163 new_command.extend(('--accept', 'postpone')) | 1193 new_command.extend(('--accept', 'postpone')) |
1164 return new_command | 1194 return new_command |
OLD | NEW |