Skip to content

Commit 577d1bd

Browse files
author
Bhavi Dhingra
committed
fix(gcommit): add new scopes in a loop
1 parent 8a96882 commit 577d1bd

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

cmd/gcommit

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
88
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__type_header
99
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__scope_header
10+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__new_scope_header
1011
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__description_header
1112
source "${__CUSTOM_CONSOLE_UTIL}"/__print_info
1213
source "${__CUSTOM_CONSOLE_UTIL}"/__print_as
@@ -52,9 +53,28 @@ function main() {
5253
return
5354
}
5455
commitMessage="${commitType}"
55-
print_commit_msg "${commitMessage}"
5656

57-
local commitScope="$(get_commit_scope)"
57+
local commitScope=""
58+
while true; do
59+
print_commit_msg "${commitMessage}"
60+
commitScope="$(get_commit_scope)"
61+
[ "${commitScope}" != "ADD NEW SCOPE" ] && break
62+
63+
local GCOMMIT_SCOPE_HEADER="$(__new_scope_header)"
64+
print_commit_msg "${commitMessage}"
65+
commitScope="$(echo "" | grep "x" |\
66+
fzf --height 10%\
67+
--info=hidden\
68+
--bind 'enter:print-query'\
69+
--header "${GCOMMIT_SCOPE_HEADER}")"
70+
71+
local repoPath="$(git rev-parse --show-toplevel)"
72+
local commitScopeFile="${repoPath}/git-commit-scope.list"
73+
# check if the new scope is already present
74+
[ -n "${commitScope}" ] \
75+
&& ! grep -Fxq "${commitScope}" "${commitScopeFile}" \
76+
&& echo "${commitScope}" >> "${commitScopeFile}"
77+
done
5878
[ -n "${commitScope}" ] && commitMessage="${commitMessage}(${commitScope})"
5979

6080
[ "${isBreakingChange}" == "true" ] && commitMessage="${commitMessage}!"
@@ -119,26 +139,11 @@ function get_commit_scope() {
119139
commitScopes=("${commitScopes[@]}" "${commitScope}")
120140
done < "${commitScopeFile}"
121141

122-
local addNewScope=false
123-
local GCOMMIT_SCOPE_HEADER="$(__scope_header "${addNewScope}")"
124-
142+
local GCOMMIT_SCOPE_HEADER="$(__scope_header)"
125143
commitScope="$(printf "%s\n" "${commitScopes[@]}" |\
126144
fzf --height 55%\
127145
--header "${GCOMMIT_SCOPE_HEADER}")"
128146

129-
if [[ "${commitScope}" == "ADD NEW SCOPE" ]]; then
130-
addNewScope=true
131-
local GCOMMIT_SCOPE_HEADER="$(__scope_header "${addNewScope}")"
132-
commitScope="$(echo "" | grep "x" |\
133-
fzf --height 10%\
134-
--info=hidden\
135-
--bind 'enter:print-query'\
136-
--header "${GCOMMIT_SCOPE_HEADER}")"
137-
# check if the new scope is already present
138-
[ -n "${commitScope}" ] \
139-
&& ! grep -Fxq "${commitScope}" "${commitScopeFile}" \
140-
&& echo "${commitScope}" >> "${commitScopeFile}"
141-
fi
142147
echo "${commitScope}"
143148
}
144149

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC1091
4+
#shellcheck disable=SC2155
5+
#shellcheck disable=SC2001
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __new_scope_header() {
10+
11+
local msg="", edge="", GCOMMIT_SCOPE_HEADER=""
12+
13+
local colorMsg="${UNDERLINE}${MAGENTA}gcommit${RESET} follows ${BLUE}https://www.conventionalcommits.org/${RESET} \n"
14+
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Add the scope to the commit message, "
15+
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}skip\n\n"
16+
colorMsg="${colorMsg}Enter the new commit scope (e.g. component or file name)"
17+
18+
msg="<enter>: Add the new commit scope to the list, <esc>: skip"
19+
edge="$(echo "${msg}" | sed "s/./-/g")"
20+
GCOMMIT_SCOPE_HEADER="${edge}\n${colorMsg}"
21+
22+
echo -e "${GCOMMIT_SCOPE_HEADER}"
23+
}

util/fzf_headers/gcommit/__scope_header

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,16 @@ source "$__CUSTOM_CONSOLE_UTIL"/__common
88

99
function __scope_header() {
1010

11-
local addNewScope="${1}"
1211
local msg="", edge="", GCOMMIT_SCOPE_HEADER=""
1312

1413
local colorMsg="${UNDERLINE}${MAGENTA}gcommit${RESET} follows ${BLUE}https://www.conventionalcommits.org/${RESET} \n"
15-
if [[ "${addNewScope}" == "true" ]]; then
16-
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Add the scope to the commit message, "
17-
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}skip\n\n"
18-
colorMsg="${colorMsg}Enter the new commit scope (e.g. component or file name)"
14+
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Select the commit scope, "
15+
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}skip\n\n"
16+
colorMsg="${colorMsg}What is the scope of this change? (e.g. component or file name)"
1917

20-
msg="<enter>: Add the new commit scope to the list, <esc>: skip"
21-
edge="$(echo "${msg}" | sed "s/./-/g")"
22-
GCOMMIT_SCOPE_HEADER="${edge}\n${colorMsg}"
23-
else
24-
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Select the commit scope, "
25-
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}skip\n\n"
26-
colorMsg="${colorMsg}What is the scope of this change? (e.g. component or file name)"
27-
28-
msg="What is the scope of this change? (e.g. component or file name)"
29-
edge="$(echo "${msg}" | sed "s/./-/g")"
30-
GCOMMIT_SCOPE_HEADER="${edge}\n${colorMsg}\n${edge}\n "
31-
fi
18+
msg="What is the scope of this change? (e.g. component or file name)"
19+
edge="$(echo "${msg}" | sed "s/./-/g")"
20+
GCOMMIT_SCOPE_HEADER="${edge}\n${colorMsg}\n${edge}\n "
3221

3322
echo -e "${GCOMMIT_SCOPE_HEADER}"
3423
}

0 commit comments

Comments
 (0)