Bump for 4.0-15
[LibreOffice.git] / g
blobc99519b08bcaadb78f4cae57834e7e75e12aa7b8
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 COREDIR=$(pwd)
14 popd > /dev/null
16 usage()
18 git
19 echo
20 echo "Usage: g [options] [git (checkout|clone|fetch|grep|pull|push|reset) [git options/args..]]"
21 echo ""
22 echo " -z restore the git hooks and do other sanity checks"
25 refresh_submodule_hooks()
27 local repo=$1
28 local hook
29 local hook_name
31 if [ -d ${repo?}/.git ] ; then
32 # use core's hook by default
33 for hook_name in $(ls -1 ${COREDIR?}/.git-hooks) ; do
34 hook="${repo?}/.git/hooks/${hook_name?}"
35 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
36 rm -f "${hook?}"
37 ln -sf "${COREDIR?}/.git-hooks/${hook_name?}" "${hook?}"
39 done
40 # override if need be by the submodules' own hooks
41 for hook_name in $(ls -1 ${COREDIR?}/${repo?}/.git-hooks 2>/dev/null) ; do
42 hook="${repo?}/.git/hooks/${hook_name?}"
43 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
44 rm -f "${hook?}"
45 ln -sf "${COREDIR?}/${repo?}/.git-hooks/${hook_name?}" "${hook?}"
47 done
51 refresh_all_hooks()
53 local repo
54 local hook_name
55 local hook
57 pushd ${COREDIR?} > /dev/null
58 for hook_name in $(ls -1 ${COREDIR?}/.git-hooks) ; do
59 hook=".git/hooks/${hook_name?}"
60 if [ ! -e "${hook?}" -o -L "${hook?}" ] ; then
61 rm -f "${hook?}"
62 ln -sf "${COREDIR?}/.git-hooks/${hook_name?}" "${hook?}"
64 done
66 for repo in ${SUBMODULES_ALL?} ; do
67 refresh_submodule_hooks $repo
68 done
69 popd > /dev/null
73 set_push_url()
75 local repo
77 repo="$1"
78 if [ -n "$repo" ] ; then
79 pushd "${COREDIR?}/${repo?}" > /dev/null
80 else
81 pushd "${COREDIR?}" > /dev/null
82 repo="core"
84 echo "setting up push url for ${repo?}"
85 if [ "${repo?}" = "helpcontent2" ] ; then
86 git config remote.origin.pushurl "ssh://${PUSH_USER}gerrit.libreoffice.org:29418/help"
87 else
88 git config remote.origin.pushurl "ssh://${PUSH_USER}gerrit.libreoffice.org:29418/${repo?}"
90 popd > /dev/null
93 set_push_urls()
95 PUSH_USER="$1"
96 set_push_url
97 for repo in ${SUBMODULES_ACTIVE?} ; do
98 set_push_url "${repo?}"
99 done
102 get_active_submodules()
104 SUBMODULES_ACTIVE=""
105 local repo
107 for repo in ${SUBMODULES_ALL?} ; do
108 if [ -d ${repo?}/.git ] ; then
109 SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
111 done
114 get_configured_submodules()
116 SUBMODULES_CONFIGURED=""
117 if [ -f "config_host.mk" ] ; then
118 SUBMODULES_CONFIGURED=$(cat config_host.mk | grep GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
119 else
120 # if we need the configured submoduel before the configuration is done. we assumed you want them all
121 SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
125 get_git_reference()
127 REFERENCED_GIT=""
128 if [ -f config_host.mk ]; then
129 REFERENCED_GIT=$(cat config_host.mk | grep GIT_REFERENCE_SRC | sed -e "s/.*=//")
133 do_shortcut_update()
135 local module
136 local repo
138 for module in $SUBMODULES_CONFIGURED ; do
139 if [ ! -d ${module?}/.git ] ; then
140 case "${module?}" in
141 helpcontent2)
142 if [ -d clone/help/.git ] ; then
143 repo="clone/help/.git"
147 if [ -d clone/${module?}/.git ] ; then
148 repo="clone/${module?}/.git"
151 esac
152 if [ -n "$repo" ] ; then
153 cp -r "${repo?}" "${module?}/."
156 done
159 do_git_cmd()
161 echo "cmd:$@"
162 git "$@"
163 git submodule foreach git "$@" $KEEP_GOING
166 do_checkout()
168 local cmd
169 local create_branch="0"
170 local branch
171 local module
173 git checkout "$@" || return $?
174 for cmd in "$@" ; do
175 if [ "$cmd" = "-f" ]; then
176 return 0
177 elif [ "$cmd" = "-b" ] ; then
178 create_branch=1
179 elif [ "$create_branch" = "1" ] ; then
180 branch="$arg"
181 create_branch=0
183 done
184 if [ -f .gitmodules ] ; then
185 git submodule update
186 if [ -n "$branch" ] ; then
187 git submodules foreach git checkout -b ${branch} HEAD || return $?
189 else
190 # now that is the nasty case we moved prior to submodules
191 # delete the submodules left over if any
192 for module in $SUBMODULES_ALL ; do
193 echo "clean-up submodule $module"
194 rm -fr ${module}
195 done
196 # make sure we have the needed repo in clone
197 ./g clone && ./g -f checkout "$@" || return $?
199 return $?
202 do_reset()
204 git reset "$@" || return $?
205 if [ -f .gitmodules ] ; then
206 git submodule update || return $?
207 else
208 # now that is the nasty case we moved prior to submodules
209 # delete the submodules left over if any
210 for module in $SUBMODULES_ALL ; do
211 echo "clean-up submodule $module"
212 rm -fr ${module}
213 done
214 # make sure we have the needed repo in clone
215 ./g clone && ./g -f reset "$@"
217 return $?;
220 do_init_modules()
222 local module
223 local configured
225 do_shortcut_update
227 for module in $SUBMODULES_CONFIGURED ; do
228 configured=$(git config --local --get submodule.${module}.url)
229 if [ -z "$configured" ] ; then
230 git submodule init $module || return $?
232 done
233 if [ -n "$REFERENCED_GIT" ] ; then
234 for module in $SUBMODULES_CONFIGURED ; do
235 git submodule update --reference $REFERENCED_GIT/.git/modules/$module $module || return $?
236 done
238 return 0
242 # no params, no action
243 if [ "$#" -eq "0" ] ; then
244 usage
247 if [ ! "`type -p git`" ]; then
248 echo "Cannot find the git binary! Is git installed and is in PATH?"
249 exit 1
253 get_active_submodules
254 get_configured_submodules
255 get_git_reference
260 # extra params for some commands, like log
261 EXTRA=
262 COMMAND="$1"
263 PAGER=
264 RELATIVIZE=1
265 PUSH_ALL=
266 PUSH_USER=
267 PUSH_NOTES=
268 LAST_WORKING=
269 SET_LAST_WORKING=
270 ALLOW_EMPTY=
271 KEEP_GOING=
272 REPORT_REPOS=1
273 REPORT_COMMANDS=0
274 REPORT_COMPACT=0
275 DO_HOOK_REFRESH=false
277 while [ "${COMMAND:0:1}" = "-" ] ; do
278 case "$COMMAND" in
279 -f )KEEP_GOING="||:"
282 refresh_all_hooks
283 exit 0;
285 --set-push-urls)
286 shift
287 PUSH_USER="$1"
288 if [ -n "${PUSH_USER}" ] ; then
289 PUSH_USER="${PUSH_USER}@"
291 set_push_urls "$PUSH_USER"
292 exit 0;
295 echo "option: $COMMAND not supported" 1>&2
296 exit 1
297 esac
298 shift
299 COMMAND="$1"
300 done
302 shift
304 case "$COMMAND" in
305 branch)
306 do_git_cmd ${COMMAND} "$@"
308 checkout)
309 do_checkout "$@"
311 clone)
312 do_init_modules && refresh_all_hooks
314 fetch)
315 (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update
318 grep)
319 KEEP_GOING="||:"
320 do_git_cmd ${COMMAND} "$@"
322 pull)
323 git pull "$@" && git submodule update && refresh_all_hooks
325 push)
326 git submodule foreach git push "$@"
327 if [ "$?" = "0" ] ; then
328 git push "$@"
331 reset)
332 do_reset
334 tag)
335 do_git_cmd ${COMMAND} "$@"
340 echo "./g does not support command: $COMMAND" 1>&2
341 exit 1;
343 esac
345 exit $?
347 # vi:set shiftwidth=4 expandtab: