xref: /netbsd-src/external/gpl3/autoconf/dist/build-aux/git-version-gen (revision d874e91932377fc40d53f102e48fc3ee6f4fe9de)
1*d874e919Schristos#!/bin/sh
2*d874e919Schristos# Print a version string.
3*d874e919Schristosscriptversion=2012-03-18.17; # UTC
4*d874e919Schristos
5*d874e919Schristos# Copyright (C) 2007-2012 Free Software Foundation, Inc.
6*d874e919Schristos#
7*d874e919Schristos# This program is free software: you can redistribute it and/or modify
8*d874e919Schristos# it under the terms of the GNU General Public License as published by
9*d874e919Schristos# the Free Software Foundation; either version 3 of the License, or
10*d874e919Schristos# (at your option) any later version.
11*d874e919Schristos#
12*d874e919Schristos# This program is distributed in the hope that it will be useful,
13*d874e919Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*d874e919Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*d874e919Schristos# GNU General Public License for more details.
16*d874e919Schristos#
17*d874e919Schristos# You should have received a copy of the GNU General Public License
18*d874e919Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*d874e919Schristos
20*d874e919Schristos# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
21*d874e919Schristos# It may be run two ways:
22*d874e919Schristos# - from a git repository in which the "git describe" command below
23*d874e919Schristos#   produces useful output (thus requiring at least one signed tag)
24*d874e919Schristos# - from a non-git-repo directory containing a .tarball-version file, which
25*d874e919Schristos#   presumes this script is invoked like "./git-version-gen .tarball-version".
26*d874e919Schristos
27*d874e919Schristos# In order to use intra-version strings in your project, you will need two
28*d874e919Schristos# separate generated version string files:
29*d874e919Schristos#
30*d874e919Schristos# .tarball-version - present only in a distribution tarball, and not in
31*d874e919Schristos#   a checked-out repository.  Created with contents that were learned at
32*d874e919Schristos#   the last time autoconf was run, and used by git-version-gen.  Must not
33*d874e919Schristos#   be present in either $(srcdir) or $(builddir) for git-version-gen to
34*d874e919Schristos#   give accurate answers during normal development with a checked out tree,
35*d874e919Schristos#   but must be present in a tarball when there is no version control system.
36*d874e919Schristos#   Therefore, it cannot be used in any dependencies.  GNUmakefile has
37*d874e919Schristos#   hooks to force a reconfigure at distribution time to get the value
38*d874e919Schristos#   correct, without penalizing normal development with extra reconfigures.
39*d874e919Schristos#
40*d874e919Schristos# .version - present in a checked-out repository and in a distribution
41*d874e919Schristos#   tarball.  Usable in dependencies, particularly for files that don't
42*d874e919Schristos#   want to depend on config.h but do want to track version changes.
43*d874e919Schristos#   Delete this file prior to any autoconf run where you want to rebuild
44*d874e919Schristos#   files to pick up a version string change; and leave it stale to
45*d874e919Schristos#   minimize rebuild time after unrelated changes to configure sources.
46*d874e919Schristos#
47*d874e919Schristos# As with any generated file in a VC'd directory, you should add
48*d874e919Schristos# /.version to .gitignore, so that you don't accidentally commit it.
49*d874e919Schristos# .tarball-version is never generated in a VC'd directory, so needn't
50*d874e919Schristos# be listed there.
51*d874e919Schristos#
52*d874e919Schristos# Use the following line in your configure.ac, so that $(VERSION) will
53*d874e919Schristos# automatically be up-to-date each time configure is run (and note that
54*d874e919Schristos# since configure.ac no longer includes a version string, Makefile rules
55*d874e919Schristos# should not depend on configure.ac for version updates).
56*d874e919Schristos#
57*d874e919Schristos# AC_INIT([GNU project],
58*d874e919Schristos#         m4_esyscmd([build-aux/git-version-gen .tarball-version]),
59*d874e919Schristos#         [bug-project@example])
60*d874e919Schristos#
61*d874e919Schristos# Then use the following lines in your Makefile.am, so that .version
62*d874e919Schristos# will be present for dependencies, and so that .version and
63*d874e919Schristos# .tarball-version will exist in distribution tarballs.
64*d874e919Schristos#
65*d874e919Schristos# EXTRA_DIST = $(top_srcdir)/.version
66*d874e919Schristos# BUILT_SOURCES = $(top_srcdir)/.version
67*d874e919Schristos# $(top_srcdir)/.version:
68*d874e919Schristos#	echo $(VERSION) > $@-t && mv $@-t $@
69*d874e919Schristos# dist-hook:
70*d874e919Schristos#	echo $(VERSION) > $(distdir)/.tarball-version
71*d874e919Schristos
72*d874e919Schristos
73*d874e919Schristosme=$0
74*d874e919Schristos
75*d874e919Schristosversion="git-version-gen $scriptversion
76*d874e919Schristos
77*d874e919SchristosCopyright 2011 Free Software Foundation, Inc.
78*d874e919SchristosThere is NO warranty.  You may redistribute this software
79*d874e919Schristosunder the terms of the GNU General Public License.
80*d874e919SchristosFor more information about these matters, see the files named COPYING."
81*d874e919Schristos
82*d874e919Schristosusage="\
83*d874e919SchristosUsage: $me [OPTION]... \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT]
84*d874e919SchristosPrint a version string.
85*d874e919Schristos
86*d874e919SchristosOptions:
87*d874e919Schristos
88*d874e919Schristos   --prefix           prefix of git tags (default 'v')
89*d874e919Schristos
90*d874e919Schristos   --help             display this help and exit
91*d874e919Schristos   --version          output version information and exit
92*d874e919Schristos
93*d874e919SchristosRunning without arguments will suffice in most cases."
94*d874e919Schristos
95*d874e919Schristosprefix=v
96*d874e919Schristos
97*d874e919Schristoswhile test $# -gt 0; do
98*d874e919Schristos  case $1 in
99*d874e919Schristos    --help) echo "$usage"; exit 0;;
100*d874e919Schristos    --version) echo "$version"; exit 0;;
101*d874e919Schristos    --prefix) shift; prefix="$1";;
102*d874e919Schristos    -*)
103*d874e919Schristos      echo "$0: Unknown option '$1'." >&2
104*d874e919Schristos      echo "$0: Try '--help' for more information." >&2
105*d874e919Schristos      exit 1;;
106*d874e919Schristos    *)
107*d874e919Schristos      if test -z "$tarball_version_file"; then
108*d874e919Schristos        tarball_version_file="$1"
109*d874e919Schristos      elif test -z "$tag_sed_script"; then
110*d874e919Schristos        tag_sed_script="$1"
111*d874e919Schristos      else
112*d874e919Schristos        echo "$0: extra non-option argument '$1'." >&2
113*d874e919Schristos        exit 1
114*d874e919Schristos      fi;;
115*d874e919Schristos  esac
116*d874e919Schristos  shift
117*d874e919Schristosdone
118*d874e919Schristos
119*d874e919Schristosif test -z "$tarball_version_file"; then
120*d874e919Schristos    echo "$usage"
121*d874e919Schristos    exit 1
122*d874e919Schristosfi
123*d874e919Schristos
124*d874e919Schristostag_sed_script="${tag_sed_script:-s/x/x/}"
125*d874e919Schristos
126*d874e919Schristosnl='
127*d874e919Schristos'
128*d874e919Schristos
129*d874e919Schristos# Avoid meddling by environment variable of the same name.
130*d874e919Schristosv=
131*d874e919Schristosv_from_git=
132*d874e919Schristos
133*d874e919Schristos# First see if there is a tarball-only version file.
134*d874e919Schristos# then try "git describe", then default.
135*d874e919Schristosif test -f $tarball_version_file
136*d874e919Schristosthen
137*d874e919Schristos    v=`cat $tarball_version_file` || v=
138*d874e919Schristos    case $v in
139*d874e919Schristos        *$nl*) v= ;; # reject multi-line output
140*d874e919Schristos        [0-9]*) ;;
141*d874e919Schristos        *) v= ;;
142*d874e919Schristos    esac
143*d874e919Schristos    test -z "$v" \
144*d874e919Schristos        && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2
145*d874e919Schristosfi
146*d874e919Schristos
147*d874e919Schristosif test -n "$v"
148*d874e919Schristosthen
149*d874e919Schristos    : # use $v
150*d874e919Schristos# Otherwise, if there is at least one git commit involving the working
151*d874e919Schristos# directory, and "git describe" output looks sensible, use that to
152*d874e919Schristos# derive a version string.
153*d874e919Schristoselif test "`git log -1 --pretty=format:x . 2>&1`" = x \
154*d874e919Schristos    && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \
155*d874e919Schristos          || git describe --abbrev=4 HEAD 2>/dev/null` \
156*d874e919Schristos    && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
157*d874e919Schristos    && case $v in
158*d874e919Schristos         $prefix[0-9]*) ;;
159*d874e919Schristos         *) (exit 1) ;;
160*d874e919Schristos       esac
161*d874e919Schristosthen
162*d874e919Schristos    # Is this a new git that lists number of commits since the last
163*d874e919Schristos    # tag or the previous older version that did not?
164*d874e919Schristos    #   Newer: v6.10-77-g0f8faeb
165*d874e919Schristos    #   Older: v6.10-g0f8faeb
166*d874e919Schristos    case $v in
167*d874e919Schristos        *-*-*) : git describe is okay three part flavor ;;
168*d874e919Schristos        *-*)
169*d874e919Schristos            : git describe is older two part flavor
170*d874e919Schristos            # Recreate the number of commits and rewrite such that the
171*d874e919Schristos            # result is the same as if we were using the newer version
172*d874e919Schristos            # of git describe.
173*d874e919Schristos            vtag=`echo "$v" | sed 's/-.*//'`
174*d874e919Schristos            commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \
175*d874e919Schristos                || { commit_list=failed;
176*d874e919Schristos                     echo "$0: WARNING: git rev-list failed" 1>&2; }
177*d874e919Schristos            numcommits=`echo "$commit_list" | wc -l`
178*d874e919Schristos            v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
179*d874e919Schristos            test "$commit_list" = failed && v=UNKNOWN
180*d874e919Schristos            ;;
181*d874e919Schristos    esac
182*d874e919Schristos
183*d874e919Schristos    # Change the first '-' to a '.', so version-comparing tools work properly.
184*d874e919Schristos    # Remove the "g" in git describe's output string, to save a byte.
185*d874e919Schristos    v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
186*d874e919Schristos    v_from_git=1
187*d874e919Schristoselse
188*d874e919Schristos    v=UNKNOWN
189*d874e919Schristosfi
190*d874e919Schristos
191*d874e919Schristosv=`echo "$v" |sed "s/^$prefix//"`
192*d874e919Schristos
193*d874e919Schristos# Test whether to append the "-dirty" suffix only if the version
194*d874e919Schristos# string we're using came from git.  I.e., skip the test if it's "UNKNOWN"
195*d874e919Schristos# or if it came from .tarball-version.
196*d874e919Schristosif test -n "$v_from_git"; then
197*d874e919Schristos  # Don't declare a version "dirty" merely because a time stamp has changed.
198*d874e919Schristos  git update-index --refresh > /dev/null 2>&1
199*d874e919Schristos
200*d874e919Schristos  dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty=
201*d874e919Schristos  case "$dirty" in
202*d874e919Schristos      '') ;;
203*d874e919Schristos      *) # Append the suffix only if there isn't one already.
204*d874e919Schristos          case $v in
205*d874e919Schristos            *-dirty) ;;
206*d874e919Schristos            *) v="$v-dirty" ;;
207*d874e919Schristos          esac ;;
208*d874e919Schristos  esac
209*d874e919Schristosfi
210*d874e919Schristos
211*d874e919Schristos# Omit the trailing newline, so that m4_esyscmd can use the result directly.
212*d874e919Schristosecho "$v" | tr -d "$nl"
213*d874e919Schristos
214*d874e919Schristos# Local variables:
215*d874e919Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
216*d874e919Schristos# time-stamp-start: "scriptversion="
217*d874e919Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
218*d874e919Schristos# time-stamp-time-zone: "UTC"
219*d874e919Schristos# time-stamp-end: "; # UTC"
220*d874e919Schristos# End:
221