Merge pull request #4851 from totten/master-createtest-bao
[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###########################################
bd586bd4
ML
76## usage: do_targzify <targz-url> <file-name> <existing-dir>
77## Fetches a tar.gz archive and unpacks it in the current directory
78function 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]]"
bed96570
TO
86 return
87 fi
bed96570 88
bd586bd4
ML
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"
bed96570
TO
101}
102
d68421a7 103###########################################
1fe0e278
TO
104## usage: do_gencode <civicrm-path>
105function 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
d68421a7 116###########################################
1fe0e278 117## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
d68421a7 118## 1 2 3 4
1fe0e278 119function config_repo() {
d68421a7 120 do_gitify "${UPSTREAM_GIT_BASE_URL}/${1}.git" "$2" -b "$3"
1fe0e278 121 do_hookify "$1" "$2" "$4"
d68421a7
TO
122 ## doesn't work with http -- git ls-remote "git://github.com/civicrm/civicrm-drupalz.git" HEAD --exit-code &>- ; echo $?
123 if [ -n "$FORK_GIT_BASE_URL" ]; then
124 git_set_remote "$2" upstream "${UPSTREAM_GIT_BASE_URL}/${1}.git"
125 git_set_remote "$2" origin "${FORK_GIT_BASE_URL}/${1}.git"
126 else
127 git_set_remote "$2" origin "${UPSTREAM_GIT_BASE_URL}/${1}.git"
128 fi
1fe0e278
TO
129}
130
bed96570 131function check_dep() {
a32518b8 132 if [ -z "`which git`" ]; then
bed96570 133 echo "command not found: git"
a32518b8 134 exit 3
bed96570 135 fi
e29a638a
TO
136 if [ -z `which php` ]; then
137 echo "command not found: php"
138 fi
bed96570
TO
139}
140
d68421a7
TO
141###########################################
142#### Main: Parse arguments
bed96570
TO
143
144set -e
145
a32518b8 146CIVICRM_CMS=""
a32518b8
TO
147CIVICRM_ROOT=""
148CIVICRM_L10N=""
149CIVICRM_GIT_HOOKS=""
bed96570 150CIVICRM_BRANCH="master"
d68421a7
TO
151FORK_GIT_BASE_URL=""
152UPSTREAM_GIT_BASE_URL="https://github.com/civicrm"
7f614f4d 153SKIP_GENCODE=
a32518b8
TO
154
155while [ -n "$1" ]; do
156 if [ "$1" == "--l10n" ]; then
157 CIVICRM_L10N="$1"
158 elif [ "$1" == "--hooks" ]; then
159 CIVICRM_GIT_HOOKS="$1"
d68421a7
TO
160 elif [ "$1" == "--upstream" ]; then
161 shift
162 UPSTREAM_GIT_BASE_URL="$1"
163 elif [ "$1" == "--fork" ]; then
164 shift
165 FORK_GIT_BASE_URL="$1"
7f614f4d
TO
166 elif [ "$1" == "--skip-gencode" ]; then
167 SKIP_GENCODE=1
168 elif [ "$1" == "--branch" ]; then
169 shift
170 CIVICRM_BRANCH="$1"
a32518b8
TO
171 elif [ -z "$CIVICRM_CMS" ]; then
172 ## First arg
173 CIVICRM_CMS="$1"
a32518b8
TO
174 elif [ -z "$CIVICRM_ROOT" ]; then
175 ## Third arg
176 CIVICRM_ROOT="$1"
177 else
178 echo "unrecognized argument: $1"
179 exit 2
180 fi
181 shift
182done
183
d68421a7 184if [ -z "$CIVICRM_ROOT" -o ! -d "$CIVICRM_ROOT" -o -z "$UPSTREAM_GIT_BASE_URL" -o -z "$CIVICRM_CMS" ]; then
bed96570 185 echo "Convert a directory into a set of CiviCRM git clones"
7f614f4d 186 echo "usage: $0 <Drupal|Drupal6|Joomla|WordPress|all> <existing-civicrm-root> [--fork <base-url>] [--upstream <base-url>] [--l10n] [--hooks] [--branch <branch>]"
bed96570
TO
187 echo " <cms-name>: one of: Drupal|Drupal6|Joomla|WordPress|all"
188 echo " <git-base-url>: a base URL shared by the desiried git repos (e.g. git://github.com/civicrm)"
189 echo " <existing-civicrm-root>: the main directory containing CiviCRM"
d68421a7
TO
190 echo " --upstream <base-url>: specify the base URL for upstream repositories"
191 echo " --fork <base-url>: specify the base URL for your personal fork repositories"
bd586bd4 192 echo " --l10n: optionally fetch localization data"
a32518b8
TO
193 echo " --hooks: optionally install recommended git hooks; the hooks are mostly"
194 echo " tested with git CLI under Linux and OSX; they haven't been"
195 echo " tested with git GUIs or Windows"
7f614f4d
TO
196 echo " --branch <branch>: specy the base branch name to checkout (ex: 'master', '4.4')"
197 echo " For some repos, this name is adapted (ex: Drupal's '7.x-master' or '6.x-master'"
198 echo " --skip-gencode: optionally disable gencode execution"
bed96570
TO
199 echo ""
200 echo "Note: If pointing to a pre-existing directory, your local changes may be replaced by"
bd586bd4 201 echo "the pristine code from git. If you've made changes, then make sure there's a backup!"
bed96570 202 echo ""
d68421a7
TO
203 echo "example: $0 Drupal /var/www/drupal7/sites/all/modules/civicrm"
204 echo " (checkout core code plus Drupal 7.x integration code)"
bed96570 205 echo ""
d68421a7
TO
206 echo "example: $0 Drupal6 /var/www/drupal6/sites/all/modules/civicrm"
207 echo " (checkout core code plus Drupal 6.x integration code)"
bed96570 208 echo ""
d68421a7 209 echo "example: $0 all ~/src/civicrm --upstream git@github.com:civicrm --l10n"
bed96570
TO
210 echo " (checkout core code plus Drupal 7.x, Joomla, and WordPress integration code and l10n using SSH)"
211 exit 1
212fi
213
d68421a7 214###########################################
1fe0e278 215#### Main: Update git repo metadata ####
bed96570 216check_dep
1fe0e278
TO
217
218## config_repo <repo-name> <local-path> <default-branch> <git-scripts-path>
219config_repo civicrm-core "$CIVICRM_ROOT" "$CIVICRM_BRANCH" "../tools/scripts/git"
220config_repo civicrm-packages "$CIVICRM_ROOT/packages" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
221case "$CIVICRM_CMS" in
222 Drupal)
1fe0e278 223 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
224 ;;
225 Drupal6)
1fe0e278 226 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "6.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
227 ;;
228 Joomla)
1fe0e278 229 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
230 ;;
231 WordPress)
1fe0e278 232 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
233 ;;
234 all)
1fe0e278
TO
235 config_repo civicrm-drupal "$CIVICRM_ROOT/drupal" "7.x-$CIVICRM_BRANCH" "../../tools/scripts/git"
236 config_repo civicrm-joomla "$CIVICRM_ROOT/joomla" "$CIVICRM_BRANCH" "../../tools/scripts/git"
237 config_repo civicrm-wordpress "$CIVICRM_ROOT/WordPress" "$CIVICRM_BRANCH" "../../tools/scripts/git"
bed96570
TO
238 ;;
239 *)
240 echo "Unrecognized CMS: $CIVICRM_CMS"
241esac
242
243if [ "$CIVICRM_L10N" == "--l10n" ]; then
bd586bd4 244 do_targzify "https://download.civicrm.org/civicrm-l10n-core/archives/civicrm-l10n-daily.tar.gz" "civicrm-l10n-daily.tar.gz" "$CIVICRM_ROOT/l10n"
bed96570 245fi
e29a638a 246
7f614f4d
TO
247if [ -z "$SKIP_GENCODE" ]; then
248 do_gencode "$CIVICRM_ROOT"
249fi