Simple Git & GitHub CLI Shell Scripts

refactor[gbd]: Cleaner code

+103 -94
+103 -94
gh_scripts/gbd.sh
··· 17 17 if [ $# -eq 1 ]; then 18 18 if [ "$1" = "$default_branch" ]; then 19 19 echo "${BOLD} Fatal ! Cannot Delete the Default Branch " 20 - elif ! git show-ref --verify --quiet "refs/heads/$1" &>/dev/null; then 20 + return 0 21 + fi 22 + 23 + if ! git show-ref --verify --quiet "refs/heads/$1" &>/dev/null; then 21 24 echo "${BOLD} Fatal ! Branch ${GREEN}$1 ${RESET_COLOR}doesn't exist ${RESET}" 22 - else 23 - # this to check if we want to delete the remote branch too 24 - check_delete_remote_branch() { 25 - if [ "$current_branch" = "$default_branch" ]; then 26 - echo "${BOLD} Fatal ! Cannot Delete the Default Branch " 27 - else 28 - printf "${BOLD}${RESET_COLOR}Delete remote branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 29 - read delete_remote_branch 30 - echo ${RESET} 31 - if [ "$delete_remote_branch" = "y" ]; then 32 - git push origin --delete "$current_branch" 33 - elif [ "$delete_remote_branch" = "n" ]; then 34 - return 0 35 - else 36 - check_delete_remote_branch 37 - fi 38 - fi 39 - } 25 + return 0 26 + fi 40 27 41 - check_delete_branch() { 42 - branch_name="$1" 28 + # this to check if we want to delete the remote branch too 29 + check_delete_remote_branch() { 30 + if [ "$current_branch" = "$default_branch" ]; then 31 + echo "${BOLD} Fatal ! Cannot Delete the Default Branch " 32 + return 0 33 + fi 34 + 35 + printf "${BOLD}${RESET_COLOR}Delete remote branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 36 + read delete_remote_branch 37 + echo ${RESET} 38 + 39 + if [ "$delete_remote_branch" = "y" ]; then 40 + git push origin --delete "$current_branch" 41 + elif [ "$delete_remote_branch" = "n" ]; then 42 + return 0 43 + else 44 + check_delete_remote_branch 45 + fi 46 + } 43 47 44 - printf "${BOLD}${RESET_COLOR}Delete branch${GREEN} "$branch_name"${RESET_COLOR} ? (y/n) ${RESET}" 45 - read delete_branch 48 + check_delete_branch() { 49 + branch_name="$1" 50 + 51 + printf "${BOLD}${RESET_COLOR}Delete branch${GREEN} "$branch_name"${RESET_COLOR} ? (y/n) ${RESET}" 52 + read delete_branch 46 53 47 - if [ "$delete_branch" = "y" ]; then 48 - if [ "$current_branch" != "$default_branch" ]; then 49 - git checkout $default_branch >/dev/null 2>&1 50 - fi 54 + if [ "$delete_branch" = "y" ]; then 55 + if [ "$current_branch" != "$default_branch" ]; then 56 + git checkout $default_branch >/dev/null 2>&1 57 + fi 51 58 52 - if has_remote; then 53 - repo_url=$(git config --get remote.origin.url) 54 - repo_owner=$(echo "$repo_url" | awk -F '[/:]' '{print $(NF-1)}') 59 + if has_remote; then 60 + repo_url=$(git config --get remote.origin.url) 61 + repo_owner=$(echo "$repo_url" | awk -F '[/:]' '{print $(NF-1)}') 55 62 56 - # check if we are not the owner of the repo 57 - if [ "$repo_owner" == "$current_user" ]; then 58 - is_remote_branch=$(git branch -r | grep "origin/$1") 59 - if [ -n "$is_remote_branch" ]; then 60 - # prompt for sudo 61 - # password if required 62 - allow_sudo 63 + # check if we are not the owner of the repo 64 + if [ "$repo_owner" == "$current_user" ]; then 65 + is_remote_branch=$(git branch -r | grep "origin/$1") 66 + if [ -n "$is_remote_branch" ]; then 67 + # prompt for sudo 68 + # password if required 69 + allow_sudo 63 70 64 - # Check for internet connectivity to GitHub 65 - if $SUDO ping -c 1 github.com &>/dev/null; then 66 - check_delete_remote_branch 67 - fi 71 + # Check for internet connectivity to GitHub 72 + if $SUDO ping -c 1 github.com &>/dev/null; then 73 + check_delete_remote_branch 68 74 fi 69 75 fi 70 - fi 71 - git branch -D "$1" 72 - elif [ "$delete_branch" = "n" ]; then 73 - return 0 74 - else 75 - check_delete_branch $branch_name 76 + fi 76 77 fi 77 - } 78 - check_delete_branch $1 79 - fi 78 + 79 + git branch -D "$1" 80 + elif [ "$delete_branch" = "n" ]; then 81 + return 0 82 + else 83 + check_delete_branch $branch_name 84 + fi 85 + } 86 + 87 + check_delete_branch $1 80 88 elif [ $# -eq 0 ]; then 81 89 if [ "$current_branch" = "$default_branch" ]; then 82 90 echo "${BOLD}${RESET_COLOR} Fatal ! Cannot Delete the Default Branch " 83 - else 84 - check_delete_branch() { 85 - printf "${BOLD}${RESET_COLOR}Delete branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 86 - read delete_branch 87 - if [ "$delete_branch" = "y" ]; then 88 - # TODO : Remote branch Deletion 89 - check_delete_remote_branch() { 90 - if [ "$current_branch" = "$default_branch" ]; then 91 - echo "${BOLD}${RESET_COLOR} Fatal ! Cannot Delete the Default Branch " 91 + return 0 92 + fi 93 + 94 + check_delete_branch() { 95 + printf "${BOLD}${RESET_COLOR}Delete branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 96 + read delete_branch 97 + if [ "$delete_branch" = "y" ]; then 98 + # TODO : Remote branch Deletion 99 + check_delete_remote_branch() { 100 + if [ "$current_branch" = "$default_branch" ]; then 101 + echo "${BOLD}${RESET_COLOR} Fatal ! Cannot Delete the Default Branch " 102 + else 103 + printf "${BOLD}${RESET_COLOR}Delete remote branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 104 + read delete_remote_branch 105 + echo ${RESET} 106 + if [ "$delete_remote_branch" = "y" ]; then 107 + git push origin --delete "$current_branch" 108 + elif [ "$delete_remote_branch" = "n" ]; then 109 + return 0 92 110 else 93 - printf "${BOLD}${RESET_COLOR}Delete remote branch${GREEN} "$current_branch"${RESET_COLOR} ? (y/n) ${RESET}" 94 - read delete_remote_branch 95 - echo ${RESET} 96 - if [ "$delete_remote_branch" = "y" ]; then 97 - git push origin --delete "$current_branch" 98 - elif [ "$delete_remote_branch" = "n" ]; then 99 - return 0 100 - else 101 - check_delete_remote_branch 102 - fi 111 + check_delete_remote_branch 103 112 fi 104 - } 113 + fi 114 + } 105 115 106 - git checkout "$default_branch" >/dev/null 2>&1 116 + git checkout "$default_branch" >/dev/null 2>&1 107 117 108 - if has_remote; then 109 - repo_url=$(git config --get remote.origin.url) 110 - repo_owner=$(echo "$repo_url" | awk -F '[/:]' '{print $(NF-1)}') 118 + if has_remote; then 119 + repo_url=$(git config --get remote.origin.url) 120 + repo_owner=$(echo "$repo_url" | awk -F '[/:]' '{print $(NF-1)}') 111 121 112 - # check if we are not the owner of the repo 113 - if [ "$repo_owner" == "$current_user" ]; then 114 - is_remote_branch=$(git branch -r | grep "origin/$current_branch") 115 - 116 - if [ -n "$is_remote_branch" ]; then 117 - # prompt for sudo 118 - # password if required 119 - allow_sudo 122 + # check if we are not the owner of the repo 123 + if [ "$repo_owner" == "$current_user" ]; then 124 + is_remote_branch=$(git branch -r | grep "origin/$current_branch") 125 + 126 + if [ -n "$is_remote_branch" ]; then 127 + # prompt for sudo 128 + # password if required 129 + allow_sudo 120 130 121 - # Check for internet connectivity to GitHub 122 - if $SUDO ping -c 1 github.com &>/dev/null; then 123 - check_delete_remote_branch 124 - fi 131 + # Check for internet connectivity to GitHub 132 + if $SUDO ping -c 1 github.com &>/dev/null; then 133 + check_delete_remote_branch 125 134 fi 126 135 fi 127 136 fi 128 - git branch -D "$current_branch" 129 - elif [ "$delete_branch" = "n" ]; then 130 - return 0 131 - else 132 - check_delete_branch 133 137 fi 134 - } 135 - check_delete_branch 136 - fi 138 + git branch -D "$current_branch" 139 + elif [ "$delete_branch" = "n" ]; then 140 + return 0 141 + else 142 + check_delete_branch 143 + fi 144 + } 145 + check_delete_branch 137 146 else 138 147 echo "${BOLD}${RESET_COLOR} Usage : gbd branch_to_delete" 139 148 fi