blob: 746babe23539d6a7fe97f300e47b0b38e1e6e6bc [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
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030015 SRC_ROOT=$(< 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{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020034 local repo=$1
35 local hook
36 local hook_name
Norbert Thiebaud9152c422012-10-01 23:48:10 -050037
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030038 if [ -d "${repo?}"/.git ] ; then
Norbert Thiebaud9152c422012-10-01 23:48:10 -050039 # use core's hook by default
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020040 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030041 hook="${repo?}/.git/hooks/${hook_name##*/}"
42 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020043 rm -f "${hook?}"
44 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050045 fi
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020046 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -050047 # override if need be by the submodules' own hooks
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020048 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030049 hook="${repo?}/.git/hooks/${hook_name##*/}"
50 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020051 rm -f "${hook?}"
52 ln -sf "${hook_name}" "${hook?}"
53 fi
54 done
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030055 elif [ -d .git/modules/"${repo}"/hooks ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020056 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030057 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
58 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020059 rm -f "${hook?}"
60 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060061 fi
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020062 done
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060063 # override if need be by the submodules' own hooks
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020064 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030065 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
66 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020067 rm -f "${hook?}"
68 ln -sf "${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{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020077 local repo
78 local hook_name
79 local hook
Norbert Thiebaud9152c422012-10-01 23:48:10 -050080
Stephan Bergmanna3e51e22015-01-14 15:42:00 +010081 pushd "${COREDIR?}" > /dev/null
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030082 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030083 hook=".git/hooks/${hook_name##*/}"
84 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
Norbert Thiebaud9152c422012-10-01 23:48:10 -050085 rm -f "${hook?}"
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030086 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050087 fi
88 done
89
90 for repo in ${SUBMODULES_ALL?} ; do
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030091 refresh_submodule_hooks "$repo"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050092 done
93 popd > /dev/null
94
95}
96
97set_push_url()
98{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +020099 local repo
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500100
101 repo="$1"
102 if [ -n "$repo" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200103 pushd "${COREDIR?}/${repo?}" > /dev/null
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500104 else
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200105 pushd "${COREDIR?}" > /dev/null
106 repo="core"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500107 fi
108 echo "setting up push url for ${repo?}"
109 if [ "${repo?}" = "helpcontent2" ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200110 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500111 else
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200112 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500113 fi
114 popd > /dev/null
115}
116
117set_push_urls()
118{
119 PUSH_USER="$1"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500120 set_push_url
121 for repo in ${SUBMODULES_ACTIVE?} ; do
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200122 set_push_url "${repo?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500123 done
124}
125
126get_active_submodules()
127{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200128 SUBMODULES_ACTIVE=""
129 local repo
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500130
131 for repo in ${SUBMODULES_ALL?} ; do
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200132 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
133 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
134 fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500135 done
136}
137
138get_configured_submodules()
139{
140 SUBMODULES_CONFIGURED=""
Luboš Luňákd4498692012-12-11 16:39:02 +0100141 if [ -f config_host.mk ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200142 SUBMODULES_CONFIGURED=$(< config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500143 else
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200144 # if we need the configured submodule before the configuration is done. we assumed you want them all
145 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500146 fi
147}
148
Miklos Vajna803137f2013-02-09 19:09:32 +0100149get_git_reference()
150{
151 REFERENCED_GIT=""
152 if [ -f config_host.mk ]; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200153 REFERENCED_GIT=$(< config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
Miklos Vajna803137f2013-02-09 19:09:32 +0100154 fi
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200155 LINKED_GIT=""
156 if [ -f config_host.mk ]; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200157 LINKED_GIT=$(< config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200158 fi
Miklos Vajna803137f2013-02-09 19:09:32 +0100159}
160
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500161do_shortcut_update()
162{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200163 local module
164 local repo
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500165
166 for module in $SUBMODULES_CONFIGURED ; do
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200167 if [ ! -d "${module?}"/.git ] ; then
168 case "${module?}" in
169 helpcontent2)
170 if [ -d clone/help/.git ] ; then
171 repo="clone/help/.git"
172 fi
173 ;;
174 *)
175 if [ -d clone/"${module?}"/.git ] ; then
176 repo="clone/${module?}/.git"
177 fi
178 ;;
179 esac
180 if [ -n "$repo" ] ; then
181 cp -r "${repo?}" "${module?}/."
182 fi
183 fi
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500184 done
185}
186
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500187do_git_cmd()
188{
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300189 echo "cmd:$*"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500190 git "$@"
191 git submodule foreach git "$@" $KEEP_GOING
192}
193
194do_checkout()
195{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200196 local cmd
197 local create_branch="0"
198 local branch
199 local module
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500200
201 git checkout "$@" || return $?
202 for cmd in "$@" ; do
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200203 if [ "$cmd" = "-f" ]; then
Thorsten Behrens5fce97a2018-07-02 18:11:47 +0200204 continue
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200205 elif [ "$cmd" = "-b" ] ; then
206 create_branch=1
207 elif [ "$create_branch" = "1" ] ; then
208 branch="$cmd"
209 create_branch=0
210 fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500211 done
212 if [ -f .gitmodules ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200213 git submodule update
214 if [ -n "$branch" ] ; then
215 git submodule foreach git checkout -b "${branch}" HEAD || return $?
216 fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500217 else
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200218 # now that is the nasty case we moved prior to submodules
219 # delete the submodules left over if any
220 for module in $SUBMODULES_ALL ; do
221 echo "clean-up submodule $module"
222 rm -fr "${module}"
223 done
224 # make sure we have the needed repo in clone
225 ./g clone && ./g -f checkout "$@" || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500226 fi
227 return $?
228}
229
230do_reset()
231{
232 git reset "$@" || return $?
233 if [ -f .gitmodules ] ; then
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200234 git submodule update || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500235 else
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200236 # now that is the nasty case we moved prior to submodules
237 # delete the submodules left over if any
238 for module in $SUBMODULES_ALL ; do
239 echo "clean-up submodule $module"
240 rm -fr "${module}"
241 done
242 # make sure we have the needed repo in clone
243 ./g clone && ./g -f reset "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500244 fi
245 return $?;
246}
247
248do_init_modules()
249{
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200250 local module
251 local configured
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500252
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500253 do_shortcut_update
254
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500255 for module in $SUBMODULES_CONFIGURED ; do
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200256 if [ -n "$LINKED_GIT" ] ; then
257 if ! [ -d ".git/modules/${module}" ]; then
258 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
259 fi
260 fi
261 configured=$(git config --local --get submodule."${module}".url)
262 if [ -z "$configured" ] ; then
263 git submodule init "$module" || return $?
264 fi
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500265 done
Miklos Vajna73b50b72013-02-26 16:17:51 +0100266 for module in $SUBMODULES_CONFIGURED ; do
267 if [ -n "$REFERENCED_GIT" ] ; then
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300268 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" "$module" || return $?
Miklos Vajna73b50b72013-02-26 16:17:51 +0100269 else
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300270 git submodule update "$module" || return $?
Miklos Vajna73b50b72013-02-26 16:17:51 +0100271 fi
272 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500273 return 0
274}
275
276
277# no params, no action
278if [ "$#" -eq "0" ] ; then
279 usage
Jan Holesovskya694de12010-11-16 15:00:48 +0100280fi
281
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +0300282if [ ! "$(type -p git)" ]; then
Miklos Vajna889934e2011-09-05 20:45:43 +0200283 echo "Cannot find the git binary! Is git installed and is in PATH?"
284 exit 1
285fi
Jan Holesovskya694de12010-11-16 15:00:48 +0100286
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500287
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500288get_active_submodules
289get_configured_submodules
Miklos Vajna803137f2013-02-09 19:09:32 +0100290get_git_reference
Bjoern Michaelsen79e9a642012-07-10 12:57:01 +0200291
Jan Holesovsky40dd1bc2010-11-25 00:13:14 +0100292
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500293
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500294
Jan Holesovskya694de12010-11-16 15:00:48 +0100295# extra params for some commands, like log
296EXTRA=
297COMMAND="$1"
298PAGER=
299RELATIVIZE=1
300PUSH_ALL=
Michael Meeks4d2803d2011-10-25 13:20:58 +0100301PUSH_USER=
302PUSH_NOTES=
303LAST_WORKING=
304SET_LAST_WORKING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100305ALLOW_EMPTY=
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500306KEEP_GOING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100307REPORT_REPOS=1
Stephan Bergmann79e65ee2011-11-29 16:38:46 +0100308REPORT_COMMANDS=0
Norbert Thiebaud67dd44fb2011-05-01 03:26:45 -0500309REPORT_COMPACT=0
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500310DO_HOOK_REFRESH=false
Jan Holesovskya694de12010-11-16 15:00:48 +0100311
312while [ "${COMMAND:0:1}" = "-" ] ; do
313 case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500314 -f )KEEP_GOING="||:"
Michael Stahl175f17b2011-11-03 21:56:21 +0100315 ;;
316 -z)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200317 refresh_all_hooks
318 exit 0;
Michael Stahl175f17b2011-11-03 21:56:21 +0100319 ;;
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200320 --set-push-urls)
321 shift
322 PUSH_USER="$1"
323 if [ -n "${PUSH_USER}" ] ; then
324 PUSH_USER="${PUSH_USER}@"
325 fi
326 set_push_urls "$PUSH_USER"
327 exit 0;
328 ;;
329 -*)
330 echo "option: $COMMAND not supported" 1>&2
331 exit 1
Jan Holesovskya694de12010-11-16 15:00:48 +0100332 esac
333 shift
334 COMMAND="$1"
335done
336
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500337shift
338
Jan Holesovskya694de12010-11-16 15:00:48 +0100339case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500340 branch)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200341 do_git_cmd "${COMMAND}" "$@"
342 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500343 checkout)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200344 do_checkout "$@"
345 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500346 clone)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200347 do_init_modules && refresh_all_hooks
Jan Holesovskya694de12010-11-16 15:00:48 +0100348 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500349 fetch)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200350 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500351
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200352 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500353 grep)
354 KEEP_GOING="||:"
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200355 do_git_cmd "${COMMAND}" "$@"
356 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500357 pull)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200358 git pull "$@" && git submodule update && refresh_all_hooks
359 ;;
Jan Holesovskya694de12010-11-16 15:00:48 +0100360 push)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200361 git submodule foreach git push "$@"
362 if [ "$?" = "0" ] ; then
363 git push "$@"
364 fi
365 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500366 reset)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200367 do_reset
368 ;;
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500369 tag)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200370 do_git_cmd "${COMMAND}" "$@"
371 ;;
372 "")
373 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500374 *)
Thorsten Behrens4c475bc2018-07-02 18:11:22 +0200375 echo "./g does not support command: $COMMAND" 1>&2
376 exit 1;
Jan Holesovskya694de12010-11-16 15:00:48 +0100377 ;;
378esac
379
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500380exit $?
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500381
Jan Holesovskya694de12010-11-16 15:00:48 +0100382# vi:set shiftwidth=4 expandtab: