Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # Copyright 2008 Google Inc. All Rights Reserved. | |
| 6 # | |
| 7 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 8 # you may not use this file except in compliance with the License. | |
| 9 # You may obtain a copy of the License at | |
| 10 # | |
| 11 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 12 # | |
| 13 # Unless required by applicable law or agreed to in writing, software | |
| 14 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 16 # See the License for the specific language governing permissions and | |
| 17 # limitations under the License. | |
| 18 | |
| 19 """Utility for parsing patches, shamelessly stolen from rietveld.""" | |
| 20 | |
| 21 import logging | |
| 22 import re | |
| 23 | |
| 24 | |
| 25 _CHUNK_RE = re.compile(r""" | |
| 26 @@ | |
| 27 \s+ | |
| 28 - | |
| 29 (?: (\d+) (?: , (\d+) )?) | |
| 30 \s+ | |
| 31 \+ | |
| 32 (?: (\d+) (?: , (\d+) )?) | |
| 33 \s+ | |
| 34 @@ | |
| 35 """, re.VERBOSE) | |
|
iannucci
2013/08/08 10:35:20
Scary :( I really wish we had a real, robust, test
Mattias Nissler (ping if slow)
2013/08/08 13:04:13
I'm with you on this, but the python standard libr
| |
| 36 | |
| 37 | |
| 38 _NO_NEWLINE_MESSAGE = "\\ No newline at end of file" | |
| 39 | |
| 40 | |
| 41 def ParsePatchToLines(lines): | |
| 42 """Parses a patch from a list of lines. | |
| 43 | |
| 44 Args: | |
| 45 lines: The lines to parse. | |
| 46 | |
| 47 Returns: | |
| 48 None on error, otherwise a list of 3-tuples: | |
| 49 (old_line_no, new_line_no, line) | |
| 50 | |
| 51 A line number can be 0 if it doesn't exist in the old/new file. | |
|
iannucci
2013/08/08 10:35:20
why not None? Unless lines start at 1?
Mattias Nissler (ping if slow)
2013/08/08 13:04:13
None is indeed better. Changed.
| |
| 52 """ | |
| 53 result = [] | |
| 54 in_prelude = True | |
| 55 for line in lines: | |
| 56 if in_prelude: | |
| 57 # Skip leading lines until after we've seen one starting with '+++' | |
| 58 if line.startswith("+++"): | |
| 59 in_prelude = False | |
|
iannucci
2013/08/08 10:35:20
how do we exit the prelude for a multi-chunk patch
Mattias Nissler (ping if slow)
2013/08/08 13:04:13
Hunks start with @@, so we reset.
Multi-file patc
| |
| 60 elif line.startswith("@"): | |
| 61 match = _CHUNK_RE.match(line) | |
| 62 if not match: | |
| 63 logging.warn("ParsePatchToLines match failed on %s", line) | |
| 64 return None | |
| 65 old_ln = int(match.groups()[0]) | |
| 66 new_ln = int(match.groups()[2]) | |
| 67 else: | |
| 68 if line[0] == "-": | |
| 69 result.append((old_ln, 0, line[1:])) | |
| 70 old_ln += 1 | |
| 71 elif line[0] == "+": | |
| 72 result.append((0, new_ln, line[1:])) | |
| 73 new_ln += 1 | |
| 74 elif line[0] == " ": | |
| 75 result.append((old_ln, new_ln, line[1:])) | |
| 76 old_ln += 1 | |
| 77 new_ln += 1 | |
| 78 return result | |
| OLD | NEW |