a11y: Drop unnecessary null check
[LibreOffice.git] / g
blob6363ce5666eb5f0b26e37db8ac55e9e3883696d7
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_create_link()
34 local hook_name=$1
35 local hook=$2
36 local lnarg=$3
38 # if it doesn't exist or is neither a symlink nor sharing the same inode (hardlink)
39 if [ ! -e "${hook?}" ] || [ ! \( -L "${hook?}" -o "${hook_name}" -ef "${hook?}" \) ] ; then
40 rm -f "${hook?}"
41 ln -f $lnarg "${hook_name}" "${hook?}"
45 refresh_submodule_hooks()
47 local repo=$1
48 local lnarg=$2
49 local hook
50 local hook_name
52 if [ -d "${repo?}"/.git ] ; then
53 # use core's hook by default
54 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
55 if [ ! -e "${hook_name}" ] ; then
56 continue
58 hook="${repo?}/.git/hooks/${hook_name##*/}"
59 refresh_create_link "${hook_name}" "${hook?}" "$lnarg"
60 done
61 # override if need be by the submodules' own hooks
62 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
63 if [ ! -e "${hook_name}" ] ; then
64 continue
66 hook="${repo?}/.git/hooks/${hook_name##*/}"
67 refresh_create_link "${hook_name}" "${hook?}" "$lnarg"
68 done
69 elif [ -d .git/modules/"${repo}"/hooks ] ; then
70 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
71 if [ ! -e "${hook_name}" ] ; then
72 continue
74 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
75 refresh_create_link "${hook_name}" "${hook?}" "$lnarg"
76 done
77 # override if need be by the submodules' own hooks
78 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
79 if [ ! -e "${hook_name}" ] ; then
80 continue
82 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
83 refresh_create_link "${hook_name}" "${hook?}" "$lnarg"
84 done
89 refresh_all_hooks()
91 local repo
92 local hook_name
93 local hook
94 local gitbash
95 local lnarg
97 pushd "${COREDIR?}" > /dev/null
99 # it is 'GIT for Windows'
100 gitbash=$(echo $OSTYPE | grep -ic msys)
102 # git-bash/MSYS doesn't create symlinks by default, and "real" symlinks are restricted to
103 # Admin-mode or when devmode is activated, junction points as fallback would work for bash/
104 # regular use but not when git tries to spawn them, similar for plain windows shortcuts (worse
105 # because running the hooks will fail silently/they'd be inactive)
106 # ln -s without setting MSYS to contain winsymlinks:{lnk,native,nativestrict,sys} to force one
107 # of the other modes described above will do plain copies.
108 # So in case of git-bash use hardlinks since those work just fine, everywhere else use symlinks
109 if [ $gitbash -ne 1 ]; then
110 lnarg="-s"
112 # There's no ".git" e.g. in a secondary worktree
113 if [ -d ".git" ]; then
114 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
115 hook=".git/hooks/${hook_name##*/}"
116 refresh_create_link "${hook_name}" "${hook?}" "$lnarg"
117 done
120 for repo in ${SUBMODULES_ALL?} ; do
121 refresh_submodule_hooks "$repo" "$lnarg"
122 done
124 popd > /dev/null
128 set_push_url()
130 local repo
132 repo="$1"
133 if [ -n "$repo" ] ; then
134 pushd "${COREDIR?}/${repo?}" > /dev/null
135 else
136 pushd "${COREDIR?}" > /dev/null
137 repo="core"
139 echo "setting up push url for ${repo?}"
140 if [ "${repo?}" = "helpcontent2" ] ; then
141 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
142 else
143 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
145 popd > /dev/null
148 set_push_urls()
150 PUSH_USER="$1"
151 set_push_url
152 for repo in ${SUBMODULES_ACTIVE?} ; do
153 set_push_url "${repo?}"
154 done
157 get_active_submodules()
159 SUBMODULES_ACTIVE=""
160 local repo
162 for repo in ${SUBMODULES_ALL?} ; do
163 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
164 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
166 done
169 get_configured_submodules()
171 SUBMODULES_CONFIGURED=""
172 if [ -f ${BUILDDIR}/config_host.mk ] ; then
173 SUBMODULES_CONFIGURED=$(< ${BUILDDIR}/config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
174 else
175 # if we need the configured submodule before the configuration is done. we assumed you want them all
176 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
180 get_git_reference()
182 REFERENCED_GIT=""
183 if [ -f ${BUILDDIR}/config_host.mk ]; then
184 REFERENCED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
186 LINKED_GIT=""
187 if [ -f ${BUILDDIR}/config_host.mk ]; then
188 LINKED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
192 do_shortcut_update()
194 local module
195 local repo
197 for module in $SUBMODULES_CONFIGURED ; do
198 if [ ! -d "${module?}"/.git ] ; then
199 case "${module?}" in
200 helpcontent2)
201 if [ -d clone/help/.git ] ; then
202 repo="clone/help/.git"
206 if [ -d clone/"${module?}"/.git ] ; then
207 repo="clone/${module?}/.git"
210 esac
211 if [ -n "$repo" ] ; then
212 cp -r "${repo?}" "${module?}/."
215 done
218 do_git_cmd()
220 echo "cmd:$*"
221 git "$@"
222 git submodule foreach git "$@" $KEEP_GOING
225 do_checkout()
227 local cmd
228 local create_branch="0"
229 local branch
230 local module
232 git checkout "$@" || return $?
233 for cmd in "$@" ; do
234 if [ "$cmd" = "-f" ]; then
235 continue
236 elif [ "$cmd" = "-b" ] ; then
237 create_branch=1
238 elif [ "$create_branch" = "1" ] ; then
239 branch="$cmd"
240 create_branch=0
242 done
243 if [ -f .gitmodules ] ; then
244 git submodule update --progress
245 if [ -n "$branch" ] ; then
246 git submodule foreach git checkout -b "${branch}" HEAD || return $?
248 else
249 # now that is the nasty case we moved prior to submodules
250 # delete the submodules left over if any
251 for module in $SUBMODULES_ALL ; do
252 echo "clean-up submodule $module"
253 rm -fr "${module}"
254 done
255 # make sure we have the needed repo in clone
256 ./g clone && ./g -f checkout "$@" || return $?
258 return $?
261 do_reset()
263 git reset "$@" || return $?
264 if [ -f .gitmodules ] ; then
265 git submodule update --progress || return $?
266 else
267 # now that is the nasty case we moved prior to submodules
268 # delete the submodules left over if any
269 for module in $SUBMODULES_ALL ; do
270 echo "clean-up submodule $module"
271 rm -fr "${module}"
272 done
273 # make sure we have the needed repo in clone
274 ./g clone && ./g -f reset "$@"
276 return $?;
279 do_init_modules()
281 local module
282 local configured
284 do_shortcut_update
286 for module in $SUBMODULES_CONFIGURED ; do
287 if [ -n "$LINKED_GIT" ] ; then
288 if ! [ -d ".git/modules/${module}" ]; then
289 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
292 configured=$(git config --local --get submodule."${module}".url)
293 if [ -z "$configured" ] ; then
294 git submodule init "$module" || return $?
296 done
297 for module in $SUBMODULES_CONFIGURED ; do
298 if [ -n "$REFERENCED_GIT" ] ; then
299 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" --progress "$module" || return $?
300 else
301 git submodule update --progress "$module" || return $?
303 done
304 return 0
308 # no params, no action
309 if [ "$#" -eq "0" ] ; then
310 usage
314 if [ ! "$(type -p git)" ]; then
315 echo "Cannot find the git binary! Is git installed and is in PATH?"
316 exit 1
320 get_active_submodules
321 get_configured_submodules
322 get_git_reference
327 # extra params for some commands, like log
328 EXTRA=
329 COMMAND="$1"
330 PAGER=
331 RELATIVIZE=1
332 PUSH_ALL=
333 PUSH_USER=
334 PUSH_NOTES=
335 LAST_WORKING=
336 SET_LAST_WORKING=
337 ALLOW_EMPTY=
338 KEEP_GOING=
339 REPORT_REPOS=1
340 REPORT_COMMANDS=0
341 REPORT_COMPACT=0
342 DO_HOOK_REFRESH=false
345 while [ "${COMMAND:0:1}" = "-" ] ; do
346 case "$COMMAND" in
347 -f )KEEP_GOING="||:"
350 refresh_all_hooks
351 exit 0;
353 --set-push-urls)
354 shift
355 PUSH_USER="$1"
356 if [ -n "${PUSH_USER}" ] ; then
357 PUSH_USER="${PUSH_USER}@"
359 set_push_urls "$PUSH_USER"
360 exit 0;
363 echo "option: $COMMAND not supported" 1>&2
364 exit 1
365 esac
366 shift
367 COMMAND="$1"
368 done
370 shift
372 case "$COMMAND" in
373 branch)
374 do_git_cmd "${COMMAND}" "$@"
376 checkout)
377 do_checkout "$@"
379 clone)
380 do_init_modules && refresh_all_hooks
382 fetch)
383 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update --progress
387 (git gc "$@" && git submodule foreach git gc "$@" )
389 grep)
390 KEEP_GOING="||:"
391 do_git_cmd "${COMMAND}" "$@"
393 pull)
394 git pull "$@" && git submodule update --progress && refresh_all_hooks
396 push)
397 git submodule foreach git push "$@"
398 if [ "$?" = "0" ] ; then
399 git push "$@"
402 reset)
403 do_reset
405 tag)
406 do_git_cmd "${COMMAND}" "$@"
411 echo "./g does not support command: $COMMAND" 1>&2
412 exit 1;
414 esac
416 exit $?
418 # vi:set shiftwidth=4 expandtab: