Skip to content

Commit 673283b

Browse files
author
Bhavi Dhingra
committed
feat(gcommit): add new command gcommit
1 parent 719cfe7 commit 673283b

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

cmd/gcommit

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155
4+
#shellcheck disable=SC1091
5+
6+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
7+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/__gcommit_type_header
8+
9+
function main() {
10+
11+
__assertgitrepo
12+
13+
local numStagedFiles=$(git status --short | grep "^[A-Z]" -c)
14+
if [[ $numStagedFiles -eq 0 ]]; then
15+
local numUnstagedFiles=$(git status --short | grep "^.[A-Z?]" -c)
16+
if [[ $numUnstagedFiles -eq 0 ]]; then
17+
__print_info "Nothing to commit"
18+
return
19+
fi
20+
gstatus
21+
printf "\nCommit all the changes? (y/n): "
22+
read -r
23+
if [[ "${REPLY}" == "y" ]]; then
24+
git add -A
25+
fi
26+
fi
27+
gstatus
28+
29+
local isBreakingChange=false
30+
local commitType="$(get_commit_type "${isBreakingChange}")"
31+
case "${commitType}" in
32+
BREAKING-CHANGE)
33+
isBreakingChange=true
34+
commitType="$(get_commit_type "${isBreakingChange}")"
35+
;;
36+
"DEFINE NEW COMMIT TYPE")
37+
echo "Implement me"
38+
;;
39+
*)
40+
;;
41+
esac
42+
43+
# echo "${commitType}"
44+
local commitScope
45+
local commitScopeFile="./git-commit-scope.list"
46+
if [[ ! -f "${commitScopeFile}" ]]; then
47+
printf "commit scope (press <enter> to skip): "
48+
read -r commitScope
49+
else
50+
commitScope="$(fzf < "${commitScopeFile}")"
51+
fi
52+
53+
local commitDescription
54+
printf "commit description: "
55+
read -r commitDescription
56+
57+
local commitMessage="${commitType}"
58+
[ -n "${commitScope}" ] && commitMessage="${commitMessage}(${commitScope})"
59+
[ "${isBreakingChange}" == "true" ] && commitMessage="${commitMessage}!"
60+
commitMessage="${commitMessage}: ${commitDescription}"
61+
62+
git commit -m "${commitMessage}"
63+
}
64+
65+
# Usage: get_commit_type true false
66+
# $1: show "BREAKING-CHANGE" option
67+
# $2: show "ADD NEW TYPE" option
68+
function get_commit_type() {
69+
70+
local isBreakingChange="${1}"
71+
72+
local commitTypes=("feat" "fix" "perf" "refactor" "test" "style" "docs" "build" "ci" "cd")
73+
74+
[ "${isBreakingChange}" == "false" ] && {
75+
commitTypes=("${commitTypes[@]}" "BREAKING-CHANGE" "DEFINE NEW COMMIT TYPE")
76+
}
77+
78+
local GCOMMIT_TYPE_HEADER="$(__gcommit_type_header "${isBreakingChange}")"
79+
80+
local commitType="$(printf "%s\n" "${commitTypes[@]}" |\
81+
fzf --height=60% --preview-window :10%\
82+
--header "${GCOMMIT_TYPE_HEADER}"\
83+
--bind '?:toggle-preview'\
84+
--preview-window hidden\
85+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/__gcommit_type_preview; __gcommit_type_preview {}")"
86+
87+
echo "${commitType}"
88+
}
89+
90+
function get_commit_scope() {
91+
92+
local scope
93+
local commitScopeFile="./gcommit-scope.list"
94+
if [[ ! -f "${commitScopeFile}" ]]; then
95+
printf "Enter commit scope (press <enter> to skip): "
96+
read -r scope
97+
echo "${scope}"
98+
return
99+
fi
100+
101+
echo "${commitScopeFile} file exists"
102+
}
103+
104+
main

cmd/gstatus

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# shellcheck disable=SC1091
44

5-
source __assertgitrepo
5+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
66

77
__assertgitrepo
88

util/__assertgitrepo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# shellcheck disable=SC1091
44

5-
source __isgitrepo
5+
source "${__CUSTOM_GIT_UTIL}"/__isgitrepo
66

77
function __assertgitrepo() {
88

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 __gcommit_type_header() {
10+
11+
local isBreakingChange="${1}"
12+
13+
local msg="<enter>: select commit type, <?>: toggle type definition, <esc>: quit"
14+
local colormsg="${UNDERLINE}${MAGENTA}gcommit${RESET} follows ${BLUE}https://www.conventionalcommits.org/${RESET} \n\
15+
${BLUE}<enter>: ${RESET}select commit type, \
16+
${BLUE}<?>: ${RESET}toggle type definition, \
17+
${BLUE}<esc>: ${RESET}quit\n\n"
18+
19+
colormsg="${colormsg}Please select the commit type"
20+
if [[ "${isBreakingChange}" == "true" ]]; then
21+
colormsg="${colormsg} for the ${BLUE}BREAKING-CHANGE${RESET}"
22+
fi
23+
24+
local edge="$(echo "${msg}" | sed "s/./-/g")"
25+
local GCOMMIT_TYPE_HEADER="${edge}\n${colormsg}\n${edge}\n "
26+
echo -e "${GCOMMIT_TYPE_HEADER}"
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
function __gcommit_type_preview() {
4+
5+
local commit_type="${1}"
6+
case "${commit_type}" in
7+
feat) echo "A new feature"
8+
;;
9+
fix) echo "A bug fix"
10+
;;
11+
perf) echo "A code change that improves performance"
12+
;;
13+
refactor) echo "A code change that neither fixes a bug nor adds a feature"
14+
;;
15+
test) echo "Adding missing tests or correcting existing tests"
16+
;;
17+
style) echo "Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc)"
18+
;;
19+
docs) echo "Documentation only changes"
20+
;;
21+
build) echo "Changes that affect the build system or external dependencies"
22+
;;
23+
ci) echo "Changes to our CI configuration files and scripts"
24+
;;
25+
cd) echo "Changes to our CD configuration files and scripts"
26+
;;
27+
BREAKING-CHANGE) echo "Introduces a breaking API change"
28+
;;
29+
"DEFINE NEW COMMIT TYPE") echo "Define a new commit type"
30+
;;
31+
*) echo "Unknown commit type"
32+
;;
33+
esac
34+
}

0 commit comments

Comments
 (0)