Philipp Wollermann | a5afe95 | 2016-06-21 14:58:09 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 2 | |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Philipp Wollermann | a5afe95 | 2016-06-21 14:58:09 +0000 | [diff] [blame] | 17 | set -eu |
| 18 | |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 19 | # Some common method for release scripts |
| 20 | |
| 21 | # A release candidate is created from a branch named "release-%name%" |
| 22 | # where %name% is the name of the release. Once promoted to a release, |
| 23 | # A tag %name% will be created from this branch and the corresponding |
| 24 | # branch removed. |
| 25 | # The last commit of the release branch is always a commit containing |
| 26 | # the release notes in the commit message and updating the CHANGELOG.md. |
| 27 | # This last commit will be cherry-picked back in the master branch |
| 28 | # when the release candidate is promoted to a release. |
| 29 | # To follow tracks and to support how CI systems fetch the refs, we |
| 30 | # store two commit notes: the release name and the candidate number. |
| 31 | |
Philipp Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 32 | # Get the short hash of a commit |
| 33 | function __git_commit_hash() { |
| 34 | git rev-parse "${1}" |
| 35 | } |
| 36 | |
| 37 | # Get the subject (first line of the commit message) of a commit |
| 38 | function __git_commit_subject() { |
| 39 | git show -s --pretty=format:%s "$@" |
| 40 | } |
| 41 | |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 42 | # Returns the branch name of the current git repository |
| 43 | function git_get_branch() { |
| 44 | git symbolic-ref --short HEAD |
| 45 | } |
| 46 | |
Yun Peng | 123f2b9 | 2020-02-12 04:34:00 -0800 | [diff] [blame] | 47 | # Returns the tag name of the current git repository |
| 48 | function git_get_tag() { |
| 49 | git describe --tag |
| 50 | } |
| 51 | |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 52 | # Show the commit message of the ref specified in argument |
| 53 | function git_commit_msg() { |
| 54 | git show -s --pretty=format:%B "$@" |
| 55 | } |
| 56 | |
Yun Peng | 123f2b9 | 2020-02-12 04:34:00 -0800 | [diff] [blame] | 57 | # Extract the release candidate number from the git branch name |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 58 | function get_release_candidate() { |
Yun Peng | 123f2b9 | 2020-02-12 04:34:00 -0800 | [diff] [blame] | 59 | # Match rcX and return X |
| 60 | git_get_branch 2>/dev/null | grep -Po "(?<=rc)([0-9]|\.)*$" || true |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Yun Peng | 123f2b9 | 2020-02-12 04:34:00 -0800 | [diff] [blame] | 63 | # Extract the release name from the git branch name |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 64 | function get_release_name() { |
fwe | 1f52e9a | 2021-05-31 08:58:10 -0700 | [diff] [blame] | 65 | # Match branch name release-X.X.X[-pre.XXXXXXXX.X]rcY and return X.X.X[-pre.XXXXXXXX.X] |
| 66 | # or match tag name X.X.X[-pre.XXXXXXXX.X] and return X.X.X[-pre.XXXXXXXX.X] |
| 67 | git_get_branch 2>/dev/null | grep -Po "(?<=release-)([0-9]|\.)*(-pre\.[0-9]{8}(\.[0-9]+){1,2})?(?=rc)" || git_get_tag | grep -Po "^([0-9]|\.)*(-pre\.[0-9]{8}(\.[0-9]+){1,2})?$" || true |
| 68 | } |
| 69 | |
| 70 | # Returns whether this is a rolling release (or an RCs of one) |
| 71 | function is_rolling_release() { |
| 72 | if [[ "$(get_release_name)" =~ [0-9]+\.[0-9]+\.[0-9]+-pre\.[0-9]{8}\.[0-9]+(\.[0-9]+)?$ ]]; then |
| 73 | echo 1 |
| 74 | else |
| 75 | echo 0 |
| 76 | fi |
| 77 | } |
| 78 | |
| 79 | # Returns the name of the LTS release that belongs to the current rolling release |
| 80 | function get_lts_name() { |
| 81 | local release_name="$(get_release_name)" |
| 82 | echo "${release_name}" | grep -oE "^([0-9]+\.[0-9]+\.[0-9]+)" |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 85 | # Get the list of commit hashes between two revisions |
| 86 | function git_log_hash() { |
| 87 | local baseline="$1" |
| 88 | local head="$2" |
| 89 | shift 2 |
| 90 | git log --pretty=format:%H "${baseline}".."${head}" "$@" |
| 91 | } |
| 92 | |
Yun Peng | 123f2b9 | 2020-02-12 04:34:00 -0800 | [diff] [blame] | 93 | # Extract the full release name from the branch name or tag name |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 94 | function get_full_release_name() { |
| 95 | local name="$(get_release_name "$@")" |
| 96 | local rc="$(get_release_candidate "$@")" |
| 97 | if [ -n "${rc}" ]; then |
| 98 | echo "${name}rc${rc}" |
| 99 | else |
| 100 | echo "${name}" |
| 101 | fi |
| 102 | } |
| 103 | |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 104 | # Returns the info from the branch of the release. It is the current branch |
| 105 | # but it errors out if the current branch is not a release branch. This |
| 106 | # method returns the tag of the release and the number of the current |
| 107 | # candidate in this release. |
| 108 | function get_release_branch() { |
| 109 | local branch_name=$(git_get_branch) |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 110 | if [ -z "$(get_release_name)" ] || [ -z "$(get_release_candidate)" ]; then |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 111 | echo "Not a release branch: ${branch_name}." >&2 |
| 112 | exit 1 |
| 113 | fi |
| 114 | echo "${branch_name}" |
| 115 | } |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 116 | |
| 117 | # fmt behaves differently on *BSD and on GNU/Linux, use fold. |
| 118 | function wrap_text() { |
| 119 | fold -s -w $1 | sed 's/ *$//' |
| 120 | } |
| 121 | |