-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitprune.sh
More file actions
executable file
·135 lines (125 loc) · 3.51 KB
/
gitprune.sh
File metadata and controls
executable file
·135 lines (125 loc) · 3.51 KB
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
#!/bin/bash
exists=0
yorn='y'
say_no=0
say_yes=0
while getopts ":pshnyd:" o; do
case "${o}" in
p)
only_prune=1
;;
s)
only_stale=1
;;
d)
stale_days=${OPTARG}
;;
n)
say_no=1
;;
y)
say_yes=1
;;
h)
echo "gitprune.sh [-p] [-s] [-d days]"
echo " -p - Prune Only. Prunes and asks if you want to delete local pruned branches."
echo " -s - Doesn't Prune or delete local branches. Asks if you want to delete stale remote branches."
echo " Can use the -d option to specify how many days a branch is stale by."
echo " -n - Say no to deleting stale branches."
echo " -y - Say yes to deleting local branches."
echo " -d days - How many days must a branch be stale before asking if you want to delete it."
exit 0
;;
*)
;;
esac
done
shift $((OPTIND-1))
if [ -z "$stale_days" ]; then
stale_days=5
fi
IFS=$'\n'
git remote prune origin
# Prune branches
if [ -z $only_stale ]; then
for i in $(git branch); do
i=$(echo ${i//[[:blank:]]/})
if [ "${i:0:1}" == "*" ]; then
# don't do branch we are currently on.
continue
fi
for j in $(git branch -r); do
j=$(echo ${j//[[:blank:]]/})
if [ "origin/$i" == "$j" ]; then
exists=1
break
fi
done
if [ $exists == 0 ]; then
if [ $say_yes == 0 ]; then
echo -n "Delete $i? "
read yorn
fi
if [ $say_yes == 1 ] || [ "$yorn" == "yes" ] || [ "$yorn" == "y" ]; then
echo -n "Deleting $i: "
git branch -D "$i"
fi
fi
exists=0
done
fi
# Notify of possible stale branches.
if [ -z $only_prune ]; then
branches=()
today=`date +%s`
total_days=$(expr $stale_days \* 24 \* 60 \* 60)
for k in $(git branch -r | sed s/^..//); do
if [ "$k" != "origin/master" ] && [ "$k" != "origin/HEAD -> origin/master" ]; then
date=`git log -1 --pretty=format:"%ci" $k --`
secs=`date -d "${date}" +%s`
diff=`expr $today - $secs`
if [ $diff -ge $total_days ]; then
if [ -z $display ]; then
echo
echo "--- Possible stale branches ---"
display=1
fi
days=`expr $diff / 60 / 60 / 24`
branches+=("${days}:${k}")
# echo "$k is ${days} days old"
fi
fi
done
IFS=$'\n' sorted=($(sort -n <<<"${branches[*]}"))
for b in ${sorted[@]}; do
days=$(echo $b | awk -F: '{print $1}')
branch=$(echo $b | awk -F: '{print $2}')
echo "$branch is $days days old"
done
if [ ${#sorted[@]} -eq 0 ]; then
exit
fi
if [ "$say_no" == 0 ]; then
echo -n "Do you want to kill these? "
read yorn
else
yorn='n'
fi
if [ "$yorn" == "yes" ] || [ "$yorn" == "y" ]; then
echo -n "For realsies? "
read yorn
if [ "$yorn" == "yes" ] || [ "$yorn" == "y" ]; then
total=1
for b in ${sorted[@]}; do
days=$(echo $b | awk -F: '{print $1}')
branch=$(echo $b | awk -F: '{print $2}' | awk -F/ '{st=index($0,"/");print substr($0,st+1)}')
git push origin :"$branch"
total=$(expr $total + 1)
if [ $total -eq 30 ]; then
sleep 60
total=1
fi
done
fi
fi
fi