try to autodetect and fix upstream rebase
[xonotic/xonotic.git] / all
1 #!/bin/sh
2 # vim: filetype=zsh
3
4 set -e
5
6 # I use this in EVERY shell script ;)
7 LF="
8 "
9
10 d00=`pwd`
11 while ! [ -f ./all ]; do
12         if [ x"`pwd`" = x"/" ]; then
13                 echo "Cannot find myself."
14                 echo "Please run this script with the working directory inside a Xonotic checkout."
15                 exit 1
16         fi
17         cd ..
18 done
19 d0=`pwd`
20 SELF="$d0/all"
21
22 # If we are on WINDOWS:
23 case "$0" in
24         all|*/all)
25                 case "`uname`" in
26                         MINGW*|Win*)
27                                 # Windows hates users. So this script has to copy itself elsewhere first...
28                                 cp "$SELF" ../all.xonotic.sh
29                                 export WE_HATE_OUR_USERS=1
30                                 exec ../all.xonotic.sh "$@"
31                                 ;;
32                 esac
33                 ;;
34 esac
35
36 msg()
37 {
38         echo "\e[1m$*\e[m"
39 }
40
41 self=`git hash-object "$SELF"`
42 checkself()
43 {
44         self_new=`git hash-object "$SELF"`
45         if [ x"$self" != x"$self_new" ]; then
46                 msg "./all has changed."
47                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
48                         msg "Rerunning the requested operation to make sure."
49                         export XONOTIC_FORBID_RERUN_ALL=1
50                         exec "$SELF" "$@"
51                 else
52                         msg "Please try $SELF update, and then retry your requested operation."
53                         exit 1
54                 fi
55         fi
56         return 0
57 }
58
59 verbose()
60 {
61         msg "+ $*"
62         "$@"
63 }
64
65 visible_repo_name()
66 {
67         case "$1" in
68                 .)
69                         echo "the root directory"
70                         ;;
71                 *)
72                         echo "\"$1\""
73                         ;;
74         esac
75 }
76
77 check_mergeconflict()
78 {
79         if git ls-files -u | grep ' 1   '; then
80                 echo
81                 echo "MERGE CONFLICT."
82                 echo "change into the \"$1\" project directory, and then:"
83                 echo "- edit the files mentioned above with your favorite editor,"
84                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
85                 echo "- for binary files, you can select the files using"
86                 echo "  git checkout --ours or git checkout --theirs"
87                 echo "- when done with a file, 'git add' the file"
88                 echo "- when done, 'git commit'"
89                 echo
90                 exit 1
91         fi
92 }
93
94 yesno()
95 {
96         yesno=
97         while [ x"$yesno" != x"y" -a x"$yesno" != x"n" ]; do
98                 eval "$2"
99                 echo "$1"
100                 IFS= read -r yesno
101         done
102         [ x"$yesno" = x"y" ]
103 }
104
105 enter()
106 {
107         $2 cd "$1" || exit 1
108         check_mergeconflict "$1"
109 }
110
111 repos_urls="
112 .                             |                                                   | master      |
113 data/xonotic-data.pk3dir      |                                                   | master      |
114 data/xonotic-maps.pk3dir      |                                                   | master      |
115 data/xonotic-music.pk3dir     |                                                   | master      |
116 data/xonotic-nexcompat.pk3dir |                                                   | master      |
117 mediasource                   |                                                   | master      |
118 darkplaces                    |                                                   | div0-stable | svn
119 fteqcc                        | git://github.com/Blub/qclib.git                   | master      |
120 div0-gittools                 | git://git.icculus.org/divverent/div0-gittools.git | master      |
121 netradiant                    |                                                   | master      |
122 "
123 # todo: in darkplaces, change repobranch to div0-stable
124
125 repos=`echo "$repos_urls" | grep . | cut -d '|' -f 1 | tr -d ' '`
126
127 base=`git config remote.origin.url`
128 case "$base" in
129         */xonotic.git)
130                 base=${base%xonotic.git}
131                 ;;
132         *)
133                 echo "The main repo is not xonotic.git, what have you done?"
134                 exit 1
135                 ;;
136 esac
137
138 repourl()
139 {
140         repo_t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 2 | tr -d ' '`
141         if [ -n "$repo_t" ]; then
142                 case "$repo_t" in
143                         *://*)
144                                 echo "$repo_t"
145                                 ;;
146                         *)
147                                 echo "$base$repo_t"
148                                 ;;
149                 esac
150         else
151                 if [ x"$1" = x"." ]; then
152                         echo "$base""xonotic.git"
153                 else
154                         echo "$base${1##*/}.git"
155                 fi
156         fi
157 }
158
159 repobranch()
160 {
161         repo_t=`echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 3 | tr -d ' '`
162         if [ -n "$repo_t" ]; then
163                 echo "$repo_t"
164         else
165                 echo "master"
166         fi
167 }
168
169 repoflags()
170 {
171         echo "$repos_urls" | grep "^$1 " | cut -d '|' -f 4 | tr -d ' '
172 }
173
174 repos=`for d in $repos; do
175         p="${d%dir}"
176         if [ x"$p" = x"$d" ] || [ -d "$d" ] || ! { [ -f "$d.no" ] || [ -f "$p" ]; }; then
177                 echo "$d"
178         fi
179 done`
180
181 if [ "$#" = 0 ]; then
182         set -- help
183 fi
184 cmd=$1
185 shift
186
187 fix_upstream_rebase()
188 {
189         if [ -z "$r_me" ] || [ -z "$r_other" ]; then
190                 return
191         fi
192         r_base=`git merge-base "$r_me" "$r_other"`
193
194         # no merge-base? upstream did filter-branch
195         if [ -n "$r_base" ]; then
196                 # otherwise, check if the two histories are "similar"
197                 r_l_me=`git log --pretty="format:%s" "$r_other".."$r_me" | grep -v "^Merged" | sort -u`
198                 r_l_other=`git log --pretty="format:%s" "$r_me".."$r_other" | grep -v "^Merged" | sort -u`
199
200                 # heuristics: upstream rebase/filter-branch if more than 50% of the commits of one of the sides are in the other too
201                 r_lc_me=`echo "$r_l_me" | wc -l`
202                 r_lc_other=`echo "$r_l_other" | wc -l`
203                 r_lc_together=`{ echo "$r_l_me"; echo "$r_l_other"; } | sort -u | wc -l`
204                 r_lc_same=$(($r_lc_me + $r_lc_other - $r_lc_together))
205
206                 if [ $(( $r_lc_same * 2 )) -le $(( $r_lc_me )) ] && [ $(( $r_lc_same * 2 )) -le $(( $r_lc_other )) ]; then
207                         if yesno "Probable upstream rebase detected, automatically fix?" 'git log --oneline --graph --left-right "$r_l_other".."$r_l_me"'; then
208                                 git reset --hard "$r_me"
209                                 git pull --rebase
210                                 return 1
211                         fi
212                 fi
213         fi
214
215         return 0
216 }
217
218 fix_upstream_rebase_mergeok()
219 {
220         r_me=`git rev-parse --revs-only HEAD^1 2>/dev/null || true`
221         r_other=`git rev-parse --revs-only HEAD^2 2>/dev/null || true`
222         fix_upstream_rebase
223 }
224
225 fix_upstream_rebase_mergefail()
226 {
227         r_me=`git rev-parse --revs-only HEAD 2>/dev/null || true`
228         r_other=`git rev-parse --revs-only MERGE_HEAD 2>/dev/null || true`
229         fix_upstream_rebase
230 }
231
232 case "$cmd" in
233         fix_upstream_rebase)
234                 for d in $repos; do
235                         fix_upstream_rebase_mergefail && fix_upstream_rebase_mergeok
236                 done
237                 ;;
238         update|pull)
239                 allow_pull=true
240                 if [ x"$1" = x"-N" ]; then
241                         allow_pull=false
242                 fi
243                 for d in $repos; do
244                         url=`repourl "$d"`
245                         branch=`repobranch "$d"`
246                         if [ -d "$d0/$d" ]; then
247                                 if $allow_pull; then
248                                         enter "$d0/$d" verbose
249                                         verbose git config remote.origin.url "$url"
250                                         verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
251
252                                         verbose git config remote.origin.autocrlf input
253
254                                         r=`git symbolic-ref HEAD`
255                                         r=${r#refs/heads/}
256                                         if git config branch.$r.remote >/dev/null 2>&1; then
257                                                 if ! verbose git pull; then
258                                                         fix_upstream_rebase_mergefailed || true
259                                                         check_mergeconflict "$d"
260                                                         echo "Pulling failed. Press ENTER to continue, or Ctrl-C to abort."
261                                                         read -r DUMMY
262                                                 else
263                                                         fix_upstream_rebase_mergeok || true
264                                                 fi
265                                         fi
266
267                                         cd "$d00"
268                                         checkself "$cmd" "$@"
269                                         cd "$d0/$d"
270                                         verbose git remote prune origin
271                                         cd "$d0"
272                                 fi
273                         else
274                                 verbose git clone "$url" "$d0/$d"
275                                 enter "$d0/$d" verbose
276                                 if [ "$branch" != "master" ]; then
277                                         verbose git checkout --track -b "$branch" origin/"$branch"
278                                 fi
279                                 cd "$d0"
280                         fi
281                 done
282                 ;;
283         update-maps)
284                 misc/tools/xonotic-map-compiler-autobuild download
285                 ;;
286         checkout|switch)
287                 remote=$1
288                 branch=$2
289                 if [ -z "$branch" ]; then
290                         case "$remote" in
291                                 origin/*)
292                                         branch=${remote#origin/}
293                                         remote=origin
294                                         ;;
295                                 *)
296                                         branch=$remote
297                                         remote=origin
298                                         ;;
299                         esac
300                 fi
301                 exists=false
302                 for d in $repos; do
303                         enter "$d0/$d" verbose
304                         b=$branch
305                         if [ -n "$b" ] && git rev-parse "refs/heads/$b" >/dev/null 2>&1; then
306                                 exists=true
307                                 verbose git checkout "$b"
308                         elif [ -n "$b" ] && git rev-parse "refs/remotes/$remote/$b" >/dev/null 2>&1; then
309                                 exists=true
310                                 verbose git checkout --track -b "$b" "$remote/$b"
311                         else
312                                 b=`repobranch "$d"`
313                                 if git rev-parse "refs/heads/$b" >/dev/null 2>&1; then
314                                         exists=true
315                                         verbose git checkout "$b"
316                                 elif git rev-parse "refs/remotes/$remote/$b" >/dev/null 2>&1; then
317                                         exists=true
318                                         verbose git checkout --track -b "$b" "$remote/$b"
319                                 else
320                                         echo "WTF? Not even branch $b doesn't exist in $d"
321                                         exit 1
322                                 fi
323                         fi
324                         cd "$d00"
325                         checkself "$cmd" "$@"
326                         cd "$d0"
327                 done
328                 if ! $exists; then
329                         echo "The requested branch was not found in any repository."
330                 fi
331                 exec "$SELF" branch
332                 ;;
333         branch)
334                 remote=$1
335                 branch=$2
336                 srcbranch=$3
337                 if [ -z "$branch" ]; then
338                         branch=$remote
339                         remote=origin
340                 fi
341                 if [ -z "$branch" ]; then
342                         for d in $repos; do
343                                 enter "$d0/$d"
344                                 r=`git symbolic-ref HEAD`
345                                 r=${r#refs/heads/}
346                                 echo "$d is at $r"
347                                 cd "$d0"
348                         done
349                 else
350                         for d in $repos; do
351                                 dv=`visible_repo_name "$d"`
352                                 enter "$d0/$d" verbose
353                                 if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
354                                         echo "Already having this branch in $dv."
355                                 else
356                                         if yesno "Branch in $dv?"; then
357                                                 if [ -n "$srcbranch" ]; then
358                                                         b=$srcbranch
359                                                 else
360                                                         b=origin/"`repobranch "$d"`"
361                                                         verbose git fetch origin || true
362                                                 fi
363                                                 # TODO do this without pushing
364                                                 verbose git checkout -b "$branch" "$b"
365                                                 verbose git config "branch.$branch.remote" "$remote"
366                                                 verbose git config "branch.$branch.merge" "refs/heads/$branch"
367                                         fi
368                                 fi
369                                 cd "$d0"
370                         done
371                         "$SELF" branch
372                 fi
373                 ;;
374         branches)
375                 for d in $repos; do
376                         cd "$d0/$d" # am in a pipe, shouldn't use enter
377                         git branch -a -v -v | cut -c 3- | sed "s/^(no branch)/(no_branch)/" | sed "s,^,$d ,"
378                         cd "$d0"
379                 done | {
380                         branches_list=
381                         # branches_repos_*=
382                         while read -r d BRANCH REV UPSTREAM TEXT; do
383                                 if [ x"$BRANCH" = x"`repobranch "$d"`" ]; then
384                                         continue
385                                 fi
386                                 case "$UPSTREAM" in
387                                         \[*)
388                                                 UPSTREAM=${UPSTREAM#\[}
389                                                 UPSTREAM=${UPSTREAM%\]}
390                                                 UPSTREAM=${UPSTREAM%:*}
391                                                 ;;
392                                         *)
393                                                 TEXT="$UPSTREAM $TEXT"
394                                                 UPSTREAM=
395                                                 ;;
396                                 esac
397                                 if [ x"$REV" = x"->" ]; then
398                                         continue
399                                 fi
400                                 BRANCH=${BRANCH#remotes/}
401                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
402                                 branches_list="$branches_list $BRANCH" # TEH SORT MAKEZ IT UNIEQ
403                                 eval "r=\$branches_repos_$ID"
404                                 case "$UPSTREAM" in
405                                         '')
406                                                 r="$r $d"
407                                                 ;;
408                                         *)
409                                                 r="$r $d:$UPSTREAM"
410                                                 ;;
411                                 esac
412                                 eval "branches_repos_$ID=\$r"
413                         done
414                         echo -n "$branches_list" | xargs -n 1 echo | sort -u | while IFS= read -r BRANCH; do
415                                 ID=`echo "$BRANCH" | tr -c "A-Za-z0-9." "_"`
416                                 eval "r=\$branches_repos_$ID"
417                                 printf "%-60s %s\n" "$BRANCH" "$r"
418                                 #echo "$BRANCH: $r"
419                         done
420                 }
421                 ;;
422         merge)
423                 for d in $repos; do
424                         dv=`visible_repo_name "$d"`
425                         enter "$d0/$d" verbose
426                         r=`git symbolic-ref HEAD`
427                         r=${r#refs/heads/}
428                         if git log HEAD..origin/"`repobranch "$d"`" | grep .; then
429                                 # we have uncommitted changes
430                                 if yesno "Could merge from \"`repobranch "$d"`\" into \"$r\" in $dv. Do it?"; then
431                                         if ! verbose git merge origin/"`repobranch "$d"`"; then
432                                                 check_mergeconflict "$d"
433                                                 exit 1 # this should ALWAYS be fatal
434                                         fi
435                                 fi
436                         fi
437                         cd "$d0"
438                 done
439                 ;;
440         push|commit)
441                 submit=$1
442                 for d in $repos; do
443                         dv=`visible_repo_name "$d"`
444                         enter "$d0/$d" verbose
445                         r=`git symbolic-ref HEAD`
446                         r=${r#refs/heads/}
447                         diffdata=`git diff --color HEAD`
448                         if [ -n "$diffdata" ]; then
449                                 # we have uncommitted changes
450                                 if yesno "Uncommitted changes in \"$r\" in $dv. Commit?" 'echo "$diffdata" | less -r'; then
451                                         verbose git commit -a
452                                 fi
453                         fi
454                         rem=`git config "branch.$r.remote" || echo origin`
455                         bra=`git config "branch.$r.merge" || echo "$r"`
456                         upstream="$rem/${bra#refs/heads/}"
457                         if ! git rev-parse "$upstream" >/dev/null 2>&1; then
458                                 upstream="origin/`repobranch "$d"`"
459                         fi
460                         logdata=`git log --color "$upstream".."$r"`
461                         if [ -n "$logdata" ]; then
462                                 if yesno "Push \"$r\" in $dv?" 'echo "$logdata" | less -r'; then
463                                         verbose git push "$rem" HEAD
464                                 fi
465                         fi
466                         if [ x"$submit" = x"-s" ]; then
467                                 case "$r" in
468                                         */*)
469                                                 verbose git push "$rem" HEAD:"${bra%%/*}/finished/${bra#*/}"
470                                                 ;;
471                                 esac
472                         fi
473                         cd "$d0"
474                 done
475                 ;;
476         compile)
477                 if [ -n "$WE_HATE_OUR_USERS" ]; then
478                         TARGETS="sv-debug cl-debug"
479                         if [ -z "$CC" ]; then
480                                 export CC=gcc
481                         fi
482                 elif [ x"`uname`" = x"Darwin" ]; then
483                         case "`uname -r`" in
484                                 ?.*)
485                                         TARGETS="sv-debug cl-debug sdl-debug"
486                                         ;;
487                                 *)
488                                         # AGL cannot be compiled on systems with a kernel > 10.x (Snow Leopard)
489                                         TARGETS="sv-debug sdl-debug"
490                                         ;;
491                         esac
492                         export CC="gcc -I$PWD/misc/buildfiles/osx/Xonotic-SDL.app/Contents/Frameworks/SDL.framework/Headers -F$PWD/misc/buildfiles/osx/Xonotic-SDL.app/Contents/Frameworks"
493                 else
494                         TARGETS="sv-debug cl-debug sdl-debug"
495                 fi
496                 case "$1" in
497                         -c)
498                                 cleandp=true
499                                 cleanqcc=true
500                                 cleanqc=true
501                                 shift
502                                 ;;
503                         -n)
504                                 cleandp=false
505                                 cleanqcc=false
506                                 cleanqc=false
507                                 shift
508                                 ;;
509                         *)
510                                 cleandp=false
511                                 cleanqcc=false
512                                 cleanqc=true # version info
513                                 ;;
514                 esac
515                 if [ $# -gt 0 ] && [ x"$1" = x"" ]; then
516                         # if we give the command make the arg "", it will surely fail (invalid filename),
517                         # so better handle it as an empty client option
518                         BAD_TARGETS=" "
519                         shift
520                 elif [ -n "$1" ]; then
521                         BAD_TARGETS=
522                         TARGETS_SAVE=$TARGETS
523                         TARGETS=
524                         for X in $1; do
525                                 case "$X" in
526                                         sdl)
527                                                 TARGETS="$TARGETS sdl-debug"
528                                                 ;;
529                                         glx|agl|wgl)
530                                                 TARGETS="$TARGETS cl-debug"
531                                                 ;;
532                                         dedicated)
533                                                 TARGETS="$TARGETS sv-debug"
534                                                 ;;
535                                         *)
536                                                 BAD_TARGETS="$BAD_TARGETS $X"
537                                                 ;;
538                                 esac
539                         done
540                         if [ -n "$TARGETS" ]; then # at least a valid client
541                                 shift
542                         else # no valid client, let's assume this option is not meant to be a client then
543                                 TARGETS=$TARGETS_SAVE
544                                 BAD_TARGETS=
545                         fi
546                 fi
547                 if [ -z "$MAKEFLAGS" ]; then
548                         if [ -f /proc/cpuinfo ]; then
549                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
550                                 if [ $ncpus -gt 1 ]; then
551                                         MAKEFLAGS=-j$ncpus
552                                 fi
553                         fi
554                         case "`uname`" in
555                                 Linux|*BSD)
556                                         MAKEFLAGS="$MAKEFLAGS DP_LINK_TO_LIBJPEG=1"
557                                         ;;
558                         esac
559                         if [ -n "$WE_HATE_OUR_USERS" ]; then
560                                 MAKEFLAGS="$MAKEFLAGS DP_MAKE_TARGET=mingw"
561                         fi
562                 fi
563
564                 enter "$d0/fteqcc" verbose
565                 if $cleanqcc; then
566                         verbose make $MAKEFLAGS clean
567                 fi
568                 verbose make $MAKEFLAGS
569
570                 enter "$d0/data/xonotic-data.pk3dir" verbose
571                 if $cleanqc; then
572                         verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS clean
573                 fi
574                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" "$@" $MAKEFLAGS
575
576                 enter "$d0/darkplaces" verbose
577                 if [ x"$BAD_TARGETS" = x" " ]; then
578                         echo "Warning: invalid empty client, default clients will be used."
579                 fi
580                 if $cleandp; then
581                         verbose make $MAKEFLAGS clean
582                 fi
583                 for T in $TARGETS; do
584                         verbose make $MAKEFLAGS "$@" "$T"
585                 done
586                 for T in $BAD_TARGETS; do
587                         echo "Warning: discarded invalid client $T."
588                 done
589
590                 verbose "$SELF" update-maps
591                 ;;
592         run)
593                 if [ -n "$WE_HATE_OUR_USERS" ]; then
594                         client=
595                         export PATH="$d0/misc/buildfiles/win64:$d0/misc/buildfiles/win32:$PATH"
596                 elif [ x"`uname`" = x"Darwin" ]; then
597                         export DYLD_LIBRARY_PATH="$d0/misc/buildfiles/osx/Xonotic-SDL.app/Contents/MacOS"
598                         export DYLD_FRAMEWORK_PATH="$d0/misc/buildfiles/osx/Xonotic-SDL.app/Contents/Frameworks"
599                         client=-sdl
600                 else
601                         client=-sdl
602                 fi
603                 case "$1" in
604                         sdl|glx|agl|dedicated)
605                                 client=-$1
606                                 shift
607                                 ;;
608                         wgl)
609                                 client=
610                                 shift
611                                 ;;
612                 esac
613                 if ! [ -x "darkplaces/darkplaces$client" ]; then
614                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
615                                 client=$client.exe
616                         else
617                                 echo "Client darkplaces/darkplaces$client not found, aborting"
618                                 exit 1
619                         fi
620                 fi
621                 set -- "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic -mygames "$@"
622
623                 # if pulseaudio is running: USE IT
624                 if [ -z "$SDL_AUDIODRIVER" ] && ! [ -n "$WE_HATE_OUR_USERS" ] && ! [ x"`uname`" = x"Darwin" ]; then
625                         if ps -C pulseaudio >/dev/null; then
626                                 if ldd /usr/lib/libSDL.so 2>/dev/null | grep pulse >/dev/null; then
627                                         export SDL_AUDIODRIVER=pulse
628                                 fi
629                         fi
630                 fi
631
632                 if [ -n "$USE_GDB" ]; then
633                         set -- gdb --args "$@"
634                 fi
635                 "$@"
636                 ;;
637         each|foreach)
638                 keep_going=false
639                 if [ x"$1" = x"-k" ]; then
640                         keep_going=true
641                         shift
642                 fi
643                 for d in $repos; do
644                         if verbose cd "$d0/$d"; then
645                                 if $keep_going; then
646                                         verbose "$@" || true
647                                 else
648                                         verbose "$@"
649                                 fi
650                                 cd "$d0"
651                         fi
652                 done
653                 ;;
654         save-patches)
655                 outfile=$1
656                 patchdir=`mktemp -d -t save-patches.XXXXXX`
657                 for d in $repos; do
658                         enter "$d0/$d" verbose
659                         git branch -v -v | cut -c 3- | {
660                                 i=0
661                                 while read -r BRANCH REV UPSTREAM TEXT; do
662                                         case "$UPSTREAM" in
663                                                 \[*)
664                                                         UPSTREAM=${UPSTREAM#\[}
665                                                         UPSTREAM=${UPSTREAM%\]}
666                                                         UPSTREAM=${UPSTREAM%:*}
667                                                         TRACK=true
668                                                         ;;
669                                                 *)
670                                                         UPSTREAM=origin/"`repobranch "$d"`"
671                                                         TRACK=false
672                                                         ;;
673                                         esac
674                                         if [ x"$REV" = x"->" ]; then
675                                                 continue
676                                         fi
677                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
678                                                 echo "$d" > "$patchdir/$i/info.txt"
679                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
680                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
681                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
682                                                 i=$(($i+1))
683                                         else
684                                                 rm -rf "$patchdir/$i"
685                                         fi
686                                 done
687                         }
688                 done
689                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
690                 rm -rf "$patchdir"
691                 ;;
692         restore-patches)
693                 infile=$1
694                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
695                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
696                 # detach the head
697                 for P in "$patchdir"/*/info.txt; do
698                         D=${P%/info.txt}
699                         exec 3<"$P"
700                         read -r d <&3
701                         read -r BRANCH <&3
702                         read -r UPSTREAM <&3
703                         read -r TRACK <&3
704                         verbose git checkout HEAD^0
705                         verbose git branch -D "$BRANCH"
706                         if [ x"$TRACK" = x"true" ]; then
707                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
708                         else
709                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
710                         fi
711                         verbose git am "$D"
712                 done
713                 rm -rf "$patchdir"
714                 ;;
715         admin-merge)
716                 branch=$1
717                 t=`mktemp`
718                 report=""
719                 reportecho()
720                 {
721                         report=$report"$*$LF"
722                         echo "$*"
723                 }
724                 reportecho4()
725                 {
726                         report=$report"    $*$LF"
727                         echo "    $*"
728                 }
729                 reportdo4()
730                 {
731                         o=`"$@" | sed 's/^/    /' || true`
732                         reportecho "$o"
733                 }
734                 for d in $repos; do
735                         enter "$d0/$d" verbose
736                         base="`repobranch "$d"`"
737                         reportecho "In $d:"
738                         for ref in `git for-each-ref --format='%(refname)' refs/remotes/origin/`; do
739                                 case "${ref#refs/remotes/origin/}" in
740                                         "$base")
741                                                 continue
742                                                 ;;
743                                         HEAD|master)
744                                                 continue
745                                                 ;;
746                                         */*)
747                                                 ;;
748                                         *)
749                                                 continue
750                                                 ;;
751                                 esac
752                                 if [ -n "$branch" ]; then
753                                         if [ x"$branch" != x"${ref#refs/remotes/origin/}" ]; then
754                                                 continue
755                                         fi
756                                 fi
757                                 case "$base" in
758                                         master)
759                                                 realbase=$base
760                                                 ;;
761                                         *)
762                                                 l0=`git rev-list "$base".."$ref" | wc -l`
763                                                 l1=`git rev-list master.."$ref" | wc -l`
764                                                 if [ $l0 -gt $l1 ]; then
765                                                         realbase=master
766                                                 else
767                                                         realbase=$base
768                                                 fi
769                                                 ;;
770                                 esac
771                                 reportecho "  Branch $ref:"
772                                 note=`GIT_NOTES_REF=refs/notes/admin-merge git notes show "$ref" 2>/dev/null || true`
773                                 logdata=`git log --color "$realbase".."$ref"`
774                                 if [ -z "$logdata" ]; then
775                                         reportecho4 "--> not merging, no changes vs master"
776                                         if yesno "Branch \"$ref\" probably should get deleted. Do it?" ''; then
777                                                 git push origin :"${ref#refs/remotes/origin/}"
778                                                 reportecho4 "--> branch deleted"
779                                         fi
780                                 else
781                                         diffdata=`git diff --color --find-copies-harder --ignore-space-change "$realbase"..."$ref"`
782                                         if [ -z "$diffdata" ]; then
783                                                 reportecho4 "--> not merging, no changes vs master, branch contains redundant history"
784                                                 if yesno "Branch \"$ref\" probably should get deleted. Do it?" '{ echo "$logdata"; } | less -r'; then
785                                                         git push origin :"${ref#refs/remotes/origin/}"
786                                                         reportecho4 "--> branch deleted"
787                                                 fi
788                                         elif [ -z "$branch" ] && [ -n "$note" ]; then
789                                                 reportdo4 echo "$note"
790                                                 reportecho4 "--> not merging, already had this one rejected before"
791                                         elif yesno "Branch \"$ref\" may want to get merged. Do it?" '{ echo "$logdata"; echo "$diffdata"; } | less -r'; then
792                                                 git checkout "$realbase"
793                                                 org=`git rev-parse HEAD`
794                                                 if ! git merge --no-ff "$ref" 2>&1 | tee "$t" && ! { git ls-files -u | grep ' 1   ' >/dev/null; }; then
795                                                         git reset --hard "$org"
796                                                         GIT_NOTES_REF=refs/notes/admin-merge git notes edit -m "Merge failed:$LF`cat "$t"`" "$ref"
797                                                         reportdo4 cat "$t"
798                                                         reportecho4 "--> merge failed"
799                                                 elif ! "$SELF" compile -n 2>&1 | tee "$t"; then
800                                                         git reset --hard "$org"
801                                                         GIT_NOTES_REF=refs/notes/admin-merge git notes edit -m "Compile failed:$LF`cat "$t"`" "$ref"
802                                                         reportdo4 cat "$t"
803                                                         reportecho4 "--> compile failed"
804                                                 elif ! yesno "Still merge \"$ref\" into `git symbolic-ref HEAD` of $d? Maybe you want to test first."; then
805                                                         git reset --hard "$org"
806                                                         GIT_NOTES_REF=refs/notes/admin-merge git notes edit "$ref"
807                                                         note=`GIT_NOTES_REF=refs/notes/admin-merge git notes show "$ref" 2>/dev/null || true`
808                                                         if [ x"$note" = x"del" ]; then
809                                                                 git push origin :"${ref#refs/remotes/origin/}"
810                                                                 reportecho4 "--> test failed, branch deleted"
811                                                         elif [ -n "$note" ]; then
812                                                                 reportdo4 echo "$note"
813                                                                 reportecho4 "--> test failed"
814                                                         else
815                                                                 reportecho4 "--> test failed, postponed"
816                                                         fi
817                                                 else
818                                                         echo "MERGING"
819                                                         case ",`repoflags "$d"`," in
820                                                                 *,svn,*)
821                                                                         # we do quite a mess here... luckily we know $org
822                                                                         git fetch # svn needs to be current
823                                                                         git rebase -i --onto origin/master "$org"
824                                                                         git svn dcommit --add-author-from
825                                                                         git reset --hard "$org"
826                                                                         ;;
827                                                                 *)
828                                                                         git push origin HEAD
829                                                                         ;;
830                                                         esac
831                                                         reportecho4 "--> MERGED"
832                                                         if yesno "Delete original branch \"$ref\"?"; then
833                                                                 git push origin :"${ref#refs/remotes/origin/}"
834                                                                 reportecho4 "--> branch deleted"
835                                                         fi
836                                                 fi
837                                         else
838                                                 GIT_NOTES_REF=refs/notes/admin-merge git notes edit "$ref"
839                                                 note=`GIT_NOTES_REF=refs/notes/admin-merge git notes show "$ref" 2>/dev/null || true`
840                                                 if [ x"$note" = x"del" ]; then
841                                                         git push origin :"${ref#refs/remotes/origin/}"
842                                                         reportecho4 "--> branch deleted"
843                                                 elif [ -n "$note" ]; then
844                                                         reportdo4 echo "$note"
845                                                         reportecho4 "--> rejected"
846                                                 else
847                                                         reportecho4 "--> postponed"
848                                                 fi
849                                         fi
850                                 fi
851                                 reportecho ""
852                         done
853                         reportecho ""
854                 done
855                 rm -f "$t"
856                 echo "$report" | ssh nexuiz@rm.endoftheinternet.org cat '>>' public_html/xonotic-merge-notes.txt
857                 ;;
858
859         # release building goes here
860         release-prepare)
861                 #"$SELF" each git clean -fxd
862                 rm -rf Xonotic
863                 mkdir -p Xonotic
864                 git archive --format=tar HEAD -- Docs misc server xonotic-linux-glx.sh xonotic-linux-sdl.sh misc/buildfiles | {
865                         cd Xonotic
866                         mkdir data fteqcc
867                         tar xvf -
868                         rm -rf misc/builddeps
869                         mv misc/buildfiles/win32/* .
870                         mv misc/buildfiles/win64/* .
871                         mv misc/buildfiles/osx/* .
872                         rm -rf misc/buildfiles
873                 }
874                 ;;
875         release-compile-run)
876                 host=$1
877                 buildpath=$2
878                 maketargets=$3
879                 makeflags=$4
880                 srcdir=$5
881                 targetfiles=$6
882                 case " $HOSTS_THAT_ARE_MYSELF " in
883                         *\ $host\ *)
884                                 rsync --delete -zvaSHPAX "$srcdir"/ "$buildpath/"
885                                 (
886                                         cd $buildpath
887                                         eval make clean $maketargets $makeflags
888                                 )
889                                 for f in $targetfiles; do
890                                         mv "$buildpath/${f%:*}" "${f##*:}"
891                                 done
892                                 ;;
893                         *)
894                                 rsync --delete -zvaSHPAX "$srcdir"/ "$host:$buildpath/"
895                                 ssh "$host" ". ~/.profile && cd $buildpath && make clean $maketargets $makeflags"
896                                 for f in $targetfiles; do
897                                         rsync -zvaSHPAX "$host:$buildpath/${f%:*}" "${f##*:}"
898                                 done
899                                 ;;
900                 esac
901                 ;;
902         release-compile)
903                 suffix=$1
904                 makeflags=$2
905                 fteqcc_maketargets=$3
906                 fteqcc_files=$4
907                 darkplaces_maketargets=$5
908                 darkplaces_files=$6
909                 "$SELF" release-compile-run "xonotic-build-$suffix" /tmp/fteqcc.build."$suffix" "$fteqcc_maketargets" "$makeflags" "fteqcc" "$fteqcc_files"
910                 "$SELF" release-compile-run "xonotic-build-$suffix" /tmp/Darkplaces.build."$suffix" "$darkplaces_maketargets" "$makeflags" "darkplaces" "$darkplaces_files"
911                 ;;
912         release-engine-win32)
913                 "$SELF" release-compile win32 \
914                         '"DP_MAKE_TARGET=mingw CC="i586-mingw32msvc-gcc -Wl,--dynamicbase -Wl,--nxcompat -g -DSUPPORTDIRECTX -DUSE_WSPIAPI_H -I$HOME/dp.win32/include -L$HOME/dp.win32/lib" WINDRES="i586-mingw32msvc-windres" SDL_CONFIG="$HOME/dp.win32/bin/sdl-config"' \
915                         win 'fteqcc.exe:Xonotic/fteqcc/fteqcc.exe' \
916                         debug 'darkplaces.exe:xonotic.exe darkplaces-sdl.exe:xonotic-sdl.exe darkplaces-dedicated.exe:xonotic-dedicated.exe'
917                 ;;
918         release-engine-win64)
919                 "$SELF" release-compile win64 \
920                         '"DP_MAKE_TARGET=mingw CC="amd64-mingw32msvc-gcc -Wl,--dynamicbase -Wl,--nxcompat -g -DSUPPORTDIRECTX -DUSE_WSPIAPI_H -I$HOME/dp.win64/include -L$HOME/dp.win64/lib" WINDRES="amd64-mingw32msvc-windres" SDL_CONFIG="$HOME/dp.win64/bin/sdl-config" LDFLAGS_WINCOMMON=""' \
921                         win 'fteqcc.exe:Xonotic/fteqcc/fteqcc-64.exe' \
922                         debug 'darkplaces.exe:xonotic-64.exe darkplaces-sdl.exe:xonotic-sdl-64.exe darkplaces-dedicated.exe:xonotic-dedicated-64.exe'
923                 ;;
924         release-engine-osx)
925                 "$SELF" release-compile osx \
926                         'CC="gcc -g -arch i386 -arch ppc -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -I$HOME/dp.osx/include -L$HOME/dp.osx/lib"' \
927                         all 'fteqcc.bin:Xonotic/fteqcc/fteqcc.osx' \
928                         sdl-debug 'darkplaces-sdl:Xonotic/Xonotic-SDL.app/Contents/MacOS/xonotic-osx-sdl-bin'
929                 ;;
930         release-engine-linux32)
931                 "$SELF" release-compile linux32 \
932                         'CC="gcc -m32 -g -Wl,--hash-style=sysv -I$HOME/dp.linux32/include -L$HOME/dp.linux32/lib" DP_MODPLUG_STATIC_LIBDIR=$HOME/dp.linux32/lib DP_LINK_TO_LIBJPEG=1' \
933                         all 'fteqcc.bin:Xonotic/fteqcc/fteqcc.linux32' \
934                         debug 'darkplaces-glx:Xonotic/xonotic-linux-linux32-glx darkplaces-sdl:Xonotic/xonotic-linux32-sdl darkplaces-dedicated:Xonotic/xonotic-linux32-dedicated'
935                 ;;
936         release-engine-linux64)
937                 "$SELF" release-compile linux64 \
938                         'CC="gcc -m64 -g -Wl,--hash-style=sysv -I$HOME/dp.linux64/include -L$HOME/dp.linux64/lib" DP_MODPLUG_STATIC_LIBDIR=$HOME/dp.linux64/lib DP_LINK_TO_LIBJPEG=1' \
939                         all 'fteqcc.bin:Xonotic/fteqcc/fteqcc.linux64' \
940                         debug 'darkplaces-glx:Xonotic/xonotic-linux-linux64-glx darkplaces-sdl:Xonotic/xonotic-linux64-sdl darkplaces-dedicated:Xonotic/xonotic-linux64-dedicated'
941                 ;;
942         release-engine)
943                 "$SELF" release-engine-win32 &
944                 "$SELF" release-engine-win64 &
945                 "$SELF" release-engine-osx &
946                 "$SELF" release-engine-linux32 &
947                 "$SELF" release-engine-linux64 &
948                 wait
949                 ;;
950         release-maps)
951                 "$SELF" update-maps
952                 for X in data/*-????????????????????????????????????????-????????????????????????????????????????.pk3; do
953                         if [ -f "$X" ]; then
954                                 cd Xonotic/data/xonotic-maps.pk3dir
955                                 unzip ../../../"$X"
956                                 cd ../../..
957                         fi
958                 done
959                 ;;
960         release-finish)
961                 # version numnber and stuff like that
962                 ;;
963         release-qc)
964                 verbose make -C data/xonotic-data.pk3dir FTEQCC="$d0/Xonotic/fteqcc/fteqcc.linux32" FTEQCCFLAGS_WATERMARK=
965                 ;;
966         release-buildpk3-transform-raw)
967                 dir=$1
968                 ;;
969         release-buildpk3-transform-normal)
970                 dir=$1
971                 cd "$dir"
972                 # texture: convert to jpeg and dds
973                 export do_jpeg=true
974                 export jpeg_qual_rgb=95
975                 export jpeg_qual_a=99
976                 export do_dds=true
977                 export dds_flags=
978                 export do_ogg=false
979                 find textures -type f -print0 | xargs -0 "$d0"/misc/tools/cached-converter.sh
980                 ;;
981         release-buildpk3-transform-low)
982                 dir=$1
983                 cd "$dir"
984                 # texture: convert to jpeg and dds
985                 # music: reduce bitrate
986                 export do_jpeg=true
987                 export jpeg_qual_rgb=95
988                 export jpeg_qual_a=99
989                 export do_dds=false
990                 export do_ogg=true
991                 export ogg_qual=1
992                 find textures sound/cdtracks -type f -print0 | xargs -0 "$d0"/misc/tools/cached-converter.sh
993                 ;;
994         release-buildpk3)
995                 src=$1
996                 dst=$2
997                 transform=$3
998                 case "$dst" in
999                         /*)
1000                                 ;;
1001                         */)
1002                                 dst="$PWD/$dst"
1003                                 ;;
1004                 esac
1005                 rm -rf Xonotic/temp
1006                 mkdir -p Xonotic/temp
1007                 {
1008                         cd "$src"
1009                         git archive --format=tar HEAD
1010                 } | {
1011                         cd Xonotic/temp
1012                         tar xvf -
1013                 }
1014                 if [ x"$src" = x"data/xonotic-data.pk3dir" ]; then
1015                         cp "$src"/progs.dat Xonotic/temp/
1016                         cp "$src"/csprogs.dat Xonotic/temp/
1017                         cp "$src"/menu.dat Xonotic/temp/
1018                 fi
1019                 "$SELF" release-buildpk3-transform-$transform "Xonotic/temp"
1020                 cd Xonotic/temp
1021                 zip -9r "../../$dst" . ########### 7za a -tzip -mx=9 "../../$dst" .
1022                 cd ../..
1023                 rm -rf Xonotic/temp
1024                 ;;
1025         release-buildpk3s)
1026                 src=$1
1027                 shift
1028                 while [ "$#" -gt 1 ]; do
1029                         "$SELF" release-buildpk3 "$src" "Xonotic/${src%.pk3dir}$2.pk3" "$1"
1030                         shift
1031                         shift
1032                 done
1033                 ;;
1034         release-pack)
1035                 "$SELF" release-buildpk3s data/font-dejavu.pk3dir                  raw ''
1036                 "$SELF" release-buildpk3s data/xonotic-data.pk3dir       normal '' raw '-raw' low '-low'
1037                 "$SELF" release-buildpk3s data/xonotic-maps.pk3dir       normal '' raw '-raw' low '-low'
1038                 "$SELF" release-buildpk3s data/xonotic-music.pk3dir      normal '' raw '-raw' low '-low'
1039                 "$SELF" release-buildpk3s data/xonotic-nexcompat.pk3dir                       low ''
1040                 ;;
1041         release)
1042                 "$SELF" release-prepare
1043                 "$SELF" release-engine
1044                 "$SELF" release-maps
1045                 "$SELF" release-finish
1046                 "$SELF" release-qc
1047                 "$SELF" release-pack
1048                 ;;
1049
1050         *)
1051                 echo "Usage:"
1052                 echo "  $SELF pull"
1053                 echo "  $SELF merge"
1054                 echo "  $SELF push [-s]"
1055                 echo "  $SELF branches"
1056                 echo "  $SELF branch [<remote>] <branchname>"
1057                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
1058                 echo "  $SELF checkout [<remote>] <branchname>"
1059                 echo "  $SELF compile [-c|-n] [<client>] <options>"
1060                 echo "  $SELF run [<client>] <options>"
1061                 echo "  $SELF each <command>"
1062                 ;;
1063 esac