summaryrefslogtreecommitdiff
path: root/build-aux/git-version-gen
blob: 5f819ba7219e4792344100ddd6092ff6e14c9379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
# Print a version string.
scriptversion=2008-03-02.16

# Copyright (C) 2007 Free Software Foundation
#
# This program 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; either version 3, or (at your option)
# any later version.
#
# This program 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
# It may be run two ways:
# - from a git repository in which the "git describe" command below
#   produces useful output (thus requiring at least one signed tag)
# - from a non-git-repo directory containing a .tarball-version file, which
#   presumes this script is invoked like "./git-version-gen .tarball-version".

case $# in
    1) ;;
    *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
esac

tarball_version_file=$1
nl='
'

# First see if there is a tarball-only version file.
# then try "git describe", then default.
if test -f $tarball_version_file
then
    v=`cat $tarball_version_file` || exit 1
    case $v in
	*$nl*) v= ;; # reject multi-line output
	[0-9]*) ;;
	*) v= ;;
    esac
    test -z "$v" \
	&& echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
fi

if test -n "$v"
then
    : # use $v
elif test -d .git \
    && v=`git describe --abbrev=4 HEAD 2>/dev/null` \
    && case $v in
	 v[0-9]*) ;;
	 *) (exit 1) ;;
       esac
then
    # Is this a new git that lists number of commits since the last
    # tag or the previous older version that did not?
    #   Newer: v6.10-77-g0f8faeb
    #   Older: v6.10-g0f8faeb
    case $v in
	*-*-*) : git describe is okay three part flavor ;;
	*-*)
	    : git describe is older two part flavor
	    # Recreate the number of commits and rewrite such that the
	    # result is the same as if we were using the newer version
	    # of git describe.
	    vtag=`echo "$v" | sed 's/-.*//'`
	    numcommits=`git rev-list "$vtag"..HEAD | wc -l`
	    v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
	    ;;
    esac

    # Change the first '-' to a '.', so version-comparing tools work properly.
    # Remove the "g" in git describe's output string, to save a byte.
    v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
else
    v=UNKNOWN
fi

v=`echo "$v" |sed 's/^v//'`

# Don't declare a version "dirty" merely because a time stamp has changed.
git status > /dev/null 2>&1

dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
case "$dirty" in
    '') ;;
    *) # Append the suffix only if there isn't one already.
	case $v in
	  *-dirty) ;;
	  *) v="$v-dirty" ;;
	esac ;;
esac

# Omit the trailing newline, so that m4_esyscmd can use the result directly.
echo "$v" | tr -d '\012'

# Local variables:
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-end: "$"
# End: