Skip to content

Commit 026b80c

Browse files
author
Bhavi Dhingra
committed
fix(gcommit): don't create .gitcommitscopes file beforehand
1 parent 39c5e02 commit 026b80c

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

cmd/gcommit

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ __assertgitrepo
1919

2020
REPO_PATH="$(git rev-parse --show-toplevel)"
2121
COMMIT_SCOPE_FILE="${REPO_PATH}/.gitcommitscopes"
22+
ADD_NEW_SCOPE="ADD NEW SCOPE TO THE LIST"
23+
# DELETE_SCOPES="DELETE SCOPE(s) FROM THE LIST"
2224

2325
function main() {
2426

@@ -62,21 +64,16 @@ function main() {
6264
while true; do
6365
print_commit_msg "${commitMessage}"
6466
commitScope="$(get_commit_scope)"
65-
[ "${commitScope}" != "ADD NEW SCOPE" ] && break
66-
67-
local GCOMMIT_SCOPE_HEADER="$(__new_scope_header)"
68-
print_commit_msg "${commitMessage}"
69-
commitScope="$(echo "" | grep "x" |\
70-
fzf --height 10%\
71-
--info=hidden\
72-
--bind 'enter:print-query'\
73-
--header "${GCOMMIT_SCOPE_HEADER}")"
74-
75-
# check if the new scope is already present
76-
[ -n "${commitScope}" ] \
77-
&& ! grep -Fxq "${commitScope}" "${COMMIT_SCOPE_FILE}" \
78-
&& echo "${commitScope}" >> "${COMMIT_SCOPE_FILE}"
67+
case "${commitScope}" in
68+
"${ADD_NEW_SCOPE}") add_new_scope
69+
;;
70+
# "${DELETE_SCOPES}") delete_scopes #TODO
71+
# ;;
72+
*) break
73+
;;
74+
esac
7975
done
76+
8077
[ -n "${commitScope}" ] && commitMessage="${commitMessage}(${commitScope})"
8178

8279
[ "${isBreakingChange}" == "true" ] && commitMessage="${commitMessage}!"
@@ -130,14 +127,13 @@ function get_commit_type() {
130127

131128
function get_commit_scope() {
132129

133-
if [[ ! -f "${COMMIT_SCOPE_FILE}" ]]; then
134-
touch "${COMMIT_SCOPE_FILE}"
135-
fi
136-
local commitScopes=("ADD NEW SCOPE")
130+
local commitScopes=("${ADD_NEW_SCOPE}")
137131
local commitScope=""
138-
while IFS= read -r commitScope; do
139-
commitScopes=("${commitScopes[@]}" "${commitScope}")
140-
done < "${COMMIT_SCOPE_FILE}"
132+
if [[ -f "${COMMIT_SCOPE_FILE}" ]]; then
133+
while IFS= read -r commitScope; do
134+
commitScopes=("${commitScopes[@]}" "${commitScope}")
135+
done < "${COMMIT_SCOPE_FILE}"
136+
fi
141137

142138
local GCOMMIT_SCOPE_HEADER="$(__scope_header)"
143139
commitScope="$(printf "%s\n" "${commitScopes[@]}" |\
@@ -147,6 +143,28 @@ function get_commit_scope() {
147143
echo "${commitScope}"
148144
}
149145

146+
function add_new_scope() {
147+
148+
local GCOMMIT_SCOPE_HEADER="$(__new_scope_header)"
149+
print_commit_msg "${commitMessage}"
150+
commitScope="$(echo "" | grep "x" |\
151+
fzf --height 10%\
152+
--info=hidden\
153+
--bind 'enter:print-query'\
154+
--header "${GCOMMIT_SCOPE_HEADER}")"
155+
156+
[ -z "${commitScope}" ] && return
157+
158+
if [[ ! -f "${COMMIT_SCOPE_FILE}" ]]; then
159+
echo "${commitScope}" > "${COMMIT_SCOPE_FILE}" # creates the file
160+
else
161+
# check if the scope is already present in the file
162+
! grep -Fxq "${commitScope}" "${COMMIT_SCOPE_FILE}" && {
163+
echo "${commitScope}" >> "${COMMIT_SCOPE_FILE}" # appends to the file
164+
}
165+
fi
166+
}
167+
150168
function get_commit_description() {
151169

152170
local GCOMMIT_DESCRIPTION_HEADER="$(__description_header)"

0 commit comments

Comments
 (0)