-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyle-format.sh
executable file
·132 lines (106 loc) · 3.21 KB
/
style-format.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
#!/bin/bash
#
# Check or apply/fix the project coding style to all files passed as arguments.
# Uses clang-format for C and flake8 for Python.
#
# Requires clang-format version 10 (apt install clang-format-10).
#
CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
set -e
ret=0
if [[ -z $1 ]]; then
echo "Usage: $0 [--fix] srcfile1.c srcfile2.h srcfile3.c ..."
echo ""
exit 0
fi
if [[ $1 == "--fix" ]]; then
fix=1
shift
else
fix=0
fi
clang_format_version=$(${CLANG_FORMAT} --version | sed -Ee 's/.*version ([[:digit:]]+)\.[[:digit:]]+\.[[:digit:]]+.*/\1/')
if [[ $clang_format_version != "10" ]] ; then
echo "$0: clang-format version 10, '$clang_format_version' detected"
exit 1
fi
# Get list of files from .formatignore to ignore formatting for.
ignore_files=( $(grep '^[^#]..' .formatignore) )
function ignore {
local file=$1
local f
for f in "${ignore_files[@]}" ; do
[[ $file == $f ]] && return 0
done
return 1
}
extra_info=""
for f in $*; do
if ignore $f ; then
echo "$f is ignored by .formatignore" 1>&2
continue
fi
lang="c"
if [[ $f == *.py ]]; then
lang="py"
style="pep8"
stylename="pep8"
else
style="file" # Use .clang-format
stylename="C"
fi
check=0
if [[ $fix == 1 ]]; then
# Convert tabs to 8 spaces first.
if grep -ql $'\t' "$f"; then
sed -i -e 's/\t/ /g' "$f"
echo "$f: tabs converted to spaces"
fi
if [[ $lang == c ]]; then
# Run clang-format to reformat the file
${CLANG_FORMAT} --style="$style" "$f" > _styletmp
else
# Run autopep8 to reformat the file.
python3 -m autopep8 -a "$f" > _styletmp
# autopep8 can't fix all errors, so we also perform a flake8 check.
check=1
fi
if ! cmp -s "$f" _styletmp; then
echo "$f: style fixed ($stylename)"
# Use cp to preserve target file mode/attrs.
cp _styletmp "$f"
rm _styletmp
fi
fi
if [[ $fix == 0 || $check == 1 ]]; then
# Check for tabs
if grep -q $'\t' "$f" ; then
echo "$f: contains tabs: convert to 8 spaces instead"
ret=1
fi
# Check style
if [[ $lang == c ]]; then
if ! ${CLANG_FORMAT} --style="$style" --Werror --dry-run "$f" ; then
echo "$f: had style errors ($stylename): see clang-format output above"
ret=1
fi
elif [[ $lang == py ]]; then
if ! python3 -m flake8 "$f"; then
echo "$f: had style errors ($stylename): see flake8 output above"
if [[ $fix == 1 ]]; then
# autopep8 couldn't fix all errors. Let the user know.
extra_info="Error: autopep8 could not fix all errors, fix the flake8 errors manually and run again."
fi
ret=1
fi
fi
fi
done
rm -f _styletmp
if [[ $ret != 0 ]]; then
echo ""
echo "You can run the following command to automatically fix the style:"
echo " $ make style-fix"
[[ -n $extra_info ]] && echo "$extra_info"
fi
exit $ret