summaryrefslogtreecommitdiff
path: root/g
blob: e5a91f38cd5341d80b12b774a2c27675c71cda9a (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
#!/usr/bin/env bash
#
# Wrapper for git to handle more subdirs at the same time
#

# no params, no action
if [ "$#" -eq "0" ] ; then
    git
    echo
    echo "Additional options available only in this 'g' wrapper:"
    echo
    echo "Usage: g [options] [git commands]"
    echo "   -f         Force - act on all the repos, not only the changed ones"
    echo "   -s         Silent - do not report the repo names."
    echo "   -1         report the repos name on the first line of the output as <repo>:"
    echo "   -z         just to some house cleaning (hooks mostly). this is a stand-alone option as in ./g -z"
    echo "   --set-push-user [username] re-write an existing tree's config with an fd.o commit account name"
    exit $?
fi

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

pushd $(dirname $0) > /dev/null
COREDIR=$(pwd)
popd > /dev/null

refresh_hooks()
{
    repo=$1
    case "$repo" in
	core)
	    pushd $COREDIR > /dev/null
	    for hook_name in $(ls -1 $COREDIR/git-hooks) ; do
		hook=".git/hooks/$hook_name"
		if [ ! -x "$hook" -a ! -L "$hook" ] ; then
		    rm -f "$hook"
		    ln -s "git-hooks/$hook_name" "$hook"
		fi
	    done
	    popd > /dev/null
	    ;;
	translations)
	    if [ -d $COREDIR/clone/translations ] ; then
		pushd $COREDIR/clone/translations > /dev/null
		for hook_name in $(ls -1 $COREDIR/clone/translations/git-hooks) ; do
		    hook=".git/hooks/$hook_name"
		    rm -f "$hook"
		    ln -sf "git-hooks/$hook_name" "$hook"
		done
		# .gitattribute should be per-repo, avoid entangling repos
		if [ -L .gitattributes ] ; then
		    rm -f .gitattributes
		fi
		popd > /dev/null
	    fi
	    ;;
	help|dictionaries)
	    if [ -d $COREDIR/clone/$repo ] ; then
		pushd $COREDIR/clone/$repo > /dev/null
		# fixme: we should really keep these per-repo to
		# keep the repos independant. since these two
		# are realy not independant yet, we keep using core's hooks
		for hook_name in $(ls -1 $COREDIR/git-hooks) ; do
		    hook=".git/hooks/$hook_name"
		    rm -f "$hook"
		    ln -sf "$COREDIR/git-hooks/$hook_name" "$hook"
		done
		# .gitattribute should be per-repo, avoid entangling repos
		if [ -L .gitattributes ] ; then
		    rm -f .gitattributes
		fi
		popd > /dev/null
	    fi
	    ;;
    esac
}

refresh_all_hooks()
{
    repos="core $(cat "$COREDIR/bin/repo-list")"
    for repo in $repos ; do
	refresh_hooks $repo
    done
}

postprocess()
{
    rc=$1
    if $DO_HOOK_REFRESH ; then
	refresh_all_hooks
    fi

    exit $rc;
}

CLONEDIR="$COREDIR/clone"
if [ ! -e ${CLONEDIR} ]; then mkdir -p "$CLONEDIR"; fi

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

while [ "${COMMAND:0:1}" = "-" ] ; do
    case "$COMMAND" in
        -f) KEEP_GOING=1
            ;;
        -s) REPORT_REPOS=0
            ;;
	-1) REPORT_COMPACT=1
            ;;
	--set-push-user)
	    shift
	    PUSH_USER="$1"
	    ;;
	-z)
	    DO_HOOK_REFRESH=true
	    postprocess 0
	    ;;
    esac
    shift
    COMMAND="$1"
done

