Files

120 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# export-public.sh — the publishable tree of this repo, checked, and (with
# --push) one new commit on the public mirror.
#
# scripts/export-public.sh <dest-dir> [--ref REV] [--push [GIT-URL]] [--branch main]
#
# Writes into <dest-dir> (missing or empty) the files of REV (default HEAD) —
# the `root` paths and the `public` plugins of catalog/public.json, nothing
# else: no private plugin, no .git, no .ai/, nothing untracked — then runs the
# scrub check (catalog/scrub.py + tests/test_public_scrub.py) on the export and
# refuses (exit 1) when it fails.
#
# --push GIT-URL (or $PUBLIC_REPO_URL): clones the public repo into a temp dir,
# replaces its tree with the export and commits it as ONE new commit authored
# "ai-agent plugins <noreply@gabvdl.xyz>" (a root commit the first time), then
# pushes BRANCH. Every git command of the push runs in that temp clone: the
# private repo's refs, config and history are never touched or sent.
set -euo pipefail
usage() { sed -n '2,17p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-2}"; }
REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DEST="" REF="HEAD" PUSH=0 URL="${PUBLIC_REPO_URL:-}" BRANCH="main"
AUTHOR_NAME="ai-agent plugins" AUTHOR_EMAIL="noreply@gabvdl.xyz"
while (($#)); do
case "$1" in
--ref) REF="$2"; shift 2 ;;
--branch) BRANCH="$2"; shift 2 ;;
--push)
PUSH=1
if [[ $# -gt 1 && "$2" != --* ]]; then URL="$2"; shift; fi
shift ;;
-h|--help) usage 0 ;;
--*) echo "export-public: unknown option $1" >&2; usage ;;
*) [[ -z "$DEST" ]] || usage; DEST="$1"; shift ;;
esac
done
[[ -n "$DEST" ]] || usage
if ((PUSH)) && [[ -z "$URL" ]]; then
echo "export-public: --push needs the public repo's git URL (or PUBLIC_REPO_URL)" >&2; exit 2
fi
if [[ -e "$DEST" ]] && [[ -n "$(ls -A "$DEST" 2>/dev/null)" ]]; then
echo "export-public: $DEST is not empty — give a new or empty directory" >&2; exit 2
fi
mkdir -p "$DEST"
DEST="$(cd "$DEST" && pwd)"
case "$DEST/" in "$REPO"/*) echo "export-public: $DEST is inside the repo" >&2; exit 2 ;; esac
# ── what is published, read from REV's own catalog ─────────────────────────────
CATALOG="$(git -C "$REPO" show "$REF:catalog/public.json")"
mapfile -t PATHS < <(python3 -c '
import json, sys
c = json.loads(sys.stdin.read())
for p in c.get("root") or []:
print(p)
for p in c["public"]:
print("plugins/" + p)
' <<<"$CATALOG")
for p in "${PATHS[@]}"; do
case "$p" in ""|/*|*..*|.git|.git/*|.ai|.ai/*|plugins) echo "export-public: refusing path '$p'" >&2; exit 1 ;; esac
git -C "$REPO" cat-file -e "$REF:$p" 2>/dev/null \
|| { echo "export-public: '$p' is not in $REF" >&2; exit 1; }
done
# ── the export: tracked files of REV only ──────────────────────────────────────
git -C "$REPO" archive --format=tar "$REF" -- "${PATHS[@]}" | tar -x -C "$DEST"
find "$DEST" \( -name .ai -o -name .git -o -name __pycache__ -o -name node_modules \) -prune -exec rm -rf {} +
# only the allow-listed plugins, nothing else at the root
want="$(printf '%s\n' "${PATHS[@]}" | sed -n 's#^plugins/##p' | sort)"
have="$( (ls -1 "$DEST/plugins" 2>/dev/null || true) | sort)"
if [[ "$want" != "$have" ]]; then
echo "export-public: the exported plugins differ from catalog/public.json" >&2
diff <(echo "$want") <(echo "$have") >&2 || true
exit 1
fi
# ── the scrub check, on the export itself ──────────────────────────────────────
echo "export-public: checking $DEST" >&2
export PYTHONDONTWRITEBYTECODE=1
if ! python3 -I "$DEST/catalog/scrub.py" "$DEST"; then
echo "export-public: REFUSED — banned content in the export (above)" >&2; exit 1
fi
if ! out="$(SCRUB_ROOT="$DEST" python3 -I "$DEST/tests/test_public_scrub.py" 2>&1)"; then
tail -n 30 <<<"$out" >&2
echo "export-public: REFUSED — tests/test_public_scrub.py fails on the export" >&2; exit 1
fi
find "$DEST" -name __pycache__ -prune -exec rm -rf {} +
n_files="$(find "$DEST" -type f | wc -l)"
echo "export-public: $DEST — $(wc -l <<<"$have") plugins, $n_files files, clean" >&2
((PUSH)) || exit 0
# ── one commit on the public repo ──────────────────────────────────────────────
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
pub() { git -C "$WORK/repo" -c core.hooksPath=/dev/null -c commit.gpgsign=false "$@"; }
if git clone --quiet --branch "$BRANCH" --single-branch "$URL" "$WORK/repo" 2>/dev/null; then
echo "export-public: on top of $BRANCH of $URL" >&2
else
# an empty repo, or no such branch yet: the first export is a root commit
git clone --quiet "$URL" "$WORK/repo" 2>/dev/null || { mkdir -p "$WORK/repo"; git -C "$WORK/repo" init -q; pub remote add origin "$URL"; }
pub checkout -q --orphan "$BRANCH"
pub rm -rq --cached . 2>/dev/null || true
echo "export-public: first export — a root commit on $BRANCH" >&2
fi
find "$WORK/repo" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
cp -a "$DEST/." "$WORK/repo/"
pub add -A
if pub diff --cached --quiet; then
echo "export-public: nothing changed since the last export — no commit" >&2; exit 0
fi
export GIT_AUTHOR_NAME="$AUTHOR_NAME" GIT_AUTHOR_EMAIL="$AUTHOR_EMAIL"
export GIT_COMMITTER_NAME="$AUTHOR_NAME" GIT_COMMITTER_EMAIL="$AUTHOR_EMAIL"
pub -c user.name="$AUTHOR_NAME" -c user.email="$AUTHOR_EMAIL" commit -q \
-m "Update the published plugins ($(date -u +%Y-%m-%d))"
pub push -q origin "HEAD:refs/heads/$BRANCH"
echo "export-public: pushed $(pub rev-parse --short HEAD) to $BRANCH of $URL" >&2