2015-07-30 02:37:31 +00:00
|
|
|
#!/usr/bin/env bash
|
2015-07-21 23:57:06 +00:00
|
|
|
#
|
|
|
|
# Backports a PR into a release branch:
|
|
|
|
#
|
|
|
|
# # backport PR #123 into release-0.5-backport-123
|
|
|
|
# $ script/backport-pr 0.5 123
|
|
|
|
|
|
|
|
relversion="v$1.x"
|
|
|
|
relbranch="release-$1"
|
|
|
|
pr="$2"
|
|
|
|
prbranch="$relbranch-backport-$pr"
|
|
|
|
pullsurl="https://api.github.com/repos/github/git-lfs/pulls"
|
|
|
|
prurl="https://api.github.com/repos/github/git-lfs/pulls/$pr"
|
|
|
|
prjson="$(curl -n $pullsurl/$pr 2>/dev/null)"
|
|
|
|
headref="$(echo $prjson | jq -r -e ".head.ref")"
|
|
|
|
[ "$?" -ne 0 ] && {
|
|
|
|
echo "PR #$pr is invalid."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
prtitle="$(echo $prjson | jq -r ".title" | sed "s/\"/'/g")"
|
|
|
|
|
|
|
|
git checkout -q -f $relbranch
|
|
|
|
git clean -q -fdx
|
|
|
|
git pull -q
|
|
|
|
git checkout -q -f -B $prbranch
|
|
|
|
|
|
|
|
commit=`git log -1 --pretty=%H "--grep=Merge pull request #$pr" "--grep=Merge branch '.*$headref'" master`
|
|
|
|
|
|
|
|
echo "Backporting:\n"
|
|
|
|
|
|
|
|
git log -1 $commit
|
|
|
|
|
|
|
|
conflicts=""
|
|
|
|
|
|
|
|
git cherry-pick -x --allow-empty -m1 $commit &> /dev/null || {
|
2015-07-22 00:04:48 +00:00
|
|
|
unmerged=$(git ls-files --unmerged --stage | cut -f 2 -d$'\t' | uniq)
|
2015-07-21 23:57:06 +00:00
|
|
|
conflicts="\n\nConflicting files:"
|
|
|
|
for file in $unmerged; do
|
|
|
|
git add "$file"
|
|
|
|
conflicts="$conflicts\n- $file"
|
|
|
|
done
|
|
|
|
git commit -q --no-edit
|
|
|
|
}
|
|
|
|
|
|
|
|
commitmsg="Backport $headref from #$pr to $relbranch"
|
|
|
|
if [ "$conflicts" ]; then
|
|
|
|
commitmsg="$commitmsg [merge conflicts]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
git commit -q --allow-empty --amend -m "$commitmsg"
|
2015-07-22 19:19:03 +00:00
|
|
|
git push -q -f origin $prbranch
|
2015-07-21 23:57:06 +00:00
|
|
|
git checkout -q -f $relbranch
|
|
|
|
git branch -q -D $prbranch
|
|
|
|
|
2015-07-22 19:19:03 +00:00
|
|
|
curl -in $pullsurl -d "{
|
2015-07-21 23:57:06 +00:00
|
|
|
\"title\": \"Backport #$pr for $relversion: $prtitle\",
|
|
|
|
\"head\": \"$prbranch\",
|
|
|
|
\"base\": \"$relbranch\",
|
|
|
|
\"body\": \"This backports #$pr.$conflicts\"
|
|
|
|
}" 2>/dev/null
|