/ .legacy-gitfield / gitfield-github
gitfield-github
  1  #!/bin/bash
  2  set -euo pipefail
  3  IFS=$'\n\t'
  4  
  5  GIT_REMOTE_NAME="github"
  6  REPO_NAME=$(basename "$(pwd)")
  7  DEFAULT_NAME="Mark Randall Havens"
  8  DEFAULT_EMAIL="mark.r.havens@gmail.com"
  9  
 10  # ────────────────
 11  # Logging Helpers
 12  # ────────────────
 13  info()  { echo -e "\e[1;34m[INFO]\e[0m $*"; }
 14  warn()  { echo -e "\e[1;33m[WARN]\e[0m $*"; }
 15  error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
 16  
 17  # ────────────────
 18  # Git and GitHub CLI Setup
 19  # ────────────────
 20  info "Checking for required tools..."
 21  
 22  if ! command -v git &>/dev/null; then
 23    info "Installing Git..."
 24    sudo apt update && sudo apt install git -y || error "Failed to install Git"
 25  else
 26    info "Git already installed: $(git --version)"
 27  fi
 28  
 29  if ! command -v gh &>/dev/null; then
 30    info "Installing GitHub CLI..."
 31    sudo apt install curl -y
 32    curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
 33    sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
 34    echo "deb [arch=$(dpkg --print-architecture)] signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg https://cli.github.com/packages stable main" | \
 35      sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
 36    sudo apt update && sudo apt install gh -y || error "Failed to install GitHub CLI"
 37  else
 38    info "GitHub CLI already installed: $(gh --version | head -n 1)"
 39  fi
 40  
 41  # ────────────────
 42  # GitHub Authentication
 43  # ────────────────
 44  if ! gh auth status &>/dev/null; then
 45    info "Authenticating GitHub CLI..."
 46    gh auth login || error "GitHub authentication failed"
 47  else
 48    info "GitHub CLI authenticated."
 49  fi
 50  
 51  # ────────────────
 52  # Git Identity
 53  # ────────────────
 54  USER_NAME=$(git config --global user.name || true)
 55  USER_EMAIL=$(git config --global user.email || true)
 56  
 57  if [[ -z "$USER_NAME" || -z "$USER_EMAIL" ]]; then
 58    git config --global user.name "$DEFAULT_NAME"
 59    git config --global user.email "$DEFAULT_EMAIL"
 60    info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>"
 61  else
 62    info "Git identity already set to: $USER_NAME <$USER_EMAIL>"
 63  fi
 64  
 65  # ────────────────
 66  # Ensure SSH Key Exists
 67  # ────────────────
 68  if [ ! -f "$HOME/.ssh/id_ed25519" ]; then
 69    warn "SSH key not found. Generating a new one..."
 70    read -rp "[PROMPT] Enter your GitHub email: " SSH_EMAIL
 71    ssh-keygen -t ed25519 -C "$SSH_EMAIL" -f "$HOME/.ssh/id_ed25519" -N ""
 72    eval "$(ssh-agent -s)"
 73    ssh-add "$HOME/.ssh/id_ed25519"
 74    info "Public key:"
 75    cat "$HOME/.ssh/id_ed25519.pub"
 76    info "Now adding key to GitHub..."
 77    gh ssh-key add "$HOME/.ssh/id_ed25519.pub" --title "$(hostname)" || warn "You may need to add it manually"
 78  else
 79    info "SSH key already exists."
 80  fi
 81  
 82  # ────────────────
 83  # Initialize Git Repo
 84  # ────────────────
 85  if [ ! -d ".git" ]; then
 86    info "Initializing Git repo..."
 87    git init
 88    git add .
 89    git commit -m "Initial commit" || warn "Nothing to commit"
 90  else
 91    info "Git repo already initialized."
 92  fi
 93  
 94  # ────────────────
 95  # Ensure First Commit
 96  # ────────────────
 97  if ! git rev-parse HEAD &>/dev/null; then
 98    git add .
 99    git commit -m "Initial commit" || warn "Nothing to commit"
100  fi
101  
102  # ────────────────
103  # Setup GitHub Remote (SSH)
104  # ────────────────
105  USERNAME=$(gh api user | jq -r .login)
106  SSH_REMOTE_URL="git@github.com:$USERNAME/$REPO_NAME.git"
107  
108  if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then
109    if gh repo view "$USERNAME/$REPO_NAME" &>/dev/null; then
110      info "Linking to existing GitHub repo via SSH..."
111      git remote add "$GIT_REMOTE_NAME" "$SSH_REMOTE_URL"
112    else
113      info "Creating GitHub repo..."
114      gh repo create "$REPO_NAME" --public --source=. --remote="$GIT_REMOTE_NAME" --push || error "Failed to create GitHub repo"
115    fi
116  else
117    info "Remote '$GIT_REMOTE_NAME' already set."
118    git remote set-url "$GIT_REMOTE_NAME" "$SSH_REMOTE_URL"
119  fi
120  
121  # ────────────────
122  # Commit Changes
123  # ────────────────
124  if ! git diff --quiet || ! git diff --cached --quiet; then
125    info "Changes detected — committing..."
126    git add .
127    git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
128  else
129    info "No uncommitted changes found."
130  fi
131  
132  # ────────────────
133  # Push via SSH
134  # ────────────────
135  BRANCH=$(git rev-parse --abbrev-ref HEAD)
136  
137  if ! git config --get branch."$BRANCH".remote &>/dev/null; then
138    info "Setting upstream and pushing..."
139    git push -u "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
140  else
141    info "Pushing via SSH to '$GIT_REMOTE_NAME'..."
142    git push "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
143  fi
144  
145  info "✅ Sync complete: $SSH_REMOTE_URL"