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 | |
| 47 | # Show the commit message of the ref specified in argument |
| 48 | function git_commit_msg() { |
| 49 | git show -s --pretty=format:%B "$@" |
| 50 | } |
| 51 | |
| 52 | # Extract the release candidate number from the git notes |
| 53 | function get_release_candidate() { |
dmarting | fc6c3cb | 2017-07-28 00:01:00 +0200 | [diff] [blame] | 54 | git notes --ref=release-candidate show "$@" 2>/dev/null || true |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | # Extract the release name from the git notes |
| 58 | function get_release_name() { |
dmarting | fc6c3cb | 2017-07-28 00:01:00 +0200 | [diff] [blame] | 59 | git notes --ref=release show "$@" 2>/dev/null || true |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 62 | # Get the list of commit hashes between two revisions |
| 63 | function 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 |
| 71 | function 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-Guillerez | 0edd354 | 2016-11-22 15:16:38 +0000 | [diff] [blame] | 81 | # Extract the release notes from the git notes |
| 82 | function get_release_notes() { |
| 83 | git notes --ref=release-notes show "$@" 2>/dev/null || true |
| 84 | } |
| 85 | |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 86 | # 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. |
| 90 | function get_release_branch() { |
| 91 | local branch_name=$(git_get_branch) |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 92 | if [ -z "$(get_release_name)" ] || [ -z "$(get_release_candidate)" ]; then |
Damien Martin-Guillerez | fa15d39 | 2015-07-28 15:54:40 +0000 | [diff] [blame] | 93 | echo "Not a release branch: ${branch_name}." >&2 |
| 94 | exit 1 |
| 95 | fi |
| 96 | echo "${branch_name}" |
| 97 | } |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 98 | |
| 99 | # fmt behaves differently on *BSD and on GNU/Linux, use fold. |
| 100 | function 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 Aehlig | faaff7f | 2018-09-13 02:46:38 -0700 | [diff] [blame] | 110 | # |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 111 | # + 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 Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 114 | function __create_revision_information() { |
| 115 | echo "Baseline: $(__git_commit_hash "${1}")" |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 116 | local first=1 |
| 117 | shift |
| 118 | while [ -n "${1-}" ]; do |
| 119 | if [[ "$first" -eq 1 ]]; then |
| 120 | echo -e "\nCherry picks:" |
Klaus Aehlig | faaff7f | 2018-09-13 02:46:38 -0700 | [diff] [blame] | 121 | echo |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 122 | first=0 |
| 123 | fi |
Philipp Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 124 | local hash="$(__git_commit_hash "${1}")" |
| 125 | local subject="$(__git_commit_subject $hash)" |
Damien Martin-Guillerez | 930fa9b | 2017-01-26 22:52:29 +0000 | [diff] [blame] | 126 | local lines=$(echo "$subject" | wrap_text 65) # 5 leading spaces. |
| 127 | echo " + $hash:" |
| 128 | echo "$lines" | sed 's/^/ /' |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 129 | shift |
| 130 | done |
| 131 | } |
| 132 | |
| 133 | # Get the baseline of master. |
Philipp Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 134 | # Args: $1: release branch (or HEAD) |
| 135 | # TODO(philwo) this gives the wrong baseline when HEAD == release == master. |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 136 | function get_release_baseline() { |
Philipp Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 137 | git merge-base master "$1" |
Damien Martin-Guillerez | 353ce47 | 2017-01-10 14:23:10 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 140 | # 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) |
| 144 | function 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 Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 150 | local master_changes="$(git_log_hash "${baseline}" master | xargs git show | git patch-id)" |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 151 | # Now for each changes on the release branch |
| 152 | for i in ${changes}; do |
Damien Martin-Guillerez | 353ce47 | 2017-01-10 14:23:10 +0000 | [diff] [blame] | 153 | 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-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 166 | done |
| 167 | } |
| 168 | |
| 169 | # Generate the title of the release with the date from the release name ($1). |
| 170 | function 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-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 176 | # Args: |
| 177 | # $1: release name |
| 178 | # $2: release ref (default HEAD) |
| 179 | # $3: delimiter around the revision information (default none) |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 180 | function generate_release_message() { |
| 181 | local release_name="$1" |
| 182 | local branch="${2:-HEAD}" |
Damien Martin-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 183 | local delimiter="${3-}" |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 184 | local baseline="$(get_release_baseline "${branch}")" |
| 185 | local cherrypicks="$(get_cherrypicks "${branch}" "${baseline}")" |
| 186 | |
Damien Martin-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 187 | get_release_title "$release_name" |
| 188 | echo |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 189 | |
Damien Martin-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 190 | if [ -n "${delimiter}" ]; then |
| 191 | echo "${delimiter}" |
| 192 | fi |
Philipp Wollermann | 5fabb43 | 2018-03-27 04:37:23 -0700 | [diff] [blame] | 193 | __create_revision_information $baseline $cherrypicks |
Damien Martin-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 194 | if [ -n "${delimiter}" ]; then |
| 195 | echo "${delimiter}" |
| 196 | fi |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 197 | |
Damien Martin-Guillerez | 9f91554 | 2017-01-10 15:25:29 +0000 | [diff] [blame] | 198 | echo |
| 199 | get_release_notes "${branch}" |
Damien Martin-Guillerez | 0d35261 | 2016-12-02 13:48:14 +0000 | [diff] [blame] | 200 | } |
| 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. |
| 205 | function 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 | } |