blob: d8ef389bd818a9259892597f73957b3158d4b425 [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
Patrick Williams384d7412020-11-06 16:15:41 -060013cd "${DIR}"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080014
Andrew Jeffery0cce8482018-04-30 11:37:01 +093015set -e
16
Andrew Geissler9b0105c2018-01-10 10:58:35 -080017echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080018
Patrick Venture30ec0c42018-10-22 11:56:27 -070019if [[ -f "setup.cfg" ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060020 pycodestyle --show-source .
21 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070022 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060023 exit ${rc}
24 fi
25fi
26
Patrick Williams01ced282020-07-17 15:10:15 -050027# If .shellcheck exists, stop on error. Otherwise, allow pass.
28if [[ -f ".shellcheck" ]]; then
29 shellcheck_allowfail="false"
30else
31 shellcheck_allowfail="true"
32fi
33
34# Run shellcheck on any shell-script.
35shell_scripts="$(git ls-files | xargs -n1 file -0 | \
36 grep -a "shell script" | cut -d '' -f 1)"
37for script in ${shell_scripts}; do
38 shellcheck -x "${script}" || ${shellcheck_allowfail}
39done
40
William A. Kennington III078c3b52018-10-04 19:44:31 -070041# Allow called scripts to know which clang format we are using
Patrick Williamsd83ca9a2021-02-22 14:29:21 -060042export CLANG_FORMAT="clang-format"
Patrick Venture366dd762018-10-22 13:55:03 -070043IGNORE_FILE=".clang-ignore"
44declare -a IGNORE_LIST
45
46if [[ -f "${IGNORE_FILE}" ]]; then
47 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
48fi
49
50ignorepaths=""
51ignorefiles=""
52
53for path in "${IGNORE_LIST[@]}"; do
54 # Check for comment, line starting with space, or zero-length string.
55 # Checking for [[:space:]] checks all options.
56 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
57 continue
58 fi
59
60 # All paths must start with ./ for find's path prune expectation.
61 if [[ "${path}" =~ ^\.\/.+$ ]]; then
James Feistf1665d62019-11-22 09:06:33 -080062 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070063 else
James Feistf1665d62019-11-22 09:06:33 -080064 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070065 fi
66done
William A. Kennington III078c3b52018-10-04 19:44:31 -070067
James Feistf1665d62019-11-22 09:06:33 -080068searchfiles=""
Patrick Williams384d7412020-11-06 16:15:41 -060069while read -r path; do
James Feistf1665d62019-11-22 09:06:33 -080070 # skip ignorefiles
71 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
72 continue
73 fi
74
75 skip=false
76 #skip paths in ingorepaths
77 for pathname in $ignorepaths; do
78 if [[ "./${path}" == "${pathname}"* ]]; then
79 skip=true
80 break
81 fi
82 done
83
84 if [ "$skip" = true ]; then
85 continue
86 fi
Patrick Williams384d7412020-11-06 16:15:41 -060087 # shellcheck disable=2089
James Feistf1665d62019-11-22 09:06:33 -080088 searchfiles+="\"./${path}\" "
89
90# Get C and C++ files managed by git and skip the mako files
Patrick Williams384d7412020-11-06 16:15:41 -060091done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
James Feistf1665d62019-11-22 09:06:33 -080092
Patrick Venture30ec0c42018-10-22 11:56:27 -070093if [[ -f ".clang-format" ]]; then
Patrick Williams384d7412020-11-06 16:15:41 -060094 # shellcheck disable=SC2090 disable=SC2086
James Feistf1665d62019-11-22 09:06:33 -080095 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060096 git --no-pager diff --exit-code
97fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +103098
99# Sometimes your situation is terrible enough that you need the flexibility.
100# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -0700101if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +1030102 ./format-code.sh
103 git --no-pager diff --exit-code
104fi