xref: /aosp_15_r20/external/libaom/tools/intersect-diffs.py (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*77c1e3ccSAndroid Build Coastguard Worker##
3*77c1e3ccSAndroid Build Coastguard Worker## Copyright (c) 2016, Alliance for Open Media. All rights reserved.
4*77c1e3ccSAndroid Build Coastguard Worker##
5*77c1e3ccSAndroid Build Coastguard Worker## This source code is subject to the terms of the BSD 2 Clause License and
6*77c1e3ccSAndroid Build Coastguard Worker## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7*77c1e3ccSAndroid Build Coastguard Worker## was not distributed with this source code in the LICENSE file, you can
8*77c1e3ccSAndroid Build Coastguard Worker## obtain it at www.aomedia.org/license/software. If the Alliance for Open
9*77c1e3ccSAndroid Build Coastguard Worker## Media Patent License 1.0 was not distributed with this source code in the
10*77c1e3ccSAndroid Build Coastguard Worker## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11*77c1e3ccSAndroid Build Coastguard Worker##
12*77c1e3ccSAndroid Build Coastguard Worker"""Calculates the "intersection" of two unified diffs.
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard WorkerGiven two diffs, A and B, it finds all hunks in B that had non-context lines
15*77c1e3ccSAndroid Build Coastguard Workerin A and prints them to stdout. This is useful to determine the hunks in B that
16*77c1e3ccSAndroid Build Coastguard Workerare relevant to A. The resulting file can be applied with patch(1) on top of A.
17*77c1e3ccSAndroid Build Coastguard Worker"""
18*77c1e3ccSAndroid Build Coastguard Worker
19*77c1e3ccSAndroid Build Coastguard Worker__author__ = "[email protected]"
20*77c1e3ccSAndroid Build Coastguard Worker
21*77c1e3ccSAndroid Build Coastguard Workerimport sys
22*77c1e3ccSAndroid Build Coastguard Worker
23*77c1e3ccSAndroid Build Coastguard Workerimport diff
24*77c1e3ccSAndroid Build Coastguard Worker
25*77c1e3ccSAndroid Build Coastguard Worker
26*77c1e3ccSAndroid Build Coastguard Workerdef FormatDiffHunks(hunks):
27*77c1e3ccSAndroid Build Coastguard Worker    """Re-serialize a list of DiffHunks."""
28*77c1e3ccSAndroid Build Coastguard Worker    r = []
29*77c1e3ccSAndroid Build Coastguard Worker    last_header = None
30*77c1e3ccSAndroid Build Coastguard Worker    for hunk in hunks:
31*77c1e3ccSAndroid Build Coastguard Worker        this_header = hunk.header[0:2]
32*77c1e3ccSAndroid Build Coastguard Worker        if last_header != this_header:
33*77c1e3ccSAndroid Build Coastguard Worker            r.extend(hunk.header)
34*77c1e3ccSAndroid Build Coastguard Worker            last_header = this_header
35*77c1e3ccSAndroid Build Coastguard Worker        else:
36*77c1e3ccSAndroid Build Coastguard Worker            r.extend(hunk.header[2])
37*77c1e3ccSAndroid Build Coastguard Worker        r.extend(hunk.lines)
38*77c1e3ccSAndroid Build Coastguard Worker        r.append("\n")
39*77c1e3ccSAndroid Build Coastguard Worker    return "".join(r)
40*77c1e3ccSAndroid Build Coastguard Worker
41*77c1e3ccSAndroid Build Coastguard Worker
42*77c1e3ccSAndroid Build Coastguard Workerdef ZipHunks(rhs_hunks, lhs_hunks):
43*77c1e3ccSAndroid Build Coastguard Worker    """Join two hunk lists on filename."""
44*77c1e3ccSAndroid Build Coastguard Worker    for rhs_hunk in rhs_hunks:
45*77c1e3ccSAndroid Build Coastguard Worker        rhs_file = rhs_hunk.right.filename.split("/")[1:]
46*77c1e3ccSAndroid Build Coastguard Worker
47*77c1e3ccSAndroid Build Coastguard Worker        for lhs_hunk in lhs_hunks:
48*77c1e3ccSAndroid Build Coastguard Worker            lhs_file = lhs_hunk.left.filename.split("/")[1:]
49*77c1e3ccSAndroid Build Coastguard Worker            if lhs_file != rhs_file:
50*77c1e3ccSAndroid Build Coastguard Worker                continue
51*77c1e3ccSAndroid Build Coastguard Worker            yield (rhs_hunk, lhs_hunk)
52*77c1e3ccSAndroid Build Coastguard Worker
53*77c1e3ccSAndroid Build Coastguard Worker
54*77c1e3ccSAndroid Build Coastguard Workerdef main():
55*77c1e3ccSAndroid Build Coastguard Worker    old_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[1], "r"))]
56*77c1e3ccSAndroid Build Coastguard Worker    new_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[2], "r"))]
57*77c1e3ccSAndroid Build Coastguard Worker    out_hunks = []
58*77c1e3ccSAndroid Build Coastguard Worker
59*77c1e3ccSAndroid Build Coastguard Worker    # Join the right hand side of the older diff with the left hand side of the
60*77c1e3ccSAndroid Build Coastguard Worker    # newer diff.
61*77c1e3ccSAndroid Build Coastguard Worker    for old_hunk, new_hunk in ZipHunks(old_hunks, new_hunks):
62*77c1e3ccSAndroid Build Coastguard Worker        if new_hunk in out_hunks:
63*77c1e3ccSAndroid Build Coastguard Worker            continue
64*77c1e3ccSAndroid Build Coastguard Worker        old_lines = old_hunk.right
65*77c1e3ccSAndroid Build Coastguard Worker        new_lines = new_hunk.left
66*77c1e3ccSAndroid Build Coastguard Worker
67*77c1e3ccSAndroid Build Coastguard Worker        # Determine if this hunk overlaps any non-context line from the other
68*77c1e3ccSAndroid Build Coastguard Worker        for i in old_lines.delta_line_nums:
69*77c1e3ccSAndroid Build Coastguard Worker            if i in new_lines:
70*77c1e3ccSAndroid Build Coastguard Worker                out_hunks.append(new_hunk)
71*77c1e3ccSAndroid Build Coastguard Worker                break
72*77c1e3ccSAndroid Build Coastguard Worker
73*77c1e3ccSAndroid Build Coastguard Worker    if out_hunks:
74*77c1e3ccSAndroid Build Coastguard Worker        print(FormatDiffHunks(out_hunks))
75*77c1e3ccSAndroid Build Coastguard Worker        sys.exit(1)
76*77c1e3ccSAndroid Build Coastguard Worker
77*77c1e3ccSAndroid Build Coastguard Workerif __name__ == "__main__":
78*77c1e3ccSAndroid Build Coastguard Worker    main()
79