new feature: "./all merge" - merges changes from master into your branches (to be...
[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*)
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 repos_urls="
74         .
75         data/xonotic-data.pk3dir
76         data/xonotic-maps.pk3dir
77         data/xonotic-music.pk3dir
78         data/xonotic-nexcompat.pk3dir
79         darkplaces
80         fteqcc@git://github.com/Blub/qclib.git
81         div0-gittools@git://git.icculus.org/divverent/div0-gittools.git
82         netradiant
83 "
84
85 repos=`for X in $repos_urls; do echo "${X%%@*}"; done`
86
87 if [ "$#" = 0 ]; then
88         set -- help
89 fi
90 cmd=$1
91 shift
92
93 case "$cmd" in
94         update|pull)
95                 base=`git config remote.origin.url`
96                 base=${base%xonotic.git}
97                 for dcomplete in $repos_urls; do
98                         case "$dcomplete" in
99                                 *@*)
100                                         d=${dcomplete%%@*}
101                                         url=${dcomplete#*@}
102                                         switch=false
103                                         ;;
104                                 *)
105                                         d=${dcomplete%%@*}
106                                         url=$base${d##*/}.git
107                                         switch=true
108                                         ;;
109                         esac
110                         if [ -d "$d0/$d" ]; then
111                                 verbose cd "$d0/$d"
112                                 case "$d" in
113                                         .)
114                                                 ;;
115                                         *)
116                                                 if $switch; then
117                                                         verbose git config remote.origin.url "$url"
118                                                 fi
119                                                 ;;
120                                 esac
121                                 verbose git pull || true # errors if the branch is not tracking anything
122                                 cd "$d00"
123                                 checkself "$SELF" "$@"
124                                 cd "$d0/$d"
125                                 verbose git remote prune origin
126                                 cd "$d0"
127                         else
128                                 verbose git clone "$url" "$d0/$d"
129                         fi
130                 done
131                 ;;
132         checkout|switch)
133                 remote=$1
134                 branch=$2
135                 if [ -z "$branch" ]; then
136                         branch=$remote
137                         remote=origin
138                 fi
139                 exists=false
140                 for d in $repos; do
141                         verbose cd "$d0/$d"
142                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
143                                 exists=true
144                                 verbose git checkout "$branch"
145                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
146                                 exists=true
147                                 verbose git checkout --track -b "$branch" "$remote/$branch"
148                         else
149                                 verbose git checkout master
150                         fi
151                         cd "$d00"
152                         checkself "$SELF" "$@"
153                         cd "$d0"
154                 done
155                 if ! $exists; then
156                         echo "The requested branch was not found in any repository."
157                 fi
158                 exec "$SELF" branch
159                 ;;
160         branch)
161                 remote=$1
162                 branch=$2
163                 if [ -z "$branch" ]; then
164                         branch=$remote
165                         remote=origin
166                 fi
167                 if [ -z "$branch" ]; then
168                         for d in $repos; do
169                                 cd "$d0/$d"
170                                 r=`git symbolic-ref HEAD`
171                                 r=${r#refs/heads/}
172                                 echo "$d is at $r"
173                                 cd "$d0"
174                         done
175                 else
176                         for d in $repos; do
177                                 cd "$d0/$d"
178                                 a=
179                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
180                                         echo "Branch in \"$d\"?"
181                                         read -r a
182                                 done
183                                 if [ x"$a" = x"y" ]; then
184                                         verbose git push "$remote" HEAD:"$branch"
185                                         verbose git checkout --track -b "$branch" "$remote/$branch"
186                                 fi
187                                 cd "$d0"
188                         done
189                         "$SELF" branch
190                 fi
191                 ;;
192         branches)
193                 for d in $repos; do
194                         cd "$d0/$d"
195                         echo "In $d:"
196                         git branch -a | sed 's/^/  /; /->/d'
197                         cd "$d0"
198                 done
199                 ;;
200         merge)
201                 for d in $repos; do
202                         cd "$d0/$d"
203                         r=`git symbolic-ref HEAD`
204                         r=${r#refs/heads/}
205                         if git log HEAD..origin/master | grep .; then
206                                 # we have uncommitted changes
207                                 a=
208                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
209                                         echo "Could merge from \"master\" into \"$r\" in \"$d\". Do it?"
210                                         read -r a
211                                 done
212                                 if [ x"$a" = x"y" ]; then
213                                         if ! verbose git merge origin/master; then
214                                                 echo
215                                                 echo "MERGE CONFLICT."
216                                                 echo "change into the \"$d\" project directory, and then:"
217                                                 echo "- edit the files mentioned above with your favorite editor,"
218                                                 echo "  and fix the conflicts (marked with <<<<<<< blocks)"
219                                                 echo "- when done with a file, 'git add' the file"
220                                                 echo "- when done, 'git commit'"
221                                                 exit 1
222                                         fi
223                                 fi
224                         fi
225                         cd "$d0"
226                 done
227                 ;;
228         push)
229                 for d in $repos; do
230                         cd "$d0/$d"
231                         r=`git symbolic-ref HEAD`
232                         r=${r#refs/heads/}
233                         if git diff HEAD | grep .; then
234                                 # we have uncommitted changes
235                                 a=
236                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
237                                         echo "Uncommitted changes in \"$r\" in \"$d\". Commit?"
238                                         read -r a
239                                 done
240                                 if [ x"$a" = x"y" ]; then
241                                         verbose git commit -a
242                                 fi
243                         fi
244                         if git log "origin/$r".."$r" | grep .; then
245                                 a=
246                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
247                                         echo "Push \"$r\" in \"$d\"?"
248                                         read -r a
249                                 done
250                                 if [ x"$a" = x"y" ]; then
251                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
252                                 fi
253                         fi
254                         cd "$d0"
255                 done
256                 ;;
257         compile)
258                 if [ -z "$MAKEFLAGS" ]; then
259                         if [ -f /proc/cpuinfo ]; then
260                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
261                                 if [ $ncpus -gt 1 ]; then
262                                         MAKEFLAGS=-j$ncpus
263                                 fi
264                         fi
265                 fi
266                 verbose cd "$d0/fteqcc"
267                 verbose make $MAKEFLAGS
268                 verbose cd "$d0/data/xonotic-data.pk3dir"
269                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
270                 verbose cd "$d0/darkplaces"
271                 verbose make $MAKEFLAGS sv-debug
272                 verbose make $MAKEFLAGS cl-debug
273                 verbose make $MAKEFLAGS sdl-debug
274                 ;;
275         run)
276                 client=-sdl
277                 case "$1" in
278                         sdl|glx|agl|dedicated)
279                                 client=-$1
280                                 shift
281                                 ;;
282                         wgl)
283                                 client=
284                                 shift
285                                 ;;
286                 esac
287                 if ! [ -x "darkplaces/darkplaces$client" ]; then
288                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
289                                 client=$client.exe
290                         else
291                                 echo "Client darkplaces/darkplaces$client not found, aborting"
292                                 exit 1
293                         fi
294                 fi
295                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
296                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
297                 ;;
298         each|foreach)
299                 for d in $repos; do
300                         verbose cd "$d0/$d"
301                         verbose "$@"
302                         cd "$d0"
303                 done
304                 ;;
305         *)
306                 echo "Usage:"
307                 echo "  $SELF pull"
308                 echo "  $SELF merge"
309                 echo "  $SELF push"
310                 echo "  $SELF branches"
311                 echo "  $SELF branch [<remote>] <branchname>"
312                 echo "  $SELF checkout [<remote>] <branchname>"
313                 echo "  $SELF compile"
314                 echo "  $SELF run <client> <options>"
315                 echo "  $SELF each <command>"
316                 ;;
317 esac