interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / store / tests / fixstore.sh
| 1 | #!/usr/bin/env bash |
| 2 | # ORACLE: host test helper (bash, git, interplane-native ipl), not product code. |
| 3 | # store/tests/fixstore.sh RIDS_MD STORAGE ROOT: turn a test's Radicle-shaped fixture (the first table |
| 4 | # of RIDS_MD, and STORAGE/<RID> bare repositories or links to them) into a native store at ROOT, so a |
| 5 | # suite written for the Radicle build runs against the store-reading build (its STORE_VARIANT=1). For |
| 6 | # every row of the first table whose STORAGE/<RID> exists: when FIXSTORE_FROM (default |
| 7 | # ~/interplane-store-owner) holds a project of that name with that old id and the same head on the row's |
| 8 | # branch, its folder is hard-linked into ROOT (cp -al; read only: nothing here writes it); otherwise |
| 9 | # the repository is cloned bare into ROOT/projects/NAME/repo.git and recorded with ipl init and ipl |
| 10 | # import (old id rad:<RID>, signed by a throwaway key made here). ROOT/catalog.tsv is the projects' |
| 11 | # own catalog lines in table order. Rows the record rules refuse (ipl init fails) are left out and |
| 12 | # named on stderr. env: IPL_DIR (required, interplane-native with bin/ built); FIXSTORE_HARDLINK=1 |
| 13 | # clones with hard-linked objects (only for a STORAGE the test made itself, never a shared one: git |
| 14 | # never rewrites an object file, and the forge only reads). Exit 0, or 1 when no row could be recorded. |
| 15 | set -u |
| 16 | RIDS=${1:?RIDS_MD} STORAGE=${2:?STORAGE} ROOT=${3:?ROOT} |
| 17 | IPLD=${IPL_DIR:?IPL_DIR} |
| 18 | FROM=${FIXSTORE_FROM:-$HOME/interplane-store-owner} |
| 19 | mkdir -p "$ROOT/projects" || exit 1 |
| 20 | [ -f "$ROOT/.fixkey" ] || ssh-keygen -q -t ed25519 -N "" -C "store/tests/fixstore.sh throwaway key" -f "$ROOT/.fixkey" || exit 1 |
| 21 | og() { env -i PATH=/usr/bin:/bin HOME=/nonexistent GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null git "$@"; } |
| 22 | n_ok=0; : > "$ROOT/.rows" |
| 23 | while IFS=$'\t' read -r name rid br; do |
| 24 | repo=$STORAGE/${rid#rad:} |
| 25 | [ -e "$repo" ] || continue |
| 26 | [ -e "$ROOT/projects/$name" ] && continue |
| 27 | head=$(og --git-dir="$repo" rev-parse -q --verify "refs/heads/$br" 2>/dev/null) || { echo "fixstore: $name has no refs/heads/$br" >&2; continue; } |
| 28 | if [ -f "$FROM/projects/$name/records/1" ] && grep -qx "alias $rid" "$FROM/projects/$name/records/1" \ |
| 29 | && [ "$(og --git-dir="$FROM/projects/$name/repo.git" rev-parse -q --verify "refs/heads/$br" 2>/dev/null)" = "$head" ]; then |
| 30 | cp -al "$FROM/projects/$name" "$ROOT/projects/$name" || exit 1 |
| 31 | else |
| 32 | hl=--no-hardlinks; [ "${FIXSTORE_HARDLINK:-0}" = 1 ] && hl= |
| 33 | og clone -q --bare $hl "$repo" "$ROOT/projects/$name/repo.git" 2> /dev/null || { echo "fixstore: clone of $name failed" >&2; rm -rf "${ROOT:?}/projects/$name"; continue; } |
| 34 | if ! "$IPLD/ipl" init "$ROOT" "$name" "$br" public "$rid" "$ROOT/.fixkey" > /dev/null 2>> "$ROOT/.ipl.err"; then |
| 35 | echo "fixstore: $name left out: ipl init refused it ($(tail -1 "$ROOT/.ipl.err"))" >&2; rm -rf "${ROOT:?}/projects/$name"; continue |
| 36 | fi |
| 37 | echo "$name" > "$ROOT/.ev" |
| 38 | "$IPLD/ipl" import "$ROOT" "$name" "$br" "$ROOT/.ev" "$ROOT/.ev" "fix-$name" "$ROOT/.fixkey" > /dev/null 2>> "$ROOT/.ipl.err" \ |
| 39 | || { echo "fixstore: $name left out: ipl import failed ($(tail -1 "$ROOT/.ipl.err"))" >&2; rm -rf "${ROOT:?}/projects/$name"; continue; } |
| 40 | fi |
| 41 | echo "$name" >> "$ROOT/.rows"; n_ok=$((n_ok + 1)) |
| 42 | done < <(awk -F '|' '/^\| repo \| RID \| default branch/ { if (seen) exit; seen=1; getline; next } |
| 43 | seen && !/^\|/ { exit } seen { for (i = 2; i <= 4; i++) gsub(/^ +| +$/, "", $i); print $2 "\t" $3 "\t" $4 }' "$RIDS") |
| 44 | while read -r n; do cat "$ROOT/projects/$n/catalog.row"; done < "$ROOT/.rows" > "$ROOT/catalog.tsv.new" && mv "$ROOT/catalog.tsv.new" "$ROOT/catalog.tsv" |
| 45 | echo "fixstore: $n_ok projects in $ROOT ($(grep -c . "$ROOT/catalog.tsv") catalog lines)" >&2 |
| 46 | [ "$n_ok" -gt 0 ] |