blob: 3df3e32a12f64d6efdc26dcb667ffed133b89720 [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
47# Show the commit message of the ref specified in argument
48function git_commit_msg() {
49 git show -s --pretty=format:%B "$@"
50}
51
52# Extract the release candidate number from the git notes
53function get_release_candidate() {
dmartingfc6c3cb2017-07-28 00:01:00 +020054 git notes --ref=release-candidate show "$@" 2>/dev/null || true
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000055}
56
57# Extract the release name from the git notes
58function get_release_name() {
dmartingfc6c3cb2017-07-28 00:01:00 +020059 git notes --ref=release show "$@" 2>/dev/null || true
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000060}
61
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +000062# Get the list of commit hashes between two revisions
63function git_log_hash() {
64 local baseline="$1"
65 local head="$2"
66 shift 2
67 git log --pretty=format:%H "${baseline}".."${head}" "$@"
68}
69
70# Extract the full release name from the git notes
71function get_full_release_name() {
72 local name="$(get_release_name "$@")"
73 local rc="$(get_release_candidate "$@")"
74 if [ -n "${rc}" ]; then
75 echo "${name}rc${rc}"
76 else
77 echo "${name}"
78 fi
79}
80
Damien Martin-Guillerez0edd3542016-11-22 15:16:38 +000081# Extract the release notes from the git notes
82function get_release_notes() {
83 git notes --ref=release-notes show "$@" 2>/dev/null || true
84}
85
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000086# Returns the info from the branch of the release. It is the current branch
87# but it errors out if the current branch is not a release branch. This
88# method returns the tag of the release and the number of the current
89# candidate in this release.
90function get_release_branch() {
91 local branch_name=$(git_get_branch)
Androbincfb2ec02017-06-27 13:47:43 +020092 if [ -z "$(get_release_name)" ] || [ -z "$(get_release_candidate)" ]; then
Damien Martin-Guillerezfa15d392015-07-28 15:54:40 +000093 echo "Not a release branch: ${branch_name}." >&2
94 exit 1
95 fi
96 echo "${branch_name}"
97}
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +000098
99# fmt behaves differently on *BSD and on GNU/Linux, use fold.
100function wrap_text() {
101 fold -s -w $1 | sed 's/ *$//'
102}
103
104# Create the revision information given a list of commits. The first
105# commit should be the baseline, and the other ones are the cherry-picks.
106# The result is of the form:
107# Baseline: BASELINE_COMMIT
108#
109# Cherry picks:
Klaus Aehligfaaff7f2018-09-13 02:46:38 -0700110#
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000111# + CHERRY_PICK1: commit message summary of the CHERRY_PICK1. This
112# message will be wrapped into 70 columns.
113# + CHERRY_PICK2: commit message summary of the CHERRY_PICK2.
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700114function __create_revision_information() {
115 echo "Baseline: $(__git_commit_hash "${1}")"
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000116 local first=1
117 shift
118 while [ -n "${1-}" ]; do
119 if [[ "$first" -eq 1 ]]; then
120 echo -e "\nCherry picks:"
Klaus Aehligfaaff7f2018-09-13 02:46:38 -0700121 echo
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000122 first=0
123 fi
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700124 local hash="$(__git_commit_hash "${1}")"
125 local subject="$(__git_commit_subject $hash)"
Damien Martin-Guillerez930fa9b2017-01-26 22:52:29 +0000126 local lines=$(echo "$subject" | wrap_text 65) # 5 leading spaces.
127 echo " + $hash:"
128 echo "$lines" | sed 's/^/ /'
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000129 shift
130 done
131}
132
133# Get the baseline of master.
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700134# Args: $1: release branch (or HEAD)
135# TODO(philwo) this gives the wrong baseline when HEAD == release == master.
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000136function get_release_baseline() {
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700137 git merge-base master "$1"
Damien Martin-Guillerez353ce472017-01-10 14:23:10 +0000138}
139
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000140# Get the list of cherry-picks since master
141# Args:
142# $1: branch, default to HEAD
143# $2: baseline change, default to $(get_release_baseline $1)
144function get_cherrypicks() {
145 local branch="${1:-HEAD}"
146 local baseline="${2:-$(get_release_baseline "${branch}")}"
147 # List of changes since the baseline on the release branch
148 local changes="$(git_log_hash "${baseline}" "${branch}" --reverse)"
149 # List of changes since the baseline on the master branch, and their patch-id
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700150 local master_changes="$(git_log_hash "${baseline}" master | xargs git show | git patch-id)"
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000151 # Now for each changes on the release branch
152 for i in ${changes}; do
Damien Martin-Guillerez353ce472017-01-10 14:23:10 +0000153 local hash=$(git notes --ref=cherrypick show "$i" 2>/dev/null || true)
154 if [ -z "${hash}" ]; then
155 # Find the change with the same patch-id on the master branch if the note is not present
156 hash=$(echo "${master_changes}" \
157 | grep "^$(git show "$i" | git patch-id | cut -d " " -f 1)" \
158 | cut -d " " -f 2)
159 fi
160 if [ -z "${hash}" ]; then
161 # We don't know which cherry-pick it is coming from, fall back to the new commit hash.
162 echo "$i"
163 else
164 echo "${hash}"
165 fi
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000166 done
167}
168
169# Generate the title of the release with the date from the release name ($1).
170function get_release_title() {
171 echo "Release ${1} ($(date +%Y-%m-%d))"
172}
173
174# Generate the release message to be added to the changelog
175# from the release notes for release $1
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000176# Args:
177# $1: release name
178# $2: release ref (default HEAD)
179# $3: delimiter around the revision information (default none)
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000180function generate_release_message() {
181 local release_name="$1"
182 local branch="${2:-HEAD}"
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000183 local delimiter="${3-}"
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000184 local baseline="$(get_release_baseline "${branch}")"
185 local cherrypicks="$(get_cherrypicks "${branch}" "${baseline}")"
186
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000187 get_release_title "$release_name"
188 echo
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000189
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000190 if [ -n "${delimiter}" ]; then
191 echo "${delimiter}"
192 fi
Philipp Wollermann5fabb432018-03-27 04:37:23 -0700193 __create_revision_information $baseline $cherrypicks
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000194 if [ -n "${delimiter}" ]; then
195 echo "${delimiter}"
196 fi
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000197
Damien Martin-Guillerez9f915542017-01-10 15:25:29 +0000198 echo
199 get_release_notes "${branch}"
Damien Martin-Guillerez0d352612016-12-02 13:48:14 +0000200}
201
202# Returns the release notes for the CHANGELOG.md taken from either from
203# the notes for a release candidate or from the commit message for a
204# full release.
205function get_full_release_notes() {
206 local release_name="$(get_full_release_name "$@")"
207
208 if [[ "${release_name}" =~ rc[0-9]+$ ]]; then
209 # Release candidate, we need to generate from the notes
210 generate_release_message "${release_name}" "$@"
211 else
212 # Full release, returns the commit message
213 git_commit_msg "$@"
214 fi
215}