| OLD | NEW |
| 1 # coding=utf8 | 1 # coding=utf8 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 self._fail('Unexpected git diff: %s != %s.' % (old, match.group(1))) | 262 self._fail('Unexpected git diff: %s != %s.' % (old, match.group(1))) |
| 263 if not lines or not lines[0].startswith('+++'): | 263 if not lines or not lines[0].startswith('+++'): |
| 264 self._fail('Missing git diff output name.') | 264 self._fail('Missing git diff output name.') |
| 265 return | 265 return |
| 266 | 266 |
| 267 match = re.match(r'^\+\+\+ (.*)$', line) | 267 match = re.match(r'^\+\+\+ (.*)$', line) |
| 268 if match: | 268 if match: |
| 269 if not last_line.startswith('---'): | 269 if not last_line.startswith('---'): |
| 270 self._fail('Unexpected git diff: --- not following +++.') | 270 self._fail('Unexpected git diff: --- not following +++.') |
| 271 # TODO(maruel): new == self.filename. | 271 # TODO(maruel): new == self.filename. |
| 272 if new != self.mangle(match.group(1)) and '/dev/null' != match.group(1): | 272 if '/dev/null' == match.group(1): |
| 273 # TODO(maruel): Can +++ be /dev/null? If so, assert self.is_delete == | 273 self.is_delete = True |
| 274 # True. | 274 elif new != self.mangle(match.group(1)): |
| 275 self._fail('Unexpected git diff: %s != %s.' % (new, match.group(1))) | 275 self._fail('Unexpected git diff: %s != %s.' % (new, match.group(1))) |
| 276 if lines: | 276 if lines: |
| 277 self._fail('Crap after +++') | 277 self._fail('Crap after +++') |
| 278 # We're done. | 278 # We're done. |
| 279 return | 279 return |
| 280 | 280 |
| 281 def _verify_svn_header(self): | 281 def _verify_svn_header(self): |
| 282 """Sanity checks the header. | 282 """Sanity checks the header. |
| 283 | 283 |
| 284 A svn diff can contain only property changes, in that case there will be no | 284 A svn diff can contain only property changes, in that case there will be no |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 # match.group(1) != '/dev/null'): | 316 # match.group(1) != '/dev/null'): |
| 317 # self.source_file = match.group(1) | 317 # self.source_file = match.group(1) |
| 318 if not lines or not lines[0].startswith('+++'): | 318 if not lines or not lines[0].startswith('+++'): |
| 319 self._fail('Nothing after header.') | 319 self._fail('Nothing after header.') |
| 320 return | 320 return |
| 321 | 321 |
| 322 match = re.match(r'^\+\+\+ ([^\t]+).*$', line) | 322 match = re.match(r'^\+\+\+ ([^\t]+).*$', line) |
| 323 if match: | 323 if match: |
| 324 if not last_line.startswith('---'): | 324 if not last_line.startswith('---'): |
| 325 self._fail('Unexpected diff: --- not following +++.') | 325 self._fail('Unexpected diff: --- not following +++.') |
| 326 if (self.mangle(match.group(1)) != self.filename and | 326 if match.group(1) == '/dev/null': |
| 327 match.group(1) != '/dev/null'): | 327 self.is_delete = True |
| 328 # TODO(maruel): Can +++ be /dev/null? If so, assert self.is_delete == | 328 elif self.mangle(match.group(1)) != self.filename: |
| 329 # True. | |
| 330 self._fail('Unexpected diff: %s.' % match.group(1)) | 329 self._fail('Unexpected diff: %s.' % match.group(1)) |
| 331 if lines: | 330 if lines: |
| 332 self._fail('Crap after +++') | 331 self._fail('Crap after +++') |
| 333 # We're done. | 332 # We're done. |
| 334 return | 333 return |
| 335 | 334 |
| 336 | 335 |
| 337 class PatchSet(object): | 336 class PatchSet(object): |
| 338 """A list of FilePatch* objects.""" | 337 """A list of FilePatch* objects.""" |
| 339 | 338 |
| 340 def __init__(self, patches): | 339 def __init__(self, patches): |
| 341 self.patches = patches | 340 self.patches = patches |
| 342 for p in self.patches: | 341 for p in self.patches: |
| 343 assert isinstance(p, FilePatchBase) | 342 assert isinstance(p, FilePatchBase) |
| 344 | 343 |
| 345 def set_relpath(self, relpath): | 344 def set_relpath(self, relpath): |
| 346 """Used to offset the patch into a subdirectory.""" | 345 """Used to offset the patch into a subdirectory.""" |
| 347 for patch in self.patches: | 346 for patch in self.patches: |
| 348 patch.set_relpath(relpath) | 347 patch.set_relpath(relpath) |
| 349 | 348 |
| 350 def __iter__(self): | 349 def __iter__(self): |
| 351 for patch in self.patches: | 350 for patch in self.patches: |
| 352 yield patch | 351 yield patch |
| 353 | 352 |
| 354 @property | 353 @property |
| 355 def filenames(self): | 354 def filenames(self): |
| 356 return [p.filename for p in self.patches] | 355 return [p.filename for p in self.patches] |
| OLD | NEW |