blob: 0609d9fbaa084a9d346dc38eca015d6b6a8c42d2 [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
Ed Tanousac5915f2022-03-10 08:32:54 -080031if [[ -f ".eslintignore" ]]; then
32 ESLINT_IGNORE="--ignore-path .eslintignore"
33elif [[ -f ".gitignore" ]]; then
34 ESLINT_IGNORE="--ignore-path .gitignore"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053035fi
36
Ed Tanousac5915f2022-03-10 08:32:54 -080037# Get the eslint configuration from the repository
38if [[ -f ".eslintrc.json" ]]; then
39 echo "Running the json validator on the repo using it's config > "
40 ESLINT_RC="-c .eslintrc.json"
41else
42 echo "Running the json validator on the repo using the global config"
43 ESLINT_RC="--no-eslintrc -c ${WORKSPACE}/eslint-global-config.json"
44fi
45
46ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \
47 --ext .json --format=json \
48 --resolve-plugins-relative-to /usr/local/lib/node_modules \
49 --no-error-on-unmatched-pattern"
50
Manojkiran Eda87111bb2021-08-14 11:26:16 +053051# Print eslint command
Ed Tanousac5915f2022-03-10 08:32:54 -080052echo "$ESLINT_COMMAND"
Manojkiran Eda87111bb2021-08-14 11:26:16 +053053# Run eslint
Ed Tanousac5915f2022-03-10 08:32:54 -080054$ESLINT_COMMAND
Manojkiran Eda87111bb2021-08-14 11:26:16 +053055
Patrick Venture30ec0c42018-10-22 11:56:27 -070056if [[ -f "setup.cfg" ]]; then
Patrick Williams481a5072021-04-22 12:16:25 -050057 pycodestyle --show-source --exclude=subprojects .
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060058 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070059 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060060 exit ${rc}
61 fi
62fi
63
Patrick Williams01ced282020-07-17 15:10:15 -050064# If .shellcheck exists, stop on error. Otherwise, allow pass.
65if [[ -f ".shellcheck" ]]; then
66 shellcheck_allowfail="false"
67else
68 shellcheck_allowfail="true"
69fi
70
71# Run shellcheck on any shell-script.
72shell_scripts="$(git ls-files | xargs -n1 file -0 | \
73 grep -a "shell script" | cut -d '' -f 1)"
74for script in ${shell_scripts}; do
Ed Tanous86cafa62022-02-16 16:08:10 -080075 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
Patrick Williams01ced282020-07-17 15:10:15 -050076done
77
William A. Kennington III078c3b52018-10-04 19:44:31 -070078# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060079export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070080IGNORE_FILE=".clang-ignore"
81declare -a IGNORE_LIST
82
83if [[ -f "${IGNORE_FILE}" ]]; then
84 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
85fi
86
87ignorepaths=""
88ignorefiles=""
89
90for path in "${IGNORE_LIST[@]}"; do
91 # Check for comment, line starting with space, or zero-length string.
92 # Checking for [[:space:]] checks all options.
93 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
94 continue
95 fi
96
97 # All paths must start with ./ for find's path prune expectation.
98 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -080099 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700100 else
James Feistf1665d62019-11-22 09:06:33 -0800101 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -0700102 fi
103done
William A. Kennington III078c3b52018-10-04 19:44:31 -0700104
James Feistf1665d62019-11-22 09:06:33 -0800105searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -0600106while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -0800107 # skip ignorefiles
108 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
109 continue
110 fi
111
112 skip=false
113 #skip paths in ingorepaths
114 for pathname in $ignorepaths; do
115 if [[ "./${path}" == "${pathname}"* ]]; then
116 skip=true
117 break
118 fi
119 done
120
121 if [ "$skip" = true ]; then
122 continue
123 fi
Patrick Williams384d7412020-11-06 16:15:41 -0600124 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -0800125 searchfiles+="\"./${path}\" "
126
127# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600128done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800129
Patrick Venture30ec0c42018-10-22 11:56:27 -0700130if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600131 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800132 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600133 git --no-pager diff --exit-code
134fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030135
136# Sometimes your situation is terrible enough that you need the flexibility.
137# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700138if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030139 ./format-code.sh
140 git --no-pager diff --exit-code
141fi