#!/usr/bin/env bash # Стандартные git remote: home (papatramp), kalinamall, github (PTah). set -euo pipefail OWNER="${OWNER:-PapaTramp}" GITHUB_ORG="${GITHUB_ORG:-PTah}" REPO="${REPO:-}" WHAT_IF="${WHAT_IF:-0}" log_step() { printf '[*] %s\n' "$1"; } log_ok() { printf '[+] %s\n' "$1"; } if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo 'Run inside a git repository or set REPO.' >&2 exit 1 fi repo_name="${REPO:-$(basename "$(git rev-parse --show-toplevel)")}" home_url="ssh://git@git.papatramp.ru:2222/${OWNER}/${repo_name}.git" kalinamall_url="ssh://git@git.kalinamall.ru:2222/${OWNER}/${repo_name}.git" github_url="git@github.com:${GITHUB_ORG}/${repo_name}.git" origin_remote_target() { local url url="$(git remote get-url origin 2>/dev/null | tr '[:upper:]' '[:lower:]' || true)" [[ -z "$url" ]] && return 0 if [[ "$url" == *kalinamall* ]]; then echo kalinamall; return 0; fi if [[ "$url" == *papatramp* ]]; then echo home; return 0; fi if [[ "$url" == *github.com* ]]; then echo github; return 0; fi } set_remote() { local name="$1" local url="$2" local existing current old origin_target if [[ "$WHAT_IF" == '1' ]]; then log_step "WhatIf: remote $name -> $url" return 0 fi if git remote | grep -Fxq "$name"; then current="$(git remote get-url "$name" | tr -d '\r\n')" if [[ "$current" == "$url" ]]; then log_ok "remote $name OK" return 0 fi git remote set-url "$name" "$url" log_ok "remote $name updated -> $url" return 0 fi if git remote | grep -Fxq origin; then origin_target="$(origin_remote_target || true)" if [[ "$origin_target" == "$name" ]]; then git remote rename origin "$name" git remote set-url "$name" "$url" log_ok "remote origin renamed to $name -> $url" return 0 fi fi case "$name" in home) legacy=(gitea papatramp origin-home) ;; github) legacy=() ;; kalinamall) legacy=(gitea-kalinamall kalina) ;; *) legacy=() ;; esac for old in "${legacy[@]}"; do if git remote | grep -Fxq "$old"; then git remote rename "$old" "$name" git remote set-url "$name" "$url" log_ok "remote $old renamed to $name -> $url" return 0 fi done git remote add "$name" "$url" log_ok "remote $name added -> $url" } log_step "Repo: $repo_name" set_remote home "$home_url" set_remote kalinamall "$kalinamall_url" set_remote github "$github_url" printf '\n' git remote -v