#!/usr/bin/env bash set -euo pipefail # ============================================================================= # phase10_teardown.sh — Reverse local repo remote cutover from phase 10 # Reverts local repos so GitHub is origin again: # 1. Move Gitea origin -> gitea (if present) # 2. Move github -> origin # 3. Set local branch upstreams to origin/ where available # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" source "${SCRIPT_DIR}/lib/phase10_common.sh" load_env require_vars GITEA_ORG_NAME GITEA_DOMAIN GITHUB_USERNAME phase_header 10 "Local Repo Remote Cutover — Teardown" LOCAL_REPO_ROOT="${PHASE10_LOCAL_ROOT:-/Users/s/development}" EXPECTED_REPO_COUNT="${PHASE10_EXPECTED_REPO_COUNT:-3}" INCLUDE_PATHS=() AUTO_YES=false if [[ -n "${PHASE10_INCLUDE_PATHS:-}" ]]; then # Space-delimited list of extra repo roots to include in phase10 discovery. read -r -a INCLUDE_PATHS <<< "${PHASE10_INCLUDE_PATHS}" fi for arg in "$@"; do case "$arg" in --local-root=*) LOCAL_REPO_ROOT="${arg#*=}" ;; --expected-count=*) EXPECTED_REPO_COUNT="${arg#*=}" ;; --include-path=*) INCLUDE_PATHS+=("${arg#*=}") ;; --yes|-y) AUTO_YES=true ;; --help|-h) cat <&2 read -r confirm if [[ "$confirm" != "y" ]] && [[ "$confirm" != "Y" ]]; then log_info "Teardown cancelled" exit 0 fi fi if ! phase10_discover_local_repos "$LOCAL_REPO_ROOT" "$GITHUB_USERNAME" "$SCRIPT_DIR" 0; then exit 1 fi for include_path in "${INCLUDE_PATHS[@]}"; do [[ -z "$include_path" ]] && continue if ! phase10_include_repo_path "$include_path" "$GITHUB_USERNAME"; then exit 1 fi done phase10_sort_repo_arrays if ! phase10_enforce_expected_count "$EXPECTED_REPO_COUNT" "$LOCAL_REPO_ROOT"; then exit 1 fi set_tracking_to_origin_where_available() { local repo_path="$1" repo_name="$2" local branch branch_count branch_count=0 while IFS= read -r branch; do [[ -z "$branch" ]] && continue branch_count=$((branch_count + 1)) if git -C "$repo_path" show-ref --verify --quiet "refs/remotes/origin/${branch}"; then if git -C "$repo_path" branch --set-upstream-to="origin/${branch}" "$branch" >/dev/null 2>&1; then log_success "${repo_name}: branch ${branch} now tracks origin/${branch}" else log_warn "${repo_name}: could not set upstream for ${branch}" fi else log_warn "${repo_name}: origin/${branch} not found (upstream unchanged)" fi done < <(git -C "$repo_path" for-each-ref --format='%(refname:short)' refs/heads) if [[ "$branch_count" -eq 0 ]]; then log_warn "${repo_name}: no local branches found" fi } ensure_origin_is_github() { local repo_path="$1" repo_name="$2" github_url="$3" gitea_url="$4" local origin_url github_url_existing gitea_url_existing origin_url="$(git -C "$repo_path" remote get-url origin 2>/dev/null || true)" github_url_existing="$(git -C "$repo_path" remote get-url github 2>/dev/null || true)" gitea_url_existing="$(git -C "$repo_path" remote get-url gitea 2>/dev/null || true)" if [[ -n "$origin_url" ]]; then if phase10_url_is_github_repo "$origin_url" "$GITHUB_USERNAME" "$repo_name"; then git -C "$repo_path" remote set-url origin "$github_url" if [[ -n "$github_url_existing" ]]; then git -C "$repo_path" remote remove github fi return 0 fi if phase10_url_is_gitea_repo "$origin_url" "$GITEA_DOMAIN" "$GITEA_ORG_NAME" "$repo_name"; then if [[ -z "$gitea_url_existing" ]]; then git -C "$repo_path" remote rename origin gitea else git -C "$repo_path" remote set-url gitea "$gitea_url" git -C "$repo_path" remote remove origin fi else log_error "${repo_name}: origin remote is unexpected (${origin_url})" return 1 fi fi if git -C "$repo_path" remote get-url origin >/dev/null 2>&1; then : elif [[ -n "$github_url_existing" ]]; then git -C "$repo_path" remote rename github origin else git -C "$repo_path" remote add origin "$github_url" fi git -C "$repo_path" remote set-url origin "$github_url" if git -C "$repo_path" remote get-url github >/dev/null 2>&1; then git -C "$repo_path" remote remove github fi if git -C "$repo_path" remote get-url gitea >/dev/null 2>&1; then git -C "$repo_path" remote set-url gitea "$gitea_url" fi return 0 } SUCCESS=0 FAILED=0 for i in "${!PHASE10_REPO_NAMES[@]}"; do repo_name="${PHASE10_REPO_NAMES[$i]}" repo_path="${PHASE10_REPO_PATHS[$i]}" github_url="${PHASE10_GITHUB_URLS[$i]}" gitea_url="$(phase10_canonical_gitea_url "$GITEA_DOMAIN" "$GITEA_ORG_NAME" "$repo_name")" log_info "--- Reverting repo: ${repo_name} (${repo_path}) ---" if ! ensure_origin_is_github "$repo_path" "$repo_name" "$github_url" "$gitea_url"; then FAILED=$((FAILED + 1)) continue fi set_tracking_to_origin_where_available "$repo_path" "$repo_name" SUCCESS=$((SUCCESS + 1)) done printf '\n' TOTAL=${#PHASE10_REPO_NAMES[@]} log_info "Results: ${SUCCESS} reverted, ${FAILED} failed (out of ${TOTAL})" if [[ "$FAILED" -gt 0 ]]; then log_error "Phase 10 teardown completed with failures" exit 1 fi log_success "Phase 10 teardown complete"