| 1 | #!/bin/sh |
| 2 | # Print a version string. |
| 3 | scriptversion=2025-06-10.02; # UTC |
| 4 | |
| 5 | # Copyright (C) 2007-2026 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 | |
| 22 | # It may be run in the following ways, presuming the script is invoked |
| 23 | # like "./git-version-gen .tarball-version": |
| 24 | # |
| 25 | # - from a git repository in which the "git describe" command below |
| 26 | # produces useful output (thus requiring at least one signed tag) |
| 27 | # |
| 28 | # - from a "make dist" non-git-repo directory containing a |
| 29 | # .tarball-version file |
| 30 | # |
| 31 | # - from a "git archive" non-git-repo directory containing a |
| 32 | # .tarball-version-git file |
| 33 | |
| 34 | # In order to use intra-version strings in your project, you will need some |
| 35 | # separate generated version string files: |
| 36 | # |
| 37 | # .tarball-version - contains the version number assigned by the maintainer. |
| 38 | # Present or missing in a checked-out repository, at the discretion of the |
| 39 | # maintainer/contributor. Present in a distribution tarball, because the |
| 40 | # tarball does not include the version control history. |
| 41 | # Used by git-version-gen as an override. |
| 42 | # Cannot be used in any dependencies (since it may be absent). |
| 43 | # GNUmakefile has hooks to force a reconfigure at "make dist" time to get |
| 44 | # the value correct, without penalizing normal development with extra |
| 45 | # reconfigures. |
| 46 | # |
| 47 | # .tarball-version-git - a file committed to git containing a single |
| 48 | # line with the string $Format:%(describe)$ and that the file is |
| 49 | # marked in .gitattributes with ".tarball-version-git export-subst". |
| 50 | # If the file doesn't exist or the export-subst keyword wasn't |
| 51 | # effective, the file is ignored. |
| 52 | # |
| 53 | # Use the following snippet in your configure.ac, so that $(VERSION) will |
| 54 | # automatically be up-to-date each time configure is run (and note that |
| 55 | # since configure.ac no longer includes a version string, Makefile rules |
| 56 | # should not depend on configure.ac for version updates). |
| 57 | # |
| 58 | # AC_INIT([@var{package}], [package]) |
| 59 | # AC_CONFIG_SRCDIR([@var{unique-file-in-source-dir}]) |
| 60 | # AC_CONFIG_AUX_DIR([build-aux]) |
| 61 | # VERSION_NUMBER=`cd $srcdir \ |
| 62 | # && build-aux/git-version-gen .tarball-version` |
| 63 | # gl_INIT_PACKAGE_VERSION([$VERSION_NUMBER]) |
| 64 | # AM_INIT_AUTOMAKE([@var{options}]) |
| 65 | # |
| 66 | # Then use the following lines in your Makefile.am, so that |
| 67 | # .tarball-version will exist in distribution tarballs. |
| 68 | # |
| 69 | # dist-hook: dist-tarball-version |
| 70 | # .PHONY: dist-tarball-version |
| 71 | # dist-tarball-version: |
| 72 | # echo '$(VERSION)' > $(distdir)/.tarball-version |
| 73 | # |
| 74 | # To setup support for "git archive" tarballs, use the following: |
| 75 | # |
| 76 | # echo '$Format:%(describe)$' > .tarball-version-git |
| 77 | # echo '.tarball-version-git export-subst' >> .gitattributes |
| 78 | # git add .tarball-version-git .gitattributes |
| 79 | # git commit -m "Add .tarball-version-git for git-version-gen." |
| 80 | |
| 81 | me=$0 |
| 82 | |
| 83 | year=`expr "$scriptversion" : '\([^-]*\)'` |
| 84 | version="git-version-gen $scriptversion |
| 85 | |
| 86 | Copyright (C) ${year} Free Software Foundation, Inc. |
| 87 | License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. |
| 88 | This is free software: you are free to change and redistribute it. |
| 89 | There is NO WARRANTY, to the extent permitted by law." |
| 90 | |
| 91 | usage="\ |
| 92 | Usage: $me [OPTION]... \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT] |
| 93 | Print a version string. |
| 94 | |
| 95 | Options: |
| 96 | |
| 97 | --prefix PREFIX prefix of git tags (default 'v') |
| 98 | --fallback VERSION |
| 99 | fallback version to use if \"git --version\" fails |
| 100 | |
| 101 | --help display this help and exit |
| 102 | --version output version information and exit |
| 103 | |
| 104 | Send patches and bug reports to <[email protected]>." |
| 105 | |
| 106 | prefix=v |
| 107 | fallback= |
| 108 | |
| 109 | while test $# -gt 0; do |
| 110 | case $1 in |
| 111 | --help) echo "$usage"; exit 0;; |
| 112 | --version) echo "$version"; exit 0;; |
| 113 | --prefix) shift; prefix=${1?};; |
| 114 | --fallback) shift; fallback=${1?};; |
| 115 | -*) |
| 116 | echo "$0: Unknown option '$1'." >&2 |
| 117 | echo "$0: Try '--help' for more information." >&2 |
| 118 | exit 1;; |
| 119 | *) |
| 120 | if test "x$tarball_version_file" = x; then |
| 121 | tarball_version_file="$1" |
| 122 | elif test "x$tag_sed_script" = x; then |
| 123 | tag_sed_script="$1" |
| 124 | else |
| 125 | echo "$0: extra non-option argument '$1'." >&2 |
| 126 | exit 1 |
| 127 | fi;; |
| 128 | esac |
| 129 | shift |
| 130 | done |
| 131 | |
| 132 | if test "x$tarball_version_file" = x; then |
| 133 | echo "$usage" |
| 134 | exit 1 |
| 135 | fi |
| 136 | |
| 137 | tag_sed_script="${tag_sed_script:-s/x/x/}" |
| 138 | |
| 139 | nl=' |
| 140 | ' |
| 141 | |
| 142 | # Avoid meddling by environment variable of the same name. |
| 143 | v= |
| 144 | v_from_git= |
| 145 | |
| 146 | # First see if there is a tarball-only version file. |
| 147 | # then try "git describe", then default. |
| 148 | if test -f "$tarball_version_file" |
| 149 | then |
| 150 | v=`cat "$tarball_version_file"` || v= |
| 151 | case $v in |
| 152 | *$nl*) v= ;; # reject multi-line output |
| 153 | esac |
| 154 | test "x$v" = x \ |
| 155 | && echo "$0: WARNING: $tarball_version_file is damaged" 1>&2 |
| 156 | fi |
| 157 | |
| 158 | if test "x$v" != x |
| 159 | then |
| 160 | : # use $v |
| 161 | # Otherwise, if there is at least one git commit involving the working |
| 162 | # directory, and "git describe" output looks sensible, use that to |
| 163 | # derive a version string. |
| 164 | elif test "`git log -1 --pretty=format:x . 2>&1`" = x \ |
| 165 | && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ |
| 166 | || git describe --abbrev=4 HEAD 2>/dev/null` \ |
| 167 | && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \ |
| 168 | && case $v in |
| 169 | $prefix[0-9]*) ;; |
| 170 | *) (exit 1) ;; |
| 171 | esac |
| 172 | then |
| 173 | # Is this a new git that lists number of commits since the last |
| 174 | # tag or the previous older version that did not? |
| 175 | # Newer: v6.10-77-g0f8faeb |
| 176 | # Older: v6.10-g0f8faeb |
| 177 | vprefix=`expr "X$v" : 'X\(.*\)-g[^-]*$'` || vprefix=$v |
| 178 | case $vprefix in |
| 179 | *-*) : git describe is probably okay three part flavor ;; |
| 180 | *) |
| 181 | : git describe is older two part flavor |
| 182 | # Recreate the number of commits and rewrite such that the |
| 183 | # result is the same as if we were using the newer version |
| 184 | # of git describe. |
| 185 | vtag=`echo "$v" | sed 's/-.*//'` |
| 186 | commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \ |
| 187 | || { commit_list=failed; |
| 188 | echo "$0: WARNING: git rev-list failed" 1>&2; } |
| 189 | numcommits=`echo "$commit_list" | wc -l` |
| 190 | v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`; |
| 191 | test "$commit_list" = failed && v=UNKNOWN |
| 192 | ;; |
| 193 | esac |
| 194 | v_from_git=1 |
| 195 | elif test "x$fallback" = x || git --version >/dev/null 2>&1; then |
| 196 | v=UNKNOWN |
| 197 | else |
| 198 | v=$fallback |
| 199 | fi |
| 200 | |
| 201 | if test "x$v" = xUNKNOWN \ |
| 202 | && test -f "$tarball_version_file"-git \ |
| 203 | && v=$(head -1 "$tarball_version_file"-git); then |
| 204 | case $v in |
| 205 | *Format*) v=UNKNOWN ;; |
| 206 | esac |
| 207 | fi |
| 208 | |
| 209 | # Change the penultimate "-" to ".", for version-comparing tools. |
| 210 | # Remove the "g" to save a byte. |
| 211 | v=`echo "$v" | sed 's/-\([^-]*\)-g\([^-]*\)$/.\1-\2/'`; |
| 212 | |
| 213 | v=`echo "$v" |sed "s/^$prefix//"` |
| 214 | |
| 215 | # The "-modified" suffix was previously called "-dirty". While this term |
| 216 | # was invented by the git people to designate a checkout that is not "clean", |
| 217 | # it has a negative connotation that is not in line with the fact that |
| 218 | # a Free Software developer routinely works with modified source code. |
| 219 | # In fact, modifying source code is the *essence* of Free Software. |
| 220 | # What we need here is a term that is suitable for naming tarballs, without |
| 221 | # shaming the developer. Giving a name to a tarball is something else than |
| 222 | # describing the state of a git checkout. |
| 223 | # |
| 224 | # Test whether to append the "-modified" suffix only if the version |
| 225 | # string we're using came from git. I.e., skip the test if it's "UNKNOWN" |
| 226 | # or if it came from .tarball-version. |
| 227 | if test "x$v_from_git" != x; then |
| 228 | # Don't declare a version "modified" merely because a timestamp has changed. |
| 229 | git update-index --refresh > /dev/null 2>&1 |
| 230 | |
| 231 | modified=`exec 2>/dev/null;git diff-index --name-only HEAD` || modified= |
| 232 | case "$modified" in |
| 233 | '') ;; |
| 234 | *) # Append the suffix only if there isn't one already. |
| 235 | case $v in |
| 236 | *-dirty | *-modified) ;; |
| 237 | *) v="$v-modified" ;; |
| 238 | esac ;; |
| 239 | esac |
| 240 | fi |
| 241 | |
| 242 | # Omit the trailing newline, so that m4_esyscmd can use the result directly. |
| 243 | printf %s "$v" |
| 244 | |
| 245 | # Local variables: |
| 246 | # eval: (add-hook 'before-save-hook 'time-stamp nil t) |
| 247 | # time-stamp-start: "scriptversion=" |
| 248 | # time-stamp-format: "%Y-%02m-%02d.%02H" |
| 249 | # time-stamp-time-zone: "UTC0" |
| 250 | # time-stamp-end: "; # UTC" |
| 251 | # End: |