OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ## Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
| 3 ## |
| 4 ## Use of this source code is governed by a BSD-style license |
| 5 ## that can be found in the LICENSE file in the root of the source |
| 6 ## tree. An additional intellectual property rights grant can be found |
| 7 ## in the file PATENTS. All contributing project authors may |
| 8 ## be found in the AUTHORS file in the root of the source tree. |
| 9 ## |
| 10 """Classes for representing diff pieces.""" |
| 11 |
| 12 __author__ = "jkoleszar@google.com" |
| 13 |
| 14 import re |
| 15 |
| 16 |
| 17 class DiffLines(object): |
| 18 """A container for one half of a diff.""" |
| 19 |
| 20 def __init__(self, filename, offset, length): |
| 21 self.filename = filename |
| 22 self.offset = offset |
| 23 self.length = length |
| 24 self.lines = [] |
| 25 self.delta_line_nums = [] |
| 26 |
| 27 def Append(self, line): |
| 28 l = len(self.lines) |
| 29 if line[0] != " ": |
| 30 self.delta_line_nums.append(self.offset + l) |
| 31 self.lines.append(line[1:]) |
| 32 assert l+1 <= self.length |
| 33 |
| 34 def Complete(self): |
| 35 return len(self.lines) == self.length |
| 36 |
| 37 def __contains__(self, item): |
| 38 return item >= self.offset and item <= self.offset + self.length - 1 |
| 39 |
| 40 |
| 41 class DiffHunk(object): |
| 42 """A container for one diff hunk, consisting of two DiffLines.""" |
| 43 |
| 44 def __init__(self, header, file_a, file_b, start_a, len_a, start_b, len_b): |
| 45 self.header = header |
| 46 self.left = DiffLines(file_a, start_a, len_a) |
| 47 self.right = DiffLines(file_b, start_b, len_b) |
| 48 self.lines = [] |
| 49 |
| 50 def Append(self, line): |
| 51 """Adds a line to the DiffHunk and its DiffLines children.""" |
| 52 if line[0] == "-": |
| 53 self.left.Append(line) |
| 54 elif line[0] == "+": |
| 55 self.right.Append(line) |
| 56 elif line[0] == " ": |
| 57 self.left.Append(line) |
| 58 self.right.Append(line) |
| 59 else: |
| 60 assert False, ("Unrecognized character at start of diff line " |
| 61 "%r" % line[0]) |
| 62 self.lines.append(line) |
| 63 |
| 64 def Complete(self): |
| 65 return self.left.Complete() and self.right.Complete() |
| 66 |
| 67 def __repr__(self): |
| 68 return "DiffHunk(%s, %s, len %d)" % ( |
| 69 self.left.filename, self.right.filename, |
| 70 max(self.left.length, self.right.length)) |
| 71 |
| 72 |
| 73 def ParseDiffHunks(stream): |
| 74 """Walk a file-like object, yielding DiffHunks as they're parsed.""" |
| 75 |
| 76 file_regex = re.compile(r"(\+\+\+|---) (\S+)") |
| 77 range_regex = re.compile(r"@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?") |
| 78 hunk = None |
| 79 while True: |
| 80 line = stream.readline() |
| 81 if not line: |
| 82 break |
| 83 |
| 84 if hunk is None: |
| 85 # Parse file names |
| 86 diff_file = file_regex.match(line) |
| 87 if diff_file: |
| 88 if line.startswith("---"): |
| 89 a_line = line |
| 90 a = diff_file.group(2) |
| 91 continue |
| 92 if line.startswith("+++"): |
| 93 b_line = line |
| 94 b = diff_file.group(2) |
| 95 continue |
| 96 |
| 97 # Parse offset/lengths |
| 98 diffrange = range_regex.match(line) |
| 99 if diffrange: |
| 100 if diffrange.group(2): |
| 101 start_a = int(diffrange.group(1)) |
| 102 len_a = int(diffrange.group(3)) |
| 103 else: |
| 104 start_a = 1 |
| 105 len_a = int(diffrange.group(1)) |
| 106 |
| 107 if diffrange.group(5): |
| 108 start_b = int(diffrange.group(4)) |
| 109 len_b = int(diffrange.group(6)) |
| 110 else: |
| 111 start_b = 1 |
| 112 len_b = int(diffrange.group(4)) |
| 113 |
| 114 header = [a_line, b_line, line] |
| 115 hunk = DiffHunk(header, a, b, start_a, len_a, start_b, len_b) |
| 116 else: |
| 117 # Add the current line to the hunk |
| 118 hunk.Append(line) |
| 119 |
| 120 # See if the whole hunk has been parsed. If so, yield it and prepare |
| 121 # for the next hunk. |
| 122 if hunk.Complete(): |
| 123 yield hunk |
| 124 hunk = None |
| 125 |
| 126 # Partial hunks are a parse error |
| 127 assert hunk is None |
OLD | NEW |