ondas 2.1.0

Read-only, format-independent waveform analysis library
Documentation
#!/usr/bin/env bash
set -euo pipefail

usage() {
    echo "usage: ./dev [--recreate | --exec-only] <command> [args...]" >&2
    echo "       ./dev --install-hooks" >&2
    exit 2
}

fail() {
    echo "error: $*" >&2
    exit 1
}

if [[ ${1:-} == --install-hooks ]]; then
    (( $# == 1 )) || usage
    root=$(git rev-parse --show-toplevel 2>/dev/null) || fail "not inside an Ondas Git worktree"
    git_dir=$(git rev-parse --path-format=absolute --absolute-git-dir)
    hooks_dir="$git_dir/ondas-hooks"
    git config extensions.worktreeConfig true
    configured=$(git config --get core.hooksPath || true)
    if [[ -n $configured && $configured != "$hooks_dir" ]]; then
        fail "refusing to replace existing worktree core.hooksPath: $configured"
    fi
    install -d -m 0755 "$hooks_dir"
    install -m 0755 "$root/dev" "$hooks_dir/dev"
    install -m 0755 "$root/tools/repo/git-hook" "$hooks_dir/pre-commit"
    git config --worktree core.hooksPath "$hooks_dir"
    echo "Installed host Git hook in $hooks_dir"
    exit 0
fi

recreate=0
exec_only=0
case ${1:-} in
    --recreate) recreate=1; shift ;;
    --exec-only) exec_only=1; shift ;;
esac
(( $# > 0 )) || usage

for tool in git docker devcontainer sha256sum realpath; do
    command -v "$tool" >/dev/null 2>&1 || fail "required host command not found: $tool"
done
docker info >/dev/null 2>&1 || fail "Docker daemon is unavailable"

root=$(git rev-parse --show-toplevel 2>/dev/null) || fail "not inside an Ondas Git worktree"
root=$(realpath "$root")
common_dir=$(git rev-parse --path-format=absolute --git-common-dir)
common_dir=$(realpath "$common_dir")
case "$(realpath "$PWD")/" in
    "$root/"*) ;;
    *) fail "current directory is outside the worktree root: $root" ;;
esac

if [[ -f $root/.env ]]; then
    set -a
    # shellcheck disable=SC1091
    source "$root/.env"
    set +a
fi

config_relative=${ONDAS_DEV_CONFIG:-.devcontainer/devcontainer.json}
[[ $config_relative != /* ]] || fail "ONDAS_DEV_CONFIG must be relative to the worktree root"
config=$(realpath -m "$root/$config_relative")
case "$config" in
    "$root/"*) ;;
    *) fail "ONDAS_DEV_CONFIG escapes the worktree root: $config_relative" ;;
esac
[[ -f $config ]] || fail "devcontainer config not found: $config_relative"

config_dir=$(dirname "$config")
fingerprint=$(
    {
        find "$root/.devcontainer" "$config_dir" -type f -print0 2>/dev/null \
            | sort -zu \
            | xargs -0 sha256sum
        printf 'config=%s\ngit-common=%s\n' "$config" "$common_dir"
    } | sha256sum | cut -d' ' -f1
)
worktree_id=$(printf '%s' "$root" | sha256sum | cut -d' ' -f1)
worktree_label="dev.ondas.worktree=$worktree_id"
config_label="dev.ondas.config=$fingerprint"

mapfile -t containers < <(docker ps -aq --filter "label=$worktree_label")
if (( ${#containers[@]} > 1 && ! recreate )); then
    fail "multiple containers exist for this worktree; run ./dev --recreate <command>"
fi
for container in "${containers[@]}"; do
    previous=$(docker inspect --format '{{ index .Config.Labels "dev.ondas.config" }}' "$container")
    if (( recreate )); then
        docker rm -f "$container" >/dev/null
    elif [[ $previous != "$fingerprint" ]]; then
        fail "devcontainer configuration changed; run ./dev --recreate <command>"
    fi
done

up=(
    devcontainer up
    --workspace-folder "$root"
    --config "$config"
    --id-label "$worktree_label"
    --id-label "$config_label"
)
exec_args=(
    devcontainer exec
    --workspace-folder "$root"
    --config "$config"
    --id-label "$worktree_label"
    --id-label "$config_label"
)
if [[ $common_dir != "$root/.git" ]]; then
    up+=(--mount "type=bind,source=$common_dir,target=$common_dir")
fi

if (( exec_only )); then
    (( ${#containers[@]} == 1 )) || fail "no existing container; start this worktree explicitly with: ./dev true"
    [[ $(docker inspect --format '{{.State.Running}}' "${containers[0]}") == true ]] \
        || fail "container is stopped; start this worktree explicitly with: ./dev true"
else
    "${up[@]}" >/dev/null
fi

relative_cwd=$(realpath --relative-to "$root" "$PWD")
container_cwd="/workspaces/$(basename "$root")"
[[ $relative_cwd == . ]] || container_cwd+="/$relative_cwd"

# shellcheck disable=SC2016
"${exec_args[@]}" bash -c 'cd "$1"; shift; exec "$@"' ondas-dev "$container_cwd" "$@"