1141cc406Sopenharmony_ci#!/bin/sh
2141cc406Sopenharmony_ci# Print a version string.
3141cc406Sopenharmony_ciscriptversion=2019-10-13.15; # UTC
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci# Copyright (C) 2007-2021 Free Software Foundation, Inc.
6141cc406Sopenharmony_ci#
7141cc406Sopenharmony_ci# This program is free software: you can redistribute it and/or modify
8141cc406Sopenharmony_ci# it under the terms of the GNU General Public License as published by
9141cc406Sopenharmony_ci# the Free Software Foundation; either version 3 of the License, or
10141cc406Sopenharmony_ci# (at your option) any later version.
11141cc406Sopenharmony_ci#
12141cc406Sopenharmony_ci# This program is distributed in the hope that it will be useful,
13141cc406Sopenharmony_ci# but WITHOUT ANY WARRANTY; without even the implied warranty of
14141cc406Sopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15141cc406Sopenharmony_ci# GNU General Public License for more details.
16141cc406Sopenharmony_ci#
17141cc406Sopenharmony_ci# You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19141cc406Sopenharmony_ci
20141cc406Sopenharmony_ci# This script is derived from GIT-VERSION-GEN from GIT: https://git-scm.com/.
21141cc406Sopenharmony_ci# It may be run two ways:
22141cc406Sopenharmony_ci# - from a git repository in which the "git describe" command below
23141cc406Sopenharmony_ci#   produces useful output (thus requiring at least one signed tag)
24141cc406Sopenharmony_ci# - from a non-git-repo directory containing a .tarball-version file, which
25141cc406Sopenharmony_ci#   presumes this script is invoked like "./git-version-gen .tarball-version".
26141cc406Sopenharmony_ci
27141cc406Sopenharmony_ci# In order to use intra-version strings in your project, you will need two
28141cc406Sopenharmony_ci# separate generated version string files:
29141cc406Sopenharmony_ci#
30141cc406Sopenharmony_ci# .tarball-version - present only in a distribution tarball, and not in
31141cc406Sopenharmony_ci#   a checked-out repository.  Created with contents that were learned at
32141cc406Sopenharmony_ci#   the last time autoconf was run, and used by git-version-gen.  Must not
33141cc406Sopenharmony_ci#   be present in either $(srcdir) or $(builddir) for git-version-gen to
34141cc406Sopenharmony_ci#   give accurate answers during normal development with a checked out tree,
35141cc406Sopenharmony_ci#   but must be present in a tarball when there is no version control system.
36141cc406Sopenharmony_ci#   Therefore, it cannot be used in any dependencies.  GNUmakefile has
37141cc406Sopenharmony_ci#   hooks to force a reconfigure at distribution time to get the value
38141cc406Sopenharmony_ci#   correct, without penalizing normal development with extra reconfigures.
39141cc406Sopenharmony_ci#
40141cc406Sopenharmony_ci# .version - present in a checked-out repository and in a distribution
41141cc406Sopenharmony_ci#   tarball.  Usable in dependencies, particularly for files that don't
42141cc406Sopenharmony_ci#   want to depend on config.h but do want to track version changes.
43141cc406Sopenharmony_ci#   Delete this file prior to any autoconf run where you want to rebuild
44141cc406Sopenharmony_ci#   files to pick up a version string change; and leave it stale to
45141cc406Sopenharmony_ci#   minimize rebuild time after unrelated changes to configure sources.
46141cc406Sopenharmony_ci#
47141cc406Sopenharmony_ci# As with any generated file in a VC'd directory, you should add
48141cc406Sopenharmony_ci# /.version to .gitignore, so that you don't accidentally commit it.
49141cc406Sopenharmony_ci# .tarball-version is never generated in a VC'd directory, so needn't
50141cc406Sopenharmony_ci# be listed there.
51141cc406Sopenharmony_ci#
52141cc406Sopenharmony_ci# Use the following line in your configure.ac, so that $(VERSION) will
53141cc406Sopenharmony_ci# automatically be up-to-date each time configure is run (and note that
54141cc406Sopenharmony_ci# since configure.ac no longer includes a version string, Makefile rules
55141cc406Sopenharmony_ci# should not depend on configure.ac for version updates).
56141cc406Sopenharmony_ci#
57141cc406Sopenharmony_ci# AC_INIT([GNU project],
58141cc406Sopenharmony_ci#         m4_esyscmd([build-aux/git-version-gen .tarball-version]),
59141cc406Sopenharmony_ci#         [bug-project@example])
60141cc406Sopenharmony_ci#
61141cc406Sopenharmony_ci# Then use the following lines in your Makefile.am, so that .version
62141cc406Sopenharmony_ci# will be present for dependencies, and so that .version and
63141cc406Sopenharmony_ci# .tarball-version will exist in distribution tarballs.
64141cc406Sopenharmony_ci#
65141cc406Sopenharmony_ci# EXTRA_DIST = $(top_srcdir)/.version
66141cc406Sopenharmony_ci# BUILT_SOURCES = $(top_srcdir)/.version
67141cc406Sopenharmony_ci# $(top_srcdir)/.version:
68141cc406Sopenharmony_ci#	echo $(VERSION) > $@-t && mv $@-t $@
69141cc406Sopenharmony_ci# dist-hook:
70141cc406Sopenharmony_ci#	echo $(VERSION) > $(distdir)/.tarball-version
71141cc406Sopenharmony_ci
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_cime=$0
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_ciyear=`expr "$scriptversion" : '\([^-]*\)'`
76141cc406Sopenharmony_civersion="git-version-gen $scriptversion
77141cc406Sopenharmony_ci
78141cc406Sopenharmony_ciCopyright $year Free Software Foundation, Inc.
79141cc406Sopenharmony_ciThere is NO warranty.  You may redistribute this software
80141cc406Sopenharmony_ciunder the terms of the GNU General Public License.
81141cc406Sopenharmony_ciFor more information about these matters, see the files named COPYING."
82141cc406Sopenharmony_ci
83141cc406Sopenharmony_ciusage="\
84141cc406Sopenharmony_ciUsage: $me [OPTION]... \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT]
85141cc406Sopenharmony_ciPrint a version string.
86141cc406Sopenharmony_ci
87141cc406Sopenharmony_ciOptions:
88141cc406Sopenharmony_ci
89141cc406Sopenharmony_ci   --prefix PREFIX    prefix of git tags (default 'v')
90141cc406Sopenharmony_ci   --fallback VERSION
91141cc406Sopenharmony_ci                      fallback version to use if \"git --version\" fails
92141cc406Sopenharmony_ci
93141cc406Sopenharmony_ci   --help             display this help and exit
94141cc406Sopenharmony_ci   --version          output version information and exit
95141cc406Sopenharmony_ci
96141cc406Sopenharmony_ciRunning without arguments will suffice in most cases."
97141cc406Sopenharmony_ci
98141cc406Sopenharmony_ciprefix=v
99141cc406Sopenharmony_cifallback=
100141cc406Sopenharmony_ci
101141cc406Sopenharmony_ciwhile test $# -gt 0; do
102141cc406Sopenharmony_ci  case $1 in
103141cc406Sopenharmony_ci    --help) echo "$usage"; exit 0;;
104141cc406Sopenharmony_ci    --version) echo "$version"; exit 0;;
105141cc406Sopenharmony_ci    --prefix) shift; prefix=${1?};;
106141cc406Sopenharmony_ci    --fallback) shift; fallback=${1?};;
107141cc406Sopenharmony_ci    -*)
108141cc406Sopenharmony_ci      echo "$0: Unknown option '$1'." >&2
109141cc406Sopenharmony_ci      echo "$0: Try '--help' for more information." >&2
110141cc406Sopenharmony_ci      exit 1;;
111141cc406Sopenharmony_ci    *)
112141cc406Sopenharmony_ci      if test "x$tarball_version_file" = x; then
113141cc406Sopenharmony_ci        tarball_version_file="$1"
114141cc406Sopenharmony_ci      elif test "x$tag_sed_script" = x; then
115141cc406Sopenharmony_ci        tag_sed_script="$1"
116141cc406Sopenharmony_ci      else
117141cc406Sopenharmony_ci        echo "$0: extra non-option argument '$1'." >&2
118141cc406Sopenharmony_ci        exit 1
119141cc406Sopenharmony_ci      fi;;
120141cc406Sopenharmony_ci  esac
121141cc406Sopenharmony_ci  shift
122141cc406Sopenharmony_cidone
123141cc406Sopenharmony_ci
124141cc406Sopenharmony_ciif test "x$tarball_version_file" = x; then
125141cc406Sopenharmony_ci    echo "$usage"
126141cc406Sopenharmony_ci    exit 1
127141cc406Sopenharmony_cifi
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_citag_sed_script="${tag_sed_script:-s/x/x/}"
130141cc406Sopenharmony_ci
131141cc406Sopenharmony_cinl='
132141cc406Sopenharmony_ci'
133141cc406Sopenharmony_ci
134141cc406Sopenharmony_ci# Avoid meddling by environment variable of the same name.
135141cc406Sopenharmony_civ=
136141cc406Sopenharmony_civ_from_git=
137141cc406Sopenharmony_ci
138141cc406Sopenharmony_ci# First see if there is a tarball-only version file.
139141cc406Sopenharmony_ci# then try "git describe", then default.
140141cc406Sopenharmony_ciif test -f $tarball_version_file
141141cc406Sopenharmony_cithen
142141cc406Sopenharmony_ci    v=`cat $tarball_version_file` || v=
143141cc406Sopenharmony_ci    case $v in
144141cc406Sopenharmony_ci        *$nl*) v= ;; # reject multi-line output
145141cc406Sopenharmony_ci    esac
146141cc406Sopenharmony_ci    test "x$v" = x \
147141cc406Sopenharmony_ci        && echo "$0: WARNING: $tarball_version_file is damaged" 1>&2
148141cc406Sopenharmony_cifi
149141cc406Sopenharmony_ci
150141cc406Sopenharmony_ciif test "x$v" != x
151141cc406Sopenharmony_cithen
152141cc406Sopenharmony_ci    : # use $v
153141cc406Sopenharmony_ci# Otherwise, if there is at least one git commit involving the working
154141cc406Sopenharmony_ci# directory, and "git describe" output looks sensible, use that to
155141cc406Sopenharmony_ci# derive a version string.
156141cc406Sopenharmony_cielif test "`git log -1 --pretty=format:x . 2>&1`" = x \
157141cc406Sopenharmony_ci    && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \
158141cc406Sopenharmony_ci          || git describe --abbrev=4 HEAD 2>/dev/null` \
159141cc406Sopenharmony_ci    && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
160141cc406Sopenharmony_ci    && case $v in
161141cc406Sopenharmony_ci         $prefix[0-9]*) ;;
162141cc406Sopenharmony_ci         *) (exit 1) ;;
163141cc406Sopenharmony_ci       esac
164141cc406Sopenharmony_cithen
165141cc406Sopenharmony_ci    # Is this a new git that lists number of commits since the last
166141cc406Sopenharmony_ci    # tag or the previous older version that did not?
167141cc406Sopenharmony_ci    #   Newer: v6.10-77-g0f8faeb
168141cc406Sopenharmony_ci    #   Older: v6.10-g0f8faeb
169141cc406Sopenharmony_ci    vprefix=`expr "X$v" : 'X\(.*\)-g[^-]*$'` || vprefix=$v
170141cc406Sopenharmony_ci    case $vprefix in
171141cc406Sopenharmony_ci        *-*) : git describe is probably okay three part flavor ;;
172141cc406Sopenharmony_ci        *)
173141cc406Sopenharmony_ci            : git describe is older two part flavor
174141cc406Sopenharmony_ci            # Recreate the number of commits and rewrite such that the
175141cc406Sopenharmony_ci            # result is the same as if we were using the newer version
176141cc406Sopenharmony_ci            # of git describe.
177141cc406Sopenharmony_ci            vtag=`echo "$v" | sed 's/-.*//'`
178141cc406Sopenharmony_ci            commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \
179141cc406Sopenharmony_ci                || { commit_list=failed;
180141cc406Sopenharmony_ci                     echo "$0: WARNING: git rev-list failed" 1>&2; }
181141cc406Sopenharmony_ci            numcommits=`echo "$commit_list" | wc -l`
182141cc406Sopenharmony_ci            v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
183141cc406Sopenharmony_ci            test "$commit_list" = failed && v=UNKNOWN
184141cc406Sopenharmony_ci            ;;
185141cc406Sopenharmony_ci    esac
186141cc406Sopenharmony_ci
187141cc406Sopenharmony_ci    # Change the penultimate "-" to ".", for version-comparing tools.
188141cc406Sopenharmony_ci    # Remove the "g" to save a byte.
189141cc406Sopenharmony_ci    v=`echo "$v" | sed 's/-\([^-]*\)-g\([^-]*\)$/.\1-\2/'`;
190141cc406Sopenharmony_ci    v_from_git=1
191141cc406Sopenharmony_cielif test "x$fallback" = x || git --version >/dev/null 2>&1; then
192141cc406Sopenharmony_ci    v=UNKNOWN
193141cc406Sopenharmony_cielse
194141cc406Sopenharmony_ci    v=$fallback
195141cc406Sopenharmony_cifi
196141cc406Sopenharmony_ci
197141cc406Sopenharmony_civ=`echo "$v" |sed "s/^$prefix//"`
198141cc406Sopenharmony_ci
199141cc406Sopenharmony_ci# Test whether to append the "-dirty" suffix only if the version
200141cc406Sopenharmony_ci# string we're using came from git.  I.e., skip the test if it's "UNKNOWN"
201141cc406Sopenharmony_ci# or if it came from .tarball-version.
202141cc406Sopenharmony_ciif test "x$v_from_git" != x; then
203141cc406Sopenharmony_ci  # Don't declare a version "dirty" merely because a timestamp has changed.
204141cc406Sopenharmony_ci  git update-index --refresh > /dev/null 2>&1
205141cc406Sopenharmony_ci
206141cc406Sopenharmony_ci  dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty=
207141cc406Sopenharmony_ci  case "$dirty" in
208141cc406Sopenharmony_ci      '') ;;
209141cc406Sopenharmony_ci      *) # Append the suffix only if there isn't one already.
210141cc406Sopenharmony_ci          case $v in
211141cc406Sopenharmony_ci            *-dirty) ;;
212141cc406Sopenharmony_ci            *) v="$v-dirty" ;;
213141cc406Sopenharmony_ci          esac ;;
214141cc406Sopenharmony_ci  esac
215141cc406Sopenharmony_cifi
216141cc406Sopenharmony_ci
217141cc406Sopenharmony_ci# Omit the trailing newline, so that m4_esyscmd can use the result directly.
218141cc406Sopenharmony_ciprintf %s "$v"
219141cc406Sopenharmony_ci
220141cc406Sopenharmony_ci# Local variables:
221141cc406Sopenharmony_ci# eval: (add-hook 'before-save-hook 'time-stamp)
222141cc406Sopenharmony_ci# time-stamp-start: "scriptversion="
223141cc406Sopenharmony_ci# time-stamp-format: "%:y-%02m-%02d.%02H"
224141cc406Sopenharmony_ci# time-stamp-time-zone: "UTC0"
225141cc406Sopenharmony_ci# time-stamp-end: "; # UTC"
226141cc406Sopenharmony_ci# End:
227