• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# Print a version string.
3scriptversion=2012-09-25.20
4
5# Copyright (C) 2007-2008 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, see <http://www.gnu.org/licenses/>.
19
20# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
21# It may be run two ways:
22# - from a git repository in which the "git describe" command below
23#   produces useful output (thus requiring at least one signed tag)
24# - from a non-git-repo directory containing a .tarball-version file, which
25#   presumes this script is invoked like "./git-version-gen .tarball-version".
26
27# In order to use intra-version strings in your project, you will need two
28# separate generated version string files:
29#
30# .tarball-version - present only in a distribution tarball, and not in
31#   a checked-out repository.  Created with contents that were learned at
32#   the last time autoconf was run, and used by git-version-gen.  Must not
33#   be present in either $(srcdir) or $(builddir) for git-version-gen to
34#   give accurate answers during normal development with a checked out tree,
35#   but must be present in a tarball when there is no version control system.
36#   Therefore, it cannot be used in any dependencies.  GNUmakefile has
37#   hooks to force a reconfigure at distribution time to get the value
38#   correct, without penalizing normal development with extra reconfigures.
39#
40# .version - present in a checked-out repository and in a distribution
41#   tarball.  Usable in dependencies, particularly for files that don't
42#   want to depend on config.h but do want to track version changes.
43#   Delete this file prior to any autoconf run where you want to rebuild
44#   files to pick up a version string change; and leave it stale to
45#   minimize rebuild time after unrelated changes to configure sources.
46#
47# It is probably wise to add these two files to .gitignore, so that you
48# don't accidentally commit either generated file.
49#
50# Use the following line in your configure.ac, so that $(VERSION) will
51# automatically be up-to-date each time configure is run (and note that
52# since configure.ac no longer includes a version string, Makefile rules
53# should not depend on configure.ac for version updates).
54#
55# AC_INIT([GNU project],
56#         m4_esyscmd([build-aux/git-version-gen .tarball-version]),
57#         [bug-project@example])
58#
59# Then use the following lines in your Makefile.am, so that .version
60# will be present for dependencies, and so that .tarball-version will
61# exist in distribution tarballs.
62#
63# BUILT_SOURCES = $(top_srcdir)/.version
64# $(top_srcdir)/.version:
65#	echo $(VERSION) > $@-t && mv $@-t $@
66# dist-hook:
67#	echo $(VERSION) > $(distdir)/.tarball-version
68#	echo $(VERSION) > $(distdir)/.version
69
70case $# in
71    1) ;;
72    *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
73esac
74
75tarball_version_file=$1
76nl='
77'
78v=
79
80# First see if there is a tarball-only version file.
81# then try "git describe", then default.
82if test -f $tarball_version_file
83then
84    v=`cat $tarball_version_file` || exit 1
85    case $v in
86	*$nl*) v= ;; # reject multi-line output
87	[0-9]*)
88		echo "$v" | tr -d '\012'
89		exit 0
90		;;
91	*) v= ;;
92    esac
93    test -z "$v" \
94	&& echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
95fi
96
97# This is presently used by the GNOME-OSTree build system; it
98# helps support the case where the meta-build system has already
99# determined the git revision, but we may not be able to run "git describe"
100# because we're inside a chroot.
101if test -n "$GIT_DESCRIBE_FOR_BUILD";
102then
103    v=$GIT_DESCRIBE_FOR_BUILD
104fi
105
106if test -n "$v"
107then
108    : # use $v
109elif test -e .git \
110    && v=`git describe --abbrev=4 --match='v[0-9]*' HEAD 2>/dev/null` \
111    && [ -n "$v" ]
112then
113    # If we are on a "dev" tag, we need to check that it is not the same
114    # reference as the a previous version tag (this only happens when we are
115    # working with a release tag).
116    # NB The below trick relies on the $v being an exact tag to work which
117    # will only work when HEAD == tag. When further commits have been made on top
118    # of the tag, the $v will be supplimented with the number of commits since
119    # that tag and the commit ref of the most recent commit and thus will
120    # fail the test below (as intended)
121    v2=`git describe --abbrev=4 --match='v[0-9]\.[0-9]' --contains $v 2>/dev/null | cut -d'^' -f1`
122    [ -n "$v2" ] && v=$v2
123
124    # Is this a new git that lists number of commits since the last
125    # tag or the previous older version that did not?
126    #   Newer: v6.10-77-g0f8faeb
127    #   Older: v6.10-g0f8faeb
128#    case $v in
129#	*-*-*) : git describe is okay three part flavor ;;
130#	*-*)
131#	    : git describe is older two part flavor
132#	    # Recreate the number of commits and rewrite such that the
133#	    # result is the same as if we were using the newer version
134#	    # of git describe.
135#	    vtag=`echo "$v" | sed 's/-.*//'`
136#	    numcommits=`git rev-list "$vtag"..HEAD | wc -l`
137#	    v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
138#	    ;;
139#    esac
140
141    # Change the first '-' to a '.', so version-comparing tools work properly.
142    # Remove the "g" in git describe's output string, to save a byte.
143#    v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
144    :
145else
146    echo 1>&2 "$0: Failed to determine git revision"
147    exit 1
148fi
149
150v=`echo "$v" |sed 's/^v//'`
151
152# Don't declare a version "dirty" merely because a time stamp has changed.
153git status > /dev/null 2>&1
154
155dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
156case "$dirty" in
157    '') ;;
158    *) # Append the suffix only if there isn't one already.
159	case $v in
160	  *-dirty) ;;
161	  *) v="$v-dirty" ;;
162	esac ;;
163esac
164
165# Omit the trailing newline, so that m4_esyscmd can use the result directly.
166echo "$v" | tr -d '\012'
167
168# Local variables:
169# eval: (add-hook 'write-file-hooks 'time-stamp)
170# time-stamp-start: "scriptversion="
171# time-stamp-format: "%:y-%02m-%02d.%02H"
172# time-stamp-end: "$"
173# End:
174