Files
proxmox/scripts/push-hybx-sidecars-to-gitea.sh
defiQUG bea1903ac9
Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
Sync all local changes: docs, config, scripts, submodule refs, verification evidence
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-21 15:46:06 -08:00

125 lines
4.6 KiB
Bash

#!/usr/bin/env bash
# Push all nine HYBX sidecar repos to Gitea (HYBX org).
# Usage: GITEA_TOKEN=xxx bash scripts/push-hybx-sidecars-to-gitea.sh [--dry-run] [--pull-first|--sync] [REPO_NAME]
# Optional: HYBX_SIDECARS_BASE=/path (default: /home/intlc/projects/HYBX_Sidecars)
# Optional: ONLY_REPO=name or pass REPO_NAME as first non-flag arg to sync a single repo.
# Requires: GITEA_TOKEN with push access to gitea.d-bis.org HYBX org.
# --pull-first / --sync: pull from gitea (stashing local changes if needed) then push; syncs all sidecars and docs.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Repo root = parent of scripts/ (proxmox)
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# Load GITEA_TOKEN from repo root .env (allow unset vars during source)
if [ -f "$PROJECT_ROOT/.env" ]; then
set +u
source "$PROJECT_ROOT/.env"
set -u
fi
GITEA_ORG="${GITEA_ORG:-HYBX}"
GITEA_URL="${GITEA_URL:-https://gitea.d-bis.org}"
BASE="${HYBX_SIDECARS_BASE:-/home/intlc/projects/HYBX_Sidecars}"
DRY_RUN=false
PULL_FIRST=false
ONLY_REPO="${ONLY_REPO:-}"
for arg in "${1:-}" "${2:-}" "${3:-}"; do
[[ "$arg" == "--dry-run" ]] && DRY_RUN=true
[[ "$arg" == "--pull-first" || "$arg" == "--sync" ]] && PULL_FIRST=true
# Single-repo: first non-flag arg
if [[ -n "$arg" && "$arg" != "--dry-run" && "$arg" != "--pull-first" && "$arg" != "--sync" ]]; then
ONLY_REPO="${ONLY_REPO:-$arg}"
fi
done
# Nine HYBX sidecar repos (see docs/11-references/GITEA_HYBX_ORGANIZATION_AND_REPOS.md)
SIDECAR_REPOS=(
mifos-fineract-sidecar
mt103-hardcopy-sidecar
off-ledger-2-on-ledger-sidecar
securitization-engine-sidecar
card-networks-sidecar
securities-sidecar
flash-loan-xau-sidecar
server-funds-sidecar
docs-for-sidecars
)
if [ -z "${GITEA_TOKEN:-}" ] && [ "$DRY_RUN" = false ]; then
echo "Set GITEA_TOKEN to push to Gitea (e.g. from Gitea Settings → Applications)."
exit 1
fi
echo "Gitea: $GITEA_URL | Org: $GITEA_ORG | Base: $BASE | DRY_RUN=$DRY_RUN | PULL_FIRST=$PULL_FIRST"
# Use credential helper so GITEA_TOKEN is never echoed in git output (remote URL stays token-free)
export GITEA_TOKEN
CRED_HELPER="!f() { echo \"username=git\"; echo \"password=\$GITEA_TOKEN\"; }; f"
ok=0
skip=0
fail=0
for name in "${SIDECAR_REPOS[@]}"; do
if [ -n "$ONLY_REPO" ] && [ "$name" != "$ONLY_REPO" ]; then
continue
fi
dir="$BASE/$name"
if [ ! -d "$dir" ]; then
echo " $name: skip (dir not found: $dir)"
((skip++)) || true
continue
fi
if [ ! -d "$dir/.git" ]; then
echo " $name: skip (not a git repo)"
((skip++)) || true
continue
fi
branch=$(git -C "$dir" branch --show-current 2>/dev/null || echo "main")
if [ "$DRY_RUN" = true ]; then
echo " [DRY-RUN] $name: would push branch $branch"
((ok++)) || true
continue
fi
if ! git -C "$dir" remote get-url gitea &>/dev/null; then
git -C "$dir" remote add gitea "$GITEA_URL/$GITEA_ORG/$name.git"
fi
# If merge in progress (e.g. docs-for-sidecars after conflict resolve), complete it before pull/push
if [ -f "$dir/.git/MERGE_HEAD" ]; then
echo " $name: completing merge..."
git -C "$dir" add README.md 2>/dev/null || true
git -C "$dir" add -A 2>/dev/null || true
git -C "$dir" commit -m "Merge gitea/main into main" --no-edit 2>/dev/null && echo " $name: merge commit done" || true
fi
if [ "$PULL_FIRST" = true ]; then
remote=gitea
git -C "$dir" remote get-url "$remote" &>/dev/null || remote=origin
# Skip pull if we still have unmerged files (merge just completed or unresolved)
if [ -f "$dir/.git/MERGE_HEAD" ]; then
echo " $name: skip pull (merge still in progress)"
elif git -C "$dir" -c "credential.helper=$CRED_HELPER" fetch "$remote" "$branch" 2>/dev/null; then
if ! git -C "$dir" merge-base --is-ancestor "$remote/$branch" "$branch" 2>/dev/null; then
echo " $name: pulling from $remote/$branch..."
stashed=false
if ! git -C "$dir" diff --quiet 2>/dev/null || ! git -C "$dir" diff --cached --quiet 2>/dev/null; then
git -C "$dir" stash push -m "push-hybx-sidecars: before pull" 2>/dev/null && stashed=true
fi
git -C "$dir" -c "credential.helper=$CRED_HELPER" pull --rebase "$remote" "$branch" 2>&1 || true
if [ "$stashed" = true ]; then
git -C "$dir" stash pop 2>/dev/null || true
fi
fi
fi
fi
if git -C "$dir" -c "credential.helper=$CRED_HELPER" push gitea "$branch" --set-upstream 2>&1; then
echo " $name: pushed ($branch)"
((ok++)) || true
else
echo " $name: push failed" >&2
((fail++)) || true
fi
done
echo ""
echo "Done: $ok pushed, $skip skipped, $fail failed"
[ "$fail" -gt 0 ] && exit 1
exit 0