case "$COMMAND" in
    apply)
        EXTRA="-p0 --stat --apply --index --ignore-space-change --whitespace=error"
        RELATIVIZE=0
        ;;
    clone|fetch|pull)
	DO_HOOK_REFRESH=true
	;;
    diff)
        PAGER='--no-pager'
        REPORT_REPOS=0
        ;;
    log)
        if [ "$#" = "1" ] ; then
            EXTRA='-1'
        fi
        PAGER='--no-pager'
        ;;
    push)
        if [ "$#" != "1" ] ; then
            PUSH_ALL=1
        fi
        ;;
esac

# absolutize the parameters first
unset FILES
FILESNUM=0
while shift ; do
    PARAM="$1"
    if [ -z "$PARAM" ] ; then
        continue
    elif [ "${PARAM:0:1}" = "-" ] ; then
        if [ \( "$COMMAND" = "checkout" -a "$PARAM" = "-b" \) -o \
             \( "$COMMAND" = "clone"    -a "$PARAM" = "--reference" \) -o \
             \( "$COMMAND" = "commit"   -a "$PARAM" = "-m" \) -o \
             \( "$COMMAND" = "commit"   -a "$PARAM" = "-am" \) -o \
             \( "$COMMAND" = "tag"      -a "$PARAM" = "-m" \) ]
        then
            # params that take an argument
            FILES[$FILESNUM]="$PARAM"
            FILESNUM=$(($FILESNUM+1))
            shift
            FILES[$FILESNUM]="$1"
            FILESNUM=$(($FILESNUM+1))
        else
            if [ "$COMMAND" = "commit" -a "$PARAM" = "-F" ]
            then
                shift
                # this still needs some magic to handle relative paths
                EXTRA="${EXTRA} -F ${1}"
            else
                [ "$COMMAND" = "commit" -a "$PARAM" = "--allow-empty" ] && ALLOW_EMPTY=1
                FILES[$FILESNUM]="$PARAM"
                FILESNUM=$(($FILESNUM+1))
            fi
        fi
    else
        if [ "$COMMAND" = "apply" ] ; then
            grep -qs $'^+ *\t' "$PARAM" && {
                echo "Patch '$PARAM' introduces tabs in indentation, aborting."
                echo
                echo "Please fix the patch (something like s/^\(+ *\)\t/\1    /) and try again."
                echo
                exit 1
            }
        fi

        # make the paths absolute
        FILES[$FILESNUM]=$(perl -e 'use Cwd "abs_path"; print abs_path(shift);' "$PARAM")
        if [ -z "${FILES[$FILESNUM]}" -o ! -e "${FILES[$FILESNUM]}" ] ; then
            # it is probably not a file, but a tag name, or something
            FILES[$FILESNUM]="$PARAM"
        fi
        FILESNUM=$(($FILESNUM+1))
    fi
done

# do it!
DIRS="core $(cd $CLONEDIR ; ls)"
if [ "$COMMAND" = "clone" ] ; then
    DIRS=$(cat "$COREDIR/bin/repo-list")
