interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / ipl / tools / import-radicle.sh
| 1 | #!/bin/bash |
| 2 | # import-radicle.sh ROOT KEY: one-time migration instrument (not part of the store). |
| 3 | # Copies every public project of ours from the old Radicle storage into ROOT with the reviewed |
| 4 | # snapshot tool (HardenOS gitstore/snapshot.om at a82c8504), then records it: PROJECT, IMPORT, signed. |
| 5 | # Reads the old store only. "Ours" means: this node signed refs there AND the old store names this |
| 6 | # node's key as the project's only delegate. Private projects are left out of this trial. |
| 7 | # The old store is $RAD_HOME/storage (default ~/.radicle/storage). No `rad` program is needed: the |
| 8 | # node id is IMPORT_NID (the z6Mk... key the old node signed with), and delegates, visibility, name |
| 9 | # and default branch are read with git from the project's identity document, the blob |
| 10 | # embeds/radicle.json of the commit refs/rad/id points to (the document `rad inspect` printed). |
| 11 | # ROOT/import.tsv gets one line for every repository of the old store that this node signed: name, |
| 12 | # old id, branch, old head, store head, and OK, SKIP (left out on purpose, with the reason) or |
| 13 | # REFUSED (a failure, with the reason). Repositories this node never signed get no line; they are |
| 14 | # counted. Exit 0 when nothing was refused and every project taken has the old store's head, else 1; |
| 15 | # 2 when the run could not start or the old store holds nothing. |
| 16 | set -u |
| 17 | ROOT=$1 KEY=$2 |
| 18 | here=$(cd "$(dirname "$0")/.." && pwd) |
| 19 | OLD=${RAD_HOME:-$HOME/.radicle}/storage |
| 20 | ( cd "$here/bin" && grep -q ' snapshot$' BUILD.sha256 && sha256sum -c --quiet BUILD.sha256 ) 2>/dev/null || |
| 21 | { echo "import: bin/ is not what build.sh built with the snapshot source (run build.sh OMC SHA2_OM CHAIN_OM SNAPSHOT_OM)" >&2; exit 2; } |
| 22 | [ -d "$OLD" ] || { echo "import: no old store at $OLD" >&2; exit 2; } |
| 23 | ME=${IMPORT_NID:-} |
| 24 | [[ $ME =~ ^z6Mk[1-9A-HJ-NP-Za-km-z]{44}$ ]] || { echo "import: IMPORT_NID must be the old node's key (z6Mk followed by 44 base58 characters)" >&2; exit 2; } |
| 25 | # the old store is read with no caller git settings |
| 26 | og() { env -i PATH=/usr/bin:/bin HOME=/nonexistent GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null git "$@"; } |
| 27 | mkdir -p "$ROOT/projects" "$ROOT/import-notes" || exit 2 |
| 28 | : > "$ROOT/import.tsv"; bad=0; seen=0; shopt -s nullglob |
| 29 | for d in "$OLD"/z*; do |
| 30 | rid=${d##*/}; seen=$((seen + 1)) |
| 31 | # a repository that cannot be read is a failure of this run; one this node never signed is not ours |
| 32 | if ! mine=$(og --git-dir="$d" for-each-ref --count=1 --format=x "refs/namespaces/$ME/refs/rad/sigrefs" 2>/dev/null); then |
| 33 | printf '%s\trad:%s\t-\t-\t-\tREFUSED cannot read the old repository\n' - "$rid" >> "$ROOT/import.tsv"; bad=$((bad + 1)); continue |
| 34 | fi |
| 35 | [ -n "$mine" ] || continue |
| 36 | # a project we cannot inspect is a failure of this run, not a project left out on purpose |
| 37 | if ! doc=$(og --git-dir="$d" cat-file blob refs/rad/id:embeds/radicle.json 2>/dev/null) || |
| 38 | ! dl=$(printf '%s' "$doc" | jq -er '.delegates[]' 2>/dev/null) || ! vis=$(printf '%s' "$doc" | jq -er '.visibility.type // "public"' 2>/dev/null) || |
| 39 | ! pl=$(printf '%s' "$doc" | jq -ec '.payload' 2>/dev/null); then |
| 40 | printf '%s\trad:%s\t-\t-\t-\tREFUSED cannot inspect in the old store\n' - "$rid" >> "$ROOT/import.tsv"; bad=$((bad + 1)); continue |
| 41 | fi |
| 42 | if [ "$(printf '%s\n' "$dl" | grep -c .)" != 1 ] || [ "${dl%% *}" != "did:key:$ME" ]; then |
| 43 | printf '%s\trad:%s\t-\t-\t-\tSKIP this node is not its only delegate\n' - "$rid" >> "$ROOT/import.tsv"; continue |
| 44 | fi |
| 45 | [ "$vis" = public ] || { printf '%s\trad:%s\t-\t-\t-\tSKIP not public\n' - "$rid" >> "$ROOT/import.tsv"; continue; } |
| 46 | name=$(printf '%s' "$pl" | jq -r '."xyz.radicle.project".name'); br=$(printf '%s' "$pl" | jq -r '."xyz.radicle.project".defaultBranch') |
| 47 | oldhead=$(og --git-dir="$d" rev-parse -q --verify "refs/heads/$br" 2>/dev/null) |
| 48 | row() { printf '%s\trad:%s\t%s\t%s\t%s\t%s\n' "$name" "$rid" "$br" "${oldhead:-none}" "$1" "$2" >> "$ROOT/import.tsv"; } |
| 49 | [ -n "$oldhead" ] || { row - "REFUSED no canonical head"; bad=$((bad + 1)); continue; } |
| 50 | P=$ROOT/projects/$name |
| 51 | if [ -e "$P" ]; then row - "REFUSED name already taken"; bad=$((bad + 1)); continue; fi |
| 52 | "$here/bin/landrec" project "$name" "$br" public "SHA256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "rad:$rid" 00000000000000000000000000000000 >/dev/null 2>&1 || { row - "REFUSED name or branch breaks the record rules"; bad=$((bad + 1)); continue; } |
| 53 | mkdir -p "$P" || exit 2 |
| 54 | "$here/bin/snapshot" "$d" "$P/repo.git" > "$ROOT/import-notes/$name.snapshot.log" 2>&1; src=$? |
| 55 | if [ "$src" != 0 ]; then row - "REFUSED snapshot exit $src"; bad=$((bad + 1)); continue; fi |
| 56 | ( cd "$P" && find repo.git.migration -type f -print0 2>/dev/null | sort -z | xargs -0 -r sha256sum ) > "$ROOT/import-notes/$name.evidence" |
| 57 | printf 'Imported from the old Radicle store (rad:%s) on %s by tools/import-radicle.sh.\nThis head is history taken over as it was. It was not gated or reviewed through the native route.\n' "$rid" "$(date -u +%F)" > "$ROOT/import-notes/$name.note" |
| 58 | "$here/ipl" init "$ROOT" "$name" "$br" public "rad:$rid" "$KEY" >/dev/null 2>> "$ROOT/import-notes/$name.snapshot.log" && |
| 59 | out=$("$here/ipl" import "$ROOT" "$name" "$br" "$ROOT/import-notes/$name.evidence" "$ROOT/import-notes/$name.note" "import-$rid" "$KEY" 2>> "$ROOT/import-notes/$name.snapshot.log") || |
| 60 | { row - "REFUSED record step"; bad=$((bad + 1)); continue; } |
| 61 | newhead=$(printf '%s' "$out" | cut -d' ' -f5) |
| 62 | if [ "$newhead" = "$oldhead" ]; then row "$newhead" OK; else row "$newhead" "MISMATCH"; bad=$((bad + 1)); fi |
| 63 | done |
| 64 | awk -F'\t' '{n[$6 ~ /^OK/ ? "ok" : ($6 ~ /^SKIP/ ? "skip" : "refused")]++} END {printf "import: ok %d, skipped %d, refused %d\n", n["ok"], n["skip"], n["refused"]}' "$ROOT/import.tsv" |
| 65 | echo "import: looked at $seen repositories in the old store; tools: identity from refs/rad/id:embeds/radicle.json via $(git --version), $(jq --version)" |
| 66 | [ "$seen" -gt 0 ] || { echo "import: the old store holds no repositories" >&2; exit 2; } |
| 67 | [ "$bad" = 0 ] |