-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpre-commit
executable file
·260 lines (255 loc) · 9.87 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
255
256
257
258
259
260
#!/usr/bin/env tcsh
#
#################################################################
# #
# Copyright (c) 2020-2025 YottaDB LLC and/or its subsidiaries. #
# All rights reserved. #
# #
# This source code contains the intellectual property #
# of its copyright holder(s), and is made available #
# under a license. If you do not know the terms of #
# the license, please stop and do not read further. #
# #
#################################################################
#
# Pre-commit hook that does many things (described in comments below)
#
set filelist = `git diff --name-only HEAD`
@ exitstatus = 0
# Set gtm_dist to $ydb_dist if it exists; otherwise, if gtm_dist is not already set, to one of the following, in order of highest precedence:
# $gtm_dist/$gtm_curpro/pro
# pkg-config --variable=prefix yottadb
if ( $?ydb_dist ) then
setenv gtm_dist "$ydb_dist"
endif
if ( ! $?gtm_dist && $?gtm_root && $?gtm_curpro ) then
setenv gtm_dist $gtm_root/$gtm_curpro/pro
endif
if ( ! $?gtm_dist && `where pkg-config` != '' ) then
setenv gtm_dist_tmp `pkg-config --variable=prefix yottadb --silence-errors`
if ("" != "$gtm_dist_tmp") then
setenv gtm_dist $gtm_dist_tmp
endif
endif
if ( ! $?gtm_dist && -f build/yottadb ) then
setenv gtm_dist `realpath build`
ln -sf yottadb build/mumps
endif
# -----------------------------------------------------------------
# 1) Regenerates auto-generated files/sections-of-files if needed
# -----------------------------------------------------------------
#
foreach file ($filelist)
if ($file == "sr_unix/ttt.txt") then
# Regenerate ttt.c. For now sr_x86_64 and sr_armv7l will have the same ttt.c but that might not be true in the future so
# have this script go through both directories always (instead of just copying the sr_x86_64/ttt.c onto sr_armv7l/ttt.c).
foreach arch (sr_x86_64 sr_armv7l sr_aarch64)
if (! -e $arch) then
# In case sr_armv7l directory does not exist (possible in a prior git branch), skip.
continue
endif
cd $arch
# Switch to gtm_curpro version
setenv gtm_tools ../sr_port # needed for sr_port/copyright.txt to be used
mkdir tmpobj
unsetenv ydb_routines # In case it is set in parent shell, do not let that interfere here
setenv gtmroutines "tmpobj(../sr_unix ../sr_port)"
$gtm_dist/mumps -run tttgen ../sr_unix/ttt.txt ../sr_port/opcode_def.h ../sr_port/vxi.h >& tmpobj/tttgen.out
@ status1 = $status
cd .. # Need this cd for below "git add" to work inside a pre-commit hook
if ($status1) then
echo "TTTGEN-E-FAIL : Below command failed"
echo "$gtm_dist/mumps -run tttgen ../sr_unix/ttt.txt ../sr_port/opcode_def.h ../sr_port/vxi.h >& tmpobj/tttgen.out"
echo "Output follows"
cat $arch/tmpobj/tttgen.out
@ exitstatus = 1
else
git add $arch/ttt.c
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed : git add $arch/ttt.c"
endif
endif
rm -rf $arch/tmpobj # remove .o files generated in tmpobj
end
endif
if ($file:e == "msg") then
# Regenerate one of sr_port/{merrors_ctl.c,cmierrors_ctl.c,gdeerrors_ctl.c,cmerrors_ctl.c}
foreach arch (sr_port)
cd $arch
# Switch to gtm_curpro version
setenv gtm_tools ../sr_port # needed for sr_port/copyright.txt to be used
mkdir tmpobj
unsetenv ydb_routines # In case it is set in parent shell, do not let that interfere here
setenv gtmroutines "tmpobj(../sr_port)"
$gtm_dist/mumps -run msg ../$file
@ status1 = $status
cd .. # Need this cd for below "git add" to work inside a pre-commit hook
if ($status1) then
@ exitstatus = 1
echo "MSGGEN-E-FAIL : Command failed with status $status1 : $gtm_dist/mumps -run msg ../$file"
else
set basename = $file:t:r
git add $arch/${basename}_ctl.c
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed : git add $arch/${basename}_ctl.c"
endif
#
# If the basefile is ydberrrors then we want to create ydberrors.h instead of
# ydbydberrors.h
if ("ydberrors" == "$basename") then
set ydbfn = "ydberrors"
else
set ydbfn = "ydb${basename}"
endif
git add $arch/${ydbfn}.h
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed : git add $arch/${basename}.h"
endif
if ("merrors" == "$basename") then
#
# For merrors.msg, msg.m creates 2 additional files that need to be added:
# 1. merrors_ansi.h
# 2. libydberrors.h
#
set merrfiles = "${basename}_ansi.h libydberrors.h"
else if ("ydberrors" == "$basename") then
# For ydberrors.msg, msg.m creates 1 additional file that needs to be added:
# 1. libydberrors2.h
set merrfiles = "libydberrors2.h"
else
set merrfiles = ""
endif
foreach merrfile ($merrfiles)
git add $arch/$merrfile
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed : git add $arch/$merrfile"
endif
end
endif
rm -rf $arch/tmpobj # remove .o files generated in tmpobj
end
endif
if ($file == "sr_linux/release_name.h") then
# Ensure YDB_RELEASE in sr_unix/libyottadb.h is in sync with YDB_ZYRELEASE in sr_linux/release_name.h
set ydbzyrelease = `awk '$2 == "YDB_ZYRELEASE" {print $3}' $file`
# ydbzyrelease is of the form "r1.23". Convert it to 123 so it can be used in YDB_RELEASE.
set ydbrelease = `echo $ydbzyrelease | awk -F'[".r]' '{printf "%s%s\n", $3,$4}'`
set file2 = "sr_unix/libyottadb.h"
set actual = `awk '($1 == "#define") && ($2 == "YDB_RELEASE") { print $3 }' $file2`
if ("$ydbrelease" != "$actual") then
set echo
set verbose
perl -p -i -e 's/^(#define\t\t*YDB_RELEASE\t)\t*'$actual'/$1\t'$ydbrelease'/g' $file2
if ($status) then
@ exitstatus = 1
echo "PERL-E-FAIL : Command failed : perl substitution for YDB_RELEASE in $file2"
endif
unset echo
unset verbose
git add $file2
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed : git add $file2"
endif
endif
endif
end
# -----------------------------------------------------------------
# 2) Enforces YottaDB Copyright in changed modules
# -----------------------------------------------------------------
set filelist = `git diff --name-only HEAD` # recompute "filelist" in case Step (1) had regenerated new files
set fixlist = ""
set curyear = `date +%Y`
foreach file ($filelist)
ci/needs_copyright.sh $file
if ($status != 0) then
continue
endif
# Optimization: don't run the python script unless necessary
grep 'Copyright (c) .*'$curyear' YottaDB LLC' $file >& /dev/null
if ($status == 0) then
continue
endif
# Try to automatically add a copyright to the file
set tempfile = `mktemp`
ci/copyright.py $file > $tempfile
set copyright_status = $status
if ($copyright_status == 1) then
echo "note: automatically committing copyright updates to $file"
# Preserve the original file permissions
chmod --reference=$file $tempfile
mv $tempfile $file
git add $file
else if ($copyright_status != 0) then
set fixlist = "$fixlist $file"
endif
end
if ("" != "$fixlist") then
echo " --> Hook $0 returned non-zero status"
echo " --> Below files are missing YottaDB Copyright notice and/or current year $curyear. Fix and retry commit"
foreach file ($fixlist)
echo " $file"
end
@ exitstatus = 1
endif
# -----------------------------------------------------------------
# 3) Remove trailing white space
# -----------------------------------------------------------------
set nowhitespace_extensions = "patch" # List of extensions that cannot have trailing white space removed.
set filelist = `git diff --name-only HEAD` # recompute "filelist" in case Step (1) had regenerated new files
foreach file ($filelist)
if (! -e $file) then
# If file is being deleted as part of this commit, skip whitespace conversion on it
continue
endif
if ("" != "$nowhitespace_extensions") then
set extension = $file:e
set skip = 0
foreach ext ($nowhitespace_extensions)
if ($ext == $extension) then
set skip = 1
break
endif
end
if ($skip) then
continue
endif
endif
if ("sr_port/copyright.txt" == $file) then
# Skip this special file
continue
endif
grep -q '[ ][ ]*$' $file
if ! ($status) then
echo $file
sed -i 's/[ ][ ]*$//' $file
git add $file
if ($status) then
@ exitstatus = 1
echo "GITADD-E-FAIL : Command failed after removing trailing whitespace : git add $file"
endif
endif
end
# -----------------------------------------------------------------
# 4) Alerts if YDBEncrypt needs to be updated
# -----------------------------------------------------------------
set enclist = "sr_unix/Makefile.mk sr_unix/encrypt_sign_db_key.sh sr_unix/gen_keypair.sh sr_unix/gen_sym_hash.sh"
set enclist = "$enclist sr_unix/gen_sym_key.sh sr_unix/gtm_tls_impl.c sr_unix/gtm_tls_impl.h sr_unix/gtmcrypt_dbk_ref.c"
set enclist = "$enclist sr_unix/gtmcrypt_dbk_ref.h sr_unix/gtmcrypt_pk_ref.c sr_unix/gtmcrypt_pk_ref.h sr_unix/gtmcrypt_ref.c"
set enclist = "$enclist sr_unix/gtmcrypt_ref.h sr_unix/gtmcrypt_sym_ref.c sr_unix/gtmcrypt_sym_ref.h sr_unix/gtmcrypt_util.c"
set enclist = "$enclist sr_unix/gtmcrypt_util.h sr_unix/import_and_sign_key.sh sr_unix/maskpass.c sr_unix/pinentry-gtm.sh"
set enclist = "$enclist sr_unix/pinentry.m sr_unix/show_install_config.sh sr_port/minimal_gbldefs.c sr_port/ydb_getenv.c"
set enclist = "$enclist sr_port/ydb_getenv.h sr_port/ydb_logicals.h sr_port/ydb_logicals_tab.h sr_unix/ydb_tls_interface.h"
set enclist = "$enclist sr_unix/ydbcrypt_interface.h"
foreach file ($filelist)
foreach encfile ($enclist)
if ("$file" == "$encfile") then
echo "$file modified. Need to update equivalent file in YDBEncrypt accordingly."
endif
end
end
exit $exitstatus