Skip to content

Commit 02e0734

Browse files
author
Bhavi Dhingra
committed
Merge branch 'release-0.3'
2 parents 291d281 + 3ef8b01 commit 02e0734

20 files changed

+686
-3
lines changed

cmd/gadd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ source "$__CUSTOM_CONSOLE_UTIL"/__print_info
1111
source "$__CUSTOM_CONSOLE_UTIL"/__print_err
1212
source "$__CUSTOM_CONSOLE_UTIL"/__split_str
1313

14+
# ensure the command is run in a git repo
15+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
16+
1417
function main() {
1518

19+
__assertgitrepo
20+
1621
local numUnstagedFiles=$(git status --short | grep "^.[A-Z?] " -c)
1722
if [[ $numUnstagedFiles -eq 0 ]]; then
1823
gstatus
@@ -74,10 +79,10 @@ if [[ $# -eq 0 ]]; then
7479
exit 0
7580
fi
7681

77-
if [[ $# -eq 1 && ( "$*" == "." || "$*" == "-A") ]]; then
82+
if [[ $# -eq 1 && ( "$*" == "." || "$*" == "-A" ) ]]; then
7883
git add "$*"
7984
gstatus
8085
exit 0
8186
fi
8287

83-
__print_err "valid inputs to gadd: \".\", \"-A\""
88+
__print_err "valid inputs to gadd: \".\", \"-A\""

cmd/gbranchcreate

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155
4+
#shellcheck disable=SC1091
5+
#shellcheck disable=SC2059
6+
7+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
8+
source "${__CUSTOM_GIT_UTIL}"/__ask
9+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranchcreate/__type_header
10+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranchcreate/__prefix_header
11+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranchcreate/__name_header
12+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_info
13+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_as
14+
source "${__CUSTOM_CONSOLE_UTIL}"/__common
15+
16+
SUCCESS=0
17+
FAILURE=1
18+
19+
function main() {
20+
21+
__assertgitrepo
22+
23+
echo
24+
25+
local branchName=""
26+
printf "${branchName}"
27+
28+
local prefix="$(get_prefix)"
29+
[ -n "${prefix}" ] && branchName="${prefix}/"
30+
printf "${branchName}"
31+
32+
local branchType="$(get_branch_type)"
33+
[ -n "${branchType}" ] && branchName="${branchName}${branchType}/"
34+
printf "${branchName}"
35+
36+
local name="$(get_branch_name)"
37+
[ -z "${name}" ] && {
38+
__print_info "branch name can't be empty"
39+
return
40+
}
41+
branchName="${branchName}${name}"
42+
printf "${branchName}"
43+
44+
printf "\r"
45+
46+
local confirmation="$(__ask "Create branch ${BLUE}${branchName}${RESET} ?")"
47+
[ "${confirmation}" != "yes" ] && return
48+
49+
git branch "${branchName}"
50+
git push --set-upstream origin "${branchName}"
51+
if (( $? != SUCCESS )); then
52+
__print_err "Some error occurred while pushing the new branch to upstream. Please check your network."
53+
exit ${FAILURE}
54+
fi
55+
56+
local switch="$(__ask "Switch to the new branch ${BLUE}${branchName}${RESET} ?")"
57+
[ "${switch}" == "yes" ] && git checkout "${branchName}"
58+
}
59+
60+
function get_prefix() {
61+
62+
local PREFIX_HEADER="$(__prefix_header)"
63+
local prefix="$(echo "" | grep "x" |\
64+
fzf --height 40%\
65+
--info=hidden\
66+
--bind 'enter:print-query'\
67+
--header "${PREFIX_HEADER}"\
68+
--bind '?:toggle-preview'\
69+
--preview-window hidden\
70+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gbranchcreate/__prefix_preview;
71+
__prefix_preview")"
72+
echo "${prefix}"
73+
}
74+
75+
function get_branch_type() {
76+
77+
local TYPE_HEADER="$(__type_header)"
78+
local branchTypes=("feat" "fix" "perf" "refactor" "test" "style" "docs" "build" "ci" "cd")
79+
80+
local branchType="$(printf "%s\n" "${branchTypes[@]}" |\
81+
fzf --height=70% --preview-window :30%\
82+
--info=hidden\
83+
--header "${TYPE_HEADER}"\
84+
--bind '?:toggle-preview'\
85+
--preview-window hidden\
86+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gbranchcreate/__type_preview;
87+
__type_preview {}")"
88+
89+
echo "${branchType}"
90+
}
91+
92+
function get_branch_name() {
93+
94+
local NAME_HEADER="$(__name_header)"
95+
local name="$(echo "" | grep "x" |\
96+
fzf --height 35%\
97+
--info=hidden\
98+
--bind 'enter:print-query'\
99+
--header "${NAME_HEADER}"\
100+
--bind '?:toggle-preview'\
101+
--preview-window hidden\
102+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gbranchcreate/__name_preview;
103+
__name_preview")"
104+
echo "${name}"
105+
}
106+
107+
main

cmd/gcommit

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155
4+
#shellcheck disable=SC1091
5+
#shellcheck disable=SC2059
6+
7+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
8+
source "${__CUSTOM_GIT_UTIL}"/__ask
9+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__type_header
10+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__scope_header
11+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__new_scope_header
12+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gcommit/__description_header
13+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_info
14+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_as
15+
source "${__CUSTOM_CONSOLE_UTIL}"/__println_as
16+
source "${__CUSTOM_CONSOLE_UTIL}"/__common
17+
18+
__assertgitrepo
19+
20+
REPO_PATH="$(git rev-parse --show-toplevel)"
21+
COMMIT_SCOPE_FILE="${REPO_PATH}/.gitcommitscopes"
22+
ADD_NEW_SCOPE="ADD NEW SCOPE"
23+
# DELETE_SCOPES="DELETE SCOPE(s) FROM THE LIST"
24+
25+
function main() {
26+
27+
local numStagedFiles=$(git status --short | grep "^[A-Z]" -c)
28+
if [[ $numStagedFiles -eq 0 ]]; then
29+
local numUnstagedFiles=$(git status --short | grep "^.[A-Z?]" -c)
30+
if [[ $numUnstagedFiles -eq 0 ]]; then
31+
__print_info "Nothing to commit"
32+
return
33+
fi
34+
gstatus
35+
36+
local commitAll="$(__ask "Stage all the files?")"
37+
if [[ "${commitAll}" == "yes" ]]; then
38+
git add -A
39+
else
40+
echo
41+
__print_info "Stage the files to commit"
42+
return
43+
fi
44+
fi
45+
gstatus
46+
47+
local commitMessage=""
48+
49+
# get commit type
50+
printf "${commitMessage}"
51+
local isBreakingChange=false
52+
local commitType="$(get_commit_type "${isBreakingChange}")"
53+
if [[ "${commitType}" == "BREAKING-CHANGE" ]]; then
54+
printf "${commitMessage}"
55+
isBreakingChange=true
56+
commitType="$(get_commit_type "${isBreakingChange}")"
57+
fi
58+
[ -z "${commitType}" ] && {
59+
__print_info "commit type can't be empty"
60+
return
61+
}
62+
commitMessage="${commitType}"
63+
64+
# get commit scope
65+
local commitScope=""
66+
while true; do
67+
printf "${commitMessage}"
68+
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
77+
done
78+
[ -n "${commitScope}" ] && commitMessage="${commitMessage}(${commitScope})"
79+
80+
[ "${isBreakingChange}" == "true" ] && commitMessage="${commitMessage}!"
81+
82+
commitMessage="${commitMessage}: "
83+
84+
# get commit description
85+
printf "${commitMessage}"
86+
local commitDescription="$(get_commit_description "${isBreakingChange}")"
87+
[ -z "${commitDescription}" ] && {
88+
__print_info "commit description can't be empty"
89+
return
90+
}
91+
commitMessage="${commitMessage}${commitDescription}"
92+
93+
clear
94+
git commit -m "${commitMessage}"
95+
git log
96+
97+
echo
98+
echo "To add the commit body/footer or edit the commit message, do "
99+
echo -e " $ git commit --amend\n"
100+
}
101+
102+
# Usage: get_commit_type true
103+
# $1 bool: isBreakingChange
104+
function get_commit_type() {
105+
106+
local isBreakingChange="${1}"
107+
108+
local commitTypes=("feat" "fix" "perf" "refactor" "test" "style" "docs" "build" "ci" "cd")
109+
110+
local previewVisibility="nohidden"
111+
[ "${isBreakingChange}" == "false" ] && {
112+
commitTypes=("${commitTypes[@]}" "BREAKING-CHANGE")
113+
previewVisibility="hidden"
114+
}
115+
116+
local GCOMMIT_TYPE_HEADER="$(__type_header)"
117+
118+
local commitType="$(printf "%s\n" "${commitTypes[@]}" |\
119+
fzf --height=70% --preview-window :35%\
120+
--info=hidden\
121+
--header "${GCOMMIT_TYPE_HEADER}"\
122+
--bind '?:toggle-preview'\
123+
--preview-window ${previewVisibility}\
124+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gcommit/__type_preview;
125+
__type_preview {} ${isBreakingChange}")"
126+
127+
echo "${commitType}"
128+
}
129+
130+
function get_commit_scope() {
131+
132+
local isBreakingChange="${1}"
133+
134+
local commitScopes=("${ADD_NEW_SCOPE}")
135+
local commitScope=""
136+
if [[ -f "${COMMIT_SCOPE_FILE}" ]]; then
137+
while IFS= read -r commitScope; do
138+
commitScopes=("${commitScopes[@]}" "${commitScope}")
139+
done < "${COMMIT_SCOPE_FILE}"
140+
fi
141+
142+
local GCOMMIT_SCOPE_HEADER="$(__scope_header)"
143+
commitScope="$(printf "%s\n" "${commitScopes[@]}" |\
144+
fzf --height=65% --preview-window :35%\
145+
--header "${GCOMMIT_SCOPE_HEADER}"\
146+
--bind '?:toggle-preview'\
147+
--preview-window hidden\
148+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gcommit/__scope_preview;
149+
__scope_preview ${isBreakingChange}")"
150+
151+
echo "${commitScope}"
152+
}
153+
154+
function add_new_scope() {
155+
156+
local isBreakingChange="${1}"
157+
158+
local GCOMMIT_SCOPE_HEADER="$(__new_scope_header)"
159+
printf "${commitMessage}"
160+
commitScope="$(echo "" | grep "x" |\
161+
fzf --height 1%\
162+
--info=hidden\
163+
--bind 'enter:print-query'\
164+
--header "${GCOMMIT_SCOPE_HEADER}")"
165+
166+
[ -z "${commitScope}" ] && return
167+
168+
if [[ ! -f "${COMMIT_SCOPE_FILE}" ]]; then
169+
echo "${commitScope}" > "${COMMIT_SCOPE_FILE}" # creates the file
170+
else
171+
# check if the scope is already present in the file
172+
! grep -Fxq "${commitScope}" "${COMMIT_SCOPE_FILE}" && {
173+
echo "${commitScope}" >> "${COMMIT_SCOPE_FILE}" # appends to the file
174+
}
175+
fi
176+
sort "${COMMIT_SCOPE_FILE}" > "${COMMIT_SCOPE_FILE}_sorted"
177+
mv "${COMMIT_SCOPE_FILE}_sorted" "${COMMIT_SCOPE_FILE}"
178+
}
179+
180+
function get_commit_description() {
181+
182+
local isBreakingChange="${1}"
183+
184+
local GCOMMIT_DESCRIPTION_HEADER="$(__description_header)"
185+
local commitDescription="$(echo "" | grep "x" |\
186+
fzf --height 40%\
187+
--info=hidden\
188+
--bind 'enter:print-query'\
189+
--header "${GCOMMIT_DESCRIPTION_HEADER}"\
190+
--bind '?:toggle-preview'\
191+
--preview-window hidden\
192+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/gcommit/__description_preview;
193+
__description_preview ${isBreakingChange}")"
194+
echo "${commitDescription}"
195+
}
196+
197+
main

cmd/gstatus

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#!/usr/bin/env bash
22

3+
# shellcheck disable=SC1091
4+
5+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
6+
7+
__assertgitrepo
8+
39
clear
410

511
if [[ $# -eq 0 ]]; then
612
git status
713
else
814
git status "$*"
9-
fi
15+
fi

util/__ask

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# shellcheck disable=SC1091
4+
# shellcheck disable=SC2155
5+
6+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/__ask_header
7+
8+
function __ask() {
9+
10+
local question="${1}"
11+
local yesFirst="${2:-"true"}"
12+
local escAction="${3:-"exit"}"
13+
local options=("yes" "no")
14+
[ "${yesFirst}" == "false" ] && options=("no" "yes")
15+
16+
local ASK_HEADER="$(__ask_header "${question}" "${escAction}")"
17+
local choice="$(printf "%s\n" "${options[@]}" |\
18+
fzf --height 10%\
19+
--info=hidden\
20+
--header "${ASK_HEADER}")"
21+
echo "${choice}"
22+
}

util/__assertgitrepo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck disable=SC1091
4+
5+
source "${__CUSTOM_GIT_UTIL}"/__isgitrepo
6+
7+
function __assertgitrepo() {
8+
9+
if [[ $(__isgitrepo) == false ]]; then
10+
echo "[AssertionError] should be a git repo."
11+
exit 1
12+
fi
13+
}

0 commit comments

Comments
 (0)