OLD | NEW |
1 # coding=utf8 | 1 # coding=utf8 |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 """Utility functions to handle patches.""" | 5 """Utility functions to handle patches.""" |
6 | 6 |
7 import posixpath | 7 import posixpath |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 # http://codereview.chromium.org/download/issue6250123_3013_6010.diff | 212 # http://codereview.chromium.org/download/issue6250123_3013_6010.diff |
213 # Rename no change: | 213 # Rename no change: |
214 # http://codereview.chromium.org/download/issue6287022_3001_4010.diff | 214 # http://codereview.chromium.org/download/issue6287022_3001_4010.diff |
215 return any(l.startswith('diff --git') for l in diff_header.splitlines()) | 215 return any(l.startswith('diff --git') for l in diff_header.splitlines()) |
216 | 216 |
217 def _split_hunks(self): | 217 def _split_hunks(self): |
218 """Splits the hunks and does verification.""" | 218 """Splits the hunks and does verification.""" |
219 hunks = [] | 219 hunks = [] |
220 for line in self.diff_hunks.splitlines(True): | 220 for line in self.diff_hunks.splitlines(True): |
221 if line.startswith('@@'): | 221 if line.startswith('@@'): |
222 match = re.match(r'^@@ -(\d+),(\d+) \+([\d,]+) @@.*$', line) | 222 match = re.match(r'^@@ -([\d,]+) \+([\d,]+) @@.*$', line) |
223 # File add will result in "-0,0 +1" but file deletion will result in | 223 # File add will result in "-0,0 +1" but file deletion will result in |
224 # "-1,N +0,0" where N is the number of lines deleted. That's from diff | 224 # "-1,N +0,0" where N is the number of lines deleted. That's from diff |
225 # and svn diff. git diff doesn't exhibit this behavior. | 225 # and svn diff. git diff doesn't exhibit this behavior. |
| 226 # svn diff for a single line file rewrite "@@ -1 +1 @@". Fun. |
226 if not match: | 227 if not match: |
227 self._fail('Hunk header is unparsable') | 228 self._fail('Hunk header is unparsable') |
228 if ',' in match.group(3): | 229 if ',' in match.group(1): |
229 start_dst, lines_dst = map(int, match.group(3).split(',', 1)) | 230 start_src, lines_src = map(int, match.group(1).split(',', 1)) |
230 else: | 231 else: |
231 start_dst = int(match.group(3)) | 232 start_src = int(match.group(1)) |
| 233 lines_src = 0 |
| 234 if ',' in match.group(2): |
| 235 start_dst, lines_dst = map(int, match.group(2).split(',', 1)) |
| 236 else: |
| 237 start_dst = int(match.group(2)) |
232 lines_dst = 0 | 238 lines_dst = 0 |
233 new_hunk = Hunk(int(match.group(1)), int(match.group(2)), | 239 new_hunk = Hunk(start_src, lines_src, start_dst, lines_dst) |
234 start_dst, lines_dst) | |
235 if hunks: | 240 if hunks: |
236 if new_hunk.start_src <= hunks[-1].start_src: | 241 if new_hunk.start_src <= hunks[-1].start_src: |
237 self._fail('Hunks source lines are not ordered') | 242 self._fail('Hunks source lines are not ordered') |
238 if new_hunk.start_dst <= hunks[-1].start_dst: | 243 if new_hunk.start_dst <= hunks[-1].start_dst: |
239 self._fail('Hunks destination lines are not ordered') | 244 self._fail('Hunks destination lines are not ordered') |
240 hunks.append(new_hunk) | 245 hunks.append(new_hunk) |
241 continue | 246 continue |
242 hunks[-1].text.append(line) | 247 hunks[-1].text.append(line) |
243 | 248 |
244 if len(hunks) == 1: | 249 if len(hunks) == 1: |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 def __iter__(self): | 494 def __iter__(self): |
490 for patch in self.patches: | 495 for patch in self.patches: |
491 yield patch | 496 yield patch |
492 | 497 |
493 def __getitem__(self, key): | 498 def __getitem__(self, key): |
494 return self.patches[key] | 499 return self.patches[key] |
495 | 500 |
496 @property | 501 @property |
497 def filenames(self): | 502 def filenames(self): |
498 return [p.filename for p in self.patches] | 503 return [p.filename for p in self.patches] |
OLD | NEW |