git-hacks

git-hacks Git Source Tree

Root/gitdeploy

1# This script is expected to be sourced from git hooks.
2#
3# USAGE:
4# source gitdeploy WEBDIR [FRONTEND]
5#
6# where WEBDIR is the (usually absolute) directory where the public
7# website is contained. FRONTEND specifies the web framework to know
8# how the data must be handled. If not specified, FRONTEND falls
9# back to "dummy" (that is no-op).
10#
11# EXAMPLE:
12# In the remote repository, create two hooks:
13#
14# #!/bin/sh
15# # pre-receive hook
16# source /path/to/gitdeploy /var/vhosts/mysite silverstripe
17# public2remote
18#
19# #!/bin/sh
20# # post-receive hook
21# source /path/to/gitdeploy /var/vhosts/mysite silverstripe
22# remote2public
23
24
25error() {
26 echo "** $@" 1>&2
27}
28
29
30[ -z "$1" ] && error 'gitdeploy requires a WEBDIR as first argument' && exit 1
31webdir="$1"
32frontend="$2"
33[ -z "$frontend" ] && frontend=dummy
34
35
36# Ovveride git to explicitely set its working directory to $webdir.
37git() {
38 GIT_WORK_TREE="$webdir" command git "$@"
39}
40
41
42remote2public() {
43 local branch="$1"
44 [ -z "$branch" ] && branch=master
45
46 echo -n "Syncing public with remote... "
47 git checkout -qf $branch
48 echo done
49
50 if [ ! -r "$webdir/bootstrap.cfg" ]; then return 0; fi
51
52 # Default bootstrap hook: $webdir/bootstrap.cfg can override it
53 bootstrap() {
54return 0
55 }
56
57 source $webdir/bootstrap.cfg
58
59 if [ -n "${writables[*]}" ]; then
60chmod 0777 ${writables[*]/#/$webdir/}
61 fi
62
63 bootstrap
64}
65
66refresh_dummy() {
67 return 0
68}
69
70refresh_silverstripe() {
71 local dir="$1"
72 local php code type server username password database
73
74 # Check if this directory has at least one _config* file/directory
75 stat -c '' $dir/*/_config* >/dev/null 2>&1 || return 1
76
77 php="define('BASE_PATH', realpath('.'));
78 define('FRAMEWORK_DIR', file_exists(BASE_PATH . '/framework') ? 'framework' : 'sapphire');
79 require_once(BASE_PATH . '/' . FRAMEWORK_DIR . '/core/Core.php');
80 foreach (\$databaseConfig as \$k => \$v) {
81 \$v = addcslashes(\$v, '\\'');
82 echo \"\$k='\$v'\n\";
83}"
84
85 cd "$dir"
86 code=$(php -r "$php")
87 cd - >/dev/null
88
89 eval "$code" || return 1
90 [ -n "$database" -a -n "$server" -a -n "$username" -a -n "$password" ] || return 1
91
92 mysqldump -h"$server" -u"$username" -p"$password" \
93--skip-lock-tables --skip-dump-date --skip-add-locks \
94$database > $(dirname "$dir")/"$database".sql
95
96 return 0
97}
98
99refresh_public() {
100 local dir subdomain
101
102 for dir in "$webdir"/*; do
103if [ ! -d $dir ]; then continue; fi
104
105subdomain="$(basename $dir)"
106echo -n "Refreshing subdomain $subdomain... "
107
108if refresh_$frontend "$dir" "$webdir/$subdomain.data"; then
109 echo done
110else
111 echo not a subdomain
112fi
113 done
114}
115
116public2remote() {
117 refresh_public
118
119 echo -n "Syncing remote with public... "
120
121 git add -A
122 if git diff --quiet --cached; then
123echo not needed
124return 0
125 else
126git commit -qm 'Automatically updated by gitdeploy'
127echo done
128 fi
129}

Archive Download this file

Branches