do-release-commit-and-tag 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/sh
  2. # In a git/autoconf/automake-enabled project with a NEWS file and a version-
  3. # controlled .prev-version file, automate the procedure by which we record
  4. # the date, release-type and version string in the NEWS file. That commit
  5. # will serve to identify the release, so apply a signed tag to it as well.
  6. VERSION=2018-03-07.03 # UTC
  7. # Note: this is a bash script (could be zsh or dash)
  8. # Copyright (C) 2009-2018 Free Software Foundation, Inc.
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  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. # Written by Jim Meyering
  20. ME=$(basename "$0")
  21. warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
  22. die() { warn "$*"; exit 1; }
  23. help()
  24. {
  25. cat <<EOF
  26. Usage: $ME [OPTION...] VERSION RELEASE_TYPE
  27. Run this script from top_srcdir to perform the final pre-release NEWS
  28. update in which the date, release-type and version string are
  29. recorded. Commit that result with a log entry marking the release,
  30. and apply a signed tag. Run it from your project's top-level
  31. directory.
  32. Requirements:
  33. - you use git for version-control
  34. - a version-controlled .prev-version file
  35. - a NEWS file, with line 3 identical to this:
  36. $noteworthy_stub
  37. Options:
  38. --branch=BRANCH set release branch (default: $branch)
  39. -C, --builddir=DIR location of (configured) Makefile (default: $builddir)
  40. --help print this help, then exit
  41. --version print version number, then exit
  42. EXAMPLE:
  43. To update NEWS and tag the beta 8.1 release of coreutils, I would run this:
  44. $ME 8.1 beta
  45. Report bugs and patches to <bug-gnulib@gnu.org>.
  46. EOF
  47. exit
  48. }
  49. version()
  50. {
  51. year=$(echo "$VERSION" | sed 's/[^0-9].*//')
  52. cat <<EOF
  53. $ME $VERSION
  54. Copyright (C) $year Free Software Foundation, Inc,
  55. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
  56. This is free software: you are free to change and redistribute it.
  57. There is NO WARRANTY, to the extent permitted by law.
  58. EOF
  59. exit
  60. }
  61. ## ------ ##
  62. ## Main. ##
  63. ## ------ ##
  64. # Constants.
  65. noteworthy='* Noteworthy changes in release'
  66. noteworthy_stub="$noteworthy ?.? (????-??-??) [?]"
  67. # Variables.
  68. branch=$(git branch | sed -ne '/^\* /{s///;p;q;}')
  69. builddir=.
  70. while test $# != 0
  71. do
  72. # Handle --option=value by splitting apart and putting back on argv.
  73. case $1 in
  74. --*=*)
  75. opt=$(echo "$1" | sed -e 's/=.*//')
  76. val=$(echo "$1" | sed -e 's/[^=]*=//')
  77. shift
  78. set dummy "$opt" "$val" "$@"; shift
  79. ;;
  80. esac
  81. case $1 in
  82. --help|--version) ${1#--};;
  83. --branch) shift; branch=$1; shift ;;
  84. -C|--builddir) shift; builddir=$1; shift ;;
  85. --*) die "unrecognized option: $1";;
  86. *) break;;
  87. esac
  88. done
  89. test $# = 2 \
  90. || die "Usage: $ME [OPTION...] VERSION TYPE"
  91. ver=$1
  92. type=$2
  93. ## ---------------------- ##
  94. ## First, sanity checks. ##
  95. ## ---------------------- ##
  96. # Verify that $ver looks like a version number, and...
  97. echo "$ver"|grep -E '^[0-9][0-9.]*[0-9]$' > /dev/null \
  98. || die "invalid version: $ver"
  99. prev_ver=$(cat .prev-version) \
  100. || die 'failed to determine previous version number from .prev-version'
  101. # Verify that $ver is sensible (> .prev-version).
  102. case $(printf "$prev_ver\n$ver\n"|sort -V -u|tr '\n' ':') in
  103. "$prev_ver:$ver:") ;;
  104. *) die "invalid version: $ver (<= $prev_ver)";;
  105. esac
  106. case $type in
  107. alpha|beta|stable) ;;
  108. *) die "invalid release type: $type";;
  109. esac
  110. # No local modifications allowed.
  111. case $(git diff-index --name-only HEAD) in
  112. '') ;;
  113. *) die 'this tree is dirty; commit your changes first';;
  114. esac
  115. # Ensure the current branch name is correct:
  116. curr_br=$(git rev-parse --symbolic-full-name HEAD)
  117. test "$curr_br" = "refs/heads/$branch" || die not on branch $branch
  118. # Extract package name from Makefile.
  119. Makefile=$builddir/Makefile
  120. pkg=$(sed -n 's/^PACKAGE = \(.*\)/\1/p' "$Makefile") \
  121. || die "failed to determine package name from $Makefile"
  122. # Check that line 3 of NEWS is the stub line about to be replaced.
  123. test "$(sed -n 3p NEWS)" = "$noteworthy_stub" \
  124. || die "line 3 of NEWS must be exactly '$noteworthy_stub'"
  125. ## --------------- ##
  126. ## Then, changes. ##
  127. ## --------------- ##
  128. # Update NEWS to have today's date, plus desired version number and $type.
  129. perl -MPOSIX -ni -e 'my $today = strftime "%F", localtime time;' \
  130. -e 'my ($type, $ver) = qw('"$type $ver"');' \
  131. -e 'my $pfx = "'"$noteworthy"'";' \
  132. -e 'print $.==3 ? "$pfx $ver ($today) [$type]\n" : $_' \
  133. NEWS || die 'failed to update NEWS'
  134. printf "version $ver\n\n* NEWS: Record release date.\n" \
  135. | git commit -F - -a || die 'git commit failed'
  136. git tag -s -m "$pkg $ver" v$ver HEAD || die 'git tag failed'
  137. # Local variables:
  138. # indent-tabs-mode: nil
  139. # eval: (add-hook 'before-save-hook 'time-stamp)
  140. # time-stamp-start: "VERSION="
  141. # time-stamp-format: "%:y-%02m-%02d.%02H"
  142. # time-stamp-time-zone: "UTC0"
  143. # time-stamp-end: " # UTC"
  144. # End: