#!/usr/bin/env bash # prep-sandbox-space.sh — host-side修补:A-1 mount 断链 host-side 重播种 # # 用法: # bash scripts/prep-sandbox-space.sh # bash scripts/prep-sandbox-space.sh --force # 强制 rm 重播种 # # 行为: # 1. 检查 /tmp/xstudio-sandbox//space/ 是否存在 # 2. 不存在或 --force → 拷贝 workspace 当前状态进去 + git checkout -B sandbox # 3. 存在 → 跳过 # 4. 输出 workspaceRoot 推断 # # 配合 dispatch-preflight.sh 自动调用 set -euo pipefail usage() { cat <<'EOF' Usage: prep-sandbox-space.sh [--force] wsHash xstudio-sandbox/ 里的 hash --force 强制 rm + 重播种(即使空间已存在) Exit codes: 0 空间就绪(已存在或刚创建) 1 参数错误 2 workspaceRoot 不可推断 3 cp / git 失败 EOF } if [ $# -lt 1 ] || [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then usage exit 0 fi WS_HASH="$1" shift FORCE="false" while [ $# -gt 0 ]; do case "$1" in --force) FORCE="true"; shift ;; *) echo "ERROR: unknown arg: $1" >&2; usage >&2; exit 1 ;; esac done SPACE_BASE="/tmp/xstudio-sandbox" SPACE_DIR="$SPACE_BASE/$WS_HASH/space" # 推断 workspaceRoot WORKSPACE_ROOT="${WORKSPACE_ROOT:-}" if [ -z "$WORKSPACE_ROOT" ]; then # 兜底:从 macOS 上 xstudio 的 last_workspace 文件读 if [ -f "$HOME/Library/Application Support/XStudio/last_workspace" ]; then WORKSPACE_ROOT=$(cat "$HOME/Library/Application Support/XStudio/last_workspace" 2>/dev/null || echo "") fi fi if [ -z "$WORKSPACE_ROOT" ] || [ ! -d "$WORKSPACE_ROOT" ]; then echo "ERROR: workspaceRoot 不可推断(\$WORKSPACE_ROOT 空 + last_workspace 不存在/无效)" >&2 exit 2 fi echo "[prep-sandbox-space] workspaceRoot=$WORKSPACE_ROOT" echo "[prep-sandbox-space] spaceDir=$SPACE_DIR" echo "[prep-sandbox-space] force=$FORCE" if [ -d "$SPACE_DIR" ] && [ "$FORCE" != "true" ]; then echo "[prep-sandbox-space] 空间已存在,跳过重播种" exit 0 fi if [ "$FORCE" = "true" ] && [ -d "$SPACE_DIR" ]; then echo "[prep-sandbox-space] --force: 删旧空间" rm -rf "$SPACE_DIR" fi # 拷贝 workspace 当前状态 mkdir -p "$SPACE_DIR" if ! cp -R "$WORKSPACE_ROOT/." "$SPACE_DIR/"; then echo "ERROR: cp $WORKSPACE_ROOT/. → $SPACE_DIR 失败" >&2 exit 3 fi # 在空间里 checkout sandbox 分支 cd "$SPACE_DIR" if ! git checkout -B sandbox 2>&1 | tail -3; then echo "ERROR: git checkout -B sandbox 失败" >&2 exit 3 fi echo "[prep-sandbox-space] 重播种完成" git log --oneline | head -3