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