Skip to content

Commit 46181cb

Browse files
committed
dev: make the pr script a little friendlier with conflicts
1 parent 0f980f5 commit 46181cb

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

scripts/pr

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
# pr <url|number> [<upstream remote>=origin]
99

1010
main () {
11+
if [ "$1" = "finish" ]; then
12+
shift
13+
finish "$@"
14+
return $?
15+
fi
16+
1117
local url="$(prurl "$@")"
1218
local num=$(basename $url)
1319
local prpath="${url#git@github.com:}"
@@ -49,10 +55,53 @@ main () {
4955
git pull --rebase origin pull/$num/head
5056
fi
5157

52-
# add the PR-URL to the last commit, after squashing
53-
5458
git rebase -i $curbranch # squash and test
5559

60+
if [ $? -eq 0 ]; then
61+
finish "${curbranch}"
62+
else
63+
echo "resolve conflicts and run: $0 finish "'"'${curbranch}'"'
64+
fi
65+
}
66+
67+
# add the PR-URL to the last commit, after squashing
68+
finish () {
69+
if [ $# -eq 0 ]; then
70+
echo "Usage: $0 finish <branch> (while on a PR-### branch)" >&2
71+
return 1
72+
fi
73+
74+
local curbranch="$1"
75+
local ref=$(cat .git/HEAD)
76+
local prnum
77+
case $ref in
78+
"ref: refs/heads/PR-"*)
79+
prnum=${ref#ref: refs/heads/PR-}
80+
;;
81+
*)
82+
echo "not on the PR-## branch any more!" >&2
83+
return 1
84+
;;
85+
esac
86+
87+
local me=$(git config github.user || git config user.name)
88+
if [ "$me" == "" ]; then
89+
echo "run 'git config --add github.user <username>'" >&2
90+
return 1
91+
fi
92+
93+
set -x
94+
95+
local url="$(prurl "$prnum")"
96+
local num=$prnum
97+
local prpath="${url#git@github.com:}"
98+
local repo=${prpath%/pull/$num}
99+
local prweb="https://github.com/$prpath"
100+
local root="$(prroot "$url")"
101+
102+
local api="https://api.github.com/repos/${repo}/pulls/${num}"
103+
local user=$(curl -s $api | json user.login)
104+
56105
local lastmsg="$(git log -1 --pretty=%B)"
57106
local newmsg="${lastmsg}
58107
@@ -63,9 +112,11 @@ Reviewed-by: @${me}
63112
"
64113
git commit --amend -m "$newmsg"
65114
git checkout $curbranch
66-
git merge $branch --ff-only
115+
git merge PR-${prnum} --ff-only
116+
set +x
67117
}
68118

119+
69120
prurl () {
70121
local url="$1"
71122
if [ "$url" == "" ] && type pbpaste &>/dev/null; then

0 commit comments

Comments
 (0)