cherry_pick_pull.sh 9.1 KB

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