volcano.sh/volcano@v1.9.0/hack/cherry_pick_pull.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2015 The Kubernetes Authors. 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 # Checkout a PR from GitHub. (Yes, this is sitting in a Git tree. How 18 # meta.) Assumes you care about pulls from remote "upstream" and 19 # checks thems out to a branch named: 20 # automated-cherry-pick-of-<pr>-<target branch>-<timestamp> 21 22 set -o errexit 23 set -o nounset 24 set -o pipefail 25 26 declare -r KUBE_ROOT="$(dirname "${BASH_SOURCE}")/.." 27 cd "${KUBE_ROOT}" 28 29 declare -r STARTINGBRANCH=$(git symbolic-ref --short HEAD) 30 declare -r REBASEMAGIC="${KUBE_ROOT}/.git/rebase-apply" 31 DRY_RUN=${DRY_RUN:-""} 32 REGENERATE_DOCS=${REGENERATE_DOCS:-""} 33 UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream} 34 FORK_REMOTE=${FORK_REMOTE:-origin} 35 36 if [[ -z ${GITHUB_USER:-} ]]; then 37 echo "Please export GITHUB_USER=<your-user> (or GH organization, if that's where your fork lives)" 38 exit 1 39 fi 40 41 if ! which hub > /dev/null; then 42 echo "Can't find 'hub' tool in PATH, please install from https://github.com/github/hub" 43 exit 1 44 fi 45 46 if [[ "$#" -lt 2 ]]; then 47 echo "${0} <remote branch> <pr-number>...: cherry pick one or more <pr> onto <remote branch> and leave instructions for proposing pull request" 48 echo 49 echo " Checks out <remote branch> and handles the cherry-pick of <pr> (possibly multiple) for you." 50 echo " Examples:" 51 echo " $0 upstream/release-3.14 12345 # Cherry-picks PR 12345 onto upstream/release-3.14 and proposes that as a PR." 52 echo " $0 upstream/release-3.14 12345 56789 # Cherry-picks PR 12345, then 56789 and proposes the combination as a single PR." 53 echo 54 echo " Set the DRY_RUN environment var to skip git push and creating PR." 55 echo " This is useful for creating patches to a release branch without making a PR." 56 echo " When DRY_RUN is set the script will leave you in a branch containing the commits you cherry-picked." 57 echo 58 echo " Set the REGENERATE_DOCS environment var to regenerate documentation for the target branch after picking the specified commits." 59 echo " This is useful when picking commits containing changes to API documentation." 60 echo 61 echo " Set UPSTREAM_REMOTE (default: upstream) and FORK_REMOTE (default: origin)" 62 echo " To override the default remote names to what you have locally." 63 exit 2 64 fi 65 66 if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then 67 echo "!!! Dirty tree. Clean up and try again." 68 exit 1 69 fi 70 71 if [[ -e "${REBASEMAGIC}" ]]; then 72 echo "!!! 'git rebase' or 'git am' in progress. Clean up and try again." 73 exit 1 74 fi 75 76 declare -r BRANCH="$1" 77 shift 1 78 declare -r PULLS=( "$@" ) 79 80 function join { local IFS="$1"; shift; echo "$*"; } 81 declare -r PULLDASH=$(join - "${PULLS[@]/#/#}") # Generates something like "#12345-#56789" 82 declare -r PULLSUBJ=$(join " " "${PULLS[@]/#/#}") # Generates something like "#12345 #56789" 83 84 echo "+++ Updating remotes..." 85 git remote update "${UPSTREAM_REMOTE}" "${FORK_REMOTE}" 86 87 if ! git log -n1 --format=%H "${BRANCH}" >/dev/null 2>&1; then 88 echo "!!! '${BRANCH}' not found. The second argument should be something like ${UPSTREAM_REMOTE}/release-0.21." 89 echo " (In particular, it needs to be a valid, existing remote branch that I can 'git checkout'.)" 90 exit 1 91 fi 92 93 declare -r NEWBRANCHREQ="automated-cherry-pick-of-${PULLDASH}" # "Required" portion for tools. 94 declare -r NEWBRANCH="$(echo "${NEWBRANCHREQ}-${BRANCH}" | sed 's/\//-/g')" 95 declare -r NEWBRANCHUNIQ="${NEWBRANCH}-$(date +%s)" 96 echo "+++ Creating local branch ${NEWBRANCHUNIQ}" 97 98 cleanbranch="" 99 prtext="" 100 gitamcleanup=false 101 function return_to_kansas { 102 if [[ "${gitamcleanup}" == "true" ]]; then 103 echo 104 echo "+++ Aborting in-progress git am." 105 git am --abort >/dev/null 2>&1 || true 106 fi 107 108 # return to the starting branch and delete the PR text file 109 if [[ -z "${DRY_RUN}" ]]; then 110 echo 111 echo "+++ Returning you to the ${STARTINGBRANCH} branch and cleaning up." 112 git checkout -f "${STARTINGBRANCH}" >/dev/null 2>&1 || true 113 if [[ -n "${cleanbranch}" ]]; then 114 git branch -D "${cleanbranch}" >/dev/null 2>&1 || true 115 fi 116 if [[ -n "${prtext}" ]]; then 117 rm "${prtext}" 118 fi 119 fi 120 } 121 trap return_to_kansas EXIT 122 123 SUBJECTS=() 124 function make-a-pr() { 125 local rel="$(basename "${BRANCH}")" 126 echo 127 echo "+++ Creating a pull request on GitHub at ${GITHUB_USER}:${NEWBRANCH}" 128 129 # This looks like an unnecessary use of a tmpfile, but it avoids 130 # https://github.com/github/hub/issues/976 Otherwise stdin is stolen 131 # when we shove the heredoc at hub directly, tickling the ioctl 132 # crash. 133 prtext="$(mktemp -t prtext.XXXX)" # cleaned in return_to_kansas 134 local numandtitle=$(printf '%s\n' "${SUBJECTS[@]}") 135 cat >"${prtext}" <<EOF 136 Automated cherry pick of ${numandtitle} 137 138 Cherry pick of ${PULLSUBJ} on ${rel}. 139 140 ${numandtitle} 141 EOF 142 143 hub pull-request -F "${prtext}" -h "${GITHUB_USER}:${NEWBRANCH}" -b "volcano-sh/volcano:${rel}" 144 } 145 146 git checkout -b "${NEWBRANCHUNIQ}" "${BRANCH}" 147 cleanbranch="${NEWBRANCHUNIQ}" 148 149 gitamcleanup=true 150 for pull in "${PULLS[@]}"; do 151 echo "+++ Downloading patch to /tmp/${pull}.patch (in case you need to do this again)" 152 curl -o "/tmp/${pull}.patch" -sSL "https://github.com/volcano-sh/volcano/pull/${pull}.patch" 153 echo 154 echo "+++ About to attempt cherry pick of PR. To reattempt:" 155 echo " $ git am -3 /tmp/${pull}.patch" 156 echo 157 git am -3 "/tmp/${pull}.patch" || { 158 conflicts=false 159 while unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] \ 160 || [[ -e "${REBASEMAGIC}" ]]; do 161 conflicts=true # <-- We should have detected conflicts once 162 echo 163 echo "+++ Conflicts detected:" 164 echo 165 (git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?" 166 echo 167 echo "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')" 168 read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r 169 echo 170 if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then 171 echo "Aborting." >&2 172 exit 1 173 fi 174 done 175 176 if [[ "${conflicts}" != "true" ]]; then 177 echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'" 178 exit 1 179 fi 180 } 181 182 # set the subject 183 subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //') 184 SUBJECTS+=("#${pull}: ${subject}") 185 186 # remove the patch file from /tmp 187 rm -f "/tmp/${pull}.patch" 188 done 189 gitamcleanup=false 190 191 # Re-generate docs (if needed) 192 if [[ -n "${REGENERATE_DOCS}" ]]; then 193 echo 194 echo "Regenerating docs..." 195 if ! hack/generate-docs.sh; then 196 echo 197 echo "hack/generate-docs.sh FAILED to complete." 198 exit 1 199 fi 200 fi 201 202 if [[ -n "${DRY_RUN}" ]]; then 203 echo "!!! Skipping git push and PR creation because you set DRY_RUN." 204 echo "To return to the branch you were in when you invoked this script:" 205 echo 206 echo " git checkout ${STARTINGBRANCH}" 207 echo 208 echo "To delete this branch:" 209 echo 210 echo " git branch -D ${NEWBRANCHUNIQ}" 211 exit 0 212 fi 213 214 if git remote -v | grep ^${FORK_REMOTE} | grep volcano-sh/volcano.git; then 215 echo "!!! You have ${FORK_REMOTE} configured as your volcano-sh/volcano.git" 216 echo "This isn't normal. Leaving you with push instructions:" 217 echo 218 echo "+++ First manually push the branch this script created:" 219 echo 220 echo " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}" 221 echo 222 echo "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)." 223 echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values." 224 echo 225 make-a-pr 226 cleanbranch="" 227 exit 0 228 fi 229 230 echo 231 echo "+++ I'm about to do the following to push to GitHub (and I'm assuming ${FORK_REMOTE} is your personal fork):" 232 echo 233 echo " git push ${FORK_REMOTE} ${NEWBRANCHUNIQ}:${NEWBRANCH}" 234 echo 235 read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r 236 if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then 237 echo "Aborting." >&2 238 exit 1 239 fi 240 241 git push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}" 242 make-a-pr