|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pre-clone external nargo git dependencies referenced in Nargo.toml files under |
| 3 | +# noir-projects/ with bounded retry. Works around transient DNS / network failures |
| 4 | +# during nargo's on-demand clone (e.g. "Could not resolve host: github.com"), |
| 5 | +# which on merge-queue-heavy shards halts the entire run via parallel --halt fail=1. |
| 6 | +# |
| 7 | +# Idempotent: skips deps already cached. Safe under concurrent invocation across |
| 8 | +# parallel make targets via a per-dep flock. |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +NARGO_HOME="${NARGO_HOME:-$HOME/nargo}" |
| 13 | +ROOT="$(git rev-parse --show-toplevel)" |
| 14 | + |
| 15 | +mapfile -t deps < <( |
| 16 | + find "$ROOT/noir-projects" -name Nargo.toml -print0 \ |
| 17 | + | xargs -0 grep -hE 'git\s*=\s*"https://github\.com/[^"]+"' \ |
| 18 | + | sed -nE 's/.*git\s*=\s*"https:\/\/github\.com\/([^"]+)".*tag\s*=\s*"([^"]+)".*/\1|\2/p; |
| 19 | + t end; |
| 20 | + s/.*tag\s*=\s*"([^"]+)".*git\s*=\s*"https:\/\/github\.com\/([^"]+)".*/\2|\1/p; |
| 21 | + :end' \ |
| 22 | + | sort -u |
| 23 | +) |
| 24 | + |
| 25 | +clone_one() ( |
| 26 | + set -euo pipefail |
| 27 | + local org_repo=$1 |
| 28 | + local tag=$2 |
| 29 | + local dest="$NARGO_HOME/github.com/$org_repo/$tag" |
| 30 | + if [ -f "$dest/Nargo.toml" ]; then |
| 31 | + return 0 |
| 32 | + fi |
| 33 | + mkdir -p "$(dirname "$dest")" |
| 34 | + local lock="$NARGO_HOME/github.com/$org_repo/.$tag.lock" |
| 35 | + exec 9>"$lock" |
| 36 | + flock 9 |
| 37 | + if [ -f "$dest/Nargo.toml" ]; then |
| 38 | + return 0 |
| 39 | + fi |
| 40 | + rm -rf "$dest" |
| 41 | + local attempt |
| 42 | + for attempt in 1 2 3; do |
| 43 | + if git -c advice.detachedHead=false clone --quiet --depth 1 --branch "$tag" "https://github.com/$org_repo" "$dest"; then |
| 44 | + return 0 |
| 45 | + fi |
| 46 | + rm -rf "$dest" |
| 47 | + if [ "$attempt" -lt 3 ]; then |
| 48 | + sleep $((attempt * 2)) |
| 49 | + fi |
| 50 | + done |
| 51 | + echo "ERROR: failed to clone https://github.com/$org_repo @ $tag after 3 attempts" >&2 |
| 52 | + return 1 |
| 53 | +) |
| 54 | + |
| 55 | +failed=0 |
| 56 | +for dep in "${deps[@]}"; do |
| 57 | + IFS='|' read -r org_repo tag <<< "$dep" |
| 58 | + if ! clone_one "$org_repo" "$tag"; then |
| 59 | + failed=1 |
| 60 | + fi |
| 61 | +done |
| 62 | + |
| 63 | +exit $failed |
0 commit comments