generated from cpp-best-practices/gui_starter_template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
formatLastCommit.sh
executable file
·62 lines (54 loc) · 1.56 KB
/
formatLastCommit.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
#/bin/bash
#enforces .clang-format style guide prior to committing to the git repository
CLANG_MIN_VERSION="9.0.0"
set -e
CLANG_FORMAT="$(command -v clang-format)"
CLANG_VERSION="$(${CLANG_FORMAT} --version | sed '/^clang-format version /!d;s///;s/-.*//;s///g')"
compare_version () {
echo " "
if [[ $1 == $2 ]]
then
CLANG_MIN_VERSION_MATCH="="
return
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
CLANG_MIN_VERSION_MATCH="<"
return
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
CLANG_MIN_VERSION_MATCH=">"
return
fi
done
CLANG_MIN_VERSION_MATCH="="
return
}
compare_version ${CLANG_MIN_VERSION} ${CLANG_VERSION}
git reset HEAD~1 --soft
files=$((git diff --name-only --cached | grep -Ei "\.(c|cc|cpp|cxx|c\+\+|h|hh|hpp|hxx|h\+\+|java)$") || true)
if [ -n "${files}" ]; then
if [ -n "${CLANG_FORMAT}" ] && [ "$CLANG_MIN_VERSION_MATCH" != "<" ]; then
spaced_files=$(echo "$files" | paste -s -d " " -)
echo "reformatting ${spaced_files}"
"${CLANG_FORMAT}" -style=file -i $spaced_files >/dev/null
git --no-pager diff
git add ${spaced_files}
fi
fi
git commit -C ORIG_HEAD