-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-merge-pr
executable file
·72 lines (56 loc) · 1.61 KB
/
git-merge-pr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/sh
set -e
function usage() {
bold=$(tput bold)
normal=$(tput sgr0)
cat << EOF
${bold}NAME${normal}
git-merge-pr - Merge a Pull Request into the master branch
${bold}SYNOPSIS${normal}
git merge-pr <PR-NUMBER>
EOF
}
# actually parse the options and do stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
*) ;;
esac
shift
done
pullRequest=$1
if [ -z "$pullRequest" ]; then
echo "Error: No pull request number given"
usage
echo "Recently Updated Pull Requests:"
hub pr list -o "updated" -L 10
exit 1
fi
currentBranch=$(git rev-parse --abbrev-ref HEAD)
# Checkout the pull request / branch
hub pr checkout ${pullRequest}
pullRequestTitle=$(hub pr show -f "%i: '%t'" ${pullRequest})
pullRequestBranch=$(git rev-parse --abbrev-ref HEAD)
upstream=$(git rev-parse --abbrev-ref @{upstream} 2>/dev/null)
defaultBranch=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
echo '[INFO] Syncing remotes and branches'
hub sync
echo "[INFO] git checkout $defaultBranch"
git checkout $defaultBranch
echo "[INFO] Merge pull request: ${pullRequestTitle}"
git merge --no-ff --no-edit ${pullRequestBranch}
git push origin $defaultBranch
echo "[INFO] Delete local and remote branches"
# Delete the pull request branch locally
git branch -d ${pullRequestBranch}
# Delete the remote branch if it lives in origin
if [ "${upstream}" == "origin" ]; then
git push ${upstream} --delete ${pullRequestBranch}
fi
# Checkout the original working branch if it wasn't the PR branch
if [ "${currentBranch}" != "${pullRequestBranch}" ]; then
git checkout ${currentBranch}
fi