#!/usr/bin/env bash # Push the CONTENTS of a source dir to a Gitea repo (flat, at repo root or a subdir). # Connection (GIT_HOST/GIT_USER/GIT_TOKEN) comes from env or config/gitea.env. # Usage: bash push_to_gitea.sh --repo --src [--subdir ] [--message "msg"] set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" CFG="$HERE/../config/gitea.env" if [[ -f "$CFG" ]]; then while IFS='=' read -r k v; do [[ "$k" =~ ^[A-Z_]+$ ]] || continue case "$k" in GIT_HOST|GIT_USER|GIT_TOKEN) [[ -z "${!k:-}" ]] && export "$k=$v" ;; esac done < "$CFG" fi HOST="${GIT_HOST:-}" USER_="${GIT_USER:-}" TOKEN="${GIT_TOKEN:-}" REPO="" SRC="" SUBDIR="" MSG="" while [[ $# -gt 0 ]]; do case "$1" in --repo) REPO="$2"; shift 2;; --src) SRC="$2"; shift 2;; --subdir) SUBDIR="$2"; shift 2;; --message) MSG="$2"; shift 2;; *) echo "unknown arg: $1" >&2; exit 2;; esac; done [[ -z "$REPO" || -z "$SRC" || -z "$HOST" || -z "$TOKEN" ]] && { echo "need --repo, --src, and GIT_HOST/GIT_TOKEN" >&2; exit 2; } [[ -d "$SRC" ]] || { echo "ERROR: src not found: $SRC" >&2; exit 2; } [[ -z "$MSG" ]] && MSG="Update from library-manager" HOST="${HOST#http://}"; HOST="${HOST#https://}"; HOST="${HOST%/}" CRED="${USER_:+$USER_:}${TOKEN}" URL="https://${CRED}@${HOST}/${REPO}.git" scrub(){ sed -e "s/${TOKEN}/***/g"; } WORK="$(mktemp -d)" git clone "$URL" "$WORK" 2>&1 | scrub || { echo "ERROR: clone failed (repo must exist + host reachable + token scope)"; exit 3; } cd "$WORK"; git checkout -B main >/dev/null 2>&1 || true DEST="$WORK${SUBDIR:+/$SUBDIR}"; mkdir -p "$DEST" cp -r "$SRC/." "$DEST/" git config user.email "datasheet-bot@local"; git config user.name "library-manager" git add -A if git diff --cached --quiet; then echo "Nothing new for $REPO."; else git commit -m "$MSG" >/dev/null; git push -u origin main 2>&1 | scrub fi echo "Pushed to https://${HOST}/${REPO}${SUBDIR:+/$SUBDIR} ($(find "$SRC" -type f | wc -l | tr -d ' ') file(s))"