blob: 5035e358f496096cdf418e75bd3cd467316c38d7 [file] [log] [blame]
Philipp Wollermanna5afe952016-06-21 14:58:09 +00001#!/bin/bash
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +00002
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +00004#
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 Wollermanna5afe952016-06-21 14:58:09 +000017set -eu
18
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000019# 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 Wollermann5fabb432018-03-27 04:37:23 -070032# Get the short hash of a commit
33function __git_commit_hash() {
34 git rev-parse "${1}"
35}
36
37# Get the subject (first line of the commit message) of a commit
38function __git_commit_subject() {
39 git show -s --pretty=format:%s "$@"
40}
41
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000042# Returns the branch name of the current git repository
43function git_get_branch() {
44 git symbolic-ref --short HEAD
45}
46
Yun Peng123f2b92020-02-12 04:34:00 -080047# Returns the tag name of the current git repository
48function git_get_tag() {
49 git describe --tag
50}
51
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000052# Show the commit message of the ref specified in argument
53function git_commit_msg() {
54 git show -s --pretty=format:%B "$@"
55}
56
Yun Peng123f2b92020-02-12 04:34:00 -080057# Extract the release candidate number from the git branch name
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000058function get_release_candidate() {
Yun Peng123f2b92020-02-12 04:34:00 -080059 # Match rcX and return X
60 git_get_branch 2>/dev/null | grep -Po "(?<=rc)([0-9]|\.)*$" || true
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000061}
62
Yun Peng123f2b92020-02-12 04:34:00 -080063# Extract the release name from the git branch name
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000064function get_release_name() {
fwe1f52e9a2021-05-31 08:58:10 -070065 # 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)
71function 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
80function get_lts_name() {
81 local release_name="$(get_release_name)"
82 echo "${release_name}" | grep -oE "^([0-9]+\.[0-9]+\.[0-9]+)"
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000083}
84
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +000085# Get the list of commit hashes between two revisions
86function 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 Peng123f2b92020-02-12 04:34:00 -080093# Extract the full release name from the branch name or tag name
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +000094function 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-Guillerezfa15d392015-07-28 15:54:40 +0000104# 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.
108function get_release_branch() {
109 local branch_name=$(git_get_branch)
Androbincfb2ec02017-06-27 13:47:43 +0200110 if [ -z "$(get_release_name)" ] || [ -z "$(get_release_candidate)" ]; then
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +0000111 echo "Not a release branch: ${branch_name}." >&2
112 exit 1
113 fi
114 echo "${branch_name}"
115}
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000116
117# fmt behaves differently on *BSD and on GNU/Linux, use fold.
118function wrap_text() {
119 fold -s -w $1 | sed 's/ *$//'
120}
121