Avoid potential negative array index access to cached text.
[LibreOffice.git] / g
blob377890a3396aa372773aaa6a3d9ddfd4b662f67d
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 wingit
94 local gitbash
95 local lnkfile=".git/hooks/pre-commit"
97 pushd "${COREDIR?}" > /dev/null
99 # it is 'GIT for Windows'
100 wingit=$(git --version | grep -ic windows)
101 gitbash=$(echo $OSTYPE | grep -ic msys)
103 # In the win-git-bash, do not make links, it makes only copies
104 if [ $gitbash -eq 1 ]; then
105 if [ -d ".git" ]; then
106 if [ ! -e "${lnkfile}" ] || [ ! -L "${lnkfile}" ] ; then
107 # here when wrong link then the file not exist
108 echo "Your hooks not right, solve this in cygwin with"
109 echo " ./g -z"
112 else
113 if [ $wingit -eq 1 ]; then
114 # There's no ".git" e.g. in a secondary worktree
115 if [ -d ".git" ]; then
116 winlnk=0
117 if [ -e "${lnkfile}" ] && [ -L "${lnkfile}" ] ; then
118 # if linux-links or windows-links?
119 # dos dir output windows link:
120 # 04.09.2020 10:54 <SYMLINK> pre-commit [..\..\.git-hooks\pre-commit]
121 # dos dir output linux link:
122 # file not found
123 winlnk=$(cmd /C "DIR ${lnkfile//'/'/'\'}" 2>&1)
124 winlnk=$(echo "$winlnk" | grep -icE "<SYMLINK>.*${lnkfile##*/} \[")
127 if [ $winlnk -eq 0 ]; then
128 echo "You using GIT for Windows, but the hook-links not right, change with mklink"
129 cat .git-hooks/README
132 else
133 # There's no ".git" e.g. in a secondary worktree
134 if [ -d ".git" ]; then
135 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
136 hook=".git/hooks/${hook_name##*/}"
137 if [ ! -e "${hook?}" ] || [ ! -L "${hook?}" ] ; then
138 rm -f "${hook?}"
139 ln -sf "${hook_name}" "${hook?}"
141 done
144 for repo in ${SUBMODULES_ALL?} ; do
145 refresh_submodule_hooks "$repo"
146 done
150 popd > /dev/null
154 set_push_url()
156 local repo
158 repo="$1"
159 if [ -n "$repo" ] ; then
160 pushd "${COREDIR?}/${repo?}" > /dev/null
161 else
162 pushd "${COREDIR?}" > /dev/null
163 repo="core"
165 echo "setting up push url for ${repo?}"
166 if [ "${repo?}" = "helpcontent2" ] ; then
167 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
168 else
169 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
171 popd > /dev/null
174 set_push_urls()
176 PUSH_USER="$1"
177 set_push_url
178 for repo in ${SUBMODULES_ACTIVE?} ; do
179 set_push_url "${repo?}"
180 done
183 get_active_submodules()
185 SUBMODULES_ACTIVE=""
186 local repo
188 for repo in ${SUBMODULES_ALL?} ; do
189 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
190 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
192 done
195 get_configured_submodules()
197 SUBMODULES_CONFIGURED=""
198 if [ -f ${BUILDDIR}/config_host.mk ] ; then
199 SUBMODULES_CONFIGURED=$(< ${BUILDDIR}/config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
200 else
201 # if we need the configured submodule before the configuration is done. we assumed you want them all
202 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
206 get_git_reference()
208 REFERENCED_GIT=""
209 if [ -f ${BUILDDIR}/config_host.mk ]; then
210 REFERENCED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
212 LINKED_GIT=""
213 if [ -f ${BUILDDIR}/config_host.mk ]; then
214 LINKED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
218 do_shortcut_update()
220 local module
221 local repo
223 for module in $SUBMODULES_CONFIGURED ; do
224 if [ ! -d "${module?}"/.git ] ; then
225 case "${module?}" in
226 helpcontent2)
227 if [ -d clone/help/.git ] ; then
228 repo="clone/help/.git"
232 if [ -d clone/"${module?}"/.git ] ; then
233 repo="clone/${module?}/.git"
236 esac
237 if [ -n "$repo" ] ; then
238 cp -r "${repo?}" "${module?}/."
241 done
244 do_git_cmd()
246 echo "cmd:$*"
247 git "$@"
248 git submodule foreach git "$@" $KEEP_GOING
251 do_checkout()
253 local cmd
254 local create_branch="0"
255 local branch
256 local module
258 git checkout "$@" || return $?
259 for cmd in "$@" ; do
260 if [ "$cmd" = "-f" ]; then
261 continue
262 elif [ "$cmd" = "-b" ] ; then
263 create_branch=1
264 elif [ "$create_branch" = "1" ] ; then
265 branch="$cmd"
266 create_branch=0
268 done
269 if [ -f .gitmodules ] ; then
270 git submodule update --progress
271 if [ -n "$branch" ] ; then
272 git submodule foreach git checkout -b "${branch}" HEAD || return $?
274 else
275 # now that is the nasty case we moved prior to submodules
276 # delete the submodules left over if any
277 for module in $SUBMODULES_ALL ; do
278 echo "clean-up submodule $module"
279 rm -fr "${module}"
280 done
281 # make sure we have the needed repo in clone
282 ./g clone && ./g -f checkout "$@" || return $?
284 return $?
287 do_reset()
289 git reset "$@" || return $?
290 if [ -f .gitmodules ] ; then
291 git submodule update --progress || return $?
292 else
293 # now that is the nasty case we moved prior to submodules
294 # delete the submodules left over if any
295 for module in $SUBMODULES_ALL ; do
296 echo "clean-up submodule $module"
297 rm -fr "${module}"
298 done
299 # make sure we have the needed repo in clone
300 ./g clone && ./g -f reset "$@"
302 return $?;
305 do_init_modules()
307 local module
308 local configured
310 do_shortcut_update
312 for module in $SUBMODULES_CONFIGURED ; do
313 if [ -n "$LINKED_GIT" ] ; then
314 if ! [ -d ".git/modules/${module}" ]; then
315 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
318 configured=$(git config --local --get submodule."${module}".url)
319 if [ -z "$configured" ] ; then
320 git submodule init "$module" || return $?
322 done
323 for module in $SUBMODULES_CONFIGURED ; do
324 if [ -n "$REFERENCED_GIT" ] ; then
325 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" --progress "$module" || return $?
326 else
327 git submodule update --progress "$module" || return $?
329 done
330 return 0
334 # no params, no action
335 if [ "$#" -eq "0" ] ; then
336 usage
340 if [ ! "$(type -p git)" ]; then
341 echo "Cannot find the git binary! Is git installed and is in PATH?"
342 exit 1
346 get_active_submodules
347 get_configured_submodules
348 get_git_reference
353 # extra params for some commands, like log
354 EXTRA=
355 COMMAND="$1"
356 PAGER=
357 RELATIVIZE=1
358 PUSH_ALL=
359 PUSH_USER=
360 PUSH_NOTES=
361 LAST_WORKING=
362 SET_LAST_WORKING=
363 ALLOW_EMPTY=
364 KEEP_GOING=
365 REPORT_REPOS=1
366 REPORT_COMMANDS=0
367 REPORT_COMPACT=0
368 DO_HOOK_REFRESH=false
371 while [ "${COMMAND:0:1}" = "-" ] ; do
372 case "$COMMAND" in
373 -f )KEEP_GOING="||:"
376 refresh_all_hooks
377 exit 0;
379 --set-push-urls)
380 shift
381 PUSH_USER="$1"
382 if [ -n "${PUSH_USER}" ] ; then
383 PUSH_USER="${PUSH_USER}@"
385 set_push_urls "$PUSH_USER"
386 exit 0;
389 echo "option: $COMMAND not supported" 1>&2
390 exit 1
391 esac
392 shift
393 COMMAND="$1"
394 done
396 shift
398 case "$COMMAND" in
399 branch)
400 do_git_cmd "${COMMAND}" "$@"
402 checkout)
403 do_checkout "$@"
405 clone)
406 do_init_modules && refresh_all_hooks
408 fetch)
409 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update --progress
413 (git gc "$@" && git submodule foreach git gc "$@" )
415 grep)
416 KEEP_GOING="||:"
417 do_git_cmd "${COMMAND}" "$@"
419 pull)
420 git pull "$@" && git submodule update --progress && refresh_all_hooks
422 push)
423 git submodule foreach git push "$@"
424 if [ "$?" = "0" ] ; then
425 git push "$@"
428 reset)
429 do_reset
431 tag)
432 do_git_cmd "${COMMAND}" "$@"
437 echo "./g does not support command: $COMMAND" 1>&2
438 exit 1;
440 esac
442 exit $?
444 # vi:set shiftwidth=4 expandtab: