#!/usr/bin/env bash # Record one evidence command into $HTH_RUN_DIR/.log and .meta. set -euo pipefail if [[ $# -lt 2 ]]; then echo "usage: record_cmd.sh NAME command..." >&2 exit 2 fi NAME="$1" shift RUN="${HTH_RUN_DIR:?set HTH_RUN_DIR}" ROOT="${HTH_GIT_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}" mkdir -p "${RUN}" START_UTC="$(date -u +%Y-%m-%dT%H:%M:%SZ)" LOG="${RUN}/${NAME}.log" META="${RUN}/${NAME}.meta" COMMIT="${HTH_GIT_COMMIT:-}" TREE="${HTH_GIT_TREE:-}" if [[ -z "${COMMIT}" ]]; then COMMIT="$(git -C "${ROOT}" rev-parse HEAD 2>/dev/null || echo unknown)" fi if [[ -z "${TREE}" ]]; then TREE="$(git -C "${ROOT}" rev-parse 'HEAD^{tree}' 2>/dev/null || echo unknown)" fi DIRTY="${HTH_GIT_DIRTY:-}" SCOPE="${HTH_GIT_SCOPE:-all}" if [[ -z "${DIRTY}" ]]; then DIRTY="clean" if [[ "${SCOPE}" == "host" ]]; then if git -C "${ROOT}" status --porcelain -- host 2>/dev/null | grep -q .; then DIRTY="dirty" fi else if ! git -C "${ROOT}" diff --quiet 2>/dev/null || ! git -C "${ROOT}" diff --cached --quiet 2>/dev/null; then DIRTY="dirty" fi if git -C "${ROOT}" status --porcelain 2>/dev/null | grep -q .; then DIRTY="dirty" fi fi fi detect_tool() { local argv0="$1" local base base="$(basename -- "${argv0}")" case "${base}" in go) command -v go; go version 2>/dev/null || true ;; gcc|cc) command -v "${base}"; "${base}" --version 2>/dev/null | head -n 1 || true ;; python|python3|python3.11|python3.12|python3.13) command -v "${base}"; "${base}" --version 2>/dev/null || true "${base}" -c 'import google.protobuf as p; print("protobuf", getattr(p, "__version__", "unknown"))' 2>/dev/null || true ;; pip|pip3) command -v "${base}"; "${base}" --version 2>/dev/null || true ;; pytest|ruff|mypy|pyright) command -v "${base}"; "${base}" --version 2>/dev/null | head -n 1 || true ;; protoc) command -v protoc; protoc --version 2>/dev/null || true ;; protoc-gen-go) command -v protoc-gen-go; protoc-gen-go --version 2>/dev/null || true ;; buf) command -v buf; buf --version 2>/dev/null || true ;; staticcheck) command -v staticcheck; staticcheck -version 2>/dev/null || true ;; govulncheck) command -v govulncheck; govulncheck -version 2>/dev/null || true ;; qemu-system-x86_64|qemu-system-aarch64) command -v "${base}"; "${base}" --version 2>/dev/null | head -n 1 || true ;; iverilog|vvp) command -v "${base}"; "${base}" -V 2>/dev/null | head -n 1 || true ;; verilator) command -v verilator; verilator --version 2>/dev/null || true ;; *) if command -v "${argv0}" >/dev/null 2>&1; then command -v "${argv0}" fi go version 2>/dev/null || true uname -sr 2>/dev/null || true ;; esac } TOOL="$(detect_tool "$1" | tr '\n' '|' | sed 's/|$//')" { echo "run_id=${HTH_RUN_ID:-${RUN##*/}}" echo "commit=${COMMIT}" echo "tree=${TREE}" echo "dirty=${DIRTY}" echo "cwd=$(pwd)" echo "start_utc=${START_UTC}" echo "tool=${TOOL}" echo "command=$*" } > "${META}.part" set +e "$@" >"${LOG}" 2>&1 RC=$? set -e END_UTC="$(date -u +%Y-%m-%dT%H:%M:%SZ)" START_EPOCH="$(date -u -d "${START_UTC}" +%s 2>/dev/null || true)" END_EPOCH="$(date -u -d "${END_UTC}" +%s 2>/dev/null || true)" DURATION="" if [[ -n "${START_EPOCH}" && -n "${END_EPOCH}" ]]; then DURATION="$((END_EPOCH - START_EPOCH))" fi SHA="$(sha256sum "${LOG}" | awk '{print $1}')" STATUS=FAIL if [[ "${RC}" -eq 0 ]]; then STATUS=PASS elif [[ "${RC}" -eq 2 ]]; then STATUS="NOT RUN" fi { cat "${META}.part" echo "end_utc=${END_UTC}" echo "duration_s=${DURATION}" echo "exit=${RC}" echo "status=${STATUS}" echo "log_sha256=${SHA}" echo "working_directory=$(pwd)" } > "${META}" rm -f "${META}.part" echo "${NAME} status=${STATUS} exit=${RC} sha256=${SHA}" exit "${RC}"