Update: 2025-05-30 21:53:40

This commit is contained in:
Mark Randall Havens 2025-05-30 21:53:40 -05:00
parent 23a21da069
commit ceabc8af94

View file

@ -7,16 +7,16 @@ REPO_NAME=$(basename "$(pwd)")
DEFAULT_NAME="Mark Randall Havens" DEFAULT_NAME="Mark Randall Havens"
DEFAULT_EMAIL="mark.r.havens@gmail.com" DEFAULT_EMAIL="mark.r.havens@gmail.com"
# ───────────────────────────── # ────────────────
# LOGGING UTILITY # Logging Helpers
# ───────────────────────────── # ────────────────
info() { echo -e "\033[1;34m[INFO]\033[0m $*"; } info() { echo -e "\e[1;34m[INFO]\e[0m $*"; }
warn() { echo -e "\033[1;33m[WARN]\033[0m $*"; } warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; }
error() { echo -e "\033[1;31m[ERROR]\033[0m $*" >&2; exit 1; } error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
# ───────────────────────────── # ────────────────
# ENSURE DEPENDENCIES # Git and GitHub CLI Setup
# ───────────────────────────── # ────────────────
info "Checking for required tools..." info "Checking for required tools..."
if ! command -v git &>/dev/null; then if ! command -v git &>/dev/null; then
@ -38,19 +38,19 @@ else
info "GitHub CLI already installed: $(gh --version | head -n 1)" info "GitHub CLI already installed: $(gh --version | head -n 1)"
fi fi
# ───────────────────────────── # ────────────────
# AUTHENTICATE GH # GitHub Authentication
# ───────────────────────────── # ────────────────
if ! gh auth status &>/dev/null; then if ! gh auth status &>/dev/null; then
info "Authenticating GitHub CLI..." info "Authenticating GitHub CLI..."
gh auth login || error "Authentication failed" gh auth login || error "GitHub authentication failed"
else else
info "GitHub CLI authenticated." info "GitHub CLI authenticated."
fi fi
# ───────────────────────────── # ────────────────
# CONFIGURE GIT IDENTITY # Git Identity
# ───────────────────────────── # ────────────────
USER_NAME=$(git config --global user.name || true) USER_NAME=$(git config --global user.name || true)
USER_EMAIL=$(git config --global user.email || true) USER_EMAIL=$(git config --global user.email || true)
@ -62,48 +62,65 @@ else
info "Git identity already set to: $USER_NAME <$USER_EMAIL>" info "Git identity already set to: $USER_NAME <$USER_EMAIL>"
fi fi
# ───────────────────────────── # ────────────────
# INITIALIZE IF NEEDED # Ensure SSH Key Exists
# ───────────────────────────── # ────────────────
if [ ! -f "$HOME/.ssh/id_ed25519" ]; then
warn "SSH key not found. Generating a new one..."
read -rp "[PROMPT] Enter your GitHub email: " SSH_EMAIL
ssh-keygen -t ed25519 -C "$SSH_EMAIL" -f "$HOME/.ssh/id_ed25519" -N ""
eval "$(ssh-agent -s)"
ssh-add "$HOME/.ssh/id_ed25519"
info "Public key:"
cat "$HOME/.ssh/id_ed25519.pub"
info "Now adding key to GitHub..."
gh ssh-key add "$HOME/.ssh/id_ed25519.pub" --title "$(hostname)" || warn "You may need to add it manually"
else
info "SSH key already exists."
fi
# ────────────────
# Initialize Git Repo
# ────────────────
if [ ! -d ".git" ]; then if [ ! -d ".git" ]; then
info "Initializing local git repository..." info "Initializing Git repo..."
git init git init
git add . git add .
git commit -m "Initial commit" || warn "Nothing to commit" git commit -m "Initial commit" || warn "Nothing to commit"
else else
info "Git repository already initialized." info "Git repo already initialized."
fi fi
# ───────────────────────────── # ────────────────
# CREATE FIRST COMMIT IF MISSING # Ensure First Commit
# ───────────────────────────── # ────────────────
if ! git rev-parse HEAD &>/dev/null; then if ! git rev-parse HEAD &>/dev/null; then
info "Creating first commit..."
git add . git add .
git commit -m "Initial commit" || warn "Nothing to commit" git commit -m "Initial commit" || warn "Nothing to commit"
fi fi
# ───────────────────────────── # ────────────────
# LINK OR CREATE GITHUB REPO # Setup GitHub Remote (SSH)
# ───────────────────────────── # ────────────────
USERNAME=$(gh api user | jq -r .login) USERNAME=$(gh api user | jq -r .login)
REMOTE_URL="https://github.com/$USERNAME/$REPO_NAME.git" SSH_REMOTE_URL="git@github.com:$USERNAME/$REPO_NAME.git"
if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then
if gh repo view "$USERNAME/$REPO_NAME" &>/dev/null; then if gh repo view "$USERNAME/$REPO_NAME" &>/dev/null; then
info "Linking to existing GitHub repo: $REMOTE_URL" info "Linking to existing GitHub repo via SSH..."
git remote add "$GIT_REMOTE_NAME" "$REMOTE_URL" git remote add "$GIT_REMOTE_NAME" "$SSH_REMOTE_URL"
else else
info "Creating GitHub repository '$REPO_NAME'..." info "Creating GitHub repo..."
gh repo create "$REPO_NAME" --public --source=. --remote="$GIT_REMOTE_NAME" || error "Failed to create GitHub repo" gh repo create "$REPO_NAME" --public --source=. --remote="$GIT_REMOTE_NAME" --push || error "Failed to create GitHub repo"
fi fi
else else
info "Remote '$GIT_REMOTE_NAME' already set to: $(git remote get-url $GIT_REMOTE_NAME)" info "Remote '$GIT_REMOTE_NAME' already set."
git remote set-url "$GIT_REMOTE_NAME" "$SSH_REMOTE_URL"
fi fi
# ───────────────────────────── # ────────────────
# COMMIT CHANGES IF ANY # Commit Changes
# ───────────────────────────── # ────────────────
if ! git diff --quiet || ! git diff --cached --quiet; then if ! git diff --quiet || ! git diff --cached --quiet; then
info "Changes detected — committing..." info "Changes detected — committing..."
git add . git add .
@ -112,16 +129,17 @@ else
info "No uncommitted changes found." info "No uncommitted changes found."
fi fi
# ───────────────────────────── # ────────────────
# PUSH TO GITHUB # Push via SSH
# ───────────────────────────── # ────────────────
BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=$(git rev-parse --abbrev-ref HEAD)
if ! git config --get branch."$BRANCH".remote &>/dev/null; then if ! git config --get branch."$BRANCH".remote &>/dev/null; then
info "Setting upstream and pushing..." info "Setting upstream and pushing..."
git push -u "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed" git push -u "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
else else
info "Pushing to remote '$GIT_REMOTE_NAME'..." info "Pushing via SSH to '$GIT_REMOTE_NAME'..."
git push "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed" git push "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed"
fi fi
info "✅ Sync complete: https://github.com/$USERNAME/$REPO_NAME" info "✅ Sync complete: $SSH_REMOTE_URL"