Debian Codegraph @

contrib/depends-diff.py

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2024 Robin Jarry
4
5 import argparse
6 import re
7 import subprocess
8
9 DEP_CHANGE_RE = re.compile(
10 r"""
11 ^
12 (?P<diff>[\+\-])\s*
13 (?P<name>\S+)\s*
14 (?P<version>v\S+)\s*
15 (?://\s*indirect)?
16 $
17 """,
18 re.VERBOSE,
19 )
20 REPLACE_RE = re.compile(
21 r"""
22 ^
23 (?P<diff>[\+\-])\s*
24 replace
25 (?P<name>\S+)\s*
26 =>\s*
27 (?P<replacement>\S+)\s*
28 (?P<version>v\S+)\s*
29 $
30 """,
31 re.VERBOSE,
32 )
33
34
35 def main():
36 parser = argparse.ArgumentParser(description=__doc__)
37 parser.add_argument(
38 "git_range",
39 metavar="GIT_RANGE",
40 help="The git revision range (see gitrevisions(7)).",
41 )
42 args = parser.parse_args()
43
44 old_deps = {}
45 new_deps = {}
46
47 with subprocess.Popen(
48 ["git", "diff", "-U0", "--ignore-all-space", args.git_range, "--", "go.mod"],
49 stdout=subprocess.PIPE,
50 encoding="utf-8",
51 ) as proc:
52 for line in proc.stdout:
53 match = DEP_CHANGE_RE.match(line.strip())
54 if not match:
55 match = REPLACE_RE.match(line.strip())
56 if not match:
57 continue
58 diff, name, replacement, version = match.groups()
59 if diff == "+":
60 new_deps[replacement] = version
61 del new_deps[name]
62 continue
63 diff, name, version = match.groups()
64 if diff == "+":
65 new_deps[name] = version
66 else:
67 old_deps[name] = version
68
69 once = False
70 added = new_deps.keys() - old_deps.keys()
71 if added:
72 print("## New")
73 print()
74 for a in sorted(added):
75 print("+", a, new_deps[a])
76 once = True
77
78 updated = old_deps.keys() & new_deps.keys()
79 if updated:
80 if once:
81 print()
82 print("## Updated")
83 print()
84 for u in sorted(updated):
85 print("*", u, old_deps[u], "=>", new_deps[u])
86 once = True
87
88 removed = old_deps.keys() - new_deps.keys()
89 if removed:
90 if once:
91 print()
92 print("## Removed")
93 print()
94 for r in sorted(removed):
95 print("-", r)
96 once = True
97
98 if not once:
99 print("none")
100
101
102 if __name__ == "__main__":
103 main()