blob: c24ca3bad4810eb34ab239f8c93ff77233a659e4 [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
Luboš Luňákd4498692012-12-11 16:39:02 +010015 SRC_ROOT=$(cat config_host.mk | grep 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
40 for hook_name in $(ls -1 ${COREDIR?}/.git-hooks) ; do
41 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
48 for hook_name in $(ls -1 ${COREDIR?}/${repo?}/.git-hooks 2>/dev/null) ; do
49 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
56 for hook_name in $(ls -1 ${COREDIR?}/.git-hooks) ; do
57 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
64 for hook_name in $(ls -1 ${COREDIR?}/${repo?}/.git-hooks 2>/dev/null) ; do
65 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
81 pushd ${COREDIR?} > /dev/null
82 for hook_name in $(ls -1 ${COREDIR?}/.git-hooks) ; do
83 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
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
110 git config remote.origin.pushurl "ssh://${PUSH_USER}gerrit.libreoffice.org:29418/help"
111 else
112 git config remote.origin.pushurl "ssh://${PUSH_USER}gerrit.libreoffice.org:29418/${repo?}"
113 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
132 if [ -d ${repo?}/.git ] ; then
133 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
142 SUBMODULES_CONFIGURED=$(cat config_host.mk | grep GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500143 else
144 # if we need the configured submoduel before the configuration is done. we assumed you want them all
145 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
153 REFERENCED_GIT=$(cat config_host.mk | grep GIT_REFERENCE_SRC | sed -e "s/.*=//")
154 fi
155}
156
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500157do_shortcut_update()
158{
159local module
160local repo
161
162 for module in $SUBMODULES_CONFIGURED ; do
163 if [ ! -d ${module?}/.git ] ; then
164 case "${module?}" in
Miklos Vajna2a8cabe2012-10-18 09:27:45 +0200165 helpcontent2)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500166 if [ -d clone/help/.git ] ; then
167 repo="clone/help/.git"
168 fi
169 ;;
170 *)
171 if [ -d clone/${module?}/.git ] ; then
172 repo="clone/${module?}/.git"
173 fi
174 ;;
175 esac
176 if [ -n "$repo" ] ; then
177 cp -r "${repo?}" "${module?}/."
178 fi
Norbert Thiebaudc58b4fc2012-10-02 13:29:01 -0500179 fi
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500180 done
181}
182
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500183do_git_cmd()
184{
185 echo "cmd:$@"
186 git "$@"
187 git submodule foreach git "$@" $KEEP_GOING
188}
189
190do_checkout()
191{
192local cmd
193local create_branch="0"
194local branch
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500195local module
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500196
197 git checkout "$@" || return $?
198 for cmd in "$@" ; do
199 if [ "$cmd" = "-f" ]; then
200 return 0
201 elif [ "$cmd" = "-b" ] ; then
202 create_branch=1
203 elif [ "$create_branch" = "1" ] ; then
204 branch="$arg"
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500205 create_branch=0
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500206 fi
207 done
208 if [ -f .gitmodules ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500209 git submodule update
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500210 if [ -n "$branch" ] ; then
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500211 git submodules foreach git checkout -b ${branch} HEAD || return $?
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500212 fi
213 else
214 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500215 # delete the submodules left over if any
216 for module in $SUBMODULES_ALL ; do
217 echo "clean-up submodule $module"
218 rm -fr ${module}
219 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500220 # make sure we have the needed repo in clone
221 ./g clone && ./g -f checkout "$@" || return $?
222 fi
223 return $?
224}
225
226do_reset()
227{
228 git reset "$@" || return $?
229 if [ -f .gitmodules ] ; then
230 git submodule update || return $?
231 else
232 # now that is the nasty case we moved prior to submodules
Norbert Thiebaud2c5cdc42012-10-02 10:32:45 -0500233 # delete the submodules left over if any
234 for module in $SUBMODULES_ALL ; do
235 echo "clean-up submodule $module"
236 rm -fr ${module}
237 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500238 # make sure we have the needed repo in clone
239 ./g clone && ./g -f reset "$@"
240 fi
241 return $?;
242}
243
244do_init_modules()
245{
246local module
247local configured
248
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500249 do_shortcut_update
250
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500251 for module in $SUBMODULES_CONFIGURED ; do
252 configured=$(git config --local --get submodule.${module}.url)
253 if [ -z "$configured" ] ; then
254 git submodule init $module || return $?
255 fi
256 done
Miklos Vajna73b50b72013-02-26 16:17:51 +0100257 for module in $SUBMODULES_CONFIGURED ; do
258 if [ -n "$REFERENCED_GIT" ] ; then
Miklos Vajna803137f2013-02-09 19:09:32 +0100259 git submodule update --reference $REFERENCED_GIT/.git/modules/$module $module || return $?
Miklos Vajna73b50b72013-02-26 16:17:51 +0100260 else
261 git submodule update $module || return $?
262 fi
263 done
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500264 return 0
265}
266
267
268# no params, no action
269if [ "$#" -eq "0" ] ; then
270 usage
Jan Holesovskya694de12010-11-16 15:00:48 +0100271fi
272
Miklos Vajna889934e2011-09-05 20:45:43 +0200273if [ ! "`type -p git`" ]; then
274 echo "Cannot find the git binary! Is git installed and is in PATH?"
275 exit 1
276fi
Jan Holesovskya694de12010-11-16 15:00:48 +0100277
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500278
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500279get_active_submodules
280get_configured_submodules
Miklos Vajna803137f2013-02-09 19:09:32 +0100281get_git_reference
Bjoern Michaelsen79e9a642012-07-10 12:57:01 +0200282
Jan Holesovsky40dd1bc2010-11-25 00:13:14 +0100283
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500284
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500285
Jan Holesovskya694de12010-11-16 15:00:48 +0100286# extra params for some commands, like log
287EXTRA=
288COMMAND="$1"
289PAGER=
290RELATIVIZE=1
291PUSH_ALL=
Michael Meeks4d2803d2011-10-25 13:20:58 +0100292PUSH_USER=
293PUSH_NOTES=
294LAST_WORKING=
295SET_LAST_WORKING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100296ALLOW_EMPTY=
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500297KEEP_GOING=
Jan Holesovskya694de12010-11-16 15:00:48 +0100298REPORT_REPOS=1
Stephan Bergmann79e65ee2011-11-29 16:38:46 +0100299REPORT_COMMANDS=0
Norbert Thiebaud67dd44fb2011-05-01 03:26:45 -0500300REPORT_COMPACT=0
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500301DO_HOOK_REFRESH=false
Jan Holesovskya694de12010-11-16 15:00:48 +0100302
303while [ "${COMMAND:0:1}" = "-" ] ; do
304 case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500305 -f )KEEP_GOING="||:"
Michael Stahl175f17b2011-11-03 21:56:21 +0100306 ;;
307 -z)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500308 refresh_all_hooks
309 exit 0;
Michael Stahl175f17b2011-11-03 21:56:21 +0100310 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500311 --set-push-urls)
312 shift
313 PUSH_USER="$1"
314 if [ -n "${PUSH_USER}" ] ; then
315 PUSH_USER="${PUSH_USER}@"
316 fi
Jan Holesovskye1b59fc2012-11-23 20:58:27 +0100317 set_push_urls "$PUSH_USER"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500318 exit 0;
319 ;;
320 -*)
321 echo "option: $COMMAND not supported" 1>&2
322 exit 1
Jan Holesovskya694de12010-11-16 15:00:48 +0100323 esac
324 shift
325 COMMAND="$1"
326done
327
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500328shift
329
Jan Holesovskya694de12010-11-16 15:00:48 +0100330case "$COMMAND" in
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500331 branch)
332 do_git_cmd ${COMMAND} "$@"
333 ;;
334 checkout)
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500335 do_checkout "$@"
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500336 ;;
337 clone)
Miklos Vajna803137f2013-02-09 19:09:32 +0100338 do_init_modules && refresh_all_hooks
Jan Holesovskya694de12010-11-16 15:00:48 +0100339 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500340 fetch)
341 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
342
343 ;;
344 grep)
345 KEEP_GOING="||:"
346 do_git_cmd ${COMMAND} "$@"
347 ;;
348 pull)
349 git pull "$@" && git submodule update && refresh_all_hooks
350 ;;
Jan Holesovskya694de12010-11-16 15:00:48 +0100351 push)
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500352 git submodule foreach git push "$@"
353 if [ "$?" = "0" ] ; then
354 git push "$@"
355 fi
356 ;;
357 reset)
358 do_reset
359 ;;
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500360 tag)
361 do_git_cmd ${COMMAND} "$@"
362 ;;
Peter Foley41b5c802012-10-16 20:43:21 -0400363 "")
364 ;;
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500365 *)
Peter Foley41b5c802012-10-16 20:43:21 -0400366 echo "./g does not support command: $COMMAND" 1>&2
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500367 exit 1;
Jan Holesovskya694de12010-11-16 15:00:48 +0100368 ;;
369esac
370
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500371exit $?
Norbert Thiebaud71e52002011-08-27 23:41:18 -0500372
Jan Holesovskya694de12010-11-16 15:00:48 +0100373# vi:set shiftwidth=4 expandtab: