Simple Git & GitHub CLI Shell Scripts
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 196 lines 5.4 kB view raw
1#!/bin/bash 2 3function gbd { 4 if ! is_a_git_repo; then 5 echo "${BOLD}${RESET_COLOR} This won't work, you are not in a git repo !" 6 return 0 7 fi 8 9 current_branch=$(git branch | awk '/\*/ {print $2}') 10 default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') 11 current_user=$(awk '/user:/ {print $2; exit}' ~/.config/gh/hosts.yml) 12 13 if [ -z "$default_branch" ]; then 14 default_branch=$(git config --get init.defaultBranch) 15 fi 16 17 if [ $# -eq 1 ]; then 18 if [ "$1" = "$default_branch" ]; then 19 echo "${BOLD} Fatal ! Cannot Delete the Default Branch " 20 return 0 21 fi 22 23 if ! git show-ref --verify --quiet "refs/heads/$1" &>/dev/null; then 24 echo "${BOLD} Fatal ! Branch ${GREEN}$1 ${RESET_COLOR}doesn't exist ${RESET}" 25 return 0 26 fi 27 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 } 47 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 53 54 if [ "$delete_branch" = "y" ]; then 55 if [ "$current_branch" != "$default_branch" ]; then 56 git checkout $default_branch >/dev/null 2>&1 57 fi 58 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)}') 62 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 70 71 # Check for internet connectivity to GitHub 72 if $SUDO ping -c 1 github.com &>/dev/null; then 73 check_delete_remote_branch 74 fi 75 fi 76 fi 77 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 88 elif [ $# -eq 0 ]; then 89 if [ "$current_branch" = "$default_branch" ]; then 90 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 110 else 111 check_delete_remote_branch 112 fi 113 fi 114 } 115 116 git checkout "$default_branch" >/dev/null 2>&1 117 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)}') 121 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 130 131 # Check for internet connectivity to GitHub 132 if $SUDO ping -c 1 github.com &>/dev/null; then 133 check_delete_remote_branch 134 fi 135 fi 136 fi 137 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 146 else 147 echo "${BOLD}${RESET_COLOR} Usage : gbd branch_to_delete" 148 fi 149} 150 151# Resolve the full path to the script's directory 152REAL_PATH="$(dirname "$(readlink -f "$0")")" 153PARENT_DIR="$(dirname "$REAL_PATH")" 154CATEGORY="gh.scripts" 155 156HELPS_DIR="$PARENT_DIR/helps/$CATEGORY" 157HELP_FILE="$(basename "$0" .sh)_help.sh" 158 159UTILS_DIR="$PARENT_DIR/utils" 160 161# Import necessary variables and functions 162source "$UTILS_DIR/check_connection.sh" 163source "$UTILS_DIR/check_remote.sh" 164source "$UTILS_DIR/check_git.sh" 165source "$UTILS_DIR/setup_git.sh" 166source "$UTILS_DIR/check_sudo.sh" 167source "$UTILS_DIR/colors.sh" 168source "$UTILS_DIR/usage.sh" 169 170# Import help file 171source "$HELPS_DIR/$HELP_FILE" 172 173# Usage function to display help 174function usage { 175 show_help "Usage" "${gbd_arguments[@]}" 176 show_help "Description" "${gbd_descriptions[@]}" 177 show_help "Options" "${gbd_options[@]}" 178 show_extra "${gbd_extras[@]}" 179 exit 0 180} 181 182# Check if --help is the first argument 183[ "$1" = "--help" ] && usage 184 185# prompt for sudo 186# password if required 187allow_sudo 188 189# Setting up git 190setup_git 191 192# Check for internet connectivity to GitHub 193check_connection 194 195# Call gbd function 196gbd "$@"