-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-secure-push
executable file
·446 lines (373 loc) · 9.68 KB
/
git-secure-push
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
#!/bin/bash
# Update this variable value with your GPG Key
GPG_KEY=`git config user.signingkey`
GPG_KEY_EXISTS=$(gpg2 --list-key $GPG_KEY)
if [[ $? -ne 0 ]]
then
echo "GPG Key $GPG_KEY is unavailable in the current GPG keychain"
exit 4
fi
#************** Functions ***********************************
function print_usage {
echo "Usage: git secure-push [<branch> | <local_branch>:<remote_branch> | HEAD]"
echo ""
echo "Pushes the current state of a branch and adds a push entry to the RSL."
echo ""
echo "NOTE: Only pushes to the 'origin' remote."
echo "origin: `git remote get-url origin`"
}
#This function creates the rsl entries related to the push operation
function update_rsl_push {
commits=`git log @{u}.. --pretty=format:"%H" 2>/dev/null || git rev-list $BRANCH_TO_PUSH`
git checkout -q rsl
#Computing Hash for previous entries
RSL=$(find_last_entry_number)
#finding the last push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
#Caluculating the HASH
LAST_ENTRY=$(find_last_entry_number)
if [ -f $LAST_PUSH_ENTRY ]
then
for i in $(seq $LAST_PUSH_ENTRY $LAST_ENTRY)
do
cat $i >>hash_file.tmp
done
else
for i in $(seq 1 $LAST_ENTRY)
do
cat $i >>hash_file.tmp
done
fi
HASH=`cat hash_file.tmp|sha512sum | cut -d' ' -f1`
rm hash_file.tmp
#Updte rsl
echo "Related Commits" >rsl.tmp
echo $commits >>rsl.tmp
echo "Branch:"$REMOTE_BRANCH >>rsl.tmp
HEAD_HASH=`git rev-parse $BRANCH_TO_PUSH`
echo "HEAD":$HEAD_HASH >>rsl.tmp
echo "PREV_HASH:"$HASH >>rsl.tmp
}
function find_last_entry_number {
echo $(find . -maxdepth 1 -type f | grep -v 'rsl.tmp' | wc -l)
}
#This function creates the rsl entries related to the fetch operation
function update_rsl_fetch {
LAST_ENTRY=$(find_last_entry_number)
RSL=$(($LAST_ENTRY+1))
echo `od -A n -t d -N 4 /dev/urandom` >$RSL
git add $RSL
echo "Commiting fetch entry to RSL branch."
echo "<---- GPG signature required."
git commit -S -qm "Adding the fetch entry at RSL"
}
#This function intializes the rsl branch and creates rsl file intial entries
function rsl_init {
#Set init variabe
init=1
commits=`git rev-list $BRANCH_TO_PUSH`
git checkout -q --orphan rsl
git rm -qrf .
echo "Related Commits" >rsl.tmp
echo $commits >>rsl.tmp
echo "Branch:"$REMOTE_BRANCH >>rsl.tmp
HEAD_HASH=`git rev-parse $BRANCH_TO_PUSH`
echo "HEAD":$HEAD_HASH >>rsl.tmp
echo "PREV_HASH:"$HASH >>rsl.tmp
}
#This function fetches the newer version of rsl
function rsl_fetch {
git fetch -q origin rsl
git checkout -q rsl
RSL=$(find_last_entry_number)
#finding the last verified push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_VERIFIED_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
git merge -q
}
# This function will sign the rsl entries and commits the changes
function sign_rsl {
LAST_ENTRY=$(find_last_entry_number)
RSL=$(($LAST_ENTRY+1))
#Sign the rsl
echo "Clearsigning RSL entry"
echo "<---- GPG signature required"
cat rsl.tmp |gpg2 --default-key $GPG_KEY --clearsign >$RSL
#Remove temp file
rm rsl.tmp
# Commit the new RSL
git add $RSL
echo "Commiting push entry to RSL branch"
echo "<---- GPG signature required"
git commit -S -qm "Adding the push entry at RSL for $BRANCH_TO_PUSH"
}
#This function pushes the rsl branch to server
function push_rsl {
git push -q --set-upstream origin rsl 2>/dev/null
#Checking the status of RSL Push
status=$?
}
#This funtion pushes the branch to server
function push_branch {
git checkout -q $BRANCH_TO_PUSH
git push -q --set-upstream origin $BRANCH_TO_PUSH:$REMOTE_BRANCH
}
#This function verifies the rsl file
function rsl_verify {
git checkout -q rsl
if git diff $RSL_PREV_HEAD HEAD --stat|grep -q 'deletion'
then
echo "RSL file corrupted"
fi
#verify the signature on last enry
LAST_ENTRY=$(ls -lrt|wc -l)
#Computing Hash for previous entries
RSL=$(find_last_entry_number)
#Finding first push entry
for i in $(seq 1 $RSL)
do
if grep -q HEAD $i
then
#Update Last RSL push entry
FIRST_PUSH_ENTRY=$i
break
else
RSL=$(($i+1))
fi
done
RSL=$(find_last_entry_number)
#finding the last push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
echo "******* Verifying the sign on last rsl entry *******"
if [ -f $LAST_PUSH_ENTRY ]
then
gpg2 --default-key $GPG_KEY --verify $LAST_PUSH_ENTRY &> /dev/null
local status=$?
if [ $status -ne 0 ]
then
echo "Signature verification on last push entry is failed"
else
echo "Signature verification on last push entry is successful"
fi
else
echo "There is no RSL push entry exist before. No need of signature verification"
fi
echo "****************************************************"
echo "******* Verifying the hash on new rsl push entries *******"
#Hash Verification
VERIFY=1
while [ $VERIFY -eq 1 ]
do
RSL=$(($LAST_PUSH_ENTRY-1))
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY2=$RSL
break
else
RSL=$(($RSL-1))
fi
done
if [ -f $LAST_PUSH_ENTRY2 ]
then
for i in $(seq $LAST_PUSH_ENTRY2 $(($LAST_PUSH_ENTRY-1)))
do
cat $i >>hash_file.tmp
done
else
for i in $(seq 1 $(($LAST_PUSH_ENTRY-1)))
do
cat $i >>hash_file.tmp
done
fi
HASH_1=`cat $LAST_PUSH_ENTRY|grep 'PREV_HASH:'|cut -d':' -f2`
HASH_2=`cat hash_file.tmp|sha512sum|cut -d' ' -f1`
if [[ "$HASH_2" != "$HASH_1" && ! -z "$HASH_1" ]]
then
echo "Hash verfication on the following entry failed"
echo "HASH_1:"$HASH_1 " HASH_2":$HASH_2
echo `cat hash_file.tmp`
exit 1
else
echo "Hash verification successful "
fi
rm hash_file.tmp
if [[ $LAST_PUSH_ENTRY -eq $LAST_VERIFIED_PUSH_ENTRY || $LAST_PUSH_ENTRY -eq $FIRST_PUSH_ENTRY || -z $LAST_PUSH_ENTRY ]]
then
VERIFY=0
fi
LAST_PUSH_ENTRY=$LAST_PUSH_ENTRY2
LAST_PUSH_ENTRY2=0
done
}
function unstash_changes() {
if [ "$STASH_OUTPUT" != "No local changes to save" ]; then
POP_STASH="$(git stash pop)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$POP_STASH"
exit 9
fi
fi
}
function restore_original_state() {
CLEAR_INDEX="$(git reset --hard HEAD)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CLEAR_INDEX"
fi
CHECKOUT_ORIGINAL_OUTPUT="$(git checkout -q $PRIOR_BRANCH)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CHECKOUT_ORIGINAL_OUTPUT"
exit 8
fi
cd $WORKING_DIRECTORY
unstash_changes
}
#************************************************************************************
WORKING_DIRECTORY=$(pwd)
PRIOR_BRANCH="$(git symbolic-ref --short HEAD 2>&1)"
if [[ $? != 0 ]]; then
PRIOR_BRANCH="$(git rev-parse HEAD)"
fi
#Get Current Branch
while [ $# -ne 0 ]
do
case "$1" in
-h | -help )
print_usage
exit 0
;;
-* )
print_usage
exit -1
;;
HEAD )
BRANCH_TO_PUSH=$PRIOR_BRANCH
REMOTE_BRANCH=$PRIOR_BRANCH
shift
;;
HEAD:* )
BRANCH_TO_PUSH=$PRIOR_BRANCH
REMOTE_BRANCH=$(echo "$1" | cut -d ":" -f 2)
shift
;;
*:* )
BRANCH_TO_PUSH=$(echo "$1" | cut -d ":" -f 1)
REMOTE_BRANCH=$(echo "$1" | cut -d ":" -f 2)
shift
;;
* )
BRANCH_TO_PUSH=$1
REMOTE_BRANCH=$1
shift
;;
esac
done
if [[ -z $REMOTE_BRANCH ]]
then
REMOTE_BRANCH=$PRIOR_BRANCH
BRANCH_TO_PUSH=$PRIOR_BRANCH
fi
#check if we are pushing rsl branch
if [ "$REMOTE_BRANCH" == "rsl" ]
then
exit 0
fi
echo "Stashing any local changes"
STASH_OUTPUT="$(git -c commit.gpgSign=false stash save -a)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$STASH_OUTPUT"
exit 1
fi
cd $(git rev-parse --show-toplevel)
#Update RSL
if [[ $(git branch -a |grep 'remotes/origin/rsl') ]]
then
echo "Updating the rsl ..."
update_rsl_push
else
# RSL branch Intialization
echo "Intializing the rsl ..."
rsl_init
fi
#Signing the rsl
echo "Signing and Commiting the rsl ..."
sign_rsl
#Push RSL
echo "Pushing the rsl ... "
push_rsl
while [ $status -ne 0 ]
do
echo "rsl push failed !!"
echo "Fetching the rsl from server ..."
#reset the rsl branch
git reset --hard origin/rsl
rsl_fetch
rsl_verify
RSL=$(find_last_entry_number)
#finding the last push entry
while [ -f $RSL ]
do
if grep -q HEAD $RSL
then
#Update Last RSL push entry
LAST_PUSH_ENTRY=$RSL
break
else
RSL=$(($RSL-1))
fi
done
RSL_BRANCH_HEAD=`cat $LAST_PUSH_ENTRY |grep HEAD |tail -1|cut -d':' -f2`
#Verify the branch can be pushed to remote
if [[ $(git rev-list $BRANCH_TO_PUSH |grep $RSL_BRANCH_HEAD) ]]
then
echo "Updating the rsl with push entry"
update_rsl_push
echo "Signing the rsl"
sign_rsl
echo "Pushing the rsl"
push_rsl
else
echo $BRANCH_TO_BE_PUSHED "Can't be pushed to remote branch $REMOTE_BRANCH. Please fetch and merge the changes then retry the fetch"
restore_original_state
exit 1
fi
done
#Push current branch
echo "Pushing the $BRANCH_TO_PUSH to remote branch $REMOTE_BRANCH ..."
push_branch
restore_original_state