Home | History | Annotate | Download | only in curl
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2016 The Android Open-Source Project
      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 #
     17 
     18 set -e -u
     19 
     20 base_dir=$(realpath $(dirname $0))
     21 cd "${base_dir}"
     22 
     23 UPSTREAM_GIT="https://github.com/curl/curl"
     24 UPSTREAM="github_curl"
     25 README_FILE="README.version"
     26 
     27 TEMP_FILES=()
     28 cleanup() {
     29   trap - INT TERM ERR EXIT
     30   if [[ ${#TEMP_FILES[@]} -ne 0 ]]; then
     31     rm -f "${TEMP_FILES[@]}"
     32   fi
     33 }
     34 trap cleanup INT TERM ERR EXIT
     35 
     36 # Prints the URL to the package for the passed version.
     37 upstream_url() {
     38   local version="$1"
     39   echo "https://curl.haxx.se/download/curl-${version}.tar.gz"
     40 }
     41 
     42 # Update the contents of the README.version with the new version string passed
     43 # as the first parameter.
     44 update_readme() {
     45   local version="$1"
     46   local sha="$2"
     47   local tmp_readme=$(mktemp update_readme.XXXXXX)
     48   TEMP_FILES+=("${tmp_readme}")
     49   local url
     50   url=$(upstream_url "${version}")
     51 
     52   cat >"${tmp_readme}" <<EOF
     53 URL: ${url}
     54 Version: ${version}
     55 Upstream commit: ${sha}
     56 EOF
     57   grep -v -E '^(URL|Version|Upstream commit):' "${README_FILE}" \
     58     >>"${tmp_readme}" 2>/dev/null || true
     59   cp "${tmp_readme}" "${README_FILE}"
     60 }
     61 
     62 
     63 # Print the current branch name.
     64 git_current_branch() {
     65   git rev-parse --abbrev-ref HEAD
     66 }
     67 
     68 # Setup and fetch the upstream remote. While we have mirroring setup of the
     69 # remote, we need to fetch all the tags from upstream to identify the latest
     70 # release.
     71 setup_git() {
     72   local current_branch
     73   current_branch=$(git_current_branch)
     74   if [[ "${current_branch}" == "HEAD" ]]; then
     75     echo "Not working on a branch, please run 'repo start [branch_name] .'" \
     76       " first." >&2
     77     exit 1
     78   fi
     79 
     80   # Setup and fetch the remote.
     81   if ! git remote show | grep "^${UPSTREAM}\$" >/dev/null; then
     82     echo "Adding remote ${UPSTREAM} to ${UPSTREAM_GIT}"
     83     git remote add -t master "${UPSTREAM}" "${UPSTREAM_GIT}"
     84   fi
     85 
     86   TRACKING_BRANCH=$(git rev-parse --abbrev-ref --symbolic-full-name @{u})
     87   OUR_REMOTE="${TRACKING_BRANCH%/*}"
     88 
     89   echo "Fetching latest upstream code..."
     90   git fetch --quiet "${UPSTREAM}" master
     91 }
     92 
     93 
     94 main() {
     95   setup_git
     96 
     97   # Load the current version's upstream hash.
     98   local current_sha current_version
     99   current_version=$(grep '^Version: ' "${README_FILE}" | cut -d ' ' -f 2-)
    100   current_sha=$(grep '^Upstream commit: ' "${README_FILE}" | cut -d ' ' -f 3)
    101 
    102   if [[ -z "${current_sha}" ]]; then
    103     echo "Couldn't determine the upstream commit the current code is at." \
    104       "Please update ${README_FILE} to include it." >&2
    105     exit 1
    106   fi
    107 
    108   local target_sha target_versio
    109   target_sha="${1:-}"
    110   if [[ -z "${target_sha}" ]]; then
    111     cat >&2 <<EOF
    112 Usage: $0 [target_sha]
    113 
    114 Pass the upstream commit SHA of the release you want to update to.
    115 Candidate values are:
    116 EOF
    117     # Find the list of potential target_version values.
    118     git --no-pager log "${UPSTREAM}/master" --not "${current_sha}" --oneline \
    119       --grep=RELEASE-NOTES --grep=THANKS -- RELEASE-NOTES docs/THANKS
    120     exit 1
    121   fi
    122 
    123   # Expand the hash to the full value:
    124   target_sha=$(git rev-list -n 1 "${target_sha}")
    125   target_version=$(git show ${target_sha}:include/curl/curlver.h | \
    126     grep '^#define LIBCURL_VERSION ')
    127   target_version=$(echo "${target_version}" | cut -f 2 -d '"')
    128   target_version="${target_version%-DEV}"
    129 
    130   # Sanity check that the passed hash is forward in the chain.
    131   if [[ $(git log --oneline "${target_sha}" --not "${current_sha}" | wc -l) \
    132       == 0 ]]; then
    133     echo "The target SHA (${target_sha}) is not forward from ${current_sha}.">&2
    134     exit 1
    135   fi
    136 
    137   echo "Current version: ${current_version} / ${current_sha}"
    138   echo "Target version:  ${target_version} / ${target_sha}"
    139   echo
    140 
    141   # Generate the log message.
    142   local log_message=$(mktemp log_message.XXXXXX)
    143   TEMP_FILES+=("${log_message}")
    144 
    145   (cat <<EOF
    146 Update libcurl from ${current_version} to ${target_version}.
    147 
    148 Bug: COMPLETE
    149 Test: COMPLETE
    150 
    151 Note: This patch includes the following squashed commits from upstream:
    152 
    153 EOF
    154    git --no-pager log "${target_sha}" \
    155      --not "${current_sha}")>"${log_message}"
    156 
    157   # Land the changes and commit.
    158   update_readme "${target_version}" "${target_sha}"
    159   git add "README.version"
    160 
    161   git cherry-pick --no-commit "${target_sha}" --not "${current_sha}"
    162   git commit --file="${log_message}"
    163 
    164   cat <<EOF
    165 
    166   Run:
    167     git commit --amend
    168 
    169   edit the message to add the bug number and test it.
    170 
    171 EOF
    172 }
    173 
    174 main "$@"
    175