Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-25-17-38-49
[civicrm-core.git] / bin / gitify
CommitLineData
bed96570
TO
1#!/bin/bash
2
3## Take an existing, tar-based CiviCRM directory and convert it to a git working directory
4
5#### Helpers ####
6
d68421a7 7###########################################
bed96570
TO
8## usage: do_gitify <repo-url> <existing-dir> [git-checkout-options]
9function 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
d68421a7 36###########################################
a32518b8 37## add hook shims to a repo
3e0f9f2e 38## usage: do_hookify <canonical-repo-name> <repo-path> <relative-hook-path>
a32518b8 39function do_hookify() {
3e0f9f2e
TO
40 GIT_CANONICAL_REPO_NAME="$1"
41 TGT="$2"
42 HOOK_DIR="$3"
a32518b8
TO
43 if [ -n "$CIVICRM_GIT_HOOKS" ]; then
44 echo "[[Install recommended hooks ($TGT)]]"
fb687a12 45 for HOOK in commit-msg post-checkout post-merge pre-commit prepare-commit-msg post-commit pre-rebase post-rewrite ;do
a32518b8
TO
46 cat << TMPL > "$TGT/.git/hooks/$HOOK"
47#!/bin/bash
48if [ -f "\$GIT_DIR/${HOOK_DIR}/${HOOK}" ]; then
3e0f9f2e
TO
49 ## Note: GIT_CANONICAL_REPO_NAME was not provided by early hook-stubs
50 export GIT_CANONICAL_REPO_NAME="$GIT_CANONICAL_REPO_NAME"
a32518b8
TO
51 source "\$GIT_DIR/${HOOK_DIR}/${HOOK}"
52fi
53TMPL
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
d68421a7
TO
61###########################################
62## Create or update the URL of a git remote
63## usage: git_set_remote <local-repo-path> <remote-name> <remote-url>
64function 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###########################################
bed96570
TO
76## usage: do_svnify <repo-url> <existing-dir>
77function do_svnify() {
78 REPO="$1"
79 TGT="$2"
80 shift 2
81
82 if [ -d "$TGT/.svn" ]; then
83 echo "[[Already checked out SVN repo ($TGT) -- skip $REPO]]"
84 return
85 fi
86 [ -d $TGT ] || mkdir -p "$TGT"
87 [ -d $TMP ] && rm -rf "$TMP"
88 echo "[[Checkout $REPO ($TMP)]]"
89
90 echo "[svn co \"$REPO\" \"$TGT\"]"
91 svn co "$REPO" "$TGT"
92}
93
d68421a7 94###########################################
1fe0e278
TO
95## usage: do_gencode <civicrm-path>
96function do_gencode() {
97 pushd "$1/xml" > /dev/null
98 if [ -f "GenCode.php" ]; then
99 echo "[[Generate files]]"
100 php GenCode.php
101 else
102 echo "[[Skip \"Generate files\"]]"
103 fi
104 popd > /dev/null
105}
106
d68421a7 107###########################################
1fe0e278 108## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
d68421a7 109## 1 2 3 4
1fe0e278 110function config_repo() {
d68421a7 111 do_gitify "${UPSTREAM_GIT_BASE_URL}/${1}.git" "$2" -b "$3"
1fe0e278 112 do_hookify "$1" "$2" "$4"
d68421a7
TO
113 ## doesn't work with http -- git ls-remote "git://github.com/civicrm/civicrm-drupalz.git" HEAD --exit-code &>- ; echo $?
114 if [ -n "$FORK_GIT_BASE_URL" ]; then
115 git_set_remote "$2" upstream "${UPSTREAM_GIT_BASE_URL}/${1}.git"
116 git_set_remote "$2" origin "${FORK_GIT_BASE_URL}/${1}.git"
117 else
118 git_set_remote "$2" origin "${UPSTREAM_GIT_BASE_URL}/${1}.git"
119 fi
1fe0e278
TO
120}
121
bed96570 122function check_dep() {
a32518b8 123 if [ -z "`which git`" ]; then
bed96570 124 echo "command not found: git"
a32518b8 125 exit 3
bed96570 126 fi
e29a638a
TO
127 if [ -z `which php` ]; then
128 echo "command not found: php"
129 fi
bed96570
TO
130}
131
d68421a7
TO
132###########################################
133#### Main: Parse arguments
bed96570
TO
134
135set -e
136
a32518b8 137CIVICRM_CMS=""
a32518b8
TO
138CIVICRM_ROOT=""
139CIVICRM_L10N=""
140CIVICRM_GIT_HOOKS=""
bed96570 141CIVICRM_BRANCH="master"
d68421a7
TO
142FORK_GIT_BASE_URL=""
143UPSTREAM_GIT_BASE_URL="https://github.com/civicrm"
a32518b8
TO
144
145while [ -n "$1" ]; do
146 if [ "$1" == "--l10n" ]; then
147 CIVICRM_L10N="$1"
148 elif [ "$1" == "--hooks" ]; then
149 CIVICRM_GIT_HOOKS="$1"
d68421a7
TO
150 elif [ "$1" == "--upstream" ]; then
151 shift
152 UPSTREAM_GIT_BASE_URL="$1"
153 elif [ "$1" == "--fork" ]; then
154 shift
155 FORK_GIT_BASE_URL="$1"
a32518b8
TO
156 elif [ -z "$CIVICRM_CMS" ]; then
157 ## First arg
158 CIVICRM_CMS="$1"
a32518b8
TO
159 elif [ -z "$CIVICRM_ROOT" ]; then
160 ## Third arg
161 CIVICRM_ROOT="$1"
162 else
163 echo "unrecognized argument: $1"
164 exit 2
165 fi
166 shift
167done
168
d68421a7 169if [ -z "$CIVICRM_ROOT" -o ! -d "$CIVICRM_ROOT" -o -z "$UPSTREAM_GIT_BASE_URL" -o -z "$CIVICRM_CMS" ]; then
bed96570 170 echo "Convert a directory into a set of CiviCRM git clones"
d68421a7 171 echo "usage: $0 <Drupal|Drupal6|Joomla|WordPress|all> <existing-civicrm-root> [--fork <base-url>] [--upstream <base-url>] [--l10n] [--hooks]"
bed96570
TO
172 echo " <cms-name>: one of: Drupal|Drupal6|Joomla|WordPress|all"
173 echo " <git-base-url>: a base URL shared by the desiried git repos (e.g. git://github.com/civicrm)"
174 echo " <existing-civicrm-root>: the main directory containing CiviCRM"
d68421a7
TO
175 echo " --upstream <base-url>: specify the base URL for upstream repositories"
176 echo " --fork <base-url>: specify the base URL for your personal fork repositories"
a32518b8
TO
177 echo " --l10n: optionally fetch localization data; currently requires svn"
178 echo " --hooks: optionally install recommended git hooks; the hooks are mostly"
179 echo " tested with git CLI under Linux and OSX; they haven't been"
180 echo " tested with git GUIs or Windows"
bed96570
TO
181 echo ""
182 echo "Note: If pointing to a pre-existing directory, your local changes may be replaced by"
183 echo "the pristine code from git/svn. If you've made changes, then make sure there's a backup!"
184 echo ""
d68421a7
TO
185 echo "example: $0 Drupal /var/www/drupal7/sites/all/modules/civicrm"
186 echo " (checkout core code plus Drupal 7.x integration code)"
bed96570 187 echo ""
d68421a7
TO
188 echo "example: $0 Drupal6 /var/www/drupal6/sites/all/modules/civicrm"
189 echo " (checkout core code plus Drupal 6.x integration code)"
bed96570 190 echo ""
d68421a7 191 echo "example: $0 all ~/src/civicrm --upstream git@github.com:civicrm --l10n"
bed96570
TO
192 echo " (checkout core code plus Drupal 7.x, Joomla, and WordPress integration code and l10n using SSH)"
193 exit 1
194fi
195
d68421a7 196###########################################
1fe0e278 197#### Main: Update git repo metadata ####
bed96570 198check_dep
1fe0e278
TO
199
200## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
201config_repo civicrm-core "$CIVICRM_ROOT" "$CIVICRM_BRANCH" "../tools/scripts/git"
202config_repo civicrm-packages "$CIVICRM_ROOT/packages" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
203case "$CIVICRM_CMS" in
204 Drupal)
1fe0e278 205 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
206 ;;
207 Drupal6)
1fe0e278 208 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "6.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
209 ;;
210 Joomla)
1fe0e278 211 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
212 ;;
213 WordPress)
1fe0e278 214 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
215 ;;
216 all)
1fe0e278
TO
217 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
218 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
219 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
220 ;;
221 *)
222 echo "Unrecognized CMS: $CIVICRM_CMS"
223esac
224
225if [ "$CIVICRM_L10N" == "--l10n" ]; then
226 do_svnify "http://svn.civicrm.org/l10n/trunk" "$CIVICRM_ROOT/l10n"
227fi
e29a638a 228
1fe0e278 229do_gencode "$CIVICRM_ROOT"