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