/ .legacy-gitfield / gitfield-gitlab
gitfield-gitlab
1 #!/bin/bash 2 set -euo pipefail 3 IFS=$'\n\t' 4 5 # ──────────────── 6 # Configuration 7 # ──────────────── 8 GIT_REMOTE_NAME="gitlab" 9 REPO_NAME=$(basename "$(pwd)") 10 DEFAULT_NAME="Mark Randall Havens" 11 DEFAULT_EMAIL="mark.r.havens@gmail.com" 12 GITLAB_WEB="https://gitlab.com" 13 GITLAB_API="$GITLAB_WEB/api/v4" 14 GITLAB_SSH="git@gitlab.com" 15 TOKEN_FILE="$HOME/.gitfield_token" 16 17 # ──────────────── 18 # Logging 19 # ──────────────── 20 info() { echo -e "\e[1;34m[INFO]\e[0m $*"; } 21 warn() { echo -e "\e[1;33m[WARN]\e[0m $*"; } 22 error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; } 23 24 # ──────────────── 25 # Token Handling 26 # ──────────────── 27 RESET_TOKEN=false 28 if [[ "${1:-}" == "--reset-token" ]]; then 29 RESET_TOKEN=true 30 rm -f "$TOKEN_FILE" 31 info "Token reset requested." 32 fi 33 34 if [ -f "$TOKEN_FILE" ] && [ "$RESET_TOKEN" = false ]; then 35 TOKEN=$(<"$TOKEN_FILE") 36 info "Using cached token from $TOKEN_FILE" 37 else 38 echo 39 echo "🔐 Paste your GitLab Personal Access Token (scopes: api, read_user, write_repository, write_ssh_key)" 40 echo "→ Generate at: $GITLAB_WEB/-/user_settings/personal_access_tokens" 41 read -rp "🔑 Token: " TOKEN 42 echo "$TOKEN" > "$TOKEN_FILE" 43 chmod 600 "$TOKEN_FILE" 44 info "Token saved for future use at $TOKEN_FILE" 45 fi 46 47 # ──────────────── 48 # Git Identity 49 # ──────────────── 50 git config --global user.name "$DEFAULT_NAME" 51 git config --global user.email "$DEFAULT_EMAIL" 52 info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>" 53 54 # ──────────────── 55 # Git Init 56 # ──────────────── 57 if [ ! -d .git ]; then 58 info "Initializing Git repository..." 59 git init 60 git add . || warn "Nothing to add" 61 git commit -m "Initial commit" || warn "Nothing to commit" 62 else 63 info "Git repo already initialized." 64 fi 65 66 if ! git rev-parse HEAD &>/dev/null; then 67 git add . && git commit -m "Initial commit" || warn "Nothing to commit" 68 fi 69 70 # ──────────────── 71 # SSH Key Setup 72 # ──────────────── 73 if [ ! -f ~/.ssh/id_rsa ]; then 74 info "Generating new SSH key..." 75 ssh-keygen -t rsa -b 4096 -C "$DEFAULT_EMAIL" -f ~/.ssh/id_rsa -N "" || error "SSH keygen failed" 76 fi 77 78 eval "$(ssh-agent -s)" 79 ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key" 80 81 # ──────────────── 82 # Username from GitLab 83 # ──────────────── 84 USERNAME=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_API/user" | grep -oP '(?<="username":")[^"]*') || { 85 error "Failed to retrieve GitLab username — invalid token?" 86 } 87 info "GitLab username: $USERNAME" 88 89 # ──────────────── 90 # Upload SSH Key if Needed 91 # ──────────────── 92 if ! ssh -T "$GITLAB_SSH" 2>&1 | grep -q "Welcome"; then 93 PUBKEY=$(<~/.ssh/id_rsa.pub) 94 TITLE="AutoKey-$(hostname)-$(date +%s)" 95 info "Uploading SSH key to GitLab..." 96 curl -s --fail -X POST "$GITLAB_API/user/keys" \ 97 -H "PRIVATE-TOKEN: $TOKEN" \ 98 -H "Content-Type: application/json" \ 99 -d "{\"title\": \"$TITLE\", \"key\": \"$PUBKEY\"}" || warn "SSH key upload may have failed" 100 sleep 2 101 fi 102 103 # ──────────────── 104 # Create GitLab Repo (Graceful Fallback) 105 # ──────────────── 106 if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then 107 info "Creating GitLab repository '$REPO_NAME'..." 108 if curl -s --fail -X POST "$GITLAB_API/projects" \ 109 -H "PRIVATE-TOKEN: $TOKEN" \ 110 -H "Content-Type: application/json" \ 111 -d "{\"name\": \"$REPO_NAME\", \"visibility\": \"public\"}" | grep -q '"ssh_url_to_repo":'; then 112 info "Repository created." 113 else 114 warn "Repo may already exist or creation failed — continuing..." 115 fi 116 117 REMOTE_URL="$GITLAB_SSH:$USERNAME/$REPO_NAME.git" 118 git remote add "$GIT_REMOTE_NAME" "$REMOTE_URL" 119 info "Remote set to: $REMOTE_URL" 120 else 121 info "Remote already configured: $(git remote get-url "$GIT_REMOTE_NAME")" 122 fi 123 124 # ──────────────── 125 # Commit & Push 126 # ──────────────── 127 if ! git diff --quiet || ! git diff --cached --quiet; then 128 git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "No changes" 129 else 130 info "No uncommitted changes." 131 fi 132 133 BRANCH=$(git rev-parse --abbrev-ref HEAD) 134 if ! git config --get branch."$BRANCH".remote &>/dev/null; then 135 info "Pushing with upstream..." 136 git push -u "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed" 137 else 138 info "Pushing to $GIT_REMOTE_NAME/$BRANCH..." 139 git push "$GIT_REMOTE_NAME" "$BRANCH" || error "Push failed" 140 fi