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