2 ## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
10 """Calculates the "intersection" of two unified diffs.
12 Given two diffs, A and B, it finds all hunks in B that had non-context lines
13 in A and prints them to stdout. This is useful to determine the hunks in B that
14 are relevant to A. The resulting file can be applied with patch(1) on top of A.
17 __author__ = "jkoleszar@google.com"
23 class DiffLines(object):
24 """A container for one half of a diff."""
26 def __init__(self, filename, offset, length):
27 self.filename = filename
31 self.delta_line_nums = []
33 def Append(self, line):
36 self.delta_line_nums.append(self.offset + l)
37 self.lines.append(line[1:])
38 assert l+1 <= self.length
41 return len(self.lines) == self.length
43 def __contains__(self, item):
44 return item >= self.offset and item <= self.offset + self.length - 1
47 class DiffHunk(object):
48 """A container for one diff hunk, consisting of two DiffLines."""
50 def __init__(self, header, file_a, file_b, start_a, len_a, start_b, len_b):
52 self.left = DiffLines(file_a, start_a, len_a)
53 self.right = DiffLines(file_b, start_b, len_b)
56 def Append(self, line):
57 """Adds a line to the DiffHunk and its DiffLines children."""
59 self.left.Append(line)
61 self.right.Append(line)
63 self.left.Append(line)
64 self.right.Append(line)
66 assert False, ("Unrecognized character at start of diff line "
68 self.lines.append(line)
71 return self.left.Complete() and self.right.Complete()
74 return "DiffHunk(%s, %s, len %d)" % (
75 self.left.filename, self.right.filename,
76 max(self.left.length, self.right.length))
79 def ParseDiffHunks(stream):
80 """Walk a file-like object, yielding DiffHunks as they're parsed."""
82 file_regex = re.compile(r"(\+\+\+|---) (\S+)")
83 range_regex = re.compile(r"@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?")
86 line = stream.readline()
92 diff_file = file_regex.match(line)
94 if line.startswith("---"):
96 a = diff_file.group(2)
98 if line.startswith("+++"):
100 b = diff_file.group(2)
103 # Parse offset/lengths
104 diffrange = range_regex.match(line)
106 if diffrange.group(2):
107 start_a = int(diffrange.group(1))
108 len_a = int(diffrange.group(3))
111 len_a = int(diffrange.group(1))
113 if diffrange.group(5):
114 start_b = int(diffrange.group(4))
115 len_b = int(diffrange.group(6))
118 len_b = int(diffrange.group(4))
120 header = [a_line, b_line, line]
121 hunk = DiffHunk(header, a, b, start_a, len_a, start_b, len_b)
123 # Add the current line to the hunk
126 # See if the whole hunk has been parsed. If so, yield it and prepare
132 # Partial hunks are a parse error
136 def FormatDiffHunks(hunks):
137 """Re-serialize a list of DiffHunks."""
141 this_header = hunk.header[0:2]
142 if last_header != this_header:
143 r.extend(hunk.header)
144 last_header = this_header
146 r.extend(hunk.header[2])
152 def ZipHunks(rhs_hunks, lhs_hunks):
153 """Join two hunk lists on filename."""
154 for rhs_hunk in rhs_hunks:
155 rhs_file = rhs_hunk.right.filename.split("/")[1:]
157 for lhs_hunk in lhs_hunks:
158 lhs_file = lhs_hunk.left.filename.split("/")[1:]
159 if lhs_file != rhs_file:
161 yield (rhs_hunk, lhs_hunk)
165 old_hunks = [x for x in ParseDiffHunks(open(sys.argv[1], "r"))]
166 new_hunks = [x for x in ParseDiffHunks(open(sys.argv[2], "r"))]
169 # Join the right hand side of the older diff with the left hand side of the
171 for old_hunk, new_hunk in ZipHunks(old_hunks, new_hunks):
172 if new_hunk in out_hunks:
174 old_lines = old_hunk.right
175 new_lines = new_hunk.left
177 # Determine if this hunk overlaps any non-context line from the other
178 for i in old_lines.delta_line_nums:
180 out_hunks.append(new_hunk)
184 print FormatDiffHunks(out_hunks)
187 if __name__ == "__main__":