Simple Git & GitHub CLI Shell Scripts

fix[ghc]: remote add origin depends on gh protocol in use

+113 -107
+113 -107
gh_scripts/ghc.sh
··· 1 1 #!/bin/bash 2 2 3 3 function ghc { 4 - # Get the repo name and visibility 5 - if [ $# -eq 0 ]; then 6 - repo="$(basename "$PWD")" 7 - repo_visibility="public" 8 - elif [ $# -eq 1 ]; then 9 - if [ "$1" = "private" ]; then 10 - repo="$(basename "$PWD")" 11 - repo_visibility="private" 12 - else 13 - repo="$1" 14 - repo_visibility="public" 15 - fi 16 - elif [ $# -eq 2 ]; then 17 - repo="$1" 18 - repo_visibility="$2" 19 - else 20 - echo "${BOLD}${RED}Error: Too many arguments.${RESET}" 21 - usage 22 - fi 4 + # Get the repo name and visibility 5 + if [ $# -eq 0 ]; then 6 + repo="$(basename "$PWD")" 7 + repo_visibility="public" 8 + elif [ $# -eq 1 ]; then 9 + if [ "$1" = "private" ]; then 10 + repo="$(basename "$PWD")" 11 + repo_visibility="private" 12 + else 13 + repo="$1" 14 + repo_visibility="public" 15 + fi 16 + elif [ $# -eq 2 ]; then 17 + repo="$1" 18 + repo_visibility="$2" 19 + else 20 + echo "${BOLD}${RED}Error: Too many arguments.${RESET}" 21 + usage 22 + fi 23 23 24 - # Clean the repo name 25 - repo_name=$(clean_repo "$repo") 24 + # Clean the repo name 25 + repo_name=$(clean_repo "$repo") 26 26 27 - if is_a_git_repo; then 28 - if has_remote; then 29 - printf "${BOLD} This repo already has a remote on GitHub!${RESET}\n" 30 - return 0 31 - fi 32 - 33 - current_user=$(awk '/user:/ {print $2; exit}' ~/.config/gh/hosts.yml) 27 + if is_a_git_repo; then 28 + if has_remote; then 29 + printf "${BOLD} This repo already has a remote on GitHub!${RESET}\n" 30 + return 0 31 + fi 34 32 35 - check_set_repo() { 36 - printf "${BOLD}${RESET_COLOR} Create ${GREEN}$repo_visibility ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 37 - read set_repo 38 - if [ "$set_repo" = "y" ]; then 39 - # Create the repo & set it as remote of the local one 40 - printf "${BOLD} New repository ${LIGHT_BLUE}$repo_name ${RESET_COLOR}on GitHub ... ${RESET}" 41 - gh repo create "$repo_name" --"$repo_visibility" &>/dev/null 42 - git remote add origin "git@github.com:$current_user/$repo_name.git" 43 - printf "${BOLD}${GREEN} ${RESET}\n" 33 + current_user=$(awk '/user:/ {print $2; exit}' ~/.config/gh/hosts.yml) 34 + current_protocol=$(grep 'git_protocol:' ~/.config/gh/hosts.yml | awk '{print $2}') 35 + origin_base_url=$( 36 + [[ "$current_protocol" == "ssh" ]] && 37 + echo "git@github.com:" || 38 + echo "https://github.com/" 39 + ) 44 40 45 - check_push() { 46 - printf "${BOLD}${RESET_COLOR} Push local commits to ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 47 - read check_push_commit 41 + check_set_repo() { 42 + printf "${BOLD}${RESET_COLOR} Create ${GREEN}$repo_visibility ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 43 + read set_repo 44 + if [ "$set_repo" = "y" ]; then 45 + # Create the repo & set it as remote of the local one 46 + printf "${BOLD} New repository ${LIGHT_BLUE}$repo_name ${RESET_COLOR}on GitHub ... ${RESET}" 47 + gh repo create "$repo_name" --"$repo_visibility" &>/dev/null 48 + git remote add origin "$origin_base_url$current_user/$repo_name.git" 49 + printf "${BOLD}${GREEN} ${RESET}\n" 48 50 49 - if [ "$check_push_commit" = "y" ]; then 50 - current_branch=$(git branch | awk '/\*/ {print $2}') 51 - git push origin "$current_branch" 52 - elif [ "$check_push_commit" = "n" ]; then 53 - return 0 54 - else 55 - check_push 56 - fi 57 - } 51 + check_push() { 52 + printf "${BOLD}${RESET_COLOR} Push local commits to ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 53 + read check_push_commit 58 54 59 - current_branch=$(git branch | awk '/\*/ {print $2}') 55 + if [ "$check_push_commit" = "y" ]; then 56 + current_branch=$(git branch | awk '/\*/ {print $2}') 57 + git push origin "$current_branch" 58 + elif [ "$check_push_commit" = "n" ]; then 59 + return 0 60 + else 61 + check_push 62 + fi 63 + } 60 64 61 - if git rev-list --count "$current_branch" 2>/dev/null | grep -q '^[1-9]'; then 62 - check_push 63 - fi 64 - elif [ "$set_repo" = "n" ]; then 65 - return 0 66 - else 67 - check_set_repo 68 - fi 69 - } 70 - check_set_repo 71 - else 72 - # Check for internet connectivity to GitHub 73 - if ! connected; then 74 - echo "${BOLD} Sorry, you are offline !${RESET}" 75 - check_local() { 76 - printf "${BOLD}${RESET_COLOR} Create ${GREEN}local ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 77 - read create_local 65 + current_branch=$(git branch | awk '/\*/ {print $2}') 78 66 79 - if [ "$create_local" = "y" ]; then 80 - git init &>/dev/null 81 - elif [ "$create_local" = "n" ]; then 82 - return 0 83 - else 84 - check_local 85 - fi 86 - } 87 - check_local 88 - else 89 - check_create_repo() { 90 - printf "${BOLD}${RESET_COLOR} Create ${GREEN}$repo_visibility ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 91 - read create_repo 92 - if [ "$create_repo" = "y" ]; then 93 - # Create the repo & clone it locally 94 - printf "${BOLD} New repository ${LIGHT_BLUE}$repo_name ${RESET_COLOR}on GitHub ... ${RESET}" 95 - gh repo create "$repo_name" --"$repo_visibility" -c &>/dev/null 96 - mv "$repo_name/.git" . && rm -rf "$repo_name" 97 - printf "${BOLD}${GREEN} ${RESET}\n" 98 - elif [ "$create_repo" = "n" ]; then 99 - check_local() { 100 - printf "${BOLD}${RESET_COLOR} Create ${GREEN}local ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 101 - read create_local 67 + if git rev-list --count "$current_branch" 2>/dev/null | grep -q '^[1-9]'; then 68 + check_push 69 + fi 70 + elif [ "$set_repo" = "n" ]; then 71 + return 0 72 + else 73 + check_set_repo 74 + fi 75 + } 76 + check_set_repo 77 + else 78 + # Check for internet connectivity to GitHub 79 + if ! connected; then 80 + echo "${BOLD} Sorry, you are offline !${RESET}" 81 + check_local() { 82 + printf "${BOLD}${RESET_COLOR} Create ${GREEN}local ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 83 + read create_local 102 84 103 - if [ "$create_local" = "y" ]; then 104 - git init &>/dev/null 105 - elif [ "$create_local" = "n" ]; then 106 - return 0 107 - else 108 - check_local 109 - fi 110 - } 111 - check_local 112 - else 113 - check_create_repo 114 - fi 115 - } 116 - check_create_repo 117 - fi 118 - fi 85 + if [ "$create_local" = "y" ]; then 86 + git init &>/dev/null 87 + elif [ "$create_local" = "n" ]; then 88 + return 0 89 + else 90 + check_local 91 + fi 92 + } 93 + check_local 94 + else 95 + check_create_repo() { 96 + printf "${BOLD}${RESET_COLOR} Create ${GREEN}$repo_visibility ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 97 + read create_repo 98 + if [ "$create_repo" = "y" ]; then 99 + # Create the repo & clone it locally 100 + printf "${BOLD} New repository ${LIGHT_BLUE}$repo_name ${RESET_COLOR}on GitHub ... ${RESET}" 101 + gh repo create "$repo_name" --"$repo_visibility" -c &>/dev/null 102 + mv "$repo_name/.git" . && rm -rf "$repo_name" 103 + printf "${BOLD}${GREEN} ${RESET}\n" 104 + elif [ "$create_repo" = "n" ]; then 105 + check_local() { 106 + printf "${BOLD}${RESET_COLOR} Create ${GREEN}local ${RESET_COLOR}repo ${LIGHT_BLUE}$repo_name ${RESET_COLOR}? (y/n) ${RESET}" 107 + read create_local 108 + 109 + if [ "$create_local" = "y" ]; then 110 + git init &>/dev/null 111 + elif [ "$create_local" = "n" ]; then 112 + return 0 113 + else 114 + check_local 115 + fi 116 + } 117 + check_local 118 + else 119 + check_create_repo 120 + fi 121 + } 122 + check_create_repo 123 + fi 124 + fi 119 125 } 120 126 121 127 # Resolve the full path to the script's directory ··· 169 175 check_connection 170 176 171 177 # Call ghc function 172 - ghc "$@" 178 + ghc "$@"