153a5a1b3Sopenharmony_ci#!/bin/sh
253a5a1b3Sopenharmony_ci# Print a version string.
353a5a1b3Sopenharmony_ciscriptversion=2012-09-25.20
453a5a1b3Sopenharmony_ci
553a5a1b3Sopenharmony_ci# Copyright (C) 2007-2008 Free Software Foundation
653a5a1b3Sopenharmony_ci#
753a5a1b3Sopenharmony_ci# This program is free software; you can redistribute it and/or modify
853a5a1b3Sopenharmony_ci# it under the terms of the GNU General Public License as published by
953a5a1b3Sopenharmony_ci# the Free Software Foundation; either version 3, or (at your option)
1053a5a1b3Sopenharmony_ci# any later version.
1153a5a1b3Sopenharmony_ci#
1253a5a1b3Sopenharmony_ci# This program is distributed in the hope that it will be useful,
1353a5a1b3Sopenharmony_ci# but WITHOUT ANY WARRANTY; without even the implied warranty of
1453a5a1b3Sopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1553a5a1b3Sopenharmony_ci# GNU General Public License for more details.
1653a5a1b3Sopenharmony_ci#
1753a5a1b3Sopenharmony_ci# You should have received a copy of the GNU General Public License
1853a5a1b3Sopenharmony_ci# along with this program; if not, see <http://www.gnu.org/licenses/>.
1953a5a1b3Sopenharmony_ci
2053a5a1b3Sopenharmony_ci# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
2153a5a1b3Sopenharmony_ci# It may be run two ways:
2253a5a1b3Sopenharmony_ci# - from a git repository in which the "git describe" command below
2353a5a1b3Sopenharmony_ci#   produces useful output (thus requiring at least one signed tag)
2453a5a1b3Sopenharmony_ci# - from a non-git-repo directory containing a .tarball-version file, which
2553a5a1b3Sopenharmony_ci#   presumes this script is invoked like "./git-version-gen .tarball-version".
2653a5a1b3Sopenharmony_ci
2753a5a1b3Sopenharmony_ci# In order to use intra-version strings in your project, you will need two
2853a5a1b3Sopenharmony_ci# separate generated version string files:
2953a5a1b3Sopenharmony_ci#
3053a5a1b3Sopenharmony_ci# .tarball-version - present only in a distribution tarball, and not in
3153a5a1b3Sopenharmony_ci#   a checked-out repository.  Created with contents that were learned at
3253a5a1b3Sopenharmony_ci#   the last time autoconf was run, and used by git-version-gen.  Must not
3353a5a1b3Sopenharmony_ci#   be present in either $(srcdir) or $(builddir) for git-version-gen to
3453a5a1b3Sopenharmony_ci#   give accurate answers during normal development with a checked out tree,
3553a5a1b3Sopenharmony_ci#   but must be present in a tarball when there is no version control system.
3653a5a1b3Sopenharmony_ci#   Therefore, it cannot be used in any dependencies.  GNUmakefile has
3753a5a1b3Sopenharmony_ci#   hooks to force a reconfigure at distribution time to get the value
3853a5a1b3Sopenharmony_ci#   correct, without penalizing normal development with extra reconfigures.
3953a5a1b3Sopenharmony_ci#
4053a5a1b3Sopenharmony_ci# .version - present in a checked-out repository and in a distribution
4153a5a1b3Sopenharmony_ci#   tarball.  Usable in dependencies, particularly for files that don't
4253a5a1b3Sopenharmony_ci#   want to depend on config.h but do want to track version changes.
4353a5a1b3Sopenharmony_ci#   Delete this file prior to any autoconf run where you want to rebuild
4453a5a1b3Sopenharmony_ci#   files to pick up a version string change; and leave it stale to
4553a5a1b3Sopenharmony_ci#   minimize rebuild time after unrelated changes to configure sources.
4653a5a1b3Sopenharmony_ci#
4753a5a1b3Sopenharmony_ci# It is probably wise to add these two files to .gitignore, so that you
4853a5a1b3Sopenharmony_ci# don't accidentally commit either generated file.
4953a5a1b3Sopenharmony_ci#
5053a5a1b3Sopenharmony_ci# Use the following line in your configure.ac, so that $(VERSION) will
5153a5a1b3Sopenharmony_ci# automatically be up-to-date each time configure is run (and note that
5253a5a1b3Sopenharmony_ci# since configure.ac no longer includes a version string, Makefile rules
5353a5a1b3Sopenharmony_ci# should not depend on configure.ac for version updates).
5453a5a1b3Sopenharmony_ci#
5553a5a1b3Sopenharmony_ci# AC_INIT([GNU project],
5653a5a1b3Sopenharmony_ci#         m4_esyscmd([build-aux/git-version-gen .tarball-version]),
5753a5a1b3Sopenharmony_ci#         [bug-project@example])
5853a5a1b3Sopenharmony_ci#
5953a5a1b3Sopenharmony_ci# Then use the following lines in your Makefile.am, so that .version
6053a5a1b3Sopenharmony_ci# will be present for dependencies, and so that .tarball-version will
6153a5a1b3Sopenharmony_ci# exist in distribution tarballs.
6253a5a1b3Sopenharmony_ci#
6353a5a1b3Sopenharmony_ci# BUILT_SOURCES = $(top_srcdir)/.version
6453a5a1b3Sopenharmony_ci# $(top_srcdir)/.version:
6553a5a1b3Sopenharmony_ci#	echo $(VERSION) > $@-t && mv $@-t $@
6653a5a1b3Sopenharmony_ci# dist-hook:
6753a5a1b3Sopenharmony_ci#	echo $(VERSION) > $(distdir)/.tarball-version
6853a5a1b3Sopenharmony_ci#	echo $(VERSION) > $(distdir)/.version
6953a5a1b3Sopenharmony_ci
7053a5a1b3Sopenharmony_cicase $# in
7153a5a1b3Sopenharmony_ci    1) ;;
7253a5a1b3Sopenharmony_ci    *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
7353a5a1b3Sopenharmony_ciesac
7453a5a1b3Sopenharmony_ci
7553a5a1b3Sopenharmony_citarball_version_file=$1
7653a5a1b3Sopenharmony_cinl='
7753a5a1b3Sopenharmony_ci'
7853a5a1b3Sopenharmony_civ=
7953a5a1b3Sopenharmony_ci
8053a5a1b3Sopenharmony_ci# First see if there is a tarball-only version file.
8153a5a1b3Sopenharmony_ci# then try "git describe", then default.
8253a5a1b3Sopenharmony_ciif test -f $tarball_version_file
8353a5a1b3Sopenharmony_cithen
8453a5a1b3Sopenharmony_ci    v=`cat $tarball_version_file` || exit 1
8553a5a1b3Sopenharmony_ci    case $v in
8653a5a1b3Sopenharmony_ci	*$nl*) v= ;; # reject multi-line output
8753a5a1b3Sopenharmony_ci	[0-9]*)
8853a5a1b3Sopenharmony_ci		echo "$v" | tr -d '\012'
8953a5a1b3Sopenharmony_ci		exit 0
9053a5a1b3Sopenharmony_ci		;;
9153a5a1b3Sopenharmony_ci	*) v= ;;
9253a5a1b3Sopenharmony_ci    esac
9353a5a1b3Sopenharmony_ci    test -z "$v" \
9453a5a1b3Sopenharmony_ci	&& echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
9553a5a1b3Sopenharmony_cifi
9653a5a1b3Sopenharmony_ci
9753a5a1b3Sopenharmony_ci# This is presently used by the GNOME-OSTree build system; it
9853a5a1b3Sopenharmony_ci# helps support the case where the meta-build system has already
9953a5a1b3Sopenharmony_ci# determined the git revision, but we may not be able to run "git describe"
10053a5a1b3Sopenharmony_ci# because we're inside a chroot.
10153a5a1b3Sopenharmony_ciif test -n "$GIT_DESCRIBE_FOR_BUILD";
10253a5a1b3Sopenharmony_cithen
10353a5a1b3Sopenharmony_ci    v=$GIT_DESCRIBE_FOR_BUILD
10453a5a1b3Sopenharmony_cifi
10553a5a1b3Sopenharmony_ci
10653a5a1b3Sopenharmony_ciif test -n "$v"
10753a5a1b3Sopenharmony_cithen
10853a5a1b3Sopenharmony_ci    : # use $v
10953a5a1b3Sopenharmony_cielif test -e .git \
11053a5a1b3Sopenharmony_ci    && v=`git describe --abbrev=4 --match='v[0-9]*' HEAD 2>/dev/null` \
11153a5a1b3Sopenharmony_ci    && [ -n "$v" ]
11253a5a1b3Sopenharmony_cithen
11353a5a1b3Sopenharmony_ci    # If we are on a "dev" tag, we need to check that it is not the same
11453a5a1b3Sopenharmony_ci    # reference as the a previous version tag (this only happens when we are
11553a5a1b3Sopenharmony_ci    # working with a release tag).
11653a5a1b3Sopenharmony_ci    # NB The below trick relies on the $v being an exact tag to work which
11753a5a1b3Sopenharmony_ci    # will only work when HEAD == tag. When further commits have been made on top
11853a5a1b3Sopenharmony_ci    # of the tag, the $v will be supplimented with the number of commits since
11953a5a1b3Sopenharmony_ci    # that tag and the commit ref of the most recent commit and thus will
12053a5a1b3Sopenharmony_ci    # fail the test below (as intended)
12153a5a1b3Sopenharmony_ci    v2=`git describe --abbrev=4 --match='v[0-9]\.[0-9]' --contains $v 2>/dev/null | cut -d'^' -f1`
12253a5a1b3Sopenharmony_ci    [ -n "$v2" ] && v=$v2
12353a5a1b3Sopenharmony_ci
12453a5a1b3Sopenharmony_ci    # Is this a new git that lists number of commits since the last
12553a5a1b3Sopenharmony_ci    # tag or the previous older version that did not?
12653a5a1b3Sopenharmony_ci    #   Newer: v6.10-77-g0f8faeb
12753a5a1b3Sopenharmony_ci    #   Older: v6.10-g0f8faeb
12853a5a1b3Sopenharmony_ci#    case $v in
12953a5a1b3Sopenharmony_ci#	*-*-*) : git describe is okay three part flavor ;;
13053a5a1b3Sopenharmony_ci#	*-*)
13153a5a1b3Sopenharmony_ci#	    : git describe is older two part flavor
13253a5a1b3Sopenharmony_ci#	    # Recreate the number of commits and rewrite such that the
13353a5a1b3Sopenharmony_ci#	    # result is the same as if we were using the newer version
13453a5a1b3Sopenharmony_ci#	    # of git describe.
13553a5a1b3Sopenharmony_ci#	    vtag=`echo "$v" | sed 's/-.*//'`
13653a5a1b3Sopenharmony_ci#	    numcommits=`git rev-list "$vtag"..HEAD | wc -l`
13753a5a1b3Sopenharmony_ci#	    v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
13853a5a1b3Sopenharmony_ci#	    ;;
13953a5a1b3Sopenharmony_ci#    esac
14053a5a1b3Sopenharmony_ci
14153a5a1b3Sopenharmony_ci    # Change the first '-' to a '.', so version-comparing tools work properly.
14253a5a1b3Sopenharmony_ci    # Remove the "g" in git describe's output string, to save a byte.
14353a5a1b3Sopenharmony_ci#    v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
14453a5a1b3Sopenharmony_ci    :
14553a5a1b3Sopenharmony_cielse
14653a5a1b3Sopenharmony_ci    echo 1>&2 "$0: Failed to determine git revision"
14753a5a1b3Sopenharmony_ci    exit 1
14853a5a1b3Sopenharmony_cifi
14953a5a1b3Sopenharmony_ci
15053a5a1b3Sopenharmony_civ=`echo "$v" |sed 's/^v//'`
15153a5a1b3Sopenharmony_ci
15253a5a1b3Sopenharmony_ci# Don't declare a version "dirty" merely because a time stamp has changed.
15353a5a1b3Sopenharmony_cigit status > /dev/null 2>&1
15453a5a1b3Sopenharmony_ci
15553a5a1b3Sopenharmony_cidirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
15653a5a1b3Sopenharmony_cicase "$dirty" in
15753a5a1b3Sopenharmony_ci    '') ;;
15853a5a1b3Sopenharmony_ci    *) # Append the suffix only if there isn't one already.
15953a5a1b3Sopenharmony_ci	case $v in
16053a5a1b3Sopenharmony_ci	  *-dirty) ;;
16153a5a1b3Sopenharmony_ci	  *) v="$v-dirty" ;;
16253a5a1b3Sopenharmony_ci	esac ;;
16353a5a1b3Sopenharmony_ciesac
16453a5a1b3Sopenharmony_ci
16553a5a1b3Sopenharmony_ci# Omit the trailing newline, so that m4_esyscmd can use the result directly.
16653a5a1b3Sopenharmony_ciecho "$v" | tr -d '\012'
16753a5a1b3Sopenharmony_ci
16853a5a1b3Sopenharmony_ci# Local variables:
16953a5a1b3Sopenharmony_ci# eval: (add-hook 'write-file-hooks 'time-stamp)
17053a5a1b3Sopenharmony_ci# time-stamp-start: "scriptversion="
17153a5a1b3Sopenharmony_ci# time-stamp-format: "%:y-%02m-%02d.%02H"
17253a5a1b3Sopenharmony_ci# time-stamp-end: "$"
17353a5a1b3Sopenharmony_ci# End:
174