blob: 152a5b22311f840cf25b11cc63df89fabf808b56 [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 >> "
25codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG
26
27cd "${DIR}"
28
Andrew Geissler9b0105c2018-01-10 10:58:35 -080029echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080030
Manojkiran Eda87111bb2021-08-14 11:26:16 +053031ESLINT_CONFIG="eslint ${DIR} --no-eslintrc -c ${WORKSPACE}/eslint-global-config.json \
32 --ext .json --resolve-plugins-relative-to /usr/local/lib/node_modules\
33 --no-error-on-unmatched-pattern"
34ESLINT_IGNORE=" --ignore-path ${DIR}/.eslintignore"
35
36# Get the eslint configuration from the repository
37if [[ -f ".eslintrc.json" ]] || [[ -f ".eslintignore" ]]; then
38 echo "Running the json validator on the repo using it's config > "
39 if [[ ! -f ".eslintrc.json" ]]; then
40 eslint_command="${ESLINT_CONFIG} ${ESLINT_IGNORE}"
41 else
42 eslint_command="$ESLINT_CONFIG"
43 fi
44else
45 echo "Running the json validator on the repo using the global config > "
46 eslint_command="$ESLINT_CONFIG"
47fi
48
49# Print eslint command
50echo "$eslint_command"
51# Run eslint
52$eslint_command
53
Patrick Venture30ec0c42018-10-22 11:56:27 -070054if [[ -f "setup.cfg" ]]; then
Patrick Williams481a5072021-04-22 12:16:25 -050055 pycodestyle --show-source --exclude=subprojects .
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060056 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070057 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060058 exit ${rc}
59 fi
60fi
61
Patrick Williams01ced282020-07-17 15:10:15 -050062# If .shellcheck exists, stop on error. Otherwise, allow pass.
63if [[ -f ".shellcheck" ]]; then
64 shellcheck_allowfail="false"
65else
66 shellcheck_allowfail="true"
67fi
68
69# Run shellcheck on any shell-script.
70shell_scripts="$(git ls-files | xargs -n1 file -0 | \
71 grep -a "shell script" | cut -d '' -f 1)"
72for script in ${shell_scripts}; do
Ed Tanous86cafa62022-02-16 16:08:10 -080073 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
Patrick Williams01ced282020-07-17 15:10:15 -050074done
75
William A. Kennington III078c3b52018-10-04 19:44:31 -070076# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060077export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070078IGNORE_FILE=".clang-ignore"
79declare -a IGNORE_LIST
80
81if [[ -f "${IGNORE_FILE}" ]]; then
82 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
83fi
84
85ignorepaths=""
86ignorefiles=""
87
88for path in "${IGNORE_LIST[@]}"; do
89 # Check for comment, line starting with space, or zero-length string.
90 # Checking for [[:space:]] checks all options.
91 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
92 continue
93 fi
94
95 # All paths must start with ./ for find's path prune expectation.
96 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -080097 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070098 else
James Feistf1665d62019-11-22 09:06:33 -080099 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700100 fi
101done
William A. Kennington III078c3b52018-10-04 19:44:31 -0700102
James Feistf1665d62019-11-22 09:06:33 -0800103searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -0600104while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -0800105 # skip ignorefiles
106 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
107 continue
108 fi
109
110 skip=false
111 #skip paths in ingorepaths
112 for pathname in $ignorepaths; do
113 if [[ "./${path}" == "${pathname}"* ]]; then
114 skip=true
115 break
116 fi
117 done
118
119 if [ "$skip" = true ]; then
120 continue
121 fi
Patrick Williams384d7412020-11-06 16:15:41 -0600122 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -0800123 searchfiles+="\"./${path}\" "
124
125# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600126done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800127
Patrick Venture30ec0c42018-10-22 11:56:27 -0700128if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600129 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800130 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600131 git --no-pager diff --exit-code
132fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030133
134# Sometimes your situation is terrible enough that you need the flexibility.
135# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700136if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030137 ./format-code.sh
138 git --no-pager diff --exit-code
139fi