fi
for REPO in $DIRS ; do
    DIR="$CLONEDIR/$REPO"
    NAME="$REPO"
    if [ "$REPO" = "core" ] ; then
        DIR="$COREDIR"
        NAME="main repo"
    fi

    if [ -d "$DIR" -a "z$PUSH_USER" != "z" ]; then
       echo "setting up push url for $DIR"
	   (cd $DIR && git config remote.origin.pushurl "ssh://${PUSH_USER}@git.freedesktop.org/git/libreoffice/${REPO}")
    elif [ \( -d "$DIR" -a -d "$DIR"/.git \) -o \( "$COMMAND" = "clone" \) ] ; then
        (
            # executed in a subshell
            if [ "$COMMAND" != "clone" ] ; then
                cd "$DIR"
            else
                cd "$CLONEDIR"
            fi

            # relativize the absolutized params again if we want to operate
            # only on the files belonging to this exact repo
            if [ "$RELATIVIZE" = "1" -a -n "$FILES" ] ; then
                FILESNUM=0
                INSERTNUM=0
                PWD=$(pwd)
                PWDLEN=$(pwd | wc -c)
                for I in "${FILES[@]}" ; do
                    I="${I//@REPO@/${REPO}}"
                    unset FILES[$FILESNUM]
                    FILESNUM=$(($FILESNUM+1))
                    # filter out files that don't belong to this repo
                    if [ \( "${I:0:1}" = "/" \) -a \( "$COMMAND" != "clone" \) ] ; then
                        if [ "${I:0:$PWDLEN}" = "$PWD/" ] ; then
                            FILES[$INSERTNUM]="${I:$PWDLEN}"
                            INSERTNUM=$(($INSERTNUM+1))
                        fi
                    else
                        FILES[$INSERTNUM]="$I"
                        INSERTNUM=$(($INSERTNUM+1))
                    fi
                done
                [ "$INSERTNUM" = "0" ] && exit 0
            fi

            # some extra params
            case "$COMMAND" in
                apply)
                    for I in * ; do
                        if [ -d "$I" ] ; then
                            EXTRA="$EXTRA --include=$I/*"
                        else
                            EXTRA="$EXTRA --include=$I"
                        fi
                    done
                    ;;
                commit)
                    if [ "$ALLOW_EMPTY" != "1" ] ; then
                        [ -z "$(git diff-index --name-only HEAD --)" ] && exit 0
                    fi
                    ;;
                push)
                    if [ "$PUSH_ALL" != "1" ] ; then
                        [ -n "$(git rev-list origin..HEAD)" ] || exit 0
                    fi
                    ;;
                status)
                    LOCALCOMMITS="$(git rev-list origin..HEAD)"
                    if [ -z "$LOCALCOMMITS" ] ; then
                        [ -z "$(git diff-index --name-only HEAD --)" ] && exit 0
                    fi
                    ;;
                clone)
                    EXTRA="$(git config remote.origin.url)"
		    EXTRA=${EXTRA/core/${REPO}}
                    ;;
            esac

            # do it!
            if [ "$COMMAND" != "clone" -o ! -d $DIR ] ; then
                if [ "$REPORT_REPOS" = "1" -a "$COMMAND" != "grep" ] ; then
                    if [ "$REPORT_COMPACT" = "1" ] ; then
                        echo -n "${REPO}:"
                    else
                        echo "===== $NAME ====="
                    fi
                fi
                git $PAGER "$COMMAND" $EXTRA "${FILES[@]}"
                RETURN=$?
            fi

            # now we can change the dir in case of clone as well
            if [ "$COMMAND" = "clone" ] ; then
                cd $DIR
            fi

            case "$COMMAND" in
                pull|clone)
                    # update links
		    if [ "$DIR" != "$COREDIR" ]; then
			for link in $(ls) ; do
			    if [ ! -e "$COREDIR/$link" ] ; then
			        if test -h "$COREDIR/$link"; then
				    rm "$COREDIR/$link"
				    echo -n "re-"
			        fi
                                echo "creating missing link $link"
                                ln -s "$DIR/$link" "$COREDIR/$link"
                            fi
                        done
		    fi
                    ;;
                status)
                    # git status returns error in some versions, clear that
                    RETURN=0
                    ;;
                grep)
                    # git grep return an 'error' if nothing is found
                    # still we should continue grepping the other repos
                    RETURN=0
                    ;;
            esac
            if [  "$KEEP_GOING" = "1" ] ; then
                RETURN=0
            fi

            exit $RETURN
        ) || postprocess $?
    fi
done

# Cleanup the broken links
if [ "$COMMAND" = "pull" ] ; then
    for link in $(ls $COREDIR) ; do
        if [ -h "$COREDIR/$link" -a ! -e "$COREDIR/$link" ]; then
            echo "Removing broken link $link"
            rm $COREDIR/$link
        fi
    done
fi

# warn
if [ "$COMMAND" = "apply" ] ; then
    echo
    echo "Don't forget to check the status & commit now ;-)"
    echo
fi

postprocess $?

# vi:set shiftwidth=4 expandtab: