* build-aux/git-version-gen: Adjust a comment and the Usage string.
[gnulib.git] / build-aux / git-version-gen
1 #!/bin/sh
2 # Print a version string.
3 scriptversion=2008-03-02.16
4
5 # Copyright (C) 2007 Free Software Foundation
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 # This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
23 # It may be run two ways:
24 # - from a git repository in which the "git describe" command below
25 #   produces useful output (thus requiring at least one signed tag)
26 # - from a non-git-repo directory containing a .tarball-version file, which
27 #   presumes this script is invoked like "./git-version-gen .tarball-version".
28
29 case $# in
30     1) ;;
31     *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
32 esac
33
34 tarball_version_file=$1
35 nl='
36 '
37
38 # First see if there is a tarball-only version file.
39 # then try "git describe", then default.
40 if test -f $tarball_version_file
41 then
42     v=`cat $tarball_version_file` || exit 1
43     case $v in
44         *$nl*) v= ;; # reject multi-line output
45         [0-9]*) ;;
46         *) v= ;;
47     esac
48     test -z "$v" \
49         && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
50 fi
51
52 if test -n "$v"
53 then
54     : # use $v
55 elif test -d .git \
56     && v=`git describe --abbrev=4 HEAD 2>/dev/null` \
57     && case $v in
58          v[0-9]*) ;;
59          *) (exit 1) ;;
60        esac
61 then
62     # Is this a new git that lists number of commits since the last
63     # tag or the previous older version that did not?
64     #   Newer: v6.10-77-g0f8faeb
65     #   Older: v6.10-g0f8faeb
66     case $v in
67         *-*-*) : git describe is okay three part flavor ;;
68         *-*)
69             : git describe is older two part flavor
70             # Recreate the number of commits and rewrite such that the
71             # result is the same as if we were using the newer version
72             # of git describe.
73             vtag=`echo "$v" | sed 's/-.*//'`
74             numcommits=`git rev-list "$vtag"..HEAD | wc -l`
75             v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
76             ;;
77     esac
78
79     # Change the first '-' to a '.', so version-comparing tools work properly.
80     # Remove the "g" in git describe's output string, to save a byte.
81     v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
82 else
83     v=UNKNOWN
84 fi
85
86 v=`echo "$v" |sed 's/^v//'`
87
88 # Don't declare a version "dirty" merely because a time stamp has changed.
89 git status > /dev/null 2>&1
90
91 dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
92 case "$dirty" in
93     '') ;;
94     *) # Append the suffix only if there isn't one already.
95         case $v in
96           *-dirty) ;;
97           *) v="$v-dirty" ;;
98         esac ;;
99 esac
100
101 # Omit the trailing newline, so that m4_esyscmd can use the result directly.
102 echo "$v" | tr -d '\012'
103
104 # Local variables:
105 # eval: (add-hook 'write-file-hooks 'time-stamp)
106 # time-stamp-start: "scriptversion="
107 # time-stamp-format: "%:y-%02m-%02d.%02H"
108 # time-stamp-end: "$"
109 # End: