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"
24 def FormatDiffHunks(hunks):
25 """Re-serialize a list of DiffHunks."""
29 this_header = hunk.header[0:2]
30 if last_header != this_header:
32 last_header = this_header
34 r.extend(hunk.header[2])
40 def ZipHunks(rhs_hunks, lhs_hunks):
41 """Join two hunk lists on filename."""
42 for rhs_hunk in rhs_hunks:
43 rhs_file = rhs_hunk.right.filename.split("/")[1:]
45 for lhs_hunk in lhs_hunks:
46 lhs_file = lhs_hunk.left.filename.split("/")[1:]
47 if lhs_file != rhs_file:
49 yield (rhs_hunk, lhs_hunk)
53 old_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[1], "r"))]
54 new_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[2], "r"))]
57 # Join the right hand side of the older diff with the left hand side of the
59 for old_hunk, new_hunk in ZipHunks(old_hunks, new_hunks):
60 if new_hunk in out_hunks:
62 old_lines = old_hunk.right
63 new_lines = new_hunk.left
65 # Determine if this hunk overlaps any non-context line from the other
66 for i in old_lines.delta_line_nums:
68 out_hunks.append(new_hunk)
72 print FormatDiffHunks(out_hunks)
75 if __name__ == "__main__":