blob: 13bc73939e495dc6c0a6308a7eda941faca445b1 [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{
34local repo=$1
35local hook
36local hook_name
37
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
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030040 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
Norbert Thiebaud9152c422012-10-01 23:48:10 -050043 rm -f "${hook?}"
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030044 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050045 fi
46 done
47 # override if need be by the submodules' own hooks
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030048 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
Norbert Thiebaud9152c422012-10-01 23:48:10 -050051 rm -f "${hook?}"
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030052 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud9152c422012-10-01 23:48:10 -050053 fi
54 done
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030055 elif [ -d .git/modules/"${repo}"/hooks ] ; then
56 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
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060059 rm -f "${hook?}"
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030060 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060061 fi
62 done
63 # override if need be by the submodules' own hooks
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +030064 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
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060067 rm -f "${hook?}"
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +030068 ln -sf "${hook_name}" "${hook?}"
Norbert Thiebaud0cac67b2012-12-21 04:33:20 -060069 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
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{
99local repo
100
101 repo="$1"
102 if [ -n "$repo" ] ; then
103 pushd "${COREDIR?}/${repo?}" > /dev/null
104 else
105 pushd "${COREDIR?}" > /dev/null
106 repo="core"
107 fi
108 echo "setting up push url for ${repo?}"
109 if [ "${repo?}" = "helpcontent2" ] ; then
Miklos Vajna677b4402013-10-04 16:03:57 +0200110 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500111 else
Miklos Vajna677b4402013-10-04 16:03:57 +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
122 set_push_url "${repo?}"
123 done
124}
125
126get_active_submodules()
127{
128SUBMODULES_ACTIVE=""
129local repo
130
131 for repo in ${SUBMODULES_ALL?} ; do
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300132 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500133 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
134 fi
135 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
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +0300142 SUBMODULES_CONFIGURED=$(< config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500143 else
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300144 # if we need the configured submodule before the configuration is done. we assumed you want them all
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500145 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
146 fi
147}
148
Miklos Vajna803137f2013-02-09 19:09:32 +0100149get_git_reference()
150{
151 REFERENCED_GIT=""
152 if [ -f config_host.mk ]; then
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +0300153 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
Ilmari Lauhakangas55c5b272017-04-15 18:35:17 +0300157 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{
163local module
164local repo
165
166 for module in $SUBMODULES_CONFIGURED ; do
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300167 if [ ! -d "${module?}"/.git ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500168 case "${module?}" in
Miklos Vajna2a8cabe2012-10-18 09:27:45 +0200169 helpcontent2)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500170 if [ -d clone/help/.git ] ; then
171 repo="clone/help/.git"
172 fi
173 ;;
174 *)
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300175 if [ -d clone/"${module?}"/.git ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500176 repo="clone/${module?}/.git"
177 fi
178 ;;
179 esac
180 if [ -n "$repo" ] ; then
181 cp -r "${repo?}" "${module?}/."
182 fi
Norbert Thiebaudc58b4fc2012-10-02 13:29:01 -0500183 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{
196local cmd
197local create_branch="0"
198local branch
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500199local module
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500200
201 git checkout "$@" || return $?
202 for cmd in "$@" ; do
203 if [ "$cmd" = "-f" ]; then
204 return 0
205 elif [ "$cmd" = "-b" ] ; then
206 create_branch=1
207 elif [ "$create_branch" = "1" ] ; then
Juergen Funk87495ce2016-01-07 17:38:15 +0100208 branch="$cmd"
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500209 create_branch=0
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500210 fi
211 done
212 if [ -f .gitmodules ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500213 git submodule update
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500214 if [ -n "$branch" ] ; then
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300215 git submodule foreach git checkout -b "${branch}" HEAD || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500216 fi
217 else
218 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500219 # delete the submodules left over if any
220 for module in $SUBMODULES_ALL ; do
221 echo "clean-up submodule $module"
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300222 rm -fr "${module}"
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500223 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500224 # make sure we have the needed repo in clone
225 ./g clone && ./g -f checkout "$@" || return $?
226 fi
227 return $?
228}
229
230do_reset()
231{
232 git reset "$@" || return $?
233 if [ -f .gitmodules ] ; then
234 git submodule update || return $?
235 else
236 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500237 # delete the submodules left over if any
238 for module in $SUBMODULES_ALL ; do
239 echo "clean-up submodule $module"
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300240 rm -fr "${module}"
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500241 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500242 # make sure we have the needed repo in clone
243 ./g clone && ./g -f reset "$@"
244 fi
245 return $?;
246}
247
248do_init_modules()
249{
250local module
251local configured
252
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
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +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
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300261 configured=$(git config --local --get submodule."${module}".url)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500262 if [ -z "$configured" ] ; then
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300263 git submodule init "$module" || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500264 fi
265 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)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500317 refresh_all_hooks
318 exit 0;
Michael Stahl175f17b2011-11-03 21:56:21 +0100319 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500320 --set-push-urls)
321 shift
322 PUSH_USER="$1"
323 if [ -n "${PUSH_USER}" ] ; then
324 PUSH_USER="${PUSH_USER}@"
325 fi
Jan Holesovskye1b59fc2012-11-23 20:58:27 +0100326 set_push_urls "$PUSH_USER"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500327 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)
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300341 do_git_cmd "${COMMAND}" "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500342 ;;
343 checkout)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500344 do_checkout "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500345 ;;
346 clone)
Miklos Vajna803137f2013-02-09 19:09:32 +0100347 do_init_modules && refresh_all_hooks
Jan Holesovskya694de12010-11-16 15:00:48 +0100348 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500349 fetch)
350 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
351
352 ;;
353 grep)
354 KEEP_GOING="||:"
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300355 do_git_cmd "${COMMAND}" "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500356 ;;
357 pull)
358 git pull "$@" && git submodule update && refresh_all_hooks
359 ;;
Jan Holesovskya694de12010-11-16 15:00:48 +0100360 push)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500361 git submodule foreach git push "$@"
362 if [ "$?" = "0" ] ; then
363 git push "$@"
364 fi
365 ;;
366 reset)
367 do_reset
368 ;;
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500369 tag)
Ilmari Lauhakangase5b1c532017-04-27 12:46:47 +0300370 do_git_cmd "${COMMAND}" "$@"
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500371 ;;
Peter Foley41b5c802012-10-16 20:43:21 -0400372 "")
373 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500374 *)
Peter Foley41b5c802012-10-16 20:43:21 -0400375 echo "./g does not support command: $COMMAND" 1>&2
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500376 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: