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