| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright 2025 The Fuchsia Authors |
| 4 | # |
| 5 | # Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 6 | # <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 7 | # license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 8 | # This file may not be copied, modified, or distributed except according to |
| 9 | # those terms. |
| 10 | |
| 11 | set -euo pipefail |
| 12 | |
| 13 | # This allows us to leave XODO comments in this file and have them still be |
| 14 | # picked up by this script without having the script itself trigger false |
| 15 | # positives. The alternative would be to exclude this script entirely, which |
| 16 | # would mean that we couldn't use XODO comments in this script. |
| 17 | KEYWORD=$(echo XODO | sed -e 's/X/T/') |
| 18 | |
| 19 | # Make sure `rg` is installed (if this fails, `set -e` above will cause the |
| 20 | # script to exit). |
| 21 | rg --version >/dev/null |
| 22 | |
| 23 | # -H: Print filename (default for multiple files/recursive) |
| 24 | # -n: Print line number |
| 25 | # -w: Match whole words |
| 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 |
| 31 | 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 |