-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgit-finish
124 lines (105 loc) · 3.07 KB
/
git-finish
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh
set -e
masterBranch="master"
if [[ "$1" = "--h" ]] ; then
echo "Finishing up feature and issue branches: ";
echo " - git finish";
echo ""
echo "Finishing up release and hotfix branches: ";
echo " - git finish <annotation>";
echo " where annotation is optional";
echo ""
echo "This command will rebase feature branches, tag release and hotfix branches"
echo "and merge them back into $masterBranch."
echo "Afterwards, the feature/release/hotfix branch will be cleaned up"
exit 1;
fi
if ! [ -d .git ] ; then
if ! [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ] ; then
echo "Current directory is not a git folder."
exit 1;
fi
fi
branch=$(git rev-parse --abbrev-ref HEAD)
shortBranch=$(echo $branch| cut -d'/' -f 2)
cleanup_localbranch () {
echo "Do you wish to delete the local branch $1?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Cleaning up local branch"; git branch -D $1; break;;
No ) break;;
esac
done
}
cleanup_remotebranch () {
if [[ "$(git branch -a | egrep "remotes/origin/$1" | wc -l)" = "1" ]]; then
echo "Do you wish to delete the remote branch remotes/origin/$1?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Cleaning up remote branch"; git push origin :$1; break;;
No ) break;;
esac
done
fi
}
create_pullrequest () {
git push origin $1
originUrl=$(git config --get remote.origin.url)
vsoRegex=".visualstudio.com"
if [[ "$originUrl" =~ $vsoRegex ]] ; then
originUrl="$originUrl/pullrequestcreate?targetRef=master&sourceRef=$1"
fi
case $( uname -s ) in
Darwin) open='open';;
MINGW*) open='start';;
MSYS*) open='start';;
CYGWIN*) open='cygstart';;
*) open='xdg-open';;
esac
# Allow printing the url if BROWSER=echo
if [[ $BROWSER != "echo" ]]; then
exec &>/dev/null
fi
# open it in a browser
${BROWSER:-$open} "$originUrl"
cleanup_localbranch $1
}
mergetomaster () {
echo "Merging to $1"
git merge --no-ff $2
git push --follow-tags origin $1
}
if [[ $branch =~ ^(feature|issue).* ]]; then
echo Pulling latest $masterBranch from origin
git pull origin $masterBranch:$masterBranch
echo Rebasing branch using $masterBranch
git rebase -i $masterBranch
elif [[ $branch =~ ^(release|hotfix).* ]]; then
echo Tagging release branch
if [ "$1" = "" ]; then
git tag V$shortBranch
else
git tag -a V$shortBranch -m "$1"
fi
else
echo "Currently not on a feature/issue/release/hotfix branch";
exit 1;
fi
git checkout $masterBranch
if [[ $branch =~ ^feature.* ]]; then
echo "Do you wish to: "
select opt in "Merge to $masterBranch" "Create a pull request"; do
case $opt in
"Merge to $masterBranch" ) mergetomaster $masterBranch $branch; break;;
"Create a pull request" ) create_pullrequest $branch; exit 0; break;;
esac
done
elif [[ $branch =~ ^issue.* ]]; then
echo "Merging to $masterBranch"
git merge --no-ff $branch -m "Resolving issue #$shortBranch"
git push origin $masterBranch
else
mergetomaster $masterBranch $branch
fi
cleanup_localbranch $branch
cleanup_remotebranch $branch