-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·254 lines (217 loc) · 9.62 KB
/
pre-commit
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# TODO integrate https://github.com/espressif/conventional-precommit-linter
# TODO add C6 and H2 to FQBN list when ready
# TODO implement checks for tools (arduino-cli / brew)
###################
# Note - this hook uses arduino-cli. If you don't have it installed
# setup:
# check brew: brew; echo $?
# check arduino cli: arduino-cli version; echo $?
# check arduino 3rd party: arduino-cli core install esp32:esp32
# change fork to
# ln -s $(pwd) ~/.arduino15/packages/esp32/hardware/esp32/2.0.9
# Terminate compilation check for a particular sketch after first fail - rest of the examples will be still compiled (default is false)
skip_example_after_single_fail=false
# Terminate all compilation checks after first fail - by default compiles everything (default value false)
fast_fail=false
# This enables you to skip the hook (will be considered success) especially while this
# hook is in development and not all the heuristics are implemented.
# For example when you add a ton of examples, most of them compile successfully, but only few fail
# after you fix them this hook would (as it is implemented now) compile all of the examples.
exec < /dev/tty
while true; do
read -n 1 -p "Do you want to run pre-commit hook? (N=pass) [Y/N]: " choice
echo -e "\nDebug: response=\"$choice\""
case $choice in
[Yy]* ) echo "Running pre-commit hook: check secrets; compile staged failed_sketches.\n"; break;;
[Nn]* ) echo "Skipping pre-commit hook. This evaluated as passed."; exit 0;;
* ) echo "Invalid input. Please enter 'y' or 'n'.";;
esac
done
########################################################################################################
# Hook variables and constants
# Variable to track if the hook should fail
changes_made=false
test_failed=false
sketches_to_compile=() # An array of examples to be compiled
failed_sketches=() # An array of failed_sketches which failed compilation and will be printed
report=() # Create an empty array to store the report
declare -a fqbn_list=(
"esp32:esp32:esp32" # ESP32 development board"
"esp32:esp32:esp32s2" # ESP32-S2 development board"
"esp32:esp32:esp32c3" # ESP32-C3 development board"
"esp32:esp32:esp32s3" # ESP32-S3 development board"
#"esp32:esp32:esp32c6" # ESP32-C6 development board"
#"esp32:esp32:esp32h2" # ESP32-H2 development board"
)
# Define color escape sequences
RED='\033[0;31m'
GREEN='\033[0;32m'
D_GRAY='\033[90m'
L_GRAY='\033[37m'
RED_BG='\033[41m'
GREEN_BG='\033[42m'
RESET='\033[0m'
########################################################################################################
##
## @brief Determines if a string is present in an Array.
## @description Used to avoid duplicate compilation of examples and duplicate library search
## (only single file in lib is enough to trigger compile all examples)
## @return True if present, False otherwise.
##
isPresent() {
local arr=("$@")
local target="${arr[-1]}"
unset 'arr[${#arr[@]}-1]'
for item in "${arr[@]}"; do
if [[ "$item" == "$target" ]]; then
return 0 # Found
fi
done
return 1 # Not found
}
########################################################################################################
# Secrets test
# Path to the secrets
echo "### Running check secrets test..."
SECRETS="/home/pilnyt/Arduino/secrets.h"
# Secret value to replace with
SAMPLE_SECRET="*****"
if [ ! -f "$SECRETS" ]; then
echo "File secrets.h does not exist at the given path \"$SECRETS\""
exit 1
fi
# Read the secrets file and extract secrets
secrets=($(grep -oP '"[^"]+"' "$SECRETS" | tr -d '"'))
#echo "Extracted secrets: ${secrets[*]}"
# Iterate over the staged files
while IFS= read -r -d '' file; do
#echo "Staged contents of $file:"
for secret in ${secrets[@]}; do
#echo "Check for secret $secret"
if git show :"$file" | grep -q "$secret"; then
sed -i "s/$secret/$SAMPLE_SECRET/g" "$file${RESET}"
echo "${RED_BG}${L_GRAY}Replaced a secret in $file"
#echo "${RED_BG}${L_GRAY}Replaced $secret value in $file${RESET}"
changes_made=true
fi
done
done < <(git diff --cached --name-only -z)
# Check if any changes were made and exit accordingly
if [ "$changes_made" = true ]; then
echo -e "${RED_BG}${L_GRAY}Test failed: Changes were made${RESET}"
echo "Run \"git diff\" to see the automatic changes if they are ok, continue by adding the changed file \"git add <changed_file>\" and finish by repeating the commit."
else
echo -e "${GREEN_BG}${D_GRAY}Test secrets passed${RESET}"
fi
echo ""
########################################################################################################
# Search for modified libraries (in libraries/) and schedule all example for those libraries to be compiled
echo "### Search for modified libraries (in libraries/) and schedule all example for those libraries to be compiled"
sketches_to_compile=()
libraries=() # List of libraries already searched and scheduled for compilation
# Usage example
myArr=("foo" "bar" "baz")
while IFS= read -r -d '' sketch; do
if [[ "$sketch" == libraries/* && ! "$sketch" == *examples* ]]; then
library_path=$(dirname "$(dirname "$sketch")")
library_name=$(basename "$library_path")
examples_folder="$library_path/examples"
if ! isPresent "${libraries[@]}" $library_name; then
echo "Adding $library_name for compilation."
libraries+=("$library_name")
if [[ -d "$examples_folder" ]]; then
while IFS= read -r -d '' example; do
if [[ "$example" == *.ino ]]; then
echo "Scheduling for compilation: $example"
sketches_to_compile+=("$example")
fi
done < <(find "$examples_folder" -type f -name '*.ino' -print0)
else
echo -e "${RED}Examples folder not found for library: $library_name${RESET}"
fi # if [[ -d "$examples_folder" ]]
else # if ! isPresent "${libraries[@]}" $library_name
echo "Library already scheduled for compilation: $library_name" # debug
fi # if isPresent "${libraries[@]}" $library_name
fi # if [[ "$sketch" == libraries/* ]]
done < <(git diff --cached --name-only -z)
echo ""
########################################################################################################
# Add modified sketched to compilation array (if not already scheduled)
echo "### Add modified sketched to compilation array (if not already scheduled)"
while IFS= read -r -d '' sketch; do
#echo "Debug: Checking file: \"$sketch\"" # debug
if [[ "$sketch" == *.ino ]]; then
if isPresent "${sketches_to_compile[@]}" "$sketch"; then
echo "Already scheduled for compilation: $sketch" # debug
else
echo "Scheduling for compilation: $sketch"
sketches_to_compile+=("$sketch")
fi # if isPresent "${sketches_to_compile[@]}" $sketch
fi # if [[ "$sketch" == *.ino ]]
done < <(git diff --cached --name-only -z)
echo ""
########################################################################################################
# Compile scheduled examples
echo "### Compile scheduled sketches"
#echo "Debug: sketches_to_compile: ${sketches_to_compile[@]}" # debug
for sketch in "${sketches_to_compile[@]}"; do
echo "> Compiling sketch: \"$sketch\""
failed_fqbn=()
for fqbn in "${fqbn_list[@]}"; do
# Get the directory of the sketch
sketch_dir=$(dirname "$sketch")
# Check if there is a hidden file matching the FQBN in the sketch directory
skip_file="${sketch_dir}/.skip.${fqbn##*:}"
if [[ -f "$skip_file" ]]; then
echo " > Skipping FQBN: $fqbn"
continue
fi
echo " > FQBN: $fqbn"
#echo "arduino-cli compile --fqbn $fqbn $sketch 2>&1"
# Compile the sketch using arduino-cli
output=$(arduino-cli compile --fqbn "$fqbn" "$sketch" --verbose)
result=$?
#output=$(arduino-cli compile --fqbn "$fqbn" "$sketch")
echo "$output" >> pre-commit-compilation.log
#echo "arduino-cli returned with result $result"
if [[ result -ne 0 ]]; then
echo -e "${RED_BG}${L_GRAY} > Failed to compile: \"$sketch\" for FQBN \"$fqbn\"${RESET}"
test_failed=true
failed_sketches+=("$sketch")
test_failed=true
failed_fqbn+=(" > $fqbn")
if [ "$fast_fail" = true ]; then
echo -e "> Terminating all compilation tests"
break 2 # Break from both loops
fi
if [ "$skip_example_after_single_fail" = true ]; then
echo -e "> Terminating compilations for sketch $sketch"
break 1 # Skip all FQBNs for this sketch
fi
else # if [[ $? -ne 0 ]]
echo -e "${GREEN_BG}${D_GRAY} > Compilation successful: $sketch${RESET}"
# TODO save successful test to skip them next time (maybe not)
fi # if [[ $? -ne 0 ]]
done # loop over all FQBNs
#if (( ${#failed_fqbn[@]} )); then
if [ -n "$failed_fqbn" ]; then
report+=("${RED_BG}${L_GRAY}Failed to compile: $sketch${RESET} for FQBNs\n${failed_fqbn[@]}")
else # if [ ${#failed_fqbn[@]} -eq 0 ]
report+=("${GREEN_BG}${D_GRAY}Compilation successful: $sketch${RESET}")
fi # if [ ${#failed_fqbn[@]} -eq 0 ]
done # for sketch in "${sketches_to_compile[@]}"
echo ""
########################################################################################################
echo "### Final check and reports"
for r in "${report[@]}"; do
echo -e "$r"
done
if [[ "$changes_made" = true || "$test_failed" = true ]]; then
echo -e "${RED_BG}${L_GRAY}Test failed - nothing was committed: Fix problems and commit again.${RESET}"
exit 1
else
echo -e "${GREEN_BG}${D_GRAY}Test succeeded: No changes were made and tests succeeded${RESET}"
#exit 1 # debug return - always fail
exit 0 # normal return
fi