forked from vmware/concord-bft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-code.sh
executable file
·56 lines (49 loc) · 1.41 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
#!/usr/bin/env sh
BFT="concord-bft"
if [ -z "$1" ]; then
>&2 echo "Usage: format-code.sh <path>"
return 1
fi
# Construct the absolute path
export TMP_CONCORD_DIR="$1"
ABS_CONCORD_PATH=$(
python -c 'import os; print(os.path.abspath(os.environ["TMP_CONCORD_DIR"]))')
unset TMP_CONCORD_DIR
# Overly cautious saftey check
IS_EXPECTED_NAME=$(echo ${ABS_CONCORD_PATH} | grep "${BFT}")
if [ ! -e ${ABS_CONCORD_PATH} ] || [ -z ${IS_EXPECTED_NAME} ]; then
>&2 echo "ERROR: Couldn't find \"${ABS_CONCORD_PATH}\" in ${BFT} directory"
return 1;
fi
FILES_TO_FORMAT=$(find ${ABS_CONCORD_PATH} \
-type f \( \
-iname "*.c" -o \
-iname "*.cc" -o \
-iname "*.cpp" -o \
-iname "*.h" -o \
-iname "*.hpp" \) \
-a -not -path "${ABS_CONCORD_PATH}/deps/*" \
-a -not -path "${ABS_CONCORD_PATH}/build/*")
if [ -n "$2" ]; then
if [ "$2" = "--is-required" ]; then
NUM_CHANGES=$(clang-format-7 \
-verbose \
-style=file \
-fallback-style=none \
-output-replacements-xml ${FILES_TO_FORMAT} \
| grep "<replacement offset" \
| wc -l)
if [ ${NUM_CHANGES} -ne 0 ]; then
# Note: exit_code = return_value % 255
echo "Code format changes needed"
return 1
else
echo "No format changes needed"
return 0
fi
fi
>&2 echo "ERROR: Unknown parameter \"$2\""
return 1
else
clang-format-7 -verbose -style=file -fallback-style=none -i ${FILES_TO_FORMAT}
fi