create-new-feature.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env bash
  2. set -e
  3. JSON_MODE=false
  4. ARGS=()
  5. for arg in "$@"; do
  6. case "$arg" in
  7. --json) JSON_MODE=true ;;
  8. --help|-h) echo "Usage: $0 [--json] <feature_description>"; exit 0 ;;
  9. *) ARGS+=("$arg") ;;
  10. esac
  11. done
  12. FEATURE_DESCRIPTION="${ARGS[*]}"
  13. if [ -z "$FEATURE_DESCRIPTION" ]; then
  14. echo "Usage: $0 [--json] <feature_description>" >&2
  15. exit 1
  16. fi
  17. # Removed custom branch management check
  18. # Function to find the repository root by searching for existing project markers
  19. find_repo_root() {
  20. local dir="$1"
  21. while [ "$dir" != "/" ]; do
  22. if [ -d "$dir/.git" ] || [ -d "$dir/.specify" ]; then
  23. echo "$dir"
  24. return 0
  25. fi
  26. dir="$(dirname "$dir")"
  27. done
  28. return 1
  29. }
  30. # Resolve repository root. Prefer git information when available, but fall back
  31. # to searching for repository markers so the workflow still functions in repositories that
  32. # were initialised with --no-git.
  33. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  34. if git rev-parse --show-toplevel >/dev/null 2>&1; then
  35. REPO_ROOT=$(git rev-parse --show-toplevel)
  36. HAS_GIT=true
  37. else
  38. REPO_ROOT="$(find_repo_root "$SCRIPT_DIR")"
  39. if [ -z "$REPO_ROOT" ]; then
  40. echo "Error: Could not determine repository root. Please run this script from within the repository." >&2
  41. exit 1
  42. fi
  43. HAS_GIT=false
  44. fi
  45. cd "$REPO_ROOT"
  46. # Branch management check removed for simplified workflow
  47. SPECS_DIR="$REPO_ROOT/specs"
  48. mkdir -p "$SPECS_DIR"
  49. HIGHEST=0
  50. if [ -d "$SPECS_DIR" ]; then
  51. for dir in "$SPECS_DIR"/*; do
  52. [ -d "$dir" ] || continue
  53. dirname=$(basename "$dir")
  54. number=$(echo "$dirname" | grep -o '^[0-9]\+' || echo "0")
  55. number=$((10#$number))
  56. if [ "$number" -gt "$HIGHEST" ]; then HIGHEST=$number; fi
  57. done
  58. fi
  59. NEXT=$((HIGHEST + 1))
  60. FEATURE_NUM=$(printf "%03d" "$NEXT")
  61. BRANCH_NAME=$(echo "$FEATURE_DESCRIPTION" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//' | sed 's/-$//')
  62. WORDS=$(echo "$BRANCH_NAME" | tr '-' '\n' | grep -v '^$' | head -3 | tr '\n' '-' | sed 's/-$//')
  63. BRANCH_NAME="${FEATURE_NUM}-${WORDS}"
  64. if [ "$HAS_GIT" = true ]; then
  65. git checkout -b "$BRANCH_NAME"
  66. else
  67. >&2 echo "[specify] Warning: Git repository not detected; skipped branch creation for $BRANCH_NAME"
  68. fi
  69. FEATURE_DIR="$SPECS_DIR/$BRANCH_NAME"
  70. mkdir -p "$FEATURE_DIR"
  71. TEMPLATE="$REPO_ROOT/.specify/templates/spec-template.md"
  72. SPEC_FILE="$FEATURE_DIR/spec.md"
  73. if [ -f "$TEMPLATE" ]; then cp "$TEMPLATE" "$SPEC_FILE"; else touch "$SPEC_FILE"; fi
  74. # Set the SPECIFY_FEATURE environment variable for the current session
  75. export SPECIFY_FEATURE="$BRANCH_NAME"
  76. if $JSON_MODE; then
  77. printf '{"BRANCH_NAME":"%s","SPEC_FILE":"%s","FEATURE_NUM":"%s"}\n' "$BRANCH_NAME" "$SPEC_FILE" "$FEATURE_NUM"
  78. else
  79. echo "BRANCH_NAME: $BRANCH_NAME"
  80. echo "SPEC_FILE: $SPEC_FILE"
  81. echo "FEATURE_NUM: $FEATURE_NUM"
  82. echo "SPECIFY_FEATURE environment variable set to: $BRANCH_NAME"
  83. fi