lok: avoid crash cleaning up help-windows with multiple views.
[LibreOffice.git] / g
blob746babe23539d6a7fe97f300e47b0b38e1e6e6bc
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 config_host.mk ] ; then
14 # we are in the BUILDDIR
15 SRC_ROOT=$(< 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|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 hook="${repo?}/.git/hooks/${hook_name##*/}"
42 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
43 rm -f "${hook?}"
44 ln -sf "${hook_name}" "${hook?}"
46 done
47 # override if need be by the submodules' own hooks
48 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
49 hook="${repo?}/.git/hooks/${hook_name##*/}"
50 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
51 rm -f "${hook?}"
52 ln -sf "${hook_name}" "${hook?}"
54 done
55 elif [ -d .git/modules/"${repo}"/hooks ] ; then
56 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
57 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
58 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
59 rm -f "${hook?}"
60 ln -sf "${hook_name}" "${hook?}"
62 done
63 # override if need be by the submodules' own hooks
64 for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
65 hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
66 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
67 rm -f "${hook?}"
68 ln -sf "${hook_name}" "${hook?}"
70 done
75 refresh_all_hooks()
77 local repo
78 local hook_name
79 local hook
81 pushd "${COREDIR?}" > /dev/null
82 for hook_name in "${COREDIR?}/.git-hooks"/* ; do
83 hook=".git/hooks/${hook_name##*/}"
84 if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
85 rm -f "${hook?}"
86 ln -sf "${hook_name}" "${hook?}"
88 done
90 for repo in ${SUBMODULES_ALL?} ; do
91 refresh_submodule_hooks "$repo"
92 done
93 popd > /dev/null
97 set_push_url()
99 local repo
101 repo="$1"
102 if [ -n "$repo" ] ; then
103 pushd "${COREDIR?}/${repo?}" > /dev/null
104 else
105 pushd "${COREDIR?}" > /dev/null
106 repo="core"
108 echo "setting up push url for ${repo?}"
109 if [ "${repo?}" = "helpcontent2" ] ; then
110 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
111 else
112 git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
114 popd > /dev/null
117 set_push_urls()
119 PUSH_USER="$1"
120 set_push_url
121 for repo in ${SUBMODULES_ACTIVE?} ; do
122 set_push_url "${repo?}"
123 done
126 get_active_submodules()
128 SUBMODULES_ACTIVE=""
129 local repo
131 for repo in ${SUBMODULES_ALL?} ; do
132 if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
133 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
135 done
138 get_configured_submodules()
140 SUBMODULES_CONFIGURED=""
141 if [ -f config_host.mk ] ; then
142 SUBMODULES_CONFIGURED=$(< config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
143 else
144 # if we need the configured submodule before the configuration is done. we assumed you want them all
145 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
149 get_git_reference()
151 REFERENCED_GIT=""
152 if [ -f config_host.mk ]; then
153 REFERENCED_GIT=$(< config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
155 LINKED_GIT=""
156 if [ -f config_host.mk ]; then
157 LINKED_GIT=$(< config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
161 do_shortcut_update()
163 local module
164 local repo
166 for module in $SUBMODULES_CONFIGURED ; do
167 if [ ! -d "${module?}"/.git ] ; then
168 case "${module?}" in
169 helpcontent2)
170 if [ -d clone/help/.git ] ; then
171 repo="clone/help/.git"
175 if [ -d clone/"${module?}"/.git ] ; then
176 repo="clone/${module?}/.git"
179 esac
180 if [ -n "$repo" ] ; then
181 cp -r "${repo?}" "${module?}/."
184 done
187 do_git_cmd()
189 echo "cmd:$*"
190 git "$@"
191 git submodule foreach git "$@" $KEEP_GOING
194 do_checkout()
196 local cmd
197 local create_branch="0"
198 local branch
199 local module
201 git checkout "$@" || return $?
202 for cmd in "$@" ; do
203 if [ "$cmd" = "-f" ]; then
204 continue
205 elif [ "$cmd" = "-b" ] ; then
206 create_branch=1
207 elif [ "$create_branch" = "1" ] ; then
208 branch="$cmd"
209 create_branch=0
211 done
212 if [ -f .gitmodules ] ; then
213 git submodule update
214 if [ -n "$branch" ] ; then
215 git submodule foreach git checkout -b "${branch}" HEAD || return $?
217 else
218 # now that is the nasty case we moved prior to submodules
219 # 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
224 # make sure we have the needed repo in clone
225 ./g clone && ./g -f checkout "$@" || return $?
227 return $?
230 do_reset()
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
237 # 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
242 # make sure we have the needed repo in clone
243 ./g clone && ./g -f reset "$@"
245 return $?;
248 do_init_modules()
250 local module
251 local configured
253 do_shortcut_update
255 for module in $SUBMODULES_CONFIGURED ; do
256 if [ -n "$LINKED_GIT" ] ; then
257 if ! [ -d ".git/modules/${module}" ]; then
258 ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
261 configured=$(git config --local --get submodule."${module}".url)
262 if [ -z "$configured" ] ; then
263 git submodule init "$module" || return $?
265 done
266 for module in $SUBMODULES_CONFIGURED ; do
267 if [ -n "$REFERENCED_GIT" ] ; then
268 git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" "$module" || return $?
269 else
270 git submodule update "$module" || return $?
272 done
273 return 0
277 # no params, no action
278 if [ "$#" -eq "0" ] ; then
279 usage
282 if [ ! "$(type -p git)" ]; then
283 echo "Cannot find the git binary! Is git installed and is in PATH?"
284 exit 1
288 get_active_submodules
289 get_configured_submodules
290 get_git_reference
295 # extra params for some commands, like log
296 EXTRA=
297 COMMAND="$1"
298 PAGER=
299 RELATIVIZE=1
300 PUSH_ALL=
301 PUSH_USER=
302 PUSH_NOTES=
303 LAST_WORKING=
304 SET_LAST_WORKING=
305 ALLOW_EMPTY=
306 KEEP_GOING=
307 REPORT_REPOS=1
308 REPORT_COMMANDS=0
309 REPORT_COMPACT=0
310 DO_HOOK_REFRESH=false
312 while [ "${COMMAND:0:1}" = "-" ] ; do
313 case "$COMMAND" in
314 -f )KEEP_GOING="||:"
317 refresh_all_hooks
318 exit 0;
320 --set-push-urls)
321 shift
322 PUSH_USER="$1"
323 if [ -n "${PUSH_USER}" ] ; then
324 PUSH_USER="${PUSH_USER}@"
326 set_push_urls "$PUSH_USER"
327 exit 0;
330 echo "option: $COMMAND not supported" 1>&2
331 exit 1
332 esac
333 shift
334 COMMAND="$1"
335 done
337 shift
339 case "$COMMAND" in
340 branch)
341 do_git_cmd "${COMMAND}" "$@"
343 checkout)
344 do_checkout "$@"
346 clone)
347 do_init_modules && refresh_all_hooks
349 fetch)
350 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
353 grep)
354 KEEP_GOING="||:"
355 do_git_cmd "${COMMAND}" "$@"
357 pull)
358 git pull "$@" && git submodule update && refresh_all_hooks
360 push)
361 git submodule foreach git push "$@"
362 if [ "$?" = "0" ] ; then
363 git push "$@"
366 reset)
367 do_reset
369 tag)
370 do_git_cmd "${COMMAND}" "$@"
375 echo "./g does not support command: $COMMAND" 1>&2
376 exit 1;
378 esac
380 exit $?
382 # vi:set shiftwidth=4 expandtab: