Update git submodules
[LibreOffice.git] / g
blobb018561c677566eedc25c668528795ae31ffa177
1 #!/usr/bin/env bash
3 # Wrapper for git to handle more subdirs at the same time
6 if [ -n "$g_debug" ] ; then
7 set -x
8 fi
10 SUBMODULES_ALL="dictionaries helpcontent2 translations"
12 pushd $(dirname $0) > /dev/null
13 if [ -f ${BUILDDIR}/config_host.mk ] ; then
14 # we are in the SRCDIR
15 SRC_ROOT=$(< ${BUILDDIR}/config_host.mk grep -a SRC_ROOT | sed -e "s/.*=//")
16 else
17 SRC_ROOT=$(pwd)
19 popd > /dev/null
21 COREDIR="$SRC_ROOT"
23 usage()
25 git
26 echo
27 echo "Usage: g [options] [git (checkout|clone|fetch|gc|grep|pull|push|reset) [git options/args..]]"
28 echo ""
29 echo " -z restore the git hooks and do other sanity checks"
32 refresh_submodule_hooks()
34 local repo=$1
35 local hook
36 local hook_name
38 if [ -d "${repo?}"/.git ] ; then
39 # use core's hook by default
40 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
41 if [ ! -e "${hook_name}" ] ; then
42 continue
44 hook="${repo?}/.git/hooks/${hook_name##*/}"
45 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
46 rm -f "${hook?}"
47 ln -sf "${hook_name}" "${hook?}"
49 done
50 # override if need be by the submodules' own hooks
51 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
52 if [ ! -e "${hook_name}" ] ; then
53 continue
55 hook="${repo?}/.git/hooks/${hook_name##*/}"
56 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
57 rm -f "${hook?}"
58 ln -sf "${hook_name}" "${hook?}"
60 done
61 elif [ -d .git/modules/"${repo}"/hooks ] ; then
62 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
63 if [ ! -e "${hook_name}" ] ; then
64 continue
66 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
67 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
68 rm -f "${hook?}"
69 ln -sf "${hook_name}" "${hook?}"
71 done
72 # override if need be by the submodules' own hooks
73 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
74 if [ ! -e "${hook_name}" ] ; then
75 continue
77 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
78 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
79 rm -f "${hook?}"
80 ln -sf "${hook_name}" "${hook?}"
82 done
87 refresh_all_hooks()
89 local repo
90 local hook_name
91 local hook
93 pushd "${COREDIR?}" > /dev/null
94 # There's no ".git" e.g. in a secondary worktree
95 if [ -d ".git" ]; then
96 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
97 hook=".git/hooks/${hook_name##*/}"
98 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
99 rm -f "${hook?}"
100 ln -sf "${hook_name}" "${hook?}"
102 done
105 for repo in ${SUBMODULES_ALL?} ; do
106 refresh_submodule_hooks "$repo"
107 done
108 popd > /dev/null
112 set_push_url()
114 local repo
116 repo="$1"
117 if [ -n "$repo" ] ; then
118 pushd "${COREDIR?}/${repo?}" > /dev/null
119 else
120 pushd "${COREDIR?}" > /dev/null
121 repo="core"
123 echo "setting up push url for ${repo?}"
124 if [ "${repo?}" = "helpcontent2" ] ; then
125 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
126 else
127 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
129 popd > /dev/null
132 set_push_urls()
134 PUSH_USER="$1"
135 set_push_url
136 for repo in ${SUBMODULES_ACTIVE?} ; do
137 set_push_url "${repo?}"
138 done
141 get_active_submodules()
143 SUBMODULES_ACTIVE=""
144 local repo
146 for repo in ${SUBMODULES_ALL?} ; do
147 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
148 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
150 done
153 get_configured_submodules()
155 SUBMODULES_CONFIGURED=""
156 if [ -f ${BUILDDIR}/config_host.mk ] ; then
157 SUBMODULES_CONFIGURED=$(< ${BUILDDIR}/config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
158 else
159 # if we need the configured submodule before the configuration is done. we assumed you want them all
160 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
164 get_git_reference()
166 REFERENCED_GIT=""
167 if [ -f ${BUILDDIR}/config_host.mk ]; then
168 REFERENCED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
170 LINKED_GIT=""
171 if [ -f ${BUILDDIR}/config_host.mk ]; then
172 LINKED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
176 do_shortcut_update()
178 local module
179 local repo
181 for module in $SUBMODULES_CONFIGURED ; do
182 if [ ! -d "${module?}"/.git ] ; then
183 case "${module?}" in
184 helpcontent2)
185 if [ -d clone/help/.git ] ; then
186 repo="clone/help/.git"
190 if [ -d clone/"${module?}"/.git ] ; then
191 repo="clone/${module?}/.git"
194 esac
195 if [ -n "$repo" ] ; then
196 cp -r "${repo?}" "${module?}/."
199 done
202 do_git_cmd()
204 echo "cmd:$*"
205 git "$@"
206 git submodule foreach git "$@" $KEEP_GOING
209 do_checkout()
211 local cmd
212 local create_branch="0"
213 local branch
214 local module
216 git checkout "$@" || return $?
217 for cmd in "$@" ; do
218 if [ "$cmd" = "-f" ]; then
219 continue
220 elif [ "$cmd" = "-b" ] ; then
221 create_branch=1
222 elif [ "$create_branch" = "1" ] ; then
223 branch="$cmd"
224 create_branch=0
226 done
227 if [ -f .gitmodules ] ; then
228 git submodule update --progress
229 if [ -n "$branch" ] ; then
230 git submodule foreach git checkout -b "${branch}" HEAD || return $?
232 else
233 # now that is the nasty case we moved prior to submodules
234 # delete the submodules left over if any
235 for module in $SUBMODULES_ALL ; do
236 echo "clean-up submodule $module"
237 rm -fr "${module}"
238 done
239 # make sure we have the needed repo in clone
240 ./g clone && ./g -f checkout "$@" || return $?
242 return $?
245 do_reset()
247 git reset "$@" || return $?
248 if [ -f .gitmodules ] ; then
249 git submodule update --progress || return $?
250 else
251 # now that is the nasty case we moved prior to submodules
252 # delete the submodules left over if any
253 for module in $SUBMODULES_ALL ; do
254 echo "clean-up submodule $module"
255 rm -fr "${module}"
256 done
257 # make sure we have the needed repo in clone
258 ./g clone && ./g -f reset "$@"
260 return $?;
263 do_init_modules()
265 local module
266 local configured
268 do_shortcut_update
270 for module in $SUBMODULES_CONFIGURED ; do
271 if [ -n "$LINKED_GIT" ] ; then
272 if ! [ -d ".git/modules/${module}" ]; then
273 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
276 configured=$(git config --local --get submodule."${module}".url)
277 if [ -z "$configured" ] ; then
278 git submodule init "$module" || return $?
280 done
281 for module in $SUBMODULES_CONFIGURED ; do
282 if [ -n "$REFERENCED_GIT" ] ; then
283 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" --progress "$module" || return $?
284 else
285 git submodule update --progress "$module" || return $?
287 done
288 return 0
292 # no params, no action
293 if [ "$#" -eq "0" ] ; then
294 usage
297 if [ ! "$(type -p git)" ]; then
298 echo "Cannot find the git binary! Is git installed and is in PATH?"
299 exit 1
303 get_active_submodules
304 get_configured_submodules
305 get_git_reference
310 # extra params for some commands, like log
311 EXTRA=
312 COMMAND="$1"
313 PAGER=
314 RELATIVIZE=1
315 PUSH_ALL=
316 PUSH_USER=
317 PUSH_NOTES=
318 LAST_WORKING=
319 SET_LAST_WORKING=
320 ALLOW_EMPTY=
321 KEEP_GOING=
322 REPORT_REPOS=1
323 REPORT_COMMANDS=0
324 REPORT_COMPACT=0
325 DO_HOOK_REFRESH=false
327 while [ "${COMMAND:0:1}" = "-" ] ; do
328 case "$COMMAND" in
329 -f )KEEP_GOING="||:"
332 refresh_all_hooks
333 exit 0;
335 --set-push-urls)
336 shift
337 PUSH_USER="$1"
338 if [ -n "${PUSH_USER}" ] ; then
339 PUSH_USER="${PUSH_USER}@"
341 set_push_urls "$PUSH_USER"
342 exit 0;
345 echo "option: $COMMAND not supported" 1>&2
346 exit 1
347 esac
348 shift
349 COMMAND="$1"
350 done
352 shift
354 case "$COMMAND" in
355 branch)
356 do_git_cmd "${COMMAND}" "$@"
358 checkout)
359 do_checkout "$@"
361 clone)
362 do_init_modules && refresh_all_hooks
364 fetch)
365 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update --progress
369 (git gc "$@" && git submodule foreach git gc "$@" )
371 grep)
372 KEEP_GOING="||:"
373 do_git_cmd "${COMMAND}" "$@"
375 pull)
376 git pull "$@" && git submodule update --progress && refresh_all_hooks
378 push)
379 git submodule foreach git push "$@"
380 if [ "$?" = "0" ] ; then
381 git push "$@"
384 reset)
385 do_reset
387 tag)
388 do_git_cmd "${COMMAND}" "$@"
393 echo "./g does not support command: $COMMAND" 1>&2
394 exit 1;
396 esac
398 exit $?
400 # vi:set shiftwidth=4 expandtab: