-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoCreateBranch.sh
executable file
·213 lines (187 loc) · 6.1 KB
/
doCreateBranch.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
#!/bin/bash
# Dedicated script to facilitate the branch creation.
#
# The script will guide the end-user step by step to collect all required
# information to create a new branch.
#
# Author : Jacques Raphanel
# Version: 1.3
#
COLOR_RESET='\033[0m'
COLOR_ERROR='\033[31m'
COLOR_SELECTED='\e[32m'
COLOR_INFO='\033[94m'
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
ctrl_c() {
tput cnorm
exit 1
}
# Print an error message through stderr
echo_err() {
printf "${COLOR_ERROR}ERROR: %s${COLOR_RESET}\n" "$@" >&2
}
# Print a normal text
echo_msg() {
printf "%s\n" "$@"
}
# Print a selected text
echo_selected() {
printf "${COLOR_SELECTED}%s${COLOR_RESET}\n" "$@"
}
# Print an informative text
echo_info() {
printf "${COLOR_INFO}%s${COLOR_RESET}\n" "$@"
}
# selected_item, ...menu_items
print_menu() {
local function_arguments=($@)
local selected_item="$1"
local menu_items=(${function_arguments[@]:1})
local menu_size="${#menu_items[@]}"
for (( i = 0; i < $menu_size; ++i )); do
if [ "$i" = "$selected_item" ]; then
echo_selected "-> ${menu_items[$i]}"
else
echo_msg " ${menu_items[$i]}"
fi
done
}
# selected_item, ...menu_items
function run_menu() {
IFS=""
local function_arguments=("$@")
local selected_item="$1"
local menu_items=(${function_arguments[@]:1})
local menu_size="${#menu_items[@]}"
local menu_limit=$((menu_size - 1))
local timeout=0.1
if [ "$(uname -s)" = "Darwin" ]; then
timeout=1
fi
tput civis
tput sc
print_menu "$selected_item" "${menu_items[@]}"
while read -rsn1 input
do
case "$input" in
$'\x1B') # ESC ASCII code (https://dirask.com/posts/ASCII-Table-pJ3Y0j)
read -rsn1 -t $timeout input
if [ "$input" = "[" ]; then
# occurs before arrow code
read -rsn1 -t $timeout input
case "$input" in
A) # Up Arrow
if [ "$selected_item" -ge 1 ]; then
selected_item=$((selected_item - 1))
tput rc
tput sc
print_menu "$selected_item" "${menu_items[@]}"
fi
;;
B) # Down Arrow
if [ "$selected_item" -lt "$menu_limit" ]; then
selected_item=$((selected_item + 1))
tput rc
tput sc
print_menu "$selected_item" "${menu_items[@]}"
fi
;;
esac
fi
read -rsn5 -t $timeout # flushing stdin
;;
"") # Enter key
tput cnorm
return "$selected_item"
;;
esac
done
}
check_git_repository() {
if ! git status >& /dev/null; then
echo_err "$PWD is not a git repository"
exit 1
fi
if [ -z "${source_branch}" ]; then
echo_err "failed to get the current branch name"
exit 1
fi
}
# We need to be in a git repository
source_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
check_git_repository
# Clear the screen to focus on branch creation
clear
# Select the commit type
selected_item=0
menu_items=('feat/feature: A new feature' )
menu_items+=('bugfix: A bug fix detected during development cycle' )
menu_items+=('hotfix: A bug fix detected as part of released version' )
menu_items+=('experimental: Test or experiment a new feature or idea' )
menu_items+=('build: Changes to to the build process')
menu_items+=('chore: Changes that are not related to a fix or feature and do not modify the source or test files')
menu_items+=('ci: Changes that are related to ci/cd activity')
echo_info "? Select the type of change that you're commiting: (Use arrow keys)"
run_menu "$selected_item" "${menu_items[@]}"
menu_result="$?"
btype=$(echo ${menu_items[$menu_result]} | cut -d: -f1)
# Enter short description/subject without space
subject=""
while [ -z "${subject// /}" ]; do
echo ""
echo_info "? A description MUST immediately follow the branch name prefix. The description is a short description of the changes."
read -p "Enter a branch description: " subject
done
# ensure that no space character present
subject=${subject// /-}
# Enter Jira user-story reference
jiraus=""
while true; do
echo ""
echo_info "? The associated JIRA user-story reference. Optional in case of experimental branch type"
read -p "Enter the jira reference: " jiraus
if [[ "$jiraus" =~ ^[A-Z]{2,6}-[0-9]{1,6}$ ]]; then
# Properly formatted
break
elif [ -z "${jiraus}" -a "${btype}" = "experimental" ]; then
# Optional in case of experimental branch
break
elif [ "${jiraus}" = "no-ref" -a "${btype}" = "experimental" ]; then
# no-ref is only allowed in case of experimental branch
break
fi
done
# Enter the source branch name
while true; do
echo ""
echo_info "? The source branch name from which you want to create your new branch"
read -p "Enter the branch name [${source_branch}]: " branch
if [ -z "${branch}" ]; then
branch=${source_branch}
fi
# Ensure that the branch name really exists
if git branch -a | grep -qc "${branch}\$"; then
break
fi
echo_err "${branch} : branch not found, please verify its existence or enter a new name"
done
new_branch="${btype}/${subject}"
if [ -n "${jiraus}" ]; then
new_branch+="/${jiraus}"
fi
while true; do
read -p "Do you really want to create ${new_branch} from ${branch}? [Y/n] " confirm
if [ "${confirm,,}" = "n" ]; then
# Skip branch creation
exit 0
fi
if [ -z "${confirm:-}" -o "${confirm,,}" = "y" ]; then
# Accept branch creation
break
fi
done
echo ""
echo_info "creating ${new_branch} ..."
git checkout -b "${new_branch}" "${branch}"
echo ""