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 echo -n "Syncing public with remote... "
44 git checkout -qf
45 echo done
46
47 if [ ! -r "$webdir/bootstrap.cfg" ]; then return 0; fi
48
49 # Default bootstrap hook: $webdir/bootstrap.cfg can override it
50 bootstrap() {
51return 0
52 }
53
54 source $webdir/bootstrap.cfg
55
56 if [ -n "${writables[*]}" ]; then
57chmod 0777 ${writables[*]/#/$webdir/}
58 fi
59
60 bootstrap
61}
62
63refresh_dummy() {
64 return 0
65}
66
67refresh_silverstripe() {
68 local path="$1"
69 local code
70 local cfg type server username password database
71
72 local php="
73define('BASE_PATH', '$path');
74define('FRAMEWORK_DIR', 'framework');
75define('TEMP_FOLDER', '/tmp');
76set_include_path(BASE_PATH . '/framework' . PATH_SEPARATOR . get_include_path());
77// Disable cache entirely
78require_once('cache/Cache.php');
79SS_Cache::add_backend('default', 'black_hole');
80require_once('core/Core.php');
81foreach (\$databaseConfig as \$k => \$v) {
82 \$v = addcslashes(\$v, '\\\'');
83 echo \"\$k='\$v\'\n\";
84}"
85
86 for cfg in "$path"/*/_config.php; do
87code=$(php -r "$php" 2>/dev/null)
88[ "$?" = "0" ] && eval "$code" && [ -n "$database" ] && break
89 done
90
91 if [ -z "$server" -o -z "$username" -o -z "$password" -o -z "$database" ]; then
92return 1
93 fi
94
95 mysqldump -h"$server" -u"$username" -p"$password" \
96--skip-lock-tables --skip-dump-date --skip-add-locks \
97$database > $(dirname "$path")/"$database".sql
98
99 return 0
100}
101
102refresh_public() {
103 local dir subdomain
104
105 for dir in "$webdir"/*; do
106if [ ! -d $dir ]; then continue; fi
107
108subdomain="$(basename $dir)"
109echo -n "Refreshing subdomain $subdomain... "
110
111if refresh_$frontend "$dir" "$webdir/$subdomain.data"; then
112 echo done
113else
114 echo not a subdomain
115fi
116 done
117}
118
119public2remote() {
120 refresh_public
121
122 echo -n "Syncing remote with public... "
123
124 git add -A
125 if git diff --quiet --cached; then
126echo not needed
127return 0
128 else
129git commit -qm 'Automatically updated by gitdeploy'
130echo done
131 fi
132}

Archive Download this file

Branches