hack/cherry-pick.sh
25012b65
 #!/bin/bash
 
 # See HACKING.md for usage
614bf6cc
 source "$(dirname "${BASH_SOURCE}")/lib/init.sh"
25012b65
 
cf28c106
 repo="${UPSTREAM_REPO:-k8s.io/kubernetes}"
 package="${UPSTREAM_PACKAGE:-pkg/api}"
571c8dad
 UPSTREAM_REPO_LOCATION="${UPSTREAM_REPO_LOCATION:-../../../${repo}}"
9e82cfbd
 pr="$1"
cf28c106
 
25012b65
 if [[ "$#" -ne 1 ]]; then
cf28c106
   echo "You must supply a pull request by number or a Git range in the upstream ${repo} project" 1>&2
25012b65
   exit 1
 fi
 os::build::require_clean_tree # Origin tree must be clean
 
d1828cd6
 patch="${TMPDIR:-/tmp}/patch"
2aa915b2
 rm -rf "${patch}"
 mkdir -p "${patch}"
 patch="${patch}/cherry-pick"
 
571c8dad
 if [[ ! -d "${UPSTREAM_REPO_LOCATION}" ]]; then
   echo "Expected ${UPSTREAM_REPO_LOCATION} to exist" 1>&2
25012b65
   exit 1
 fi
 
 if [[ -z "${NO_REBASE-}" ]]; then
54fca663
   lastrev="$(go run ${OS_ROOT}/tools/godepversion/godepversion.go ${OS_ROOT}/Godeps/Godeps.json ${repo}/${package})"
25012b65
 fi
 
571c8dad
 pushd "${UPSTREAM_REPO_LOCATION}" > /dev/null
25012b65
 os::build::require_clean_tree
 
35cb35a3
 remote="${UPSTREAM_REMOTE:-origin}"
 git fetch ${remote}
 
9e82cfbd
 selector="$(os::build::commit_range $pr ${remote}/master)"
25012b65
 
 if [[ -z "${NO_REBASE-}" ]]; then
cf28c106
   echo "++ Generating patch for ${selector} onto ${lastrev} ..." 2>&1
   if git rev-parse last_upstream_branch > /dev/null 2>&1; then
     git branch -d last_upstream_branch
25012b65
   fi
cf28c106
   git checkout -b last_upstream_branch "${lastrev}"
94bcf639
   git diff -p --raw --binary "${selector}" > "${patch}"
25012b65
   if ! git apply -3 "${patch}"; then
     git rerere # record pre state
     echo 2>&1
     echo "++ Merge conflicts when generating patch, please resolve conflicts and then press ENTER to continue" 1>&2
     read
   fi
   git rerere # record post state
   # stage any new files
   git add . > /dev/null
   # construct a new patch
f0eadcca
   git diff --cached -p --raw --binary --{src,dst}-prefix=a/vendor/${repo}/ > "${patch}"
25012b65
   # cleanup the current state
   git reset HEAD --hard > /dev/null
   git checkout master > /dev/null
b2164643
   git branch -D last_upstream_branch > /dev/null
25012b65
 else
   echo "++ Generating patch for ${selector} without rebasing ..." 2>&1
f0eadcca
   git diff -p --raw --binary --{src,dst}-prefix=a/vendor/${repo}/ "${selector}" > "${patch}"
25012b65
 fi
 
 popd > /dev/null
 
 echo "++ Applying patch ..." 2>&1
 echo 2>&1
 set +e
 git apply --reject "${patch}"
 if [[ $? -ne 0 ]]; then
80a50a19
   echo "++ Not all patches applied, merge *.rej into your files or rerun with REBASE=1"
25012b65
   exit 1
 fi
 
9e82cfbd
 commit_message="UPSTREAM: $pr: Cherry-picked"
 if [ "$repo" != "k8s.io/kubernetes" ]; then
   commit_message="UPSTREAM: $repo: $pr: Cherry-picked"
 fi
 
25012b65
 set -o errexit
 git add .
9e82cfbd
 git commit -m "$commit_message" > /dev/null
25012b65
 git commit --amend
 echo 2>&1
 echo "++ Done" 2>&1