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('-d', '--dry-run', action='store_true',
80 help="Print trailer without applying")
81 parser.add_argument('-r', '--rebase', nargs='?',
82 help="Rebase on the specified branch applying tags to all commits")
83 args = parser.parse_args()
84
85 # If we are rebasing, let git reinvoke this script with the rebase related
86 # arguments stripped.
87 if args.rebase is not None:
88 relevant_args = [sys.argv[0]]
89 if args.ack:
90 relevant_args.append("--ack")
91
92 relevant_args += args.person
93
94 cmd = f"python3 {' '.join(relevant_args)}"
95 rebase = ['git', 'rebase', '--exec', cmd, args.rebase]
96
97 if args.dry_run:
98 print(' '.join([shlex.quote(s) for s in rebase]))
99 returncode = 0
100 else:
101 returncode = subprocess.run(rebase).returncode
102
103 sys.exit(returncode)
104
105 for p in args.person:
106 person = find_person(p)
107 if person is None:
108 print(f'Could not uniquely identify {p}, skipping')
109
110 trailer = 'Acked-by' if args.ack else 'Reviewed-by'
111 trailer = f'{trailer}: {person[0]} <{person[1]}>'
112
113 if args.dry_run:
114 print(trailer)
115 continue
116
117 diff_index = subprocess.run("git diff-index --quiet --cached HEAD --".split(" "))
118 if diff_index.returncode != 0:
119 print("You have staged changes.")
120 print("Please commit before applying review tags.")
121 sys.exit(1)
122
123 env = os.environ.copy()
124 env['GIT_EDITOR'] = f'git interpret-trailers --trailer "{trailer}" --in-place'
125 subprocess.run(["git", "commit", "--amend"], env=env)