/ gitfield-github
gitfield-github
1 #!/bin/bash 2 3 set -euo pipefail 4 IFS=$'\n\t' 5 6 GIT_REMOTE_NAME="github" 7 REPO_NAME=$(basename "$(pwd)") 8 DEFAULT_NAME="Mark Randall Havens" 9 DEFAULT_EMAIL="mark.r.havens@gmail.com" 10 11 # ──────────────── 12 # Logging Helpers 13 # ──────────────── 14 info() { echo -e "\e[1;34m[INFO]\e[0m $*"; } 15 warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; } 16 error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; } 17 18 # ──────────────── 19 # Ensure Git is Installed 20 # ──────────────── 21 if ! command -v git &>/dev/null; then 22 info "Installing Git..." 23 sudo apt update && sudo apt install git -y || error "Failed to install Git" 24 else 25 info "Git already installed: $(git --version)" 26 fi 27 28 # ──────────────── 29 # Ensure GitHub CLI is Installed 30 # ──────────────── 31 if ! command -v gh &>/dev/null; then 32 info "Installing GitHub CLI..." 33 type -p curl >/dev/null || sudo apt install curl -y 34 curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | \ 35 sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg 36 sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg 37 echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] \ 38 https://cli.github.com/packages stable main" | \ 39 sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null 40 sudo apt update && sudo apt install gh -y || error "Failed to install GitHub CLI" 41 else 42 info "GitHub CLI already installed: $(gh --version | head -n 1)" 43 fi 44 45 # ──────────────── 46 # Ensure GitHub CLI is Authenticated 47 # ──────────────── 48 if ! gh auth status &>/dev/null; then 49 info "Authenticating GitHub CLI..." 50 gh auth login || error "GitHub authentication failed" 51 else 52 info "GitHub CLI authenticated." 53 fi 54 55 # ──────────────── 56 # Ensure Git Identity is Set 57 # ──────────────── 58 USER_NAME=$(git config --global user.name || true) 59 USER_EMAIL=$(git config --global user.email || true) 60 61 if [[ -z "$USER_NAME" || -z "$USER_EMAIL" ]]; then 62 info "Setting global Git identity..." 63 git config --global user.name "$DEFAULT_NAME" 64 git config --global user.email "$DEFAULT_EMAIL" 65 info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>" 66 else 67 info "Git identity already set to: $USER_NAME <$USER_EMAIL>" 68 fi 69 70 # ──────────────── 71 # Initialize Git Repo If Missing 72 # ──────────────── 73 if [ ! -d ".git" ]; then 74 info "Initializing local Git repository..." 75 git init || error "Failed to initialize git" 76 git add . || warn "Nothing to add" 77 git commit -m "Initial commit" || warn "Nothing to commit" 78 else 79 info "Git repository already initialized." 80 fi 81 82 # ──────────────── 83 # Ensure at Least One Commit Exists 84 # ──────────────── 85 if ! git rev-parse HEAD &>/dev/null; then 86 info "Creating first commit..." 87 git add . || warn "Nothing to add" 88 git commit -m "Initial commit" || warn "Nothing to commit" 89 fi 90 91 # ──────────────── 92 # Create Remote GitHub Repo If Missing 93 # ──────────────── 94 if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then 95 info "Creating GitHub repository '$REPO_NAME'..." 96 gh repo create "$REPO_NAME" --public --source=. --remote="$GIT_REMOTE_NAME" || error "Failed to create GitHub repo" 97 else 98 info "Remote '$GIT_REMOTE_NAME' already set to: $(git remote get-url $GIT_REMOTE_NAME)" 99 fi 100 101 # ──────────────── 102 # Commit Changes If Needed 103 # ──────────────── 104 if ! git diff --quiet || ! git diff --cached --quiet; then 105 info "Changes detected — committing..." 106 git add . 107 git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit" 108 else 109 info "No uncommitted changes found." 110 fi 111 112 # ──────────────── 113 # Final Push — Always Push, Even If No Upstream 114 # ──────────────── 115 BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) 116 117 if ! git config --get branch."$BRANCH_NAME".remote &>/dev/null; then 118 info "No upstream detected. Setting upstream and pushing..." 119 git push -u "$GIT_REMOTE_NAME" "$BRANCH_NAME" || error "Failed to push and set upstream" 120 else 121 info "Pushing to remote '$GIT_REMOTE_NAME'..." 122 git push "$GIT_REMOTE_NAME" "$BRANCH_NAME" || error "Push failed" 123 fi