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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 out += ' ' | 93 out += ' ' |
94 if self.source_filename: | 94 if self.source_filename: |
95 out += 'R' | 95 out += 'R' |
96 else: | 96 else: |
97 out += ' ' | 97 out += ' ' |
98 out += ' ' | 98 out += ' ' |
99 if self.source_filename: | 99 if self.source_filename: |
100 out += '%s->' % self.source_filename_utf8 | 100 out += '%s->' % self.source_filename_utf8 |
101 return out + self.filename_utf8 | 101 return out + self.filename_utf8 |
102 | 102 |
| 103 def dump(self): |
| 104 """Dumps itself in a verbose way to help diagnosing.""" |
| 105 return str(self) |
| 106 |
103 | 107 |
104 class FilePatchDelete(FilePatchBase): | 108 class FilePatchDelete(FilePatchBase): |
105 """Deletes a file.""" | 109 """Deletes a file.""" |
106 is_delete = True | 110 is_delete = True |
107 | 111 |
108 def __init__(self, filename, is_binary): | 112 def __init__(self, filename, is_binary): |
109 super(FilePatchDelete, self).__init__(filename) | 113 super(FilePatchDelete, self).__init__(filename) |
110 self.is_binary = is_binary | 114 self.is_binary = is_binary |
111 | 115 |
112 | 116 |
113 class FilePatchBinary(FilePatchBase): | 117 class FilePatchBinary(FilePatchBase): |
114 """Content of a new binary file.""" | 118 """Content of a new binary file.""" |
115 is_binary = True | 119 is_binary = True |
116 | 120 |
117 def __init__(self, filename, data, svn_properties, is_new): | 121 def __init__(self, filename, data, svn_properties, is_new): |
118 super(FilePatchBinary, self).__init__(filename) | 122 super(FilePatchBinary, self).__init__(filename) |
119 self.data = data | 123 self.data = data |
120 self.svn_properties = svn_properties or [] | 124 self.svn_properties = svn_properties or [] |
121 self.is_new = is_new | 125 self.is_new = is_new |
122 | 126 |
123 def get(self): | 127 def get(self): |
124 return self.data | 128 return self.data |
125 | 129 |
| 130 def __str__(self): |
| 131 return str(super(FilePatchBinary, self)) + ' %d bytes' % len(self.data) |
| 132 |
126 | 133 |
127 class Hunk(object): | 134 class Hunk(object): |
128 """Parsed hunk data container.""" | 135 """Parsed hunk data container.""" |
129 | 136 |
130 def __init__(self, start_src, lines_src, start_dst, lines_dst): | 137 def __init__(self, start_src, lines_src, start_dst, lines_dst): |
131 self.start_src = start_src | 138 self.start_src = start_src |
132 self.lines_src = lines_src | 139 self.lines_src = lines_src |
133 self.start_dst = start_dst | 140 self.start_dst = start_dst |
134 self.lines_dst = lines_dst | 141 self.lines_dst = lines_dst |
135 self.variation = self.lines_dst - self.lines_src | 142 self.variation = self.lines_dst - self.lines_src |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 self._fail('Unexpected diff: --- not following +++.') | 491 self._fail('Unexpected diff: --- not following +++.') |
485 if match.group(1) == '/dev/null': | 492 if match.group(1) == '/dev/null': |
486 self.is_delete = True | 493 self.is_delete = True |
487 elif self.mangle(match.group(1)) != self.filename_utf8: | 494 elif self.mangle(match.group(1)) != self.filename_utf8: |
488 self._fail('Unexpected diff: %s.' % match.group(1)) | 495 self._fail('Unexpected diff: %s.' % match.group(1)) |
489 if lines: | 496 if lines: |
490 self._fail('Crap after +++') | 497 self._fail('Crap after +++') |
491 # We're done. | 498 # We're done. |
492 return | 499 return |
493 | 500 |
| 501 def dump(self): |
| 502 """Dumps itself in a verbose way to help diagnosing.""" |
| 503 return str(self) + '\n' + self.get(True) |
| 504 |
494 | 505 |
495 class PatchSet(object): | 506 class PatchSet(object): |
496 """A list of FilePatch* objects.""" | 507 """A list of FilePatch* objects.""" |
497 | 508 |
498 def __init__(self, patches): | 509 def __init__(self, patches): |
499 for p in patches: | 510 for p in patches: |
500 assert isinstance(p, FilePatchBase) | 511 assert isinstance(p, FilePatchBase) |
501 | 512 |
502 def key(p): | 513 def key(p): |
503 """Sort by ordering of application. | 514 """Sort by ordering of application. |
(...skipping 19 matching lines...) Expand all Loading... |
523 def __iter__(self): | 534 def __iter__(self): |
524 for patch in self.patches: | 535 for patch in self.patches: |
525 yield patch | 536 yield patch |
526 | 537 |
527 def __getitem__(self, key): | 538 def __getitem__(self, key): |
528 return self.patches[key] | 539 return self.patches[key] |
529 | 540 |
530 @property | 541 @property |
531 def filenames(self): | 542 def filenames(self): |
532 return [p.filename for p in self.patches] | 543 return [p.filename for p in self.patches] |
OLD | NEW |