-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_code_format.sh
50 lines (38 loc) · 1.31 KB
/
check_code_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
#!/bin/bash
# Runs clang-format and fails if any file has changed
# Copyright (c) 2020 Benardi Nunes <[email protected]>
# ---------------------------------------------------------------------
# Make it executable
# chmod +x ./check_code_format.sh
# Requires Git and clang-format to be installed
# sudo apt install git-all
# sudo apt install clang-format
cleanstate=`git status | grep "modified"`
if ! [[ -z $cleanstate ]]; then
echo "Script must be applied on a clean git state"
exit 1
fi
# Execute clang-format
/bin/bash ./format_code.sh
# check if something was modified
notcorrectlist=`git status | grep "modified"`
n_total="$(ls {tests/*.c,src/*.c,src/include/*.h} -1 | wc -l)"
if [[ -z $notcorrectlist ]]; then
# if nothing changed ok
echo "┬──┬◡ノ(° -°ノ)"
echo "${n_total} files would be left unchanged"
exit 0;
else
changed="$(git diff --name-only ./src/ ./tests/)"
n_changed="$(git diff --name-only ./src/ ./tests | wc -l)"
declare -i n_unchanged
n_unchanged=$((${n_total} - ${n_changed}))
for file in ${changed}; do
echo "would reformat ${PWD}/${file}"
done
echo "(╯°□°)╯︵ ┻━┻"
echo "${n_changed} files would be reformatted, ${n_unchanged} files would be left unchanged."
fi
# cleanup changes in git
git reset HEAD --hard &> /dev/null
exit 1