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