forked from mavlink/MAVSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_style.sh
executable file
·111 lines (87 loc) · 2.76 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
#!/usr/bin/env bash
# This script runs clang-format over all files ending in .h, .c, .cpp listed
# by git in the given directory.
# Check for the clang-format version
version_required_major="6"
clang_format_version() {
echo "At least ${version_required} is required"
}
# check if clang-format is installed
condition=$(which clang-format 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
echo "clang-format is not installed"
clang_format_version
exit 1
else
version=$(clang-format --version)
semver_regex="(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)"
if [[ $version =~ $semver_regex ]]; then
version_major=${BASH_REMATCH[1]}
if [ "$version_required_major" -gt "$version_major" ]; then
echo "Clang version $version_major too old (required >= $version_required_major)"
exit 1
fi
else
echo "Could not determine clang-format version"
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"
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 style checks, go to next file.
continue
fi
cp $file $file.orig
result=`clang-format -style=file -i $file`
if ! cmp $file $file.orig >/dev/null 2>&1; then
echo "Changed $file:"
$diff_cmd $file.orig $file
error_found=true
fi
rm $file.orig
# 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