-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_cleanup.sh
executable file
·62 lines (60 loc) · 1.69 KB
/
git_cleanup.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
#!/bin/sh
######################################################
# git-cleanup.sh
#
# Convenience script for deleting local git branches
######################################################
arr=($(git branch))
len=${#arr[@]}
if [[ ${len} > 0 ]]; then
while :
do
i=1
echo "-----------------"
echo "Choose branch(es)"
echo
echo " e.g. 1,5,7-10"
echo "-----------------"
for (( j=0; j<${len}; j+=1 )); do
echo " $i) ${arr[j]}"
options[i]=${arr[j]}
i=$((i+1))
done
read -p "Options: " opt
re='^[0-9]+([,|-]?[0-9]+)*$'
if [[ $opt =~ $re ]]; then
shopt -s nocasematch
IFS=',' read -a selections <<< "$opt"
for selection in "${selections[@]}"
do
if [[ $selection == *"-"* ]]; then
IFS='-' read -a range <<< "$selection"
for (( k=${range[0]}; k<=${range[${#range[@]}-1]}; k++)); do
branch=${arr[$k-1]}
read -p "Delete $branch (y/n)? " confirm
if [[ $confirm =~ (y|yes) ]]; then
echo "git branch -D $branch"
git branch -D $branch
echo
fi
done
else
branch=${arr[$selection-1]}
read -p "Delete $branch (y/n)? " confirm
if [[ $confirm =~ (y|yes) ]]; then
echo "git branch -D $branch"
git branch -D $branch
echo
fi
fi
done
break
fi
echo "Please enter a valid input."
sleep 1
done
else
echo "No branches detected."
exit 1
fi
exit 0