]> git.xonotic.org Git - xonotic/xonotic.git/blob - all
4df3b9a1179b8f8c647aca7a32dbccb22d59b04c
[xonotic/xonotic.git] / all
1 #!/bin/sh
2
3 set -e
4
5 d00=`pwd`
6 while ! [ -f ./all ]; do
7         if [ x"`pwd`" = x"/" ]; then
8                 echo "Cannot find myself."
9                 echo "Please run this script with the working directory inside a Xonotic checkout."
10                 exit 1
11         fi
12         cd ..
13 done
14 d0=`pwd`
15 SELF="$d0/all"
16
17 # If we are on WINDOWS:
18 case "$0" in
19         all|*/all)
20                 case "`uname`" in
21                         MINGW*|Win*)
22                                 # Windows hates users. So this script has to copy itself elsewhere first...
23                                 tname=
24                                 cp "$SELF" ../all.xonotic.sh
25                                 exec ../all.xonotic.sh "$@"
26                                 ;;
27                 esac
28                 ;;
29 esac
30
31 msg()
32 {
33         echo "\e[1m$*\e[m"
34 }
35
36 checksum()
37 {
38         if [ -x /usr/bin/md5sum ]; then
39                 /usr/bin/md5sum "$@"
40         elif [ -x /bin/md5sum ]; then
41                 /bin/md5sum "$@"
42         elif [ -x /usr/bin/cksum ]; then
43                 /usr/bin/cksum "$@"
44         else
45                 echo "NOCHECKSUM"
46         fi
47 }
48
49 self=`checksum "$SELF"`
50 checkself()
51 {
52         self_new=`checksum "$SELF"`
53         if [ x"$self" != x"$self_new" ]; then
54                 msg "./all has changed."
55                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
56                         msg "Rerunning the requested operation to make sure."
57                         export XONOTIC_FORBID_RERUN_ALL=1
58                         exec "$SELF" "$@"
59                 else
60                         msg "Please try $SELF update, and then retry your requested operation."
61                         exit 1
62                 fi
63         fi
64         return 0
65 }
66
67 verbose()
68 {
69         msg "+ $*"
70         "$@"
71 }
72
73 visible_repo_name()
74 {
75         case "$1" in
76                 .)
77                         echo "the root directory"
78                         ;;
79                 *)
80                         echo "\"$1\""
81                         ;;
82         esac
83 }
84
85 check_mergeconflict()
86 {
87         if git ls-files -u | grep ' 1   '; then
88                 echo
89                 echo "MERGE CONFLICT."
90                 echo "change into the \"$1\" project directory, and then:"
91                 echo "- edit the files mentioned above with your favorite editor,"
92                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
93                 echo "- for binary files, you can select the files using"
94                 echo "  git checkout --ours or git checkout --theirs"
95                 echo "- when done with a file, 'git add' the file"
96                 echo "- when done, 'git commit'"
97                 echo
98                 exit 1
99         fi
100 }
101
102 enter()
103 {
104         $2 cd "$1"
105         check_mergeconflict "$1"
106 }
107
108 repos_urls="
109         .
110         data/xonotic-data.pk3dir
111         data/xonotic-maps.pk3dir
112         data/xonotic-music.pk3dir
113         data/xonotic-nexcompat.pk3dir
114         darkplaces
115         fteqcc@git://github.com/Blub/qclib.git
116         div0-gittools@git://git.icculus.org/divverent/div0-gittools.git
117         netradiant
118 "
119
120 repos=`for X in $repos_urls; do echo "${X%%@*}"; done`
121
122 if [ "$#" = 0 ]; then
123         set -- help
124 fi
125 cmd=$1
126 shift
127
128 case "$cmd" in
129         update|pull)
130                 base=`git config remote.origin.url`
131                 base=${base%xonotic.git}
132                 for dcomplete in $repos_urls; do
133                         case "$dcomplete" in
134                                 *@*)
135                                         d=${dcomplete%%@*}
136                                         url=${dcomplete#*@}
137                                         switch=false
138                                         ;;
139                                 *)
140                                         d=${dcomplete%%@*}
141                                         url=$base${d##*/}.git
142                                         switch=true
143                                         ;;
144                         esac
145                         if [ -d "$d0/$d" ]; then
146                                 enter "$d0/$d" verbose
147                                 case "$d" in
148                                         .)
149                                                 ;;
150                                         *)
151                                                 if $switch; then
152                                                         verbose git config remote.origin.url "$url"
153                                                 fi
154                                                 ;;
155                                 esac
156                                 verbose git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
157                                         # TODO remove this line later
158
159                                 r=`git symbolic-ref HEAD`
160                                 r=${r#refs/heads/}
161                                 if git config branch.$r.remote >/dev/null 2>&1; then
162                                         if ! verbose git pull; then
163                                                 check_mergeconflict "$d"
164                                                 exit 1 # FATAL
165                                         fi
166                                 fi
167
168                                 cd "$d00"
169                                 checkself "$cmd" "$@"
170                                 cd "$d0/$d"
171                                 verbose git remote prune origin
172                                 cd "$d0"
173                         else
174                                 verbose git clone "$url" "$d0/$d"
175                         fi
176                 done
177                 ;;
178         checkout|switch)
179                 remote=$1
180                 branch=$2
181                 if [ -z "$branch" ]; then
182                         branch=$remote
183                         remote=origin
184                 fi
185                 exists=false
186                 for d in $repos; do
187                         enter "$d0/$d" verbose
188                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
189                                 exists=true
190                                 verbose git checkout "$branch"
191                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
192                                 exists=true
193                                 verbose git checkout --track -b "$branch" "$remote/$branch"
194                         else
195                                 verbose git checkout master
196                         fi
197                         cd "$d00"
198                         checkself "$cmd" "$@"
199                         cd "$d0"
200                 done
201                 if ! $exists; then
202                         echo "The requested branch was not found in any repository."
203                 fi
204                 exec "$SELF" branch
205                 ;;
206         branch)
207                 remote=$1
208                 branch=$2
209                 srcbranch=$3
210                 if [ -z "$branch" ]; then
211                         branch=$remote
212                         remote=origin
213                 fi
214                 if [ -z "$srcbranch" ]; then
215                         srcbranch=master
216                 fi
217                 if [ -z "$branch" ]; then
218                         for d in $repos; do
219                                 enter "$d0/$d"
220                                 r=`git symbolic-ref HEAD`
221                                 r=${r#refs/heads/}
222                                 echo "$d is at $r"
223                                 cd "$d0"
224                         done
225                 else
226                         for d in $repos; do
227                                 dv=`visible_repo_name "$d"`
228                                 enter "$d0/$d" verbose
229                                 a=
230                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
231                                         echo "Branch in $dv?"
232                                         read -r a
233                                 done
234                                 if [ x"$a" = x"y" ]; then
235                                         verbose git push "$remote" "$srcbranch":"$branch"
236                                         verbose git checkout --track -b "$branch" "$remote/$branch"
237                                 fi
238                                 cd "$d0"
239                         done
240                         "$SELF" branch
241                 fi
242                 ;;
243         branches)
244                 for d in $repos; do
245                         enter "$d0/$d"
246                         echo "In $d:"
247                         git branch -a -v -v | cut -c 3- | while read -r BRANCH REV UPSTREAM TEXT; do
248                                 case "$UPSTREAM" in
249                                         \[*)
250                                                 UPSTREAM=${UPSTREAM#\[}
251                                                 UPSTREAM=${UPSTREAM%\]}
252                                                 UPSTREAM=${UPSTREAM%:*}
253                                                 ;;
254                                         *)
255                                                 TEXT="$UPSTREAM $TEXT"
256                                                 UPSTREAM=
257                                                 ;;
258                                 esac
259                                 if [ x"$REV" = x"->" ]; then
260                                         continue
261                                 fi
262                                 BRANCH=${BRANCH#remotes/}
263                                 echo -n "  $BRANCH"
264                                 if [ -n "$UPSTREAM" ]; then
265                                         echo -n " (tracking $UPSTREAM)"
266                                 fi
267                                 #echo " $TEXT"
268                                 echo
269                         done
270                 done
271                 ;;
272         merge)
273                 for d in $repos; do
274                         dv=`visible_repo_name "$d"`
275                         enter "$d0/$d" verbose
276                         r=`git symbolic-ref HEAD`
277                         r=${r#refs/heads/}
278                         if git log HEAD..origin/master | grep .; then
279                                 # we have uncommitted changes
280                                 a=
281                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
282                                         echo "Could merge from \"master\" into \"$r\" in $dv. Do it?"
283                                         read -r a
284                                 done
285                                 if [ x"$a" = x"y" ]; then
286                                         if ! verbose git merge origin/master; then
287                                                 check_mergeconflict "$d"
288                                                 exit 1 # this should ALWAYS be fatal
289                                         fi
290                                 fi
291                         fi
292                         cd "$d0"
293                 done
294                 ;;
295         push|commit)
296                 for d in $repos; do
297                         dv=`visible_repo_name "$d"`
298                         enter "$d0/$d" verbose
299                         r=`git symbolic-ref HEAD`
300                         r=${r#refs/heads/}
301                         if git diff HEAD | grep .; then
302                                 # we have uncommitted changes
303                                 a=
304                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
305                                         echo "Uncommitted changes in \"$r\" in $dv. Commit?"
306                                         read -r a
307                                 done
308                                 if [ x"$a" = x"y" ]; then
309                                         verbose git commit -a
310                                 fi
311                         fi
312                         if git log "origin/$r".."$r" | grep .; then
313                                 a=
314                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
315                                         echo "Push \"$r\" in $dv?"
316                                         read -r a
317                                 done
318                                 if [ x"$a" = x"y" ]; then
319                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
320                                 fi
321                         fi
322                         cd "$d0"
323                 done
324                 ;;
325         compile)
326                 if [ -z "$MAKEFLAGS" ]; then
327                         if [ -f /proc/cpuinfo ]; then
328                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
329                                 if [ $ncpus -gt 1 ]; then
330                                         MAKEFLAGS=-j$ncpus
331                                 fi
332                         fi
333                 fi
334                 enter "$d0/fteqcc" verbose
335                 verbose make $MAKEFLAGS
336                 enter "$d0/data/xonotic-data.pk3dir" verbose
337                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
338                 enter "$d0/darkplaces" verbose
339                 verbose make $MAKEFLAGS sv-debug
340                 verbose make $MAKEFLAGS cl-debug
341                 verbose make $MAKEFLAGS sdl-debug
342                 ;;
343         run)
344                 client=-sdl
345                 case "$1" in
346                         sdl|glx|agl|dedicated)
347                                 client=-$1
348                                 shift
349                                 ;;
350                         wgl)
351                                 client=
352                                 shift
353                                 ;;
354                 esac
355                 if ! [ -x "darkplaces/darkplaces$client" ]; then
356                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
357                                 client=$client.exe
358                         else
359                                 echo "Client darkplaces/darkplaces$client not found, aborting"
360                                 exit 1
361                         fi
362                 fi
363                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
364                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
365                 ;;
366         each|foreach)
367                 for d in $repos; do
368                         enter "$d0/$d" verbose
369                         verbose "$@"
370                         cd "$d0"
371                 done
372                 ;;
373         save-patches)
374                 outfile=$1
375                 patchdir=`mktemp -d -t save-patches.XXXXXX`
376                 for d in $repos; do
377                         enter "$d0/$d" verbose
378                         git branch -v -v | cut -c 3- | {
379                                 i=0
380                                 while read -r BRANCH REV UPSTREAM TEXT; do
381                                         case "$UPSTREAM" in
382                                                 \[*)
383                                                         UPSTREAM=${UPSTREAM#\[}
384                                                         UPSTREAM=${UPSTREAM%\]}
385                                                         UPSTREAM=${UPSTREAM%:*}
386                                                         TRACK=true
387                                                         ;;
388                                                 *)
389                                                         UPSTREAM=origin/master
390                                                         TRACK=false
391                                                         ;;
392                                         esac
393                                         if [ x"$REV" = x"->" ]; then
394                                                 continue
395                                         fi
396                                         if git format-patch -o "$patchdir/$i" "$UPSTREAM".."$BRANCH"; then
397                                                 echo "$d" > "$patchdir/$i/info.txt"
398                                                 echo "$BRANCH" >> "$patchdir/$i/info.txt"
399                                                 echo "$UPSTREAM" >> "$patchdir/$i/info.txt"
400                                                 echo "$TRACK" >> "$patchdir/$i/info.txt"
401                                                 i=$(($i+1))
402                                         else
403                                                 rm -rf "$patchdir/$i"
404                                         fi
405                                 done
406                         }
407                 done
408                 ( cd "$patchdir" && tar cvzf - . ) > "$outfile"
409                 rm -rf "$patchdir"
410                 ;;
411         restore-patches)
412                 infile=$1
413                 patchdir=`mktemp -d -t restore-patches.XXXXXX`
414                 ( cd "$patchdir" && tar xvzf - ) < "$infile"
415                 # detach the head
416                 for P in "$patchdir"/*/info.txt; do
417                         D=${P%/info.txt}
418                         exec 3<"$P"
419                         read -r d <&3
420                         read -r BRANCH <&3
421                         read -r UPSTREAM <&3
422                         read -r TRACK <&3
423                         verbose git checkout HEAD^0
424                         verbose git branch -D "$BRANCH"
425                         if [ x"$TRACK" = x"true" ]; then
426                                 verbose git checkout --track -b "$BRANCH" "$UPSTREAM"
427                         else
428                                 verbose git branch -b "$BRANCH" "$UPSTREAM"
429                         fi
430                         verbose git am "$D"
431                 done
432                 rm -rf "$patchdir"
433                 ;;
434         *)
435                 echo "Usage:"
436                 echo "  $SELF pull"
437                 echo "  $SELF merge"
438                 echo "  $SELF push"
439                 echo "  $SELF branches"
440                 echo "  $SELF branch [<remote>] <branchname>"
441                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
442                 echo "  $SELF checkout [<remote>] <branchname>"
443                 echo "  $SELF compile"
444                 echo "  $SELF run <client> <options>"
445                 echo "  $SELF each <command>"
446                 ;;
447 esac