-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-remove-merged.zsh
executable file
·75 lines (59 loc) · 2.06 KB
/
git-remove-merged.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
#! /bin/zsh
compdef _git-remove-merged git-remove-merged
_git-remove-merged() {
_arguments \
'*:Branch to ignore:__branch_names'
}
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format '%B%d%b'
function git-remove-merged() {
# Define functions that will only be used here locally
local function deleteLocal()
{
local COMMAND="git branch -d $@"
eval $COMMAND
}
local function deleteRemote()
{
local COMMAND="git push origin -d $@"
eval $COMMAND
}
# Do actual work
local CURRENT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
local EXCLUDE_STRING="master"
if [ "$CURRENT_BRANCH" != "master" ]; then
# Add the base branch to exclude pattern with master
EXCLUDE_STRING="master\|$CURRENT_BRANCH"
fi
for i in $@
do
EXCLUDE_STRING="$EXCLUDE_STRING\|$i"
done
# Build the command to get the names of branches that have been merged into the base, excluding those in the EXCLUDE_STRING
local COMMAND="git branch --merged $CURRENT_BRANCH | grep -v '^[ *]*$EXCLUDE_STRING$'"
# Get branches from running COMMAND
local BRANCHES_TO_DELETE=($(eval $COMMAND))
if [ ! "$BRANCHES_TO_DELETE" ]; then
echo "No branches to delete"
return
fi
# Parse branches to array of values and print them for the user
echo "Branches to be deleted:"
print -l ${BRANCHES_TO_DELETE[*]}
if ! verify "Are you sure you would like to delete these? (y/n)"; then
return
fi
# Convert array back to " " seperated string
BRANCHES_TO_DELETE=${BRANCHES_TO_DELETE[*]}
if verify "Would you like to delete both LOCAL and REMOTE versions, THIS CANNOT BE UNDONE? (y/n)" ; then
deleteLocal $BRANCHES_TO_DELETE
deleteRemote $BRANCHES_TO_DELETE
return
fi
if verify "Would you like to delete LOCAL versions? (y/n)" ; then
deleteLocal $BRANCHES_TO_DELETE
fi
if verify "Would you like to delete REMOTE versions? (y/n)" ; then
deleteRemote $BRANCHES_TO_DELETE
fi
}