blob: 2eab096967ed071644dffe289bb55f509bb5c5b6 [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 Williams147f37a2022-12-04 14:57:10 -060013 echo "usage: format-code.sh [-h | --help] [--no-diff] [--disable <tool>]"
14 echo " [--list-tools] [<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 Williams26d9d2c2022-12-05 15:03:07 -060022 echo " --allow-missing Run even if linters are not all present"
Patrick Williams147f37a2022-12-04 14:57:10 -060023 echo " path Path to git repository (default to pwd)"
Patrick Williamsc1513c82022-11-27 17:37:08 -060024}
Andrew Geissler9b0105c2018-01-10 10:58:35 -080025
Patrick Williams147f37a2022-12-04 14:57:10 -060026LINTERS_ALL=( \
27 commit_gitlint \
28 commit_spelling \
29 clang_format \
30 eslint \
31 pycodestyle \
32 shellcheck \
33 )
34LINTERS_DISABLED=()
35LINTERS_ENABLED=()
Patrick Williams785327a2022-12-05 16:20:43 -060036declare -A LINTERS_FAILED=()
Patrick Williams147f37a2022-12-04 14:57:10 -060037
Patrick Williams26d9d2c2022-12-05 15:03:07 -060038eval set -- "$(getopt -o 'h' --long 'help,list-tools,no-diff,disable:,allow-missing' -n 'format-code.sh' -- "$@")"
Patrick Williamsc1513c82022-11-27 17:37:08 -060039while true; do
40 case "$1" in
41 '-h'|'--help')
42 display_help && exit 0
43 ;;
Andrew Geissler9b0105c2018-01-10 10:58:35 -080044
Patrick Williams147f37a2022-12-04 14:57:10 -060045 '--list-tools')
46 echo "Available tools:"
47 for t in "${LINTERS_ALL[@]}"; do
48 echo " $t"
49 done
50 exit 0
51 ;;
52
Patrick Williamsd27ab4c2022-11-27 17:51:35 -060053 '--no-diff')
54 OPTION_NO_DIFF=1
55 shift
56 ;;
57
Patrick Williams147f37a2022-12-04 14:57:10 -060058 '--disable')
59 LINTERS_DISABLED+=("$2")
60 shift && shift
61 ;;
62
Patrick Williams26d9d2c2022-12-05 15:03:07 -060063 '--allow-missing')
64 ALLOW_MISSING=yes
65 shift
66 ;;
67
Patrick Williamsc1513c82022-11-27 17:37:08 -060068 '--')
69 shift
70 break
71 ;;
72
73 *)
74 echo "unknown option: $1"
75 display_help && exit 1
76 ;;
77 esac
78done
79
80# Detect tty and set nicer colors.
81if [ -t 1 ]; then
82 BLUE="\e[34m"
83 GREEN="\e[32m"
84 NORMAL="\e[0m"
85 RED="\e[31m"
86 YELLOW="\e[33m"
87else # non-tty, no escapes.
88 BLUE=""
89 GREEN=""
90 NORMAL=""
91 RED=""
92 YELLOW=""
93fi
94
Patrick Williamsdad9b7a2022-11-30 13:10:20 -060095# Allow called scripts to know which clang format we are using
96export CLANG_FORMAT="clang-format"
97
Patrick Williamsc1513c82022-11-27 17:37:08 -060098# Path to default config files for linters.
99CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config"
100
101# Find repository root for `pwd` or $1.
102if [ -z "$1" ]; then
103 DIR="$(git rev-parse --show-toplevel || pwd)"
104else
105 DIR="$(git -C "$1" rev-parse --show-toplevel)"
106fi
Patrick Williamsbc85b712022-12-05 15:16:27 -0600107if [ ! -e "$DIR/.git" ]; then
108 echo -e "${RED}Error:${NORMAL} Directory ($DIR) does not appear to be a git repository"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600109 exit 1
110fi
Andrew Jeffery0cce8482018-04-30 11:37:01 +0930111
Manojkiran Edae6f120a2021-03-20 11:13:43 +0530112cd "${DIR}"
Patrick Williamsc1513c82022-11-27 17:37:08 -0600113echo -e " ${BLUE}Formatting code under${NORMAL} $DIR"
Andrew Geissler9b0105c2018-01-10 10:58:35 -0800114
Patrick Williams3d294942022-12-05 14:58:15 -0600115# 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 Williamsdad9b7a2022-11-30 13:10:20 -0600128declare -A LINTER_REQUIRE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600129declare -A LINTER_IGNORE=()
Patrick Williams71b73242022-12-02 21:07:52 -0600130declare -A LINTER_TYPES=()
Patrick Williams3d294942022-12-05 14:58:15 -0600131declare -A LINTER_CONFIG=()
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530132
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600133LINTER_REQUIRE+=([commit_spelling]="codespell")
Patrick Williams71b73242022-12-02 21:07:52 -0600134LINTER_TYPES+=([commit_spelling]="commit")
Patrick Williams38515a32022-11-27 06:58:56 -0600135function do_commit_spelling() {
Patrick Williams38515a32022-11-27 06:58:56 -0600136 # Run the codespell with openbmc spcific spellings on the patchset
Patrick Williams71b73242022-12-02 21:07:52 -0600137 echo -n "openbmc-dictionary - misspelling count >> "
138 sed "s/Signed-off-by.*//" "$@" | \
Patrick Williamsc1513c82022-11-27 17:37:08 -0600139 codespell -D "${CONFIG_PATH}/openbmc-spelling.txt" -d --count -
Patrick Williams38515a32022-11-27 06:58:56 -0600140
141 # Run the codespell with generic dictionary on the patchset
Patrick Williams71b73242022-12-02 21:07:52 -0600142 echo -n "generic-dictionary - misspelling count >> "
143 sed "s/Signed-off-by.*//" "$@" | \
Patrick Williams38515a32022-11-27 06:58:56 -0600144 codespell --builtin clear,rare,en-GB_to_en-US -d --count -
145}
146
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600147LINTER_REQUIRE+=([commit_gitlint]="gitlint")
Patrick Williams71b73242022-12-02 21:07:52 -0600148LINTER_TYPES+=([commit_gitlint]="commit")
Patrick Williams38515a32022-11-27 06:58:56 -0600149function do_commit_gitlint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600150 gitlint --extra-path "${CONFIG_PATH}/gitlint/" \
Patrick Williamsc1513c82022-11-27 17:37:08 -0600151 --config "${CONFIG_PATH}/.gitlint"
Patrick Williams38515a32022-11-27 06:58:56 -0600152}
153
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600154LINTER_REQUIRE+=([eslint]="eslint;.eslintrc.json;${CONFIG_PATH}/eslint-global-config.json")
Patrick Williams71b73242022-12-02 21:07:52 -0600155LINTER_IGNORE+=([eslint]=".eslintignore")
156LINTER_TYPES+=([eslint]="json")
Patrick Williams38515a32022-11-27 06:58:56 -0600157function do_eslint() {
Patrick Williams71b73242022-12-02 21:07:52 -0600158 eslint --no-eslintrc -c "${LINTER_CONFIG[eslint]}" \
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600159 --ext .json --format=stylish \
160 --resolve-plugins-relative-to /usr/local/lib/node_modules \
Patrick Williams71b73242022-12-02 21:07:52 -0600161 --no-error-on-unmatched-pattern "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600162}
Manojkiran Eda87111bb2021-08-14 11:26:16 +0530163
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600164LINTER_REQUIRE+=([pycodestyle]="pycodestyle;setup.cfg")
Patrick Williams71b73242022-12-02 21:07:52 -0600165LINTER_TYPES+=([pycodestyle]="python")
Patrick Williams38515a32022-11-27 06:58:56 -0600166function do_pycodestyle() {
Patrick Williams617a3f22022-12-07 09:54:47 -0600167 pycodestyle --ignore=E121,E123,E126,E226,E24,E704,W503,W504,E203,E501 \
168 --show-source "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600169}
James Feistf1665d62019-11-22 09:06:33 -0800170
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600171LINTER_REQUIRE+=([shellcheck]="shellcheck;.shellcheck")
Patrick Williams71b73242022-12-02 21:07:52 -0600172LINTER_TYPES+=([shellcheck]="bash;sh")
Patrick Williams38515a32022-11-27 06:58:56 -0600173function do_shellcheck() {
Patrick Williams71b73242022-12-02 21:07:52 -0600174 shellcheck --color=never -x "$@"
Patrick Williams38515a32022-11-27 06:58:56 -0600175}
James Feistf1665d62019-11-22 09:06:33 -0800176
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600177LINTER_REQUIRE+=([clang_format]="clang-format;.clang-format")
Patrick Williams71b73242022-12-02 21:07:52 -0600178LINTER_IGNORE+=([clang_format]=".clang-ignore;.clang-format-ignore")
179LINTER_TYPES+=([clang_format]="c;cpp")
Patrick Williams476a7e92022-12-06 09:52:53 -0600180function do_clang_format() {
Patrick Williams71b73242022-12-02 21:07:52 -0600181 "${CLANG_FORMAT}" -i "$@"
182}
Patrick Williams38515a32022-11-27 06:58:56 -0600183
Patrick Williams71b73242022-12-02 21:07:52 -0600184function get_file_type()
185{
186 case "$(basename "$1")" in
187 # First to early detect template files.
188 *.in | *.meson) echo "meson-template" && return ;;
189 *.mako | *.mako.*) echo "mako" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600190
Patrick Williams71b73242022-12-02 21:07:52 -0600191 *.ac) echo "autoconf" && return ;;
192 *.[ch]) echo "c" && return ;;
193 *.[ch]pp) echo "cpp" && return ;;
194 *.json) echo "json" && return ;;
195 *.md) echo "markdown" && return ;;
196 *.py) echo "python" && return ;;
197 *.yaml | *.yml) echo "yaml" && return ;;
Patrick Williamsd535ecb2022-12-02 14:54:18 -0600198
Patrick Williams71b73242022-12-02 21:07:52 -0600199 # Special files.
200 .git/COMMIT_EDITMSG) echo "commit" && return ;;
201 meson.build) echo "meson" && return ;;
202 esac
203
204 case "$(file "$1")" in
205 *Bourne-Again\ shell*) echo "bash" && return ;;
206 *C++\ source*) echo "cpp" && return ;;
207 *C\ source*) echo "c" && return ;;
208 *JSON\ data*) echo "json" && return ;;
209 *POSIX\ shell*) echo "sh" && return ;;
210 *Python\ script*) echo "python" && return ;;
211 *zsh\ shell*) echo "zsh" && return ;;
212 esac
213
214 echo "unknown"
Patrick Williams38515a32022-11-27 06:58:56 -0600215}
216
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600217function check_linter()
218{
219 TITLE="$1"
220 IFS=";" read -r -a ARGS <<< "$2"
221
Patrick Williams147f37a2022-12-04 14:57:10 -0600222 if [[ "${LINTERS_DISABLED[*]}" =~ $1 ]]; then
223 return
224 fi
225
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600226 EXE="${ARGS[0]}"
227 if [ ! -x "${EXE}" ]; then
228 if ! which "${EXE}" > /dev/null 2>&1 ; then
229 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${EXE}"
Patrick Williams26d9d2c2022-12-05 15:03:07 -0600230 if [ -z "$ALLOW_MISSING" ]; then
231 exit 1
232 fi
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600233 return
234 fi
235 fi
236
237 CONFIG="${ARGS[1]}"
238 FALLBACK="${ARGS[2]}"
239
240 if [ -n "${CONFIG}" ]; then
241 if [ -e "${CONFIG}" ]; then
242 LINTER_CONFIG+=( [${TITLE}]="${CONFIG}" )
243 elif [ -n "${FALLBACK}" ] && [ -e "${FALLBACK}" ]; then
244 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find ${CONFIG}; using ${FALLBACK}"
245 LINTER_CONFIG+=( [${TITLE}]="${FALLBACK}" )
246 else
247 echo -e " ${YELLOW}${TITLE}:${NORMAL} cannot find config ${CONFIG}"
248 return
249 fi
250 fi
251
252 LINTERS_ENABLED+=( "${TITLE}" )
253}
254
Patrick Williams71b73242022-12-02 21:07:52 -0600255# Check for a global .linter-ignore file.
256GLOBAL_IGNORE=("cat")
257if [ -e ".linter-ignore" ]; then
258 GLOBAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter" ".linter-ignore")
259fi
260
261# Find all the files in the git repository and organize by type.
262declare -A FILES=()
263if [ -e .git/COMMIT_EDITMSG ]; then
264 FILES+=([commit]=".git/COMMIT_EDITMSG")
265fi
266while read -r file; do
267 ftype="$(get_file_type "$file")"
268 FILES+=([$ftype]="$(echo -ne "$file;${FILES[$ftype]:-}")")
269done < <(git ls-files | "${GLOBAL_IGNORE[@]}")
270
271# For each linter, check if there are an applicable files and if it can
272# be enabled.
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600273for op in "${LINTERS_ALL[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600274 for ftype in ${LINTER_TYPES[$op]//;/ }; do
275 if [[ -v FILES["$ftype"] ]]; then
276 check_linter "$op" "${LINTER_REQUIRE[${op}]}"
277 break
278 fi
279 done
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600280done
281
Patrick Williams71b73242022-12-02 21:07:52 -0600282# Call each linter.
Patrick Williamsdad9b7a2022-11-30 13:10:20 -0600283for op in "${LINTERS_ENABLED[@]}"; do
Patrick Williams71b73242022-12-02 21:07:52 -0600284
285 # Determine the linter-specific ignore file(s).
286 LOCAL_IGNORE=("${CONFIG_PATH}/lib/ignore-filter")
287 if [[ -v LINTER_IGNORE["$op"] ]]; then
288 for ignorefile in ${LINTER_IGNORE["$op"]//;/ } ; do
289 if [ -e "$ignorefile" ]; then
290 LOCAL_IGNORE+=("$ignorefile")
291 fi
292 done
293 fi
294 if [ 1 -eq ${#LOCAL_IGNORE[@]} ]; then
295 LOCAL_IGNORE=("cat")
296 fi
297
298 # Find all the files for this linter, filtering out the ignores.
299 LINTER_FILES=()
300 while read -r file ; do
301 if [ -e "$file" ]; then
302 LINTER_FILES+=("$file")
303 fi
304 done < <(for ftype in ${LINTER_TYPES[$op]//;/ }; do
305 # shellcheck disable=SC2001
306 echo "${FILES["$ftype"]:-}" | sed "s/;/\\n/g"
307 done | "${LOCAL_IGNORE[@]}")
308
309 # Call the linter now with all the files.
Patrick Williams200ec182022-12-06 08:32:03 -0600310 if [ 0 -ne ${#LINTER_FILES[@]} ]; then
311 echo -e " ${BLUE}Running $op${NORMAL}"
312 if ! "do_$op" "${LINTER_FILES[@]}" ; then
313 LINTERS_FAILED+=([$op]=1)
314 fi
315 else
316 echo -e " ${YELLOW}${op}:${NORMAL} all applicable files are on ignore-lists"
Patrick Williams785327a2022-12-05 16:20:43 -0600317 fi
Patrick Williams38515a32022-11-27 06:58:56 -0600318done
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030319
Patrick Williams785327a2022-12-05 16:20:43 -0600320# Check for failing linters.
321if [ 0 -ne ${#LINTERS_FAILED[@]} ]; then
322 for op in "${!LINTERS_FAILED[@]}"; do
323 echo -e "$op: ${RED}FAILED${NORMAL}"
324 done
325 exit 1
326fi
327
Patrick Williams71b73242022-12-02 21:07:52 -0600328# Check for differences.
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600329if [ -z "$OPTION_NO_DIFF" ]; then
330 echo -e " ${BLUE}Result differences...${NORMAL}"
331 if ! git --no-pager diff --exit-code ; then
332 echo -e "Format: ${RED}FAILED${NORMAL}"
333 exit 1
334 else
335 echo -e "Format: ${GREEN}PASSED${NORMAL}"
336 fi
Patrick Williamsc1513c82022-11-27 17:37:08 -0600337fi
338
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030339# Sometimes your situation is terrible enough that you need the flexibility.
340# For example, phosphor-mboxd.
Patrick Williams330b0772022-11-28 06:14:06 -0600341for formatter in "format-code.sh" "format-code"; do
342 if [[ -x "${formatter}" ]]; then
343 echo -e " ${BLUE}Calling secondary formatter:${NORMAL} ${formatter}"
344 "./${formatter}"
345 if [ -z "$OPTION_NO_DIFF" ]; then
346 git --no-pager diff --exit-code
347 fi
Patrick Williamsd27ab4c2022-11-27 17:51:35 -0600348 fi
Patrick Williams330b0772022-11-28 06:14:06 -0600349done