Blame build-aux/git-version-gen

Packit 47b4ca
#!/bin/sh
Packit 47b4ca
# Print a version string.
Packit 47b4ca
scriptversion=2012-03-18.17; # UTC
Packit 47b4ca
Packit 47b4ca
# Copyright (C) 2007-2012 Free Software Foundation, Inc.
Packit 47b4ca
#
Packit 47b4ca
# This program is free software: you can redistribute it and/or modify
Packit 47b4ca
# it under the terms of the GNU General Public License as published by
Packit 47b4ca
# the Free Software Foundation; either version 3 of the License, or
Packit 47b4ca
# (at your option) any later version.
Packit 47b4ca
#
Packit 47b4ca
# This program is distributed in the hope that it will be useful,
Packit 47b4ca
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47b4ca
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 47b4ca
# GNU General Public License for more details.
Packit 47b4ca
#
Packit 47b4ca
# You should have received a copy of the GNU General Public License
Packit 47b4ca
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 47b4ca
Packit 47b4ca
# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
Packit 47b4ca
# It may be run two ways:
Packit 47b4ca
# - from a git repository in which the "git describe" command below
Packit 47b4ca
#   produces useful output (thus requiring at least one signed tag)
Packit 47b4ca
# - from a non-git-repo directory containing a .tarball-version file, which
Packit 47b4ca
#   presumes this script is invoked like "./git-version-gen .tarball-version".
Packit 47b4ca
Packit 47b4ca
# In order to use intra-version strings in your project, you will need two
Packit 47b4ca
# separate generated version string files:
Packit 47b4ca
#
Packit 47b4ca
# .tarball-version - present only in a distribution tarball, and not in
Packit 47b4ca
#   a checked-out repository.  Created with contents that were learned at
Packit 47b4ca
#   the last time autoconf was run, and used by git-version-gen.  Must not
Packit 47b4ca
#   be present in either $(srcdir) or $(builddir) for git-version-gen to
Packit 47b4ca
#   give accurate answers during normal development with a checked out tree,
Packit 47b4ca
#   but must be present in a tarball when there is no version control system.
Packit 47b4ca
#   Therefore, it cannot be used in any dependencies.  GNUmakefile has
Packit 47b4ca
#   hooks to force a reconfigure at distribution time to get the value
Packit 47b4ca
#   correct, without penalizing normal development with extra reconfigures.
Packit 47b4ca
#
Packit 47b4ca
# .version - present in a checked-out repository and in a distribution
Packit 47b4ca
#   tarball.  Usable in dependencies, particularly for files that don't
Packit 47b4ca
#   want to depend on config.h but do want to track version changes.
Packit 47b4ca
#   Delete this file prior to any autoconf run where you want to rebuild
Packit 47b4ca
#   files to pick up a version string change; and leave it stale to
Packit 47b4ca
#   minimize rebuild time after unrelated changes to configure sources.
Packit 47b4ca
#
Packit 47b4ca
# As with any generated file in a VC'd directory, you should add
Packit 47b4ca
# /.version to .gitignore, so that you don't accidentally commit it.
Packit 47b4ca
# .tarball-version is never generated in a VC'd directory, so needn't
Packit 47b4ca
# be listed there.
Packit 47b4ca
#
Packit 47b4ca
# Use the following line in your configure.ac, so that $(VERSION) will
Packit 47b4ca
# automatically be up-to-date each time configure is run (and note that
Packit 47b4ca
# since configure.ac no longer includes a version string, Makefile rules
Packit 47b4ca
# should not depend on configure.ac for version updates).
Packit 47b4ca
#
Packit 47b4ca
# AC_INIT([GNU project],
Packit 47b4ca
#         m4_esyscmd([build-aux/git-version-gen .tarball-version]),
Packit 47b4ca
#         [bug-project@example])
Packit 47b4ca
#
Packit 47b4ca
# Then use the following lines in your Makefile.am, so that .version
Packit 47b4ca
# will be present for dependencies, and so that .version and
Packit 47b4ca
# .tarball-version will exist in distribution tarballs.
Packit 47b4ca
#
Packit 47b4ca
# EXTRA_DIST = $(top_srcdir)/.version
Packit 47b4ca
# BUILT_SOURCES = $(top_srcdir)/.version
Packit 47b4ca
# $(top_srcdir)/.version:
Packit 47b4ca
#	echo $(VERSION) > $@-t && mv $@-t $@
Packit 47b4ca
# dist-hook:
Packit 47b4ca
#	echo $(VERSION) > $(distdir)/.tarball-version
Packit 47b4ca
Packit 47b4ca
Packit 47b4ca
me=$0
Packit 47b4ca
Packit 47b4ca
version="git-version-gen $scriptversion
Packit 47b4ca
Packit 47b4ca
Copyright 2011 Free Software Foundation, Inc.
Packit 47b4ca
There is NO warranty.  You may redistribute this software
Packit 47b4ca
under the terms of the GNU General Public License.
Packit 47b4ca
For more information about these matters, see the files named COPYING."
Packit 47b4ca
Packit 47b4ca
usage="\
Packit 47b4ca
Usage: $me [OPTION]... \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT]
Packit 47b4ca
Print a version string.
Packit 47b4ca
Packit 47b4ca
Options:
Packit 47b4ca
Packit 47b4ca
   --prefix           prefix of git tags (default 'v')
