Debian Codegraph @

bin/rb.py

raw
1 #!/usr/bin/python3
2 #
3 # Copyright 2025 Valve Corporation
4 # SPDX-License-Identifier: MIT
5
6 import argparse
7 import csv
8 import unittest
9 import sys
10 import subprocess
11 import os
12 import shlex
13 from unidecode import unidecode
14
15 def normalize(x):
16 return unidecode(x.lower())
17
18 def name(row):
19 return normalize(row[0])
20
21 def username(row):
22 return normalize(row[2])
23
24 def find_person(x):
25 x = normalize(x)
26
27 filename = 'people.csv'
28 path = os.path.join(os.path.dirname(os.path.realpath(__file__)), filename)
29 with open(path, 'r') as f:
30 people = list(csv.reader(f, skipinitialspace=True))
31
32 # First, try to exactly match username
33 for row in people:
34 if username(row) == x:
35 return row
36
37 # Next, try to exactly match fullname
38 for row in people:
39 if name(row) == x:
40 return row
41
42 # Now we get fuzzy. Try to match a first name.
43 candidates = [r for r in people if name(r).split(' ')[0] == x]
44 if len(candidates) == 1:
45 return candidates[0]
46
47 # Or a last name?
48 candidates = [r for r in people if x in name(r).split(' ')]
49 if len(candidates) == 1:
50 return candidates[0]
51
52 # Well, frick.
53 return None
54
55 # Self-test... is it even worth find a unit test framework for this?
56 TEST_CASES = {
57 'gfxstrand': '[email protected]',
58 'Faith': '[email protected]',
59 'faith': '[email protected]',
60 'alyssa': '[email protected]',
61 'briano': '[email protected]',
62 'schurmann': '[email protected]',
63 'Schürmann': '[email protected]',
64 }
65
66 for test in TEST_CASES:
67 a, b = find_person(test), TEST_CASES[test]
68 if a is None or a[1] != b:
69 print(test, a, b)
70 assert(a is not None and a[1] == b)
71
72 # Now the tool itself
73 if __name__ == "__main__":
74 parser = argparse.ArgumentParser(
75 prog='rb',
76 description='Add review trailers')
77 parser.add_argument('person', nargs='+', help="Reviewer's username, first name, or full name")
78 parser.add_argument('-a', '--ack', action='store_true', help="Apply an acked-by tag")
79 parser.add_argument('-t', '--test', action='store_true', help="Apply an tested-by tag")
80 parser.add_argument('-d', '--dry-run', action='store_true',
81 help="Print trailer without applying")
82 parser.add_argument('-r', '--rebase', nargs='?',
83 help="Rebase on the specified branch applying tags to all commits")
84 args = parser.parse_args()
85
86 # If we are rebasing, let git reinvoke this script with the rebase related
87 # arguments stripped.
88 if args.rebase is not None:
89 relevant_args = [sys.argv[0]]
90 if args.ack:
91 relevant_args.append("--ack")
92 elif args.test:
93 relevant_args.append("--test")
94
95 relevant_args += args.person
96
97 cmd = f"python3 {' '.join(relevant_args)}"
98 rebase = ['git', 'rebase', '--exec', cmd, args.rebase]
99
100 if args.dry_run:
101 print(' '.join([shlex.quote(s) for s in rebase]))
102 returncode = 0
103 else:
104 returncode = subprocess.run(rebase).returncode
105
106 sys.exit(returncode)
107
108 for p in args.person:
109 person = find_person(p)
110 if person is None:
111 print(f'Could not uniquely identify {p}, skipping')
112
113 trailer = 'Reviewed-by'
114 if args.ack:
115 trailer = 'Acked-by'
116 elif args.test:
117 trailer = 'Tested-by'
118
119 trailer = f'{trailer}: {person[0]} <{person[1]}>'
120
121 if args.dry_run:
122 print(trailer)
123 continue
124
125 diff_index = subprocess.run("git diff-index --quiet --cached HEAD --".split(" "))
126 if diff_index.returncode != 0:
127 print("You have staged changes.")
128 print("Please commit before applying review tags.")
129 sys.exit(1)
130
131 env = os.environ.copy()
132 env['GIT_EDITOR'] = f'git interpret-trailers --trailer "{trailer}" --in-place'
133 subprocess.run(["git", "commit", "--amend"], env=env)