blob: 17a6d1e3aa1e756dfa6c7464d464d1c4e147cabd [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=( \
28 commit_gitlint \
29 commit_spelling \
Patrick Williamsc5969592022-12-06 09:50:25 -060030 beautysh \
31 beautysh_sh \
Patrick Williams147f37a2022-12-04 14:57:10 -060032 clang_format \
33 eslint \
Patrick Williamsc5ad7ff2022-12-05 10:21:40 -060034 flake8 \
Patrick Williamsb08ddf72022-12-06 08:56:31 -060035 prettier \
Patrick Williams147f37a2022-12-04 14:57:10 -060036 shellcheck \
37 )
38LINTERS_DISABLED=()
39LINTERS_ENABLED=()
Patrick Williams785327a2022-12-05 16:20:43 -060040declare -A LINTERS_FAILED=()
Patrick Williams147f37a2022-12-04 14:57:10 -060041
Patrick Williams816c7cc2022-12-08 05:51:57 -060042eval 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 -060043while true; do
44 case "$1" in
45 '-h'|'--help')
46 display_help && exit 0
47 ;;
Andrew Geissler9b0105c2018-01-10 10:58:35 -080048
Patrick Williams147f37a2022-12-04 14:57:10 -060049 '--list-tools')
50 echo "Available tools:"
51 for t in "${LINTERS_ALL[@]}"; do
52 echo " $t"
53 done
54 exit 0
55 ;;
56
Patrick Williamsd27ab4c2022-11-27 17:51:35 -060057 '--no-diff')
58 OPTION_NO_DIFF=1
59 shift
60 ;;
61
Patrick Williams147f37a2022-12-04 14:57:10 -060062 '--disable')
63 LINTERS_DISABLED+=("$2")
64 shift && shift
65 ;;
66
Patrick Williams816c7cc2022-12-08 05:51:57 -060067 '--enable')
68 LINTERS_ENABLED+=("$2")
69 shift && shift
70 ;;
71
Patrick Williams26d9d2c2022-12-05 15:03:07 -060072 '--allow-missing')
73 ALLOW_MISSING=yes
74 shift
75 ;;
76
Patrick Williamsc1513c82022-11-27 17:37:08 -060077 '--')
78 shift
79 break
80 ;;
81
82 *)
83 echo "unknown option: $1"
84 display_help && exit 1
85 ;;
86 esac
87done
88
89# Detect tty and set nicer colors.
90if [ -t 1 ]; then
91 BLUE="\e[34m"
92 GREEN="\e[32m"
93 NORMAL="\e[0m"
94 RED="\e[31m"
95 YELLOW="\e[33m"
96else # non-tty, no escapes.
97 BLUE=""
98 GREEN=""
99 NORMAL=""
100 RED=""
101 YELLOW=""
102fi
103
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600104# Allow called scripts to know which clang format we are using
105export CLANG_FORMAT="clang-format"
106
Patrick Williamsc1513c82022-11-27 17:37:08 -0600107# Path to default config files for linters.
108CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config"
109
110# Find repository root for `pwd` or $1.
111if [ -z "$1" ]; then
112 DIR="$(git rev-parse --show-toplevel || pwd)"
113else
114 DIR="$(git -C "$1" rev-parse --show-toplevel)"
115fi
Patrick Williamsbc85b712022-12-05 15:16:27 -0600116if [ ! -e "$DIR/.git" ]; then
117 echo -e "${RED}Error:${NORMAL} Directory ($DIR) does not appear to be a git repository"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600118 exit 1
119fi
Andrew Jeffery0cce8482018-04-30 11:37:01 +0930120
Manojkiran Edae6f120a2021-03-20 11:13:43 +0530121cd "${DIR}"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600122echo -e " ${BLUE}Formatting code under${NORMAL} $DIR"
Andrew Geissler9b0105c2018-01-10 10:58:35 -0800123
Patrick Williams3d294942022-12-05 14:58:15 -0600124# Config hashes:
125# LINTER_REQUIRE - The requirements to run a linter, semi-colon separated.
126# 1. Executable.
127# 2. [optional] Configuration file.
128# 3. [optional] Global fallback configuration file.
129#
130# LINTER_IGNORE - An optional set of semi-colon separated ignore-files
131# specific to the linter.
132#
133# LINTER_TYPES - The file types supported by the linter, semi-colon separated.
134#
135# LINTER_CONFIG - The config (from LINTER_REQUIRE) chosen for the repository.
136#
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600137declare -A LINTER_REQUIRE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600138declare -A LINTER_IGNORE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600139declare -A LINTER_TYPES=()
Patrick Williams3d294942022-12-05 14:58:15 -0600140declare -A LINTER_CONFIG=()
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530141
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600142LINTER_REQUIRE+=([commit_spelling]="codespell")
Patrick Williams71b73242022-12-02 21:07:52 -0600143LINTER_TYPES+=([commit_spelling]="commit")
Patrick Williams38515a32022-11-27 06:58:56 -0600144function do_commit_spelling() {
Patrick Williams38515a32022-11-27 06:58:56 -0600145 # Run the codespell with openbmc spcific spellings on the patchset
Patrick Williams71b73242022-12-02 21:07:52 -0600146 echo -n "openbmc-dictionary - misspelling count >> "
147 sed "s/Signed-off-by.*//" "$@" | \
Patrick Williamsc1513c82022-11-27 17:37:08 -0600148 codespell -D "${CONFIG_PATH}/openbmc-spelling.txt" -d --count -
Patrick Williams38515a32022-11-27 06:58:56 -0600149
150 # Run the codespell with generic dictionary on the patchset
Patrick Williams71b73242022-12-02 21:07:52 -0600151 echo -n "generic-dictionary - misspelling count >> "
152 sed "s/Signed-off-by.*//" "$@" | \
Patrick Williams38515a32022-11-27 06:58:56 -0600153 codespell --builtin clear,rare,en-GB_to_en-US -d --count -
154}
155
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600156LINTER_REQUIRE+=([commit_gitlint]="gitlint")
Patrick Williams71b73242022-12-02 21:07:52 -0600157LINTER_TYPES+=([commit_gitlint]="commit")
Patrick Williams38515a32022-11-27 06:58:56 -0600158function do_commit_gitlint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600159 gitlint --extra-path "${CONFIG_PATH}/gitlint/" \
Patrick Williamsc1513c82022-11-27 17:37:08 -0600160 --config "${CONFIG_PATH}/.gitlint"
Patrick Williams38515a32022-11-27 06:58:56 -0600161}
162
Patrick Williamsc5969592022-12-06 09:50:25 -0600163# We need different function style for bash/zsh vs plain sh, so beautysh is
164# split into two linters. "function foo()" is not traditionally accepted
165# POSIX-shell syntax, so shellcheck barfs on it.
166LINTER_REQUIRE+=([beautysh]="beautysh")
167LINTER_IGNORE+=([beautysh]=".beautysh-ignore")
168LINTER_TYPES+=([beautysh]="bash;zsh")
169function do_beautysh() {
170 beautysh --force-function-style fnpar "$@"
171}
172LINTER_REQUIRE+=([beautysh_sh]="beautysh")
173LINTER_IGNORE+=([beautysh_sh]=".beautysh-ignore")
174LINTER_TYPES+=([beautysh_sh]="sh")
175function do_beautysh_sh() {
176 beautysh --force-function-style paronly "$@"
177}
178
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600179LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json")
Patrick Williams71b73242022-12-02 21:07:52 -0600180LINTER_IGNORE+=([eslint]=".eslintignore")
181LINTER_TYPES+=([eslint]="json")
Patrick Williams38515a32022-11-27 06:58:56 -0600182function do_eslint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600183 eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600184 --ext .json --format=stylish \
185 --resolve-plugins-relative-to /usr/local/lib/node_modules \
Patrick Williams71b73242022-12-02 21:07:52 -0600186 --no-error-on-unmatched-pattern "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600187}
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530188
Patrick Williamsc5ad7ff2022-12-05 10:21:40 -0600189LINTER_REQUIRE+=([flake8]="flake8")
190LINTER_IGNORE+=([flake8]=".flake8-ignore")
191LINTER_TYPES+=([flake8]="python")
192function do_flake8() {
193 flake8 --show-source --extend-ignore=E203,E501 "$@"
194 # We disable E203 and E501 because 'black' is handling these and they
195 # disagree on best practices.
Patrick Williams38515a32022-11-27 06:58:56 -0600196}
James Feistf1665d62019-11-22 09:06:33 -0800197
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600198LINTER_REQUIRE+=([prettier]="prettier;.prettierrc.yaml;${CONFIG_PATH}/prettierrc.yaml")
199LINTER_IGNORE+=([prettier]=".prettierignore")
200LINTER_TYPES+=([prettier]="json;markdown;yaml")
201function do_prettier() {
202 prettier --config "${LINTER_CONFIG[prettier]}" --write "$@"
203}
204
Patrick Williamse0df3052022-12-05 10:22:00 -0600205LINTER_REQUIRE+=([shellcheck]="shellcheck")
206LINTER_IGNORE+=([shellcheck]=".shellcheck-ignore")
Patrick Williams71b73242022-12-02 21:07:52 -0600207LINTER_TYPES+=([shellcheck]="bash;sh")
Patrick Williams38515a32022-11-27 06:58:56 -0600208function do_shellcheck() {
Patrick Williams71b73242022-12-02 21:07:52 -0600209 shellcheck --color=never -x "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600210}
James Feistf1665d62019-11-22 09:06:33 -0800211
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600212LINTER_REQUIRE+=([clang_format]="clang-format;.clang-format")
Patrick Williams71b73242022-12-02 21:07:52 -0600213LINTER_IGNORE+=([clang_format]=".clang-ignore;.clang-format-ignore")
214LINTER_TYPES+=([clang_format]="c;cpp")
Patrick Williams476a7e92022-12-06 09:52:53 -0600215function do_clang_format() {
Patrick Williams71b73242022-12-02 21:07:52 -0600216 "${CLANG_FORMAT}" -i "$@"
217}
Patrick Williams38515a32022-11-27 06:58:56 -0600218
Patrick Williams71b73242022-12-02 21:07:52 -0600219function get_file_type()
220{
221 case "$(basename "$1")" in
222 # First to early detect template files.
223 *.in | *.meson) echo "meson-template" && return ;;
224 *.mako | *.mako.*) echo "mako" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600225
Patrick Williams71b73242022-12-02 21:07:52 -0600226 *.ac) echo "autoconf" && return ;;
227 *.[ch]) echo "c" && return ;;
228 *.[ch]pp) echo "cpp" && return ;;
229 *.json) echo "json" && return ;;
230 *.md) echo "markdown" && return ;;
231 *.py) echo "python" && return ;;
Patrick Williams5095cbe2022-12-08 06:50:34 -0600232 *.tcl) echo "tcl" && return ;;
Patrick Williams71b73242022-12-02 21:07:52 -0600233 *.yaml | *.yml) echo "yaml" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600234
Patrick Williams71b73242022-12-02 21:07:52 -0600235 # Special files.
236 .git/COMMIT_EDITMSG) echo "commit" && return ;;
237 meson.build) echo "meson" && return ;;
238 esac
239
240 case "$(file "$1")" in
241 *Bourne-Again\ shell*) echo "bash" && return ;;
242 *C++\ source*) echo "cpp" && return ;;
243 *C\ source*) echo "c" && return ;;
244 *JSON\ data*) echo "json" && return ;;
245 *POSIX\ shell*) echo "sh" && return ;;
246 *Python\ script*) echo "python" && return ;;
Patrick Williamsd5d63952022-12-07 17:55:03 -0600247 *python3\ script*) echo "python" && return ;;
Patrick Williams71b73242022-12-02 21:07:52 -0600248 *zsh\ shell*) echo "zsh" && return ;;
249 esac
250
251 echo "unknown"
Patrick Williams38515a32022-11-27 06:58:56 -0600252}
253
Patrick Williams816c7cc2022-12-08 05:51:57 -0600254LINTERS_AVAILABLE=()
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600255function check_linter()
256{
257 TITLE="$1"
258 IFS=";" read -r -a ARGS <<< "$2"
259
Patrick Williams147f37a2022-12-04 14:57:10 -0600260 if [[ "${LINTERS_DISABLED[*]}" =~ $1 ]]; then
261 return
262 fi
263
Patrick Williams816c7cc2022-12-08 05:51:57 -0600264 if [ 0 -ne "${#LINTERS_ENABLED[@]}" ]; then
265 if ! [[ "${LINTERS_ENABLED[*]}" =~ $1 ]]; then
266 return
267 fi
268 fi
269
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600270 EXE="${ARGS[0]}"
271 if [ ! -x "${EXE}" ]; then
272 if ! which "${EXE}" > /dev/null 2>&1 ; then
273 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${EXE}"
Patrick Williams26d9d2c2022-12-05 15:03:07 -0600274 if [ -z "$ALLOW_MISSING" ]; then
275 exit 1
276 fi
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600277 return
278 fi
279 fi
280
281 CONFIG="${ARGS[1]}"
282 FALLBACK="${ARGS[2]}"
283
284 if [ -n "${CONFIG}" ]; then
285 if [ -e "${CONFIG}" ]; then
286 LINTER_CONFIG+=( [${TITLE}]="${CONFIG}" )
287 elif [ -n "${FALLBACK}" ] && [ -e "${FALLBACK}" ]; then
288 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${CONFIG}; using ${FALLBACK}"
289 LINTER_CONFIG+=( [${TITLE}]="${FALLBACK}" )
290 else
291 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find config ${CONFIG}"
292 return
293 fi
294 fi
295
Patrick Williams816c7cc2022-12-08 05:51:57 -0600296 LINTERS_AVAILABLE+=( "${TITLE}" )
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600297}
298
Patrick Williams71b73242022-12-02 21:07:52 -0600299# Check for a global .linter-ignore file.
300GLOBAL_IGNORE=("cat")
301if [ -e ".linter-ignore" ]; then
302 GLOBAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter" ".linter-ignore")
303fi
304
305# Find all the files in the git repository and organize by type.
306declare -A FILES=()
307if [ -e .git/COMMIT_EDITMSG ]; then
308 FILES+=([commit]=".git/COMMIT_EDITMSG")
309fi
310while read -r file; do
311 ftype="$(get_file_type "$file")"
312 FILES+=([$ftype]="$(echo -ne "$file;${FILES[$ftype]:-}")")
313done < <(git ls-files | "${GLOBAL_IGNORE[@]}")
314
315# For each linter, check if there are an applicable files and if it can
316# be enabled.
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600317for op in "${LINTERS_ALL[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600318 for ftype in ${LINTER_TYPES[$op]//;/ }; do
319 if [[ -v FILES["$ftype"] ]]; then
320 check_linter "$op" "${LINTER_REQUIRE[${op}]}"
321 break
322 fi
323 done
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600324done
325
Patrick Williams71b73242022-12-02 21:07:52 -0600326# Call each linter.
Patrick Williams816c7cc2022-12-08 05:51:57 -0600327for op in "${LINTERS_AVAILABLE[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600328
329 # Determine the linter-specific ignore file(s).
330 LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter")
331 if [[ -v LINTER_IGNORE["$op"] ]]; then
332 for ignorefile in ${LINTER_IGNORE["$op"]//;/ } ; do
333 if [ -e "$ignorefile" ]; then
334 LOCAL_IGNORE+=("$ignorefile")
335 fi
336 done
337 fi
338 if [ 1 -eq ${#LOCAL_IGNORE[@]} ]; then
339 LOCAL_IGNORE=("cat")
340 fi
341
342 # Find all the files for this linter, filtering out the ignores.
343 LINTER_FILES=()
344 while read -r file ; do
345 if [ -e "$file" ]; then
346 LINTER_FILES+=("$file")
347 fi
348 done < <(for ftype in ${LINTER_TYPES[$op]//;/ }; do
349 # shellcheck disable=SC2001
350 echo "${FILES["$ftype"]:-}" | sed "s/;/\\n/g"
351 done | "${LOCAL_IGNORE[@]}")
352
353 # Call the linter now with all the files.
Patrick Williams200ec182022-12-06 08:32:03 -0600354 if [ 0 -ne ${#LINTER_FILES[@]} ]; then
355 echo -e " ${BLUE}Running $op${NORMAL}"
356 if ! "do_$op" "${LINTER_FILES[@]}" ; then
357 LINTERS_FAILED+=([$op]=1)
358 fi
359 else
360 echo -e " ${YELLOW}${op}:${NORMAL} all applicable files are on ignore-lists"
Patrick Williams785327a2022-12-05 16:20:43 -0600361 fi
Patrick Williams38515a32022-11-27 06:58:56 -0600362done
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030363
Patrick Williams785327a2022-12-05 16:20:43 -0600364# Check for failing linters.
365if [ 0 -ne ${#LINTERS_FAILED[@]} ]; then
366 for op in "${!LINTERS_FAILED[@]}"; do
367 echo -e "$op: ${RED}FAILED${NORMAL}"
368 done
369 exit 1
370fi
371
Patrick Williams71b73242022-12-02 21:07:52 -0600372# Check for differences.
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600373if [ -z "$OPTION_NO_DIFF" ]; then
374 echo -e " ${BLUE}Result differences...${NORMAL}"
375 if ! git --no-pager diff --exit-code ; then
376 echo -e "Format: ${RED}FAILED${NORMAL}"
377 exit 1
378 else
379 echo -e "Format: ${GREEN}PASSED${NORMAL}"
380 fi
Patrick Williamsc1513c82022-11-27 17:37:08 -0600381fi
382
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030383# Sometimes your situation is terrible enough that you need the flexibility.
384# For example, phosphor-mboxd.
Patrick Williams330b0772022-11-28 06:14:06 -0600385for formatter in "format-code.sh" "format-code"; do
386 if [[ -x "${formatter}" ]]; then
387 echo -e " ${BLUE}Calling secondary formatter:${NORMAL} ${formatter}"
388 "./${formatter}"
389 if [ -z "$OPTION_NO_DIFF" ]; then
390 git --no-pager diff --exit-code
391 fi
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600392 fi
Patrick Williams330b0772022-11-28 06:14:06 -0600393done