forked from microsoft/CCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-format.sh
executable file
·48 lines (42 loc) · 1.09 KB
/
check-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
#!/bin/bash
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
set -u
if [ "$#" -eq 0 ]; then
echo "No args given - specify dir(s) to be formatted"
exit 1
fi
fix=false
while getopts ":f:" opt; do
case $opt in
f)
fix=true
shift
;;
\?) echo "Invalid option -$OPTARG" >&2
exit
;;
esac
done
echo "Checking file format in" "$@"
unformatted_files=""
for file in $(find "$@" -name "*.h" -or -name "*.hpp" -or -name "*.cpp" -or -name "*.c"); do
# Workaround for https://bugs.llvm.org/show_bug.cgi?id=39216
d=$(cat "$file" | clang-format-7 -style=file --assume-filename "${file%.*}".cpp | diff "$file" -)
if $fix ; then
cat "$file" | clang-format-7 -style=file --assume-filename "${file%.*}".cpp > "$file".tmp
mv "$file".tmp "$file"
fi
if [ "$d" != "" ]; then
if [ "$unformatted_files" != "" ]; then
unformatted_files+=$'\n'
fi
unformatted_files+="$file"
fi
done
if [ "$unformatted_files" != "" ]; then
echo "$unformatted_files"
exit 1
else
echo "All files formatted correctly"
fi