Update: 2025-05-30 06:31:41
This commit is contained in:
parent
69b0906bc0
commit
639e8ba023
1 changed files with 51 additions and 45 deletions
|
@ -3,7 +3,7 @@ set -euo pipefail
|
|||
IFS=$'\n\t'
|
||||
|
||||
# ────────────────
|
||||
# Config
|
||||
# Configuration
|
||||
# ────────────────
|
||||
GIT_REMOTE_NAME="origin"
|
||||
REPO_NAME=$(basename "$(pwd)")
|
||||
|
@ -12,6 +12,7 @@ DEFAULT_EMAIL="mark.r.havens@gmail.com"
|
|||
GITLAB_WEB="https://gitlab.com"
|
||||
GITLAB_API="$GITLAB_WEB/api/v4"
|
||||
GITLAB_SSH="git@gitlab.com"
|
||||
TOKEN_FILE="$HOME/.gitfield_token"
|
||||
|
||||
# ────────────────
|
||||
# Logging
|
||||
|
@ -20,6 +21,29 @@ 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; }
|
||||
|
||||
# ────────────────
|
||||
# Token Handling
|
||||
# ────────────────
|
||||
RESET_TOKEN=false
|
||||
if [[ "${1:-}" == "--reset-token" ]]; then
|
||||
RESET_TOKEN=true
|
||||
rm -f "$TOKEN_FILE"
|
||||
info "Token reset requested."
|
||||
fi
|
||||
|
||||
if [ -f "$TOKEN_FILE" ] && [ "$RESET_TOKEN" = false ]; then
|
||||
TOKEN=$(<"$TOKEN_FILE")
|
||||
info "Using cached token from $TOKEN_FILE"
|
||||
else
|
||||
echo
|
||||
echo "🔐 Paste your GitLab Personal Access Token (scopes: api, read_user, write_repository, write_ssh_key)"
|
||||
echo "→ Generate at: $GITLAB_WEB/-/profile/personal_access_tokens"
|
||||
read -rp "🔑 Token: " TOKEN
|
||||
echo "$TOKEN" > "$TOKEN_FILE"
|
||||
chmod 600 "$TOKEN_FILE"
|
||||
info "Token saved for future use at $TOKEN_FILE"
|
||||
fi
|
||||
|
||||
# ────────────────
|
||||
# Git Identity
|
||||
# ────────────────
|
||||
|
@ -28,10 +52,10 @@ git config --global user.email "$DEFAULT_EMAIL"
|
|||
info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>"
|
||||
|
||||
# ────────────────
|
||||
# Git Repo Init
|
||||
# Git Init
|
||||
# ────────────────
|
||||
if [ ! -d .git ]; then
|
||||
info "Initializing Git repo..."
|
||||
info "Initializing Git repository..."
|
||||
git init
|
||||
git add . || warn "Nothing to add"
|
||||
git commit -m "Initial commit" || warn "Nothing to commit"
|
||||
|
@ -39,9 +63,6 @@ else
|
|||
info "Git repo already initialized."
|
||||
fi
|
||||
|
||||
# ────────────────
|
||||
# Ensure First Commit
|
||||
# ────────────────
|
||||
if ! git rev-parse HEAD &>/dev/null; then
|
||||
git add . && git commit -m "Initial commit" || warn "Nothing to commit"
|
||||
fi
|
||||
|
@ -50,31 +71,23 @@ fi
|
|||
# SSH Key Setup
|
||||
# ────────────────
|
||||
if [ ! -f ~/.ssh/id_rsa ]; then
|
||||
info "Generating SSH key..."
|
||||
ssh-keygen -t rsa -b 4096 -C "$DEFAULT_EMAIL" -f ~/.ssh/id_rsa -N "" || error "Failed to generate SSH key"
|
||||
info "Generating new SSH key..."
|
||||
ssh-keygen -t rsa -b 4096 -C "$DEFAULT_EMAIL" -f ~/.ssh/id_rsa -N "" || error "SSH keygen failed"
|
||||
fi
|
||||
|
||||
eval "$(ssh-agent -s)"
|
||||
ssh-add ~/.ssh/id_rsa || error "Failed to add SSH key"
|
||||
|
||||
# ────────────────
|
||||
# GitLab Token Prompt
|
||||
# ────────────────
|
||||
echo
|
||||
echo "🔐 Paste your GitLab Personal Access Token (scopes: api, read_user, write_repository, write_ssh_key)"
|
||||
echo "→ Generate at: $GITLAB_WEB/-/profile/personal_access_tokens"
|
||||
read -rp "🔑 Token: " TOKEN
|
||||
|
||||
# ────────────────
|
||||
# Get GitLab Username (via API)
|
||||
# Username from GitLab
|
||||
# ────────────────
|
||||
USERNAME=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_API/user" | grep -oP '(?<="username":")[^"]*') || {
|
||||
error "Failed to retrieve username — invalid token?"
|
||||
error "Failed to retrieve GitLab username — invalid token?"
|
||||
}
|
||||
info "GitLab username detected: $USERNAME"
|
||||
info "GitLab username: $USERNAME"
|
||||
|
||||
# ────────────────
|
||||
# Register SSH Key (if missing)
|
||||
# Upload SSH Key if Needed
|
||||
# ────────────────
|
||||
if ! ssh -T "$GITLAB_SSH" 2>&1 | grep -q "Welcome"; then
|
||||
PUBKEY=$(<~/.ssh/id_rsa.pub)
|
||||
|
@ -83,45 +96,38 @@ if ! ssh -T "$GITLAB_SSH" 2>&1 | grep -q "Welcome"; then
|
|||
curl -s --fail -X POST "$GITLAB_API/user/keys" \
|
||||
-H "PRIVATE-TOKEN: $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"title\": \"$TITLE\", \"key\": \"$PUBKEY\"}" || warn "SSH key may already exist or failed to upload"
|
||||
-d "{\"title\": \"$TITLE\", \"key\": \"$PUBKEY\"}" || warn "SSH key upload may have failed"
|
||||
sleep 2
|
||||
fi
|
||||
|
||||
# ────────────────
|
||||
# Create Repository (via API)
|
||||
# Create GitLab Repo (Graceful Fallback)
|
||||
# ────────────────
|
||||
REPO_API_URL="$GITLAB_API/projects"
|
||||
REPO_PAYLOAD="{\"name\": \"$REPO_NAME\", \"visibility\": \"public\"}"
|
||||
|
||||
info "Creating GitLab repository '$REPO_NAME'..."
|
||||
if curl -s --fail -X POST "$REPO_API_URL" \
|
||||
-H "PRIVATE-TOKEN: $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$REPO_PAYLOAD" | grep -q '"ssh_url_to_repo":'; then
|
||||
info "Repository created successfully."
|
||||
else
|
||||
warn "Repository may already exist or creation failed — continuing..."
|
||||
fi
|
||||
|
||||
# ────────────────
|
||||
# Add SSH Remote
|
||||
# ────────────────
|
||||
REMOTE_URL="$GITLAB_SSH:$USERNAME/$REPO_NAME.git"
|
||||
if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then
|
||||
info "Creating GitLab repository '$REPO_NAME'..."
|
||||
if curl -s --fail -X POST "$GITLAB_API/projects" \
|
||||
-H "PRIVATE-TOKEN: $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"name\": \"$REPO_NAME\", \"visibility\": \"public\"}" | grep -q '"ssh_url_to_repo":'; then
|
||||
info "Repository created."
|
||||
else
|
||||
warn "Repo may already exist or creation failed — continuing..."
|
||||
fi
|
||||
|
||||
REMOTE_URL="$GITLAB_SSH:$USERNAME/$REPO_NAME.git"
|
||||
git remote add "$GIT_REMOTE_NAME" "$REMOTE_URL"
|
||||
info "SSH remote set to: $REMOTE_URL"
|
||||
info "Remote set to: $REMOTE_URL"
|
||||
else
|
||||
info "Remote already exists: $(git remote get-url "$GIT_REMOTE_NAME")"
|
||||
info "Remote already configured: $(git remote get-url "$GIT_REMOTE_NAME")"
|
||||
fi
|
||||
|
||||
# ────────────────
|
||||
# Commit + Push
|
||||
# Commit & Push
|
||||
# ────────────────
|
||||
if ! git diff --quiet || ! git diff --cached --quiet; then
|
||||
info "Committing local changes..."
|
||||
git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "Nothing to commit"
|
||||
git add . && git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')" || warn "No changes"
|
||||
else
|
||||
info "No local changes to commit."
|
||||
info "No uncommitted changes."
|
||||
fi
|
||||
|
||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue