Cotton code
78 lines · 3.4 KB · ShellRaw
#!/usr/bin/env bash# Persistent iOS walk output. Evidence is never pruned by the producer.## On 4 Oct 2026 the smoke's prune deleted most of a home folder: it removed# "every sibling of the output directory except the last ten", and the output# directory had been set to $HOME/x — so its siblings were ~/Code, ~/Desktop,# ~/Documents. The rules below are what make that impossible, and# scripts/__tests__/smoke-out.test.ts holds each against a fake home.## Runs live in ~/cotton-captures/ios-smoke and carry .cotton-evidence.# Caller paths are judged before writing; no run authorizes deleting another.## Sourced, never run. No `set -e` of its own.SMOKE_RUN_MARKER=".cotton-ios-smoke-run"# The one root default runs live under.# Is this path inside a git work tree? Its nearest existing ancestor is asked,# so a folder not made yet is judged by where it would be made.## WALK EVIDENCE NEVER ENTERS A REPOSITORY (owner, 10 Oct 2026): screenshots,# accessibility dumps and logs of a walk are the household's evidence and live# in Cotton; a run folder inside a checkout is one `git add -A` from a commit.out_in_repository() {  local probe="$1"  while [ -n "$probe" ] && [ ! -e "$probe" ]; do probe="$(dirname "$probe")"; done  [ -n "$probe" ] || return 1  [ -d "$probe" ] || probe="$(dirname "$probe")"  git -C "$probe" rev-parse --show-toplevel >/dev/null 2>&1}smoke_out_root() { printf '%s/cotton-captures/ios-smoke' "$HOME"; }# Why a caller-named output directory is refused, on stdout; exit 1 when it is.smoke_out_refusal() {  local out="$1" home="${HOME%/}" rest  case "$out" in    /*) ;;    *) echo "the output directory must be an absolute path: $out"; return 1 ;;  esac  case "$out" in    */.cache/*) echo "evidence belongs in cotton-captures, never a cache: $out"; return 1 ;;    */../* | */.. | */./* | */.) echo "the output directory must not contain . or .. : $out"; return 1 ;;  esac  [ -n "$home" ] && [ "$home" != "/" ] || { echo "HOME is not set to a folder, so no output directory can be judged"; return 1; }  out="${out%/}"  case "$out" in    "$home"/*) rest="${out#"$home"/}" ;;    *) echo "the output directory must be under $home: $out"; return 1 ;;  esac  case "$rest" in    */*) ;;    *) echo "the output directory must be at least two levels below $home (for example $home/smoke/run-1), never directly under it: $out"; return 1 ;;  esac  if [ -L "$out" ]; then    echo "the output directory must not be a symlink: $out"; return 1  fi  if out_in_repository "$out"; then    echo "the output directory is inside a git repository — walk evidence lives in Cotton, never in a repository: $out"; return 1  fi  if [ -e "$out" ]; then    [ -d "$out" ] || { echo "the output path exists and is not a directory: $out"; return 1; }    [ -z "$(ls -A "$out" 2>/dev/null)" ] || { echo "the output directory already holds files — name an empty or new one: $out"; return 1; }  fi  return 0}# A new default run directory for <branch>, made and marked; its path on stdout.smoke_out_default() {  local branch out  branch="$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '-')"  case "$branch" in "" | . | ..) branch="detached" ;; esac  out="$(smoke_out_root)/$branch/$(date -u +%Y%m%dT%H%M%SZ)"  python3 "$(dirname "${BASH_SOURCE[0]}")/evidence.py" begin "$out" >/dev/null || return 1  : > "$out/$SMOKE_RUN_MARKER" || return 1  : > "$out/.cotton-evidence" || return 1  printf '%s' "$out"}