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