|
| 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 |
0 commit comments