forked from ggreer/the_silver_searcher
-
Notifications
You must be signed in to change notification settings - Fork 5
/
format.sh
executable file
·57 lines (51 loc) · 1.26 KB
/
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
#!/bin/bash
function usage() {
echo "Usage: $0 test|reformat"
}
if [ $# -eq 0 ]
then
usage
exit 0
fi
if [ -z "$CLANG_FORMAT" ]
then
CLANG_FORMAT=clang-format
echo "No CLANG_FORMAT set. Using $CLANG_FORMAT"
fi
if ! type "$CLANG_FORMAT" &> /dev/null
then
echo "The command \"$CLANG_FORMAT\" was not found"
exit 1
fi
SOURCE_FILES=$(git ls-files src/)
if [ "$1" == "reformat" ]
then
echo "Reformatting source files"
# shellcheck disable=2086
echo $CLANG_FORMAT -style=file -i $SOURCE_FILES
# shellcheck disable=2086
$CLANG_FORMAT -style=file -i $SOURCE_FILES
exit 0
elif [ "$1" == "test" ]
then
# shellcheck disable=2086
RESULT=$($CLANG_FORMAT -style=file -output-replacements-xml $SOURCE_FILES | grep -c '<replacement ')
if [ "$RESULT" -eq 0 ]
then
echo "code is formatted correctly :)"
exit 0
else
echo "code is not formatted correctly! :("
echo "Suggested change:"
cp -r src clang_format_src
$CLANG_FORMAT -style=file -i clang_format_src/*.c clang_format_src/*.h
diff -ur src clang_format_src
rm -r clang_format_src
echo "Run '$0 reformat' to fix formatting"
exit 1
fi
else
echo "invalid command: $1"
usage
exit 1
fi