router.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # Worktree Skill Router
  4. # Manages git worktrees as sibling directories with isolated ports and envs
  5. #############################################################################
  6. set -e
  7. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  8. show_help() {
  9. cat << 'HELP'
  10. Git Worktree Skill
  11. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  12. Usage: router.sh [COMMAND] [OPTIONS]
  13. COMMANDS:
  14. create <branch> [name] Create a new worktree as a sibling directory
  15. remove <name> [--keep-branch] Remove a worktree and clean up
  16. list List all worktrees for this repo
  17. ports Show the port index table
  18. help Show this help message
  19. EXAMPLES:
  20. bash .opencode/skills/worktree/router.sh create feature/auth
  21. bash .opencode/skills/worktree/router.sh create feature/auth feature-auth
  22. bash .opencode/skills/worktree/router.sh list
  23. bash .opencode/skills/worktree/router.sh remove feature-auth
  24. bash .opencode/skills/worktree/router.sh remove feature-auth --keep-branch
  25. bash .opencode/skills/worktree/router.sh ports
  26. WORKTREE LOCATION:
  27. Worktrees are created as siblings to the main repo directory:
  28. ~/…/github/my-app/ ← main repo (you are here)
  29. ~/…/github/my-app-feature-auth/ ← created worktree
  30. PORT SCHEME (BASE + INDEX * 10):
  31. Index 0 (main): web=3000 admin=3001 api=3002 db=5432 redis=6379
  32. Index 1: web=3010 admin=3011 api=3012 db=5442 redis=6389
  33. Index 2: web=3020 admin=3021 api=3022 db=5452 redis=6399
  34. For detailed documentation, see: .opencode/skills/worktree/SKILL.md
  35. HELP
  36. }
  37. # Find project root
  38. find_git_root() {
  39. local dir
  40. dir="$(pwd)"
  41. while [ "$dir" != "/" ]; do
  42. if [ -d "$dir/.git" ]; then
  43. echo "$dir"
  44. return 0
  45. fi
  46. dir="$(dirname "$dir")"
  47. done
  48. echo "ERROR: Not inside a git repository" >&2
  49. return 1
  50. }
  51. cmd_list() {
  52. local main_repo
  53. main_repo="$(find_git_root)"
  54. echo "Worktrees for $(basename "$main_repo"):"
  55. echo ""
  56. git -C "$main_repo" worktree list
  57. }
  58. cmd_ports() {
  59. local main_repo
  60. main_repo="$(find_git_root)"
  61. local count
  62. count=$(git -C "$main_repo" worktree list | wc -l | xargs)
  63. echo "Port index table (BASE + INDEX * 10):"
  64. echo ""
  65. printf "%-6s %-10s %-6s %-7s %-5s %-6s %-7s\n" "Index" "Name" "Web" "Admin" "API" "DB" "Redis"
  66. echo "─────────────────────────────────────────────────────"
  67. local i=0
  68. while IFS= read -r line; do
  69. local path branch
  70. path="$(echo "$line" | awk '{print $1}')"
  71. branch="$(echo "$line" | awk '{print $NF}' | tr -d '[]')"
  72. local name
  73. name="$(basename "$path")"
  74. printf "%-6s %-10s %-6s %-7s %-5s %-6s %-7s\n" \
  75. "$i" "${name:0:10}" \
  76. "$((3000 + i * 10))" "$((3001 + i * 10))" "$((3002 + i * 10))" \
  77. "$((5432 + i * 10))" "$((6379 + i * 10))"
  78. i=$((i + 1))
  79. done < <(git -C "$main_repo" worktree list)
  80. local next_idx=$((count - 1))
  81. echo ""
  82. echo "Next worktree will use index $next_idx:"
  83. printf " web=%-6s admin=%-6s api=%-6s db=%-6s redis=%s\n" \
  84. "$((3000 + next_idx * 10))" "$((3001 + next_idx * 10))" \
  85. "$((3002 + next_idx * 10))" "$((5432 + next_idx * 10))" \
  86. "$((6379 + next_idx * 10))"
  87. }
  88. # No arguments — show help
  89. if [ $# -eq 0 ]; then
  90. show_help
  91. exit 0
  92. fi
  93. COMMAND="$1"
  94. shift
  95. case "$COMMAND" in
  96. create)
  97. bash "$SCRIPT_DIR/scripts/wt-new.sh" "$@"
  98. ;;
  99. remove)
  100. bash "$SCRIPT_DIR/scripts/wt-close.sh" "$@"
  101. ;;
  102. list)
  103. cmd_list
  104. ;;
  105. ports)
  106. cmd_ports
  107. ;;
  108. help|-h|--help)
  109. show_help
  110. ;;
  111. *)
  112. echo "ERROR: Unknown command: $COMMAND"
  113. echo ""
  114. show_help
  115. exit 1
  116. ;;
  117. esac