#!/usr/bin/env bash# One warm Cargo target per machine. The build/cargo lock serializes its users;# cargo-target-guard.sh makes source freshness honest before each compile.# Never wipe shared from a lane. --per-ref is for Xcode output and legacy# branch cleanup only; those directories still need distinct ref names.# Usage: cargo-target-dir.sh [--ensure] [--per-ref] <ref> [root]set -euo pipefailENSURE=0PER_REF=0while :; docase"${1-}"in --ensure) ENSURE=1; shift ;; --per-ref) PER_REF=1; shift ;; *) break ;;esacdoneREF="${1-}"ROOT="${2:-${COTTON_CARGO_TARGET_ROOT:-$HOME/.cache/cottondb/cargo-target}}"if [ -z "$REF" ]; then echo "cargo-target-dir: no ref given — refusing to guess a target directory" >&2 echo "usage: cargo-target-dir.sh [--ensure] [--per-ref] <ref> [root]" >&2exit2finame=sharedif [ "$PER_REF" = 1 ]; then# Longest name we will write. Long enough to read a branch name in an `ls`,# short enough that nothing downstream meets a path-length limit.MAX=60name="$(printf '%s' "$REF" | tr -c 'A-Za-z0-9._-' '-')"case"$name"in -*|.*) name="ref-$name" ;;esacif [ "${#name}" -gt "$MAX" ]; then# The hash is of the REF, not of the truncated name: two branches whose first# 51 characters agree must still land in two directories. hash="$(printf '%s' "$REF" | shasum | cut -c1-8)" name="${name:0:$((MAX - 9))}-$hash"fifidir="${ROOT%/}/$name"if [ "$ENSURE" = 1 ]; then mkdir -p "$dir"# The lane ledger: build output, released least recently used first when# the Mac is under its reserve. Silent: this script's stdout is the path. bash "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/lane-claim.sh""$dir" --kind cargo_target --lane "${name}" >/dev/null 2>&1 || trueif [ ! -f "$dir/CACHEDIR.TAG" ]; then cat > "$dir/CACHEDIR.TAG" <<'TAG'Signature: 8a477f597d28d172789f06886806bc55# This file is a cache directory tag created by CottonDB's CI lane.# For information about cache directory tags see https://bford.info/cachedir/# It is written by scripts/ci/cargo-target-dir.sh so that `cargo clean# --target-dir` will accept this directory, and so that backup tools skip it.TAGfifiprintf '%s\n'"$dir"