blob: a8443e0385e43abc909055c4bdd652035ec7e7c0 [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 Tanous0ae544f2022-08-17 09:12:06 -070029# Check for commit message issues
30gitlint \
31 --target "${DIR}" \
32 --extra-path "${WORKSPACE}/openbmc-build-scripts/config/gitlint/" \
33 --config "${WORKSPACE}/openbmc-build-scripts/config/.gitlint"
Ed Tanousfb9948a2022-06-21 09:10:24 -070034
Manojkiran Edae6f120a2021-03-20 11:13:43 +053035cd "${DIR}"
36
Andrew Geissler9b0105c2018-01-10 10:58:35 -080037echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080038
Ed Tanousac5915f2022-03-10 08:32:54 -080039if [[ -f ".eslintignore" ]]; then
40 ESLINT_IGNORE="--ignore-path .eslintignore"
41elif [[ -f ".gitignore" ]]; then
42 ESLINT_IGNORE="--ignore-path .gitignore"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053043fi
44
Ed Tanousac5915f2022-03-10 08:32:54 -080045# Get the eslint configuration from the repository
46if [[ -f ".eslintrc.json" ]]; then
47 echo "Running the json validator on the repo using it's config > "
48 ESLINT_RC="-c .eslintrc.json"
49else
50 echo "Running the json validator on the repo using the global config"
51 ESLINT_RC="--no-eslintrc -c ${WORKSPACE}/eslint-global-config.json"
52fi
53
54ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \
Ed Tanous0c4153b2022-08-17 10:20:26 -070055 --ext .json --format=stylish \
Ed Tanousac5915f2022-03-10 08:32:54 -080056 --resolve-plugins-relative-to /usr/local/lib/node_modules \
57 --no-error-on-unmatched-pattern"
58
Manojkiran Eda87111bb2021-08-14 11:26:16 +053059# Print eslint command
Ed Tanousac5915f2022-03-10 08:32:54 -080060echo "$ESLINT_COMMAND"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053061# Run eslint
Ed Tanousac5915f2022-03-10 08:32:54 -080062$ESLINT_COMMAND
Manojkiran Eda87111bb2021-08-14 11:26:16 +053063
Patrick Venture30ec0c42018-10-22 11:56:27 -070064if [[ -f "setup.cfg" ]]; then
Patrick Williams481a5072021-04-22 12:16:25 -050065 pycodestyle --show-source --exclude=subprojects .
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060066 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070067 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060068 exit ${rc}
69 fi
70fi
71
Patrick Williams01ced282020-07-17 15:10:15 -050072# If .shellcheck exists, stop on error. Otherwise, allow pass.
73if [[ -f ".shellcheck" ]]; then
74 shellcheck_allowfail="false"
75else
76 shellcheck_allowfail="true"
77fi
78
79# Run shellcheck on any shell-script.
80shell_scripts="$(git ls-files | xargs -n1 file -0 | \
81 grep -a "shell script" | cut -d '' -f 1)"
82for script in ${shell_scripts}; do
Ed Tanous86cafa62022-02-16 16:08:10 -080083 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
Patrick Williams01ced282020-07-17 15:10:15 -050084done
85
William A. Kennington III078c3b52018-10-04 19:44:31 -070086# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060087export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070088IGNORE_FILE=".clang-ignore"
89declare -a IGNORE_LIST
90
91if [[ -f "${IGNORE_FILE}" ]]; then
92 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
93fi
94
95ignorepaths=""
96ignorefiles=""
97
98for path in "${IGNORE_LIST[@]}"; do
99 # Check for comment, line starting with space, or zero-length string.
100 # Checking for [[:space:]] checks all options.
101 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
102 continue
103 fi
104
105 # All paths must start with ./ for find's path prune expectation.
106 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -0800107 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700108 else
James Feistf1665d62019-11-22 09:06:33 -0800109 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700110 fi
111done
William A. Kennington III078c3b52018-10-04 19:44:31 -0700112
James Feistf1665d62019-11-22 09:06:33 -0800113searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -0600114while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -0800115 # skip ignorefiles
116 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
117 continue
118 fi
119
120 skip=false
121 #skip paths in ingorepaths
122 for pathname in $ignorepaths; do
123 if [[ "./${path}" == "${pathname}"* ]]; then
124 skip=true
125 break
126 fi
127 done
128
129 if [ "$skip" = true ]; then
130 continue
131 fi
Patrick Williams384d7412020-11-06 16:15:41 -0600132 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -0800133 searchfiles+="\"./${path}\" "
134
135# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600136done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800137
Patrick Venture30ec0c42018-10-22 11:56:27 -0700138if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600139 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800140 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600141 git --no-pager diff --exit-code
142fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030143
144# Sometimes your situation is terrible enough that you need the flexibility.
145# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700146if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030147 ./format-code.sh
148 git --no-pager diff --exit-code
149fi