-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-prune.plugin.zsh
135 lines (109 loc) · 3.99 KB
/
git-prune.plugin.zsh
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
gprune() {
__prune_remote_branches() {
git push origin `git branch -r --merged "$1" | grep -v "HEAD" | grep -v '/release' | grep -v '/master$' | grep -v "/staging" | grep -v "/develop$" | grep -v "/$1$" | sed 's/origin\//:/g' | tr -d '\n'`
}
__prune_local_branches() {
git branch -d `git branch --merged "$1" | grep -v 'master$' | grep -v 'release' | grep -v "develop$" | grep -v "staging" | grep -v "$1$" | sed 's/origin\///g' | tr -d '\n'`
}
__print_remote_branches() {
__remote_branches=$(git branch -r --merged "$1" | grep -v "HEAD" | grep -v '/release' | grep -v '/master$' | grep -v "/staging" | grep -v "/develop$" | grep -v "/$1$")
if [[ -n "$__remote_branches" ]]; then
echo "These REMOTE branches will be removed:"
echo "$__remote_branches"
fi
}
__print_local_branches() {
__local_branches=$(git branch --merged "$1" | grep -v 'master$' | grep -v 'release' | grep -v "develop$" | grep -v "staging" | grep -v "$1$")
if [[ -n "$__local_branches" ]]; then
echo "These LOCAL branches will be removed:"
echo "$__local_branches"
fi
}
usage="usage:
gprune [ -r | --remote | -b | --both ] <branch-name>
options:
<branch-name>
This is the base branch which the plugin will use to compare the merged branches, for example:
given the branches \"master\", \"develop\" and \"example\", you are currently in the branch
\"develop\" and the branch named \"example\" is already merged into it but not into \"master\"
the plugin will delete the branch \"example\".
-r, --remote
Defines that only remote branches which were merged should be removed from the repository.
-b, --both
Defines that both remote and local branches which were merged
should be removed from the repository.
-s, --setup
Defines the protected branches, which won't be deleted in the process.
"
isHelp=false
isRemote=false
isLocal=false
isVersion=false
remote_branches=""
local_branches=""
branch_to_compare=$1
case $branch_to_compare in
("-h" | "--help") isHelp=true;;
("-v" | "--version") isVersion=true;;
("-r" | "--remote") isRemote=true;;
("-b" | "--both")
isRemote=true
isLocal=true
;;
(*) isLocal=true;;
esac
if $isVersion; then
echo "v1.0.3"
return
fi
if $isHelp; then
echo "$usage"
return
fi
if $isRemote; then
branch_to_compare=$2
fi
if [[ -z "$branch_to_compare" ]]; then
branch_to_compare=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
fi
if [[ -z "$branch_to_compare" ]]; then
echo "The branch name is invalid"
else
if ([ $isRemote = true ]) then
echo "Fetching remote branches..."
git fetch --prune
remote_branches=$(git branch -r --merged "$branch_to_compare" | grep -v "HEAD" | grep -v '/release' | grep -v '/master$' | grep -v "/staging" | grep -v "/develop$" | grep -v "/$branch_to_compare$")
fi
if ($isLocal) then
local_branches=$(git branch --merged "$branch_to_compare" | grep -v 'master$' | grep -v 'release' | grep -v "develop$" | grep -v "staging" | grep -v "$branch_to_compare$")
fi
echo "Current branch: $branch_to_compare"
if ([ -z "$remote_branches" ] && [ -z "$local_branches" ]) then
echo "There aren't any new merged branches into $branch_to_compare"
else
if $isLocal; then
__print_local_branches "$branch_to_compare"
fi
if $isRemote; then
__print_remote_branches "$branch_to_compare"
fi
declare choice=true
while $choice; do
read "yn?Are you sure you want to delete these branches? (Y/n): "
yn=${yn:-enter}
if [ $yn = "Y" -o $yn = "y" -o $yn = 'enter' ]; then
echo "Deleting branches..."
if ($isRemote) then
__prune_remote_branches "$branch_to_compare"
fi
if ($isLocal) then
__prune_local_branches "$branch_to_compare"
fi
choice=false
elif [ $yn = "N" -o $yn = "n" ]; then
choice=false
fi
done
fi
fi
}