Packit 47b4ca
Packit 47b4ca
   --help             display this help and exit
Packit 47b4ca
   --version          output version information and exit
Packit 47b4ca
Packit 47b4ca
Running without arguments will suffice in most cases."
Packit 47b4ca
Packit 47b4ca
prefix=v
Packit 47b4ca
Packit 47b4ca
while test $# -gt 0; do
Packit 47b4ca
  case $1 in
Packit 47b4ca
    --help) echo "$usage"; exit 0;;
Packit 47b4ca
    --version) echo "$version"; exit 0;;
Packit 47b4ca
    --prefix) shift; prefix="$1";;
Packit 47b4ca
    -*)
Packit 47b4ca
      echo "$0: Unknown option '$1'." >&2
Packit 47b4ca
      echo "$0: Try '--help' for more information." >&2
Packit 47b4ca
      exit 1;;
Packit 47b4ca
    *)
Packit 47b4ca
      if test -z "$tarball_version_file"; then
Packit 47b4ca
        tarball_version_file="$1"
Packit 47b4ca
      elif test -z "$tag_sed_script"; then
Packit 47b4ca
        tag_sed_script="$1"
Packit 47b4ca
      else
Packit 47b4ca
        echo "$0: extra non-option argument '$1'." >&2
Packit 47b4ca
        exit 1
Packit 47b4ca
      fi;;
Packit 47b4ca
  esac
Packit 47b4ca
  shift
Packit 47b4ca
done
Packit 47b4ca
Packit 47b4ca
if test -z "$tarball_version_file"; then
Packit 47b4ca
    echo "$usage"
Packit 47b4ca
    exit 1
Packit 47b4ca
fi
Packit 47b4ca
Packit 47b4ca
tag_sed_script="${tag_sed_script:-s/x/x/}"
Packit 47b4ca
Packit 47b4ca
nl='
Packit 47b4ca
'
Packit 47b4ca
Packit 47b4ca
# Avoid meddling by environment variable of the same name.
Packit 47b4ca
v=
Packit 47b4ca
v_from_git=
Packit 47b4ca
Packit 47b4ca
# First see if there is a tarball-only version file.
Packit 47b4ca
# then try "git describe", then default.
Packit 47b4ca
if test -f $tarball_version_file
Packit 47b4ca
then
Packit 47b4ca
    v=`cat $tarball_version_file` || v=
Packit 47b4ca
    case $v in
Packit 47b4ca
        *$nl*) v= ;; # reject multi-line output
Packit 47b4ca
        [0-9]*) ;;
Packit 47b4ca
        *) v= ;;
Packit 47b4ca
    esac
Packit 47b4ca
    test -z "$v" \
Packit 47b4ca
        && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2
Packit 47b4ca
fi
Packit 47b4ca
Packit 47b4ca
if test -n "$v"
Packit 47b4ca
then
Packit 47b4ca
    : # use $v
