git-version-gen 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/sh
  2. # Print a version string.
  3. scriptversion=2018-08-31.20; # UTC
  4. # Copyright (C) 2018 Red Hat, Inc.
  5. # Copyright (C) 2007-2010 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. # It is probably wise to add these two files to .gitignore, so that you
  46. # don't accidentally commit either generated file.
  47. #
  48. # In order to use git archive versions another two files has to be presented:
  49. #
  50. # .gitarchive-version - present in checked-out repository and git
  51. # archive tarball, but not in the distribution tarball. Used as a last
  52. # option for version. File must contain special string $Format:%d$,
  53. # which is substitued by git on archive operation.
  54. #
  55. # .gitattributes - present in checked-out repository and git archive
  56. # tarball, but not in the distribution tarball. Must set export-subst
  57. # attribute for .gitarchive-version file.
  58. #
  59. # Use the following line in your configure.ac, so that $(VERSION) will
  60. # automatically be up-to-date each time configure is run (and note that
  61. # since configure.ac no longer includes a version string, Makefile rules
  62. # should not depend on configure.ac for version updates).
  63. #
  64. # AC_INIT([GNU project],
  65. # m4_esyscmd([build-aux/git-version-gen .tarball-version]),
  66. # [bug-project@example])
  67. #
  68. # Then use the following lines in your Makefile.am, so that .version
  69. # will be present for dependencies, and so that .tarball-version will
  70. # exist in distribution tarballs.
  71. #
  72. # BUILT_SOURCES = $(top_srcdir)/.version
  73. # $(top_srcdir)/.version:
  74. # echo $(VERSION) > $@-t && mv $@-t $@
  75. # dist-hook:
  76. # echo $(VERSION) > $(distdir)/.tarball-version
  77. case $# in
  78. 1|2|3) ;;
  79. *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version" \
  80. '[$srcdir/.gitarchive-version] [TAG-NORMALIZATION-SED-SCRIPT]'
  81. exit 1;;
  82. esac
  83. tarball_version_file=$1
  84. gitarchive_version_file=$2
  85. tag_sed_script="${3:-s/x/x/}"
  86. nl='
  87. '
  88. # Avoid meddling by environment variable of the same name.
  89. v=
  90. # First see if there is a tarball-only version file.
  91. # then try "git describe", then default.
  92. if test -f $tarball_version_file
  93. then
  94. v=`cat $tarball_version_file` || exit 1
  95. case $v in
  96. *$nl*) v= ;; # reject multi-line output
  97. [0-9]*) ;;
  98. *) v= ;;
  99. esac
  100. test -z "$v" \
  101. && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
  102. fi
  103. if test -n "$v"
  104. then
  105. : # use $v
  106. # Otherwise, if there is at least one git commit involving the working
  107. # directory, and "git describe" output looks sensible, use that to
  108. # derive a version string.
  109. elif test "`git log -1 --pretty=format:x . 2>&1`" = x \
  110. && v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \
  111. || git describe --abbrev=4 HEAD 2>/dev/null` \
  112. && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
  113. && case $v in
  114. v[0-9]*) ;;
  115. *) (exit 1) ;;
  116. esac
  117. then
  118. # Is this a new git that lists number of commits since the last
  119. # tag or the previous older version that did not?
  120. # Newer: v6.10-77-g0f8faeb
  121. # Older: v6.10-g0f8faeb
  122. case $v in
  123. *-*-*) : git describe is okay three part flavor ;;
  124. *-*)
  125. : git describe is older two part flavor
  126. # Recreate the number of commits and rewrite such that the
  127. # result is the same as if we were using the newer version
  128. # of git describe.
  129. vtag=`echo "$v" | sed 's/-.*//'`
  130. numcommits=`git rev-list "$vtag"..HEAD | wc -l`
  131. v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
  132. ;;
  133. esac
  134. # Change the first '-' to a '.', so version-comparing tools work properly.
  135. # Remove the "g" in git describe's output string, to save a byte.
  136. v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
  137. else
  138. if test -f $gitarchive_version_file
  139. then
  140. v=`sed 's/^.*tag: \(v[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/' $gitarchive_version_file` || exit 1
  141. case $v in
  142. *$nl*) v= ;; # reject multi-line output
  143. v[0-9]*) ;;
  144. *) v= ;;
  145. esac
  146. test -z "$v" \
  147. && echo "$0: WARNING: $gitarchive_version_file doesn't contain valid version tag" 1>&2 \
  148. && v=UNKNOWN
  149. else
  150. v=UNKNOWN
  151. fi
  152. fi
  153. if test "$v" = "UNKNOWN"
  154. then
  155. echo "$0: ERROR: Can't find valid version. Please use valid git repository," \
  156. "released tarball or version tagged archive" 1>&2
  157. exit 1
  158. fi
  159. v=`echo "$v" |sed 's/^v//'`
  160. # Don't declare a version "dirty" merely because a time stamp has changed.
  161. git update-index --refresh > /dev/null 2>&1
  162. dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
  163. case "$dirty" in
  164. '') ;;
  165. *) # Append the suffix only if there isn't one already.
  166. case $v in
  167. *-dirty) ;;
  168. *) v="$v-dirty" ;;
  169. esac ;;
  170. esac
  171. # Omit the trailing newline, so that m4_esyscmd can use the result directly.
  172. echo "$v" | tr -d "$nl"
  173. # Local variables:
  174. # eval: (add-hook 'write-file-hooks 'time-stamp)
  175. # time-stamp-start: "scriptversion="
  176. # time-stamp-format: "%:y-%02m-%02d.%02H"
  177. # time-stamp-time-zone: "UTC"
  178. # time-stamp-end: "; # UTC"
  179. # End: