Resolves: tdf#154167 ERROR.TYPE(#NULL!) must return 1
[LibreOffice.git] / g
bloba9a879f5d9075da8d97ae54700017a5f327462a5
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
92 local winlnk
93 local lnkfile=".git/hooks/pre-commit"
95 pushd "${COREDIR?}" > /dev/null
97 if [ $WINGIT -eq 1 ]; then
98 winlnk=0
99 if [ -e "${lnkfile}" ] && [ -L "${lnkfile}" ] ; then
100 # if linux-links or windows-links?
101 # dos dir output windows link:
102 # 04.09.2020 10:54 <SYMLINK> pre-commit [..\..\.git-hooks\pre-commit]
103 # dos dir output linux link:
104 # file not found
105 winlnk=$(cmd /C "DIR ${lnkfile//'/'/'\'}" 2>&1)
106 winlnk=$(echo "$winlnk" | grep -icE "<SYMLINK>.*${lnkfile##*/} \[")
109 if [ $winlnk -eq 0 ]; then
110 echo "You using GIT for Windows, but the hook-links not right, change with mklink"
111 cat .git-hooks/README
113 else
114 # There's no ".git" e.g. in a secondary worktree
115 if [ -d ".git" ]; then
116 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
117 hook=".git/hooks/${hook_name##*/}"
118 if [ ! -e "${hook?}" ] || [ ! -L "${hook?}" ] ; then
119 rm -f "${hook?}"
120 ln -sf "${hook_name}" "${hook?}"
122 done
125 for repo in ${SUBMODULES_ALL?} ; do
126 refresh_submodule_hooks "$repo"
127 done
130 popd > /dev/null
134 set_push_url()
136 local repo
138 repo="$1"
139 if [ -n "$repo" ] ; then
140 pushd "${COREDIR?}/${repo?}" > /dev/null
141 else
142 pushd "${COREDIR?}" > /dev/null
143 repo="core"
145 echo "setting up push url for ${repo?}"
146 if [ "${repo?}" = "helpcontent2" ] ; then
147 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
148 else
149 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
151 popd > /dev/null
154 set_push_urls()
156 PUSH_USER="$1"
157 set_push_url
158 for repo in ${SUBMODULES_ACTIVE?} ; do
159 set_push_url "${repo?}"
160 done
163 get_active_submodules()
165 SUBMODULES_ACTIVE=""
166 local repo
168 for repo in ${SUBMODULES_ALL?} ; do
169 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
170 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
172 done
175 get_configured_submodules()
177 SUBMODULES_CONFIGURED=""
178 if [ -f ${BUILDDIR}/config_host.mk ] ; then
179 SUBMODULES_CONFIGURED=$(< ${BUILDDIR}/config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
180 else
181 # if we need the configured submodule before the configuration is done. we assumed you want them all
182 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
186 get_git_reference()
188 REFERENCED_GIT=""
189 if [ -f ${BUILDDIR}/config_host.mk ]; then
190 REFERENCED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
192 LINKED_GIT=""
193 if [ -f ${BUILDDIR}/config_host.mk ]; then
194 LINKED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
198 do_shortcut_update()
200 local module
201 local repo
203 for module in $SUBMODULES_CONFIGURED ; do
204 if [ ! -d "${module?}"/.git ] ; then
205 case "${module?}" in
206 helpcontent2)
207 if [ -d clone/help/.git ] ; then
208 repo="clone/help/.git"
212 if [ -d clone/"${module?}"/.git ] ; then
213 repo="clone/${module?}/.git"
216 esac
217 if [ -n "$repo" ] ; then
218 cp -r "${repo?}" "${module?}/."
221 done
224 do_git_cmd()
226 echo "cmd:$*"
227 git "$@"
228 git submodule foreach git "$@" $KEEP_GOING
231 do_checkout()
233 local cmd
234 local create_branch="0"
235 local branch
236 local module
238 git checkout "$@" || return $?
239 for cmd in "$@" ; do
240 if [ "$cmd" = "-f" ]; then
241 continue
242 elif [ "$cmd" = "-b" ] ; then
243 create_branch=1
244 elif [ "$create_branch" = "1" ] ; then
245 branch="$cmd"
246 create_branch=0
248 done
249 if [ -f .gitmodules ] ; then
250 git submodule update --progress
251 if [ -n "$branch" ] ; then
252 git submodule foreach git checkout -b "${branch}" HEAD || return $?
254 else
255 # now that is the nasty case we moved prior to submodules
256 # delete the submodules left over if any
257 for module in $SUBMODULES_ALL ; do
258 echo "clean-up submodule $module"
259 rm -fr "${module}"
260 done
261 # make sure we have the needed repo in clone
262 ./g clone && ./g -f checkout "$@" || return $?
264 return $?
267 do_reset()
269 git reset "$@" || return $?
270 if [ -f .gitmodules ] ; then
271 git submodule update --progress || return $?
272 else
273 # now that is the nasty case we moved prior to submodules
274 # delete the submodules left over if any
275 for module in $SUBMODULES_ALL ; do
276 echo "clean-up submodule $module"
277 rm -fr "${module}"
278 done
279 # make sure we have the needed repo in clone
280 ./g clone && ./g -f reset "$@"
282 return $?;
285 do_init_modules()
287 local module
288 local configured
290 do_shortcut_update
292 for module in $SUBMODULES_CONFIGURED ; do
293 if [ -n "$LINKED_GIT" ] ; then
294 if ! [ -d ".git/modules/${module}" ]; then
295 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
298 configured=$(git config --local --get submodule."${module}".url)
299 if [ -z "$configured" ] ; then
300 git submodule init "$module" || return $?
302 done
303 for module in $SUBMODULES_CONFIGURED ; do
304 if [ -n "$REFERENCED_GIT" ] ; then
305 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" --progress "$module" || return $?
306 else
307 git submodule update --progress "$module" || return $?
309 done
310 return 0
314 # no params, no action
315 if [ "$#" -eq "0" ] ; then
316 usage
319 gitfile="$(type -p git)"
321 if [ ! "${gitfile}" ]; then
322 echo "Cannot find the git binary! Is git installed and is in PATH?"
323 exit 1
327 get_active_submodules
328 get_configured_submodules
329 get_git_reference
334 # extra params for some commands, like log
335 EXTRA=
336 COMMAND="$1"
337 PAGER=
338 RELATIVIZE=1
339 PUSH_ALL=
340 PUSH_USER=
341 PUSH_NOTES=
342 LAST_WORKING=
343 SET_LAST_WORKING=
344 ALLOW_EMPTY=
345 KEEP_GOING=
346 REPORT_REPOS=1
347 REPORT_COMMANDS=0
348 REPORT_COMPACT=0
349 DO_HOOK_REFRESH=false
350 WINGIT=
352 # it is 'GIT for Windows'
353 WINGIT=$(echo $gitfile | grep -c cygdrive)
354 if [ $WINGIT -eq 1 ]; then
355 gitfile=$(git --version)
356 WINGIT=$(echo $gitfile | grep -ic windows)
359 while [ "${COMMAND:0:1}" = "-" ] ; do
360 case "$COMMAND" in
361 -f )KEEP_GOING="||:"
364 refresh_all_hooks
365 exit 0;
367 --set-push-urls)
368 shift
369 PUSH_USER="$1"
370 if [ -n "${PUSH_USER}" ] ; then
371 PUSH_USER="${PUSH_USER}@"
373 set_push_urls "$PUSH_USER"
374 exit 0;
377 echo "option: $COMMAND not supported" 1>&2
378 exit 1
379 esac
380 shift
381 COMMAND="$1"
382 done
384 shift
386 case "$COMMAND" in
387 branch)
388 do_git_cmd "${COMMAND}" "$@"
390 checkout)
391 do_checkout "$@"
393 clone)
394 do_init_modules && refresh_all_hooks
396 fetch)
397 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update --progress
401 (git gc "$@" && git submodule foreach git gc "$@" )
403 grep)
404 KEEP_GOING="||:"
405 do_git_cmd "${COMMAND}" "$@"
407 pull)
408 git pull "$@" && git submodule update --progress && refresh_all_hooks
410 push)
411 git submodule foreach git push "$@"
412 if [ "$?" = "0" ] ; then
413 git push "$@"
416 reset)
417 do_reset
419 tag)
420 do_git_cmd "${COMMAND}" "$@"
425 echo "./g does not support command: $COMMAND" 1>&2
426 exit 1;
428 esac
430 exit $?
432 # vi:set shiftwidth=4 expandtab: