Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 2b83611

Browse files
authored
Merge pull request #79 from JoshVanL/add-cherry-pick-file
Adds check-pick file to automate generating cherry pick PRs
2 parents cbe4643 + 80a8ffb commit 2b83611

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed

hack/cherry-pick-pull.sh

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

0 commit comments

Comments
 (0)