aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/depends-diff.py
blob: 45376120b3639fef3f37adfd878f1ca036ab7a78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 Robin Jarry

import argparse
import re
import subprocess

DEP_CHANGE_RE = re.compile(
    r"""
    ^
    (?P<diff>[\+\-])\s*
    (?P<name>\S+)\s*
    (?P<version>v\S+)\s*
    (?://\s*indirect)?
    $
    """,
    re.VERBOSE,
)
REPLACE_RE = re.compile(
    r"""
    ^
    (?P<diff>[\+\-])\s*
    replace
    (?P<name>\S+)\s*
    =>\s*
    (?P<replacement>\S+)\s*
    (?P<version>v\S+)\s*
    $
    """,
    re.VERBOSE,
)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "git_range",
        metavar="GIT_RANGE",
        help="The git revision range (see gitrevisions(7)).",
    )
    args = parser.parse_args()

    old_deps = {}
    new_deps = {}

    with subprocess.Popen(
        ["git", "diff", "-U0", "--ignore-all-space", args.git_range, "--", "go.mod"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
    ) as proc:
        for line in proc.stdout:
            match = DEP_CHANGE_RE.match(line.strip())
            if not match:
                match = REPLACE_RE.match(line.strip())
                if not match:
                    continue
                diff, name, replacement, version = match.groups()
                if diff == "+":
                    new_deps[replacement] = version
                    del new_deps[name]
                continue
            diff, name, version = match.groups()
            if diff == "+":
                new_deps[name] = version
            else:
                old_deps[name] = version

    once = False
    added = new_deps.keys() - old_deps.keys()
    if added:
        print("## New")
        print()
        for a in sorted(added):
            print("+", a, new_deps[a])
        once = True

    updated = old_deps.keys() & new_deps.keys()
    if updated:
        if once:
            print()
        print("## Updated")
        print()
        for u in sorted(updated):
            print("*", u, old_deps[u], "=>", new_deps[u])
        once = True

    removed = old_deps.keys() - new_deps.keys()
    if removed:
        if once:
            print()
        print("## Removed")
        print()
        for r in sorted(removed):
            print("-", r)
        once = True

    if not once:
        print("none")


if __name__ == "__main__":
    main()