4
0

git-version-gen 5.8 KB

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