/
git_push.sh
executable file
·106 lines (87 loc) · 2.42 KB
/
git_push.sh
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
#!/bin/bash
#for testing
terminal="/dev/null"
# terminal="/dev/pts/10"
# echo "-------------" > $terminal
# # export SSH_AUTH_SOCK=/run/user/1000/keyring-r9YizP/ssh
#
# source $HOME/.bash_profile
#
# echo $HOME > $terminal
# printenv > $terminal
# echo "-------------" > $terminal
#store file locations
BASEDIR=$(dirname $0)
current_dir=$(pwd)
if [ $BASEDIR = '.' ]
then
BASEDIR="$current_dir"
fi
echo $BASEDIR > $terminal
QUEUE=$BASEDIR/queue.csv
ERRORQUEUE=$BASEDIR/error_queue.csv
LOG=$BASEDIR/log.csv
#Retrive item from queue
LINE=$(sed -n '1p' $QUEUE) #queue.csv)
sed -i '1d' $QUEUE
#Parse item into components
set -- "$LINE"
IFS=","; declare -a Array=($*)
#Set variables. Note: Itemtype is either a directory or file
ITEMTYPE="${Array[0]}"
DIRECTORY="${Array[1]}"
NAME="${Array[2]}"
COMMENT="${Array[3]}"
#git status of remote
GITSTATUS=$(cd $DIRECTORY && git pull 2>&1)
echo $GITSTATUS > $terminal
#make sure there are no merge conflicts
if echo "$GITSTATUS" | grep -q "merge issue";
then
PROCEED=0
else
PROCEED=1
echo "git_off: no merge issues found!" > $terminal
fi
#make the commits if possible
if [ "$ITEMTYPE" == "directory" ] && [ "$PROCEED" == 1 ]; then
COMMIT=$(cd $DIRECTORY && git add -A && git commit -m $COMMENT 2>&1)
elif [ "$ITEMTYPE" == "file" ] && [ "$PROCEED" == 1 ]; then
COMMIT=$(cd $DIRECTORY && git add $NAME && git commit -m $COMMENT 2>&1)
fi
echo $COMMIT > $terminal
# check the commit status
COMMITPATTERN1="nothing to commit"
if echo "$COMMIT" | grep -q "$COMMITPATTERN1";
then
PROCEED=0
else
PROCEED=1
echo "git_off: no commit issues found!" > $terminal
fi
#if no errors, push to remote
if [ "$PROCEED" == 1 ]; then
PUSH=$(cd $DIRECTORY && git push --progress 2>&1)
echo $PUSH > $terminal
#check push status
PUSHPATTERN1="Writing objects: 100%"
# PATTERN2="Total" #other patterns that may be helpful
# PATTERN3="done."
if echo "$PUSH" | grep -q "$PUSHPATTERN1";
then
PROCEED=1
echo "git_off: no push issues found!" > $terminal
else
PROCEED=0
fi
fi
#get time
TIME=$(date +"%m-%d-%Y %r")
# log status
if [ "$PROCEED" == 1 ]; then
echo "$TIME, SUCCESS, $ITEMTYPE, $DIRECTORY, $NAME, $COMMENT" >> $LOG
echo "git_off: SUCCESS in pushing to remote repo" > $terminal
else
echo "$TIME, $ITEMTYPE, $DIRECTORY, $NAME, $COMMENT, --PROGRAM OUTPUT: $GITSTATUS, $COMMIT, $PUSH" >> $ERRORQUEUE
# echo "$TIME, ERROR, $ITEMTYPE, $DIRECTORY, $NAME, $COMMENT" >> $LOG
fi