Merge branch '4.5' of https://github.com/civicrm/civicrm-core into 4.6
[civicrm-core.git] / bin / gitify
1 #!/bin/bash
2
3 ## Take an existing, tar-based CiviCRM directory and convert it to a git working directory
4
5 #### Helpers ####
6
7 ###########################################
8 ## usage: do_gitify <repo-url> <existing-dir> [git-checkout-options]
9 function do_gitify() {
10 REPO="$1"
11 TGT="$2"
12 TMP="${TGT}.tmp"
13 shift 2
14
15 if [ -d "$TGT/.git" ]; then
16 echo "[[Already checked out git repo ($TGT) -- skip $REPO]]"
17 return
18 fi
19 [ -d $TGT ] || mkdir -p "$TGT"
20 [ -d $TMP ] && rm -rf "$TMP"
21 echo "[[Checkout $REPO ($TMP)]]"
22 echo "[git clone "$@" "$REPO" "$TMP"]"
23 git clone "$@" "$REPO" "$TMP"
24
25 echo "[[Swap metadata]]"
26 mv "$TMP/.git" "$TGT/.git"
27
28 echo "[[Remove local modifications]]"
29 pushd "$TGT" > /dev/null
30 git checkout -- .
31 popd > /dev/null
32
33 rm -rf "$TMP"
34 }
35
36 ###########################################
37 ## add hook shims to a repo
38 ## usage: do_hookify <canonical-repo-name> <repo-path> <relative-hook-path>
39 function do_hookify() {
40 GIT_CANONICAL_REPO_NAME="$1"
41 TGT="$2"
42 HOOK_DIR="$3"
43 if [ -n "$CIVICRM_GIT_HOOKS" ]; then
44 echo "[[Install recommended hooks ($TGT)]]"
45 for HOOK in commit-msg post-checkout post-merge pre-commit prepare-commit-msg post-commit pre-rebase post-rewrite ;do
46 cat << TMPL > "$TGT/.git/hooks/$HOOK"
47 #!/bin/bash
48 if [ -f "\$GIT_DIR/${HOOK_DIR}/${HOOK}" ]; then
49 ## Note: GIT_CANONICAL_REPO_NAME was not provided by early hook-stubs
50 export GIT_CANONICAL_REPO_NAME="$GIT_CANONICAL_REPO_NAME"
51 source "\$GIT_DIR/${HOOK_DIR}/${HOOK}"
52 fi
53 TMPL
54 chmod +x "$TGT/.git/hooks/$HOOK"
55 done
56 else
57 echo "[[Skip hook installation ($TGT) -- use \"--hooks\" to enable]]"
58 fi
59 }
60
61 ###########################################
62 ## Create or update the URL of a git remote
63 ## usage: git_set_remote <local-repo-path> <remote-name> <remote-url>
64 function git_set_remote() {
65 REPODIR="$1"
66 REMOTE_NAME="$2"
67 REMOTE_URL="$3"
68 echo "[[Set remote ($REMOTE_NAME => $REMOTE_URL within $REPODIR)]]"
69
70 pushd "$REPODIR" >> /dev/null
71 git remote set-url "$REMOTE_NAME" "$REMOTE_URL" >/dev/null 2>&1 || git remote add "$REMOTE_NAME" "$REMOTE_URL"
72 popd >> /dev/null
73 }
74
75 ###########################################
76 ## usage: do_targzify <targz-url> <file-name> <existing-dir>
77 ## Fetches a tar.gz archive and unpacks it in the current directory
78 function do_targzify() {
79 TARGZURL="$1"
80 TARFILE="$2"
81 TGT="$3"
82 shift 3
83
84 if [ -d "$TGT" ]; then
85 echo "[[Already have a copy of the archive ($TGT) -- skip $TARGZURL]]"
86 return
87 fi
88
89 TMP=`mktemp -d`
90
91 echo "[[Downloading $TARGZURL ($TMP)]]"
92 echo "[wget \"$TARGZURL\" -O \"$TMP/$TARFILE\"]"
93
94 pushd "$CIVICRM_ROOT" > /dev/null
95 wget -q "$TARGZURL" -O "$TMP/$TARFILE"
96 tar zxfv "$TMP/$TARFILE"
97 popd
98
99 rm "$TMP/$TARFILE"
100 rmdir "$TMP"
101 }
102
103 ###########################################
104 ## usage: do_gencode <civicrm-path>
105 function do_gencode() {
106 pushd "$1/xml" > /dev/null
107 if [ -f "GenCode.php" ]; then
108 echo "[[Generate files]]"
109 php GenCode.php
110 else
111 echo "[[Skip \"Generate files\"]]"
112 fi
113 popd > /dev/null
114 }
115
116 ## usage: do_setupconf <civicrm-path>
117 function do_setupconf() {
118 pushd "$1" >> /dev/null
119 echo "[[Initialize bin/setup.conf]]"
120 if [ ! -f "bin/setup.conf" ]; then
121 pwd
122 echo cp "bin/setup.conf.txt" "bin/setup.conf"
123 cp "bin/setup.conf.txt" "bin/setup.conf"
124 fi
125 echo "[[chmod 600 bin/setup.conf]]"
126 chmod 600 bin/setup.conf
127 echo ""
128 echo "====================[ Next steps (for Civi v4.6+) ]===================="
129 echo " * Edit the following file and fill in credentials for the CiviCRM DB."
130 echo " $1/bin/setup.conf"
131 echo " * Perform one of these steps:"
132 echo " + Run 'bin/setup.sh -Dg' to download and generate missing code."
133 echo " + Run 'bin/setup.sh' to download code and reset the CiviCRM DB"
134 echo " with the latest schema."
135 echo " * If this is a network-accessible server (eg staging/production), then"
136 echo " setup.conf may present a security issue. The permissions have been"
137 echo " preset to restrict access in most servers - but this may not work"
138 echo " in all environments. Please:"
139 echo " + Check whether the file is web-accessible."
140 echo " + Optionally, change the permissions."
141 echo " + Optionally, delete the file."
142 popd > /dev/null
143 }
144
145 ###########################################
146 ## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
147 ## 1 2 3 4
148 function config_repo() {
149 do_gitify "${UPSTREAM_GIT_BASE_URL}/${1}.git" "$2" -b "$3"
150 do_hookify "$1" "$2" "$4"
151 ## doesn't work with http -- git ls-remote "git://github.com/civicrm/civicrm-drupalz.git" HEAD --exit-code &>- ; echo $?
152 if [ -n "$FORK_GIT_BASE_URL" ]; then
153 git_set_remote "$2" upstream "${UPSTREAM_GIT_BASE_URL}/${1}.git"
154 git_set_remote "$2" origin "${FORK_GIT_BASE_URL}/${1}.git"
155 else
156 git_set_remote "$2" origin "${UPSTREAM_GIT_BASE_URL}/${1}.git"
157 fi
158 }
159
160 function check_dep() {
161 if [ -z "`which git`" ]; then
162 echo "command not found: git"
163 exit 3
164 fi
165 if [ -z `which php` ]; then
166 echo "command not found: php"
167 fi
168 }
169
170 ###########################################
171 #### Main: Parse arguments
172
173 set -e
174
175 CIVICRM_CMS=""
176 CIVICRM_ROOT=""
177 CIVICRM_L10N=""
178 CIVICRM_GIT_HOOKS=""
179 CIVICRM_BRANCH="master"
180 FORK_GIT_BASE_URL=""
181 UPSTREAM_GIT_BASE_URL="https://github.com/civicrm"
182 SKIP_GENCODE=
183
184 while [ -n "$1" ]; do
185 if [ "$1" == "--l10n" ]; then
186 CIVICRM_L10N="$1"
187 elif [ "$1" == "--hooks" ]; then
188 CIVICRM_GIT_HOOKS="$1"
189 elif [ "$1" == "--upstream" ]; then
190 shift
191 UPSTREAM_GIT_BASE_URL="$1"
192 elif [ "$1" == "--fork" ]; then
193 shift
194 FORK_GIT_BASE_URL="$1"
195 elif [ "$1" == "--skip-gencode" ]; then
196 SKIP_GENCODE=1
197 elif [ "$1" == "--branch" ]; then
198 shift
199 CIVICRM_BRANCH="$1"
200 elif [ -z "$CIVICRM_CMS" ]; then
201 ## First arg
202 CIVICRM_CMS="$1"
203 elif [ -z "$CIVICRM_ROOT" ]; then
204 ## Third arg
205 CIVICRM_ROOT="$1"
206 else
207 echo "unrecognized argument: $1"
208 exit 2
209 fi
210 shift
211 done
212
213 if [ -z "$CIVICRM_ROOT" -o ! -d "$CIVICRM_ROOT" -o -z "$UPSTREAM_GIT_BASE_URL" -o -z "$CIVICRM_CMS" ]; then
214 echo "Convert a directory into a set of CiviCRM git clones"
215 echo "usage: $0 <Drupal|Drupal6|Joomla|WordPress|all> <existing-civicrm-root> [--fork <base-url>] [--upstream <base-url>] [--l10n] [--hooks] [--branch <branch>]"
216 echo " <cms-name>: one of: Drupal|Drupal6|Joomla|WordPress|all"
217 echo " <git-base-url>: a base URL shared by the desiried git repos (e.g. git://github.com/civicrm)"
218 echo " <existing-civicrm-root>: the main directory containing CiviCRM"
219 echo " --upstream <base-url>: specify the base URL for upstream repositories"
220 echo " --fork <base-url>: specify the base URL for your personal fork repositories"
221 echo " --l10n: optionally fetch localization data"
222 echo " --hooks: optionally install recommended git hooks; the hooks are mostly"
223 echo " tested with git CLI under Linux and OSX; they haven't been"
224 echo " tested with git GUIs or Windows"
225 echo " --branch <branch>: specy the base branch name to checkout (ex: 'master', '4.4')"
226 echo " For some repos, this name is adapted (ex: Drupal's '7.x-master' or '6.x-master'"
227 echo " --skip-gencode: optionally disable gencode execution"
228 echo ""
229 echo "Note: If pointing to a pre-existing directory, your local changes may be replaced by"
230 echo "the pristine code from git. If you've made changes, then make sure there's a backup!"
231 echo ""
232 echo "example: $0 Drupal /var/www/drupal7/sites/all/modules/civicrm"
233 echo " (checkout core code plus Drupal 7.x integration code)"
234 echo ""
235 echo "example: $0 Drupal6 /var/www/drupal6/sites/all/modules/civicrm"
236 echo " (checkout core code plus Drupal 6.x integration code)"
237 echo ""
238 echo "example: $0 all ~/src/civicrm --upstream git@github.com:civicrm --l10n"
239 echo " (checkout core code plus Drupal 7.x, Joomla, and WordPress integration code and l10n using SSH)"
240 exit 1
241 fi
242
243 ###########################################
244 #### Main: Update git repo metadata ####
245 check_dep
246
247 ## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
248 config_repo civicrm-core "$CIVICRM_ROOT" "$CIVICRM_BRANCH" "../tools/scripts/git"
249 config_repo civicrm-packages "$CIVICRM_ROOT/packages" "$CIVICRM_BRANCH" "../../tools/scripts/git"
250 case "$CIVICRM_CMS" in
251 Drupal)
252 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
253 ;;
254 Drupal6)
255 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "6.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
256 ;;
257 Joomla)
258 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
259 ;;
260 WordPress)
261 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
262 ;;
263 all)
264 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
265 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
266 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
267 ;;
268 none)
269 ;;
270 *)
271 echo "Unrecognized CMS: $CIVICRM_CMS"
272 esac
273
274 if [ "$CIVICRM_L10N" == "--l10n" ]; then
275 do_targzify "https://download.civicrm.org/civicrm-l10n-core/archives/civicrm-l10n-daily.tar.gz" "civicrm-l10n-daily.tar.gz" "$CIVICRM_ROOT/l10n"
276 fi
277
278 if [ -f "$CIVICRM_ROOT/composer.json" ]; then
279 ## Civi v4.6+
280 do_setupconf "$CIVICRM_ROOT"
281 elif [ -z "$SKIP_GENCODE" ]; then
282 do_gencode "$CIVICRM_ROOT"
283 fi