findversion.sh
author matthijs
Sun, 07 Sep 2008 13:24:49 +0000
changeset 10085 de2d010b7db9
child 10088 a87f25ff2b42
permissions -rwxr-xr-x
(svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
new bash script findversion.sh. This commit should have no functional changes.
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
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
     9
Output: <REV>\t<REV_NR>\t<MODIFIED>
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
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    43
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
    44
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
    45
"awk".
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    46
EOF
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    47
exit 1;
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    48
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    49
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    50
# 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
    51
if [ -z "$AWK" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    52
	AWK=awk
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    53
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    54
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    55
# Find out some dirs
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    56
cd `dirname "$0"`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    57
ROOT_DIR=`pwd`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    58
SRC_DIR=src
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    59
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    60
# 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
    61
# 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
    62
MODIFIED="0"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    63
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
    64
	# We are an svn checkout
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    65
	if svnversion "$SRC_DIR" | grep 'M' > /dev/null; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    66
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    67
	fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    68
	# 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
    69
	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 } }'`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    70
	REV_NR=`LC_ALL=C svn info "$SRC_DIR" | "$AWK" '/^Last Changed Rev:/ { print $4 }'`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    71
	REV="r$REV_NR"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    72
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
    73
	# We are a git checkout
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    74
	if git diff-index HEAD "$SRC_DIR" | read dummy; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    75
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    76
	fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    77
	HASH=`LC_ALL=C git rev-parse --verify HEAD 2>/dev/null | cut -c1-8`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    78
	REV="g$HASH"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    79
	BRANCH=`git branch|grep '[*]' | sed 's/\* //;s/^master$//'`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    80
	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/"`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    81
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
    82
	# We are a hg checkout
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    83
	if hg status "$SRC_DIR" | grep -v '^?' | read dummy; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    84
		MODIFIED="2"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    85
	fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    86
	HASH=`LC_ALL=C hg tip 2>/dev/null | head -n 1 | cut -d: -f3 | cut -c1-8`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    87
	REV="h$HASH"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    88
	BRANCH=`hg branch | sed 's/^default$//'`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    89
	REV_NR=`LC_ALL=C hg log -k "svn" -l 1 --template "{desc}\n" "$SRC_DIR" | grep "^(svn r[0-9]*)" | head -n 1 | sed "s/.*(svn r\([0-9]*\)).*/\1/"`
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    90
else
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    91
	# We don't know
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    92
	MODIFIED="1"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    93
	BRANCH=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    94
	REV=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    95
	REV_NR=""
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    96
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    97
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
    98
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
    99
	REV="${REV}M"
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   100
fi
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   101
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   102
if [ -n "$BRANCH" ]; then
de2d010b7db9 (svn r14261) -Change [Makefile]: Move the revision detection code from Makefile.src.in to a
matthijs
parents:
diff changeset
   103
	REV="${REV}-$BRANCH"
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
echo -e "$REV\t$REV_NR\t$MODIFIED"