./all - handle upstream rebase better (abort, instead of causing a frankenstein merge)
[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 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         verbose 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"
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
158                                 r=`git symbolic-ref HEAD`
159                                 r=${r#refs/heads/}
160                                 if git config branch.$r.remote >/dev/null 2>&1; then
161                                         if ! verbose git pull; then
162                                                 check_mergeconflict "$d"
163                                                 exit 1 # FATAL
164                                         fi
165                                 fi
166
167                                 cd "$d00"
168                                 checkself "$cmd" "$@"
169                                 cd "$d0/$d"
170                                 verbose git remote prune origin
171                                 cd "$d0"
172                         else
173                                 verbose git clone "$url" "$d0/$d"
174                         fi
175                 done
176                 ;;
177         checkout|switch)
178                 remote=$1
179                 branch=$2
180                 if [ -z "$branch" ]; then
181                         branch=$remote
182                         remote=origin
183                 fi
184                 exists=false
185                 for d in $repos; do
186                         enter "$d0/$d"
187                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
188                                 exists=true
189                                 verbose git checkout "$branch"
190                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
191                                 exists=true
192                                 verbose git checkout --track -b "$branch" "$remote/$branch"
193                         else
194                                 verbose git checkout master
195                         fi
196                         cd "$d00"
197                         checkself "$cmd" "$@"
198                         cd "$d0"
199                 done
200                 if ! $exists; then
201                         echo "The requested branch was not found in any repository."
202                 fi
203                 exec "$SELF" branch
204                 ;;
205         branch)
206                 remote=$1
207                 branch=$2
208                 srcbranch=$3
209                 if [ -z "$branch" ]; then
210                         branch=$remote
211                         remote=origin
212                 fi
213                 if [ -z "$srcbranch" ]; then
214                         srcbranch=master
215                 fi
216                 if [ -z "$branch" ]; then
217                         for d in $repos; do
218                                 enter "$d0/$d"
219                                 r=`git symbolic-ref HEAD`
220                                 r=${r#refs/heads/}
221                                 echo "$d is at $r"
222                                 cd "$d0"
223                         done
224                 else
225                         for d in $repos; do
226                                 dv=`visible_repo_name "$d"`
227                                 enter "$d0/$d"
228                                 a=
229                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
230                                         echo "Branch in $dv?"
231                                         read -r a
232                                 done
233                                 if [ x"$a" = x"y" ]; then
234                                         verbose git push "$remote" "$srcbranch":"$branch"
235                                         verbose git checkout --track -b "$branch" "$remote/$branch"
236                                 fi
237                                 cd "$d0"
238                         done
239                         "$SELF" branch
240                 fi
241                 ;;
242         branches)
243                 for d in $repos; do
244                         enter "$d0/$d"
245                         echo "In $d:"
246                         git branch -a | sed 's/^/  /; /->/d'
247                         cd "$d0"
248                 done
249                 ;;
250         merge)
251                 for d in $repos; do
252                         dv=`visible_repo_name "$d"`
253                         enter "$d0/$d"
254                         r=`git symbolic-ref HEAD`
255                         r=${r#refs/heads/}
256                         if git log HEAD..origin/master | grep .; then
257                                 # we have uncommitted changes
258                                 a=
259                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
260                                         echo "Could merge from \"master\" into \"$r\" in $dv. Do it?"
261                                         read -r a
262                                 done
263                                 if [ x"$a" = x"y" ]; then
264                                         if ! verbose git merge origin/master; then
265                                                 check_mergeconflict "$d"
266                                                 exit 1 # this should ALWAYS be fatal
267                                         fi
268                                 fi
269                         fi
270                         cd "$d0"
271                 done
272                 ;;
273         push)
274                 for d in $repos; do
275                         dv=`visible_repo_name "$d"`
276                         enter "$d0/$d"
277                         r=`git symbolic-ref HEAD`
278                         r=${r#refs/heads/}
279                         if git diff HEAD | grep .; then
280                                 # we have uncommitted changes
281                                 a=
282                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
283                                         echo "Uncommitted changes in \"$r\" in $dv. Commit?"
284                                         read -r a
285                                 done
286                                 if [ x"$a" = x"y" ]; then
287                                         verbose git commit -a
288                                 fi
289                         fi
290                         if git log "origin/$r".."$r" | grep .; then
291                                 a=
292                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
293                                         echo "Push \"$r\" in $dv?"
294                                         read -r a
295                                 done
296                                 if [ x"$a" = x"y" ]; then
297                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
298                                 fi
299                         fi
300                         cd "$d0"
301                 done
302                 ;;
303         compile)
304                 if [ -z "$MAKEFLAGS" ]; then
305                         if [ -f /proc/cpuinfo ]; then
306                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
307                                 if [ $ncpus -gt 1 ]; then
308                                         MAKEFLAGS=-j$ncpus
309                                 fi
310                         fi
311                 fi
312                 enter "$d0/fteqcc"
313                 verbose make $MAKEFLAGS
314                 enter "$d0/data/xonotic-data.pk3dir"
315                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
316                 enter "$d0/darkplaces"
317                 verbose make $MAKEFLAGS sv-debug
318                 verbose make $MAKEFLAGS cl-debug
319                 verbose make $MAKEFLAGS sdl-debug
320                 ;;
321         run)
322                 client=-sdl
323                 case "$1" in
324                         sdl|glx|agl|dedicated)
325                                 client=-$1
326                                 shift
327                                 ;;
328                         wgl)
329                                 client=
330                                 shift
331                                 ;;
332                 esac
333                 if ! [ -x "darkplaces/darkplaces$client" ]; then
334                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
335                                 client=$client.exe
336                         else
337                                 echo "Client darkplaces/darkplaces$client not found, aborting"
338                                 exit 1
339                         fi
340                 fi
341                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
342                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
343                 ;;
344         each|foreach)
345                 for d in $repos; do
346                         enter "$d0/$d"
347                         verbose "$@"
348                         cd "$d0"
349                 done
350                 ;;
351         *)
352                 echo "Usage:"
353                 echo "  $SELF pull"
354                 echo "  $SELF merge"
355                 echo "  $SELF push"
356                 echo "  $SELF branches"
357                 echo "  $SELF branch [<remote>] <branchname>"
358                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
359                 echo "  $SELF checkout [<remote>] <branchname>"
360                 echo "  $SELF compile"
361                 echo "  $SELF run <client> <options>"
362                 echo "  $SELF each <command>"
363                 ;;
364 esac