Skip to content

Commit 7408d90

Browse files
committed
Copy original cherry pick script from kubernetes main repo
1 parent 7191e0e commit 7408d90

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

scripts/cherry_pick_pull.sh

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#!/bin/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+
UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream}
33+
FORK_REMOTE=${FORK_REMOTE:-origin}
34+
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+
40+
if ! which hub > /dev/null; then
41+
echo "Can't find 'hub' tool in PATH, please install from https://github.com/github/hub"
42+
exit 1
43+
fi
44+
45+
if [[ "$#" -lt 2 ]]; then
46+
echo "${0} <remote branch> <pr-number>...: cherry pick one or more <pr> onto <remote branch> and leave instructions for proposing pull request"
47+
echo
48+
echo " Checks out <remote branch> and handles the cherry-pick of <pr> (possibly multiple) for you."
49+
echo " Examples:"
50+
echo " $0 upstream/release-3.14 12345 # Cherry-picks PR 12345 onto upstream/release-3.14 and proposes that as a PR."
51+
echo " $0 upstream/release-3.14 12345 56789 # Cherry-picks PR 12345, then 56789 and proposes the combination as a single PR."
52+
echo
53+
echo " Set the DRY_RUN environment var to skip git push and creating PR."
54+
echo " This is useful for creating patches to a release branch without making a PR."
55+
echo " When DRY_RUN is set the script will leave you in a branch containing the commits you cherry-picked."
56+
echo
57+
echo " Set UPSTREAM_REMOTE (default: upstream) and FORK_REMOTE (default: origin)"
58+
echo " To override the default remote names to what you have locally."
59+
exit 2
60+
fi
61+
62+
if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then
63+
echo "!!! Dirty tree. Clean up and try again."
64+
exit 1
65+
fi
66+
67+
if [[ -e "${REBASEMAGIC}" ]]; then
68+
echo "!!! 'git rebase' or 'git am' in progress. Clean up and try again."
69+
exit 1
70+
fi
71+
72+
declare -r BRANCH="$1"
73+
shift 1
74+
declare -r PULLS=( "$@" )
75+
76+
function join { local IFS="$1"; shift; echo "$*"; }
77+
declare -r PULLDASH=$(join - "${PULLS[@]/#/#}") # Generates something like "#12345-#56789"
78+
declare -r PULLSUBJ=$(join " " "${PULLS[@]/#/#}") # Generates something like "#12345 #56789"
79+
80+
echo "+++ Updating remotes..."
81+
git remote update "${UPSTREAM_REMOTE}" "${FORK_REMOTE}"
82+
83+
if ! git log -n1 --format=%H "${BRANCH}" >/dev/null 2>&1; then
84+
echo "!!! '${BRANCH}' not found. The second argument should be something like ${UPSTREAM_REMOTE}/release-0.21."
85+
echo " (In particular, it needs to be a valid, existing remote branch that I can 'git checkout'.)"
86+
exit 1
87+
fi
88+
89+
declare -r NEWBRANCHREQ="automated-cherry-pick-of-${PULLDASH}" # "Required" portion for tools.
90+
declare -r NEWBRANCH="$(echo "${NEWBRANCHREQ}-${BRANCH}" | sed 's/\//-/g')"
91+
declare -r NEWBRANCHUNIQ="${NEWBRANCH}-$(date +%s)"
92+
echo "+++ Creating local branch ${NEWBRANCHUNIQ}"
93+
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+
104+
# return to the starting branch and delete the PR text file
105+
if [[ -z "${DRY_RUN}" ]]; then
106+
echo
107+
echo "+++ Returning you to the ${STARTINGBRANCH} branch and cleaning up."
108+
git checkout -f "${STARTINGBRANCH}" >/dev/null 2>&1 || true
109+
if [[ -n "${cleanbranch}" ]]; then
110+
git branch -D "${cleanbranch}" >/dev/null 2>&1 || true
111+
fi
112+
if [[ -n "${prtext}" ]]; then
113+
rm "${prtext}"
114+
fi
115+
fi
116+
}
117+
trap return_to_kansas EXIT
118+
119+
SUBJECTS=()
120+
function make-a-pr() {
121+
local rel="$(basename "${BRANCH}")"
122+
echo
123+
echo "+++ Creating a pull request on GitHub at ${GITHUB_USER}:${NEWBRANCH}"
124+
125+
# This looks like an unnecessary use of a tmpfile, but it avoids
126+
# https://github.com/github/hub/issues/976 Otherwise stdin is stolen
127+
# when we shove the heredoc at hub directly, tickling the ioctl
128+
# crash.
129+
prtext="$(mktemp -t prtext.XXXX)" # cleaned in return_to_kansas
130+
cat >"${prtext}" <<EOF
131+
Automated cherry pick of ${PULLSUBJ}
132+
133+
Cherry pick of ${PULLSUBJ} on ${rel}.
134+
135+
$(printf '%s\n' "${SUBJECTS[@]}")
136+
EOF
137+
138+
hub pull-request -F "${prtext}" -h "${GITHUB_USER}:${NEWBRANCH}" -b "kubernetes:${rel}"
139+
}
140+
141+
git checkout -b "${NEWBRANCHUNIQ}" "${BRANCH}"
142+
cleanbranch="${NEWBRANCHUNIQ}"
143+
144+
gitamcleanup=true
145+
for pull in "${PULLS[@]}"; do
146+
echo "+++ Downloading patch to /tmp/${pull}.patch (in case you need to do this again)"
147+
curl -o "/tmp/${pull}.patch" -sSL "http://pr.k8s.io/${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+
171+
if [[ "${conflicts}" != "true" ]]; then
172+
echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"
173+
exit 1
174+
fi
175+
}
176+
177+
# set the subject
178+
subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //')
179+
SUBJECTS+=("#${pull}: ${subject}")
180+
181+
# remove the patch file from /tmp
182+
rm -f "/tmp/${pull}.patch"
183+
done
184+
gitamcleanup=false
185+
186+
if [[ -n "${DRY_RUN}" ]]; then
187+
echo "!!! Skipping git push and PR creation because you set DRY_RUN."
188+
echo "To return to the branch you were in when you invoked this script:"
189+
echo
190+
echo " git checkout ${STARTINGBRANCH}"
191+
echo
192+
echo "To delete this branch:"
193+
echo
194+
echo " git branch -D ${NEWBRANCHUNIQ}"
195+
exit 0
196+
fi
197+
198+
if git remote -v | grep ^${FORK_REMOTE} | grep kubernetes/kubernetes.git; then
199+
echo "!!! You have ${FORK_REMOTE} configured as your kubernetes/kubernetes.git"
200+
echo "This isn't normal. Leaving you with push instructions:"
201+
echo
202+
echo "+++ First manually push the branch this script created:"
203+
echo
204+
echo " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}"
205+
echo
206+
echo "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)."
207+
echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values."
208+
echo
209+
make-a-pr
210+
cleanbranch=""
211+
exit 0
212+
fi
213+
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+
225+
git push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}"
226+
make-a-pr

0 commit comments

Comments
 (0)