summaryrefslogtreecommitdiffstats
path: root/g
blob: fdf95c8278ce156042e3e225b18722cc8c15f323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env bash
#
# Wrapper for git to handle more subdirs at the same time
#

if [ -n "$g_debug" ] ; then
    set -x
fi

SUBMODULES_ALL="dictionaries helpcontent2 translations"

pushd $(dirname $0) > /dev/null
if [ -f ${BUILDDIR}/config_host.mk ] ; then
    # we are in the SRCDIR
    SRC_ROOT=$(< ${BUILDDIR}/config_host.mk grep -a SRC_ROOT | sed -e "s/.*=//")
else
    SRC_ROOT=$(pwd)
fi
popd > /dev/null

COREDIR="$SRC_ROOT"

usage()
{
    git
    echo
    echo "Usage: g [options] [git (checkout|clone|fetch|gc|grep|pull|push|reset) [git options/args..]]"
    echo ""
    echo " -z restore the git hooks and do other sanity checks"
}

refresh_submodule_hooks()
{
    local repo=$1
    local hook
    local hook_name

    if [ -d "${repo?}"/.git ] ; then
        # use core's hook by default
        for hook_name in "${COREDIR?}/.git-hooks"/* ; do
            if [ ! -e "${hook_name}" ] ; then
                continue
            fi
            hook="${repo?}/.git/hooks/${hook_name##*/}"
            if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
                rm -f "${hook?}"
                ln -sf "${hook_name}" "${hook?}"
            fi
        done
        # override if need be by the submodules' own hooks
        for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
            if [ ! -e "${hook_name}" ] ; then
                continue
            fi
            hook="${repo?}/.git/hooks/${hook_name##*/}"
            if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
                rm -f "${hook?}"
                ln -sf "${hook_name}" "${hook?}"
            fi
        done
    elif [ -d .git/modules/"${repo}"/hooks ] ; then
        for hook_name in "${COREDIR?}/.git-hooks"/* ; do
            if [ ! -e "${hook_name}" ] ; then
                continue
            fi
            hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
            if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
                rm -f "${hook?}"
                ln -sf "${hook_name}" "${hook?}"
            fi
        done
        # override if need be by the submodules' own hooks
        for hook_name in "${COREDIR?}/${repo?}/.git-hooks"/* ; do
            if [ ! -e "${hook_name}" ] ; then
                continue
            fi
            hook=".git/modules/${repo?}/hooks/${hook_name##*/}"
            if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
                rm -f "${hook?}"
                ln -sf "${hook_name}" "${hook?}"
            fi
        done
    fi

}

refresh_all_hooks()
{
    local repo
    local hook_name
    local hook

    pushd "${COREDIR?}" > /dev/null
    for hook_name in "${COREDIR?}/.git-hooks"/* ; do
        hook=".git/hooks/${hook_name##*/}"
        if [ ! -e "${hook?}" ] || [ -L "${hook?}" ] ; then
            rm -f "${hook?}"
            ln -sf "${hook_name}" "${hook?}"
        fi
    done

    for repo in ${SUBMODULES_ALL?} ; do
        refresh_submodule_hooks "$repo"
    done
    popd > /dev/null

}

set_push_url()
{
    local repo

    repo="$1"
    if [ -n "$repo" ] ; then
        pushd "${COREDIR?}/${repo?}" > /dev/null
    else
        pushd "${COREDIR?}" > /dev/null
        repo="core"
    fi
    echo "setting up push url for ${repo?}"
    if [ "${repo?}" = "helpcontent2" ] ; then
        git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/help"
    else
        git config remote.origin.pushurl "ssh://${PUSH_USER}logerrit/${repo?}"
    fi
    popd > /dev/null
}

set_push_urls()
{
    PUSH_USER="$1"
    set_push_url
    for repo in ${SUBMODULES_ACTIVE?} ; do
        set_push_url "${repo?}"
    done
}

get_active_submodules()
{
    SUBMODULES_ACTIVE=""
    local repo

    for repo in ${SUBMODULES_ALL?} ; do
        if [ -d "${repo?}"/.git ] || [ -f "${repo?}"/.git ] ; then
            SUBMODULES_ACTIVE="${repo?} ${SUBMODULES_ACTIVE?}"
        fi
    done
}

get_configured_submodules()
{
    SUBMODULES_CONFIGURED=""
    if [ -f ${BUILDDIR}/config_host.mk ] ; then
        SUBMODULES_CONFIGURED=$(< ${BUILDDIR}/config_host.mk grep -a GIT_NEEDED_SUBMODULES | sed -e "s/.*=//")
    else
        # if we need the configured submodule before the configuration is done. we assumed you want them all
        SUBMODULES_CONFIGURED=${SUBMODULES_ALL?}
    fi
}

get_git_reference()
{
    REFERENCED_GIT=""
    if [ -f ${BUILDDIR}/config_host.mk ]; then
        REFERENCED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_REFERENCE_SRC | sed -e "s/.*=//")
    fi
    LINKED_GIT=""
    if [ -f ${BUILDDIR}/config_host.mk ]; then
        LINKED_GIT=$(< ${BUILDDIR}/config_host.mk grep -a GIT_LINK_SRC | sed -e "s/.*=//")
    fi
}

