mcp-stream-workflow/hooks/verify-worktree-location
2025-12-27 19:07:51 -08:00

137 lines
4.2 KiB
Bash
Executable file

#!/bin/bash
# verify-worktree-location - Verify we're in a worktree, not main directory
#
# Purpose: Prevent agents from making changes in the main directory
# Usage: ./.git-hooks/verify-worktree-location
#
# Installation: Copy to .git-hooks/verify-worktree-location and chmod +x
#
# Exit codes:
# 0 - In a worktree (safe to proceed)
# 1 - In main directory (BLOCKED)
# 2 - Unknown location (warning)
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function: Print colored message
log_info() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_blocked() { echo -e "${RED}[BLOCKED]${NC} $1"; }
# Get current directory and project info
CURRENT_DIR=$(pwd)
# Detect project root and name dynamically
PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
PROJECT_NAME=$(basename "$PROJECT_ROOT")
PROJECT_PARENT=$(dirname "$PROJECT_ROOT")
WORKTREE_ROOT="${PROJECT_PARENT}/${PROJECT_NAME}-worktrees"
# Check if we're in a worktree
is_in_worktree() {
# Method 1: Check if path contains -worktrees/
if [[ "$CURRENT_DIR" == *"-worktrees/"* ]]; then
return 0
fi
# Method 2: Check git worktree list
if git rev-parse --git-dir 2>/dev/null | grep -q "worktrees"; then
return 0
fi
return 1
}
# Extract stream name from worktree path
get_stream_name() {
# Try to extract from path like /path/project-worktrees/stream-XX-name/...
echo "$CURRENT_DIR" | grep -oP '[^/]*-worktrees/\K[^/]+' || echo "unknown"
}
# Check if we're in a worktree
if is_in_worktree; then
STREAM_NAME=$(get_stream_name)
log_info "Worktree verification: PASSED"
log_info "Location: $CURRENT_DIR"
log_info "Stream: $STREAM_NAME"
# Verify we're on the correct branch
CURRENT_BRANCH=$(git branch --show-current)
if [[ "$CURRENT_BRANCH" == "$STREAM_NAME" ]]; then
log_info "Branch: $CURRENT_BRANCH (matches worktree)"
else
log_warn "Branch: $CURRENT_BRANCH (expected: $STREAM_NAME)"
log_warn "You may be on the wrong branch!"
fi
echo ""
log_info "Safe to proceed with development work."
exit 0
elif [[ "$CURRENT_DIR" == "$PROJECT_ROOT"* ]] && [[ "$CURRENT_DIR" != *"-worktrees"* ]]; then
# We're in the main directory - BLOCK
log_blocked "========================================="
log_blocked "LOCATION VERIFICATION FAILED"
log_blocked "========================================="
log_error ""
log_error "You are in the MAIN directory:"
log_error " Current: $CURRENT_DIR"
log_error " Project: $PROJECT_NAME"
log_error ""
log_error "Expected worktree pattern:"
log_error " ${WORKTREE_ROOT}/stream-XX-name"
log_error ""
log_error "Making changes in the main directory will result in:"
log_error " - Agent collisions"
log_error " - Work being staged/stashed and lost"
log_error " - Merge conflicts between concurrent agents"
log_error ""
log_error "Action required:"
log_error " 1. Navigate to existing worktree:"
log_error " cd ${WORKTREE_ROOT}/stream-XX-name"
log_error ""
log_error " 2. Or create new worktree:"
log_error " git worktree add ${WORKTREE_ROOT}/stream-XX-name -b stream-XX-name"
log_error ""
log_error " 3. Then verify location:"
log_error " ./.git-hooks/verify-worktree-location"
log_error ""
log_blocked "========================================="
exit 1
else
# Unknown location
log_warn "========================================="
log_warn "LOCATION VERIFICATION: UNKNOWN"
log_warn "========================================="
log_warn ""
log_warn "Current directory: $CURRENT_DIR"
log_warn ""
log_warn "This doesn't match expected patterns:"
log_warn " - Worktree: ${WORKTREE_ROOT}/stream-*"
log_warn " - Main: $PROJECT_ROOT"
log_warn ""
log_warn "If you're working on a stream, you should be in a worktree."
log_warn "If this is intentional (e.g., documentation, scripts), you may proceed."
log_warn ""
# Check if git repo
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
CURRENT_BRANCH=$(git branch --show-current)
log_warn "Current branch: $CURRENT_BRANCH"
fi
log_warn "========================================="
exit 2
fi