]> git.xonotic.org Git - xonotic/xonotic.git/blob - all
104f96709bf961fbbb58c9299c56a9b49524038a
[xonotic/xonotic.git] / all
1 #!/bin/sh
2 # vim: filetype=zsh
3
4 set -e
5
6 d00=`pwd`
7 while ! [ -f ./all ]; do
8         if [ x"`pwd`" = x"/" ]; then
9                 echo "Cannot find myself."
10                 echo "Please run this script with the working directory inside a Xonotic checkout."
11                 exit 1
12         fi
13         cd ..
14 done
15 d0=`pwd`
16 SELF="$d0/all"
17
18 # If we are on WINDOWS:
19 case "$0" in
20         all|*/all)
21                 case "`uname`" in
22                         MINGW*|Win*)
23                                 # Windows hates users. So this script has to copy itself elsewhere first...
24                                 tname=
25                                 cp "$SELF" ../all.xonotic.sh
26                                 export WE_HATE_OUR_USERS=1
27                                 exec ../all.xonotic.sh "$@"
28                                 ;;
29                 esac
30                 ;;
31 esac
32
33 msg()
34 {
35         echo "\e[1m$*\e[m"
36 }
37
38 checksum()
39 {
40         if [ -x /usr/bin/md5sum ]; then
41                 /usr/bin/md5sum "$@"
42         elif [ -x /bin/md5sum ]; then
43                 /bin/md5sum "$@"
44         elif [ -x /usr/bin/cksum ]; then
45                 /usr/bin/cksum "$@"
46         else
47                 echo "NOCHECKSUM"
48         fi
49 }
50
51 self=`checksum "$SELF"`
52 checkself()
53 {
54         self_new=`checksum "$SELF"`
55         if [ x"$self" != x"$self_new" ]; then
56                 msg "./all has changed."
57                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
58                         msg "Rerunning the requested operation to make sure."
59                         export XONOTIC_FORBID_RERUN_ALL=1
60                         exec "$SELF" "$@"
61                 else
62                         msg "Please try $SELF update, and then retry your requested operation."
63                         exit 1
64                 fi
65         fi
66         return 0
67 }
68
69 verbose()
70 {
71         msg "+ $*"
72         "$@"
73 }
74
75 visible_repo_name()
76 {
77         case "$1" in
78                 .)
79                         echo "the root directory"
80                         ;;
81                 *)
82                         echo "\"$1\""
83                         ;;
84         esac
85 }
86
87 check_mergeconflict()
88 {
89         if git ls-files -u | grep ' 1   '; then
90                 echo
91                 echo "MERGE CONFLICT."
92                 echo "change into the \"$1\" project directory, and then:"
93                 echo "- edit the files mentioned above with your favorite editor,"
94                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
95                 echo "- for binary files, you can select the files using"
96                 echo "  git checkout --ours or git checkout --theirs"
97                 echo "- when done with a file, 'git add' the file"
98                 echo "- when done, 'git commit'"
99                 echo
100                 exit 1
101         fi
102 }
103
104 yesno()
105 {
106         yesno=
107         while [ x"$yesno" != x"y" -a x"$yesno" != x"n" ]; do
108                 eval "$2"
109                 echo "$1"
110                 IFS= read -r yesno
111         done
112         [ x"$yesno" = x"y" ]
113 }
114
115 enter()
116 {
117         $2 cd "$1"
118         check_mergeconflict "$1"
119 }
120
121 repos_urls="
122 .                             |                                                   | master
123 data/xonotic-data.pk3dir      |                                                   | master
124 data/xonotic-maps.pk3dir      |                                                   | master
125 data/xonotic-music.pk3dir     |                                                   | master
126 data/xonotic-nexcompat.pk3dir |                                                   | master
127 mediasource                   |                                                   | master
128 darkplaces                    |                                                   | div0-stable
129 fteqcc                        | git://github.com/Blub/qclib.git                   | master
130 div0-gittools                 | git://git.icculus.org/divverent/div0-gittools.git | master
131 netradiant                    |                                                   | master
132 "
133 # todo: in darkplaces, change repobranch to div0-stable
134
135 repos=`echo "$repos_urls" | grep . | cut -d '|' -f 1 | tr -d ' '`
136
137 base=`git config remote.origin.url`
138 case "$base" in
139         */xonotic.git)
140                 base=${base%xonotic.git}
141                 ;;
142         *)
143                 echo "The main repo is not xonotic.git, what have you done?"
144                 exit 1
145                 ;;
146 esac
147
148 repourl()
149 {
150         t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 2 | tr -d ' '`
151         if [ -n "$t" ]; then
152                 case "$t" in
153                         *://*)
154                                 echo "$t"
155                                 ;;
156                         *)
157                                 echo "$base$t"
158                                 ;;
159                 esac
160         else
161                 if [ x"$1" = x"." ]; then
162                         echo "$base""xonotic.git"
163                 else
164                         echo "$base${1##*/}.git"
165                 fi
166         fi
167 }
168
169 repobranch()
170 {
171         t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 3 | tr -d ' '`
172         if [ -n "$t" ]; then
173                 echo "$t"
174         else
175                 echo "master"
176         fi
177 }
178
179 repos=`for d in $repos; do
180         p="${d%dir}"
181         if [ x"$p" = x"$d" ] || [ -d "$d" ] || ! [ -f "$p" ]; then
182                 echo "$d"
183         fi
184 done`
185
186 if [ "$#" = 0 ]; then
187         set -- help
188 fi
189 cmd=$1
190 shift
191
192 case "$cmd" in
193         update|pull)
194                 allow_pull=true
195                 if [ x"$1" = x"-N" ]; then
196                         allow_pull=false
197                 fi
198                 for d in $repos; do
199                         url=`repourl "$d"`
200                         branch=`repobranch "$d"`
201                         if [ -d "$d0/$d" ]; then
202                                 if $allow_pull; then
203                                         enter "$d0/$d" verbose
204                                         verbose git config remote.origin.url "$url"
205                                         verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
206                                                 # TODO remove this line later
207
208                                         verbose git config core.autocrlf false
209                                         verbose git config core.safecrlf false # we don't NEED that...
210
211                                         r=`git symbolic-ref HEAD`
212                                         r=${r#refs/heads/}
213                                         if git config branch.$r.remote >/dev/null 2>&1; then
214                                                 if ! verbose git pull; then
215                                                         check_mergeconflict "$d"
216                                                         echo "Pulling failed. Press ENTER to continue, or Ctrl-C to abort."
217                                                         read -r DUMMY
218                                                 fi
219                                         fi
220
221                                         cd "$d00"
222                                         checkself "$cmd" "$@"
223                                         cd "$d0/$d"
224                                         verbose git remote prune origin
225                                         cd "$d0"
226                                 fi
227                         else
228                                 verbose git clone "$url" "$d0/$d"
229                                 enter "$d0/$d" verbose
230                                 verbose git checkout "$branch"
231                                 cd "$d0"
232                         fi
233                 done
234                 ;;
235         checkout|switch)
236                 remote=$1
237                 branch=$2
238                 if [ -z "$branch" ]; then
239                         branch=$remote
240                         remote=origin
241                 fi
242                 exists=false
243                 for d in $repos; do
244                         enter "$d0/$d" verbose
245                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
246                                 exists=true
247                                 verbose git checkout "$branch"
248                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
249                                 exists=true
250                                 verbose git checkout --track -b "$branch" "$remote/$branch"
251                         else
252                                 verbose git checkout "`repobranch "$d"`"
253                         fi
254                         cd "$d00"
255                         checkself "$cmd" "$@"
256                         cd "$d0"
257                 done
258                 if ! $exists; then
259                         echo "The requested branch was not found in any repository."
260                 fi
261                 exec "$SELF" branch
262                 ;;
263         branch)
264                 remote=$1
265                 branch=$2
266                 srcbranch=$3
267                 if [ -z "$branch" ]; then
268                         branch=$remote
269                         remote=origin
270                 fi
271                 if [ -z "$branch" ]; then
272                         for d in $repos; do
273                                 enter "$d0/$d"
274                                 r=`git symbolic-ref HEAD`
275                                 r=${r#refs/heads/}
276                                 echo "$d is at $r"
277                                 cd "$d0"
278                         done
279                 else
280                         for d in $repos; do
281                                 dv=`visible_repo_name "$d"`
282                                 enter "$d0/$d" verbose
283                                 if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
284                                         echo "Already having this branch in $dv."
285                                 else
286                                         if yesno "Branch in $dv?"; then
287                                                 if [ -n "$srcbranch" ]; then
288                                                         b=$srcbranch
289                                                 else
290                                                         b="`repobranch "$d"`"
291                                                 fi
292                                                 # TODO do this without pushing
293                                                 verbose git push "$remote" "$b":"$branch"
294                                                 verbose git checkout --track -b "$branch" "$remote/$branch"
295                                         fi
296                                 fi
297                                 cd "$d0"
298                         done
299                         "$SELF" branch
300                 fi
301                 ;;
302         branches)
303                 for d in $repos; do
304                         enter "$d0/$d"
305                         echo "In $d:"
306                         git branch -a -v -v | cut -c 3- | while read -r BRANCH REV UPSTREAM TEXT; do
307                                 case "$UPSTREAM" in
308                                         \[*)
309                                                 UPSTREAM=${UPSTREAM#\[}
310                                                 UPSTREAM=${UPSTREAM%\]}
311                                                 UPSTREAM=${UPSTREAM%:*}
312                                                 ;;
313                                         *)
314                                                 TEXT="$UPSTREAM $TEXT"
315                                                 UPSTREAM=
316                                                 ;;
317                                 esac
318                                 if [ x"$REV" = x"->" ]; then
319                                         continue
320                                 fi
321                                 BRANCH=${BRANCH#remotes/}
322                                 echo -n "  $BRANCH"
323                                 if [ -n "$UPSTREAM" ]; then
324                                         echo -n " (tracking $UPSTREAM)"
325                                 fi
326                                 #echo " $TEXT"
327                                 echo
328                         done
329                 done
330                 ;;
331         branches_short)
332                 for d in $repos; do
333                         cd "$d0/$d" # am in a pipe, shouldn't use enter
334                         git branch -a -v -v | cut -c 3- | sed "s,^,$d ,"
335                         cd "$d0"
336                 done | {
337                         branches_list=
338                         # branches_repos_*=
339                         while read -r d BRANCH REV UPSTREAM TEXT; do
340                                 case "$UPSTREAM" in
341                                         \[*)
342                                                 UPSTREAM=${UPSTREAM#\[}
343                                                 UPSTREAM=${UPSTREAM%\]}
344                                                 UPSTREAM=${UPSTREAM%:*}
345                                                 ;;
346                                         *)
347                                                 TEXT="$UPSTREAM $TEXT"
348                                                 UPSTREAM=
349                                                 ;;
350                                 esac
351                                 if [ x"$REV" = x"->" ]; then
352                                         continue
353                                 fi
354                                 BRANCH=${BRANCH#remotes/}
355                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
356                                 branches_list="$branches_list $BRANCH" # TEH SORT MAKEZ IT UNIEQ
357                                 eval "r=\$branches_repos_$ID"
358                                 r="$r $d:$UPSTREAM"
359                                 eval "branches_repos_$ID=\$r"
360                         done
361                         echo -n "$branches_list" | xargs -n 1 echo | sort -u | while IFS= read -r BRANCH; do
362                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
363                                 eval "r=\$branches_repos_$ID"
364                                 echo "$BRANCH: $r"
365                         done
366                 }
367                 ;;
368         merge)
369                 for d in $repos; do
370                         dv=`visible_repo_name "$d"`
371                         enter "$d0/$d" verbose
372                         r=`git symbolic-ref HEAD`
373                         r=${r#refs/heads/}
374                         if git log HEAD..origin/"`repobranch "$d"`" | grep .; then
375                                 # we have uncommitted changes
376                                 if yesno "Could merge from \"`repobranch "$d"`\" into \"$r\" in $dv. Do it?"; then
377                                         if ! verbose git merge origin/"`repobranch "$d"`"; then
378                                                 check_mergeconflict "$d"
379                                                 exit 1 # this should ALWAYS be fatal
380                                         fi
381                                 fi
382                         fi
383                         cd "$d0"
384                 done
385                 ;;
386         push|commit)
387                 submit=$1
388                 for d in $repos; do
389                         dv=`visible_repo_name "$d"`
390                         enter "$d0/$d" verbose
391                         r=`git symbolic-ref HEAD`
392                         r=${r#refs/heads/}
393                         if git diff HEAD | grep .; then
394                                 # we have uncommitted changes
395                                 if yesno "Uncommitted changes in \"$r\" in $dv. Commit?"; then
396                                         verbose git commit -a
397                                 fi
398                         fi
399                         rem=`git config "branch.$r.remote" || echo origin`
400                         if { git log "$rem/$r".."$r" || git log origin/"`repobranch "$d"`".."$r"; } | grep .; then
401                                 if yesno "Push \"$r\" in $dv?"; then
402                                         verbose git push "$rem" HEAD
403                                 fi
404                         fi
405                         if [ x"$submit" = x"-s" ]; then
406                                 case "$r" in
407                                         */*)
408                                                 verbose git push "$rem" HEAD:"${r%%/*}/finished/${r#*/}"
409                                                 ;;
410                                 esac
411                         fi
412                         cd "$d0"
413                 done
414                 ;;
415         compile)
416                 if [ -n "$WE_HATE_OUR_USERS" ]; then
417                         TARGETS="sv-debug cl-debug"
418                 else
419                         TARGETS="sv-debug cl-debug sdl-debug"
420                 fi
421                 case "$1" in
422                         -c)
423                                 clean=true
424                                 shift
425                                 ;;
426                         *)
427                                 clean=false
428                                 ;;
429                 esac
430                 case "$1" in
431                         sdl)
432                                 TARGETS="sdl-debug"
433                                 shift
434                                 ;;
435                         glx|agl|wgl)
436                                 TARGETS="cl-debug"
437                                 shift
438                                 ;;
439                         dedicated)
440                                 TARGETS="sv-debug"
441                                 shift
442                                 ;;
443                 esac
444                 if [ -z "$MAKEFLAGS" ]; then
445                         if [ -f /proc/cpuinfo ]; then
446                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
447                                 if [ $ncpus -gt 1 ]; then
448                                         MAKEFLAGS=-j$ncpus
449                                 fi
450                         fi
451                         case "`uname`" in
452                                 Linux|*BSD)
453                                         MAKEFLAGS="$MAKEFLAGS DP_LINK_TO_LIBJPEG=1"
454                                         ;;
455                         esac
456                         if [ -n "$WE_HATE_OUR_USERS" ]; then
457                                 MAKEFLAGS="$MAKEFLAGS DP_MAKE_TARGET=mingw"
458                         fi
459                 fi
460                 enter "$d0/fteqcc" verbose
461                 if $clean; then
462                         verbose make $MAKEFLAGS clean
463                 fi
464                 verbose make $MAKEFLAGS
465                 enter "$d0/data/xonotic-data.pk3dir" verbose
466                 if $clean; then
467                         verbose make $MAKEFLAGS clean
468                 fi
469                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS clean
470                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS
471                 enter "$d0/darkplaces" verbose
472                 if $clean; then
473                         verbose make $MAKEFLAGS clean
474                 fi
475                 for T in $TARGETS; do
476                         verbose make $MAKEFLAGS "$@" "$T"
477                 done
478                 ;;
479         run)
480                 if [ -n "$WE_HATE_OUR_USERS" ]; then
481                         client=
482                         export PATH="$d0/misc/buildfiles/w32:$PATH"
483                 elif [ x"`uname`" = x"Darwin" ]; then
484                         export DYLD_LIBRARY_PATH="$d0/misc/buildfiles/osx/Nexuiz.app/Contents/MacOS"
485                         client=-sdl
486                 else
487                         client=-sdl
488                 fi
489                 case "$1" in
490                         sdl|glx|agl|dedicated)
491                                 client=-$1
492                                 shift
493                                 ;;
494                         wgl)
495                                 client=
496                                 shift
497                                 ;;
498                 esac
499                 if ! [ -x "darkplaces/darkplaces$client" ]; then
500                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
501                                 client=$client.exe
502                         else
503                                 echo "Client darkplaces/darkplaces$client not found, aborting"
504                                 exit 1
505                         fi
506                 fi
507                 set -- "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
508
509                 # if pulseaudio is running: USE IT
510                 if ps -C pulseaudio >/dev/null; then
511                         if ldd /usr/lib/libSDL.so 2>/dev/null | grep pulse >/dev/null; then
512                                 export SDL_AUDIODRIVER=pulse
513                         fi
514                 fi
515
516                 if [ -n "$USE_GDB" ]; then
517                         set -- gdb --args "$@"
518                 fi
519                 "$@"
520                 ;;
521         each|foreach)
522                 for d in $repos; do
523                         if verbose cd "$d0/$d"; then
524                                 verbose "$@"
525                                 cd "$d0"
526                         fi
527                 done
528                 ;;
529         save-patches)
530                 outfile=$1
531                 patchdir=`mktemp -d -t save-patches.XXXXXX`
532                 for d in $repos; do
533                         enter "$d0/$d" verbose
534                         git branch -v -v | cut -c 3- | {
535                                 i=0
536                                 while read -r BRANCH REV UPSTREAM TEXT; do
537                                         case "$UPSTREAM" in
538                                                 \[*)
539                                                         UPSTREAM=${UPSTREAM#\[}
540                                                         UPSTREAM=${UPSTREAM%\]}
541                                                         UPSTREAM=${UPSTREAM%:*}
542                                                         TRACK=true
543                                                         ;;
544                                                 *)
545                                                         UPSTREAM=origin/"`repobranch "$d"`"
546                                                         TRACK=false
547                                                         ;;
548                                         esac
549                                         if [ x"$REV" = x"->" ]; then
550                                                 continue
551                                         fi
552                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
553                                                 echo "$d" > "$patchdir/$i/info.txt"
554                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
555                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
556                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
557                                                 i=$(($i+1))
558                                         else
559                                                 rm -rf "$patchdir/$i"
560                                         fi
561                                 done
562                         }
563                 done
564                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
565                 rm -rf "$patchdir"
566                 ;;
567         restore-patches)
568                 infile=$1
569                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
570                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
571                 # detach the head
572                 for P in "$patchdir"/*/info.txt; do
573                         D=${P%/info.txt}
574                         exec 3<"$P"
575                         read -r d <&3
576                         read -r BRANCH <&3
577                         read -r UPSTREAM <&3
578                         read -r TRACK <&3
579                         verbose git checkout HEAD^0
580                         verbose git branch -D "$BRANCH"
581                         if [ x"$TRACK" = x"true" ]; then
582                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
583                         else
584                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
585                         fi
586                         verbose git am "$D"
587                 done
588                 rm -rf "$patchdir"
589                 ;;
590         admin-merge)
591                 if [ "$#" = 1 ]; then
592                         set -- "${1%%/*}" "${1#*/}"
593                 fi
594                 for d in $repos; do
595                         enter "$d0/$d" verbose
596                         git rev-parse "$1/$2" || continue
597                         # 1. review
598                         {
599                                 git log HEAD.."$1/$2"
600                                 git diff HEAD..."$1/$2"
601                         } | less
602                         if yesno "Merge \"$1/$2\" into `git symbolic-ref HEAD` of $d?"; then
603                                 git merge "$1/$2"
604                                 if "$SELF" compile && yesno "Still merge \"$1/$2\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
605                                         git push origin HEAD
606                                         git push "$1" :"$2"
607                                 else
608                                         git reset --hard HEAD@{1}
609                                 fi
610                         fi
611                 done
612                 ;;
613         admin-merge-2)
614                 t=`mktemp`
615                 for d in $repos; do
616                         enter "$d0/$d" verbose
617                         for ref in `git for-each-ref --format='%(refname)' refs/remotes/origin/`; do
618                                 case "${ref#refs/remotes/origin/}" in
619                                         "`repobranch "$d"`")
620                                                 continue
621                                                 ;;
622                                         HEAD|master)
623                                                 continue
624                                                 ;;
625                                 esac
626                                 echo "$ref"
627                                 if git notes --ref "refs/notes/admin-merge" show "$ref" 2>/dev/null; then
628                                         echo "Not merging, already had this one"
629                                 elif yesno "Branch \"$ref\" may want to get merged. Do it?" '{ git log HEAD.."$ref"; git diff HEAD..."$ref"; } | less'; then
630                                         org=`git rev-parse HEAD`
631                                         if ! git merge "$ref" 2>&1 | tee "$t"; then
632                                                 git reset --hard "$org"
633                                                 git notes --ref "refs/notes/admin-merge" add -m "Merge failed:
634 `cat "$t"`" "$ref"
635                                         elif ! "$SELF" compile 2>&1 | tee "$t"; then
636                                                 git reset --hard "$org"
637                                                 git notes --ref "refs/notes/admin-merge" add -m "Compile failed:
638 `cat "$t"`" "$ref"
639                                         elif ! yesno "Still merge \"$ref\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
640                                                 git reset --hard "$org"
641                                                 git notes --ref "refs/notes/admin-merge" add "$ref"
642                                         else
643                                                 git push origin HEAD
644                                                 if yesno "Delete original branch \"$ref\"?"; then
645                                                         git push origin :"${ref#refs/remotes/origin/}"
646                                                 fi
647                                         fi
648                                 else
649                                         git notes --ref "refs/notes/admin-merge" add "$ref"
650                                 fi
651                         done
652                 done
653                 rm -f "$t"
654                 ;;
655         *)
656                 echo "Usage:"
657                 echo "  $SELF pull"
658                 echo "  $SELF merge"
659                 echo "  $SELF push [-s]"
660                 echo "  $SELF branches"
661                 echo "  $SELF branch [<remote>] <branchname>"
662                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
663                 echo "  $SELF checkout [<remote>] <branchname>"
664                 echo "  $SELF compile [-c] [<client>] <options>"
665                 echo "  $SELF run [<client>] <options>"
666                 echo "  $SELF each <command>"
667                 ;;
668 esac