./all: allow the branch command even if the branch already exists, skip the repos...
[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         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                                 if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
230                                         echo "Already having this branch in $dv."
231                                 else
232                                         while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
233                                                 echo "Branch in $dv?"
234                                                 read -r a
235                                         done
236                                         if [ x"$a" = x"y" ]; then
237                                                 verbose git push "$remote" "$srcbranch":"$branch"
238                                                 verbose git checkout --track -b "$branch" "$remote/$branch"
239                                         fi
240                                 fi
241                                 cd "$d0"
242                         done
243                         "$SELF" branch
244                 fi
245                 ;;
246         branches)
247                 for d in $repos; do
248                         enter "$d0/$d"
249                         echo "In $d:"
250                         git branch -a | sed 's/^/  /; /->/d'
251                         cd "$d0"
252                 done
253                 ;;
254         merge)
255                 for d in $repos; do
256                         dv=`visible_repo_name "$d"`
257                         enter "$d0/$d"
258                         r=`git symbolic-ref HEAD`
259                         r=${r#refs/heads/}
260                         if git log HEAD..origin/master | grep .; then
261                                 # we have uncommitted changes
262                                 a=
263                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
264                                         echo "Could merge from \"master\" into \"$r\" in $dv. Do it?"
265                                         read -r a
266                                 done
267                                 if [ x"$a" = x"y" ]; then
268                                         if ! verbose git merge origin/master; then
269                                                 check_mergeconflict "$d"
270                                                 exit 1 # this should ALWAYS be fatal
271                                         fi
272                                 fi
273                         fi
274                         cd "$d0"
275                 done
276                 ;;
277         push|commit)
278                 for d in $repos; do
279                         dv=`visible_repo_name "$d"`
280                         enter "$d0/$d"
281                         r=`git symbolic-ref HEAD`
282                         r=${r#refs/heads/}
283                         if git diff HEAD | grep .; then
284                                 # we have uncommitted changes
285                                 a=
286                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
287                                         echo "Uncommitted changes in \"$r\" in $dv. Commit?"
288                                         read -r a
289                                 done
290                                 if [ x"$a" = x"y" ]; then
291                                         verbose git commit -a
292                                 fi
293                         fi
294                         if git log "origin/$r".."$r" | grep .; then
295                                 a=
296                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
297                                         echo "Push \"$r\" in $dv?"
298                                         read -r a
299                                 done
300                                 if [ x"$a" = x"y" ]; then
301                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
302                                 fi
303                         fi
304                         cd "$d0"
305                 done
306                 ;;
307         compile)
308                 if [ -z "$MAKEFLAGS" ]; then
309                         if [ -f /proc/cpuinfo ]; then
310                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
311                                 if [ $ncpus -gt 1 ]; then
312                                         MAKEFLAGS=-j$ncpus
313                                 fi
314                         fi
315                 fi
316                 enter "$d0/fteqcc"
317                 verbose make $MAKEFLAGS
318                 enter "$d0/data/xonotic-data.pk3dir"
319                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
320                 enter "$d0/darkplaces"
321                 verbose make $MAKEFLAGS sv-debug
322                 verbose make $MAKEFLAGS cl-debug
323                 verbose make $MAKEFLAGS sdl-debug
324                 ;;
325         run)
326                 client=-sdl
327                 case "$1" in
328                         sdl|glx|agl|dedicated)
329                                 client=-$1
330                                 shift
331                                 ;;
332                         wgl)
333                                 client=
334                                 shift
335                                 ;;
336                 esac
337                 if ! [ -x "darkplaces/darkplaces$client" ]; then
338                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
339                                 client=$client.exe
340                         else
341                                 echo "Client darkplaces/darkplaces$client not found, aborting"
342                                 exit 1
343                         fi
344                 fi
345                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
346                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
347                 ;;
348         each|foreach)
349                 for d in $repos; do
350                         enter "$d0/$d"
351                         verbose "$@"
352                         cd "$d0"
353                 done
354                 ;;
355         *)
356                 echo "Usage:"
357                 echo "  $SELF pull"
358                 echo "  $SELF merge"
359                 echo "  $SELF push"
360                 echo "  $SELF branches"
361                 echo "  $SELF branch [<remote>] <branchname>"
362                 echo "  $SELF branch <remote> <branchname> <srcbranchname>"
363                 echo "  $SELF checkout [<remote>] <branchname>"
364                 echo "  $SELF compile"
365                 echo "  $SELF run <client> <options>"
366                 echo "  $SELF each <command>"
367                 ;;
368 esac