-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-checkout
83 lines (63 loc) · 2.79 KB
/
post-checkout
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
#!/bin/bash
set -e
printf '\npost-checkout hook\n\n'
prevHEAD=$1
newHEAD=$2
checkoutType=$3
confPath=html/config.php
[[ $checkoutType == 1 ]] && checkoutType='branch' ||
checkoutType='file' ;
echo 'Checkout type: '$checkoutType
echo ' prev HEAD: '`git name-rev --name-only $prevHEAD`
echo ' new HEAD: '`git name-rev --name-only $newHEAD`
if [ $checkoutType == 'branch' ]
then
dbuser=$(grep username $confPath|cut -d\' -f6)
dbpass=$(grep password $confPath|cut -d\' -f6)
dbname=$(grep dbname $confPath|cut -d\' -f6)
tmperrorfile=/tmp/mysqlDbCheckErrors$RANDOM
currentBranch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
echo now you are in $currentBranch branch
echo dbusr: $dbuser dbpass: $dbpass dbname: $dbname
{ mysqlshow -u$dbuser -p$dbpass $dbname 2>$tmperrorfile
} && {
expression="/\| $dbname +\|\$/{s//replace/;h};\${x;/./{x;q0};x;q1}"
if (mysqlshow -u$dbuser -p$dbpass | sed -n -r "$expression")
then
echo Note: db exist, no need to create.
else
echo -e "\e[31mError: \e[39mSomething is wrong. can't find the database $dbname"
fi
} || {
dbSearchError=$(cat $tmperrorfile)
if [[ $dbSearchError == *"command not found"* ]]
then
echo Note: Checkout the branch inside the vagrant box \
so database can be switched/create
rm $tmperrorfile
elif [[ $dbSearchError == *"Unknown database"* ]]
then
echo -e "\e[33mWarning: \e[39mDatabase doesn't exist. \
attempting to clone the db.."
if [[ $currentBranch!=$(git name-rev --name-only $prevHEAD) ]]
#db dump and restore code goes here
then
#this dbpostfix is useless
dbpostfix=${dbname##*_}
#cut off the old postfix
db=${dbname%_$dbpostfix}
#new postfix is the current branch
newDb=$db_\$currentBranch
rm $tmperrorfile
#create the new database
mysql -u $dbuser -p$dbpass -e "create database $newDb"
#clone the db into created new database
mysqldump --opt -Q -u $dbuser -p$dbpass $dbname | mysql -u $dbuser -p$dbpass $newDb
echo $dbname to $newDb
else
echo -e "\e[31mTerminal Error:\e[39m] Previous and current \
branches are looks same. did you just merged?"
fi
fi
}
fi