git-version-gen 8.1 KB

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