-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithooks-post-checkout
More file actions
executable file
·503 lines (474 loc) · 17.4 KB
/
githooks-post-checkout
File metadata and controls
executable file
·503 lines (474 loc) · 17.4 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/bash -l
# To install
# for dir in ../*/.git ; do echo $dir ; ln -vsf $( pwd )/githooks-post-checkout $dir/hooks/post-checkout ; done
#
# OR
#
# for dir in ../*/.git/hooks ; do \cp -v githooks-post-checkout ${dir}/post-checkout ; done
#
# OR
#
# mkdir -p ~/.git/hooks/ && ln -vsf $( pwd )/githooks-post-checkout ~/.git/hooks/post-checkout && git config --global core.hooksPath ~/.git/hooks/
# The hook is given three parameters: the ref of the previous HEAD, the
# ref of the new HEAD (which may or may not have changed), and a flag
# indicating whether the checkout was a branch checkout (changing
# branches, flag=1) or a file checkout (retrieving a file from the
# index, flag=0).
set -o pipefail
error()
{
echo -e '*\n *\n *\n *'
echo -e '*\n *\n *\n * GPG signature not verified\n *\n *\n*'
echo -e ' *\n *\n *\n*'
}
_run()
{
# Usage: _run: command
echo -en "\nRun: ${@} " 2>&1 | tee -a "${logfile}"
date 2>&1 | tee -a "${logfile}"
( time ${@} ) 2>&1 | tee -a "${logfile}"
}
# Only do these validations on a branch checkout, which includes `git
# checkout` with no parameters. https://git-scm.com/docs/githooks
if [[ $3 -eq 1 ]]
then
# Put the NIH folder for external projects in a common location
if false
then
if [ -d $HOME/dev ]
then
export NIH_CACHE_ROOT=$HOME/dev/nih_c
elif [ -d /c/dev ]
then
export NIH_CACHE_ROOT=/c/dev/nih_c
else
export NIH_CACHE_ROOT=$HOME/nih_c
fi
echo NIH_CACHE_ROOT: ${NIH_CACHE_ROOT}
fi
if [[ "${OS}" =~ "Windows" ]]
then
# compilers=( "msvc17" "msvc19" "msvc22" )
# vsgen="Visual Studio 16 2019"
# compilers=( "msvc19" )
vsgen="Visual Studio 17 2022"
compilers=( "msvc22" )
build_types=( "" )
unity_types=( "ON" "OFF" )
if false
then
pg='/C/Program Files/PostgreSQL'
if [[ -d "${pg}" ]]
then
# Initialize the entry with the top dir, then look for
# numbered children. Use the first one found.
# (Don't mix versions.)
postgres="-DPostgreSQL_ROOT=${pg}"
for sub in "${pg}"/[0-9]*
do
if [[ -d "${sub}" ]]
then
echo "Found PostgreSQL at ${sub}"
postgres="-DPostgreSQL_ROOT=${sub}"
break
fi
done
fi
unset pg
else
postgres="-Dnopostgres=TRUE"
fi
for cas in '/C/Dev/cassandra'*
do
if [[ -d "${cas}" && -d "${cas}/lib" ]]
then
echo "Found cassandra at ${cas}"
prefix="${cas}"
break
fi
done
unset cas
[[ -z "${prefix}" ]] || \
cmake_prefix_path="-DCMAKE_PREFIX_PATH=${prefix}"
else
if [[ "$( type -p gcc )" -ef "$( type -p clang )" ]]
then
compilers=( "clang" )
else
compilers=( "gcc" "clang" )
fi
build_types=( "Debug" "Release" )
unity_types=( "" )
fi
maindir="$(pwd)"
builddir="$(dirname $(pwd))/build/$(basename $(pwd))"
logfile="${builddir}/checkout.log"
output=$( mkdir -pv "${builddir}" )
echo "${output}" | tee $logfile
if [[ -f CMakeLists.txt ]] && \
[[ ! ( "$(pwd)" =~ "nih_c" || "$(pwd)" =~ "build/cmake" || \
"$(pwd)" =~ "_deps" || "$(pwd)" =~ ".conan" ) ]]
then
tempfile="$( mktemp --tmpdir=${builddir} )"
echo "Building in ${builddir}" | tee -a "${logfile}"
echo "Log is in ${logfile}" | tee -a "${logfile}"
projectname=$( grep -i "^ *project(" CMakeLists.txt | \
sed 's/.*project(// ; s/).*//')
echo -e "\n\n\n\n\nBuilding cmake ${projectname} in $(pwd)\n\n\n\n\n" \
| tee -a $logfile
projectparams=()
case "${projectname}" in
xrpl)
# I like asserts
projectparams+=( "-Dassert=true" )
projectparams+=( "-Dwerr=ON" )
projectparams+=( "-Dxrpld=ON" )
projectparams+=( "-Dtests=ON" )
# projectparams+=( "-Dvalidator_keys=ON" )
;;
esac
unset conan
for conanfile in conanfile.*
do
if [[ -f "${conanfile}" ]]
then
conan=1
for exp in external/*/conanfile.*
do
if [[ -f "${exp}" ]]
then
exp=$( dirname "${exp}" )
name=$( basename "${exp}" )
extra=$( grep "${name}/[0-9.]" "${conanfile}" | \
sed "s/.*'${name}\/\([0-9.]*\)'.*/\1/" | head -1 )
search="${name}/${extra}"
cmd=( conan list --out-file "$tempfile" "${search}" )
echo "Run: ${cmd[@]}" 2>&1 | tee -a $logfile
if "${cmd[@]}" 2>&1 | tee -a $logfile && \
cat "${tempfile}" | tee -a $logfile && \
[[ $( grep -c ERROR "${tempfile}" ) == 0 ]]
then
echo "Run: INFO: ${search} already exported" 2>&1 | tee -a $logfile
continue
fi
cmd=( conan export --version "${extra}" "${exp}" )
echo "Run: ${cmd[@]}" 2>&1 | tee -a $logfile
"${cmd[@]}" 2>&1 | tee -a $logfile
fi
done
conan_profile_list=$( conan profile list -vquiet )
fi
done
for repo in . $( git config --file .gitmodules --get-regexp path | \
cut -d\ -f2 )
do
repobuild="${builddir}/${repo}"
if [[ ! -d ${repo} || ! -f ${repo}/CMakeLists.txt || \
( -e "${repobuild}" && ! -d "${repobuild}" ) ]]
then
echo "Not running cmake in ${repo}" | tee -a $logfile >&2
continue
fi
echo -e "\nBuilding ${repo} in ${repobuild}\n" | tee -a "${logfile}"
# Starting in original dir
pushd ${repo} > >( tee -a $logfile )
repo=$(pwd)
echo Compilers: ${compilers[@]} | tee -a $logfile
echo Build types: ${build_types[@]} | tee -a $logfile
echo Unity types: ${unity_types[@]} | tee -a $logfile
conan_profiles=()
for c in "${compilers[@]}"
do
for debug in "${build_types[@]}"
do
buildparam=()
if [[ "${conan}" ]]
then
tooldir="conan"
for search in "${c}${debug}" "${c}Release" "${c}"
do
if echo "${conan_profile_list}" | grep -qwx "${search}"
then
tooldir+=".${search}"
break
fi
done
if [[ -z "${debug}" ]]
then
conan_types=( Release Debug )
else
conan_types=( ${debug:-Release} )
fi
for conanbuild in "${conan_types[@]}"
do
profile=()
unset tracker
for search in "${c}${conanbuild}" "${c}Release" "${c}" "default"
do
if echo "${conan_profile_list}" | grep -qwx "${search}"
then
tracker="${tooldir}${conanbuild}${search}"
profile+=( -pr "${search}" )
break
fi
done
if [[ "${tracker}" == "" ]] || \
! printf '%s\0' "${conan_profiles[@]}" \
| grep -F -x -z -q -- "${tracker}"
then
# Starting in $repo
pushd "${repobuild}" > >( tee -a $logfile )
markerfilename="conan_toolchain.marker.${conanbuild}"
markerfile=$( find "${tooldir}" -iname "${markerfilename}" )
toolchainfile=$( find "${tooldir}" -iname \
"conan_toolchain.cmake" )
if [[ -f "${toolchainfile}" && -f "${markerfile}" ]]
then
echo "Run: INFO: Toolchain file is ${toolchainfile}, and" \
"build marker file is ${markerfile}" \
| tee -a $logfile
echo "Run: INFO: Skipping conan install. Toolchain files" \
"exist. ${toolchainfile}, ${markerfile}" \
| tee -a $logfile
echo "Run: INFO: If dependencies change, wipe build folder" \
"and start over." \
| tee -a $logfile
else
_run mkdir -pv "${tooldir}"
# Starting in $repobuild
pushd "${tooldir}" > >( tee -a $logfile )
cmd=( conan install "${repo}"
--output-folder .
--build missing
--settings build_type="${conanbuild}"
"${profile[@]}" )
echo "Run: ${cmd[@]}" 2>&1 | tee -a $logfile
"${cmd[@]}" 2>&1 | tee -a $logfile 2>&1 | \
awk '{printf "."}'
echo | tee -a $logfile
if [[ "${tracker}" != "" ]]
then
conan_profiles+=( "${tracker}" )
fi
# back to $repobuild
popd > >( tee -a $logfile )
toolchainfile=$( find "${tooldir}" -iname \
"conan_toolchain.cmake" )
echo "Run: INFO: Toolchain file is ${toolchainfile}" \
| tee -a $logfile
cmd=(
touch
"$( dirname "${toolchainfile}" )/${markerfilename}"
)
echo "Run: ${cmd[@]}" 2>&1 | tee -a $logfile
"${cmd[@]}" 2>&1 | tee -a $logfile 2>&1
fi
toolchainfile="${repobuild}/${toolchainfile}"
# echo "Pwd: $(pwd), Toolchainfile: $toolchainfile, Markerfile: $markerfile" | tee -a $logfile
# ls -l $toolchainfile $markerfile | tee -a $logfile
# back to $repo
popd > >( tee -a $logfile )
if [[ ! -f "${toolchainfile}" ]]
then
echo "Run: INFO: Conan did not make a toolchain file" | tee -a $logfile
break 3
fi
fi
done
buildparam+=( "-DCMAKE_TOOLCHAIN_FILE=${toolchainfile}" )
buildparam+=( "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW" )
fi
if [ ${c} == "msvc22" ]
then
buildparam+=( "-G${cmake_win_gen:=${vsgen}}" )
buildparam+=( "-Ax64" )
buildparam+=( "-DCMAKE_GENERATOR_TOOLSET=host=x64" )
elif [ ${c} == "msvc19" ]
then
buildparam+=( "-G${cmake_win_gen:=${vsgen}}" )
buildparam+=( "-Ax64" )
if [[ "${vsgen}" == "Visual Studio 17 2022" ]]
then
buildparam+=( "-DCMAKE_GENERATOR_TOOLSET=v142,host=x64" )
else
buildparam+=( "-DCMAKE_GENERATOR_TOOLSET=host=x64" )
fi
elif [ ${c} == "msvc17" ]
then
buildparam+=( "-G${cmake_win_gen:=${vsgen}}" )
buildparam+=( "-Ax64" )
buildparam+=( "-DCMAKE_GENERATOR_TOOLSET=v141,host=x64" )
elif [ ${c} == "msvc" ]
then
buildparam+=( "-G${cmake_win_gen:=${vsgen}}" )
buildparam+=( "-Ax64" )
buildparam+=( "-DCMAKE_GENERATOR_TOOLSET=host=x64" )
else
cxx=${c/gcc/g++}
cxx=${cxx/clang/clang++}
buildparam+=( "-DCMAKE_C_COMPILER=${c}" )
buildparam+=( "-DCMAKE_CXX_COMPILER=${cxx}" )
fi
for unity in "${unity_types[@]}"
do
# echo "c: $c, debug: $debug, unity: $unity" | tee -a $logfile
extra=( "${buildparam[@]}" )
if [ -n "$( type -p ccache )" ]
then
extra+=( "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache" )
fi
if [ -n "$( type -p clang-tidy )" ]
then
# e.g. clang-tidy --checks="-*,misc-include-cleaner" -fix -p ../current/cmake/clang.Debug.OFF/ $( git findfile Lending )
extra+=( "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" )
fi
[[ -z "${postgres}" ]] || \
extra+=( "${postgres}" )
[[ -z "${cmake_prefix_path}" ]] || \
extra+=( "${cmake_prefix_path}" )
if [ -z "${debug}" -o "${debug}" == "unified" ]
then
target=${c}
else
target=${c}.${debug}
extra+=( "-DCMAKE_BUILD_TYPE=${debug}" )
fi
if [[ "${unity}" != "" ]]
then
# echo -n "Target before: \"${target}\"" | tee -a $logfile
target+=.${unity}
# echo ", Target after: \"${target}\"" | tee -a $logfile
extra+=( "-Dunity=${unity}" )
fi
if [ -n "${BOOST_ROOT}" ]
then
extra+=( "-DBOOST_ROOT=$BOOST_ROOT" )
fi
if [ -n "${OPENSSL_ROOT}" ]
then
extra+=( "-DOPENSSL_ROOT=$OPENSSL_ROOT" )
fi
if [[ -f "${repobuild}/cmake/${target}/CMakeCache.txt" ]]
then
echo "Run: INFO: Skipping cmake call in ${target}" | tee -a $logfile
echo " CMakeCache.txt file exists" | tee -a $logfile
continue
fi
mkdir -pv ${repobuild}/cmake/${target} | tee -a $logfile
# Starting in $repo
pushd ${repobuild}/cmake/${target} > >( tee -a $logfile )
echo -e "\n\n\nTarget: $target" | tee -a $logfile
set -x
pwd | tee -a $logfile
# echo "Toolchainfile: ${toolchainfile}" | tee -a $logfile
# ls -l "${toolchainfile}" | tee -a $logfile
extra+=( "${projectparams[@]}" )
extra+=( "${repo}" )
cmd=( cmake "${extra[@]}" )
echo "Run: ${cmd[@]}" 2>&1 | tee -a $logfile
"${cmd[@]}" 2>&1 | tee -a $logfile 2>&1 | awk '{printf "."}'
echo | tee -a $logfile
set +x
# Back to $repo
popd > >( tee -a $logfile )
done
done
done
preset="${repo}/CMakeUserPresets.json"
if [[ -n "${conan}" && -f "${preset}" ]] &&
grep -q -e "${tooldir}..\?build/generators..\?CMakePresets.json" "${preset}" \
-e "windows-base"
then
_run mv -vf "${preset}" "${preset}.bad"
fi
# Back to original dir
popd > >( tee -a $logfile )
done
if false
then
# Update any custom / non-build CMake folders
cmd=( find . \( -path '*/build/cmake' -o -path '*/.nih_c' \) -prune
-false -o -name CMakeCache.txt -fprintf /dev/stderr
"Building cmake in %p\n" -print0 )
echo "Run: ${cmd[@]}" | tee -a ${logfile}
"${cmd[@]}" 2> >( tee -a ${logfile} 2>&1 ) | \
xargs -r0 -n1 -P4 cmake 2>&1 | tee -a $logfile 2>&1 | \
awk '{printf "."}' && echo | tee -a "${logfile}"
fi
# Reformat on checkout, if the current branch has instructions
g=$( git rev-parse --git-dir )
b=$( git rev-parse --abbrev-ref HEAD )
if [[ ! -f "diff.${b//\//.}" && ! -f "${g}/MERGE_HEAD"
&& ! -d "${g}/rebase-merge" && ! -d "${g}/rebase-apply" ]]
then
echo -n ".pre-commit-config.yaml "
[[ -e .pre-commit-config.yaml ]] && echo found || echo not found
hookspath=$( git config core.hookspath )
hookspath="${hookspath/#\~\//$HOME/}"
echo -n ${hookspath}/pre-commit " "
[[ -e "${hookspath}/pre-commit" ]] && echo found || echo not found
echo -n "pre-commit type "
type -t pre-commit
if [[ -e .pre-commit-config.yaml && \
-e "${hookspath}/pre-commit" ]] && \
type -t pre-commit 2>&1 >/dev/null
then
# Assume that if there's a pre-commit hook available, it's
# configured
echo "pre-commit formatting: $@" | tee -a "${logfile}"
pre-commit run -a
elif [[ -f .clang-format ]] && type -t git-clang-format >& /dev/null \
&& [[ ! -f "diff.${b//\//.}" && ! -f "${g}/MERGE_HEAD"
&& ! -d "${g}/rebase-merge" && ! -d "${g}/rebase-apply" ]]
then
echo "git-clang-formatting: $@" | tee -a "${logfile}"
if type -t gh 2>&1 >/dev/null
then
branches=( "$2" $( gh base ) )
elif git branch -r | grep -q "upstream/develop"
then
branches=( "$2" "upstream/develop" )
else
branches=( "$1" "$2" )
fi
echo -e "Looking for merge base for ${branches[@]}"
compare=$( git merge-base --octopus "${branches[@]}" )
echo -e "\nFormatting files changed since $compare" | tee -a "${logfile}"
# In Windows confirm the correct path to your LLVM install. But note
# that there are incompatibilities with the Windows version so you
# may not want to use this hook in Windows.
llvmpath="/c/Program Files/LLVM/bin/"
if [ -d "${llvmpath}" ]
then
PATH="${llvmpath}:${PATH}"
fi
# find src/ripple src/test -type f \! -name 'AccountTxPaging.cpp' \( \
# -name '*.cpp' -o -name '*.h' -o -name '*.ipp' \) -print0 \
# | xargs -0 clang-format -i
git-clang-format --diffstat --extensions c,cpp,h,cxx,ipp $compare
else
echo -e "\nNot formatting code" | tee -a "${logfile}"
fi
else
echo -e "\nNot formatting code" | tee -a "${logfile}"
fi
rm -fv "${tempfile}" 2>&1 | tee -a "${logfile}"
fi
if [ $# -gt 1 ]
then
git verify-commit ${2} || error
fi
if git branch -r | grep -q upstream/develop
then
# This format is equivalent to --oneline with the addition of the
# signature status "%G?"
format='%C(auto)%h %G?%d %s'
git log --format="${format}" -1 ..upstream/develop
fi
b="$(git symbolic-ref HEAD 2>/dev/null)"
b=${b##refs/heads/};
if [[ "$b" != "" ]]
then
echo -e "\nGit branch ready: $b\n" | tee -a "${logfile}"
fi
fi