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=( \ |
| 28 | commit_gitlint \ |
| 29 | commit_spelling \ |
Patrick Williams | c596959 | 2022-12-06 09:50:25 -0600 | [diff] [blame] | 30 | beautysh \ |
| 31 | beautysh_sh \ |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame^] | 32 | black \ |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 33 | clang_format \ |
| 34 | eslint \ |
Patrick Williams | c5ad7ff | 2022-12-05 10:21:40 -0600 | [diff] [blame] | 35 | flake8 \ |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame^] | 36 | isort \ |
Patrick Williams | b08ddf7 | 2022-12-06 08:56:31 -0600 | [diff] [blame] | 37 | prettier \ |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 38 | shellcheck \ |
| 39 | ) |
| 40 | LINTERS_DISABLED=() |
| 41 | LINTERS_ENABLED=() |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 42 | declare -A LINTERS_FAILED=() |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 43 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 44 | 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] | 45 | while true; do |
| 46 | case "$1" in |
| 47 | '-h'|'--help') |
| 48 | display_help && exit 0 |
| 49 | ;; |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 50 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 51 | '--list-tools') |
| 52 | echo "Available tools:" |
| 53 | for t in "${LINTERS_ALL[@]}"; do |
| 54 | echo " $t" |
| 55 | done |
| 56 | exit 0 |
| 57 | ;; |
| 58 | |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 59 | '--no-diff') |
| 60 | OPTION_NO_DIFF=1 |
| 61 | shift |
| 62 | ;; |
| 63 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 64 | '--disable') |
| 65 | LINTERS_DISABLED+=("$2") |
| 66 | shift && shift |
| 67 | ;; |
| 68 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 69 | '--enable') |
| 70 | LINTERS_ENABLED+=("$2") |
| 71 | shift && shift |
| 72 | ;; |
| 73 | |
Patrick Williams | 26d9d2c | 2022-12-05 15:03:07 -0600 | [diff] [blame] | 74 | '--allow-missing') |
| 75 | ALLOW_MISSING=yes |
| 76 | shift |
| 77 | ;; |
| 78 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 79 | '--') |
| 80 | shift |
| 81 | break |
| 82 | ;; |
| 83 | |
| 84 | *) |
| 85 | echo "unknown option: $1" |
| 86 | display_help && exit 1 |
| 87 | ;; |
| 88 | esac |
| 89 | done |
| 90 | |
| 91 | # Detect tty and set nicer colors. |
| 92 | if [ -t 1 ]; then |
| 93 | BLUE="\e[34m" |
| 94 | GREEN="\e[32m" |
| 95 | NORMAL="\e[0m" |
| 96 | RED="\e[31m" |
| 97 | YELLOW="\e[33m" |
| 98 | else # non-tty, no escapes. |
| 99 | BLUE="" |
| 100 | GREEN="" |
| 101 | NORMAL="" |
| 102 | RED="" |
| 103 | YELLOW="" |
| 104 | fi |
| 105 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 106 | # Allow called scripts to know which clang format we are using |
| 107 | export CLANG_FORMAT="clang-format" |
| 108 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 109 | # Path to default config files for linters. |
| 110 | CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config" |
| 111 | |
| 112 | # Find repository root for `pwd` or $1. |
| 113 | if [ -z "$1" ]; then |
| 114 | DIR="$(git rev-parse --show-toplevel || pwd)" |
| 115 | else |
| 116 | DIR="$(git -C "$1" rev-parse --show-toplevel)" |
| 117 | fi |
Patrick Williams | bc85b71 | 2022-12-05 15:16:27 -0600 | [diff] [blame] | 118 | if [ ! -e "$DIR/.git" ]; then |
| 119 | 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] | 120 | exit 1 |
| 121 | fi |
Andrew Jeffery | 0cce848 | 2018-04-30 11:37:01 +0930 | [diff] [blame] | 122 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 123 | cd "${DIR}" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 124 | echo -e " ${BLUE}Formatting code under${NORMAL} $DIR" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 125 | |
Patrick Williams | 3d29494 | 2022-12-05 14:58:15 -0600 | [diff] [blame] | 126 | # Config hashes: |
| 127 | # LINTER_REQUIRE - The requirements to run a linter, semi-colon separated. |
| 128 | # 1. Executable. |
| 129 | # 2. [optional] Configuration file. |
| 130 | # 3. [optional] Global fallback configuration file. |
| 131 | # |
| 132 | # LINTER_IGNORE - An optional set of semi-colon separated ignore-files |
| 133 | # specific to the linter. |
| 134 | # |
| 135 | # LINTER_TYPES - The file types supported by the linter, semi-colon separated. |
| 136 | # |
| 137 | # LINTER_CONFIG - The config (from LINTER_REQUIRE) chosen for the repository. |
| 138 | # |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 139 | declare -A LINTER_REQUIRE=() |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 140 | declare -A LINTER_IGNORE=() |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 141 | declare -A LINTER_TYPES=() |
Patrick Williams | 3d29494 | 2022-12-05 14:58:15 -0600 | [diff] [blame] | 142 | declare -A LINTER_CONFIG=() |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 143 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 144 | LINTER_REQUIRE+=([commit_spelling]="codespell") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 145 | LINTER_TYPES+=([commit_spelling]="commit") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 146 | function do_commit_spelling() { |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 147 | # Run the codespell with openbmc spcific spellings on the patchset |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 148 | echo -n "openbmc-dictionary - misspelling count >> " |
| 149 | sed "s/Signed-off-by.*//" "$@" | \ |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 150 | codespell -D "${CONFIG_PATH}/openbmc-spelling.txt" -d --count - |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 151 | |
| 152 | # Run the codespell with generic dictionary on the patchset |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 153 | echo -n "generic-dictionary - misspelling count >> " |
| 154 | sed "s/Signed-off-by.*//" "$@" | \ |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 155 | codespell --builtin clear,rare,en-GB_to_en-US -d --count - |
| 156 | } |
| 157 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 158 | LINTER_REQUIRE+=([commit_gitlint]="gitlint") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 159 | LINTER_TYPES+=([commit_gitlint]="commit") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 160 | function do_commit_gitlint() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 161 | gitlint --extra-path "${CONFIG_PATH}/gitlint/" \ |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 162 | --config "${CONFIG_PATH}/.gitlint" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Patrick Williams | c596959 | 2022-12-06 09:50:25 -0600 | [diff] [blame] | 165 | # We need different function style for bash/zsh vs plain sh, so beautysh is |
| 166 | # split into two linters. "function foo()" is not traditionally accepted |
| 167 | # POSIX-shell syntax, so shellcheck barfs on it. |
| 168 | LINTER_REQUIRE+=([beautysh]="beautysh") |
| 169 | LINTER_IGNORE+=([beautysh]=".beautysh-ignore") |
| 170 | LINTER_TYPES+=([beautysh]="bash;zsh") |
| 171 | function do_beautysh() { |
| 172 | beautysh --force-function-style fnpar "$@" |
| 173 | } |
| 174 | LINTER_REQUIRE+=([beautysh_sh]="beautysh") |
| 175 | LINTER_IGNORE+=([beautysh_sh]=".beautysh-ignore") |
| 176 | LINTER_TYPES+=([beautysh_sh]="sh") |
| 177 | function do_beautysh_sh() { |
| 178 | beautysh --force-function-style paronly "$@" |
| 179 | } |
| 180 | |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame^] | 181 | LINTER_REQUIRE+=([black]="black") |
| 182 | LINTER_TYPES+=([black]="python") |
| 183 | function do_black() { |
| 184 | black -l 79 --preview "$@" |
| 185 | } |
| 186 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 187 | LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 188 | LINTER_IGNORE+=([eslint]=".eslintignore") |
| 189 | LINTER_TYPES+=([eslint]="json") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 190 | function do_eslint() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 191 | eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \ |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 192 | --ext .json --format=stylish \ |
| 193 | --resolve-plugins-relative-to /usr/local/lib/node_modules \ |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 194 | --no-error-on-unmatched-pattern "$@" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 195 | } |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 196 | |
Patrick Williams | c5ad7ff | 2022-12-05 10:21:40 -0600 | [diff] [blame] | 197 | LINTER_REQUIRE+=([flake8]="flake8") |
| 198 | LINTER_IGNORE+=([flake8]=".flake8-ignore") |
| 199 | LINTER_TYPES+=([flake8]="python") |
| 200 | function do_flake8() { |
| 201 | flake8 --show-source --extend-ignore=E203,E501 "$@" |
| 202 | # We disable E203 and E501 because 'black' is handling these and they |
| 203 | # disagree on best practices. |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 204 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 205 | |
Patrick Williams | e795dfe | 2022-12-06 10:07:02 -0600 | [diff] [blame^] | 206 | LINTER_REQUIRE+=([isort]="isort") |
| 207 | LINTER_TYPES+=([isort]="python") |
| 208 | function do_isort() { |
| 209 | isort --profile black "$@" |
| 210 | } |
| 211 | |
Patrick Williams | b08ddf7 | 2022-12-06 08:56:31 -0600 | [diff] [blame] | 212 | LINTER_REQUIRE+=([prettier]="prettier;.prettierrc.yaml;${CONFIG_PATH}/prettierrc.yaml") |
| 213 | LINTER_IGNORE+=([prettier]=".prettierignore") |
| 214 | LINTER_TYPES+=([prettier]="json;markdown;yaml") |
| 215 | function do_prettier() { |
| 216 | prettier --config "${LINTER_CONFIG[prettier]}" --write "$@" |
| 217 | } |
| 218 | |
Patrick Williams | e0df305 | 2022-12-05 10:22:00 -0600 | [diff] [blame] | 219 | LINTER_REQUIRE+=([shellcheck]="shellcheck") |
| 220 | LINTER_IGNORE+=([shellcheck]=".shellcheck-ignore") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 221 | LINTER_TYPES+=([shellcheck]="bash;sh") |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 222 | function do_shellcheck() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 223 | shellcheck --color=never -x "$@" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 224 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 225 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 226 | LINTER_REQUIRE+=([clang_format]="clang-format;.clang-format") |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 227 | LINTER_IGNORE+=([clang_format]=".clang-ignore;.clang-format-ignore") |
| 228 | LINTER_TYPES+=([clang_format]="c;cpp") |
Patrick Williams | 476a7e9 | 2022-12-06 09:52:53 -0600 | [diff] [blame] | 229 | function do_clang_format() { |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 230 | "${CLANG_FORMAT}" -i "$@" |
| 231 | } |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 232 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 233 | function get_file_type() |
| 234 | { |
| 235 | case "$(basename "$1")" in |
| 236 | # First to early detect template files. |
| 237 | *.in | *.meson) echo "meson-template" && return ;; |
| 238 | *.mako | *.mako.*) echo "mako" && return ;; |
Patrick Williams | d535ecb | 2022-12-02 14:54:18 -0600 | [diff] [blame] | 239 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 240 | *.ac) echo "autoconf" && return ;; |
| 241 | *.[ch]) echo "c" && return ;; |
| 242 | *.[ch]pp) echo "cpp" && return ;; |
| 243 | *.json) echo "json" && return ;; |
| 244 | *.md) echo "markdown" && return ;; |
| 245 | *.py) echo "python" && return ;; |
Patrick Williams | 5095cbe | 2022-12-08 06:50:34 -0600 | [diff] [blame] | 246 | *.tcl) echo "tcl" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 247 | *.yaml | *.yml) echo "yaml" && return ;; |
Patrick Williams | d535ecb | 2022-12-02 14:54:18 -0600 | [diff] [blame] | 248 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 249 | # Special files. |
| 250 | .git/COMMIT_EDITMSG) echo "commit" && return ;; |
| 251 | meson.build) echo "meson" && return ;; |
| 252 | esac |
| 253 | |
| 254 | case "$(file "$1")" in |
| 255 | *Bourne-Again\ shell*) echo "bash" && return ;; |
| 256 | *C++\ source*) echo "cpp" && return ;; |
| 257 | *C\ source*) echo "c" && return ;; |
| 258 | *JSON\ data*) echo "json" && return ;; |
| 259 | *POSIX\ shell*) echo "sh" && return ;; |
| 260 | *Python\ script*) echo "python" && return ;; |
Patrick Williams | d5d6395 | 2022-12-07 17:55:03 -0600 | [diff] [blame] | 261 | *python3\ script*) echo "python" && return ;; |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 262 | *zsh\ shell*) echo "zsh" && return ;; |
| 263 | esac |
| 264 | |
| 265 | echo "unknown" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 266 | } |
| 267 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 268 | LINTERS_AVAILABLE=() |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 269 | function check_linter() |
| 270 | { |
| 271 | TITLE="$1" |
| 272 | IFS=";" read -r -a ARGS <<< "$2" |
| 273 | |
Patrick Williams | 147f37a | 2022-12-04 14:57:10 -0600 | [diff] [blame] | 274 | if [[ "${LINTERS_DISABLED[*]}" =~ $1 ]]; then |
| 275 | return |
| 276 | fi |
| 277 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 278 | if [ 0 -ne "${#LINTERS_ENABLED[@]}" ]; then |
| 279 | if ! [[ "${LINTERS_ENABLED[*]}" =~ $1 ]]; then |
| 280 | return |
| 281 | fi |
| 282 | fi |
| 283 | |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 284 | EXE="${ARGS[0]}" |
| 285 | if [ ! -x "${EXE}" ]; then |
| 286 | if ! which "${EXE}" > /dev/null 2>&1 ; then |
| 287 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${EXE}" |
Patrick Williams | 26d9d2c | 2022-12-05 15:03:07 -0600 | [diff] [blame] | 288 | if [ -z "$ALLOW_MISSING" ]; then |
| 289 | exit 1 |
| 290 | fi |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 291 | return |
| 292 | fi |
| 293 | fi |
| 294 | |
| 295 | CONFIG="${ARGS[1]}" |
| 296 | FALLBACK="${ARGS[2]}" |
| 297 | |
| 298 | if [ -n "${CONFIG}" ]; then |
| 299 | if [ -e "${CONFIG}" ]; then |
| 300 | LINTER_CONFIG+=( [${TITLE}]="${CONFIG}" ) |
| 301 | elif [ -n "${FALLBACK}" ] && [ -e "${FALLBACK}" ]; then |
| 302 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${CONFIG}; using ${FALLBACK}" |
| 303 | LINTER_CONFIG+=( [${TITLE}]="${FALLBACK}" ) |
| 304 | else |
| 305 | echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find config ${CONFIG}" |
| 306 | return |
| 307 | fi |
| 308 | fi |
| 309 | |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 310 | LINTERS_AVAILABLE+=( "${TITLE}" ) |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 313 | # Check for a global .linter-ignore file. |
| 314 | GLOBAL_IGNORE=("cat") |
| 315 | if [ -e ".linter-ignore" ]; then |
| 316 | GLOBAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter" ".linter-ignore") |
| 317 | fi |
| 318 | |
| 319 | # Find all the files in the git repository and organize by type. |
| 320 | declare -A FILES=() |
| 321 | if [ -e .git/COMMIT_EDITMSG ]; then |
| 322 | FILES+=([commit]=".git/COMMIT_EDITMSG") |
| 323 | fi |
| 324 | while read -r file; do |
| 325 | ftype="$(get_file_type "$file")" |
| 326 | FILES+=([$ftype]="$(echo -ne "$file;${FILES[$ftype]:-}")") |
| 327 | done < <(git ls-files | "${GLOBAL_IGNORE[@]}") |
| 328 | |
| 329 | # For each linter, check if there are an applicable files and if it can |
| 330 | # be enabled. |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 331 | for op in "${LINTERS_ALL[@]}"; do |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 332 | for ftype in ${LINTER_TYPES[$op]//;/ }; do |
| 333 | if [[ -v FILES["$ftype"] ]]; then |
| 334 | check_linter "$op" "${LINTER_REQUIRE[${op}]}" |
| 335 | break |
| 336 | fi |
| 337 | done |
Patrick Williams | dad9b7a | 2022-11-30 13:10:20 -0600 | [diff] [blame] | 338 | done |
| 339 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 340 | # Call each linter. |
Patrick Williams | 816c7cc | 2022-12-08 05:51:57 -0600 | [diff] [blame] | 341 | for op in "${LINTERS_AVAILABLE[@]}"; do |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 342 | |
| 343 | # Determine the linter-specific ignore file(s). |
| 344 | LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter") |
| 345 | if [[ -v LINTER_IGNORE["$op"] ]]; then |
| 346 | for ignorefile in ${LINTER_IGNORE["$op"]//;/ } ; do |
| 347 | if [ -e "$ignorefile" ]; then |
| 348 | LOCAL_IGNORE+=("$ignorefile") |
| 349 | fi |
| 350 | done |
| 351 | fi |
| 352 | if [ 1 -eq ${#LOCAL_IGNORE[@]} ]; then |
| 353 | LOCAL_IGNORE=("cat") |
| 354 | fi |
| 355 | |
| 356 | # Find all the files for this linter, filtering out the ignores. |
| 357 | LINTER_FILES=() |
| 358 | while read -r file ; do |
| 359 | if [ -e "$file" ]; then |
| 360 | LINTER_FILES+=("$file") |
| 361 | fi |
| 362 | done < <(for ftype in ${LINTER_TYPES[$op]//;/ }; do |
| 363 | # shellcheck disable=SC2001 |
| 364 | echo "${FILES["$ftype"]:-}" | sed "s/;/\\n/g" |
| 365 | done | "${LOCAL_IGNORE[@]}") |
| 366 | |
| 367 | # Call the linter now with all the files. |
Patrick Williams | 200ec18 | 2022-12-06 08:32:03 -0600 | [diff] [blame] | 368 | if [ 0 -ne ${#LINTER_FILES[@]} ]; then |
| 369 | echo -e " ${BLUE}Running $op${NORMAL}" |
| 370 | if ! "do_$op" "${LINTER_FILES[@]}" ; then |
| 371 | LINTERS_FAILED+=([$op]=1) |
| 372 | fi |
| 373 | else |
| 374 | echo -e " ${YELLOW}${op}:${NORMAL} all applicable files are on ignore-lists" |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 375 | fi |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 376 | done |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 377 | |
Patrick Williams | 785327a | 2022-12-05 16:20:43 -0600 | [diff] [blame] | 378 | # Check for failing linters. |
| 379 | if [ 0 -ne ${#LINTERS_FAILED[@]} ]; then |
| 380 | for op in "${!LINTERS_FAILED[@]}"; do |
| 381 | echo -e "$op: ${RED}FAILED${NORMAL}" |
| 382 | done |
| 383 | exit 1 |
| 384 | fi |
| 385 | |
Patrick Williams | 71b7324 | 2022-12-02 21:07:52 -0600 | [diff] [blame] | 386 | # Check for differences. |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 387 | if [ -z "$OPTION_NO_DIFF" ]; then |
| 388 | echo -e " ${BLUE}Result differences...${NORMAL}" |
| 389 | if ! git --no-pager diff --exit-code ; then |
| 390 | echo -e "Format: ${RED}FAILED${NORMAL}" |
| 391 | exit 1 |
| 392 | else |
| 393 | echo -e "Format: ${GREEN}PASSED${NORMAL}" |
| 394 | fi |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame] | 395 | fi |
| 396 | |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 397 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 398 | # For example, phosphor-mboxd. |
Patrick Williams | 330b077 | 2022-11-28 06:14:06 -0600 | [diff] [blame] | 399 | for formatter in "format-code.sh" "format-code"; do |
| 400 | if [[ -x "${formatter}" ]]; then |
| 401 | echo -e " ${BLUE}Calling secondary formatter:${NORMAL} ${formatter}" |
| 402 | "./${formatter}" |
| 403 | if [ -z "$OPTION_NO_DIFF" ]; then |
| 404 | git --no-pager diff --exit-code |
| 405 | fi |
Patrick Williams | d27ab4c | 2022-11-27 17:51:35 -0600 | [diff] [blame] | 406 | fi |
Patrick Williams | 330b077 | 2022-11-28 06:14:06 -0600 | [diff] [blame] | 407 | done |