do_shortcut_update()
{
    local module
    local repo

    for module in $SUBMODULES_CONFIGURED ; do
        if [ ! -d "${module?}"/.git ] ; then
            case "${module?}" in
                helpcontent2)
                    if [ -d clone/help/.git ] ; then
                        repo="clone/help/.git"
                    fi
                    ;;
                *)
                    if [ -d clone/"${module?}"/.git ] ; then
                        repo="clone/${module?}/.git"
                    fi
                    ;;
            esac
            if [ -n "$repo" ] ; then
                cp -r "${repo?}" "${module?}/."
            fi
        fi
    done
}

do_git_cmd()
{
    echo "cmd:$*"
    git "$@"
    git submodule foreach git "$@" $KEEP_GOING
}

do_checkout()
{
    local cmd
    local create_branch="0"
    local branch
    local module

    git checkout "$@" || return $?
    for cmd in "$@" ; do
        if [ "$cmd" = "-f" ]; then
            continue
        elif [ "$cmd" = "-b" ] ; then
            create_branch=1
        elif [ "$create_branch" = "1" ] ; then
            branch="$cmd"
            create_branch=0
        fi
    done
    if [ -f .gitmodules ] ; then
        git submodule update --progress
        if [ -n "$branch" ] ; then
            git submodule foreach git checkout -b "${branch}" HEAD || return $?
        fi
    else
        # now that is the nasty case we moved prior to submodules
        # delete the submodules left over if any
        for module in $SUBMODULES_ALL ; do
            echo "clean-up submodule $module"
            rm -fr "${module}"
        done
        # make sure we have the needed repo in clone
        ./g clone && ./g -f checkout "$@" || return $?
    fi
    return $?
}

do_reset()
{
    git reset "$@" || return $?
    if [ -f .gitmodules ] ; then
        git submodule update --progress || return $?
    else
        # now that is the nasty case we moved prior to submodules
        # delete the submodules left over if any
        for module in $SUBMODULES_ALL ; do
            echo "clean-up submodule $module"
            rm -fr "${module}"
        done
        # make sure we have the needed repo in clone
        ./g clone && ./g -f reset "$@"
    fi
    return $?;
}

do_init_modules()
{
    local module
    local configured

    do_shortcut_update

    for module in $SUBMODULES_CONFIGURED ; do
        if [ -n "$LINKED_GIT" ] ; then
            if ! [ -d ".git/modules/${module}" ]; then
                ./bin/git-new-module-workdir "${LINKED_GIT}/${module}" "${module}"
            fi
        fi
        configured=$(git config --local --get submodule."${module}".url)
        if [ -z "$configured" ] ; then
            git submodule init "$module" || return $?
        fi
    done
    for module in $SUBMODULES_CONFIGURED ; do
        if [ -n "$REFERENCED_GIT" ] ; then
            git submodule update --reference "$REFERENCED_GIT/.git/modules/$module" --progress "$module" || return $?
        else
            git submodule update --progress "$module" || return $?
        fi
    done
    return 0
}


# no params, no action
if [ "$#" -eq "0" ] ; then
    usage
fi

if [ ! "$(type -p git)" ]; then
    echo "Cannot find the git binary! Is git installed and is in PATH?"
    exit 1
fi


get_active_submodules
get_configured_submodules
get_git_reference




# extra params for some commands, like log
EXTRA=
COMMAND="$1"
PAGER=
RELATIVIZE=1
PUSH_ALL=
PUSH_USER=
PUSH_NOTES=
LAST_WORKING=
SET_LAST_WORKING=
ALLOW_EMPTY=
KEEP_GOING=
REPORT_REPOS=1
REPORT_COMMANDS=0
REPORT_COMPACT=0
DO_HOOK_REFRESH=false

while [ "${COMMAND:0:1}" = "-" ] ; do
    case "$COMMAND" in
        -f )KEEP_GOING="||:"
            ;;
        -z)
            refresh_all_hooks
            exit 0;
            ;;
        --set-push-urls)
            shift
            PUSH_USER="$1"
            if [ -n "${PUSH_USER}" ] ; then
                PUSH_USER="${PUSH_USER}@"
            fi
            set_push_urls "$PUSH_USER"
            exit 0;
            ;;
        -*)
            echo "option: $COMMAND not supported" 1>&2
            exit 1
    esac
    shift
    COMMAND="$1"
done

shift

case "$COMMAND" in
    branch)
        do_git_cmd "${COMMAND}" "$@"
        ;;
    checkout)
        do_checkout "$@"
        ;;
    clone)
        do_init_modules && refresh_all_hooks
        ;;
    fetch)
        (git fetch "$@" && git submodule foreach git fetch "$@" ) && git submodule update --progress

        ;;
    gc)
         (git gc "$@" && git submodule foreach git gc "$@" )
        ;;
    grep)
        KEEP_GOING="||:"
        do_git_cmd "${COMMAND}" "$@"
        ;;
    pull)
        git pull "$@" && git submodule update --progress && refresh_all_hooks
        ;;
    push)
        git submodule foreach git push "$@"
        if [ "$?" = "0" ] ; then
            git push "$@"
        fi
        ;;
    reset)
        do_reset
        ;;
    tag)
        do_git_cmd "${COMMAND}" "$@"
        ;;
    "")
        ;;
    *)
        echo "./g does not support command: $COMMAND" 1>&2
        exit 1;
        ;;
esac

exit $?

# vi:set shiftwidth=4 expandtab: