-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-delete-branch-interactive.sh
219 lines (199 loc) · 7.02 KB
/
git-delete-branch-interactive.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
#!/usr/bin/env bash
__gdel_script_dirname="$(dirname $0)"
gdel() {
__gdel_usage() {
echo "Git Delete++ -- SimGus 2020"
echo "Usage: gdel [-i|--interactive] [<PARTIAL-BRANCH-NAME>]"
echo "\t<PARTIAL-BRANCH-NAME>\t\tThe name of a git branch (can be partial in interactive mode)"
echo "\t-i, --interactive\t\tRun the command in interactive mode"
echo "\t-l, --local-only\tIf the command runs interactively, only allows to select and delete a local branch"
echo "\t-r, --remote-only\tIf the command runs interactively, only allows to select and delete a remote branch"
echo "\nFor information about Git's official branch delete command, please read 'man git branch'."
}
if [ "$#" -eq 0 ]
then
echo "Too few arguments"
__gdel_usage
return 1
else
local interactive=false
for arg in "$@"
do
shift
if [ "$arg" = "-i" ] || [ "$arg" = "--interactive" ]
then
interactive=true
continue
fi
set -- "$@" "$arg"
done
if [ "$interactive" = false ]
then
git branch -d $@
else
git_delete_interactive $@
fi
fi
}
git_delete_interactive() (
source "$__gdel_script_dirname"/utils.sh
__gdi_usage() {
echo "Git Delete Interactive -- SimGus 2020"
echo "Usage: git_delete_interactive [-l|-r] [<PARTIAL-BRANCH-NAME>]"
echo "\t<PARTIAL-BRANCH-NAME>\tThe name of a git branch (can be partial)"
echo "\t-l, --local-only\tOnly allows to select and delete a local branch"
echo "\t-r, --remote-only\tOnly allows to select and delete a remote branch"
}
__gdi_get_info_remote_branch() {
if [ "$#" -ne 1 ]
then
return 1
fi
local pattern="^\*?\s+$1"
local branch_info=$(git branch -vv | eval $__gi_grep_command '$pattern')
if [[ "$branch_info" =~ '^\*?\s+\w+\s+\w+\s+\[' ]]
then
local remote_info="${${branch_info#*\[}%%\]*}"
if [[ "$remote_info" =~ ':' ]]
then
echo "${remote_info%%:*} false"
else
echo "${remote_info%%:*} true"
fi
fi
}
__gdi_delete_branch() {
local local_branch=false
local remote_branch=false
for arg in "$@"
do
shift
if [ "$arg" = "--local" ]
then
local_branch=true
continue
elif [ "$arg" = "--remote" ]
then
remote_branch=true
continue
fi
set -- "$@" "$arg"
done
if [ "$#" -ne 1 ]
then
echo "Internal error: $0 received an incorrect number of parameters."
return 1
fi
if [ $local_branch = true ]
then
echo "Deleting local branch $1"
git branch -D "$1"
elif [ $remote_branch = true ]
then
echo "Deleting remote branch $1"
git push --delete $(echo $1 | sed 's|/| |')
fi
}
if [ "$(__gi_is_git_repo)" = false ]
then
echo "Fatal: not in a get repository"
return 1
else
__gi_check_dependencies
local only_remote_branch=false
local only_local_branch=false
local branch_list_arg=""
for arg in "$@"
do
shift
if [ "$arg" = "-r" ] || [ "$arg" = "--remote-only" ]
then
only_remote_branch=true
branch_list_arg="-r"
continue
elif [ "$arg" = "-l" ] || [ "$arg" = "--local-only" ]
then
only_local_branch=true
continue
fi
set -- "$@" "$arg"
done
local branches=$(__gi_fetch_branches "$branch_list_arg")
local branch_selection_return_value="$?"
if [ "$#" -gt 1 ]
then
echo "Too many arguments."
__gdi_usage
return 1
fi
if [ "$branch_selection_return_value" -ne 0 ]
then
echo "Error while fetching branches."
return 1
else
local nb_branches=$(echo $branches | wc -l)
if [ "$nb_branches" -lt 1 ]
then
echo "No branch to delete"
return 0
elif [ "$nb_branches" -eq 1 ]
then
local selected_branch=$branches
echo "Only one branch found: $selected_branch"
else
local selected_branch=$(echo $branches | fzf --cycle -q "$1")
if [ -z "$selected_branch" ]
then
return 0
fi
fi
if [ $only_local_branch = true ]
then
__gdi_delete_branch --local $selected_branch
elif [ $only_remote_branch = true ]
then
git fetch &> /dev/null
__gdi_delete_branch --remote $selected_branch
else
git fetch &> /dev/null
local remote_branch_info="$(__gdi_get_info_remote_branch $selected_branch)"
if [ -n "$remote_branch_info" ]
then
remote_branch_name="$(echo $remote_branch_info | cut -d' ' -f1)"
local up_to_date_with_remote="$(echo $remote_branch_info | cut -d' ' -f2)"
if [ $up_to_date_with_remote = true ]
then
__gdi_delete_branch --remote $remote_branch_name
else
echo "Remote branch $remote_branch_name is not up-to-date with the local tracking branch $selected_branch"
echo -n "Delete it anyway? [y/n] "
local delete_anyway
read delete_anyway
if [[ "$delete_anyway" =~ [yY] ]]
then
__gdi_delete_branch --remote $remote_branch_name
else
echo "Not deleting remote branch $remote_branch_name"
fi
fi
echo ""
fi
if [ "$selected_branch" = "$(git_current_branch)" ]
then
echo "Cannot delete current local branch. Please checkout another branch and retry."
return 1
else
echo -n "Delete local branch $selected_branch (the last copy of the work)? [y/N] "
local delete_local_branch
read delete_local_branch
if [[ "$delete_local_branch" =~ [yY] ]]
then
__gdi_delete_branch --local $selected_branch
else
echo "Not deleting local branch $selected_branch"
fi
fi
fi
fi
fi
)