#!/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] [root] set -euo pipefail ENSURE=0 PER_REF=0 while :; do case "${1-}" in --ensure) ENSURE=1; shift ;; --per-ref) PER_REF=1; shift ;; *) break ;; esac done REF="${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] [root]" >&2 exit 2 fi name=shared if [ "$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=60 name="$(printf '%s' "$REF" | tr -c 'A-Za-z0-9._-' '-')" case "$name" in -*|.*) name="ref-$name" ;; esac if [ "${#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" fi fi dir="${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 || true if [ ! -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. TAG fi fi printf '%s\n' "$dir"