blob: 65f3dacaa8de9ce85c1daad9907597994fa8d8f3 [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
Andrew Geissler9b0105c2018-01-10 10:58:35 -080013
Andrew Jeffery0cce8482018-04-30 11:37:01 +093014set -e
15
Manojkiran Edae6f120a2021-03-20 11:13:43 +053016echo "Running spelling check on Commit Message"
17
18# Run the codespell with openbmc spcific spellings on the patchset
19echo "openbmc-dictionary - misspelling count >> "
20codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG
21
22# Run the codespell with generic dictionary on the patchset
23echo "generic-dictionary - misspelling count >> "
24codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG
25
26cd "${DIR}"
27
Andrew Geissler9b0105c2018-01-10 10:58:35 -080028echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080029
Patrick Venture30ec0c42018-10-22 11:56:27 -070030if [[ -f "setup.cfg" ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060031 pycodestyle --show-source .
32 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070033 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060034 exit ${rc}
35 fi
36fi
37
Patrick Williams01ced282020-07-17 15:10:15 -050038# If .shellcheck exists, stop on error. Otherwise, allow pass.
39if [[ -f ".shellcheck" ]]; then
40 shellcheck_allowfail="false"
41else
42 shellcheck_allowfail="true"
43fi
44
45# Run shellcheck on any shell-script.
46shell_scripts="$(git ls-files | xargs -n1 file -0 | \
47 grep -a "shell script" | cut -d '' -f 1)"
48for script in ${shell_scripts}; do
49 shellcheck -x "${script}" || ${shellcheck_allowfail}
50done
51
William A. Kennington III078c3b52018-10-04 19:44:31 -070052# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060053export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070054IGNORE_FILE=".clang-ignore"
55declare -a IGNORE_LIST
56
57if [[ -f "${IGNORE_FILE}" ]]; then
58 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
59fi
60
61ignorepaths=""
62ignorefiles=""
63
64for path in "${IGNORE_LIST[@]}"; do
65 # Check for comment, line starting with space, or zero-length string.
66 # Checking for [[:space:]] checks all options.
67 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
68 continue
69 fi
70
71 # All paths must start with ./ for find's path prune expectation.
72 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -080073 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070074 else
James Feistf1665d62019-11-22 09:06:33 -080075 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070076 fi
77done
William A. Kennington III078c3b52018-10-04 19:44:31 -070078
James Feistf1665d62019-11-22 09:06:33 -080079searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -060080while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -080081 # skip ignorefiles
82 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
83 continue
84 fi
85
86 skip=false
87 #skip paths in ingorepaths
88 for pathname in $ignorepaths; do
89 if [[ "./${path}" == "${pathname}"* ]]; then
90 skip=true
91 break
92 fi
93 done
94
95 if [ "$skip" = true ]; then
96 continue
97 fi
Patrick Williams384d7412020-11-06 16:15:41 -060098 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -080099 searchfiles+="\"./${path}\" "
100
101# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -0600102done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -0800103
Patrick Venture30ec0c42018-10-22 11:56:27 -0700104if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -0600105 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -0800106 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -0600107 git --no-pager diff --exit-code
108fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030109
110# Sometimes your situation is terrible enough that you need the flexibility.
111# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700112if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030113 ./format-code.sh
114 git --no-pager diff --exit-code
115fi