Repository navigation
Expand file tree
/
Copy pathpr-check
More file actions
executable file
·251 lines (241 loc) · 12.3 KB
/
Copy pathpr-check
File metadata and controls
executable file
·251 lines (241 loc) · 12.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/bash
# SPDX-License-Identifier: MIT
# The policy checks a pull request must pass (AGENTS.md section 1, and
# CONTRIBUTING.md section 6), on the commits between BASE and HEAD:
#
# - no identifiers in added lines or commit messages: MAC and Bluetooth
# addresses (locally administered ones, like 02:..., are allowed:
# they're made up), a `getprop` dump, a serial number assignment, an
# IMEI;
# - no vendor blobs: firmware, partition images, archives, compiled
# modules and libraries, APKs;
# - no Signed-off-by from an AI agent (the human signs off; a
# Co-Authored-By or Assisted-by trailer is fine);
# - every shell script the commits touch still parses.
#
# Identifiers, blobs and AI Signed-off-by trailers are checked on every
# commit in the range: a later commit removing or replacing the value does
# not clear the check; rewrite the named commit and force-push.
#
# tools/pr-check [-C REPO] [BASE [HEAD]] (default: this repository, origin/main, HEAD)
#
# -C checks another repository's commits the same way (tools/kernel-pr-check
# uses it for the kernel).
#
# CI runs it on every pull request (.github/workflows/checks.yml). Run it
# before you push; it reads git only and changes nothing.
set -euo pipefail
self=$(readlink -f "${BASH_SOURCE[0]}")
repo=$(dirname -- "$self")/..
if [[ ${1:-} == -C ]]; then
repo=${2:?pr-check: -C needs a repository}
shift 2
fi
cd -- "$repo"
base=${1:-origin/main}
head=${2:-HEAD}
for r in "$base" "$head"; do
git rev-parse --verify -q "$r^{commit}" >/dev/null || { echo "pr-check: no such commit $r (git fetch first?)" >&2; exit 2; }
done
mb=$(git merge-base "$base" "$head") || { echo "pr-check: $base and $head share no history" >&2; exit 2; }
range="$mb..$head"
fails=0
fail() { echo "not ok - $*"; fails=$((fails + 1)); }
pass() { echo "ok - $*"; }
commits=$(git rev-list --no-merges "$range")
merges=$(git rev-list --merges "$range")
if [[ -z $commits && -z $merges ]]; then
pass "no commits to check against $base"
exit 0
fi
# ---------------------------------------------------------------- identifiers
# Added lines of every commit in the range (and lines newly introduced by any
# merge commit relative to all its parents), plus the message of every commit
# in the range, with the commit and file (or "(commit message)") each came
# from; this file's own patterns are skipped.
added=$(git -c core.quotePath=false -c log.showRoot=true log --full-history --cc --no-color --no-ext-diff --no-textconv \
--src-prefix=a/ --dst-prefix=b/ --unified=0 --format='%x1f%h %P' "$range" \
-- . ':(exclude)tools/pr-check' ':(exclude)tests/pr-check' \
| awk '
/^\037/ {
sha = substr($1, 2)
np = NF - 1; if (np < 1) np = 1
plus = ""; for (i = 0; i < np; i++) plus = plus "+"
f = ""; in_hunk = 0; next
}
/^diff --(git|cc) / { f = ""; in_hunk = 0; next }
!in_hunk && /^\+\+\+ b\// { f = substr($0, 7); next }
!in_hunk && /^\+\+\+ "b\// { f = substr($0, 8); sub(/"$/, "", f); next }
!in_hunk && /^\+\+\+ \/dev\/null$/ { f = ""; next }
/^@@+ / { in_hunk = 1; next }
in_hunk && f != "" && substr($0, 1, np) == plus {
print sha " " f ": " substr($0, np + 1)
}
')
msgs=$(git log --encoding=UTF-8 --format='%x1f%h%n%w(0,1,1)%B' "$range" \
| awk '
/^\037/ { sha = substr($0, 2); next }
sha != "" && /^ / && $0 != " " { print sha " (commit message): " substr($0, 2) }
')
[[ -z $msgs ]] || added+=${added:+$'\n'}$msgs
# A MAC is six octets joined all by colons or all by hyphens, in any case,
# with no hex digit or further octet on either side; a label before it
# (mac:, id:, wlan0-) still counts. 12 bare hex digits count in a MAC
# record: mac, macaddr, mac_address, hwaddr, bdaddr, bd_addr or ether, then
# =, : or white space. Each is read in the colon form, and one whose first
# octet is locally administered unicast (group bit clear: x2, x6, xA, xE) is
# made up; so are all zeros, all ones and RFC
E2CB
7042's documentation range
# (00:00:5e:00:53:xx). Six decimal fields joined by hyphens that read as
# a date and time, YY-MM-DD-HH-MM-SS (month 01-12, day 01-31, hour 00-23,
# minute and second 00-59), are a timestamp, such as a screenshot's name;
# with colons or bare in a MAC record the same digits are still an address.
# Each address on a line is judged on its own, so an exempt one doesn't
# hide a real one beside it. No interval expressions: mawk can't parse them.
macs=$(awk 'BEGIN {
o = "[0-9a-f][0-9a-f]"; c = o; h = o; b = o
for (i = 1; i < 6; i++) { c = c ":" o; h = h "-" o; b = b o }
re[1] = "(^|[^0-9a-f:]|[g-z_][0-9a-f]*:)" c "([^0-9a-f:]|$|:([^0-9a-f]|$))"
re[2] = "(^|[^0-9a-f-]|[g-z_][0-9a-f]*-)" h "([^0-9a-f-]|$|-([^0-9a-f]|$))"
re[3] = "(^|[^a-z])(mac([_-]?addr(ess)?)?|(hw|bd)[_-]?addr(ess)?|ether)[\"\047]?([ \t]*[=:][ \t]*|[ \t]+)[\"\047]?" b "([^0-9a-f]|$)"
any = o "[:-]" o "|" b
ts = "^[0-9][0-9]:(0[1-9]|1[0-2]):(0[1-9]|[12][0-9]|3[01]):([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"
}
{
line = tolower($0); real = 0
if (line !~ any) next
for (k = 1; k <= 3; k++) {
rest = line
while (match(rest, re[k])) {
m = substr(rest, RSTART, RLENGTH); rest = substr(rest, RSTART + RLENGTH - 1)
sub(/[^0-9a-f]+$/, "", m); gsub(/[:-]/, "", m); m = substr(m, length(m) - 11)
for (i = 10; i > 0; i -= 2) m = substr(m, 1, i) ":" substr(m, i + 1)
if (m ~ /^.[26ae]:/ || m == "00:00:00:00:00:00" || m == "ff:ff:ff:ff:ff:ff" || m ~ /^00:00:5e:00:53:/) continue
if (k == 2 && m ~ ts) continue
real = 1
}
}
if (real) print
}' <<<"$added")
[[ -z $macs ]] && pass "no MAC or Bluetooth addresses" || fail "MAC or Bluetooth addresses (use a placeholder such as <MAC>):
$(sed 's/^/ /' <<<"$macs" | sed -n '1,10p')"
dumps=$(grep -E '\[(ro|persist|vendor|sys)\.[a-z0-9_.]+\]: \[' <<<"$added" || true)
[[ -z $dumps ]] && pass "no getprop dumps" || fail "getprop output (quote single facts, never a dump):
$(sed 's/^/ /' <<<"$dumps" | sed -n '1,5p')"
# grep in the caller's locale picks serial assignments and flags lines
# with no exempt term (<serial>, SERIAL_PLACEHOLDER, FAKE..., $var,
# ${var}) as before. On base-exempted lines, awk (C locale, ASCII white
# space) strips "<sha> <path>: " (a path holding ": " leaves its tail in
# the text, stricter only), reads each assignment in the added text, and
# fails the line when any value is not itself exempt. A value ends before
# ASCII white space, quote, ;, ,, =, :, { or } (keeping ${ whole). No
# interval expressions or POSIX classes in awk: every awk parses it.
hits=$(grep -iE '(serialno|ro\.serialno|androidboot\.serialno|serial[ _-]?(number)?)[[:space:]]*[:=][[:space:]]*["'\'']?[0-9A-Z]{8,}' <<<"$added" || true)
serial_ex='<serial>|SERIAL_PLACEHOLDER|FAKE[0-9A-Z]*|\$\{?[a-z_]+'
serials=""
if [[ -n $hits ]]; then
serials=$(
grep -viE "$serial_ex" <<<"$hits" || true
{ grep -iE "$serial_ex" <<<"$hits" || true; } | LC_ALL=C awk 'BEGIN {
a = "[0-9a-z]"; s8 = a; for (i = 1; i < 8; i++) s8 = s8 a
pfx = "(serialno|ro\\.serialno|androidboot\\.serialno|serial[ _-]?(number)?)[ \t\r\f\v]*[:=][ \t\r\f\v]*[\"\047]?"
re = pfx s8 "([^ \t\r\f\v\"\047;,=:{}]|[$][{])*"
exempt = "<serial>|serial_placeholder|fake[0-9a-z]*|[$][{]?[a-z_]+"
}
{
line = tolower($0)
sub(/^[^ ]* /, "", line); i = index(line, ": "); if (i) line = substr(line, i + 2)
while (match(line, re)) {
v = substr(line, RSTART, RLENGTH); sub("^" pfx, "", v)
line = substr(line, RSTART + RLENGTH - length(v))
if (v !~ exempt) { print; next }
}
}'
)
fi
[[ -z $serials ]] && pass "no serial numbers" || fail "serial numbers (use <SERIAL>):
$(sed 's/^/ /' <<<"$serials" | sed -n '1,5p')"
imeis=$(grep -iE 'imei[^0-9]{0,12}[0-9]{15}([^0-9]|$)' <<<"$added" || true)
[[ -z $imeis ]] && pass "no IMEIs" || fail "IMEIs:
$(sed 's/^/ /' <<<"$imeis" | sed -n '1,5p')"
# ---------------------------------------------------------------- blobs
blob_ext='\.(bin|img|fw|mbn|hcd|elf|ko|so|so\.[0-9.]+|apk|zip|tar|tgz|tar\.[a-z0-9]+|zst|xz|lz4|gz|7z|ftb|wmfw|bmp|tlv)$'
raw_blobs=$(git -c core.quotePath=false -c log.showRoot=true log --no-merges --numstat --no-renames --diff-filter=d --format='%x1f%h' "$range" \
| awk -F'\t' '/^\037/ { sha = substr($1, 2); next } $1 == "-" || $2 == "-" { print sha " " $3 }')
raw_named=$(git -c core.quotePath=false -c log.showRoot=true log --no-merges --name-status --find-renames --diff-filter=AR --format='%x1f%h' "$range" \
| BLOB_EXT="$blob_ext" awk -F'\t' '
BEGIN { ext = ENVIRON["BLOB_EXT"] }
/^\037/ { sha = substr($1, 2); next }
$1 == "A" { print sha " " $2; next }
$1 ~ /^R/ && tolower($2) !~ ext { print sha " " $3 }
')
if [[ -n $merges ]]; then
merge_blobs=$(git -c core.quotePath=false -c log.showRoot=true log --merges --cc --no-color --no-renames --no-ext-diff --no-textconv --unified=0 --format='%x1f%h' "$range" \
| awk '
/^\037/ { sha = substr($0, 2); f = ""; in_hunk = 0; changed = 0; next }
/^diff --cc / { f = substr($0, 11); in_hunk = 0; changed = 0; next }
!in_hunk && /^index / {
i = index($2, "..")
if (i > 0) {
res = substr($2, i + 2)
n = split(substr($2, 1, i - 1), p, ",")
changed = (res !~ /^0+$/)
for (j = 1; j <= n; j++) if (p[j] == res) changed = 0
}
next
}
/^@@+ / { in_hunk = 1; next }
!in_hunk && /^new file mode / && f != "" { print "N " sha " " f; next }
!in_hunk && /^Binary files differ$/ && changed && f != "" { print "B " sha " " f }
')
while IFS= read -r line; do
case $line in
'B '*) raw_blobs+=${raw_blobs:+$'\n'}${line#B } ;;
'N '*) raw_named+=${raw_named:+$'\n'}${line#N } ;;
esac
done <<<"$merge_blobs"
fi
blobs=$(grep -vE '\.(png|jpe?g|svg|webp|ico|woff2?|ttf|otf)$' <<<"$raw_blobs" || true)
named=$(grep -iE "$blob_ext" <<<"$raw_named" || true)
all_blobs=$(printf '%s\n%s\n' "$blobs" "$named" | sed '/^$/d' | sort -u)
[[ -z $all_blobs ]] && pass "no vendor blobs or binaries" || fail "binaries or vendor files (our scripts extract firmware from the user's own factory image; nothing is committed):
$(sed 's/^/ /' <<<"$all_blobs" | sed -n '1,10p')"
# ---------------------------------------------------------------- trailers
# Each name as a whole word or with a short glued suffix (ai, bot, agent,
# code, cli) and no letter outside it, so a person called Tillman, or an
# address at precursor.example, isn't taken for an AI.
ai='(^|[^[:alpha:]])(claude|anthropic|openai|chatgpt|codex|copilot|gemini|cursor|llm)(ai|bot|agent|code|cli)?([^[:alpha:]]|$)|noreply@github\.com.*bot|\[bot\]'
bad_sob=""
for c in $commits; do
while IFS= read -r line; do
[[ -n $line ]] && bad_sob+="$(git log -1 --format=%h "$c") $line"$'\n'
done < <(git log -1 --format=%B "$c" | grep -iE "^Signed-off-by:.*($ai)" || true)
done
[[ -z $bad_sob ]] && pass "no Signed-off-by from an AI agent" || fail "Signed-off-by from an AI agent (the human signs off; keep the agent in Co-Authored-By or Assisted-by):
$(sed 's/^/ /' <<<"${bad_sob%$'\n'}")"
# ---------------------------------------------------------------- scripts
broken=""
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
while IFS= read -r f; do
git show "$head:$f" > "$tmp" 2>/dev/null || continue
# tr, not head -1: uutils' head writes again at the end of a short file,
# after head -1 has gone, so pipefail ended pr-check. tr reads to the end
# and drops NULs as $(...) did.
first=$(head -c 64 "$tmp" | tr -d '\0')
first=${first%%$'\n'*}
case $first in
'#!/bin/bash'*|'#!/usr/bin/env bash'*) bash -n "$tmp" 2>/dev/null || broken+="$f"$'\n' ;;
'#!/bin/sh'*|'#!/bin/busybox sh'*) sh -n "$tmp" 2>/dev/null || broken+="$f"$'\n' ;;
'#!/usr/bin/env python3'*|'#!/usr/bin/python3'*)
python3 -B -c 'import ast,sys; ast.parse(open(sys.argv[1]).read())' "$tmp" 2>/dev/null || broken+="$f"$'\n' ;;
esac
done < <(git diff --name-only --diff-filter=AM "$range")
[[ -z $broken ]] && pass "touched scripts parse" || fail "scripts that don't parse:
$(sed 's/^/ /' <<<"${broken%$'\n'}")"
if ((fails)); then
if [[ -n $macs || -n $dumps || -n $serials || -n $imeis || -n $all_blobs || -n $bad_sob ]]; then
echo "pr-check: a later commit removing or replacing the value does not clear this check; rewrite the named commit and force-push" >&2
fi
echo "pr-check: $fails check(s) failed" >&2
exit 1
fi