Skip to content

Commit 33f6cbd

Browse files
[ci] In check_todo.sh, permit disabling check (#2839)
* [ci] In check_todo.sh, permit disabling check Support `TODO-check-disable` and `TODO-check-enable` to toggle the check within a particular file. gherrit-pr-id: I0ca32fb0b6e87d3be51ff69487a4f221c0a9ee45 * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 8f910fc commit 33f6cbd

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

ci/check_todo.sh

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,53 @@ rg --version >/dev/null
2323
# -H: Print filename (default for multiple files/recursive)
2424
# -n: Print line number
2525
# -w: Match whole words
26-
output=$(rg -H -n -w "$KEYWORD" || true)
27-
28-
if [ -n "$output" ]; then
29-
echo "Found $KEYWORD markers in the codebase." >&2
30-
echo "$KEYWORD is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase, use FIXME." >&2
31-
echo "" >&2
32-
echo "$output" >&2
33-
exit 1
26+
# Match TODO, TODO-check-disable, and TODO-check-enable
27+
output=$(rg -H -n -w "$KEYWORD|$KEYWORD-check-disable|$KEYWORD-check-enable" "$@" || true)
28+
29+
if [ -z "$output" ]; then
30+
exit 0
3431
fi
32+
33+
# Track the disabled state for each file. Since we process lines in order for
34+
# each file, we can maintain state. However, rg output might interleave files if
35+
# not sorted, but usually it's grouped. To be safe, we sort the output by
36+
# filename and line number.
37+
sorted_output=$(echo "$output" | sort -t: -k1,1 -k2,2n)
38+
39+
exit_code=0
40+
current_file=""
41+
disabled=0
42+
43+
while IFS= read -r line; do
44+
if [[ "$line" =~ ^(.*):([0-9]+):(.*)$ ]]; then
45+
file="${BASH_REMATCH[1]}"
46+
content="${BASH_REMATCH[3]}"
47+
else
48+
echo "Error: could not parse rg output line: $line" >&2
49+
exit 1
50+
fi
51+
52+
if [ "$file" != "$current_file" ]; then
53+
current_file="$file"
54+
disabled=0
55+
fi
56+
57+
if [[ "$content" == *"$KEYWORD-check-disable"* ]]; then
58+
disabled=1
59+
elif [[ "$content" == *"$KEYWORD-check-enable"* ]]; then
60+
disabled=0
61+
elif [[ "$content" == *"$KEYWORD"* ]]; then
62+
if [ "$disabled" -eq 0 ]; then
63+
if [ "$exit_code" -eq 0 ]; then
64+
echo "Found $KEYWORD markers in the codebase." >&2
65+
echo "$KEYWORD is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase, use FIXME." >&2
66+
echo "If you need to allow a $KEYWORD, wrap it in $KEYWORD-check-disable and $KEYWORD-check-enable." >&2
67+
echo "" >&2
68+
exit_code=1
69+
fi
70+
echo "$line" >&2
71+
fi
72+
fi
73+
done <<< "$sorted_output"
74+
75+
exit $exit_code

0 commit comments

Comments
 (0)