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