-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-checkout
94 lines (75 loc) · 3.76 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
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Variables
MYSQL_USER=$(grep -irs "'username' => '" sites/default/settings.php | awk '{print $3}'| tail -1 | tr -d "',")
MYSQL_PW=$(grep -irs "'password' => '" sites/default/settings.php | awk '{print $3}'| tail -1 | tr -d "',")
DB_NAME=$(grep -irs "'database' => '" sites/default/settings.php | awk '{print $3}'| tail -1 | tr -d "',")
#CURRENT_DIR=$(echo "${PWD##*/}") # Use as a DB prefix
# Get the name of the branch that was checked out
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Get the name of the branch you were just on
PREVIOUS_BRANCH=$(git rev-parse --abbrev-ref @{-1})
# Check if file that stores branches exists (Created first time script is run)
if [ ! -f .branch_list ]; then
# Script hasn't been run, so ensure database is cloned from an exising database, aka the original database
PREVIOUS_BRANCH=$DB_NAME
# Stop script from trying to create original DB that already exists
echo $PREVIOUS_BRANCH > .branch_list
fi
# Get list of existing branches
readarray BRANCH_LIST < .branch_list
# Determine how many items are in the branch list
BRANCH_LIST_LENGTH=${#BRANCH_LIST[@]}
# Check if the current branch has been added to the list of branches
for ((NUM=0; NUM < $BRANCH_LIST_LENGTH; NUM++)); do
if [ $CURRENT_BRANCH == ${BRANCH_LIST[$NUM]} ]; then
# Change Drupal's database connection
sudo sed -i "s#'database' => .*#'database' => '$CURRENT_BRANCH',#" sites/default/settings.php
# Set variable so we know that this branch already existed
FOUND="TRUE"
fi
done
# If the current branch was not found in the existing branch list...
if [[ $FOUND != "TRUE" ]]; then
# Add the current branch to the list
echo $CURRENT_BRANCH >> .branch_list
# Create a new database for the current branch
mysqldump -u$MYSQL_USER -p"$MYSQL_PW" $PREVIOUS_BRANCH > .branch_dump.sql # Clone db from last branch
mysqladmin -u$MYSQL_USER -p"$MYSQL_PW" create $CURRENT_BRANCH # Create a new db for the new branch
mysql -u$MYSQL_USER -p"$MYSQL_PW" $CURRENT_BRANCH < .branch_dump.sql # Pipe the db clone into the new db
rm .branch_dump.sql # Remove the exported database file
# Change Drupal's database connection
sudo sed -i "s#'database' => .*#'database' => '$CURRENT_BRANCH',#" sites/default/settings.php
fi
#########################
### Cleanup databases ###
#########################
# Get all branches that aren't active
VALID_BRANCH_LIST=( $(git branch -a | grep -v "*" | awk '{print $1}') )
# Get the active branch as well and add it to the list
VALID_BRANCH_LIST+=( $(git branch -a | grep "*" | awk '{print $2}') )
# Determine how many items are in the valid branch list
VALID_BRANCH_LIST_LENGTH=${#VALID_BRANCH_LIST[@]}
# Account for any new items that were just added to text file of branches
readarray UPDATED_BRANCH_LIST < .branch_list
UPDATED_BRANCH_LIST_LENGTH=${#UPDATED_BRANCH_LIST[@]}
# Loop through the text file of branches
for ((NUM_L=0; NUM_L < $UPDATED_BRANCH_LIST_LENGTH; NUM_L++)); do
# Set default
STILL_VALID="NO"
# Loop through list of currently existing branches
for ((NUM_VL=0; NUM_VL < $VALID_BRANCH_LIST_LENGTH; NUM_VL++)); do
# Check if the item from the file matches the current branches
if [ ${UPDATED_BRANCH_LIST[$NUM_L]} == ${VALID_BRANCH_LIST[$NUM_VL]} ]; then
# The current item from the text file was found
STILL_VALID="YES"
fi
done
if [[ $STILL_VALID == "NO" ]]; then
# Delete the database for the branch that no longer exists
mysql -u$MYSQL_USER -p$MYSQL_PW -e "drop database if exists "${UPDATED_BRANCH_LIST[$NUM_L]}
# Remove the branch from our text file of branches
grep -xv ${UPDATED_BRANCH_LIST[$NUM_L]} .branch_list > .tmp_branch_list
cat .tmp_branch_list > .branch_list
rm .tmp_branch_list
fi
done