#!/bin/bash set -euo pipefail IFS=$'\n\t' # ╭─────────────────────────────────────╮ # │ CONFIGURATION │ # ╰─────────────────────────────────────╯ BITBUCKET_USER="mrhavens" BITBUCKET_WORKSPACE="thefoldwithin" REMOTE_NAME="bitbucket" REPO_NAME=$(basename "$(pwd)") EMAIL="mark.r.havens@gmail.com" FULL_NAME="Mark Randall Havens" APP_PASS_FILE="$HOME/.bitbucket_app_password" API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$REPO_NAME" SSH_REMOTE="git@bitbucket.org:$BITBUCKET_WORKSPACE/$REPO_NAME.git" # ╭─────────────────────────────────────╮ # │ LOGGING UTILS │ # ╰─────────────────────────────────────╯ info() { echo -e "\e[1;34m[INFO]\e[0m $*"; } warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; } error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; } # ╭─────────────────────────────────────╮ # │ CHECK + INSTALL TOOLS │ # ╰─────────────────────────────────────╯ info "Checking prerequisites..." sudo apt update -qq sudo apt install -y git curl jq openssh-client || error "Dependency install failed" # ╭─────────────────────────────────────╮ # │ GIT IDENTITY SETUP │ # ╰─────────────────────────────────────╯ git config --global user.name "$FULL_NAME" git config --global user.email "$EMAIL" info "Git identity: $FULL_NAME <$EMAIL>" # ╭─────────────────────────────────────╮ # │ SSH KEYGEN + AGENT │ # ╰─────────────────────────────────────╯ if [ ! -f ~/.ssh/id_rsa ]; then info "Generating new SSH key..." ssh-keygen -t rsa -b 4096 -C "$EMAIL" -f ~/.ssh/id_rsa -N "" fi eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key" # Add Bitbucket to known_hosts to avoid prompt ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null || true # ╭─────────────────────────────────────╮ # │ SSH AUTH VERIFICATION │ # ╰─────────────────────────────────────╯ info "Verifying SSH access to Bitbucket..." if ssh -T git@bitbucket.org 2>/dev/null; then info "✓ SSH access to Bitbucket verified." elif ssh -T git@bitbucket.org 2>&1 | grep -q "successfully authenticated"; then info "✓ SSH authentication to Bitbucket accepted." elif ssh -T git@bitbucket.org; [[ $? == 1 ]]; then info "✓ SSH connected — Bitbucket returned expected code 1." else warn "❌ SSH key not authorized with Bitbucket." echo "→ Visit: https://bitbucket.org/account/settings/ssh-keys/" echo "→ Paste this key:" echo cat ~/.ssh/id_rsa.pub echo exit 1 fi # ╭─────────────────────────────────────╮ # │ BITBUCKET APP PASSWORD SETUP │ # ╰─────────────────────────────────────╯ if [ ! -f "$APP_PASS_FILE" ]; then echo echo "🔐 Create a Bitbucket App Password (repo read/write + SSH + webhooks)" echo "→ https://bitbucket.org/account/settings/app-passwords/" read -rsp "Enter Bitbucket App Password (input hidden): " APP_PASS echo "$APP_PASS" > "$APP_PASS_FILE" chmod 600 "$APP_PASS_FILE" echo info "App password saved at $APP_PASS_FILE" fi APP_PASS=$(<"$APP_PASS_FILE") # ╭─────────────────────────────────────╮ # │ GIT INIT & COMMIT │ # ╰─────────────────────────────────────╯ if [ ! -d .git ]; then info "Initializing Git repository..." git init git add . || warn "Nothing to add" git commit -m "Initial commit" || warn "Nothing to commit" else info "Git repo already initialized." fi # ╭─────────────────────────────────────╮ # │ CREATE REMOTE IF NOT EXISTS │ # ╰─────────────────────────────────────╯ REPO_EXISTS=$(curl -s -u "$BITBUCKET_USER:$APP_PASS" "$API_URL" | jq -r '.name // empty') if [ -z "$REPO_EXISTS" ]; then info "Creating Bitbucket repository '$REPO_NAME'..." curl -s -u "$BITBUCKET_USER:$APP_PASS" -X POST "$API_URL" \ -H "Content-Type: application/json" \ -d "{\"scm\": \"git\", \"is_private\": false, \"project\": {\"key\": \"~$BITBUCKET_USER\"}}" \ | grep -q '"uuid"' || warn "Repo may already exist or creation failed" else info "Remote Bitbucket repo already exists." fi # ╭─────────────────────────────────────╮ # │ SET GIT REMOTE │ # ╰─────────────────────────────────────╯ if ! git remote get-url "$REMOTE_NAME" &>/dev/null; then git remote add "$REMOTE_NAME" "$SSH_REMOTE" info "Remote set: $SSH_REMOTE" else info "Remote already set: $(git remote get-url "$REMOTE_NAME")" fi # ╭─────────────────────────────────────╮ # │ COMMIT + PUSH LOGIC │ # ╰─────────────────────────────────────╯ BRANCH=$(git rev-parse --abbrev-ref HEAD) if ! git diff --quiet || ! git diff --cached --quiet; then git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit" else info "No uncommitted changes." fi if ! git config --get branch."$BRANCH".remote &>/dev/null; then info "Pushing with upstream..." git push -u "$REMOTE_NAME" "$BRANCH" || error "Push failed" else info "Pushing to $REMOTE_NAME/$BRANCH..." git push "$REMOTE_NAME" "$BRANCH" || error "Push failed" fi info "✅ Bitbucket push complete."