-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_tag.sh
More file actions
executable file
·181 lines (149 loc) · 3.74 KB
/
fetch_tag.sh
File metadata and controls
executable file
·181 lines (149 loc) · 3.74 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
#!/bin/bash
DATE=`date +%Y%m%d%H%M`
SCRIPT_PATH="`dirname \"$0\"`"
SCRIPT_PATH="`( cd \"$SCRIPT_PATH\" && pwd )`"
# Referencing undefined variables (which default to "")
set -o nounset
# Don't ignore failed commands
set -o errexit
# This setting prevents errors in a pipeline from being masked
set -o pipefail
# Strict IFS for loops with spaces
IFS=$'\n\t'
COLOR_OFF="\e[0;39m"
RED="\e[0;31m"
GREEN="\e[1;32m"
YELLOW="\e[0;33m"
MAGENTA="\e[0;35m"
CYAN="\e[0;36m"
DARK_GRAY="\e[90m"
BLINK="\e[5m"
function debug()
{
echo -e "${DARK_GRAY}${1}${COLOR_OFF}";
}
function error()
{
echo -e "${RED}${1}${COLOR_OFF}"
}
function notice()
{
echo -e "${YELLOW}${1}${COLOR_OFF}"
}
function success()
{
echo -e "${GREEN}${1}${COLOR_OFF}"
}
function blink()
{
echo -e "${BLINK}${1}${COLOR_OFF}"
}
if [[ -z "${1+present}" ]]; then
error "Missing argument, specify tag you want to checkout"
exit;
fi
TAG=$1
RESTORE=false
if [[ $1 == "restore" ]]; then
if [[ -z "${2+present}" ]]; then
error "Missing argument, specify tag you want to checkout"
exit;
fi
TAG=$2
RESTORE=true
fi
if [[ $RESTORE == true ]]; then
notice "This will checkout tag $TAG, and restore database from info-file $SCRIPT_PATH/$TAG.info"
if [[ ! -f $SCRIPT_PATH/$TAG.info ]]; then
error "No info file was found for the tag, no database backup info available. The specified tag will still be checked out."
else
BACKUP_PATH=`cat $SCRIPT_PATH/$TAG.info`
debug "Path to database backup: $BACKUP_PATH"
fi
fi
# A guess of where document root can be, relative to the script
# pwd.
PRE=".
..
./public_html
../public_html"
CURRENT_DIR=`pwd`
#
# Establish document root for the project.
#
debug "Establish document root."
for i in $PRE; do
TEST_DIR=$(readlink -f $CURRENT_DIR/$i)
if [[ -d $TEST_DIR ]]; then
RES=`cd $TEST_DIR; drush status drupal-version`
if [[ $RES != "" ]]; then
DOCUMENT_ROOT=$TEST_DIR
break
fi
fi
done
if [[ $DOCUMENT_ROOT == "" ]]; then
error "Could not establish document root"
exit;
fi
debug "Seems to be '$DOCUMENT_ROOT'."
#
# Check if repo is clean.
#
debug "Check status of repo."
GIT_STATUS=$(cd $DOCUMENT_ROOT; git diff --shortstat 2> /dev/null | tail -n1)
if [[ $GIT_STATUS != "" ]]; then
error "There are uncommitted changes in the repo, clean up before proceeding."
exit;
fi
debug "Repo status seems alright."
#
# Check if the tag is available.
#
CONTINUE=
for available in `cd $DOCUMENT_ROOT; git tag`; do
if [[ $available == $TAG ]]; then
CONTINUE=y
fi
done
if [ "$CONTINUE" != "y" ]; then
notice "The specified tag is not avaialble"
exit
fi
CURRENT_TAG=`cd $DOCUMENT_ROOT; git describe --tags`
if [[ $TAG == $CURRENT_TAG ]]; then
notice "The specified tag is already chekced out"
exit;
fi
notice "Current tag is $CURRENT_TAG, this will change to $TAG, continue (y/N)"
read CONTINUE
if [ "$CONTINUE" != "y" ]; then
exit
fi
if [[ $RESTORE != true ]]; then
debug "Backup database."
cd $DOCUMENT_ROOT; git fetch --tags
cd $DOCUMENT_ROOT; drush sql-dump | gzip > $HOME/backup_$DATE.sql.gz
echo "Created database backup $HOME/backup_$DATE.sql.gz"
echo "path saved in: $SCRIPT_PATH/$CURRENT_TAG.info"
# Save info about what db was used with the current tag.
echo "$HOME/backup_$DATE.sql.gz" > $SCRIPT_PATH/$CURRENT_TAG.info
fi
RES=`cd $DOCUMENT_ROOT; git checkout --quiet $TAG`
if [[ $RESTORE == true ]]; then
echo "$DATE - restore: $TAG" >> $SCRIPT_PATH/fetch_tag.log
else
echo "$DATE - checkout: $TAG" >> $SCRIPT_PATH/fetch_tag.log
fi
if [[ $RES != "" ]]; then
echo $RES
else
success "Checkout successful"
fi
if [[ $RESTORE == true ]]; then
debug "Restore database from backup."
if [[ -z "${BACKUP_PATH+present}" ]]; then
exit
fi
cd $DOCUMENT_ROOT; gunzip -c $BACKUP_PATH | drush sql-cli
fi