#!/bin/bash# Copyright 2015 The Kubernetes Authors.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# Checkout a PR from GitHub. (Yes, this is sitting in a Git tree. How# meta.) Assumes you care about pulls from remote "upstream" and# checks them out to a branch named:# automated-cherry-pick-of-<pr>-<target branch>-<timestamp>set -o errexitset -o nounsetset -o pipefaildeclare -r REPO_ROOT="$(git rev-parse --show-toplevel)"cd "${REPO_ROOT}"declare -r STARTINGBRANCH=$(git symbolic-ref --short HEAD)declare -r REBASEMAGIC="${REPO_ROOT}/.git/rebase-apply"DRY_RUN=${DRY_RUN:-""}UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream}FORK_REMOTE=${FORK_REMOTE:-origin}MAIN_REPO_NAME=${MAIN_REPO_NAME:-"python"}MAIN_REPO_ORG=${MAIN_REPO_ORG:-"kubernetes-client"}if [[ -z ${GITHUB_USER:-} ]]; thenecho "Please export GITHUB_USER=<your-user> (or GH organization, if that's where your fork lives)"exit 1fiif ! which hub > /dev/null; thenecho "Can't find 'hub' tool in PATH, please install from https://github.com/github/hub"exit 1fiif [[ "$#" -lt 2 ]]; thenecho "${0} <remote branch> <pr-number>...: cherry pick one or more <pr> onto <remote branch> and leave instructions for proposing pull request"echoecho " Checks out <remote branch> and handles the cherry-pick of <pr> (possibly multiple) for you."echo " Examples:"echo " 0ドル upstream/release-3.14 12345 # Cherry-picks PR 12345 onto upstream/release-3.14 and proposes that as a PR."echo " 0ドル upstream/release-3.14 12345 56789 # Cherry-picks PR 12345, then 56789 and proposes the combination as a single PR."echoecho " Set the DRY_RUN environment var to skip git push and creating PR."echo " This is useful for creating patches to a release branch without making a PR."echo " When DRY_RUN is set the script will leave you in a branch containing the commits you cherry-picked."echoecho " Set UPSTREAM_REMOTE (default: upstream) and FORK_REMOTE (default: origin)"echo " To override the default remote names to what you have locally."exit 2fiif git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; thenecho "!!! Dirty tree. Clean up and try again."exit 1fiif [[ -e "${REBASEMAGIC}" ]]; thenecho "!!! 'git rebase' or 'git am' in progress. Clean up and try again."exit 1fideclare -r BRANCH="1ドル"shift 1declare -r PULLS=( "$@" )function join { local IFS="1ドル"; shift; echo "$*"; }declare -r PULLDASH=$(join - "${PULLS[@]/#/#}") # Generates something like "#12345-#56789"declare -r PULLSUBJ=$(join " " "${PULLS[@]/#/#}") # Generates something like "#12345 #56789"echo "+++ Updating remotes..."git remote update "${UPSTREAM_REMOTE}" "${FORK_REMOTE}"if ! git log -n1 --format=%H "${BRANCH}" >/dev/null 2>&1; thenecho "!!! '${BRANCH}' not found. The second argument should be something like ${UPSTREAM_REMOTE}/release-0.21."echo " (In particular, it needs to be a valid, existing remote branch that I can 'git checkout'.)"exit 1fideclare -r NEWBRANCHREQ="automated-cherry-pick-of-${PULLDASH}" # "Required" portion for tools.declare -r NEWBRANCH="$(echo "${NEWBRANCHREQ}-${BRANCH}" | sed 's/\//-/g')"declare -r NEWBRANCHUNIQ="${NEWBRANCH}-$(date +%s)"echo "+++ Creating local branch ${NEWBRANCHUNIQ}"cleanbranch=""prtext=""gitamcleanup=falsefunction return_to_kansas {if [[ "${gitamcleanup}" == "true" ]]; thenechoecho "+++ Aborting in-progress git am."git am --abort >/dev/null 2>&1 || truefi# return to the starting branch and delete the PR text fileif [[ -z "${DRY_RUN}" ]]; thenechoecho "+++ Returning you to the ${STARTINGBRANCH} branch and cleaning up."git checkout -f "${STARTINGBRANCH}" >/dev/null 2>&1 || trueif [[ -n "${cleanbranch}" ]]; thengit branch -D "${cleanbranch}" >/dev/null 2>&1 || truefiif [[ -n "${prtext}" ]]; thenrm "${prtext}"fifi}trap return_to_kansas EXITSUBJECTS=()function make-a-pr() {local rel="$(basename "${BRANCH}")"echoecho "+++ Creating a pull request on GitHub at ${GITHUB_USER}:${NEWBRANCH}"# This looks like an unnecessary use of a tmpfile, but it avoids# https://github.com/github/hub/issues/976 Otherwise stdin is stolen# when we shove the heredoc at hub directly, tickling the ioctl# crash.prtext="$(mktemp -t prtext.XXXX)" # cleaned in return_to_kansascat >"${prtext}" <<EOFAutomated cherry pick of ${PULLSUBJ}Cherry pick of ${PULLSUBJ} on ${rel}.$(printf '%s\n' "${SUBJECTS[@]}")EOFhub pull-request -F "${prtext}" -h "${GITHUB_USER}:${NEWBRANCH}" -b "${MAIN_REPO_ORG}:${rel}"}git checkout -b "${NEWBRANCHUNIQ}" "${BRANCH}"cleanbranch="${NEWBRANCHUNIQ}"gitamcleanup=truefor pull in "${PULLS[@]}"; doecho "+++ Downloading patch to /tmp/${pull}.patch (in case you need to do this again)"curl -o "/tmp/${pull}.patch" -sSL "https://github.com/${MAIN_REPO_ORG}/${MAIN_REPO_NAME}/pull/${pull}.patch"echoecho "+++ About to attempt cherry pick of PR. To reattempt:"echo " $ git am -3 /tmp/${pull}.patch"echogit am -3 "/tmp/${pull}.patch" || {conflicts=falsewhile unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] \|| [[ -e "${REBASEMAGIC}" ]]; doconflicts=true # <-- We should have detected conflicts onceechoecho "+++ Conflicts detected:"echo(git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?"echoecho "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')"read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -rechoif ! [[ "${REPLY}" =~ ^[yY]$ ]]; thenecho "Aborting." >&2exit 1fidoneif [[ "${conflicts}" != "true" ]]; thenecho "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"exit 1fi}# set the subjectsubject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //')SUBJECTS+=("#${pull}: ${subject}")# remove the patch file from /tmprm -f "/tmp/${pull}.patch"donegitamcleanup=falseif [[ -n "${DRY_RUN}" ]]; thenecho "!!! Skipping git push and PR creation because you set DRY_RUN."echo "To return to the branch you were in when you invoked this script:"echoecho " git checkout ${STARTINGBRANCH}"echoecho "To delete this branch:"echoecho " git branch -D ${NEWBRANCHUNIQ}"exit 0fiif git remote -v | grep ^${FORK_REMOTE} | grep {$MAIN_REPO_ORG}/{$MAIN_REPO_NAME}.git; thenecho "!!! You have ${FORK_REMOTE} configured as your {$MAIN_REPO_ORG}/{$MAIN_REPO_NAME}.git"echo "This isn't normal. Leaving you with push instructions:"echoecho "+++ First manually push the branch this script created:"echoecho " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}"echoecho "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)."echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values."echomake-a-prcleanbranch=""exit 0fiechoecho "+++ I'm about to do the following to push to GitHub (and I'm assuming ${FORK_REMOTE} is your personal fork):"echoecho " git push ${FORK_REMOTE}${NEWBRANCHUNIQ}:${NEWBRANCH}"echoread -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -rif ! [[ "${REPLY}" =~ ^[yY]$ ]]; thenecho "Aborting." >&2exit 1figit push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}"make-a-pr
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。