forked from mavlink/MAVSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_style.sh
executable file
·131 lines (107 loc) · 3.55 KB
/
fix_style.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
#!/bin/bash
# This script runs astyle and complains about lines that are too long
# over all files ending in .h, .c, .cpp listed by git in the given
# directory.
# Check for the latest astyle version
ASTYLE_VER_REQUIRED_1="Artistic Style Version 2.05.1"
ASTYLE_VER_REQUIRED_2="Artistic Style Version 2.06"
ASTYLE_VER_REQUIRED_3="Artistic Style Version 3.0"
ASTYLE_VER_REQUIRED_4="Artistic Style Version 3.0.1"
ASTYLE_VER_REQUIRED_5="Artistic Style Version 3.1"
astyle_ver() {
echo "At least ${ASTYLE_VER_REQUIRED_1} is required"
echo "You can get the correct version here: https://sourceforge.net/projects/astyle/files/astyle/astyle%202.06/"
}
# check if astyle is installed
condition=$(which astyle 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
echo "astyle is not installed"
astyle_ver
exit 1
else
ASTYLE_VER=`astyle --version`
if [ "$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_1" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_2" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_3" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_4" -a \
"$ASTYLE_VER" != "$ASTYLE_VER_REQUIRED_5" ];
then
echo "Error: you're using ${ASTYLE_VER}"
echo "but should be using ${ASTYLE_VER_REQUIRED} instead"
exit 1
fi
fi
# Check that exactly one directory is given
if [ $# -eq 0 ];
then
echo "No directory supplied"
echo "Usage: ./fix_style.sh dir"
exit 1
elif [ $# -gt 1 ];
then
echo "Too many directories supplied"
echo "Usage: ./fix_style.sh dir"
exit 1
fi
# Ignore files listed in style-ignore.txt
style_ignore="style-ignore.txt"
# Find the directory of this script because the file astylerc should be
# next to it.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Keep track of errors for the exit value
error_found=false
# Use colordiff if available
if command -v colordiff >/dev/null 2>&1; then
diff_cmd=colordiff
else
diff_cmd=diff
fi
cd $1 > /dev/null
# Go through all .h, c., and .cpp files listed by git
# TODO: add -r argument to include all files
files=`git ls-files | grep -E "\.h$|\.c$|\.cpp$|\.proto"`
while IFS= read file; do
# We want to check if the file is in the list to ignore.
# We do this in a brute force way by looping through every
# line the ignore file and compare it against the filename.
if [[ -f $SCRIPT_DIR/$style_ignore ]]; then
need_to_ignore=false
while IFS= read ignore; do
if [[ `echo $1/$file | grep "$ignore"` ]]; then
need_to_ignore=true
fi
done < "$SCRIPT_DIR/$style_ignore"
fi
if [ "$need_to_ignore" = true ]; then
# Don't do the astyle and file length checks,
# go to next file.
continue
fi
# Run astyle with given astylerc
astyle_result=`astyle --options=$SCRIPT_DIR/astylerc $file | grep "Formatted"`
if [[ $astyle_result ]]; then
echo "Formatted $file:"
$diff_cmd $file $file.orig
rm $file.orig
error_found=true
fi
# TODO: this is disabled for now
## Go line by line
#count=0
#while IFS= read -r line; do
# # Check for lines too long
# len=${#line}
# if [ $len -gt 100 ]; then
# echo "Line $count too long"
# error_found=true
# fi
# (( count++ ))
#done < "$file"
# We need to use this clunky way because otherwise
# we lose the value of error_found.
# See http://mywiki.wooledge.org/BashFAQ/024
done < <(echo "$files")
cd - > /dev/null
if [ "$error_found" = true ]; then
exit 1
fi