-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.sh
executable file
·364 lines (292 loc) · 11.6 KB
/
branch.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env bash
### Script for working with branches: create, switch, delete
# Use a separate branch for writing new code, then merge it to default branch
# Read README.md to get more information how to use it
# Use this script only with gitbasher
### Main function
# $1: mode
# <empty>: switch to a local branch
# list: print a list of local branches
# remote: switch to a remote branch
# main: switch to the default branch
# new: create a new branch from the current one
# newd: create a new branch from the default branch
# delete: delete a local branch
function branch_script {
case "$1" in
list|l) list="true";;
remote|r|re) remote="true";;
main|def|m) main="true";;
new|n|c)
new="true"
current="true"
;;
newd|nd)
new="true"
;;
delete|del|d) delete="true";;
help|h) help="true";;
*)
wrong_mode "branch" $1
esac
### Print header
header="GIT BRANCH"
if [ -n "${remote}" ]; then
header="$header REMOTE"
elif [ -n "${main}" ]; then
header="$header DEFAULT"
elif [ -n "${current}" ]; then
header="$header NEW"
elif [ -n "${new}" ]; then
header="$header NEW FROM DEFAULT"
elif [ -n "${list}" ]; then
header="$header LIST"
elif [ -n "${delete}" ]; then
header="$header DELETE"
fi
echo -e "${YELLOW}${header}${ENDCOLOR}"
echo
if [ -n "$help" ]; then
echo -e "usage: ${YELLOW}gitb branch <mode>${ENDCOLOR}"
echo
echo -e "${YELLOW}Available modes${ENDCOLOR}"
echo -e "<empty>\t\tSelect a local branch to switch"
echo -e "list|l\t\tPrint a list of local branches"
echo -e "remote|re|r\tFetch $origin_name and select a remote branch to switch"
echo -e "main|def|m\tSwitch to $main_branch without additional confirmations"
echo -e "new|n|c\t\tBuild a conventional name and create a new branch from $main_branch"
echo -e "newd|nd\t\tBuild a conventional name, switch to $main_branch, pull it and create new branch"
echo -e "delete|del|d\tSelect a local branch to delete"
echo -e "help|h\t\tShow this help"
exit
fi
### Run switch to main logic
if [[ -n "${main}" ]]; then
switch ${main_branch}
exit
fi
### Run switch to local logic
if [[ -z "$new" ]] && [[ -z "$remote" ]] && [[ -z "$delete" ]] && [[ -z "$list" ]]; then
echo -e "${YELLOW}Select a branch to switch from '${current_branch}'${ENDCOLOR}:"
choose_branch
echo
switch ${branch_name}
exit
### Run switch to remote logic
elif [[ -z "$new" ]] && [[ -n "$remote" ]] && [[ -z "$delete" ]]; then
echo -e "${YELLOW}Fetching remote...${ENDCOLOR}"
echo
fetch_output=$(git fetch 2>&1)
check_code $? "$fetch_output" "fetch remote"
prune_output=$(git remote prune $origin_name 2>&1)
echo -e "${YELLOW}Switch from '${current_branch}' to the remote branch${ENDCOLOR}"
choose_branch "remote"
echo
switch ${branch_name}
exit
### Run delete local logic
elif [[ -z "$new" ]] && [[ -n "$delete" ]]; then
# Try to delete all merged branches
IFS=$'\n' read -rd '' -a merged_branches <<<"$(git branch -v --sort=-committerdate --merged | cat 2>&1)"
merged_branches_without_main=()
for index in "${!merged_branches[@]}"
do
branch_with_info="$(echo "${merged_branches[index]}" | sed -e 's/^[[:space:]]*//')"
if [[ ${branch_with_info} != "${main_branch}"* ]] && [[ ${branch_with_info} != "*"* ]] ; then
merged_branches_without_main+=("$branch_with_info")
fi
done
number_of_branches=${#merged_branches_without_main[@]}
if [ $number_of_branches != 0 ]; then
echo -e "${YELLOW}Do you want to delete merged local branches?${ENDCOLOR}"
echo -e "These are branches without new changes regarding ${YELLOW}${main_branch}${ENDCOLOR}"
for index in "${!merged_branches_without_main[@]}"
do
printf "\t${merged_branches_without_main[index]}\n"
done
printf "\nAnswer (y/n): "
while [ true ]; do
read -n 1 -s choice
if [ "$choice" == "y" ]; then
printf "y\n\n"
branches_to_delete="$(git branch --merged | egrep -v "(^\*|master|main|develop|${main_branch})" | xargs)"
IFS=$' ' read -rd '' -a branches <<<"$branches_to_delete"
for index in "${!branches[@]}"
do
branch_to_delete="$(echo "${branches[index]}" | xargs)"
delete_output=$(git branch -d $branch_to_delete 2>&1)
delete_code=$?
if [ $delete_code == 0 ]; then
echo -e "${GREEN}Branch '$branch_to_delete' is deleted!${ENDCOLOR}"
else
echo -e "${RED}Cannot delete branch '$branch_to_delete'!${ENDCOLOR}"
echo -e "${delete_output}"
break
fi
done
echo
break
else
printf "n\n\n"
break
fi
done
fi
# Delete in normal way
echo -e "${YELLOW}Delete a local branch${ENDCOLOR}"
choose_branch "delete"
echo
delete_output=$(git branch -d $branch_name 2>&1)
delete_code=$?
if [ "$delete_code" == 0 ]; then
echo -e "${GREEN}Branch '$branch_name' is deleted!${ENDCOLOR}"
elif [[ ${delete_output} == *"is not fully merged"* ]]; then
echo -e "${RED}The branch '$branch_name' is not fully merged${ENDCOLOR}"
echo "Do you want to force delete (-D flag) this branch?"
printf "Answer (y/n): "
while [ true ]; do
read -n 1 -s choice
if [ "$choice" == "y" ]; then
printf "y\n\n"
delete_output=$(git branch -D $branch_name 2>&1)
delete_code=$?
if [ "$delete_code" != 0 ]; then
echo -e "${RED}Cannot delete branch '$branch_name'! Error message:${ENDCOLOR}"
echo -e "${delete_output}"
exit
fi
echo -e "${GREEN}Branch '$branch_name' is deleted!${ENDCOLOR}"
break
elif [ "$choice" == "n" ]; then
printf "n\n"
exit
fi
done
else
echo -e "${RED}Cannot delete branch '$branch_name'! Error message:${ENDCOLOR}"
echo -e "${delete_output}"
exit
fi
remote_check=$(git --no-pager log $origin_name/$branch_name..HEAD 2>&1)
if [[ $remote_check != *"unknown revision or path not in the working tree"* ]]; then
echo
echo -e "${YELLOW}Do you want to delete this branch in the remote?${ENDCOLOR}"
printf "Answer (y/n): "
while [ true ]; do
read -n 1 -s choice
if [ "$choice" == "y" ]; then
printf "y\n\n"
echo -e "${YELLOW}Deleting...${YELLOW}"
push_output=$(git push $origin_name -d $branch_name 2>&1)
push_code=$?
echo
if [ "$push_code" != 0 ]; then
echo -e "${RED}Cannot delete branch '$branch_name'! Error message:${ENDCOLOR}"
echo -e "${delete_output}"
exit
fi
echo -e "${GREEN}Branch '$branch_name' is deleted in the remote!${ENDCOLOR}"
break
elif [ "$choice" == "n" ]; then
printf "n\n"
exit
fi
done
fi
exit
fi
echo -e "${YELLOW}Current local branches:${ENDCOLOR}"
list_branches
echo
### Run create new branch logic
### Step 1. Select branch type
echo -e "${YELLOW}Step 1.${ENDCOLOR} What type of branch do you want to create?"
echo -e "1. ${BOLD}feat:${ENDCOLOR}\tnew feature or logic changes, '${BOLD}feat${ENDCOLOR}' commits"
echo -e "2. ${BOLD}fix:${ENDCOLOR}\t\tsmall changes, eg. not critical bug fix"
echo -e "3. ${BOLD}hotfix:${ENDCOLOR}\tfix, that should be merged as fast as possible"
echo -e "4. ${BOLD}wip:${ENDCOLOR}\t\t'work in progress', for changes not ready for merging in the near future"
echo -e "5. ${BOLD}misc:${ENDCOLOR}\tnon-code changes, e.g. '${BOLD}ci${ENDCOLOR}', '${BOLD}docs${ENDCOLOR}', '${BOLD}build${ENDCOLOR}' commits"
echo -e "6. ${BOLD}test:${ENDCOLOR}\ttesting changes that probably won't be merged to the main branch"
echo -e "7. ${BOLD}chore:${ENDCOLOR}\tnon important style or docs changes"
if [ "$ticket_name" != "" ]; then
printf "8. $ticket_name:"
if [ $ticket_name = "" ]; then
printf "\t"
else
printf "\t\t"
fi
printf "use ticket name as prefix\n"
fi
echo -e "9. \t\tdon't use prefix for branch naming"
echo -e "0. Exit without changes"
declare -A types=(
[1]="feat"
[2]="fix"
[3]="hotfix"
[4]="wip"
[5]="misc"
[6]="test"
[7]="chore"
[8]="$ticket_name"
[9]=""
)
branch_type=""
while [ true ]; do
read -n 1 -s choice
re='^[1-9]+$'
if ! [[ $choice =~ $re ]]; then
exit
fi
branch_type="${types[$choice]}"
if [ -n "$branch_type" ]; then
branch_type_and_sep="${branch_type}${sep}"
fi
break
done
### Step 2. Enter branch name
echo
echo -e "${YELLOW}Step 2.${ENDCOLOR} Enter the name of the branch"
echo "Leave it blank if you want to exit"
printf "${BOLD}git branch${ENDCOLOR}"
read -p " ${branch_type_and_sep}" -e branch_name
if [ -z $branch_name ]; then
exit
fi
branch_name="${branch_type_and_sep}${branch_name##*( )}"
if [[ "$branch_name" == "HEAD" ]] || [[ "$branch_name" == "$origin_name" ]]; then
echo
echo -e "${RED}This name is forbidden${ENDCOLOR}"
exit
fi
### Step 3. Switch to main and pull it
from_branch=$current_branch
if [ -z "${current}" ]; then
echo
switch $main_branch "true"
echo -e "${YELLOW}Pulling '$origin_name/$main_branch'...${ENDCOLOR}"
echo
pull $main_branch $origin_name $editor
from_branch=$main_branch
fi
### Step 4. Create a new branch and switch to it
create_output=$(git switch -c $branch_name 2>&1)
create_code=$?
echo
if [ $create_code -eq 0 ]; then
echo -e "${GREEN}${create_output} from '$from_branch'${ENDCOLOR}"
changes=$(git_status)
if [ -n "$changes" ]; then
echo
echo -e "${YELLOW}Moved changes:${ENDCOLOR}"
echo -e "${changes}"
fi
exit
fi
if [[ $create_output == *"already exists"* ]]; then
echo -e "${RED}Branch with name '${branch_name}' already exists!${ENDCOLOR}"
exit $create_code
fi
echo -e "${RED}Cannot create '${branch_name}'! Error message:${ENDCOLOR}"
echo "${create_output}"
exit $create_code
}