create-new-feature.sh RawHistoryFolder
1#!/usr/bin/env bash
2# Create a new feature with branch, directory structure, and template
3# Usage: ./create-new-feature.sh "feature description"
4# ./create-new-feature.sh --json "feature description"
5
6set -e
7
8JSON_MODE=false
9
10# Collect non-flag args
11ARGS=()
12for arg in "$@"; do
13 case "$arg" in
14 --json)
15 JSON_MODE=true
16 ;;
17 --help|-h)
18 echo "Usage: $0 [--json] <feature_description>"; exit 0 ;;
19 *)
20 ARGS+=("$arg") ;;
21 esac
22done
23
24FEATURE_DESCRIPTION="${ARGS[*]}"
25if [ -z "$FEATURE_DESCRIPTION" ]; then
26 echo "Usage: $0 [--json] <feature_description>" >&2
27 exit 1
28fi
29
30# Get repository root
31REPO_ROOT=$(git rev-parse --show-toplevel)
32SPECS_DIR="$REPO_ROOT/specs"
33
34# Create specs directory if it doesn't exist
35mkdir -p "$SPECS_DIR"
36
37# Find the highest numbered feature directory
38HIGHEST=0
39if [ -d "$SPECS_DIR" ]; then
40 for dir in "$SPECS_DIR"/*; do
41 if [ -d "$dir" ]; then
42 dirname=$(basename "$dir")
43 number=$(echo "$dirname" | grep -o '^[0-9]\+' || echo "0")
44 number=$((10#$number))
45 if [ "$number" -gt "$HIGHEST" ]; then
46 HIGHEST=$number
47 fi
48 fi
49 done
50fi
51
52# Generate next feature number with zero padding
53NEXT=$((HIGHEST + 1))
54FEATURE_NUM=$(printf "%03d" "$NEXT")
55
56# Create branch name from description
57BRANCH_NAME=$(echo "$FEATURE_DESCRIPTION" | \
58 tr '[:upper:]' '[:lower:]' | \
59 sed 's/[^a-z0-9]/-/g' | \
60 sed 's/-\+/-/g' | \
61 sed 's/^-//' | \
62 sed 's/-$//')
63
64# Extract 2-3 meaningful words
65WORDS=$(echo "$BRANCH_NAME" | tr '-' '\n' | grep -v '^$' | head -3 | tr '\n' '-' | sed 's/-$//')
66
67# Final branch name
68BRANCH_NAME="${FEATURE_NUM}-${WORDS}"
69
70# Create and switch to new branch
71git checkout -b "$BRANCH_NAME"
72
73# Create feature directory
74FEATURE_DIR="$SPECS_DIR/$BRANCH_NAME"
75mkdir -p "$FEATURE_DIR"
76
77# Copy template if it exists
78TEMPLATE="$REPO_ROOT/templates/spec-template.md"
79SPEC_FILE="$FEATURE_DIR/spec.md"
80
81if [ -f "$TEMPLATE" ]; then
82 cp "$TEMPLATE" "$SPEC_FILE"
83else
84 echo "Warning: Template not found at $TEMPLATE" >&2
85 touch "$SPEC_FILE"
86fi
87
88if $JSON_MODE; then
89 printf '{"BRANCH_NAME":"%s","SPEC_FILE":"%s","FEATURE_NUM":"%s"}\n' \
90 "$BRANCH_NAME" "$SPEC_FILE" "$FEATURE_NUM"
91else
92 # Output results for the LLM to use (legacy key: value format)
93 echo "BRANCH_NAME: $BRANCH_NAME"
94 echo "SPEC_FILE: $SPEC_FILE"
95 echo "FEATURE_NUM: $FEATURE_NUM"
96fi