blob: e273171ae7c2517754541330f4540ff1e6d769c7 [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
Lionel Elie Mamaneaa1a9882013-06-21 13:31:37 +0200155 LINKED_GIT=""
156 if [ -f config_host.mk ]; then
157 LINKED_GIT=$(cat config_host.mk | grep GIT_LINK_SRC | sed -e "s/.*=//")
158 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
167 if [ ! -d ${module?}/.git ] ; then
168 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 *)
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
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{
189 echo "cmd:$@"
190 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
208 branch="$arg"
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
Norbert Thiebauda31714f2012-10-02 12:59:44 -0500215 git submodules 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"
222 rm -fr ${module}
223 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"
240 rm -fr ${module}
241 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
Norbert Thiebaud9152c422012-10-01 23:48:10 -0500261 configured=$(git config --local --get submodule.${module}.url)
262 if [ -z "$configured" ] ; then
263 git submodule init $module || return $?
264 fi
265 done
Miklos Vajna73b50b72013-02-26 16:17:51 +0100266 for module in $SUBMODULES_CONFIGURED ; do
267 if [ -n "$REFERENCED_GIT" ] ; then
Miklos Vajna803137f2013-02-09 19:09:32 +0100268 git submodule update --reference $REFERENCED_GIT/.git/modules/$module $module || return $?
Miklos Vajna73b50b72013-02-26 16:17:51 +0100269 else
270 git submodule update $module || return $?
271 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
Miklos Vajna889934e2011-09-05 20:45:43 +0200282if [ ! "`type -p git`" ]; then
283 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)
341 do_git_cmd ${COMMAND} "$@"
342 ;;
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="||:"
355 do_git_cmd ${COMMAND} "$@"
356 ;;
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)
370 do_git_cmd ${COMMAND} "$@"
371 ;;
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: