Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 2 | set -e |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 3 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 4 | # This script reformats source files using various formatters and linters. |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 5 | # |
| 6 | # Files are changed in-place, so make sure you don't have anything open in an |
| 7 | # editor, and you may want to commit before formatting in case of awryness. |
| 8 | # |
| 9 | # This must be run on a clean repository to succeed |
| 10 | # |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 11 | function display_help() |
| 12 | { |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 13 | echo "usage: format-code.sh [-h | --help] [--no-diff] [--list-tools]" |
| 14 | echo " [--disable <tool>] [--enable <tool>] [<path>]" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 15 | echo |
| 16 | echo "Format and lint a repository." |
| 17 | echo |
| 18 | echo "Arguments:" |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 19 | echo " --list-tools Display available linters and formatters" |
| 20 | echo " --no-diff Don't show final diff output" |
| 21 | echo " --disable <tool> Disable linter" |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 22 | echo " --enable <tool> Enable only specific linters" |
Patrick Williams | 26d9d2c | 2022-12-05 15:03:07 -0600 | [diff] [blame] | 23 | echo " --allow-missing Run even if linters are not all present" |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 24 | echo " path Path to git repository (default to pwd)" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 25 | } |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 26 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 27 | LINTERS_ALL=( \ |
Patrick Williams | 0b1b8ec | 2023-12-25 14:19:10 -0600 | [diff] [blame] | 28 | commit_gitlint \ |
| 29 | commit_spelling \ |
| 30 | beautysh \ |
| 31 | beautysh_sh \ |
| 32 | black \ |
| 33 | clang_format \ |
Patrick Williams | c675d08 | 2025-06-09 09:30:46 -0400 | [diff] [blame] | 34 | clang_tidy \ |
Patrick Williams | 0b1b8ec | 2023-12-25 14:19:10 -0600 | [diff] [blame] | 35 | eslint \ |
| 36 | flake8 \ |
| 37 | isort \ |
| 38 | markdownlint \ |
Patrick Williams | 1ef5c60 | 2025-02-01 08:31:27 -0500 | [diff] [blame] | 39 | meson \ |
Patrick Williams | 0b1b8ec | 2023-12-25 14:19:10 -0600 | [diff] [blame] | 40 | prettier \ |
| 41 | shellcheck \ |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 42 | ) |
| 43 | LINTERS_DISABLED=() |
| 44 | LINTERS_ENABLED=() |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 45 | declare -A LINTERS_FAILED=() |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 46 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 47 | eval set -- "$(getopt -o 'h' --long 'help,list-tools,no-diff,disable:,enable:,allow-missing' -n 'format-code.sh' -- "$@")" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 48 | while true; do |
| 49 | case "$1" in |
| 50 | '-h'|'--help') |
| 51 | display_help && exit 0 |
| 52 | ;; |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 53 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 54 | '--list-tools') |
| 55 | echo "Available tools:" |
| 56 | for t in "${LINTERS_ALL[@]}"; do |
| 57 | echo " $t" |
| 58 | done |
| 59 | exit 0 |
| 60 | ;; |
| 61 | |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 62 | '--no-diff') |
| 63 | OPTION_NO_DIFF=1 |
| 64 | shift |
| 65 | ;; |
| 66 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 67 | '--disable') |
| 68 | LINTERS_DISABLED+=("$2") |
| 69 | shift && shift |
| 70 | ;; |
| 71 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 72 | '--enable') |
| 73 | LINTERS_ENABLED+=("$2") |
| 74 | shift && shift |
| 75 | ;; |
| 76 | |
Patrick Williams | 26d9d2c | 2022-12-05 15:03:07 -0600 | [diff] [blame] | 77 | '--allow-missing') |
| 78 | ALLOW_MISSING=yes |
| 79 | shift |
| 80 | ;; |
| 81 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 82 | '--') |
| 83 | shift |
| 84 | break |
| 85 | ;; |
| 86 | |
| 87 | *) |
| 88 | echo "unknown option: $1" |
| 89 | display_help && exit 1 |
| 90 | ;; |
| 91 | esac |
| 92 | done |
| 93 | |
| 94 | # Detect tty and set nicer colors. |
| 95 | if [ -t 1 ]; then |
| 96 | BLUE="\e[34m" |
| 97 | GREEN="\e[32m" |
| 98 | NORMAL="\e[0m" |
| 99 | RED="\e[31m" |
| 100 | YELLOW="\e[33m" |
| 101 | else # non-tty, no escapes. |
| 102 | BLUE="" |
| 103 | GREEN="" |
| 104 | NORMAL="" |
| 105 | RED="" |
| 106 | YELLOW="" |
| 107 | fi |
| 108 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 109 | # Allow called scripts to know which clang format we are using |
Patrick Williams | 0b1b8ec | 2023-12-25 14:19:10 -0600 | [diff] [blame] | 110 | export CLANG_FORMAT="clang-format" |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 111 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 112 | # Path to default config files for linters. |
| 113 | CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config" |
Patrick Williams | c675d08 | 2025-06-09 09:30:46 -0400 | [diff] [blame] | 114 | TOOLS_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/tools" |
| 115 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 116 | |
| 117 | # Find repository root for `pwd` or $1. |
| 118 | if [ -z "$1" ]; then |
| 119 | DIR="$(git rev-parse --show-toplevel || pwd)" |
| 120 | else |
| 121 | DIR="$(git -C "$1" rev-parse --show-toplevel)" |
| 122 | fi |
Patrick Williams | bc85b71 | 2022-12-05 15:16:27 -0600 | [diff] [blame] | 123 | if [ ! -e "$DIR/.git" ]; then |
| 124 | echo -e "${RED}Error:${NORMAL} Directory ($DIR) does not appear to be a git repository" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 125 | exit 1 |
| 126 | fi |
Andrew Jeffery | 0cce848 | 2018-04-30 11:37:01 +0930 | [diff] [blame] | 127 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 128 | cd "${DIR}" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 129 | echo -e " ${BLUE}Formatting code under${NORMAL} $DIR" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 130 | |
Patrick Williams | 3d29494 | 2022-12-05 14:58:15 -0600 | [diff] [blame] | 131 | # Config hashes: |
| 132 | # LINTER_REQUIRE - The requirements to run a linter, semi-colon separated. |
| 133 | # 1. Executable. |
| 134 | # 2. [optional] Configuration file. |
| 135 | # 3. [optional] Global fallback configuration file. |
| 136 | # |
| 137 | # LINTER_IGNORE - An optional set of semi-colon separated ignore-files |
| 138 | # specific to the linter. |
| 139 | # |
| 140 | # LINTER_TYPES - The file types supported by the linter, semi-colon separated. |
| 141 | # |
| 142 | # LINTER_CONFIG - The config (from LINTER_REQUIRE) chosen for the repository. |
| 143 | # |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 144 | declare -A LINTER_REQUIRE=() |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 145 | declare -A LINTER_IGNORE=() |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 146 | declare -A LINTER_TYPES=() |
Patrick Williams | 3d29494 | 2022-12-05 14:58:15 -0600 | [diff] [blame] | 147 | declare -A LINTER_CONFIG=() |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 148 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 149 | LINTER_REQUIRE+=([commit_spelling]="codespell") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 150 | LINTER_TYPES+=([commit_spelling]="commit") |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 151 | |
| 152 | commit_filename="$(mktemp)" |
| 153 | function clean_up_file() { |
| 154 | rm "$commit_filename" |
| 155 | } |
| 156 | trap clean_up_file EXIT |
| 157 | |
Manojkiran Eda | c0fab5e | 2024-09-09 13:50:49 +0530 | [diff] [blame] | 158 | function find_codespell_dict_file() { |
| 159 | local python_codespell_dict |
| 160 | # @formatter:off |
| 161 | python_codespell_dict=$(python3 -c " |
| 162 | import os.path as op |
| 163 | import codespell_lib |
| 164 | codespell_dir = op.dirname(codespell_lib.__file__) |
| 165 | codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt') |
| 166 | print(codespell_file if op.isfile(codespell_file) else '', end='') |
| 167 | " 2> /dev/null) |
| 168 | # @formatter:on |
| 169 | |
| 170 | # Return the path if found, otherwise return an empty string |
| 171 | echo "$python_codespell_dict" |
| 172 | } |
| 173 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 174 | function do_commit_spelling() { |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 175 | # Write the commit message to a temporary file |
Jonathan Doman | 1d5e00d | 2024-01-11 18:33:54 -0800 | [diff] [blame] | 176 | git log --format='%B' -1 > "$commit_filename" |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 177 | |
| 178 | # Some names or emails appear as false-positive misspellings, remove them |
| 179 | sed -i "s/Signed-off-by.*//" "$commit_filename" |
| 180 | |
Manojkiran Eda | c0fab5e | 2024-09-09 13:50:49 +0530 | [diff] [blame] | 181 | # Get the path to the dictionary.txt file |
| 182 | local codespell_dict |
| 183 | codespell_dict=$(find_codespell_dict_file) |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 184 | |
Manojkiran Eda | c0fab5e | 2024-09-09 13:50:49 +0530 | [diff] [blame] | 185 | # Check if the dictionary file was found |
| 186 | if [[ -z "$codespell_dict" ]]; then |
| 187 | echo "Error: Could not find dictionary.txt file" |
| 188 | exit 1 |
| 189 | fi |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 190 | |
Manojkiran Eda | c0fab5e | 2024-09-09 13:50:49 +0530 | [diff] [blame] | 191 | # Run the codespell with codespell dictionary on the patchset |
| 192 | echo -n "codespell-dictionary - misspelling count >> " |
| 193 | codespell -D "$codespell_dict" -d --count "$commit_filename" |
| 194 | |
| 195 | # Run the codespell with builtin dictionary on the patchset |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 196 | echo -n "generic-dictionary - misspelling count >> " |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 197 | codespell --builtin clear,rare,en-GB_to_en-US -d --count "$commit_filename" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 198 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 199 | function do_version_commit_spelling() { |
| 200 | echo codespell: "$(codespell --version)" |
| 201 | } |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 202 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 203 | LINTER_REQUIRE+=([commit_gitlint]="gitlint") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 204 | LINTER_TYPES+=([commit_gitlint]="commit") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 205 | function do_commit_gitlint() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 206 | gitlint --extra-path "${CONFIG_PATH}/gitlint/" \ |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 207 | --config "${CONFIG_PATH}/.gitlint" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 208 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 209 | function do_version_commit_gitlint() { |
| 210 | gitlint --version | awk '{ print $3 }' |
| 211 | } |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 212 | |
Patrick Williams | c596959 | 2022-12-06 09:50:25 -0600 | [diff] [blame] | 213 | # We need different function style for bash/zsh vs plain sh, so beautysh is |
| 214 | # split into two linters. "function foo()" is not traditionally accepted |
| 215 | # POSIX-shell syntax, so shellcheck barfs on it. |
| 216 | LINTER_REQUIRE+=([beautysh]="beautysh") |
| 217 | LINTER_IGNORE+=([beautysh]=".beautysh-ignore") |
| 218 | LINTER_TYPES+=([beautysh]="bash;zsh") |
| 219 | function do_beautysh() { |
| 220 | beautysh --force-function-style fnpar "$@" |
| 221 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 222 | function do_version_beautysh() { |
| 223 | beautysh --version |
| 224 | } |
Patrick Williams | c596959 | 2022-12-06 09:50:25 -0600 | [diff] [blame] | 225 | LINTER_REQUIRE+=([beautysh_sh]="beautysh") |
| 226 | LINTER_IGNORE+=([beautysh_sh]=".beautysh-ignore") |
| 227 | LINTER_TYPES+=([beautysh_sh]="sh") |
| 228 | function do_beautysh_sh() { |
| 229 | beautysh --force-function-style paronly "$@" |
| 230 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 231 | function do_version_beautysh_sh() { |
| 232 | beautysh --version |
| 233 | } |
Patrick Williams | c596959 | 2022-12-06 09:50:25 -0600 | [diff] [blame] | 234 | |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame] | 235 | LINTER_REQUIRE+=([black]="black") |
| 236 | LINTER_TYPES+=([black]="python") |
| 237 | function do_black() { |
Patrick Williams | 45b2152 | 2023-02-14 16:39:20 -0600 | [diff] [blame] | 238 | black -l 79 "$@" |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame] | 239 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 240 | function do_version_black() { |
| 241 | black --version | head -n1 |
| 242 | } |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame] | 243 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 244 | LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 245 | LINTER_IGNORE+=([eslint]=".eslintignore") |
| 246 | LINTER_TYPES+=([eslint]="json") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 247 | function do_eslint() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 248 | eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \ |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 249 | --ext .json --format=stylish \ |
| 250 | --resolve-plugins-relative-to /usr/local/lib/node_modules \ |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 251 | --no-error-on-unmatched-pattern "$@" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 252 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 253 | function do_version_eslint() { |
| 254 | eslint --version |
| 255 | } |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 256 | |
Patrick Williams | c5ad7ff | 2022-12-05 10:21:40 -0600 | [diff] [blame] | 257 | LINTER_REQUIRE+=([flake8]="flake8") |
| 258 | LINTER_IGNORE+=([flake8]=".flake8-ignore") |
| 259 | LINTER_TYPES+=([flake8]="python") |
| 260 | function do_flake8() { |
| 261 | flake8 --show-source --extend-ignore=E203,E501 "$@" |
| 262 | # We disable E203 and E501 because 'black' is handling these and they |
| 263 | # disagree on best practices. |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 264 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 265 | function do_version_flake8() { |
| 266 | flake8 --version |
| 267 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 268 | |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame] | 269 | LINTER_REQUIRE+=([isort]="isort") |
| 270 | LINTER_TYPES+=([isort]="python") |
| 271 | function do_isort() { |
| 272 | isort --profile black "$@" |
| 273 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 274 | function do_version_isort() { |
| 275 | isort --version-number |
| 276 | } |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame] | 277 | |
Patrick Williams | 7d41f6d | 2022-12-06 10:19:43 -0600 | [diff] [blame] | 278 | LINTER_REQUIRE+=([markdownlint]="markdownlint;.markdownlint.yaml;${CONFIG_PATH}/markdownlint.yaml") |
| 279 | LINTER_IGNORE+=([markdownlint]=".markdownlint-ignore") |
| 280 | LINTER_TYPES+=([markdownlint]="markdown") |
| 281 | function do_markdownlint() { |
Patrick Williams | 7d27a5b | 2025-06-25 10:19:51 -0400 | [diff] [blame] | 282 | markdownlint --config "${LINTER_CONFIG[markdownlint]}" -- "$@" |
Patrick Williams | 7d41f6d | 2022-12-06 10:19:43 -0600 | [diff] [blame] | 283 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 284 | function do_version_markdownlint() { |
| 285 | markdownlint --version |
| 286 | } |
Patrick Williams | 7d41f6d | 2022-12-06 10:19:43 -0600 | [diff] [blame] | 287 | |
Patrick Williams | 1ef5c60 | 2025-02-01 08:31:27 -0500 | [diff] [blame] | 288 | LINTER_REQUIRE+=([meson]="meson;meson.build") |
| 289 | LINTER_TYPES+=([meson]="meson") |
| 290 | function do_meson() { |
| 291 | meson format -i "$@" |
| 292 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 293 | function do_version_meson() { |
| 294 | meson --version |
| 295 | } |
Patrick Williams | 1ef5c60 | 2025-02-01 08:31:27 -0500 | [diff] [blame] | 296 | |
Patrick Williams | b08ddf7 | 2022-12-06 08:56:31 -0600 | [diff] [blame] | 297 | LINTER_REQUIRE+=([prettier]="prettier;.prettierrc.yaml;${CONFIG_PATH}/prettierrc.yaml") |
| 298 | LINTER_IGNORE+=([prettier]=".prettierignore") |
| 299 | LINTER_TYPES+=([prettier]="json;markdown;yaml") |
| 300 | function do_prettier() { |
| 301 | prettier --config "${LINTER_CONFIG[prettier]}" --write "$@" |
| 302 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 303 | function do_version_prettier() { |
| 304 | prettier --version |
| 305 | } |
Patrick Williams | b08ddf7 | 2022-12-06 08:56:31 -0600 | [diff] [blame] | 306 | |
Patrick Williams | e0df305 | 2022-12-05 10:22:00 -0600 | [diff] [blame] | 307 | LINTER_REQUIRE+=([shellcheck]="shellcheck") |
| 308 | LINTER_IGNORE+=([shellcheck]=".shellcheck-ignore") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 309 | LINTER_TYPES+=([shellcheck]="bash;sh") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 310 | function do_shellcheck() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 311 | shellcheck --color=never -x "$@" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 312 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 313 | function do_version_shellcheck() { |
| 314 | shellcheck --version | awk '/^version/ { print $2 }' |
| 315 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 316 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 317 | LINTER_REQUIRE+=([clang_format]="clang-format;.clang-format") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 318 | LINTER_IGNORE+=([clang_format]=".clang-ignore;.clang-format-ignore") |
| 319 | LINTER_TYPES+=([clang_format]="c;cpp") |
Patrick Williams | 476a7e9 | 2022-12-06 09:52:53 -0600 | [diff] [blame] | 320 | function do_clang_format() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 321 | "${CLANG_FORMAT}" -i "$@" |
| 322 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 323 | function do_version_clang_format() { |
| 324 | "${CLANG_FORMAT}" --version |
| 325 | } |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 326 | |
Patrick Williams | c675d08 | 2025-06-09 09:30:46 -0400 | [diff] [blame] | 327 | LINTER_REQUIRE+=([clang_tidy]="true") |
| 328 | LINTER_TYPES+=([clang_tidy]="clang-tidy-config") |
| 329 | function do_clang_tidy() { |
| 330 | "${TOOLS_PATH}/config-clang-tidy" format |
| 331 | } |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 332 | function do_version_clang_tidy() { |
| 333 | echo openbmc-build-scripts: "$(git rev-parse HEAD)" |
| 334 | } |
Patrick Williams | c675d08 | 2025-06-09 09:30:46 -0400 | [diff] [blame] | 335 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 336 | function get_file_type() |
| 337 | { |
| 338 | case "$(basename "$1")" in |
| 339 | # First to early detect template files. |
| 340 | *.in | *.meson) echo "meson-template" && return ;; |
| 341 | *.mako | *.mako.*) echo "mako" && return ;; |
Patrick Williams | d535ecb | 2022-12-02 14:54:18 -0600 | [diff] [blame] | 342 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 343 | *.ac) echo "autoconf" && return ;; |
| 344 | *.[ch]) echo "c" && return ;; |
| 345 | *.[ch]pp) echo "cpp" && return ;; |
| 346 | *.json) echo "json" && return ;; |
| 347 | *.md) echo "markdown" && return ;; |
| 348 | *.py) echo "python" && return ;; |
Patrick Williams | 5095cbe | 2022-12-08 06:50:34 -0600 | [diff] [blame] | 349 | *.tcl) echo "tcl" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 350 | *.yaml | *.yml) echo "yaml" && return ;; |
Patrick Williams | d535ecb | 2022-12-02 14:54:18 -0600 | [diff] [blame] | 351 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 352 | # Special files. |
| 353 | .git/COMMIT_EDITMSG) echo "commit" && return ;; |
Patrick Williams | c675d08 | 2025-06-09 09:30:46 -0400 | [diff] [blame] | 354 | .clang-format) echo "clang-format-config" && return ;; |
| 355 | .clang-tidy) echo "clang-tidy-config" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 356 | meson.build) echo "meson" && return ;; |
Patrick Williams | 1ef5c60 | 2025-02-01 08:31:27 -0500 | [diff] [blame] | 357 | meson.options) echo "meson" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 358 | esac |
| 359 | |
| 360 | case "$(file "$1")" in |
| 361 | *Bourne-Again\ shell*) echo "bash" && return ;; |
| 362 | *C++\ source*) echo "cpp" && return ;; |
| 363 | *C\ source*) echo "c" && return ;; |
| 364 | *JSON\ data*) echo "json" && return ;; |
| 365 | *POSIX\ shell*) echo "sh" && return ;; |
| 366 | *Python\ script*) echo "python" && return ;; |
Patrick Williams | d5d6395 | 2022-12-07 17:55:03 -0600 | [diff] [blame] | 367 | *python3\ script*) echo "python" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 368 | *zsh\ shell*) echo "zsh" && return ;; |
| 369 | esac |
| 370 | |
| 371 | echo "unknown" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 372 | } |
| 373 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 374 | LINTERS_AVAILABLE=() |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 375 | function check_linter() |
| 376 | { |
| 377 | TITLE="$1" |
| 378 | IFS=";" read -r -a ARGS <<< "$2" |
| 379 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 380 | if [[ "${LINTERS_DISABLED[*]}" =~ $1 ]]; then |
| 381 | return |
| 382 | fi |
| 383 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 384 | if [ 0 -ne "${#LINTERS_ENABLED[@]}" ]; then |
| 385 | if ! [[ "${LINTERS_ENABLED[*]}" =~ $1 ]]; then |
| 386 | return |
| 387 | fi |
| 388 | fi |
| 389 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 390 | EXE="${ARGS[0]}" |
| 391 | if [ ! -x "${EXE}" ]; then |
| 392 | if ! which "${EXE}" > /dev/null 2>&1 ; then |
| 393 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${EXE}" |
Patrick Williams | 26d9d2c | 2022-12-05 15:03:07 -0600 | [diff] [blame] | 394 | if [ -z "$ALLOW_MISSING" ]; then |
| 395 | exit 1 |
| 396 | fi |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 397 | return |
| 398 | fi |
| 399 | fi |
| 400 | |
| 401 | CONFIG="${ARGS[1]}" |
| 402 | FALLBACK="${ARGS[2]}" |
| 403 | |
| 404 | if [ -n "${CONFIG}" ]; then |
| 405 | if [ -e "${CONFIG}" ]; then |
| 406 | LINTER_CONFIG+=( [${TITLE}]="${CONFIG}" ) |
| 407 | elif [ -n "${FALLBACK}" ] && [ -e "${FALLBACK}" ]; then |
| 408 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${CONFIG}; using ${FALLBACK}" |
| 409 | LINTER_CONFIG+=( [${TITLE}]="${FALLBACK}" ) |
| 410 | else |
| 411 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find config ${CONFIG}" |
| 412 | return |
| 413 | fi |
| 414 | fi |
| 415 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 416 | LINTERS_AVAILABLE+=( "${TITLE}" ) |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 417 | } |
| 418 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 419 | # Check for a global .linter-ignore file. |
| 420 | GLOBAL_IGNORE=("cat") |
| 421 | if [ -e ".linter-ignore" ]; then |
| 422 | GLOBAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter" ".linter-ignore") |
| 423 | fi |
| 424 | |
| 425 | # Find all the files in the git repository and organize by type. |
| 426 | declare -A FILES=() |
Ed Tanous | 910732c | 2024-01-02 10:23:03 -0800 | [diff] [blame] | 427 | FILES+=([commit]=".git") |
| 428 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 429 | while read -r file; do |
| 430 | ftype="$(get_file_type "$file")" |
| 431 | FILES+=([$ftype]="$(echo -ne "$file;${FILES[$ftype]:-}")") |
Patrick Williams | f058440 | 2023-07-11 11:27:33 -0500 | [diff] [blame] | 432 | done < <(git ls-files | xargs realpath --relative-base=. | "${GLOBAL_IGNORE[@]}") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 433 | |
| 434 | # For each linter, check if there are an applicable files and if it can |
| 435 | # be enabled. |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 436 | for op in "${LINTERS_ALL[@]}"; do |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 437 | for ftype in ${LINTER_TYPES[$op]//;/ }; do |
| 438 | if [[ -v FILES["$ftype"] ]]; then |
| 439 | check_linter "$op" "${LINTER_REQUIRE[${op}]}" |
| 440 | break |
| 441 | fi |
| 442 | done |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 443 | done |
| 444 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 445 | # Call each linter. |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 446 | for op in "${LINTERS_AVAILABLE[@]}"; do |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 447 | |
| 448 | # Determine the linter-specific ignore file(s). |
| 449 | LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter") |
| 450 | if [[ -v LINTER_IGNORE["$op"] ]]; then |
| 451 | for ignorefile in ${LINTER_IGNORE["$op"]//;/ } ; do |
| 452 | if [ -e "$ignorefile" ]; then |
| 453 | LOCAL_IGNORE+=("$ignorefile") |
| 454 | fi |
| 455 | done |
| 456 | fi |
| 457 | if [ 1 -eq ${#LOCAL_IGNORE[@]} ]; then |
| 458 | LOCAL_IGNORE=("cat") |
| 459 | fi |
| 460 | |
| 461 | # Find all the files for this linter, filtering out the ignores. |
| 462 | LINTER_FILES=() |
| 463 | while read -r file ; do |
| 464 | if [ -e "$file" ]; then |
| 465 | LINTER_FILES+=("$file") |
| 466 | fi |
| 467 | done < <(for ftype in ${LINTER_TYPES[$op]//;/ }; do |
| 468 | # shellcheck disable=SC2001 |
| 469 | echo "${FILES["$ftype"]:-}" | sed "s/;/\\n/g" |
| 470 | done | "${LOCAL_IGNORE[@]}") |
| 471 | |
| 472 | # Call the linter now with all the files. |
Patrick Williams | 200ec18 | 2022-12-06 08:32:03 -0600 | [diff] [blame] | 473 | if [ 0 -ne ${#LINTER_FILES[@]} ]; then |
Andrew Jeffery | 1832ffa | 2025-06-26 10:31:09 +0930 | [diff] [blame^] | 474 | echo -e " ${BLUE}Running $op${NORMAL} ($(do_version_"$op"))" |
Patrick Williams | 200ec18 | 2022-12-06 08:32:03 -0600 | [diff] [blame] | 475 | if ! "do_$op" "${LINTER_FILES[@]}" ; then |
| 476 | LINTERS_FAILED+=([$op]=1) |
Chris Cain | 2f5d53b | 2025-05-06 16:49:32 -0500 | [diff] [blame] | 477 | echo -e " ${RED}$op - FAILED${NORMAL}" |
Patrick Williams | 200ec18 | 2022-12-06 08:32:03 -0600 | [diff] [blame] | 478 | fi |
| 479 | else |
| 480 | echo -e " ${YELLOW}${op}:${NORMAL} all applicable files are on ignore-lists" |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 481 | fi |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 482 | done |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 483 | |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 484 | # Check for failing linters. |
| 485 | if [ 0 -ne ${#LINTERS_FAILED[@]} ]; then |
| 486 | for op in "${!LINTERS_FAILED[@]}"; do |
Chris Cain | 2f5d53b | 2025-05-06 16:49:32 -0500 | [diff] [blame] | 487 | echo -e "$op: ${RED}FAILED${NORMAL} (see prior failure)" |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 488 | done |
| 489 | exit 1 |
| 490 | fi |
| 491 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 492 | # Check for differences. |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 493 | if [ -z "$OPTION_NO_DIFF" ]; then |
| 494 | echo -e " ${BLUE}Result differences...${NORMAL}" |
| 495 | if ! git --no-pager diff --exit-code ; then |
| 496 | echo -e "Format: ${RED}FAILED${NORMAL}" |
| 497 | exit 1 |
| 498 | else |
| 499 | echo -e "Format: ${GREEN}PASSED${NORMAL}" |
| 500 | fi |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 501 | fi |
| 502 | |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 503 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 504 | # For example, phosphor-mboxd. |
Patrick Williams | 330b077 | 2022-11-28 06:14:06 -0600 | [diff] [blame] | 505 | for formatter in "format-code.sh" "format-code"; do |
| 506 | if [[ -x "${formatter}" ]]; then |
| 507 | echo -e " ${BLUE}Calling secondary formatter:${NORMAL} ${formatter}" |
| 508 | "./${formatter}" |
| 509 | if [ -z "$OPTION_NO_DIFF" ]; then |
| 510 | git --no-pager diff --exit-code |
| 511 | fi |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 512 | fi |
Patrick Williams | 330b077 | 2022-11-28 06:14:06 -0600 | [diff] [blame] | 513 | done |