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