1
1
#! /bin/bash
2
+ # Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
3
+ # For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4
+
5
+ set -e
6
+ set -o pipefail
7
+ # trap 'echo ERROR on line: $LINENO running command: $BASH_COMMAND ; trap - EXIT; exit $?' EXIT
8
+
2
9
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
3
10
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
4
11
7
14
# - with use of clang-format diff.py - for changed chunk changes only
8
15
# might be worth to consider: https://github.com/Sarcasm/run-clang-format
9
16
17
+ # shellcheck source=../config/format-config.sh
18
+ # shellcheck source=../config/clang/colors.sh
19
+ # shellcheck source=../config/clang/common.sh
10
20
11
21
L_GIT_DIR=$( git rev-parse --show-toplevel)
12
22
source $L_GIT_DIR /config/format-config.sh
13
-
14
- RED=' \e[38;2;255;13;0m'
15
- YELLOW=" \e[38;2;255;232;0m"
16
- GREEN=" \e[38;2;0;222;27m"
17
- ORANGE=" \e[38;2;255;100;0m"
18
-
19
- OK=" ${GREEN} OK!\e[0m"
20
- ERROR=" ${RED} Error!\e[0m"
21
- FIXED=" ${YELLOW} Fixed!\e[0m"
22
-
23
+ source $L_GIT_DIR /config/clang/colors.sh
24
+ source $L_GIT_DIR /config/clang/clang-common.sh
23
25
24
26
# if autoformatting was disabled by user - then ignore this commit hook
25
- if [[ $DISABLE_AUTO_FORMATTING ] ]; then
27
+ if [ " $DISABLE_AUTO_FORMATTING " -eq 1 ]; then
26
28
[[ $VERBOSE ]] && echo " auto formatting is disabled"
27
29
exit 0
28
30
fi
29
31
30
- # check if clang-format in path is in proper version, version is 3rd column in `clang-format --version`
31
- CVER=$( [[ $( which clang-format) ]] && echo $( clang-format --version | cut -d ' ' -f 3 | cut -d ' .' -f 1) || echo " 0" )
32
- # check for either clang-format or clang-format-9
33
- if [[ $CVER -lt 10 && ! $( which clang-format-10) ]]; then
34
- cat << EOF >&1
35
- Either install:
36
- clang-format in at least version 10 and set as default"
37
- or
38
- clang-format-10
39
-
40
- Your clang format version in path used:
41
- $( clang-format --version) :
42
- $( clang-format-10 --version)
43
- git commit aborted"
44
- EOF
45
- exit 1
46
- fi
32
+ verify_clang_format_version
47
33
48
- get_clang_format_script () {
49
- local declare searchpaths=(
50
- $( which " clang-format-diff.py" ) # clang-format-diff in path
51
- " /usr/share/clang/clang-format-10/clang-format-diff.py" # clang-format-diff location for clang format 9 Ubuntu/Debian 18.04
52
- " /usr/share/clang/clang-format-${CVER} /clang-format-diff.py" # clang-format-diff location on Ubuntu/Debian
53
- " /usr/share/clang/clang-format-diff.py" # clang-format_diff location on Arch last resort
54
- )
55
- local tool=" "
56
- for el in ${searchpaths[@]} ; do
57
- if [[ -f ${el} ]]; then
58
- tool=${el}
59
- break
60
- fi
61
- done
62
- if [[ ${tool} == " " ]]; then
63
- echo clang-format-diff not found in path and: ${sarchpaths[@]} > 2
64
- fi
65
- echo " ${tool} "
66
- }
67
-
68
- L_CLANG_DIFF_TOOL=$( get_clang_format_script)
34
+ L_CLANG_DIFF_TOOL=$( get_clang_format)
69
35
if ! [[ $L_CLANG_DIFF_TOOL ]] || [[ $L_CLANG_DIFF_TOOL == " " ]]; then
36
+ [[ $VERBOSE ]] && echo " clang tool not found"
70
37
exit 1
71
38
fi
72
39
40
+
73
41
check_file () {
74
42
file=" ${1} "
75
43
last_commit=" ${2} "
76
- if [ -f $ file ]; then
44
+ if [ -f " ${ file} " ]; then
77
45
# [[ $VERBOSE ]] && echo -en "Checking: \e[33m${file}\e[0m :\t"
78
46
results[" ${file} " ]=" ${OK} "
79
47
case ${last_commit} in
80
48
" True" )
81
49
if [[ ${FIX,,} == " true" ]]; then
82
- git diff -U0 --no-color remotes/origin/master...HEAD ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
83
- STATUS=$( git status --short -- ${file} )
50
+ git diff -U0 --no-color remotes/origin/master...HEAD " ${file} " | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
51
+ STATUS=$( git status --short -- " ${file} " )
84
52
if [ -n " ${STATUS} " ]; then
85
53
git add " ${file} "
86
54
results[" ${file} " ]=" ${FIXED} " ;
87
55
fi
88
56
else
89
- OUT=$( git diff -U0 --no-color remotes/origin/master...HEAD ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 )
57
+ OUT=$( git diff -U0 --no-color remotes/origin/master...HEAD " ${file} " | ${L_CLANG_DIFF_TOOL} -style file -p1 )
90
58
if [ -n " ${OUT} " ]; then
91
59
results[" ${file} " ]=" ${ERROR} "
92
60
return 1
@@ -95,23 +63,23 @@ check_file() {
95
63
;;
96
64
" Stage" )
97
65
if [[ ${FIX,,} == " true" ]]; then
98
- git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
99
- STATUS=$( git status --short -- ${file} )
66
+ git diff -U0 --no-color --cached " ${file} " | ${L_CLANG_DIFF_TOOL} -style file -p1 -i
67
+ STATUS=$( git status --short -- " ${file} " )
100
68
if [ -n " ${STATUS} " ]; then
101
69
git add " ${file} "
102
70
results[" ${file} " ]=" ${FIXED} " ;
103
71
fi
104
72
else
105
- OUT=$( git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1)
73
+ OUT=$( git diff -U0 --no-color --cached " ${file} " | ${L_CLANG_DIFF_TOOL} -style file -p1)
106
74
if [ -n " ${OUT} " ]; then
107
75
results[" ${file} " ]=" ${ERROR} "
108
76
return 1
109
77
fi
110
78
fi
111
79
;;
112
80
* )
113
- OUT=$( git diff -U0 --no-color --cached ${file} | ${L_CLANG_DIFF_TOOL} -style file -p1 )
114
- if [ -n ${OUT} ]; then
81
+ OUT=$( git diff -U0 --no-color --cached " ${file} " | ${L_CLANG_DIFF_TOOL} -style file -p1 )
82
+ if [[ -n ${OUT} ] ]; then
115
83
results[" ${file} " ]=" ${ERROR} "
116
84
return 1
117
85
fi
@@ -121,19 +89,6 @@ check_file() {
121
89
return 0
122
90
}
123
91
124
- # bash function using above config function
125
- shouldnt_ignore () {
126
- # change full name path to path relative to root git dir
127
- local fname=${1/ " $L_GIT_DIR " / " ./" }
128
- for el in ${ignore_paths[@]} ; do
129
- if [[ ${fname} =~ ^${el} .* ]]; then
130
- [[ $VERBOSE ]] && echo " Ignore: ${fname} formatting due to: $el match!"
131
- return 1
132
- fi
133
- done
134
- return 0
135
- }
136
-
137
92
function help() {
138
93
echo " Runs clang-format on source files"
139
94
cat << - EOM
@@ -176,14 +131,13 @@ case "${1}" in
176
131
;;
177
132
* )
178
133
if [[ $# -ne 0 ]]; then
179
- echo " unknown parameters: '$@ '"
134
+ echo " unknown parameters: '$* '"
180
135
help
181
136
exit 1
182
137
fi
183
138
FILES=$( git diff-index --cached --name-only HEAD)
184
139
LAST=" Stage"
185
- FIX=$( git config user.fixinstage)
186
- FIX=${FIX:- false}
140
+ FIX=$( [[ $( git config user.fixinstage) == " true" ]] && echo " true" || echo " false" )
187
141
;;
188
142
esac
189
143
0 commit comments