-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-prunify
executable file
·76 lines (68 loc) · 1.74 KB
/
git-prunify
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
#!/bin/bash
checkout=""
delete="-d"
print=''
usage(){
echo
echo " Usage: $0 [options]"
echo
echo " -b [branch] checkout to [branch] before starting"
echo " -D force-deletes branches instead of soft-delete"
echo " -p prints the branches instead of deleting them"
}
while getopts ":b:Dhp" opt; do
case ${opt} in
b ) checkout=$OPTARG ;;
D ) delete="-D" ;;
p ) print="yes" ;;
: )
echo " Invalid option: $OPTARG requires an argument"; usage ; exit 1 ;;
\? )
echo " Invalid option: $OPTARG"; usage; exit 1 ;;
esac
done
# A small animation to display while system fetches
loader=("Fetching " "Fetching. " "Fetching.. " "Fetching...")
fetcher(){
while [ true ]; do
for i in "${loader[@]}"; do
echo -ne "\r$i"
sleep 0.3
done
done
}
# Check it's a git repo
ISGIT=$(git rev-parse --is-inside-work-tree 2>/dev/null)
if [[ $ISGIT == "false" ]]; then
echo "### ERROR: This script should not be run inside .git folder ###"
exit 1
elif [[ $ISGIT != "true" ]]; then
echo "### Not inside a git repository ###"
exit 1
fi
if [[ ! -z "$checkout" ]]; then
echo "Checking out to $checkout"
git checkout $checkout
fi
fetcher &
pid=$!
git fetch -p &>/dev/null
kill $pid
wait $pid 2>/dev/null
echo -ne "\r"
# Iterate trhough it. The ones that say ": gone] " should be removed
git branch -vv | while IFS= read -r line ; do
if [[ $line = *": gone] "* ]]; then
IFS=' ' read -r -a array <<< "$line"
if [[ ${array[0]} == "*" ]]; then
branch="${array[1]}"
else
branch="${array[0]}"
fi
if [[ ${print} ]]; then
echo "Found orphaned branch ${branch}"
else
git branch $delete $branch
fi
fi
done