feat: add automated worktree creation script with make command

This commit is contained in:
Jon Leopard
2025-10-31 15:34:51 -07:00
parent 6ff88440ff
commit 62d3dafe56
2 changed files with 118 additions and 0 deletions

View File

@@ -116,6 +116,13 @@ k-test: ## Run tests in k8s pod
@echo "🧪 Running tests in k8s pod..."
@kubectl -n $(K8S_NS) exec deploy/web -- php artisan test
k-seed: ## Run database seeders in k8s (usage: make k-seed SEEDER=DevSeeder)
@kubectl -n $(K8S_NS) exec deploy/web -- php artisan db:seed --class=$(SEEDER)
k-migrate-fresh: ## Fresh database with seeding in k8s pod
@echo "🔄 Running fresh migration with seeding..."
@kubectl -n $(K8S_NS) exec deploy/web -- php artisan migrate:fresh --seed
k-status: ## Show k8s environment status
@echo "📊 Status for namespace: $(K8S_NS)"
@echo ""
@@ -222,6 +229,21 @@ install: ## Initial project setup
mailpit: ## Open Mailpit web UI
@open http://localhost:8025 || xdg-open http://localhost:8025 || echo "Open http://localhost:8025 in your browser"
new-worktree: ## Create new worktree (usage: make new-worktree BRANCH=feature/my-feature or make new-worktree BRANCH=feature/my-feature NEW=true)
@if [ -z "$(BRANCH)" ]; then \
echo "❌ Error: BRANCH parameter required"; \
echo ""; \
echo "Usage:"; \
echo " make new-worktree BRANCH=feature/my-feature # Checkout existing branch"; \
echo " make new-worktree BRANCH=feature/my-feature NEW=true # Create new branch"; \
exit 1; \
fi
@if [ "$(NEW)" = "true" ]; then \
./scripts/new-worktree.sh -b $(BRANCH); \
else \
./scripts/new-worktree.sh $(BRANCH); \
fi
help: ## Show this help message
@echo "\n📦 CannaBrands Docker Commands\n"
@echo "Local Development (Sail):"

96
scripts/new-worktree.sh Executable file
View File

@@ -0,0 +1,96 @@
#!/bin/bash
#
# Create a new git worktree with proper naming convention and .env setup
#
# Usage:
# ./scripts/new-worktree.sh feature/my-feature # Checkout existing branch
# ./scripts/new-worktree.sh -b feature/my-feature # Create new branch
#
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse arguments
CREATE_BRANCH=false
BRANCH=""
while [[ $# -gt 0 ]]; do
case $1 in
-b|--branch)
CREATE_BRANCH=true
shift
;;
*)
BRANCH="$1"
shift
;;
esac
done
# Validate branch name provided
if [ -z "$BRANCH" ]; then
echo -e "${RED}❌ Error: Branch name required${NC}"
echo ""
echo "Usage:"
echo " ./scripts/new-worktree.sh feature/my-feature # Checkout existing branch"
echo " ./scripts/new-worktree.sh -b feature/my-feature # Create new branch"
exit 1
fi
# Get to repo root (works from worktrees and main repo)
# git-common-dir points to .git, so get its parent
REPO_ROOT=$(dirname "$(git rev-parse --git-common-dir)")
# Derive worktree directory name from branch (strip prefix, replace slashes)
WORKTREE_NAME=$(echo "$BRANCH" | sed 's/feature\///' | sed 's/bugfix\///' | sed 's/hotfix\///' | sed 's/\//-/g')
WORKTREE_PATH="$REPO_ROOT/.worktrees/$WORKTREE_NAME"
cd "$REPO_ROOT"
echo ""
echo -e "${BLUE}📁 Creating worktree for branch: ${GREEN}$BRANCH${NC}"
echo -e "${BLUE} Directory: ${GREEN}$WORKTREE_PATH${NC}"
echo ""
# Create worktree
if [ "$CREATE_BRANCH" = true ]; then
echo -e "${YELLOW}🌿 Creating new branch and worktree...${NC}"
git worktree add -b "$BRANCH" "$WORKTREE_PATH"
else
echo -e "${YELLOW}🌿 Creating worktree from existing branch...${NC}"
git fetch origin
git worktree add "$WORKTREE_PATH" "$BRANCH"
fi
# Copy .env from example
echo -e "${YELLOW}📋 Setting up .env from template...${NC}"
if [ -f "$WORKTREE_PATH/.env.example" ]; then
cp "$WORKTREE_PATH/.env.example" "$WORKTREE_PATH/.env"
echo -e "${GREEN} ✅ Created .env from .env.example${NC}"
elif [ -f ".env" ]; then
# Fallback: copy from main repo if it exists
cp .env "$WORKTREE_PATH/.env"
echo -e "${GREEN} ✅ Copied .env from main repo${NC}"
else
echo -e "${YELLOW} ⚠️ No .env.example found - you'll need to create .env manually${NC}"
fi
echo ""
echo -e "${GREEN}✅ Worktree created successfully!${NC}"
echo ""
echo -e "${BLUE}📝 Next steps:${NC}"
echo -e " 1. ${YELLOW}cd $WORKTREE_PATH${NC}"
echo -e " 2. ${YELLOW}# Edit .env if needed (DB credentials, etc.)${NC}"
echo -e " 3. ${YELLOW}make k-dev${NC} ${BLUE}# Start k8s environment${NC}"
echo -e " 4. ${YELLOW}make k-artisan CMD=\"db:seed\"${NC} ${BLUE}# Seed database${NC}"
echo ""
echo -e "${BLUE}🌐 Your app will be available at:${NC}"
K8S_HOST=$(echo "$BRANCH" | sed 's/feature\///' | sed 's/bugfix\///' | sed 's/hotfix\///' | sed 's/\//-/g')
echo -e " ${GREEN}http://${K8S_HOST}.cannabrands.test${NC}"
echo ""