Files
hub/scripts/new-worktree.sh

97 lines
3.0 KiB
Bash
Executable File

#!/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 ""