-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-clean-branches
executable file
·57 lines (47 loc) · 1.03 KB
/
git-clean-branches
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
#!/bin/bash -e
valid_remote(){
local remote=$1
git remote -v | grep --q "^$remote" && return 0 || return 1
}
gone_branches(){
local remote=$1
git branch -vv | sed "s/^\*//"| grep "$remote/.*: gone]" | awk '{print $1}'
}
help_text(){
echo "Checks what branches have be removed from the given remote"
echo "and deletes them."
echo
echo "Usage: git clean-branches [-f] [-d] [remote]"
echo " -f force deletes of the branches"
echo " -d dry-run only list the branches to be deleted"
}
delete_flag='-d'
while getopts ":fd" opt; do
case ${opt} in
f ) delete_flag='-D'
;;
d ) dry_run='true'
;;
\? )
help_text >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
remote=$1
if [ -z "$remote" ]; then
echo "no remote given defaulting to origin" >&2
remote='origin'
fi
if ! valid_remote $remote; then
echo "remote $remote is not listed in \`git remote -v\`" >&2
exit 1
fi
if [ "$dry_run" == 'true' ]; then
gone_branches $remote
exit
fi
for branch in $(gone_branches $remote); do
git branch $delete_flag "$branch"
done