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"""Calculates the "intersection" of two unified diffs.
11
12Given two diffs, A and B, it finds all hunks in B that had non-context lines
13in A and prints them to stdout. This is useful to determine the hunks in B that
14are relevant to A. The resulting file can be applied with patch(1) on top of A.
15"""
16
17__author__ = "jkoleszar@google.com"
18
19import sys
20
21import diff
22
23
24def FormatDiffHunks(hunks):
25    """Re-serialize a list of DiffHunks."""
26    r = []
27    last_header = None
28    for hunk in hunks:
29        this_header = hunk.header[0:2]
30        if last_header != this_header:
31            r.extend(hunk.header)
32            last_header = this_header
33        else:
34            r.extend(hunk.header[2])
35        r.extend(hunk.lines)
36        r.append("\n")
37    return "".join(r)
38
39
40def 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:]
44
45        for lhs_hunk in lhs_hunks:
46            lhs_file = lhs_hunk.left.filename.split("/")[1:]
47            if lhs_file != rhs_file:
48                continue
49            yield (rhs_hunk, lhs_hunk)
50
51
52def main():
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"))]
55    out_hunks = []
56
57    # Join the right hand side of the older diff with the left hand side of the
58    # newer diff.
59    for old_hunk, new_hunk in ZipHunks(old_hunks, new_hunks):
60        if new_hunk in out_hunks:
61            continue
62        old_lines = old_hunk.right
63        new_lines = new_hunk.left
64
65        # Determine if this hunk overlaps any non-context line from the other
66        for i in old_lines.delta_line_nums:
67            if i in new_lines:
68                out_hunks.append(new_hunk)
69                break
70
71    if out_hunks:
72        print FormatDiffHunks(out_hunks)
73        sys.exit(1)
74
75if __name__ == "__main__":
76    main()
77