blob: 7aa864ed389fc80435ac0b65f38ffd1796ab5a48 [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
Ed Tanousfb9948a2022-06-21 09:10:24 -070029# Note, this check will be removed once the commit message rules have some usage
30if [[ -f "${DIR}/.openbmc-enforce-gitlint" ]]; then
31 # Check for commit message issues
32 gitlint \
33 --target "${DIR}" \
34 --extra-path "${WORKSPACE}/openbmc-build-scripts/config/gitlint/" \
35 --config "${WORKSPACE}/openbmc-build-scripts/config/.gitlint"
36fi
37
Manojkiran Edae6f120a2021-03-20 11:13:43 +053038cd "${DIR}"
39
Andrew Geissler9b0105c2018-01-10 10:58:35 -080040echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080041
Ed Tanousac5915f2022-03-10 08:32:54 -080042if [[ -f ".eslintignore" ]]; then
43 ESLINT_IGNORE="--ignore-path .eslintignore"
44elif [[ -f ".gitignore" ]]; then
45 ESLINT_IGNORE="--ignore-path .gitignore"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053046fi
47
Ed Tanousac5915f2022-03-10 08:32:54 -080048# Get the eslint configuration from the repository
49if [[ -f ".eslintrc.json" ]]; then
50 echo "Running the json validator on the repo using it's config > "
51 ESLINT_RC="-c .eslintrc.json"
52else
53 echo "Running the json validator on the repo using the global config"
54 ESLINT_RC="--no-eslintrc -c ${WORKSPACE}/eslint-global-config.json"
55fi
56
57ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \
58 --ext .json --format=json \
59 --resolve-plugins-relative-to /usr/local/lib/node_modules \
60 --no-error-on-unmatched-pattern"
61
Manojkiran Eda87111bb2021-08-14 11:26:16 +053062# Print eslint command
Ed Tanousac5915f2022-03-10 08:32:54 -080063echo "$ESLINT_COMMAND"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053064# Run eslint
Ed Tanousac5915f2022-03-10 08:32:54 -080065$ESLINT_COMMAND
Manojkiran Eda87111bb2021-08-14 11:26:16 +053066
Patrick Venture30ec0c42018-10-22 11:56:27 -070067if [[ -f "setup.cfg" ]]; then
Patrick Williams481a5072021-04-22 12:16:25 -050068 pycodestyle --show-source --exclude=subprojects .
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060069 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070070 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060071 exit ${rc}
72 fi
73fi
74
Patrick Williams01ced282020-07-17 15:10:15 -050075# If .shellcheck exists, stop on error. Otherwise, allow pass.
76if [[ -f ".shellcheck" ]]; then
77 shellcheck_allowfail="false"
78else
79 shellcheck_allowfail="true"
80fi
81
82# Run shellcheck on any shell-script.
83shell_scripts="$(git ls-files | xargs -n1 file -0 | \
84 grep -a "shell script" | cut -d '' -f 1)"
85for script in ${shell_scripts}; do
Ed Tanous86cafa62022-02-16 16:08:10 -080086 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
Patrick Williams01ced282020-07-17 15:10:15 -050087done
88
William A. Kennington III078c3b52018-10-04 19:44:31 -070089# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060090export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070091IGNORE_FILE=".clang-ignore"
92declare -a IGNORE_LIST
93
94if [[ -f "${IGNORE_FILE}" ]]; then
95 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
96fi
97
98ignorepaths=""
99ignorefiles=""
100
101for path in "${IGNORE_LIST[@]}"; do
102 # Check for comment, line starting with space, or zero-length string.
103 # Checking for [[:space:]] checks all options.
104 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
105 continue
106 fi
107
108 # All paths must start with ./ for find's path prune expectation.
109 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -0800110 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700111 else
James Feistf1665d62019-11-22 09:06:33 -0800112 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700113 fi
114done
William A. Kennington III078c3b52018-10-04 19:44:31 -0700115
James Feistf1665d62019-11-22 09:06:33 -0800116searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -0600117while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -0800118 # skip ignorefiles
119 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
120 continue
121 fi
122
123 skip=false
124 #skip paths in ingorepaths
125 for pathname in $ignorepaths; do
126 if [[ "./${path}" == "${pathname}"* ]]; then
127 skip=true
128 break
129 fi
130 done
131
132 if [ "$skip" = true ]; then
133 continue
134 fi
Patrick Williams384d7412020-11-06 16:15:41 -0600135 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -0800136 searchfiles+="\"./${path}\" "
137
138# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600139done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800140
Patrick Venture30ec0c42018-10-22 11:56:27 -0700141if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600142 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800143 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600144 git --no-pager diff --exit-code
145fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030146
147# Sometimes your situation is terrible enough that you need the flexibility.
148# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700149if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030150 ./format-code.sh
151 git --no-pager diff --exit-code
152fi