-
Notifications
You must be signed in to change notification settings - Fork 1
/
gprune.in
99 lines (82 loc) · 2.56 KB
/
gprune.in
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
#!/usr/bin/env bash
m4_include(gu_tools.inc.sh)
usage() {
printf '%s\n' \
'gprune @VERSION@' \
'' \
'gprune prune and delete remote branches and ' \
'prune and delete local branches no longer on remote.' \
'' \
'Usage: gprune [-h] [[-b] branch] [-r remote]' \
'' \
' -h Show this usage.' \
' branch: Name of branch to delete or prune.' \
' remote: Name of the remote to delete or prune from.'
}
set -e
# defaults
if [ -z "${REMOTE-}" ]; then
REMOTE=origin
fi
# process flags
pointer=1
while [[ $pointer -le $# ]]; do
if [[ ${!pointer} != "-"* ]]; then ((pointer++)) # not a parameter flag so advance pointer
else
param=${!pointer}
((pointer_plus = pointer + 1))
slice_len=1
case $param in
# paramter-flags with arguments
-r|--remote) REMOTE=${!pointer_plus}; ((slice_len++));;
-b|--branch) BRANCH=${!pointer_plus}; ((slice_len++));;
# binary flags
-h|--help) HELP=true;;
esac
# splice out pointer frame from positional list
[[ $pointer -gt 1 ]] \
&& set -- ${@:1:((pointer - 1))} ${@:((pointer + $slice_len)):$#} \
|| set -- ${@:((pointer + $slice_len)):$#};
fi
done
if [ ! -z "${HELP-}" ]; then
usage
exit 0
fi
if [ -z "${BRANCH-}" ]; then
BRANCH=$1
fi
# If the current selected branch was deleted from remote lets force deletion.
if [ -z "${BRANCH-}" ]; then
BRANCH=$(git b -vv | grep "*" | grep ': gone] ' | awk '{print $2}' | sed 's/* //')
fi
git fetch --prune
current_branch=$(git branch | grep "*" | sed 's/* //')
if [ "$current_branch" = "${BRANCH}" ]; then
echo Switching to a different branch
if branch_avail_on_remote ${REMOTE} ${BRANCH}; then
git checkout ${BASED_BRANCH}
else
git checkout master
fi
fi
if branch_avail_on_remote ${REMOTE} ${BRANCH}; then
read -r -p "You are deleting a local \"${BRANCH}\" branch and the remote \"${REMOTE}/${BRANCH}\" branch... Press CTRL-c to cancel or Enter to continue.." -n 1 dummy
if asksure; then
git push ${REMOTE} --delete ${BRANCH}
git branch -D ${BRANCH}
fi
else
if git rev-parse --verify ${BRANCH} > /dev/null 2>&1; then
git branch -D ${BRANCH}
fi
fi
# This part delete local branches, so there is no popups to confirm.
toDelete=$(git branch -vv | grep '*' | grep ': gone] ' | awk '{print $2}' )
if [ "$toDelete" != "" ]; then
git checkout master
git branch -D $toDelete
fi
git branch -D $(git branch -vv | grep ': gone] ' | awk '{print $1}' | xargs) > /dev/null 2>&1
echo Done.
# vim: set et ts=2 sw=2: