findversion.sh
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10281 6f45e8b392d1
permissions -rwxr-xr-x
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     1
#!/bin/sh
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     2
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     3
# Arguments given? Show help text.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     4
if [ "$#" != "0" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     5
	cat <<EOF
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     6
Usage: ./findversion.sh
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     7
Finds the current revision and if the code is modified.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     8
10092
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
     9
Output: <REV>\t<REV_NR>\t<MODIFIED>\t<CLEAN_REV>
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    10
REV
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    11
    a string describing what version of the code the current checkout is
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    12
    based on. The exact format of this string depends on the version
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    13
    control system in use, but it tries to identify the revision used as
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    14
    close as possible (using the svn revision number or hg/git hash).
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    15
    This also includes an indication of whether the checkout was
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    16
    modified and which branch was checked out. This value is not
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    17
    guaranteed to be sortable, but is mainly meant for identifying the
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    18
    revision and user display.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    19
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    20
    If no revision identifier could be found, this is left empty.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    21
REV_NR
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    22
    the revision number of the svn revision this checkout is based on.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    23
    This can be used to determine which functionality is present in this
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    24
    checkout. For trunk svn checkouts and hg/git branches based upon it,
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    25
    this number should be accurate. For svn branch checkouts, this
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    26
    number is mostly meaningless, at least when comparing with the
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    27
    REV_NR from other branches or trunk.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    28
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    29
    This number should be sortable. Within a given branch or trunk, a
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    30
    higher number means a newer version. However, when using git or hg,
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    31
    this number will not increase on new commits.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    32
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    33
    If no revision number could be found, this is left empty.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    34
MODIFIED
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    35
    Whether (the src directory of) this checkout is modified or not. A
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    36
    value of 0 means not modified, a value of 2 means it was modified.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    37
    Modification is determined in relation to the commit identified by
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    38
    REV, so not in relation to the svn revision identified by REV_NR.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    39
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    40
    A value of 1 means that the modified status is unknown, because this
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    41
    is not an svn/git/hg checkout for example.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    42
10092
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    43
CLEAN_REV
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    44
    the same as REV but without branch name
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    45
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    46
By setting the AWK environment variable, a caller can determine which
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    47
version of "awk" is used. If nothing is set, this script defaults to
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    48
"awk".
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    49
EOF
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    50
exit 1;
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    51
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    52
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    53
# Allow awk to be provided by the caller.
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    54
if [ -z "$AWK" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    55
	AWK=awk
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    56
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    57
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    58
# Find out some dirs
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    59
cd `dirname "$0"`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    60
ROOT_DIR=`pwd`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    61
SRC_DIR=src
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    62
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    63
# Determine if we are using a modified version
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    64
# Assume the dir is not modified
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    65
MODIFIED="0"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    66
if [ -d "$ROOT_DIR/.svn" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    67
	# We are an svn checkout
10096
d4ce0c2732bc (svn r14277) -Change [Makefile]: Use [ -n ... ] instead of using return values from grep and read in findversion.sh.
matthijs
parents: 10092
diff changeset
    68
	if [ -n "`svnversion \"$SRC_DIR\" | grep 'M'`" ]; then
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    69
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    70
	fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    71
	# Find the revision like: rXXXXM-branch
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    72
	BRANCH=`LC_ALL=C svn info "$SRC_DIR" | "$AWK" '/^URL:.*branches/ { split($2, a, "/"); for(i in a) if (a[i]=="branches") { print a[i+1]; break } }'`
10097
9ef63102fa6f (svn r14278) -Cleanup [Makefile]: Rename $TAGS to $TAG.
matthijs
parents: 10096
diff changeset
    73
	TAG=`LC_ALL=C svn info "$SRC_DIR" | "$AWK" '/^URL:.*tags/ { split($2, a, "/"); for(i in a) if (a[i]=="tags") { print a[i+1]; break } }'`
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    74
	REV_NR=`LC_ALL=C svn info "$SRC_DIR" | "$AWK" '/^Last Changed Rev:/ { print $4 }'`
10097
9ef63102fa6f (svn r14278) -Cleanup [Makefile]: Rename $TAGS to $TAG.
matthijs
parents: 10096
diff changeset
    75
	if [ -n "$TAG" ]; then
9ef63102fa6f (svn r14278) -Cleanup [Makefile]: Rename $TAGS to $TAG.
matthijs
parents: 10096
diff changeset
    76
		REV=$TAG
10092
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    77
	else
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    78
		REV="r$REV_NR"
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
    79
	fi
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    80
elif [ -d "$ROOT_DIR/.git" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    81
	# We are a git checkout
10096
d4ce0c2732bc (svn r14277) -Change [Makefile]: Use [ -n ... ] instead of using return values from grep and read in findversion.sh.
matthijs
parents: 10092
diff changeset
    82
	if [ -n "`git diff-index HEAD \"$SRC_DIR\"`" ]; then
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    83
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    84
	fi
10281
6f45e8b392d1 (svn r14519) -Fix [FS#2376]: do not use the short hash form for getting a specific mercurial/git revision as those can collide quite easily causing reversion detection to fail (PhilSophus)
rubidium
parents: 10239
diff changeset
    85
	HASH=`LC_ALL=C git rev-parse --verify HEAD 2>/dev/null`
6f45e8b392d1 (svn r14519) -Fix [FS#2376]: do not use the short hash form for getting a specific mercurial/git revision as those can collide quite easily causing reversion detection to fail (PhilSophus)
rubidium
parents: 10239
diff changeset
    86
	REV="g`echo $HASH | cut -c1-8`"
10239
cf55a4c21e7f (svn r14467) -Fix [FS#2350](r14443): use '~' instead of '#' as sed separator, it could be understood in a wrong way by shell
smatz
parents: 10220
diff changeset
    87
	BRANCH=`git branch|grep '[*]' | sed 's~\* ~~;s~^master$~~'`
cf55a4c21e7f (svn r14467) -Fix [FS#2350](r14443): use '~' instead of '#' as sed separator, it could be understood in a wrong way by shell
smatz
parents: 10220
diff changeset
    88
	REV_NR=`LC_ALL=C git log --pretty=format:%s "$SRC_DIR" | grep "^(svn r[0-9]*)" | head -n 1 | sed "s~.*(svn r\([0-9]*\)).*~\1~"`
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    89
elif [ -d "$ROOT_DIR/.hg" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    90
	# We are a hg checkout
10096
d4ce0c2732bc (svn r14277) -Change [Makefile]: Use [ -n ... ] instead of using return values from grep and read in findversion.sh.
matthijs
parents: 10092
diff changeset
    91
	if [ -n "`hg status \"$SRC_DIR\" | grep -v '^?'`" ]; then
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    92
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    93
	fi
10281
6f45e8b392d1 (svn r14519) -Fix [FS#2376]: do not use the short hash form for getting a specific mercurial/git revision as those can collide quite easily causing reversion detection to fail (PhilSophus)
rubidium
parents: 10239
diff changeset
    94
	HASH=`LC_ALL=C hg parents 2>/dev/null | head -n 1 | cut -d: -f3`
6f45e8b392d1 (svn r14519) -Fix [FS#2376]: do not use the short hash form for getting a specific mercurial/git revision as those can collide quite easily causing reversion detection to fail (PhilSophus)
rubidium
parents: 10239
diff changeset
    95
	REV="h`echo $HASH | cut -c1-8`"
10239
cf55a4c21e7f (svn r14467) -Fix [FS#2350](r14443): use '~' instead of '#' as sed separator, it could be understood in a wrong way by shell
smatz
parents: 10220
diff changeset
    96
	BRANCH=`hg branch | sed 's~^default$~~'`
cf55a4c21e7f (svn r14467) -Fix [FS#2350](r14443): use '~' instead of '#' as sed separator, it could be understood in a wrong way by shell
smatz
parents: 10220
diff changeset
    97
	REV_NR=`LC_ALL=C hg log -r $HASH:0 -k "svn" -l 1 --template "{desc}\n" "$SRC_DIR" | grep "^(svn r[0-9]*)" | head -n 1 | sed "s~.*(svn r\([0-9]*\)).*~\1~"`
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    98
else
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    99
	# We don't know
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   100
	MODIFIED="1"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   101
	BRANCH=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   102
	REV=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   103
	REV_NR=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   104
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   105
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   106
if [ "$MODIFIED" -eq "2" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   107
	REV="${REV}M"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   108
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   109
10092
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
   110
CLEAN_REV=${REV}
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
   111
10085
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   112
if [ -n "$BRANCH" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   113
	REV="${REV}-$BRANCH"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   114
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   115
10092
63288485ca37 (svn r14273) -Change: make findversion.sh aware of release tags and make it output the REV without the branch name so the CF can use that.
rubidium
parents: 10088
diff changeset
   116
echo "$REV	$REV_NR	$MODIFIED	$CLEAN_REV"