Blame build-aux/git-version-gen

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