* build-aux/git-version-gen: New file, from coreutils. For details, see
[gnulib.git] / build-aux / git-version-gen
1 #!/bin/sh
2 # Print a version string.
3 # This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
4 scriptversion=2007-06-30.12
5
6 # Copyright (C) 2007 Free Software Foundation
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
22
23 case $# in
24     2) ;;
25     *) echo 1>&2 "Usage: $0 \$VERSION \$srcdir/.version"; exit 1;;
26 esac
27
28 default_version=$1
29 tarball_version_file=$2
30 nl='
31 '
32
33 # First see if there is a tarball-only version file.
34 # then try git-describe, then default.
35 if test -f $tarball_version_file
36 then
37     v=`cat $tarball_version_file` || exit 1
38     case $v in
39         *$nl*) v= ;; # reject multi-line output
40         [0-9]*) ;;
41         *) v= ;;
42     esac
43     test -z "$v" \
44         && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
45 fi
46
47 if test -n "$v"
48 then
49     : # use $v
50 elif test -d .git \
51     && v=`git describe --abbrev=4 HEAD 2>/dev/null` \
52     && case $v in
53          v[0-9]*) ;;
54          *) (exit 1) ;;
55        esac
56 then
57     # Remove the "g" in git-describe's output string; change each - to a '.'
58     v=`echo "$v" | sed 's/\(.*\)-g/\1-/;s/-/./g'`;
59 else
60     v=$default_version
61 fi
62
63 v=`echo "$v" |sed 's/^v//'`
64
65 dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
66 case "$dirty" in
67     '') ;;
68     *) # Append the suffix only if there isn't one already.
69         case $v in
70           *-dirty) ;;
71           *) v="$v-dirty" ;;
72         esac ;;
73 esac
74
75 # Omit the trailing newline, so that m4_esyscmd can use the result directly.
76 echo "$v" | tr -d '\012'
77
78 # Local variables:
79 # eval: (add-hook 'write-file-hooks 'time-stamp)
80 # time-stamp-start: "scriptversion="
81 # time-stamp-format: "%:y-%02m-%02d.%02H"
82 # time-stamp-end: "$"
83 # End: