-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-branches.plugin.zsh
202 lines (167 loc) · 4.79 KB
/
git-branches.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
# ----------------------
# Git helper functions
# Merge branches
# Checkout local branches
# Find branches by keyword
# Checkout remote branches
# Delete local branches
# ----------------------
_INPUT=
branchesFile=$TMPDIR'branches'
trap "{ rm -f $branchesFile; }" EXIT
_ACTION_MERGE="merge"
_ACTION_CHECKOUT="checkout"
_ACTION_DELETE="deletion"
_ACTION_REMOTE_CHECKOUT="remote checkout"
# git merge
gm() {
_showListOrHandleUserInputForAction $_ACTION_MERGE $1
}
# git checkout
gco() {
_showListOrHandleUserInputForAction $_ACTION_CHECKOUT $1
}
# git branch -d
gbd() {
_showListOrHandleUserInputForAction $_ACTION_DELETE $1
}
# git checkout -t
gcor() {
_showListOrHandleUserInputForAction $_ACTION_REMOTE_CHECKOUT $1
}
_showListOrHandleUserInputForAction() {
_ACTION=$1
_USER_INPUT=$2
if [ -z "$_USER_INPUT" ]; then
_presentBranchList $_ACTION
elif; then
_handleActionWithUserInput $_ACTION $_USER_INPUT
fi
}
_presentBranchList() {
_ACTION=$1
_addBranchesToFileForAction $_ACTION
currentBranch=$(git branch | grep \* | cut -d ' ' -f2)
echo "Current branch:\n$fg[green]$currentBranch$reset_color"
_removeCurrentBranchFromList
echo "Branches available for $_ACTION:"
_listBranchesFromFile $branchesFile
_validateInput 'Branch number: '
branch=$(head -$_INPUT $branchesFile | tail -1 | awk '{$1=$1};1')
_handleActionWithBranch $_ACTION $branch
}
_addBranchesToFileForAction() {
_ACTION=$1
case $_ACTION in
"remote checkout") git branch -r >$branchesFile ;; # Get remote branches
*) git branch >$branchesFile ;; # Get local branches
esac
}
_handleActionWithKeyword() {
_ACTION=$1
_KEYWORD=$2
grepBranches=$TMPDIR'grepBranches'
trap "{ rm -f $grepBranches; }" EXIT
_addBranchesToFileForAction $_ACTION
_removeCurrentBranchFromList
hitCount=$(grep -c $_KEYWORD $branchesFile)
grep $_KEYWORD $branchesFile >$grepBranches
case $hitCount in
# If 0 results, return
0) return ;;
# If 1 result, give option to switch
1)
line=($(grep -i "$_KEYWORD" $grepBranches))
if read -q "choice?Did you mean $fg[green]$line$reset_color? [y/n] "; then
echo
_handleActionWithBranch $_ACTION $line
fi
;;
# If multiple results, let the user choose
*)
echo "Found branches with keyword '$_KEYWORD'"
_listBranchesFromFile $grepBranches
_validateInput 'Branch number: '
branch=$(awk -v "line=$_INPUT" 'NR==line' $grepBranches | awk '{$1=$1};1')
_handleActionWithBranch $_ACTION $branch
;;
esac
}
_removeCurrentBranchFromList() {
n=1
while read line; do
if [[ $line == *"*"* ]]; then
# Remove the current branch from the file, we cannot checkout current branch
sed -i "" $n\d $branchesFile
fi
n=$((n + 1))
done <$branchesFile
}
_listBranchesFromFile() {
FILE=$1
n=1
while read line; do
if [[ $line != *"*"* ]]; then
echo "$n. $fg[red]$line$reset_color"
n=$((n + 1))
fi
done <$FILE
}
_validateInput() {
while true; do
# Read user input
printf $1
read tmp
# If input is not an integer or if input is out of range, throw an error
# Ask for input again
if [[ ! $tmp =~ ^[0-9]+$ ]]; then
echo "$fg[red]Invalid input$reset_color"
elif [[ "$tmp" -lt "1" ]] || [[ "$tmp" -gt $((n - 1)) ]]; then
echo "$fg[red]Input out of range $reset_color"
else
_INPUT=$tmp
break
fi
done
}
_handleActionWithUserInput() {
_ACTION=$1
_USER_INPUT=$2
case $_ACTION in
$_ACTION_MERGE)
if ! git merge --no-ff $_USER_INPUT; then
_handleActionWithKeyword $_ACTION $_USER_INPUT
fi
;;
$_ACTION_REMOTE_CHECKOUT)
if ! git checkout -t $_USER_INPUT; then
_handleActionWithKeyword $_ACTION $_USER_INPUT
fi
;;
$_ACTION_CHECKOUT)
if ! git checkout $_USER_INPUT; then
_handleActionWithKeyword $_ACTION $_USER_INPUT
fi
;;
$_ACTION_DELETE)
if ! git branch -D $_USER_INPUT; then
_handleActionWithKeyword $_ACTION $_USER_INPUT
fi
;;
esac
}
_handleActionWithBranch() {
_ACTION=$1
_BRANCH=$2
case $_ACTION in
$_ACTION_MERGE) git merge --no-ff $_BRANCH ;;
$_ACTION_REMOTE_CHECKOUT) git checkout -t $_BRANCH ;;
$_ACTION_CHECKOUT) git checkout $_BRANCH ;;
$_ACTION_DELETE)
if read -q "choice?$fg[red]Are you sure you want to delete branch $reset_color$_BRANCH$fg[red]? [y/n]$reset_color "; then
echo
git branch -D $_BRANCH
fi
esac
}