1#! /bin/sh
2# Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9# This will increase the version number and pre-release tag, according to the
10# current state of the source tree, and the function's first argument (called
11# |next| internally), which is how the caller tells what the next step should
12# be.
13#
14# The possible current source tree states are:
15# ''            The source is in a released state.
16# 'dev'         The source is in development.  This is the normal state.
17# 'alpha', 'alphadev'
18#               The source is undergoing a series of alpha releases.
19# 'beta', 'betadev'
20#               The source is undergoing a series of beta releases.
21# These states are computed from $PRE_LABEL and $TYPE
22#
23# The possible |next| values are:
24# 'alpha'       The source tree should move to an alpha release state, or
25#               stay there.  This trips the alpha / pre-release counter.
26# 'beta'        The source tree should move to a beta release state, or
27#               stay there.  This trips the beta / pre-release counter.
28# 'final'       The source tree should move to a final release (assuming it's
29#               currently in one of the alpha or beta states).  This turns
30#               off the alpha or beta states.
31# ''            The source tree should move to the next release.  The exact
32#               meaning depends on the current source state.  It may mean
33#               tripping the alpha / beta / pre-release counter, or increasing
34#               the PATCH number.
35#
36# 'minor'       The source tree should move to the next minor version.  This
37#               should only be used in the master branch when a release branch
38#               has been created.
39#
40# This function expects there to be a function called fixup_version(), which
41# SHOULD take the |next| as first argument, and SHOULD increase the label
42# counter or the PATCH number accordingly, but only when the current
43# state is "in development".
44
45next_release_state () {
46    local next="$1"
47    local today="$(date '+%-d %b %Y')"
48    local retry=true
49
50    local before="$PRE_LABEL$TYPE"
51
52    while $retry; do
53        retry=false
54
55        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$before=$before"
56        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$next=$next"
57        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$MAJOR=$MAJOR"
58        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$MINOR=$MINOR"
59        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$PATCH=$PATCH"
60        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$TYPE=$TYPE"
61        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$PRE_LABEL=$PRE_LABEL"
62        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$PRE_NUM=$PRE_NUM"
63        $DEBUG >&2 "DEBUG[next_release_state]: BEGIN: \$RELEASE_DATE=$RELEASE_DATE"
64
65        case "$before+$next" in
66            # MAKING ALPHA RELEASES ##################################
67
68            # Alpha releases can't be made from beta versions or real versions
69            beta*+alpha | +alpha )
70                echo >&2 "Invalid state for an alpha release"
71                echo >&2 "Try --beta or --final, or perhaps nothing"
72                exit 1
73                ;;
74            # For alpha releases, the tag update is dev => alpha or
75            # alpha dev => alpha for the release itself, and
76            # alpha => alpha dev for post release.
77            dev+alpha | alphadev+alpha )
78                TYPE=
79                RELEASE_DATE="$today"
80                fixup_version "alpha"
81                ;;
82            alpha+alpha )
83                TYPE=dev
84                RELEASE_DATE=
85                fixup_version "alpha"
86                ;;
87
88            # MAKING BETA RELEASES ###################################
89
90            # Beta releases can't be made from real versions
91            +beta )
92                echo >&2 "Invalid state for beta release"
93                echo >&2 "Try --final, or perhaps nothing"
94                exit 1
95                ;;
96            # For beta releases, the tag update is dev => beta1, or
97            # alpha{n}-dev => beta1 when transitioning from alpha to
98            # beta, or beta{n}-dev => beta{n} for the release itself,
99            # or beta{n} => beta{n+1}-dev for post release.
100            dev+beta | alphadev+beta | betadev+beta )
101                TYPE=
102                RELEASE_DATE="$today"
103                fixup_version "beta"
104                ;;
105            beta+beta )
106                TYPE=dev
107                RELEASE_DATE=
108                fixup_version "beta"
109                ;;
110            # It's possible to switch from alpha to beta in the
111            # post release.  That's what --next-beta does.
112            alpha+beta )
113                TYPE=dev
114                RELEASE_DATE=
115                fixup_version "beta"
116                ;;
117
118            # MAKING FINAL RELEASES ##################################
119
120            # Final releases can't be made from the main development branch
121            dev+final)
122                echo >&2 "Invalid state for final release"
123                echo >&2 "This should have been preceded by an alpha or a beta release"
124                exit 1
125                ;;
126            # For final releases, the starting point must be a dev state
127            alphadev+final | betadev+final )
128                TYPE=
129                RELEASE_DATE="$today"
130                fixup_version "final"
131                ;;
132            # The final step of a final release is to switch back to
133            # development
134            +final )
135                TYPE=dev
136                RELEASE_DATE=
137                fixup_version "final"
138                ;;
139
140            # SWITCHING TO THE NEXT MINOR RELEASE ####################
141
142            *+minor )
143                TYPE=dev
144                RELEASE_DATE=
145                fixup_version "minor"
146                ;;
147
148            # MAKING DEFAULT RELEASES ################################
149
150            # If we're coming from a non-dev, simply switch to dev.
151            # fixup_version() should trip up the PATCH number.
152            + )
153                TYPE=dev
154                fixup_version ""
155                ;;
156
157            # If we're coming from development, switch to non-dev, unless
158            # the PATCH number is zero.  If it is, we force the caller to
159            # go through the alpha and beta release process.
160            dev+ )
161                if [ "$PATCH" = "0" ]; then
162                    echo >&2 "Can't update PATCH version number from 0"
163                    echo >&2 "Please use --alpha or --beta"
164                    exit 1
165                fi
166                TYPE=
167                RELEASE_DATE="$today"
168                fixup_version ""
169                ;;
170
171            # If we're currently in alpha, we continue with alpha, as if
172            # the user had specified --alpha
173            alpha*+ )
174                next=alpha
175                retry=true
176                ;;
177
178            # If we're currently in beta, we continue with beta, as if
179            # the user had specified --beta
180            beta*+ )
181                next=beta
182                retry=true
183                ;;
184
185            *)
186                echo >&2 "Invalid combination of options"
187                exit 1
188                ;;
189        esac
190
191        $DEBUG >&2 "DEBUG[next_release_state]: END: \$before=$before"
192        $DEBUG >&2 "DEBUG[next_release_state]: END: \$next=$next"
193        $DEBUG >&2 "DEBUG[next_release_state]: END: \$MAJOR=$MAJOR"
194        $DEBUG >&2 "DEBUG[next_release_state]: END: \$MINOR=$MINOR"
195        $DEBUG >&2 "DEBUG[next_release_state]: END: \$PATCH=$PATCH"
196        $DEBUG >&2 "DEBUG[next_release_state]: END: \$TYPE=$TYPE"
197        $DEBUG >&2 "DEBUG[next_release_state]: END: \$PRE_LABEL=$PRE_LABEL"
198        $DEBUG >&2 "DEBUG[next_release_state]: END: \$PRE_NUM=$PRE_NUM"
199        $DEBUG >&2 "DEBUG[next_release_state]: END: \$RELEASE_DATE=$RELEASE_DATE"
200    done
201}
202