blob: 423a200231e99186cb413e3bac21a5de3248066b [file] [log] [blame]
Jan Holesovskya694de12010-11-16 15:00:48 +01001#!/usr/bin/env bash
2#
3# Wrapper for git to handle more subdirs at the same time
4#
Norbert Thiebaud8feb6c02010-11-22 03:11:56 -06005
Norbert Thiebaud9152c422012-10-01 23:48:10 -05006if [ -n "$g_debug" ] ; then
7 set -x
8fi
9
Norbert Thiebaud060219a2012-11-08 16:25:37 -060010SUBMODULES_ALL="dictionaries helpcontent2 translations"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050011
12pushd $(dirname $0) > /dev/null
Norbert Thiebaudae4e3272012-12-11 07:49:24 -060013if [ -f config_host.mk ] ; then
Luboš Luňákc8c056f2012-12-11 15:39:49 +010014 # we are in the BUILDDIR
Tomofumi Yagi0293ec92015-02-14 19:21:57 +090015 SRC_ROOT=$(cat config_host.mk | grep -a SRC_ROOT | sed -e "s/.*=//")
Norbert Thiebaudae4e3272012-12-11 07:49:24 -060016else
17 SRC_ROOT=$(pwd)
18fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -050019popd > /dev/null
20
Norbert Thiebaudae4e3272012-12-11 07:49:24 -060021COREDIR="$SRC_ROOT"
22
Norbert Thiebaud9152c422012-10-01 23:48:10 -050023usage()
24{
Jan Holesovskya694de12010-11-16 15:00:48 +010025 git
26 echo
Norbert Thiebaud9152c422012-10-01 23:48:10 -050027 echo "Usage: g [options] [git (checkout|clone|fetch|grep|pull|push|reset) [git options/args..]]"
28 echo ""
Peter Foley41b5c802012-10-16 20:43:21 -040029 echo " -z restore the git hooks and do other sanity checks"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050030}
31
32refresh_submodule_hooks()
33{
34local repo=$1
35local hook
36local hook_name
37
38 if [ -d ${repo?}/.git ] ; then
39 # use core's hook by default
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010040 for hook_name in $(ls -1 "${COREDIR?}/.git-hooks") ; do
Norbert Thiebaud9152c422012-10-01 23:48:10 -050041 hook="${repo?}/.git/hooks/${hook_name?}"
42 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
43 rm -f "${hook?}"
44 ln -sf "${COREDIR?}/.git-hooks/${hook_name?}" "${hook?}"
45 fi
46 done
47 # override if need be by the submodules' own hooks
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010048 for hook_name in $(ls -1 "${COREDIR?}/${repo?}/.git-hooks" 2>/dev/null) ; do
Norbert Thiebaud9152c422012-10-01 23:48:10 -050049 hook="${repo?}/.git/hooks/${hook_name?}"
50 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
51 rm -f "${hook?}"
52 ln -sf "${COREDIR?}/${repo?}/.git-hooks/${hook_name?}" "${hook?}"
53 fi
54 done
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060055 elif [ -d .git/modules/${repo}/hooks ] ; then
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010056 for hook_name in $(ls -1 "${COREDIR?}/.git-hooks") ; do
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060057 hook=".git/modules/${repo?}/hooks/${hook_name?}"
58 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
59 rm -f "${hook?}"
60 ln -sf "${COREDIR?}/.git-hooks/${hook_name?}" "${hook?}"
61 fi
62 done
63 # override if need be by the submodules' own hooks
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010064 for hook_name in $(ls -1 "${COREDIR?}/${repo?}/.git-hooks" 2>/dev/null) ; do
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060065 hook=".git/modules/${repo?}/hooks/${hook_name?}"
66 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
67 rm -f "${hook?}"
68 ln -sf "${COREDIR?}/${repo?}/.git-hooks/${hook_name?}" "${hook?}"
69 fi
70 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -050071 fi
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060072
Norbert Thiebaud9152c422012-10-01 23:48:10 -050073}
74
75refresh_all_hooks()
76{
77local repo
78local hook_name
79local hook
80
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010081 pushd "${COREDIR?}" > /dev/null
82 for hook_name in $(ls -1 "${COREDIR?}/.git-hooks") ; do
Norbert Thiebaud9152c422012-10-01 23:48:10 -050083 hook=".git/hooks/${hook_name?}"
84 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
85 rm -f "${hook?}"
86 ln -sf "${COREDIR?}/.git-hooks/${hook_name?}" "${hook?}"
87 fi
88 done
89
90 for repo in ${SUBMODULES_ALL?} ; do
91 refresh_submodule_hooks $repo
92 done
Miklos Vajna27925032013-07-22 11:55:49 +020093 # In our workflow, it's always gerrit that does the submodule updates, so
94 # better ignoring them to avoid accidentally including those changes in our
95 # commits.
96 # 'git submodule status' can be still used to see if a submodule has such
97 # changes.
98 for repo in ${SUBMODULES_CONFIGURED?} ; do
99 git config submodule.$repo.ignore all
100 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500101 popd > /dev/null
102
103}
104
105set_push_url()
106{
107local repo
108
109 repo="$1"
110 if [ -n "$repo" ] ; then
111 pushd "${COREDIR?}/${repo?}" > /dev/null
112 else
113 pushd "${COREDIR?}" > /dev/null
114 repo="core"
115 fi
116 echo "setting up push url for ${repo?}"
117 if [ "${repo?}" = "helpcontent2" ] ; then
Miklos Vajna677b4402013-10-04 16:03:57 +0200118 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500119 else
Miklos Vajna677b4402013-10-04 16:03:57 +0200120 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500121 fi
122 popd > /dev/null
123}
124
125set_push_urls()
126{
127 PUSH_USER="$1"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500128 set_push_url
129 for repo in ${SUBMODULES_ACTIVE?} ; do
130 set_push_url "${repo?}"
131 done
132}
133
134get_active_submodules()
135{
136SUBMODULES_ACTIVE=""
137local repo
138
139 for repo in ${SUBMODULES_ALL?} ; do
Miklos Vajna677b4402013-10-04 16:03:57 +0200140 if [ -d ${repo?}/.git -o -f ${repo?}/.git ] ; then
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500141 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
142 fi
143 done
144}
145
146get_configured_submodules()
147{
148 SUBMODULES_CONFIGURED=""
Luboš Luňákd4498692012-12-11 16:39:02 +0100149 if [ -f config_host.mk ] ; then
Tomofumi Yagi125f06d2015-06-15 21:20:19 +0900150 SUBMODULES_CONFIGURED=$(cat config_host.mk | grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500151 else
152 # if we need the configured submoduel before the configuration is done. we assumed you want them all
153 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
154 fi
155}
156
Miklos Vajna803137f2013-02-09 19:09:32 +0100157get_git_reference()
158{
159 REFERENCED_GIT=""
160 if [ -f config_host.mk ]; then
Tomofumi Yagi125f06d2015-06-15 21:20:19 +0900161 REFERENCED_GIT=$(cat config_host.mk | grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
Miklos Vajna803137f2013-02-09 19:09:32 +0100162 fi
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200163 LINKED_GIT=""
164 if [ -f config_host.mk ]; then
Tomofumi Yagi125f06d2015-06-15 21:20:19 +0900165 LINKED_GIT=$(cat config_host.mk | grep -a GIT_LINK_SRC | sed -e "s/.*=//")
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200166 fi
Miklos Vajna803137f2013-02-09 19:09:32 +0100167}
168
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500169do_shortcut_update()
170{
171local module
172local repo
173
174 for module in $SUBMODULES_CONFIGURED ; do
175 if [ ! -d ${module?}/.git ] ; then
176 case "${module?}" in
Miklos Vajna2a8cabe2012-10-18 09:27:45 +0200177 helpcontent2)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500178 if [ -d clone/help/.git ] ; then
179 repo="clone/help/.git"
180 fi
181 ;;
182 *)
183 if [ -d clone/${module?}/.git ] ; then
184 repo="clone/${module?}/.git"
185 fi
186 ;;
187 esac
188 if [ -n "$repo" ] ; then
189 cp -r "${repo?}" "${module?}/."
190 fi
Norbert Thiebaudc58b4fc2012-10-02 13:29:01 -0500191 fi
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500192 done
193}
194
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500195do_git_cmd()
196{
197 echo "cmd:$@"
198 git "$@"
199 git submodule foreach git "$@" $KEEP_GOING
200}
201
202do_checkout()
203{
204local cmd
205local create_branch="0"
206local branch
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500207local module
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500208
209 git checkout "$@" || return $?
210 for cmd in "$@" ; do
211 if [ "$cmd" = "-f" ]; then
212 return 0
213 elif [ "$cmd" = "-b" ] ; then
214 create_branch=1
215 elif [ "$create_branch" = "1" ] ; then
216 branch="$arg"
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500217 create_branch=0
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500218 fi
219 done
220 if [ -f .gitmodules ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500221 git submodule update
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500222 if [ -n "$branch" ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500223 git submodules foreach git checkout -b ${branch} HEAD || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500224 fi
225 else
226 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500227 # delete the submodules left over if any
228 for module in $SUBMODULES_ALL ; do
229 echo "clean-up submodule $module"
230 rm -fr ${module}
231 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500232 # make sure we have the needed repo in clone
233 ./g clone && ./g -f checkout "$@" || return $?
234 fi
235 return $?
236}
237
238do_reset()
239{
240 git reset "$@" || return $?
241 if [ -f .gitmodules ] ; then
242 git submodule update || return $?
243 else
244 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500245 # delete the submodules left over if any
246 for module in $SUBMODULES_ALL ; do
247 echo "clean-up submodule $module"
248 rm -fr ${module}
249 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500250 # make sure we have the needed repo in clone
251 ./g clone && ./g -f reset "$@"
252 fi
253 return $?;
254}
255
256do_init_modules()
257{
258local module
259local configured
260
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500261 do_shortcut_update
262
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500263 for module in $SUBMODULES_CONFIGURED ; do
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200264 if [ -n "$LINKED_GIT" ] ; then
265 if ! [ -d ".git/modules/${module}" ]; then
266 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
267 fi
268 fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500269 configured=$(git config --local --get submodule.${module}.url)
270 if [ -z "$configured" ] ; then
271 git submodule init $module || return $?
272 fi
273 done
Miklos Vajna73b50b72013-02-26 16:17:51 +0100274 for module in $SUBMODULES_CONFIGURED ; do
275 if [ -n "$REFERENCED_GIT" ] ; then
Miklos Vajna803137f2013-02-09 19:09:32 +0100276 git submodule update --reference $REFERENCED_GIT/.git/modules/$module $module || return $?
Miklos Vajna73b50b72013-02-26 16:17:51 +0100277 else
278 git submodule update $module || return $?
279 fi
280 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500281 return 0
282}
283
284
285# no params, no action
286if [ "$#" -eq "0" ] ; then
287 usage
Jan Holesovskya694de12010-11-16 15:00:48 +0100288fi
289
Miklos Vajna889934e2011-09-05 20:45:43 +0200290if [ ! "`type -p git`" ]; then
291 echo "Cannot find the git binary! Is git installed and is in PATH?"
292 exit 1
293fi
Jan Holesovskya694de12010-11-16 15:00:48 +0100294
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500295
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500296get_active_submodules
297get_configured_submodules
Miklos Vajna803137f2013-02-09 19:09:32 +0100298get_git_reference
Bjoern Michaelsen79e9a642012-07-10 12:57:01 +0200299
Jan Holesovsky40dd1bc2010-11-25 00:13:14 +0100300
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500301
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500302
Jan Holesovskya694de12010-11-16 15:00:48 +0100303# extra params for some commands, like log
304EXTRA=
305COMMAND="$1"
306PAGER=
307RELATIVIZE=1
308PUSH_ALL=
Michael Meeks4d2803d2011-10-25 13:20:58 +0100309PUSH_USER=
310PUSH_NOTES=
311LAST_WORKING=
312SET_LAST_WORKING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100313ALLOW_EMPTY=
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500314KEEP_GOING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100315REPORT_REPOS=1
Stephan Bergmann79e65ee2011-11-29 16:38:46 +0100316REPORT_COMMANDS=0
Norbert Thiebaud67dd44fb2011-05-01 03:26:45 -0500317REPORT_COMPACT=0
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500318DO_HOOK_REFRESH=false
Jan Holesovskya694de12010-11-16 15:00:48 +0100319
320while [ "${COMMAND:0:1}" = "-" ] ; do
321 case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500322 -f )KEEP_GOING="||:"
Michael Stahl175f17b2011-11-03 21:56:21 +0100323 ;;
324 -z)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500325 refresh_all_hooks
326 exit 0;
Michael Stahl175f17b2011-11-03 21:56:21 +0100327 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500328 --set-push-urls)
329 shift
330 PUSH_USER="$1"
331 if [ -n "${PUSH_USER}" ] ; then
332 PUSH_USER="${PUSH_USER}@"
333 fi
Jan Holesovskye1b59fc2012-11-23 20:58:27 +0100334 set_push_urls "$PUSH_USER"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500335 exit 0;
336 ;;
337 -*)
338 echo "option: $COMMAND not supported" 1>&2
339 exit 1
Jan Holesovskya694de12010-11-16 15:00:48 +0100340 esac
341 shift
342 COMMAND="$1"
343done
344
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500345shift
346
Jan Holesovskya694de12010-11-16 15:00:48 +0100347case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500348 branch)
349 do_git_cmd ${COMMAND} "$@"
350 ;;
351 checkout)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500352 do_checkout "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500353 ;;
354 clone)
Miklos Vajna803137f2013-02-09 19:09:32 +0100355 do_init_modules && refresh_all_hooks
Jan Holesovskya694de12010-11-16 15:00:48 +0100356 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500357 fetch)
358 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
359
360 ;;
361 grep)
362 KEEP_GOING="||:"
363 do_git_cmd ${COMMAND} "$@"
364 ;;
365 pull)
366 git pull "$@" && git submodule update && refresh_all_hooks
367 ;;
Jan Holesovskya694de12010-11-16 15:00:48 +0100368 push)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500369 git submodule foreach git push "$@"
370 if [ "$?" = "0" ] ; then
371 git push "$@"
372 fi
373 ;;
374 reset)
375 do_reset
376 ;;
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500377 tag)
378 do_git_cmd ${COMMAND} "$@"
379 ;;
Peter Foley41b5c802012-10-16 20:43:21 -0400380 "")
381 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500382 *)
Peter Foley41b5c802012-10-16 20:43:21 -0400383 echo "./g does not support command: $COMMAND" 1>&2
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500384 exit 1;
Jan Holesovskya694de12010-11-16 15:00:48 +0100385 ;;
386esac
387
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500388exit $?
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500389
Jan Holesovskya694de12010-11-16 15:00:48 +0100390# vi:set shiftwidth=4 expandtab: