blob: 23a4dd89bc672f08306fd6a72f42140b04cd33b6 [file] [log] [blame]
Andrew Geissler9b0105c2018-01-10 10:58:35 -08001#!/bin/bash
2
3# This script reformats source files using the clang-format utility.
4#
5# Files are changed in-place, so make sure you don't have anything open in an
6# editor, and you may want to commit before formatting in case of awryness.
7#
8# This must be run on a clean repository to succeed
9#
10# Input parmameter must be full path to git repo to scan
11
12DIR=$1
Manojkiran Eda87111bb2021-08-14 11:26:16 +053013WORKSPACE=$PWD
Andrew Geissler9b0105c2018-01-10 10:58:35 -080014
Andrew Jeffery0cce8482018-04-30 11:37:01 +093015set -e
16
Manojkiran Edae6f120a2021-03-20 11:13:43 +053017echo "Running spelling check on Commit Message"
18
19# Run the codespell with openbmc spcific spellings on the patchset
20echo "openbmc-dictionary - misspelling count >> "
21codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG
22
23# Run the codespell with generic dictionary on the patchset
24echo "generic-dictionary - misspelling count >> "
Patrick Williams81692c02022-08-04 11:25:15 -050025codespell --builtin clear,rare,en-GB_to_en-US -d --count \
26 --ignore-words=openbmc-spelling-ignore.txt \
27 "${DIR}"/.git/COMMIT_EDITMSG
Manojkiran Edae6f120a2021-03-20 11:13:43 +053028
29cd "${DIR}"
30
Andrew Geissler9b0105c2018-01-10 10:58:35 -080031echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080032
Ed Tanousac5915f2022-03-10 08:32:54 -080033if [[ -f ".eslintignore" ]]; then
34 ESLINT_IGNORE="--ignore-path .eslintignore"
35elif [[ -f ".gitignore" ]]; then
36 ESLINT_IGNORE="--ignore-path .gitignore"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053037fi
38
Ed Tanousac5915f2022-03-10 08:32:54 -080039# Get the eslint configuration from the repository
40if [[ -f ".eslintrc.json" ]]; then
41 echo "Running the json validator on the repo using it's config > "
42 ESLINT_RC="-c .eslintrc.json"
43else
44 echo "Running the json validator on the repo using the global config"
45 ESLINT_RC="--no-eslintrc -c ${WORKSPACE}/eslint-global-config.json"
46fi
47
48ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \
49 --ext .json --format=json \
50 --resolve-plugins-relative-to /usr/local/lib/node_modules \
51 --no-error-on-unmatched-pattern"
52
Manojkiran Eda87111bb2021-08-14 11:26:16 +053053# Print eslint command
Ed Tanousac5915f2022-03-10 08:32:54 -080054echo "$ESLINT_COMMAND"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053055# Run eslint
Ed Tanousac5915f2022-03-10 08:32:54 -080056$ESLINT_COMMAND
Manojkiran Eda87111bb2021-08-14 11:26:16 +053057
Patrick Venture30ec0c42018-10-22 11:56:27 -070058if [[ -f "setup.cfg" ]]; then
Patrick Williams481a5072021-04-22 12:16:25 -050059 pycodestyle --show-source --exclude=subprojects .
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060060 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070061 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060062 exit ${rc}
63 fi
64fi
65
Patrick Williams01ced282020-07-17 15:10:15 -050066# If .shellcheck exists, stop on error. Otherwise, allow pass.
67if [[ -f ".shellcheck" ]]; then
68 shellcheck_allowfail="false"
69else
70 shellcheck_allowfail="true"
71fi
72
73# Run shellcheck on any shell-script.
74shell_scripts="$(git ls-files | xargs -n1 file -0 | \
75 grep -a "shell script" | cut -d '' -f 1)"
76for script in ${shell_scripts}; do
Ed Tanous86cafa62022-02-16 16:08:10 -080077 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
Patrick Williams01ced282020-07-17 15:10:15 -050078done
79
William A. Kennington III078c3b52018-10-04 19:44:31 -070080# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060081export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070082IGNORE_FILE=".clang-ignore"
83declare -a IGNORE_LIST
84
85if [[ -f "${IGNORE_FILE}" ]]; then
86 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
87fi
88
89ignorepaths=""
90ignorefiles=""
91
92for path in "${IGNORE_LIST[@]}"; do
93 # Check for comment, line starting with space, or zero-length string.
94 # Checking for [[:space:]] checks all options.
95 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
96 continue
97 fi
98
99 # All paths must start with ./ for find's path prune expectation.
100 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -0800101 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700102 else
James Feistf1665d62019-11-22 09:06:33 -0800103 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700104 fi
105done
William A. Kennington III078c3b52018-10-04 19:44:31 -0700106
James Feistf1665d62019-11-22 09:06:33 -0800107searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -0600108while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -0800109 # skip ignorefiles
110 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
111 continue
112 fi
113
114 skip=false
115 #skip paths in ingorepaths
116 for pathname in $ignorepaths; do
117 if [[ "./${path}" == "${pathname}"* ]]; then
118 skip=true
119 break
120 fi
121 done
122
123 if [ "$skip" = true ]; then
124 continue
125 fi
Patrick Williams384d7412020-11-06 16:15:41 -0600126 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -0800127 searchfiles+="\"./${path}\" "
128
129# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600130done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800131
Patrick Venture30ec0c42018-10-22 11:56:27 -0700132if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600133 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800134 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600135 git --no-pager diff --exit-code
136fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030137
138# Sometimes your situation is terrible enough that you need the flexibility.
139# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700140if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030141 ./format-code.sh
142 git --no-pager diff --exit-code
143fi