-
Notifications
You must be signed in to change notification settings - Fork 5
/
delete.sh
executable file
·224 lines (189 loc) · 5.2 KB
/
delete.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
## /*
# @usage delete <branch-name>
#
# @description
# This script isa wrapper for removing branches locally. Removing them locally requires a bit of
# magic, which can be determined by observing the source code carefully. This obfuscation is
# included to prevent team members without sufficient access from deleting important remote
# branches.
# description@
#
# @dependencies
# *checkout.sh
# functions/0100.bad_usage.sh
# functions/5000.branch_exists_local.sh
# functions/5000.branch_exists_remote.sh
# functions/5000.parse_git_branch.sh
# dependencies@
#
# @file delete.sh
## */
$loadfuncs
echo ${X}
# parse arguments
numArgs=$#
forceDelete=false
deletePhrase="Deleting branch"
if (( numArgs > 0 && numArgs < 4 )); then
until [ -z "$1" ]; do
echo "Param is currently: ${1}"
[ "$1" = "--force" -o "$1" = "-f" -o "$1" = "-F" ] && [ "$ADMIN" = "true" ] && forceDelete=true
# catch multiple flags
deleteBranch="$1"
[ "$1" != "--force" -a "$1" != "-F" -a "$1" != "-f" ] && deleteBranch="$1"
shift
done
fi
# echo "We would force delete: ${forceDelete}"
# make sure branch name was included
if [ -z "$deleteBranch" ]; then
__bad_usage delete "Branch name to delete is required as the only parameter."
exit 1
else
__branch_exists_local "$deleteBranch" && isLocal=true
__set_remote && __branch_exists_remote "$deleteBranch" && isRemote=true
fi
if [ $forceDelete == false ]; then
# give the user a chance to cancel
echo ${Q}"Are you sure you want to ${A}delete${Q} branch ${B}\`${deleteBranch}\`${Q}? y (n)"${X}
read yn
echo
if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
echo "You got it. Aborting ${A}delete${X}..."
exit 1
fi
fi
if [ $forceDelete == true ]; then
deletePhrase="Force deleting branch"
fi
echo
echo ${H1}${H1HL}
echo " ${deletePhrase}: ${H1B}\`${deleteBranch}\`${H1} "
echo ${H1HL}${X}
echo
echo
# check to see if already on branch to delete
cb=$(__parse_git_branch)
if [ -n "$cb" ] && [ "$cb" = "$deleteBranch" ]; then
echo ${W}" You are currently on branch \`${deleteBranch}\` so it cannot be deleted. "${X}
echo
# create choices. third choice only available if a remote is configured.
declare -a choices
choices=( "Checkout branch \`master\`" "Checkout a local branch" )
[ $isRemote ] && choices[2]="Checkout a remote branch"
if __menu "${choices[@]}"; then
# This case will not get executed unless the menu selection is valid.
checkoutBranch=
case $_menu_sel_index in
# checkout master
1)
checkoutBranch="master";;
# checkout a local branch
2)
echo
echo "Loading local branches..."
__get_branch -l && checkoutBranch=$_branch_selection || {
echo
echo ${E}" No branch chosen to switch to. Aborting... "${X}
exit 1
};;
# checkout a remote branch (if remote is configured)
3)
echo
echo " Loading remote branches..."
__get_branch -r && checkoutBranch=$(echo $_branch_selection | sed "s/${_remote}\\//") || {
echo
echo ${E}" No branch chosen to switch to. Aborting... "${X}
exit 1
};;
*)
echo " Now exiting... ";
exit 0
;;
esac
echo "Branch selected: $checkoutBranch"
exit
echo
"${gitscripts_path}"checkout.sh $checkoutBranch
echo
echo
else
echo
echo ${E}" Your selection could not be interpreted. Exiting... "${X}
exit 1
fi
fi # END if current branch is the same as branch to delete
if [ $isLocal ]; then
#Determine if your local copy is behind remote
if [ $isRemote ] && git branch -v --abbrev=7 | egrep -q "$deleteBranch.*\[behind\ [0-9]*\]"; then
if [ $forceDelete == false ]; then
echo ${W}"Your local copy of this \`${deleteBranch}\` is behind the remote."
echo "Continue anyways? (y) n"${X}
read yn
if [ -n "$yn" ] && { [ "$yn" != "y" ] || [ "$yn" != "Y" ]; }; then
echo
echo "Aborting delete of ${B}\`${deleteBranch}\`"${X}
exit 1
fi
else
if ! git branch -D $deleteBranch; then
echo ${E}" Force delete failed! Exiting... "${X}
exit 1
else
echo ${COL_GREEN}"Force delete succeeded!"${X}
echo
echo "Exiting..."
exit 0
fi
fi
fi
if ! git branch -d "$deleteBranch" > /dev/null; then
if [ $forceDelete == false ]; then
echo ${W}"Delete failed! Would you like to force-delete the branch? y (n)"${X}
read yn
echo
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
if ! git branch -D $deleteBranch; then
echo ${E}" Force delete failed! Exiting... "${X}
exit 1
else
echo ${COL_GREEN}"Force delete succeeded!"${X}
echo
fi
fi
else
if ! git branch -D $deleteBranch; then
echo ${E}" Force delete failed! Exiting... "${X}
exit 1
else
echo ${COL_GREEN}"Force delete succeeded!"${X}
echo
fi
fi
else
echo ${COL_GREEN}"Delete succeeded!"${X}
echo
fi
fi
if [ $isRemote ]; then
echo
echo ${Q}"Delete ${B}\`${_remote}/${deleteBranch}\`${Q}? y (n)"${X}
read yn
echo
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo
echo "Deleting ${B}\`${_remote}/${deleteBranch}\`${X} ..."
echo ${O}${H2HL}
echo "$ git push ${remote} :${deleteBranch}"
git push "$remote" :"$deleteBranch"
echo ${H2HL}${X}
else
echo "Delete aborted. Exiting..."
exit 1
fi
else
echo "Branch \`${deleteBranch}\` is not on a remote. Now exiting..."
exit 1
fi
exit