/
format-code.sh
executable file
·139 lines (114 loc) · 3.77 KB
/
format-code.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# This script reformats source files using the clang-format utility.
#
# Files are changed in-place, so make sure you don't have anything open in an
# editor, and you may want to commit before formatting in case of awryness.
#
# This must be run on a clean repository to succeed
#
# Input parmameter must be full path to git repo to scan
DIR=$1
WORKSPACE=$PWD
set -e
echo "Running spelling check on Commit Message"
# Run the codespell with openbmc spcific spellings on the patchset
echo "openbmc-dictionary - misspelling count >> "
codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG
# Run the codespell with generic dictionary on the patchset
echo "generic-dictionary - misspelling count >> "
codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG
cd "${DIR}"
echo "Formatting code under $DIR/"
ESLINT_CONFIG="eslint ${DIR} --no-eslintrc -c ${WORKSPACE}/eslint-global-config.json \
--ext .json --resolve-plugins-relative-to /usr/local/lib/node_modules\
--no-error-on-unmatched-pattern"
ESLINT_IGNORE=" --ignore-path ${DIR}/.eslintignore"
# Get the eslint configuration from the repository
if [[ -f ".eslintrc.json" ]] || [[ -f ".eslintignore" ]]; then
echo "Running the json validator on the repo using it's config > "
if [[ ! -f ".eslintrc.json" ]]; then
eslint_command="${ESLINT_CONFIG} ${ESLINT_IGNORE}"
else
eslint_command="$ESLINT_CONFIG"
fi
else
echo "Running the json validator on the repo using the global config > "
eslint_command="$ESLINT_CONFIG"
fi
# Print eslint command
echo "$eslint_command"
# Run eslint
$eslint_command
if [[ -f "setup.cfg" ]]; then
pycodestyle --show-source --exclude=subprojects .
rc=$?
if [[ ${rc} -ne 0 ]]; then
exit ${rc}
fi
fi
# If .shellcheck exists, stop on error. Otherwise, allow pass.
if [[ -f ".shellcheck" ]]; then
shellcheck_allowfail="false"
else
shellcheck_allowfail="true"
fi
# Run shellcheck on any shell-script.
shell_scripts="$(git ls-files | xargs -n1 file -0 | \
grep -a "shell script" | cut -d '' -f 1)"
for script in ${shell_scripts}; do
shellcheck -x "${script}" || ${shellcheck_allowfail}
done
# Allow called scripts to know which clang format we are using
export CLANG_FORMAT="clang-format"
IGNORE_FILE=".clang-ignore"
declare -a IGNORE_LIST
if [[ -f "${IGNORE_FILE}" ]]; then
readarray -t IGNORE_LIST < "${IGNORE_FILE}"
fi
ignorepaths=""
ignorefiles=""
for path in "${IGNORE_LIST[@]}"; do
# Check for comment, line starting with space, or zero-length string.
# Checking for [[:space:]] checks all options.
if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
continue
fi
# All paths must start with ./ for find's path prune expectation.
if [[ "${path}" =~ ^\.\/.+$ ]]; then
ignorepaths+=" ${path}"
else
ignorefiles+=" ${path}"
fi
done
searchfiles=""
while read -r path; do
# skip ignorefiles
if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
continue
fi
skip=false
#skip paths in ingorepaths
for pathname in $ignorepaths; do
if [[ "./${path}" == "${pathname}"* ]]; then
skip=true
break
fi
done
if [ "$skip" = true ]; then
continue
fi
# shellcheck disable=2089
searchfiles+="\"./${path}\" "
# Get C and C++ files managed by git and skip the mako files
done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
if [[ -f ".clang-format" ]]; then
# shellcheck disable=SC2090 disable=SC2086
echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
git --no-pager diff --exit-code
fi
# Sometimes your situation is terrible enough that you need the flexibility.
# For example, phosphor-mboxd.
if [[ -f "format-code.sh" ]]; then
./format-code.sh
git --no-pager diff --exit-code
fi