Packit 47b4ca
# Otherwise, if there is at least one git commit involving the working
Packit 47b4ca
# directory, and "git describe" output looks sensible, use that to
Packit 47b4ca
# derive a version string.
Packit 47b4ca
elif test "`git log -1 --pretty=format:x . 2>&1`" = x \
Packit 47b4ca
    && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \
Packit 47b4ca
          || git describe --abbrev=4 HEAD 2>/dev/null` \
Packit 47b4ca
    && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
Packit 47b4ca
    && case $v in
Packit 47b4ca
         $prefix[0-9]*) ;;
Packit 47b4ca
         *) (exit 1) ;;
Packit 47b4ca
       esac
Packit 47b4ca
then
Packit 47b4ca
    # Is this a new git that lists number of commits since the last
Packit 47b4ca
    # tag or the previous older version that did not?
Packit 47b4ca
    #   Newer: v6.10-77-g0f8faeb
Packit 47b4ca
    #   Older: v6.10-g0f8faeb
Packit 47b4ca
    case $v in
Packit 47b4ca
        *-*-*) : git describe is okay three part flavor ;;
Packit 47b4ca
        *-*)
Packit 47b4ca
            : git describe is older two part flavor
Packit 47b4ca
            # Recreate the number of commits and rewrite such that the
Packit 47b4ca
            # result is the same as if we were using the newer version
Packit 47b4ca
            # of git describe.
Packit 47b4ca
            vtag=`echo "$v" | sed 's/-.*//'`
Packit 47b4ca
            commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \
Packit 47b4ca
                || { commit_list=failed;
Packit 47b4ca
                     echo "$0: WARNING: git rev-list failed" 1>&2; }
Packit 47b4ca
            numcommits=`echo "$commit_list" | wc -l`
Packit 47b4ca
            v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
Packit 47b4ca
            test "$commit_list" = failed && v=UNKNOWN
Packit 47b4ca
            ;;
Packit 47b4ca
    esac
Packit 47b4ca
Packit 47b4ca
    # Change the first '-' to a '.', so version-comparing tools work properly.
Packit 47b4ca
    # Remove the "g" in git describe's output string, to save a byte.
Packit 47b4ca
    v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
Packit 47b4ca
    v_from_git=1
Packit 47b4ca
else
Packit 47b4ca
    v=UNKNOWN
Packit 47b4ca
fi
Packit 47b4ca
Packit 47b4ca
v=`echo "$v" |sed "s/^$prefix//"`
Packit 47b4ca
Packit 47b4ca
# Test whether to append the "-dirty" suffix only if the version
Packit 47b4ca
# string we're using came from git.  I.e., skip the test if it's "UNKNOWN"
Packit 47b4ca
# or if it came from .tarball-version.
Packit 47b4ca
if test -n "$v_from_git"; then
Packit 47b4ca
  # Don't declare a version "dirty" merely because a time stamp has changed.
Packit 47b4ca
  git update-index --refresh > /dev/null 2>&1
Packit 47b4ca
Packit 47b4ca
  dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty=
Packit 47b4ca
  case "$dirty" in
Packit 47b4ca
      '') ;;
Packit 47b4ca
      *) # Append the suffix only if there isn't one already.
Packit 47b4ca
          case $v in
Packit 47b4ca
            *-dirty) ;;
Packit 47b4ca
            *) v="$v-dirty" ;;
Packit 47b4ca
          esac ;;
Packit 47b4ca
  esac
Packit 47b4ca
fi
Packit 47b4ca
Packit 47b4ca
# Omit the trailing newline, so that m4_esyscmd can use the result directly.
Packit 47b4ca
echo "$v" | tr -d "$nl"
Packit 47b4ca
Packit 47b4ca
# Local variables:
Packit 47b4ca
# eval: (add-hook 'write-file-hooks 'time-stamp)
Packit 47b4ca
# time-stamp-start: "scriptversion="
Packit 47b4ca
# time-stamp-format: "%:y-%02m-%02d.%02H"
Packit 47b4ca
# time-stamp-time-zone: "UTC"
Packit 47b4ca
# time-stamp-end: "; # UTC"
Packit 47b4ca
# End: