From 47f54dbecaf15fe485c9dc345505f3f3cb1348ea Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Thu, 2 Mar 2017 12:39:02 -0500 Subject: [PATCH 1/2] Add script/backport-pr --- script/backport-pr | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 script/backport-pr diff --git a/script/backport-pr b/script/backport-pr new file mode 100755 index 00000000..fa0df835 --- /dev/null +++ b/script/backport-pr @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# This script is pulled from https://github.com/git-lfs/git-lfs/blob/master/script/backport-pr +# Many thanks, git-lfs team! +# +# Backports a PR into a release branch: +# +# # backport PR #123 into 1.1-stable-backport-1023 +# $ git checkout master +# $ git pull +# $ script/backport-pr 1.1 1023 + +usage() { + echo "usage: $0 " + echo "example: $0 3.4 5623" +} + +if test -z "$1"; then + echo "fatal: no minor release version, e.g. '3.4'" > /dev/stderr + usage + exit 1 +fi + +if test -z "$2"; then + echo "fatal: no pull request number, e.g. '5623'" > /dev/stderr + usage + exit 1 +fi + +relversion="v$1.x" +relbranch="$1-stable" +pr="$2" +prbranch="$relbranch-backport-$pr" +pullsurl="https://api.github.com/repos/jekyll/jekyll/pulls" +prurl="https://api.github.com/repos/jekyll/jekyll/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 || { + unmerged=$(git ls-files --unmerged --stage | cut -f 2 -d$'\t' | uniq) + 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" +git push -q -f origin $prbranch +git checkout -q -f $relbranch +git branch -q -D $prbranch + +curl -in $pullsurl -d "{ + \"title\": \"Backport #$pr for $relversion: $prtitle\", + \"head\": \"$prbranch\", + \"base\": \"$relbranch\", + \"body\": \"This backports #$pr.$conflicts\" +}" 2>/dev/null From 67a7c22defd8e342a8c900dd858335e259a35970 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 6 Mar 2017 21:00:25 -0500 Subject: [PATCH 2/2] Fix typo in backport-pr --- script/backport-pr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/backport-pr b/script/backport-pr index fa0df835..5ef6c4de 100755 --- a/script/backport-pr +++ b/script/backport-pr @@ -4,7 +4,7 @@ # # Backports a PR into a release branch: # -# # backport PR #123 into 1.1-stable-backport-1023 +# # backport PR #1023 into 1.1-stable-backport-1023 # $ git checkout master # $ git pull # $ script/backport-pr 1.1 1023