Fix compiler warnings.
[openttd-joker.git] / findversion.sh
blob6be52b696f2a2291b3054558585e7005a8a4aa4a
1 #!/bin/sh
3 # $Id$
5 # This file is part of OpenTTD.
6 # OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
7 # OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 # See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
11 # Arguments given? Show help text.
12 if [ "$#" != "0" ]; then
13 cat <<EOF
14 Usage: ./findversion.sh
15 Finds the current revision and if the code is modified.
17 Output: <REV>\t<REV_NR>\t<MODIFIED>\t<CLEAN_REV>
18 REV
19 a string describing what version of the code the current checkout is
20 based on. The exact format of this string depends on the version
21 control system in use, but it tries to identify the revision used as
22 close as possible (using the svn revision number or hg/git hash).
23 This also includes an indication of whether the checkout was
24 modified and which branch was checked out. This value is not
25 guaranteed to be sortable, but is mainly meant for identifying the
26 revision and user display.
28 If no revision identifier could be found, this is left empty.
29 REV_NR
30 the revision number of the svn revision this checkout is based on.
31 This can be used to determine which functionality is present in this
32 checkout. For trunk svn checkouts and hg/git branches based upon it,
33 this number should be accurate. For svn branch checkouts, this
34 number is mostly meaningless, at least when comparing with the
35 REV_NR from other branches or trunk.
37 This number should be sortable. Within a given branch or trunk, a
38 higher number means a newer version. However, when using git or hg,
39 this number will not increase on new commits.
41 If no revision number could be found, this is left empty.
42 MODIFIED
43 Whether (the src directory of) this checkout is modified or not. A
44 value of 0 means not modified, a value of 2 means it was modified.
45 Modification is determined in relation to the commit identified by
46 REV, so not in relation to the svn revision identified by REV_NR.
48 A value of 1 means that the modified status is unknown, because this
49 is not an svn/git/hg checkout for example.
51 CLEAN_REV
52 the same as REV but without branch name
54 By setting the AWK environment variable, a caller can determine which
55 version of "awk" is used. If nothing is set, this script defaults to
56 "awk".
57 EOF
58 exit 1;
61 # Allow awk to be provided by the caller.
62 if [ -z "$AWK" ]; then
63 AWK=awk
66 # Find out some dirs
67 cd `dirname "$0"`
68 ROOT_DIR=`pwd`
70 # Determine if we are using a modified version
71 # Assume the dir is not modified
72 MODIFIED="0"
73 if [ -d "$ROOT_DIR/.svn" ] || [ -d "$ROOT_DIR/../.svn" ]; then
74 # We are an svn checkout
75 if [ -n "`svnversion | grep 'M'`" ]; then
76 MODIFIED="2"
78 # Find the revision like: rXXXXM-branch
79 BRANCH=`LC_ALL=C svn info | "$AWK" '/^URL:.*branches/ { split($2, a, "/"); for(i in a) if (a[i]=="branches") { print a[i+1]; break } }'`
80 TAG=`LC_ALL=C svn info | "$AWK" '/^URL:.*tags/ { split($2, a, "/"); for(i in a) if (a[i]=="tags") { print a[i+1]; break } }'`
81 REV_NR=`LC_ALL=C svn info | "$AWK" '/^Last Changed Rev:/ { print $4 }'`
82 if [ -n "$TAG" ]; then
83 REV=$TAG
84 else
85 REV="r$REV_NR"
87 elif [ -d "$ROOT_DIR/.git" ]; then
88 # We are a git checkout
89 # Refresh the index to make sure file stat info is in sync, then look for modifications
90 git update-index --refresh >/dev/null
91 if [ -n "`git diff-index HEAD`" ]; then
92 MODIFIED="2"
94 HASH=`LC_ALL=C git rev-parse --verify HEAD 2>/dev/null`
95 REV="g`echo $HASH | cut -c1-8`"
96 BRANCH="`git symbolic-ref -q HEAD 2>/dev/null | sed 's@.*/@@;s@^master$@@'`"
97 REV_NR=`LC_ALL=C git log --pretty=format:%s --grep="^(svn r[0-9]*)" -1 | sed "s@.*(svn r\([0-9]*\)).*@\1@"`
98 if [ -z "$REV_NR" ]; then
99 # No rev? Maybe it is a custom git-svn clone
100 REV_NR=`LC_ALL=C git log --pretty=format:%b --grep="git-svn-id:.*@[0-9]*" -1 | sed "s@.*\@\([0-9]*\).*@\1@"`
102 TAG="`git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null | sed 's@\^0$@@'`"
103 if [ -n "$TAG" ]; then
104 BRANCH=""
105 REV="$TAG"
107 elif [ -d "$ROOT_DIR/.hg" ]; then
108 # We are a hg checkout
109 if [ -n "`HGPLAIN= hg status | grep -v '^?'`" ]; then
110 MODIFIED="2"
112 HASH=`LC_ALL=C HGPLAIN= hg id -i | cut -c1-12`
113 REV="h`echo $HASH | cut -c1-8`"
114 BRANCH="`HGPLAIN= hg branch | sed 's@^default$@@'`"
115 TAG="`HGPLAIN= hg id -t | grep -v 'tip$'`"
116 if [ -n "$TAG" ]; then
117 BRANCH=""
118 REV="$TAG"
120 REV_NR=`LC_ALL=C HGPLAIN= hg log -f -k "(svn r" -l 1 --template "{desc|firstline}\n" | grep "^(svn r[0-9]*)" | sed "s@.*(svn r\([0-9]*\)).*@\1@"`
121 if [ -z "$REV_NR" ]; then
122 # No rev? Maybe it is a custom hgsubversion clone
123 REV_NR=`LC_ALL=C HGPLAIN= hg parent --template="{svnrev}"`
125 elif [ -f "$ROOT_DIR/.ottdrev" ]; then
126 # We are an exported source bundle
127 cat $ROOT_DIR/.ottdrev
128 exit
129 else
130 # We don't know
131 MODIFIED="1"
132 BRANCH=""
133 REV=""
134 REV_NR=""
137 if [ "$MODIFIED" -eq "2" ]; then
138 REV="${REV}M"
141 CLEAN_REV=${REV}
143 if [ -n "$BRANCH" ]; then
144 REV="${REV}-$BRANCH"
147 echo "$REV $REV_NR $MODIFIED $CLEAN_REV"