more admin-merge-2 stuff ;)
[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 | svn
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 repoflags()
180 {
181         echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 4 | tr -d ' '
182         echo "$t"
183 }
184
185 repos=`for d in $repos; do
186         p="${d%dir}"
187         if [ x"$p" = x"$d" ] || [ -d "$d" ] || ! [ -f "$p" ]; then
188                 echo "$d"
189         fi
190 done`
191
192 if [ "$#" = 0 ]; then
193         set -- help
194 fi
195 cmd=$1
196 shift
197
198 case "$cmd" in
199         update|pull)
200                 allow_pull=true
201                 if [ x"$1" = x"-N" ]; then
202                         allow_pull=false
203                 fi
204                 for d in $repos; do
205                         url=`repourl "$d"`
206                         branch=`repobranch "$d"`
207                         if [ -d "$d0/$d" ]; then
208                                 if $allow_pull; then
209                                         enter "$d0/$d" verbose
210                                         verbose git config remote.origin.url "$url"
211                                         verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
212                                                 # TODO remove this line later
213
214                                         verbose git config core.autocrlf false
215                                         verbose git config core.safecrlf false # we don't NEED that...
216
217                                         r=`git symbolic-ref HEAD`
218                                         r=${r#refs/heads/}
219                                         if git config branch.$r.remote >/dev/null 2>&1; then
220                                                 if ! verbose git pull; then
221                                                         check_mergeconflict "$d"
222                                                         echo "Pulling failed. Press ENTER to continue, or Ctrl-C to abort."
223                                                         read -r DUMMY
224                                                 fi
225                                         fi
226
227                                         cd "$d00"
228                                         checkself "$cmd" "$@"
229                                         cd "$d0/$d"
230                                         verbose git remote prune origin
231                                         cd "$d0"
232                                 fi
233                         else
234                                 verbose git clone "$url" "$d0/$d"
235                                 enter "$d0/$d" verbose
236                                 verbose git checkout "$branch"
237                                 cd "$d0"
238                         fi
239                 done
240                 ;;
241         checkout|switch)
242                 remote=$1
243                 branch=$2
244                 if [ -z "$branch" ]; then
245                         branch=$remote
246                         remote=origin
247                 fi
248                 exists=false
249                 for d in $repos; do
250                         enter "$d0/$d" verbose
251                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
252                                 exists=true
253                                 verbose git checkout "$branch"
254                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
255                                 exists=true
256                                 verbose git checkout --track -b "$branch" "$remote/$branch"
257                         else
258                                 verbose git checkout "`repobranch "$d"`"
259                         fi
260                         cd "$d00"
261                         checkself "$cmd" "$@"
262                         cd "$d0"
263                 done
264                 if ! $exists; then
265                         echo "The requested branch was not found in any repository."
266                 fi
267                 exec "$SELF" branch
268                 ;;
269         branch)
270                 remote=$1
271                 branch=$2
272                 srcbranch=$3
273                 if [ -z "$branch" ]; then
274                         branch=$remote
275                         remote=origin
276                 fi
277                 if [ -z "$branch" ]; then
278                         for d in $repos; do
279                                 enter "$d0/$d"
280                                 r=`git symbolic-ref HEAD`
281                                 r=${r#refs/heads/}
282                                 echo "$d is at $r"
283                                 cd "$d0"
284                         done
285                 else
286                         for d in $repos; do
287                                 dv=`visible_repo_name "$d"`
288                                 enter "$d0/$d" verbose
289                                 if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
290                                         echo "Already having this branch in $dv."
291                                 else
292                                         if yesno "Branch in $dv?"; then
293                                                 if [ -n "$srcbranch" ]; then
294                                                         b=$srcbranch
295                                                 else
296                                                         b="`repobranch "$d"`"
297                                                 fi
298                                                 # TODO do this without pushing
299                                                 verbose git push "$remote" "$b":"$branch"
300                                                 verbose git checkout --track -b "$branch" "$remote/$branch"
301                                         fi
302                                 fi
303                                 cd "$d0"
304                         done
305                         "$SELF" branch
306                 fi
307                 ;;
308         branches)
309                 for d in $repos; do
310                         enter "$d0/$d"
311                         echo "In $d:"
312                         git branch -a -v -v | cut -c 3- | while read -r BRANCH REV UPSTREAM TEXT; do
313                                 case "$UPSTREAM" in
314                                         \[*)
315                                                 UPSTREAM=${UPSTREAM#\[}
316                                                 UPSTREAM=${UPSTREAM%\]}
317                                                 UPSTREAM=${UPSTREAM%:*}
318                                                 ;;
319                                         *)
320                                                 TEXT="$UPSTREAM $TEXT"
321                                                 UPSTREAM=
322                                                 ;;
323                                 esac
324                                 if [ x"$REV" = x"->" ]; then
325                                         continue
326                                 fi
327                                 BRANCH=${BRANCH#remotes/}
328                                 echo -n "  $BRANCH"
329                                 if [ -n "$UPSTREAM" ]; then
330                                         echo -n " (tracking $UPSTREAM)"
331                                 fi
332                                 #echo " $TEXT"
333                                 echo
334                         done
335                 done
336                 ;;
337         branches_short)
338                 for d in $repos; do
339                         cd "$d0/$d" # am in a pipe, shouldn't use enter
340                         git branch -a -v -v | cut -c 3- | sed "s,^,$d ,"
341                         cd "$d0"
342                 done | {
343                         branches_list=
344                         # branches_repos_*=
345                         while read -r d BRANCH REV UPSTREAM TEXT; do
346                                 case "$UPSTREAM" in
347                                         \[*)
348                                                 UPSTREAM=${UPSTREAM#\[}
349                                                 UPSTREAM=${UPSTREAM%\]}
350                                                 UPSTREAM=${UPSTREAM%:*}
351                                                 ;;
352                                         *)
353                                                 TEXT="$UPSTREAM $TEXT"
354                                                 UPSTREAM=
355                                                 ;;
356                                 esac
357                                 if [ x"$REV" = x"->" ]; then
358                                         continue
359                                 fi
360                                 BRANCH=${BRANCH#remotes/}
361                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
362                                 branches_list="$branches_list $BRANCH" # TEH SORT MAKEZ IT UNIEQ
363                                 eval "r=\$branches_repos_$ID"
364                                 r="$r $d:$UPSTREAM"
365                                 eval "branches_repos_$ID=\$r"
366                         done
367                         echo -n "$branches_list" | xargs -n 1 echo | sort -u | while IFS= read -r BRANCH; do
368                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
369                                 eval "r=\$branches_repos_$ID"
370                                 echo "$BRANCH: $r"
371                         done
372                 }
373                 ;;
374         merge)
375                 for d in $repos; do
376                         dv=`visible_repo_name "$d"`
377                         enter "$d0/$d" verbose
378                         r=`git symbolic-ref HEAD`
379                         r=${r#refs/heads/}
380                         if git log HEAD..origin/"`repobranch "$d"`" | grep .; then
381                                 # we have uncommitted changes
382                                 if yesno "Could merge from \"`repobranch "$d"`\" into \"$r\" in $dv. Do it?"; then
383                                         if ! verbose git merge origin/"`repobranch "$d"`"; then
384                                                 check_mergeconflict "$d"
385                                                 exit 1 # this should ALWAYS be fatal
386                                         fi
387                                 fi
388                         fi
389                         cd "$d0"
390                 done
391                 ;;
392         push|commit)
393                 submit=$1
394                 for d in $repos; do
395                         dv=`visible_repo_name "$d"`
396                         enter "$d0/$d" verbose
397                         r=`git symbolic-ref HEAD`
398                         r=${r#refs/heads/}
399                         if git diff HEAD | grep .; then
400                                 # we have uncommitted changes
401                                 if yesno "Uncommitted changes in \"$r\" in $dv. Commit?"; then
402                                         verbose git commit -a
403                                 fi
404                         fi
405                         rem=`git config "branch.$r.remote" || echo origin`
406                         if { git log "$rem/$r".."$r" || git log origin/"`repobranch "$d"`".."$r"; } | grep .; then
407                                 if yesno "Push \"$r\" in $dv?"; then
408                                         verbose git push "$rem" HEAD
409                                 fi
410                         fi
411                         if [ x"$submit" = x"-s" ]; then
412                                 case "$r" in
413                                         */*)
414                                                 verbose git push "$rem" HEAD:"${r%%/*}/finished/${r#*/}"
415                                                 ;;
416                                 esac
417                         fi
418                         cd "$d0"
419                 done
420                 ;;
421         compile)
422                 if [ -n "$WE_HATE_OUR_USERS" ]; then
423                         TARGETS="sv-debug cl-debug"
424                 else
425                         TARGETS="sv-debug cl-debug sdl-debug"
426                 fi
427                 case "$1" in
428                         -c)
429                                 clean=true
430                                 shift
431                                 ;;
432                         *)
433                                 clean=false
434                                 ;;
435                 esac
436                 case "$1" in
437                         sdl)
438                                 TARGETS="sdl-debug"
439                                 shift
440                                 ;;
441                         glx|agl|wgl)
442                                 TARGETS="cl-debug"
443                                 shift
444                                 ;;
445                         dedicated)
446                                 TARGETS="sv-debug"
447                                 shift
448                                 ;;
449                 esac
450                 if [ -z "$MAKEFLAGS" ]; then
451                         if [ -f /proc/cpuinfo ]; then
452                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
453                                 if [ $ncpus -gt 1 ]; then
454                                         MAKEFLAGS=-j$ncpus
455                                 fi
456                         fi
457                         case "`uname`" in
458                                 Linux|*BSD)
459                                         MAKEFLAGS="$MAKEFLAGS DP_LINK_TO_LIBJPEG=1"
460                                         ;;
461                         esac
462                         if [ -n "$WE_HATE_OUR_USERS" ]; then
463                                 MAKEFLAGS="$MAKEFLAGS DP_MAKE_TARGET=mingw"
464                         fi
465                 fi
466                 enter "$d0/fteqcc" verbose
467                 if $clean; then
468                         verbose make $MAKEFLAGS clean
469                 fi
470                 verbose make $MAKEFLAGS
471                 enter "$d0/data/xonotic-data.pk3dir" verbose
472                 if $clean; then
473                         verbose make $MAKEFLAGS clean
474                 fi
475                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS clean
476                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS
477                 enter "$d0/darkplaces" verbose
478                 if $clean; then
479                         verbose make $MAKEFLAGS clean
480                 fi
481                 for T in $TARGETS; do
482                         verbose make $MAKEFLAGS "$@" "$T"
483                 done
484                 ;;
485         run)
486                 if [ -n "$WE_HATE_OUR_USERS" ]; then
487                         client=
488                         export PATH="$d0/misc/buildfiles/w32:$PATH"
489                 elif [ x"`uname`" = x"Darwin" ]; then
490                         export DYLD_LIBRARY_PATH="$d0/misc/buildfiles/osx/Nexuiz.app/Contents/MacOS"
491                         client=-sdl
492                 else
493                         client=-sdl
494                 fi
495                 case "$1" in
496                         sdl|glx|agl|dedicated)
497                                 client=-$1
498                                 shift
499                                 ;;
500                         wgl)
501                                 client=
502                                 shift
503                                 ;;
504                 esac
505                 if ! [ -x "darkplaces/darkplaces$client" ]; then
506                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
507                                 client=$client.exe
508                         else
509                                 echo "Client darkplaces/darkplaces$client not found, aborting"
510                                 exit 1
511                         fi
512                 fi
513                 set -- "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
514
515                 # if pulseaudio is running: USE IT
516                 if ps -C pulseaudio >/dev/null; then
517                         if ldd /usr/lib/libSDL.so 2>/dev/null | grep pulse >/dev/null; then
518                                 export SDL_AUDIODRIVER=pulse
519                         fi
520                 fi
521
522                 if [ -n "$USE_GDB" ]; then
523                         set -- gdb --args "$@"
524                 fi
525                 "$@"
526                 ;;
527         each|foreach)
528                 for d in $repos; do
529                         if verbose cd "$d0/$d"; then
530                                 verbose "$@"
531                                 cd "$d0"
532                         fi
533                 done
534                 ;;
535         save-patches)
536                 outfile=$1
537                 patchdir=`mktemp -d -t save-patches.XXXXXX`
538                 for d in $repos; do
539                         enter "$d0/$d" verbose
540                         git branch -v -v | cut -c 3- | {
541                                 i=0
542                                 while read -r BRANCH REV UPSTREAM TEXT; do
543                                         case "$UPSTREAM" in
544                                                 \[*)
545                                                         UPSTREAM=${UPSTREAM#\[}
546                                                         UPSTREAM=${UPSTREAM%\]}
547                                                         UPSTREAM=${UPSTREAM%:*}
548                                                         TRACK=true
549                                                         ;;
550                                                 *)
551                                                         UPSTREAM=origin/"`repobranch "$d"`"
552                                                         TRACK=false
553                                                         ;;
554                                         esac
555                                         if [ x"$REV" = x"->" ]; then
556                                                 continue
557                                         fi
558                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
559                                                 echo "$d" > "$patchdir/$i/info.txt"
560                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
561                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
562                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
563                                                 i=$(($i+1))
564                                         else
565                                                 rm -rf "$patchdir/$i"
566                                         fi
567                                 done
568                         }
569                 done
570                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
571                 rm -rf "$patchdir"
572                 ;;
573         restore-patches)
574                 infile=$1
575                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
576                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
577                 # detach the head
578                 for P in "$patchdir"/*/info.txt; do
579                         D=${P%/info.txt}
580                         exec 3<"$P"
581                         read -r d <&3
582                         read -r BRANCH <&3
583                         read -r UPSTREAM <&3
584                         read -r TRACK <&3
585                         verbose git checkout HEAD^0
586                         verbose git branch -D "$BRANCH"
587                         if [ x"$TRACK" = x"true" ]; then
588                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
589                         else
590                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
591                         fi
592                         verbose git am "$D"
593                 done
594                 rm -rf "$patchdir"
595                 ;;
596         admin-merge)
597                 if [ "$#" = 1 ]; then
598                         set -- "${1%%/*}" "${1#*/}"
599                 fi
600                 for d in $repos; do
601                         enter "$d0/$d" verbose
602                         git rev-parse "$1/$2" || continue
603                         # 1. review
604                         {
605                                 git log HEAD.."$1/$2"
606                                 git diff HEAD..."$1/$2"
607                         } | less
608                         if yesno "Merge \"$1/$2\" into `git symbolic-ref HEAD` of $d?"; then
609                                 git merge "$1/$2"
610                                 if "$SELF" compile && yesno "Still merge \"$1/$2\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
611                                         git push origin HEAD
612                                         git push "$1" :"$2"
613                                 else
614                                         git reset --hard HEAD@{1}
615                                 fi
616                         fi
617                 done
618                 ;;
619         admin-merge-2)
620                 t=`mktemp`
621                 for d in $repos; do
622                         enter "$d0/$d" verbose
623                         for ref in `git for-each-ref --format='%(refname)' refs/remotes/origin/`; do
624                                 case "${ref#refs/remotes/origin/}" in
625                                         "`repobranch "$d"`")
626                                                 continue
627                                                 ;;
628                                         HEAD|master)
629                                                 continue
630                                                 ;;
631                                 esac
632                                 echo "$ref"
633                                 if git notes --ref "refs/notes/admin-merge" show "$ref" 2>/dev/null; then
634                                         echo "Not merging, already had this one"
635                                 elif yesno "Branch \"$ref\" may want to get merged. Do it?" '{ git log HEAD.."$ref"; git diff HEAD..."$ref"; } | less'; then
636                                         git checkout master
637                                         org=`git rev-parse HEAD`
638                                         if ! git merge "$ref" 2>&1 | tee "$t"; then
639                                                 git reset --hard "$org"
640                                                 git notes --ref "refs/notes/admin-merge" add -m "Merge failed:
641 `cat "$t"`" "$ref"
642                                         elif ! "$SELF" compile 2>&1 | tee "$t"; then
643                                                 git reset --hard "$org"
644                                                 git notes --ref "refs/notes/admin-merge" add -m "Compile failed:
645 `cat "$t"`" "$ref"
646                                         elif ! yesno "Still merge \"$ref\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
647                                                 git reset --hard "$org"
648                                                 git notes --ref "refs/notes/admin-merge" add "$ref"
649                                         else
650                                                 case ",`repoflags "$d"`," in
651                                                         *,svn,*)
652                                                                 git pull
653                                                                 git rebase -i "$org"
654                                                                 git svn dcommit --add-author-from
655                                                                 ;;
656                                                         *)
657                                                                 git push origin HEAD
658                                                                 ;;
659                                                 esac
660                                                 if yesno "Delete original branch \"$ref\"?"; then
661                                                         git push origin :"${ref#refs/remotes/origin/}"
662                                                 fi
663                                         fi
664                                 else
665                                         git notes --ref "refs/notes/admin-merge" add "$ref"
666                                 fi
667                         done
668                 done
669                 rm -f "$t"
670                 ;;
671         *)
672                 echo "Usage:"
673                 echo "  $SELF pull"
674                 echo "  $SELF merge"
675                 echo "  $SELF push [-s]"
676                 echo "  $SELF branches"
677                 echo "  $SELF branch [<remote>] <branchname>"
678                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
679                 echo "  $SELF checkout [<remote>] <branchname>"
680                 echo "  $SELF compile [-c] [<client>] <options>"
681                 echo "  $SELF run [<client>] <options>"
682                 echo "  $SELF each <command>"
683                 ;;
684 esac