Skip to content

Commit b366fab

Browse files
author
Bhavi Dhingra
committed
feat(gbranchout): add new command for creating new branches with formatted names
1 parent b28ca87 commit b366fab

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed

cmd/gbranchout

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155
4+
#shellcheck disable=SC1091
5+
6+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
7+
source "${__CUSTOM_GIT_UTIL}"/__ask
8+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranch/__type_header
9+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranch/__prefix_header
10+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/gbranch/__name_header
11+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_info
12+
source "${__CUSTOM_CONSOLE_UTIL}"/__print_as
13+
14+
SUCCESS=0
15+
FAILURE=1
16+
17+
function main() {
18+
19+
__assertgitrepo
20+
21+
local branchName=""
22+
print_branch_name "${branchName}"
23+
24+
local prefix="$(get_prefix)"
25+
[ -n "${prefix}" ] && branchName="${prefix}/"
26+
print_branch_name "${branchName}"
27+
28+
local branchType="$(get_branch_type)"
29+
[ -z "${branchType}" ] && {
30+
__print_info "branch type can't be empty"
31+
return
32+
}
33+
branchName="${branchName}${branchType}/"
34+
print_branch_name "${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+
print_branch_name "${branchName}"
43+
44+
printf "\r"
45+
46+
local confirmation="$(__ask "Create branch \"${branchName}\" ?")"
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 \"${branchName}\"?")"
57+
[ "${switch}" == "yes" ] && git checkout "${branchName}"
58+
}
59+
60+
function get_prefix() {
61+
62+
local GBRANCH_PREFIX_HEADER="$(__prefix_header)"
63+
local prefix="$(echo "" | grep "x" |\
64+
fzf --height 10%\
65+
--info=hidden\
66+
--bind 'enter:print-query'\
67+
--header "${GBRANCH_PREFIX_HEADER}")"
68+
echo "${prefix}"
69+
}
70+
71+
function get_branch_type() {
72+
73+
local GBRANCH_TYPE_HEADER="$(__type_header)"
74+
local branchTypes=("feat" "fix" "perf" "refactor" "test" "style" "docs" "build" "ci" "cd")
75+
76+
local branchType="$(printf "%s\n" "${branchTypes[@]}" |\
77+
fzf --height=60% --preview-window :10%\
78+
--header "${GBRANCH_TYPE_HEADER}"\
79+
--bind '?:toggle-preview'\
80+
--preview-window hidden\
81+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/__gcommit_type_preview; __gcommit_type_preview {}")"
82+
83+
echo "${branchType}"
84+
}
85+
86+
function get_branch_name() {
87+
88+
local GBRANCH_NAME_HEADER="$(__name_header)"
89+
local name="$(echo "" | grep "x" |\
90+
fzf --height 10%\
91+
--info=hidden\
92+
--bind 'enter:print-query'\
93+
--header "${GBRANCH_NAME_HEADER}")"
94+
echo "${name}"
95+
}
96+
97+
function print_branch_name() {
98+
__print_as "bold" "FORMATED BRANCH NAME:- "
99+
echo -n "${1}"
100+
}
101+
102+
main

git-commit-scope.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
gcommit
22
gadd
3+
gbranchout

util/__ask

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 options=("yes" "no")
12+
13+
local ASK_HEADER="$(__ask_header "${question}")"
14+
local choice="$(printf "%s\n" "${options[@]}" |\
15+
fzf --height 10%\
16+
--info=hidden\
17+
--header "${ASK_HEADER}")"
18+
echo "${choice}"
19+
}

util/fzf_headers/__ask_header

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+
#shellcheck disable=SC2001
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __ask_header() {
10+
11+
local question="${1}"
12+
13+
local msg="${question}"
14+
local edge="$(echo "${msg}" | sed "s/./-/g")"
15+
16+
local colorMsg="${BLUE}<enter>: ${RESET}Select an option, "
17+
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}exit\n\n"
18+
colorMsg="${colorMsg}${question}"
19+
ASK_HEADER="${edge}\n${colorMsg}\n${edge}\n"
20+
21+
echo -e "${ASK_HEADER}"
22+
}
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+
#shellcheck disable=SC2001
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __name_header() {
10+
11+
local msg="(optional) Give a prefix to the branch name (e.g. issue number / JIRA ticket etc.)"
12+
local edge="$(echo "${msg}" | sed "s/./-/g")"
13+
local GBRANCH_NAME_HEADER="${edge}\n"
14+
15+
local colorMsg="${UNDERLINE}${MAGENTA}gbranch${RESET} follows the format ${BLUE}[optional prefix]/<type>/<branch_name>${RESET} \n"
16+
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Creates the new branch, "
17+
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}exit\n\n"
18+
colorMsg="${colorMsg}What is the branch name?"
19+
GBRANCH_NAME_HEADER="${GBRANCH_NAME_HEADER}${colorMsg}"
20+
21+
echo -e "${GBRANCH_NAME_HEADER}"
22+
}
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+
#shellcheck disable=SC2001
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __prefix_header() {
10+
11+
local msg="(optional) Give a prefix to the branch name (e.g. issue number / JIRA ticket etc.)"
12+
local edge="$(echo "${msg}" | sed "s/./-/g")"
13+
local GBRANCH_PREFIX_HEADER="${edge}\n"
14+
15+
local colorMsg="${UNDERLINE}${MAGENTA}gbranch${RESET} follows the format ${BLUE}[optional prefix]/<type>/<branch_name>${RESET} \n"
16+
colorMsg="${colorMsg}${BLUE}<enter>: ${RESET}Add the prefix to the branch name, "
17+
colorMsg="${colorMsg}${BLUE}<esc>: ${RESET}skip\n\n"
18+
colorMsg="${colorMsg}(optional) Give a prefix to the branch name (e.g. issue number / JIRA ticket etc.)"
19+
GBRANCH_PREFIX_HEADER="${GBRANCH_PREFIX_HEADER}${colorMsg}"
20+
21+
echo -e "${GBRANCH_PREFIX_HEADER}"
22+
}
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 __type_header() {
10+
11+
local msg="<enter>: select branch type, <?>: toggle type definition, <esc>: quit"
12+
13+
local colormsg="${UNDERLINE}${MAGENTA}gbranch${RESET} follows the format ${BLUE}[optional prefix]/<type>/<branch_name>${RESET} \n\
14+
${BLUE}<enter>: ${RESET}select branch type, \
15+
${BLUE}<?>: ${RESET}toggle type definition, \
16+
${BLUE}<esc>: ${RESET}exit\n\n"
17+
18+
colormsg="${colormsg}Select the type of branch"
19+
20+
local edge="$(echo "${msg}" | sed "s/./-/g")"
21+
local GBRANCH_TYPE_HEADER="${edge}\n${colormsg}\n${edge}\n "
22+
echo -e "${GBRANCH_TYPE_HEADER}"
23+
}

0 commit comments

Comments
 (0)