Skip to content

Commit 5e7cdca

Browse files
authored
Merge pull request #186 from mbohlool/cherry
Port Kubernetes cherry-pick script
2 parents ff83fb6 + 9e738e4 commit 5e7cdca

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

scripts/cherry_pick_pull.sh

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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 REPO_ROOT="$(git rev-parse --show-toplevel)"
27+
cd "${REPO_ROOT}"
28+
29+
declare -r STARTINGBRANCH=$(git symbolic-ref --short HEAD)
30+
declare -r REBASEMAGIC="${REPO_ROOT}/.git/rebase-apply"
31+
DRY_RUN=${DRY_RUN:-""}
32+
UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream}
33+
FORK_REMOTE=${FORK_REMOTE:-origin}
34+
MAIN_REPO_NAME=${MAIN_REPO_NAME:-"client-python"}
35+
MAIN_REPO_ORG=${MAIN_REPO_ORG:-"kubernetes-incubator"}
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 "${MAIN_REPO_ORG}:${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+
150+
curl -o "/tmp/${pull}.patch" -sSL "https://github.com/${MAIN_REPO_ORG}/${MAIN_REPO_NAME}/pull/${pull}.patch"
151+
echo
152+
echo "+++ About to attempt cherry pick of PR. To reattempt:"
153+
echo " $ git am -3 /tmp/${pull}.patch"
154+
echo
155+
git am -3 "/tmp/${pull}.patch" || {
156+
conflicts=false
157+
while unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] \
158+
|| [[ -e "${REBASEMAGIC}" ]]; do
159+
conflicts=true # <-- We should have detected conflicts once
160+
echo
161+
echo "+++ Conflicts detected:"
162+
echo
163+
(git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?"
164+
echo
165+
echo "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')"
166+
read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
167+
echo
168+
if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
169+
echo "Aborting." >&2
170+
exit 1
171+
fi
172+
done
173+
174+
if [[ "${conflicts}" != "true" ]]; then
175+
echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"
176+
exit 1
177+
fi
178+
}
179+
180+
# set the subject
181+
subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //')
182+
SUBJECTS+=("#${pull}: ${subject}")
183+
184+
# remove the patch file from /tmp
185+
rm -f "/tmp/${pull}.patch"
186+
done
187+
gitamcleanup=false
188+
189+
if [[ -n "${DRY_RUN}" ]]; then
190+
echo "!!! Skipping git push and PR creation because you set DRY_RUN."
191+
echo "To return to the branch you were in when you invoked this script:"
192+
echo
193+
echo " git checkout ${STARTINGBRANCH}"
194+
echo
195+
echo "To delete this branch:"
196+
echo
197+
echo " git branch -D ${NEWBRANCHUNIQ}"
198+
exit 0
199+
fi
200+
201+
if git remote -v | grep ^${FORK_REMOTE} | grep {$MAIN_REPO_ORG}/{$MAIN_REPO_NAME}.git; then
202+
echo "!!! You have ${FORK_REMOTE} configured as your {$MAIN_REPO_ORG}/{$MAIN_REPO_NAME}.git"
203+
echo "This isn't normal. Leaving you with push instructions:"
204+
echo
205+
echo "+++ First manually push the branch this script created:"
206+
echo
207+
echo " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}"
208+
echo
209+
echo "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)."
210+
echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values."
211+
echo
212+
make-a-pr
213+
cleanbranch=""
214+
exit 0
215+
fi
216+
217+
echo
218+
echo "+++ I'm about to do the following to push to GitHub (and I'm assuming ${FORK_REMOTE} is your personal fork):"
219+
echo
220+
echo " git push ${FORK_REMOTE} ${NEWBRANCHUNIQ}:${NEWBRANCH}"
221+
echo
222+
read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
223+
if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
224+
echo "Aborting." >&2
225+
exit 1
226+
fi
227+
228+
git push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}"
229+
make-a-pr

0 commit comments

Comments
 (0)