./all: make "push" autodetect uncommitted changes, and offer commit
[xonotic/xonotic.git] / all
1 #!/bin/sh
2
3 set -e
4
5 msg()
6 {
7         echo "\e[1m$*\e[m"
8 }
9
10 self=`cksum "$0"`
11 checkself()
12 {
13         self_new=`cksum "$0"`
14         if [ x"$self" != x"$self_new" ]; then
15                 msg "./all has changed."
16                 if [ -z "$XONOTIC_FORBID_RERUN_ALL" ]; then
17                         msg "Rerunning the requested operation to make sure."
18                         export XONOTIC_FORBID_RERUN_ALL=1
19                         exec "$0" "$@"
20                 else
21                         msg "Please try $0 update, and then retry your requested operation."
22                         exit 1
23                 fi
24         fi
25         return 0
26 }
27
28 verbose()
29 {
30         msg "+ $*"
31         "$@"
32 }
33
34 repos_urls="
35         .
36         data/xonotic-data.pk3dir
37         data/xonotic-maps.pk3dir
38         data/xonotic-music.pk3dir
39         data/xonotic-nexcompat.pk3dir
40         darkplaces
41         fteqcc@git://github.com/Blub/qclib.git
42         div0-gittools@git://git.icculus.org/divverent/div0-gittools.git
43         netradiant
44 "
45
46 repos=`for X in $repos_urls; do echo "${X%%@*}"; done`
47
48 if [ "$#" = 0 ]; then
49         set -- help
50 fi
51 cmd=$1
52 shift
53
54 d0=`pwd`
55 case "$cmd" in
56         update|pull)
57                 base=`git config remote.origin.url`
58                 base=${base%xonotic.git}
59                 for dcomplete in $repos_urls; do
60                         case "$dcomplete" in
61                                 *@*)
62                                         d=${dcomplete%%@*}
63                                         url=${dcomplete#*@}
64                                         switch=false
65                                         ;;
66                                 *)
67                                         d=${dcomplete%%@*}
68                                         url=$base${d##*/}.git
69                                         switch=true
70                                         ;;
71                         esac
72                         if [ -d "$d0/$d" ]; then
73                                 verbose cd "$d0/$d"
74                                 case "$d" in
75                                         .)
76                                                 ;;
77                                         *)
78                                                 if $switch; then
79                                                         verbose git config remote.origin.url "$url"
80                                                 fi
81                                                 ;;
82                                 esac
83                                 verbose git pull
84                                 cd "$d0"
85                                 checkself "$0" "$@"
86                                 cd "$d0/$d"
87                                 verbose git remote prune origin
88                                 cd "$d0"
89                         else
90                                 verbose git clone "$url" "$d0/$d"
91                         fi
92                 done
93                 ;;
94         checkout|switch)
95                 remote=$1
96                 branch=$2
97                 if [ -z "$branch" ]; then
98                         branch=$remote
99                         remote=origin
100                 fi
101                 exists=false
102                 for d in $repos; do
103                         verbose cd "$d0/$d"
104                         if git rev-parse "refs/heads/$branch" >/dev/null 2>&1; then
105                                 exists=true
106                                 verbose git checkout "$branch"
107                         elif git rev-parse "refs/remotes/$remote/$branch" >/dev/null 2>&1; then
108                                 exists=true
109                                 verbose git checkout --track -b "$branch" "$remote/$branch"
110                         else
111                                 verbose git checkout master
112                         fi
113                         cd "$d0"
114                         checkself "$0" "$@"
115                 done
116                 if ! $exists; then
117                         echo "The requested branch was not found in any repository."
118                 fi
119                 "$0" branch
120                 ;;
121         branch)
122                 remote=$1
123                 branch=$2
124                 if [ -z "$branch" ]; then
125                         branch=$remote
126                         remote=origin
127                 fi
128                 if [ -z "$branch" ]; then
129                         for d in $repos; do
130                                 cd "$d0/$d"
131                                 r=`git symbolic-ref HEAD`
132                                 r=${r#refs/heads/}
133                                 echo "$d is at $r"
134                                 cd "$d0"
135                         done
136                 else
137                         for d in $repos; do
138                                 cd "$d0/$d"
139                                 a=
140                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
141                                         echo "Branch in \"$d\"?"
142                                         read -r a
143                                 done
144                                 if [ x"$a" = x"y" ]; then
145                                         verbose git push "$remote" HEAD:"$branch"
146                                         verbose git checkout --track -b "$branch" "$remote/$branch"
147                                 fi
148                                 cd "$d0"
149                         done
150                         "$0" branch
151                 fi
152                 ;;
153         branches)
154                 for d in $repos; do
155                         cd "$d0/$d"
156                         echo "In $d:"
157                         git branch -a | sed 's/^/  /; /->/d'
158                         cd "$d0"
159                 done
160                 ;;
161         push)
162                 for d in $repos; do
163                         cd "$d0/$d"
164                         r=`git symbolic-ref HEAD`
165                         r=${r#refs/heads/}
166                         a=
167                         if git diff HEAD | grep .; then
168                                 # we have uncommitted changes
169                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
170                                         echo "Uncommitted changes in \"$r\" in \"$d\". Commit?"
171                                         read -r a
172                                 done
173                                 if [ x"$a" = x"y" ]; then
174                                         verbose git commit -a
175                                 fi
176                         fi
177                         if git log "origin/$r".."$r" | grep .; then
178                                 while [ x"$a" != x"y" -a x"$a" != x"n" ]; do
179                                         echo "Push \"$r\" in \"$d\"?"
180                                         read -r a
181                                 done
182                                 if [ x"$a" = x"y" ]; then
183                                         verbose git push `git config "branch.$r.remote" || echo origin` HEAD
184                                 fi
185                         fi
186                         cd "$d0"
187                 done
188                 ;;
189         compile)
190                 if [ -z "$MAKEFLAGS" ]; then
191                         if [ -f /proc/cpuinfo ]; then
192                                 ncpus=$((`grep -c '^processor   :' /proc/cpuinfo`+0))
193                                 if [ $ncpus -gt 1 ]; then
194                                         MAKEFLAGS=-j$ncpus
195                                 fi
196                         fi
197                 fi
198                 verbose cd "$d0/fteqcc"
199                 verbose make $MAKEFLAGS
200                 verbose cd "$d0/data/xonotic-data.pk3dir"
201                 verbose make FTEQCC="$d0/fteqcc/fteqcc.bin" $MAKEFLAGS
202                 verbose cd "$d0/darkplaces"
203                 verbose make $MAKEFLAGS sv-debug
204                 verbose make $MAKEFLAGS cl-debug
205                 verbose make $MAKEFLAGS sdl-debug
206                 ;;
207         run)
208                 client=-sdl
209                 case "$1" in
210                         sdl|glx|agl|dedicated)
211                                 client=-$1
212                                 shift
213                                 ;;
214                         wgl)
215                                 client=
216                                 shift
217                                 ;;
218                 esac
219                 if ! [ -x "darkplaces/darkplaces$client" ]; then
220                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
221                                 client=$client.exe
222                         else
223                                 echo "Client darkplaces/darkplaces$client not found, aborting"
224                                 exit 1
225                         fi
226                 fi
227                 #verbose "darkplaces/darkplaces$client" -xonotic "$@"
228                 verbose "darkplaces/darkplaces$client" -nexuiz -customgamename Xonotic -customgamedirname1 data -customgamedirname2 "" -customgamescreenshotname xonotic -customgameuserdirname xonotic "$@"
229                 ;;
230         each|foreach)
231                 for d in $repos; do
232                         verbose cd "$d0/$d"
233                         verbose "$@"
234                         cd "$d0"
235                 done
236                 ;;
237         *)
238                 echo "Usage:"
239                 echo "  $0 pull"
240                 echo "  $0 push"
241                 echo "  $0 branches"
242                 echo "  $0 branch <remote> <branchname>"
243                 echo "  $0 checkout"
244                 echo "  $0 compile"
245                 echo "  $0 run <client> <options>"
246                 echo "  $0 each <command>"
247                 ;;
248 esac