#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../lib/common.sh" # Configuration GIT_HOST="git.sdf.org" GIT_USER="jchenry" GIT_SSH_PORT="2222" WORKSPACE_DIR="$HOME/.workspace/src/$GIT_HOST/$GIT_USER" EXCLUDE_REPOS=("provision") # Ensure workspace directory exists ensure_dir "$WORKSPACE_DIR" log_info "Fetching repository list from $GIT_HOST/$GIT_USER" # Method 1: Try SSH first (works if SSH keys are set up) # This gets all repos (public + private) without needing a token log_info "Attempting to list repositories via SSH..." REPO_NAMES=$(ssh -p $GIT_SSH_PORT git@$GIT_HOST ls-repos $GIT_USER 2>/dev/null | grep "^$GIT_USER/" | sed "s|^$GIT_USER/||" | sed 's/\.git$//' | sort -u || true) # Method 2: Fall back to API if SSH didn't work if [ -z "$REPO_NAMES" ]; then log_warn "SSH listing failed, using API method" # Try with authentication token if available if [ -n "${GITEA_TOKEN:-}" ]; then log_info "Using GITEA_TOKEN for authentication (includes private repos)" REPOS_JSON=$(curl -fsSL -H "Authorization: token $GITEA_TOKEN" "https://$GIT_HOST/api/v1/user/repos?limit=100") else log_warn "GITEA_TOKEN not set - fetching public repositories only" log_info "To include private repos, either:" log_info " 1. Set up SSH keys for git@$GIT_HOST:$GIT_SSH_PORT, or" log_info " 2. Set GITEA_TOKEN environment variable" log_info " Get a token at: https://$GIT_HOST/user/settings/applications" REPOS_JSON=$(curl -fsSL "https://$GIT_HOST/api/v1/users/$GIT_USER/repos?limit=100") fi if [ -z "$REPOS_JSON" ] || [ "$REPOS_JSON" = "null" ]; then log_error "Failed to fetch repository list" exit 1 fi # Parse repository names from JSON REPO_NAMES=$(echo "$REPOS_JSON" | jq -r '.[].name' | sort -u) fi if [ -z "$REPO_NAMES" ]; then log_warn "No repositories found for user $GIT_USER" exit 0 fi log_info "Found repositories:" echo "$REPO_NAMES" | sed 's/^/ - /' echo "" # Clone each repository for repo in $REPO_NAMES; do # Skip excluded repos if [[ " ${EXCLUDE_REPOS[@]} " =~ " ${repo} " ]]; then log_info "Skipping excluded repository: $repo" continue fi REPO_DIR="$WORKSPACE_DIR/$repo" if [ -d "$REPO_DIR" ]; then log_info "Repository $repo already exists, updating..." cd "$REPO_DIR" git pull log_success "Updated $repo" else log_info "Cloning $repo..." # Clone using HTTPS first (works without SSH keys) HTTPS_URL="https://$GIT_HOST/$GIT_USER/$repo.git" # If we have a token, use it for cloning private repos if [ -n "${GITEA_TOKEN:-}" ]; then CLONE_URL="https://$GITEA_TOKEN@$GIT_HOST/$GIT_USER/$repo.git" else CLONE_URL="$HTTPS_URL" fi if git clone "$CLONE_URL" "$REPO_DIR" 2>/dev/null; then # Convert to SSH for future operations (if SSH is configured) cd "$REPO_DIR" SSH_URL="ssh://git@$GIT_HOST:$GIT_SSH_PORT/$GIT_USER/$repo.git" git remote set-url origin "$SSH_URL" log_success "Cloned $repo (remote set to SSH)" else log_error "Failed to clone $repo" fi fi echo "" done log_success "Repository cloning complete!" log_info "All repositories are in: $WORKSPACE_DIR" echo "" log_info "Repository list:" ls -1 "$WORKSPACE_DIR" | sed 's/^/ - /'