blob: 2982443ced860fb437fd007a8c29e07e5243567b [file] [log] [blame]
Andrew Geissler9b0105c2018-01-10 10:58:35 -08001#!/bin/bash
Patrick Williamsc1513c82022-11-27 17:37:08 -06002set -e
Andrew Geissler9b0105c2018-01-10 10:58:35 -08003
Patrick Williamsc1513c82022-11-27 17:37:08 -06004# This script reformats source files using various formatters and linters.
Andrew Geissler9b0105c2018-01-10 10:58:35 -08005#
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 Williamsc1513c82022-11-27 17:37:08 -060011function display_help()
12{
Patrick Williams816c7cc2022-12-08 05:51:57 -060013 echo "usage: format-code.sh [-h | --help] [--no-diff] [--list-tools]"
14 echo " [--disable <tool>] [--enable <tool>] [<path>]"
Patrick Williamsc1513c82022-11-27 17:37:08 -060015 echo
16 echo "Format and lint a repository."
17 echo
18 echo "Arguments:"
Patrick Williams147f37a2022-12-04 14:57:10 -060019 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 Williams816c7cc2022-12-08 05:51:57 -060022 echo " --enable <tool> Enable only specific linters"
Patrick Williams26d9d2c2022-12-05 15:03:07 -060023 echo " --allow-missing Run even if linters are not all present"
Patrick Williams147f37a2022-12-04 14:57:10 -060024 echo " path Path to git repository (default to pwd)"
Patrick Williamsc1513c82022-11-27 17:37:08 -060025}
Andrew Geissler9b0105c2018-01-10 10:58:35 -080026
Patrick Williams147f37a2022-12-04 14:57:10 -060027LINTERS_ALL=( \
Patrick Williams0b1b8ec2023-12-25 14:19:10 -060028 commit_gitlint \
29 commit_spelling \
30 beautysh \
31 beautysh_sh \
32 black \
33 clang_format \
34 eslint \
35 flake8 \
36 isort \
37 markdownlint \
Patrick Williams1ef5c602025-02-01 08:31:27 -050038 meson \
Patrick Williams0b1b8ec2023-12-25 14:19:10 -060039 prettier \
40 shellcheck \
Patrick Williams147f37a2022-12-04 14:57:10 -060041 )
42LINTERS_DISABLED=()
43LINTERS_ENABLED=()
Patrick Williams785327a2022-12-05 16:20:43 -060044declare -A LINTERS_FAILED=()
Patrick Williams147f37a2022-12-04 14:57:10 -060045
Patrick Williams816c7cc2022-12-08 05:51:57 -060046eval set -- "$(getopt -o 'h' --long 'help,list-tools,no-diff,disable:,enable:,allow-missing' -n 'format-code.sh' -- "$@")"
Patrick Williamsc1513c82022-11-27 17:37:08 -060047while true; do
48 case "$1" in
49 '-h'|'--help')
50 display_help && exit 0
51 ;;
Andrew Geissler9b0105c2018-01-10 10:58:35 -080052
Patrick Williams147f37a2022-12-04 14:57:10 -060053 '--list-tools')
54 echo "Available tools:"
55 for t in "${LINTERS_ALL[@]}"; do
56 echo " $t"
57 done
58 exit 0
59 ;;
60
Patrick Williamsd27ab4c2022-11-27 17:51:35 -060061 '--no-diff')
62 OPTION_NO_DIFF=1
63 shift
64 ;;
65
Patrick Williams147f37a2022-12-04 14:57:10 -060066 '--disable')
67 LINTERS_DISABLED+=("$2")
68 shift && shift
69 ;;
70
Patrick Williams816c7cc2022-12-08 05:51:57 -060071 '--enable')
72 LINTERS_ENABLED+=("$2")
73 shift && shift
74 ;;
75
Patrick Williams26d9d2c2022-12-05 15:03:07 -060076 '--allow-missing')
77 ALLOW_MISSING=yes
78 shift
79 ;;
80
Patrick Williamsc1513c82022-11-27 17:37:08 -060081 '--')
82 shift
83 break
84 ;;
85
86 *)
87 echo "unknown option: $1"
88 display_help && exit 1
89 ;;
90 esac
91done
92
93# Detect tty and set nicer colors.
94if [ -t 1 ]; then
95 BLUE="\e[34m"
96 GREEN="\e[32m"
97 NORMAL="\e[0m"
98 RED="\e[31m"
99 YELLOW="\e[33m"
100else # non-tty, no escapes.
101 BLUE=""
102 GREEN=""
103 NORMAL=""
104 RED=""
105 YELLOW=""
106fi
107
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600108# Allow called scripts to know which clang format we are using
Patrick Williams0b1b8ec2023-12-25 14:19:10 -0600109export CLANG_FORMAT="clang-format"
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600110
Patrick Williamsc1513c82022-11-27 17:37:08 -0600111# Path to default config files for linters.
112CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config"
113
114# Find repository root for `pwd` or $1.
115if [ -z "$1" ]; then
116 DIR="$(git rev-parse --show-toplevel || pwd)"
117else
118 DIR="$(git -C "$1" rev-parse --show-toplevel)"
119fi
Patrick Williamsbc85b712022-12-05 15:16:27 -0600120if [ ! -e "$DIR/.git" ]; then
121 echo -e "${RED}Error:${NORMAL} Directory ($DIR) does not appear to be a git repository"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600122 exit 1
123fi
Andrew Jeffery0cce8482018-04-30 11:37:01 +0930124
Manojkiran Edae6f120a2021-03-20 11:13:43 +0530125cd "${DIR}"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600126echo -e " ${BLUE}Formatting code under${NORMAL} $DIR"
Andrew Geissler9b0105c2018-01-10 10:58:35 -0800127
Patrick Williams3d294942022-12-05 14:58:15 -0600128# Config hashes:
129# LINTER_REQUIRE - The requirements to run a linter, semi-colon separated.
130# 1. Executable.
131# 2. [optional] Configuration file.
132# 3. [optional] Global fallback configuration file.
133#
134# LINTER_IGNORE - An optional set of semi-colon separated ignore-files
135# specific to the linter.
136#
137# LINTER_TYPES - The file types supported by the linter, semi-colon separated.
138#
139# LINTER_CONFIG - The config (from LINTER_REQUIRE) chosen for the repository.
140#
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600141declare -A LINTER_REQUIRE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600142declare -A LINTER_IGNORE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600143declare -A LINTER_TYPES=()
Patrick Williams3d294942022-12-05 14:58:15 -0600144declare -A LINTER_CONFIG=()
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530145
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600146LINTER_REQUIRE+=([commit_spelling]="codespell")
Patrick Williams71b73242022-12-02 21:07:52 -0600147LINTER_TYPES+=([commit_spelling]="commit")
Ed Tanous910732c2024-01-02 10:23:03 -0800148
149commit_filename="$(mktemp)"
150function clean_up_file() {
151 rm "$commit_filename"
152}
153trap clean_up_file EXIT
154
Manojkiran Edac0fab5e2024-09-09 13:50:49 +0530155function find_codespell_dict_file() {
156 local python_codespell_dict
157 # @formatter:off
158 python_codespell_dict=$(python3 -c "
159import os.path as op
160import codespell_lib
161codespell_dir = op.dirname(codespell_lib.__file__)
162codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
163print(codespell_file if op.isfile(codespell_file) else '', end='')
164" 2> /dev/null)
165 # @formatter:on
166
167 # Return the path if found, otherwise return an empty string
168 echo "$python_codespell_dict"
169}
170
Patrick Williams38515a32022-11-27 06:58:56 -0600171function do_commit_spelling() {
Ed Tanous910732c2024-01-02 10:23:03 -0800172 # Write the commit message to a temporary file
Jonathan Doman1d5e00d2024-01-11 18:33:54 -0800173 git log --format='%B' -1 > "$commit_filename"
Ed Tanous910732c2024-01-02 10:23:03 -0800174
175 # Some names or emails appear as false-positive misspellings, remove them
176 sed -i "s/Signed-off-by.*//" "$commit_filename"
177
Manojkiran Edac0fab5e2024-09-09 13:50:49 +0530178 # Get the path to the dictionary.txt file
179 local codespell_dict
180 codespell_dict=$(find_codespell_dict_file)
Ed Tanous910732c2024-01-02 10:23:03 -0800181
Manojkiran Edac0fab5e2024-09-09 13:50:49 +0530182 # Check if the dictionary file was found
183 if [[ -z "$codespell_dict" ]]; then
184 echo "Error: Could not find dictionary.txt file"
185 exit 1
186 fi
Patrick Williams38515a32022-11-27 06:58:56 -0600187
Manojkiran Edac0fab5e2024-09-09 13:50:49 +0530188 # Run the codespell with codespell dictionary on the patchset
189 echo -n "codespell-dictionary - misspelling count >> "
190 codespell -D "$codespell_dict" -d --count "$commit_filename"
191
192 # Run the codespell with builtin dictionary on the patchset
Patrick Williams71b73242022-12-02 21:07:52 -0600193 echo -n "generic-dictionary - misspelling count >> "
Ed Tanous910732c2024-01-02 10:23:03 -0800194 codespell --builtin clear,rare,en-GB_to_en-US -d --count "$commit_filename"
Patrick Williams38515a32022-11-27 06:58:56 -0600195}
196
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600197LINTER_REQUIRE+=([commit_gitlint]="gitlint")
Patrick Williams71b73242022-12-02 21:07:52 -0600198LINTER_TYPES+=([commit_gitlint]="commit")
Patrick Williams38515a32022-11-27 06:58:56 -0600199function do_commit_gitlint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600200 gitlint --extra-path "${CONFIG_PATH}/gitlint/" \
Patrick Williamsc1513c82022-11-27 17:37:08 -0600201 --config "${CONFIG_PATH}/.gitlint"
Patrick Williams38515a32022-11-27 06:58:56 -0600202}
203
Patrick Williamsc5969592022-12-06 09:50:25 -0600204# We need different function style for bash/zsh vs plain sh, so beautysh is
205# split into two linters. "function foo()" is not traditionally accepted
206# POSIX-shell syntax, so shellcheck barfs on it.
207LINTER_REQUIRE+=([beautysh]="beautysh")
208LINTER_IGNORE+=([beautysh]=".beautysh-ignore")
209LINTER_TYPES+=([beautysh]="bash;zsh")
210function do_beautysh() {
211 beautysh --force-function-style fnpar "$@"
212}
213LINTER_REQUIRE+=([beautysh_sh]="beautysh")
214LINTER_IGNORE+=([beautysh_sh]=".beautysh-ignore")
215LINTER_TYPES+=([beautysh_sh]="sh")
216function do_beautysh_sh() {
217 beautysh --force-function-style paronly "$@"
218}
219
Patrick Williamse795dfe2022-12-06 10:07:02 -0600220LINTER_REQUIRE+=([black]="black")
221LINTER_TYPES+=([black]="python")
222function do_black() {
Patrick Williams45b21522023-02-14 16:39:20 -0600223 black -l 79 "$@"
Patrick Williamse795dfe2022-12-06 10:07:02 -0600224}
225
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600226LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json")
Patrick Williams71b73242022-12-02 21:07:52 -0600227LINTER_IGNORE+=([eslint]=".eslintignore")
228LINTER_TYPES+=([eslint]="json")
Patrick Williams38515a32022-11-27 06:58:56 -0600229function do_eslint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600230 eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600231 --ext .json --format=stylish \
232 --resolve-plugins-relative-to /usr/local/lib/node_modules \
Patrick Williams71b73242022-12-02 21:07:52 -0600233 --no-error-on-unmatched-pattern "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600234}
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530235
Patrick Williamsc5ad7ff2022-12-05 10:21:40 -0600236LINTER_REQUIRE+=([flake8]="flake8")
237LINTER_IGNORE+=([flake8]=".flake8-ignore")
238LINTER_TYPES+=([flake8]="python")
239function do_flake8() {
240 flake8 --show-source --extend-ignore=E203,E501 "$@"
241 # We disable E203 and E501 because 'black' is handling these and they
242 # disagree on best practices.
Patrick Williams38515a32022-11-27 06:58:56 -0600243}
James Feistf1665d62019-11-22 09:06:33 -0800244
Patrick Williamse795dfe2022-12-06 10:07:02 -0600245LINTER_REQUIRE+=([isort]="isort")
246LINTER_TYPES+=([isort]="python")
247function do_isort() {
248 isort --profile black "$@"
249}
250
Patrick Williams7d41f6d2022-12-06 10:19:43 -0600251LINTER_REQUIRE+=([markdownlint]="markdownlint;.markdownlint.yaml;${CONFIG_PATH}/markdownlint.yaml")
252LINTER_IGNORE+=([markdownlint]=".markdownlint-ignore")
253LINTER_TYPES+=([markdownlint]="markdown")
254function do_markdownlint() {
Patrick Williamsf20083b2025-06-10 16:10:23 -0400255 markdownlint --config "${LINTER_CONFIG[markdownlint]}" -- "$@" || \
Patrick Williams7d41f6d2022-12-06 10:19:43 -0600256 echo -e " ${YELLOW}Failed markdownlint; temporarily ignoring."
257 # We disable line-length because prettier should handle prose wrap for us.
258}
259
Patrick Williams1ef5c602025-02-01 08:31:27 -0500260LINTER_REQUIRE+=([meson]="meson;meson.build")
261LINTER_TYPES+=([meson]="meson")
262function do_meson() {
263 meson format -i "$@"
264}
265
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600266LINTER_REQUIRE+=([prettier]="prettier;.prettierrc.yaml;${CONFIG_PATH}/prettierrc.yaml")
267LINTER_IGNORE+=([prettier]=".prettierignore")
268LINTER_TYPES+=([prettier]="json;markdown;yaml")
269function do_prettier() {
270 prettier --config "${LINTER_CONFIG[prettier]}" --write "$@"
271}
272
Patrick Williamse0df3052022-12-05 10:22:00 -0600273LINTER_REQUIRE+=([shellcheck]="shellcheck")
274LINTER_IGNORE+=([shellcheck]=".shellcheck-ignore")
Patrick Williams71b73242022-12-02 21:07:52 -0600275LINTER_TYPES+=([shellcheck]="bash;sh")
Patrick Williams38515a32022-11-27 06:58:56 -0600276function do_shellcheck() {
Patrick Williams71b73242022-12-02 21:07:52 -0600277 shellcheck --color=never -x "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600278}
James Feistf1665d62019-11-22 09:06:33 -0800279
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600280LINTER_REQUIRE+=([clang_format]="clang-format;.clang-format")
Patrick Williams71b73242022-12-02 21:07:52 -0600281LINTER_IGNORE+=([clang_format]=".clang-ignore;.clang-format-ignore")
282LINTER_TYPES+=([clang_format]="c;cpp")
Patrick Williams476a7e92022-12-06 09:52:53 -0600283function do_clang_format() {
Patrick Williams71b73242022-12-02 21:07:52 -0600284 "${CLANG_FORMAT}" -i "$@"
285}
Patrick Williams38515a32022-11-27 06:58:56 -0600286
Patrick Williams71b73242022-12-02 21:07:52 -0600287function get_file_type()
288{
289 case "$(basename "$1")" in
290 # First to early detect template files.
291 *.in | *.meson) echo "meson-template" && return ;;
292 *.mako | *.mako.*) echo "mako" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600293
Patrick Williams71b73242022-12-02 21:07:52 -0600294 *.ac) echo "autoconf" && return ;;
295 *.[ch]) echo "c" && return ;;
296 *.[ch]pp) echo "cpp" && return ;;
297 *.json) echo "json" && return ;;
298 *.md) echo "markdown" && return ;;
299 *.py) echo "python" && return ;;
Patrick Williams5095cbe2022-12-08 06:50:34 -0600300 *.tcl) echo "tcl" && return ;;
Patrick Williams71b73242022-12-02 21:07:52 -0600301 *.yaml | *.yml) echo "yaml" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600302
Patrick Williams71b73242022-12-02 21:07:52 -0600303 # Special files.
304 .git/COMMIT_EDITMSG) echo "commit" && return ;;
305 meson.build) echo "meson" && return ;;
Patrick Williams1ef5c602025-02-01 08:31:27 -0500306 meson.options) echo "meson" && return ;;
Patrick Williams71b73242022-12-02 21:07:52 -0600307 esac
308
309 case "$(file "$1")" in
310 *Bourne-Again\ shell*) echo "bash" && return ;;
311 *C++\ source*) echo "cpp" && return ;;
312 *C\ source*) echo "c" && return ;;
313 *JSON\ data*) echo "json" && return ;;
314 *POSIX\ shell*) echo "sh" && return ;;
315 *Python\ script*) echo "python" && return ;;
Patrick Williamsd5d63952022-12-07 17:55:03 -0600316 *python3\ script*) echo "python" && return ;;
Patrick Williams71b73242022-12-02 21:07:52 -0600317 *zsh\ shell*) echo "zsh" && return ;;
318 esac
319
320 echo "unknown"
Patrick Williams38515a32022-11-27 06:58:56 -0600321}
322
Patrick Williams816c7cc2022-12-08 05:51:57 -0600323LINTERS_AVAILABLE=()
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600324function check_linter()
325{
326 TITLE="$1"
327 IFS=";" read -r -a ARGS <<< "$2"
328
Patrick Williams147f37a2022-12-04 14:57:10 -0600329 if [[ "${LINTERS_DISABLED[*]}" =~ $1 ]]; then
330 return
331 fi
332
Patrick Williams816c7cc2022-12-08 05:51:57 -0600333 if [ 0 -ne "${#LINTERS_ENABLED[@]}" ]; then
334 if ! [[ "${LINTERS_ENABLED[*]}" =~ $1 ]]; then
335 return
336 fi
337 fi
338
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600339 EXE="${ARGS[0]}"
340 if [ ! -x "${EXE}" ]; then
341 if ! which "${EXE}" > /dev/null 2>&1 ; then
342 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${EXE}"
Patrick Williams26d9d2c2022-12-05 15:03:07 -0600343 if [ -z "$ALLOW_MISSING" ]; then
344 exit 1
345 fi
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600346 return
347 fi
348 fi
349
350 CONFIG="${ARGS[1]}"
351 FALLBACK="${ARGS[2]}"
352
353 if [ -n "${CONFIG}" ]; then
354 if [ -e "${CONFIG}" ]; then
355 LINTER_CONFIG+=( [${TITLE}]="${CONFIG}" )
356 elif [ -n "${FALLBACK}" ] && [ -e "${FALLBACK}" ]; then
357 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${CONFIG}; using ${FALLBACK}"
358 LINTER_CONFIG+=( [${TITLE}]="${FALLBACK}" )
359 else
360 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find config ${CONFIG}"
361 return
362 fi
363 fi
364
Patrick Williams816c7cc2022-12-08 05:51:57 -0600365 LINTERS_AVAILABLE+=( "${TITLE}" )
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600366}
367
Patrick Williams71b73242022-12-02 21:07:52 -0600368# Check for a global .linter-ignore file.
369GLOBAL_IGNORE=("cat")
370if [ -e ".linter-ignore" ]; then
371 GLOBAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter" ".linter-ignore")
372fi
373
374# Find all the files in the git repository and organize by type.
375declare -A FILES=()
Ed Tanous910732c2024-01-02 10:23:03 -0800376FILES+=([commit]=".git")
377
Patrick Williams71b73242022-12-02 21:07:52 -0600378while read -r file; do
379 ftype="$(get_file_type "$file")"
380 FILES+=([$ftype]="$(echo -ne "$file;${FILES[$ftype]:-}")")
Patrick Williamsf0584402023-07-11 11:27:33 -0500381done < <(git ls-files | xargs realpath --relative-base=. | "${GLOBAL_IGNORE[@]}")
Patrick Williams71b73242022-12-02 21:07:52 -0600382
383# For each linter, check if there are an applicable files and if it can
384# be enabled.
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600385for op in "${LINTERS_ALL[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600386 for ftype in ${LINTER_TYPES[$op]//;/ }; do
387 if [[ -v FILES["$ftype"] ]]; then
388 check_linter "$op" "${LINTER_REQUIRE[${op}]}"
389 break
390 fi
391 done
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600392done
393
Patrick Williams71b73242022-12-02 21:07:52 -0600394# Call each linter.
Patrick Williams816c7cc2022-12-08 05:51:57 -0600395for op in "${LINTERS_AVAILABLE[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600396
397 # Determine the linter-specific ignore file(s).
398 LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter")
399 if [[ -v LINTER_IGNORE["$op"] ]]; then
400 for ignorefile in ${LINTER_IGNORE["$op"]//;/ } ; do
401 if [ -e "$ignorefile" ]; then
402 LOCAL_IGNORE+=("$ignorefile")
403 fi
404 done
405 fi
406 if [ 1 -eq ${#LOCAL_IGNORE[@]} ]; then
407 LOCAL_IGNORE=("cat")
408 fi
409
410 # Find all the files for this linter, filtering out the ignores.
411 LINTER_FILES=()
412 while read -r file ; do
413 if [ -e "$file" ]; then
414 LINTER_FILES+=("$file")
415 fi
416 done < <(for ftype in ${LINTER_TYPES[$op]//;/ }; do
417 # shellcheck disable=SC2001
418 echo "${FILES["$ftype"]:-}" | sed "s/;/\\n/g"
419 done | "${LOCAL_IGNORE[@]}")
420
421 # Call the linter now with all the files.
Patrick Williams200ec182022-12-06 08:32:03 -0600422 if [ 0 -ne ${#LINTER_FILES[@]} ]; then
423 echo -e " ${BLUE}Running $op${NORMAL}"
424 if ! "do_$op" "${LINTER_FILES[@]}" ; then
425 LINTERS_FAILED+=([$op]=1)
Chris Cain2f5d53b2025-05-06 16:49:32 -0500426 echo -e " ${RED}$op - FAILED${NORMAL}"
Patrick Williams200ec182022-12-06 08:32:03 -0600427 fi
428 else
429 echo -e " ${YELLOW}${op}:${NORMAL} all applicable files are on ignore-lists"
Patrick Williams785327a2022-12-05 16:20:43 -0600430 fi
Patrick Williams38515a32022-11-27 06:58:56 -0600431done
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030432
Patrick Williams785327a2022-12-05 16:20:43 -0600433# Check for failing linters.
434if [ 0 -ne ${#LINTERS_FAILED[@]} ]; then
435 for op in "${!LINTERS_FAILED[@]}"; do
Chris Cain2f5d53b2025-05-06 16:49:32 -0500436 echo -e "$op: ${RED}FAILED${NORMAL} (see prior failure)"
Patrick Williams785327a2022-12-05 16:20:43 -0600437 done
438 exit 1
439fi
440
Patrick Williams71b73242022-12-02 21:07:52 -0600441# Check for differences.
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600442if [ -z "$OPTION_NO_DIFF" ]; then
443 echo -e " ${BLUE}Result differences...${NORMAL}"
444 if ! git --no-pager diff --exit-code ; then
445 echo -e "Format: ${RED}FAILED${NORMAL}"
446 exit 1
447 else
448 echo -e "Format: ${GREEN}PASSED${NORMAL}"
449 fi
Patrick Williamsc1513c82022-11-27 17:37:08 -0600450fi
451
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030452# Sometimes your situation is terrible enough that you need the flexibility.
453# For example, phosphor-mboxd.
Patrick Williams330b0772022-11-28 06:14:06 -0600454for formatter in "format-code.sh" "format-code"; do
455 if [[ -x "${formatter}" ]]; then
456 echo -e " ${BLUE}Calling secondary formatter:${NORMAL} ${formatter}"
457 "./${formatter}"
458 if [ -z "$OPTION_NO_DIFF" ]; then
459 git --no-pager diff --exit-code
460 fi
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600461 fi
Patrick Williams330b0772022-11-28 06:14:06 -0600462done