4
0

git-version-gen 9.2 KB

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