Skip to content

Commit 9197e69

Browse files
author
Bhavi Dhingra
committed
feat(gcommit): add new scope directly to commit message
1 parent 78bfdfb commit 9197e69

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

cmd/gcommit

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,31 @@ function main() {
4747
local commitMessage=""
4848

4949
# get commit type
50-
printf "${commitMessage}"
50+
print_commit_msg "${commitMessage}"
5151
local isBreakingChange=false
5252
local commitType="$(get_commit_type "${isBreakingChange}")"
5353
if [[ "${commitType}" == "BREAKING-CHANGE" ]]; then
54-
printf "${commitMessage}"
54+
print_commit_msg "${commitMessage}"
5555
isBreakingChange=true
5656
commitType="$(get_commit_type "${isBreakingChange}")"
5757
fi
5858
[ -z "${commitType}" ] && {
59-
__print_info "commit type can't be empty"
59+
__print_info "commit type is required, for more details visit https://conventionalcommits.org"
6060
return
6161
}
6262
commitMessage="${commitType}"
6363

6464
# get commit scope
6565
local commitScope=""
66-
while true; do
67-
printf "${commitMessage}"
66+
while [ -z "${commitScope}" ]; do
67+
print_commit_msg "${commitMessage}"
6868
commitScope="$(get_commit_scope "${isBreakingChange}")"
69-
case "${commitScope}" in
70-
"${ADD_NEW_SCOPE}") add_new_scope "${isBreakingChange}"
71-
;;
72-
# "${DELETE_SCOPES}") delete_scopes #TODO
73-
# ;;
74-
*) break
75-
;;
76-
esac
69+
[ -z "${commitScope}" ] && break
70+
71+
if [[ "${commitScope}" == "${ADD_NEW_SCOPE}" ]]; then
72+
print_commit_msg "${commitMessage}"
73+
commitScope="$(get_new_scope)"
74+
fi
7775
done
7876
[ -n "${commitScope}" ] && commitMessage="${commitMessage}(${commitScope})"
7977

@@ -82,10 +80,11 @@ function main() {
8280
commitMessage="${commitMessage}: "
8381

8482
# get commit description
85-
printf "${commitMessage}"
83+
print_commit_msg "${commitMessage}"
8684
local commitDescription="$(get_commit_description "${isBreakingChange}")"
8785
[ -z "${commitDescription}" ] && {
88-
__print_info "commit description can't be empty"
86+
gstatus
87+
__print_info "commit description is required, for more details visit https://conventionalcommits.org"
8988
return
9089
}
9190
commitMessage="${commitMessage}${commitDescription}"
@@ -126,6 +125,8 @@ function get_commit_type() {
126125

127126
function get_commit_scope() {
128127

128+
sort_commit_scope_file
129+
129130
local isBreakingChange="${1}"
130131

131132
local commitScopes=("${ADD_NEW_SCOPE}")
@@ -147,19 +148,19 @@ function get_commit_scope() {
147148
echo "${commitScope}"
148149
}
149150

150-
function add_new_scope() {
151-
152-
local isBreakingChange="${1}"
151+
function get_new_scope() {
153152

154153
local GCOMMIT_SCOPE_HEADER="$(__new_scope_header)"
155-
printf "${commitMessage}"
156-
commitScope="$(echo "" | grep "x" |\
154+
local commitScope="$(echo "" | grep "x" |\
157155
fzf --height 1%\
158156
--info=hidden\
159157
--bind 'enter:print-query'\
160158
--header "${GCOMMIT_SCOPE_HEADER}")"
161159

162-
[ -z "${commitScope}" ] && return
160+
[ -z "${commitScope}" ] && {
161+
echo ""
162+
return
163+
}
163164

164165
if [[ ! -f "${COMMIT_SCOPE_FILE}" ]]; then
165166
echo "${commitScope}" > "${COMMIT_SCOPE_FILE}" # creates the file
@@ -169,6 +170,13 @@ function add_new_scope() {
169170
echo "${commitScope}" >> "${COMMIT_SCOPE_FILE}" # appends to the file
170171
}
171172
fi
173+
sort_commit_scope_file
174+
175+
echo "${commitScope}"
176+
}
177+
178+
function sort_commit_scope_file() {
179+
172180
sort "${COMMIT_SCOPE_FILE}" > "${COMMIT_SCOPE_FILE}_sorted"
173181
mv "${COMMIT_SCOPE_FILE}_sorted" "${COMMIT_SCOPE_FILE}"
174182
}
@@ -189,4 +197,9 @@ function get_commit_description() {
189197
echo "${commitDescription}"
190198
}
191199

200+
function print_commit_msg() {
201+
__print_as "bold" "reverse" "commit message:-"
202+
echo -n " ${1}"
203+
}
204+
192205
main

0 commit comments

Comments
 (0)