cherry_pick_pull.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Usage Instructions: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-release/cherry-picks.md
  16. # Checkout a PR from GitHub. (Yes, this is sitting in a Git tree. How
  17. # meta.) Assumes you care about pulls from remote "upstream" and
  18. # checks them out to a branch named:
  19. # automated-cherry-pick-of-<pr>-<target branch>-<timestamp>
  20. set -o errexit
  21. set -o nounset
  22. set -o pipefail
  23. REPO_ROOT="$(git rev-parse --show-toplevel)"
  24. declare -r REPO_ROOT
  25. cd "${REPO_ROOT}"
  26. function git_remote_get_url() {
  27. git remote get-url $1 2>/dev/null
  28. if [ "$?" -ne "0" ]; then
  29. git config remote.$1.url 2>/dev/null
  30. fi
  31. if [ "$?" -ne "0" ]; then
  32. echo "git fail get remote url for $1"
  33. exit 1
  34. fi
  35. }
  36. STARTINGBRANCH=$(git symbolic-ref --short HEAD)
  37. declare -r STARTINGBRANCH
  38. declare -r REBASEMAGIC="${REPO_ROOT}/.git/rebase-apply"
  39. DRY_RUN=${DRY_RUN:-""}
  40. UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream}
  41. FORK_REMOTE=${FORK_REMOTE:-origin}
  42. MAIN_REPO_ORG=${MAIN_REPO_ORG:-$(git_remote_get_url "$UPSTREAM_REMOTE" | awk -F'github.com' '{print $2}' | awk -F'[:./]' 'NR==1{print $2}')}
  43. MAIN_REPO_NAME=${MAIN_REPO_NAME:-$(git_remote_get_url "$UPSTREAM_REMOTE" | awk -F'github.com' '{print $2}' | awk -F'[:./]' 'NR==1{print $3}')}
  44. if [[ -z ${GITHUB_USER:-} ]]; then
  45. echo "Please export GITHUB_USER=<your-user> (or GH organization, if that's where your fork lives)"
  46. exit 1
  47. fi
  48. if ! which hub > /dev/null; then
  49. echo "Can't find 'hub' tool in PATH, please install from https://github.com/github/hub"
  50. exit 1
  51. fi
  52. function version_lt() {
  53. test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"
  54. }
  55. if version_lt "$(hub version | awk '/hub/ {print $3}')" "2.13.0"; then
  56. echo "Please install 'hub' with version 2.13.0+ from https://github.com/github/hub/releases"
  57. exit 1
  58. fi
  59. if [[ "$#" -lt 2 ]]; then
  60. echo "${0} <remote branch> <pr-number>...: cherry pick one or more <pr> onto <remote branch> and leave instructions for proposing pull request"
  61. echo
  62. echo " Checks out <remote branch> and handles the cherry-pick of <pr> (possibly multiple) for you."
  63. echo " Examples:"
  64. echo " $0 upstream/release-3.14 12345 # Cherry-picks PR 12345 onto upstream/release-3.14 and proposes that as a PR."
  65. echo " $0 upstream/release-3.14 12345 56789 # Cherry-picks PR 12345, then 56789 and proposes the combination as a single PR."
  66. echo
  67. echo " Set the DRY_RUN environment var to skip git push and creating PR."
  68. echo " This is useful for creating patches to a release branch without making a PR."
  69. echo " When DRY_RUN is set the script will leave you in a branch containing the commits you cherry-picked."
  70. echo
  71. echo " Set UPSTREAM_REMOTE (default: upstream) and FORK_REMOTE (default: origin)"
  72. echo " To override the default remote names to what you have locally."
  73. exit 2
  74. fi
  75. if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then
  76. echo "!!! Dirty tree. Clean up and try again."
  77. exit 1
  78. fi
  79. if [[ -e "${REBASEMAGIC}" ]]; then
  80. echo "!!! 'git rebase' or 'git am' in progress. Clean up and try again."
  81. exit 1
  82. fi
  83. declare -r BRANCH="$1"
  84. shift 1
  85. declare -r PULLS=( "$@" )
  86. function join { local IFS="$1"; shift; echo "$*"; }
  87. declare -r PULLDASH=$(join - "${PULLS[@]/#/#}") # Generates something like "#12345-#56789"
  88. declare -r PULLSUBJ=$(join " " "${PULLS[@]/#/#}") # Generates something like "#12345 #56789"
  89. echo "+++ Updating remotes..."
  90. git remote update "${UPSTREAM_REMOTE}" "${FORK_REMOTE}"
  91. if ! git log -n1 --format=%H "${BRANCH}" >/dev/null 2>&1; then
  92. echo "!!! '${BRANCH}' not found. The second argument should be something like ${UPSTREAM_REMOTE}/release-0.21."
  93. echo " (In particular, it needs to be a valid, existing remote branch that I can 'git checkout'.)"
  94. exit 1
  95. fi
  96. declare -r NEWBRANCHREQ="automated-cherry-pick-of-${PULLDASH}" # "Required" portion for tools.
  97. declare -r NEWBRANCH="$(echo "${NEWBRANCHREQ}-${BRANCH}" | sed 's/\//-/g')"
  98. declare -r NEWBRANCHUNIQ="${NEWBRANCH}-$(date +%s)"
  99. echo "+++ Creating local branch ${NEWBRANCHUNIQ}"
  100. cleanbranch=""
  101. prtext=""
  102. gitamcleanup=false
  103. function return_to_kansas {
  104. if [[ "${gitamcleanup}" == "true" ]]; then
  105. echo
  106. echo "+++ Aborting in-progress git am."
  107. git am --abort >/dev/null 2>&1 || true
  108. fi
  109. # return to the starting branch and delete the PR text file
  110. if [[ -z "${DRY_RUN}" ]]; then
  111. echo
  112. echo "+++ Returning you to the ${STARTINGBRANCH} branch and cleaning up."
  113. git checkout -f "${STARTINGBRANCH}" >/dev/null 2>&1 || true
  114. if [[ -n "${cleanbranch}" ]]; then
  115. git branch -D "${cleanbranch}" >/dev/null 2>&1 || true
  116. fi
  117. if [[ -n "${prtext}" ]]; then
  118. rm "${prtext}"
  119. fi
  120. fi
  121. }
  122. trap return_to_kansas EXIT
  123. SUBJECTS=()
  124. function make-a-pr() {
  125. local rel="$(echo "${BRANCH}" | sed "s|$UPSTREAM_REMOTE/||g")"
  126. echo
  127. echo "+++ Creating a pull request on GitHub at ${GITHUB_USER}:${NEWBRANCH}"
  128. # This looks like an unnecessary use of a tmpfile, but it avoids
  129. # https://github.com/github/hub/issues/976 Otherwise stdin is stolen
  130. # when we shove the heredoc at hub directly, tickling the ioctl
  131. # crash.
  132. prtext="$(mktemp -t prtext.XXXX)" # cleaned in return_to_kansas
  133. local numandtitle=$(printf '%s\n' "${SUBJECTS[@]}")
  134. cat >"${prtext}" <<EOF
  135. Automated cherry pick of ${numandtitle}
  136. Cherry pick of ${PULLSUBJ} on ${rel}.
  137. ${numandtitle}
  138. EOF
  139. hub pull-request -F "${prtext}" -h "${GITHUB_USER}:${NEWBRANCH}" -b "${MAIN_REPO_ORG}:${rel}"
  140. }
  141. git checkout -b "${NEWBRANCHUNIQ}" "${BRANCH}"
  142. cleanbranch="${NEWBRANCHUNIQ}"
  143. gitamcleanup=true
  144. for pull in "${PULLS[@]}"; do
  145. echo "+++ Downloading patch to /tmp/${pull}.patch (in case you need to do this again)"
  146. echo "Downloading https://github.com/${MAIN_REPO_ORG}/${MAIN_REPO_NAME}/pull/${pull}.patch ..."
  147. curl -o "/tmp/${pull}.patch" -sSL "https://github.com/${MAIN_REPO_ORG}/${MAIN_REPO_NAME}/pull/${pull}.patch"
  148. echo
  149. echo "+++ About to attempt cherry pick of PR. To reattempt:"
  150. echo " $ git am -3 /tmp/${pull}.patch"
  151. echo
  152. git am -3 "/tmp/${pull}.patch" || {
  153. conflicts=false
  154. while unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] \
  155. || [[ -e "${REBASEMAGIC}" ]]; do
  156. conflicts=true # <-- We should have detected conflicts once
  157. echo
  158. echo "+++ Conflicts detected:"
  159. echo
  160. (git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?"
  161. echo
  162. echo "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')"
  163. read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
  164. echo
  165. if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
  166. echo "Aborting." >&2
  167. exit 1
  168. fi
  169. done
  170. if [[ "${conflicts}" != "true" ]]; then
  171. echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"
  172. exit 1
  173. fi
  174. }
  175. SUBJECTS+=("$(hub pr show -f "%i: %t" ${pull})")
  176. # remove the patch file from /tmp
  177. rm -f "/tmp/${pull}.patch"
  178. done
  179. gitamcleanup=false
  180. if [[ -n "${DRY_RUN}" ]]; then
  181. echo "!!! Skipping git push and PR creation because you set DRY_RUN."
  182. echo "To return to the branch you were in when you invoked this script:"
  183. echo
  184. echo " git checkout ${STARTINGBRANCH}"
  185. echo
  186. echo "To delete this branch:"
  187. echo
  188. echo " git branch -D ${NEWBRANCHUNIQ}"
  189. exit 0
  190. fi
  191. if git remote -v | grep ^${FORK_REMOTE} | grep ${MAIN_REPO_ORG}/${MAIN_REPO_NAME}.git; then
  192. echo "!!! You have ${FORK_REMOTE} configured as your ${MAIN_REPO_ORG}/${MAIN_REPO_NAME}.git"
  193. echo "This isn't normal. Leaving you with push instructions:"
  194. echo
  195. echo "+++ First manually push the branch this script created:"
  196. echo
  197. echo " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}"
  198. echo
  199. echo "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)."
  200. echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values."
  201. echo
  202. make-a-pr
  203. cleanbranch=""
  204. exit 0
  205. fi
  206. echo
  207. echo "+++ I'm about to do the following to push to GitHub (and I'm assuming ${FORK_REMOTE} is your personal fork):"
  208. echo
  209. echo " git push ${FORK_REMOTE} ${NEWBRANCHUNIQ}:${NEWBRANCH}"
  210. echo
  211. read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
  212. if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
  213. echo "Aborting." >&2
  214. exit 1
  215. fi
  216. git push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}"
  217. make-a-pr