#!/usr/bin/env bash # Rewrite cross-repo URLs in tracked docs/config for the target Git host. # Usage: rewrite-git-host-urls.sh github|kalinamall|papatramp set -euo pipefail TARGET="${1:?usage: rewrite-git-host-urls.sh github|kalinamall|papatramp}" ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" case "$TARGET" in github) BASE='https://github.com/PTah' BLOB_SUFFIX='/blob/main' ;; kalinamall) BASE='https://git.kalinamall.ru/PapaTramp' BLOB_SUFFIX='/src/branch/main' ;; papatramp) BASE='https://git.papatramp.ru/PTah' BLOB_SUFFIX='/src/branch/main' ;; *) echo "unknown target: $TARGET" >&2 exit 1 ;; esac # Order matters: longer patterns first. PATTERNS=( 's|https://github.com/PTah/\([^)/"'"'"' ]*\)/blob/main/|'"${BASE}"'/\1'"${BLOB_SUFFIX}"'/|g' 's|https://git.kalinamall.ru/PapaTramp/\([^)/"'"'"' ]*\)/src/branch/main/|'"${BASE}"'/\1'"${BLOB_SUFFIX}"'/|g' 's|https://git.papatramp.ru/PTah/\([^)/"'"'"' ]*\)/src/branch/main/|'"${BASE}"'/\1'"${BLOB_SUFFIX}"'/|g' 's|https://github.com/PTah/|'"${BASE}"'/|g' 's|https://git.kalinamall.ru/PapaTramp/|'"${BASE}"'/|g' 's|https://git.papatramp.ru/PTah/|'"${BASE}"'/|g' 's|github.com/PTah/|'"${BASE#https://}"'/|g' 's|git.kalinamall.ru/PapaTramp/|'"${BASE#https://}"'/|g' 's|git.papatramp.ru/PTah/|'"${BASE#https://}"'/|g' ) FILES=() while IFS= read -r -d '' f; do FILES+=("$f") done < <(git ls-files '*.md' '*.json' '*.service' '*.example' '*.sh' '*.ps1' '*.yml' '*.yaml' 2>/dev/null | tr '\n' '\0') if [ "${#FILES[@]}" -eq 0 ]; then echo "no files to rewrite" >&2 exit 0 fi for f in "${FILES[@]}"; do [ -f "$f" ] || continue tmp="$(mktemp)" cp "$f" "$tmp" for pat in "${PATTERNS[@]}"; do sed -i "$pat" "$tmp" 2>/dev/null || sed -i '' "$pat" "$tmp" done if ! cmp -s "$f" "$tmp"; then mv "$tmp" "$f" echo "updated: $f" else rm -f "$tmp" fi done echo "rewrite-git-host-urls: target=$TARGET base=$BASE"