Skip to content

Commit 1f3d9b5

Browse files
committed
feat: check remote branches to prevent duplicate branch numbers
- Add --number parameter to create-new-feature scripts (bash & PowerShell) - Add check_existing_branches() function to fetch and scan remote branches - Update branch numbering logic to check remotes before creating new branches - Update /speckit.specify command to document remote branch checking workflow - Prevents duplicate branch numbers when branches exist on remotes but not locally - Maintains backward compatibility with existing workflows - Falls back to local directory scanning when Git is not available
1 parent 926836e commit 1f3d9b5

File tree

3 files changed

+151
-38
lines changed

3 files changed

+151
-38
lines changed

scripts/bash/create-new-feature.sh

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44

55
JSON_MODE=false
66
SHORT_NAME=""
7+
BRANCH_NUMBER=""
78
ARGS=()
89
i=1
910
while [ $i -le $# ]; do
@@ -26,17 +27,31 @@ while [ $i -le $# ]; do
2627
fi
2728
SHORT_NAME="$next_arg"
2829
;;
30+
--number)
31+
if [ $((i + 1)) -gt $# ]; then
32+
echo 'Error: --number requires a value' >&2
33+
exit 1
34+
fi
35+
i=$((i + 1))
36+
next_arg="${!i}"
37+
if [[ "$next_arg" == --* ]]; then
38+
echo 'Error: --number requires a value' >&2
39+
exit 1
40+
fi
41+
BRANCH_NUMBER="$next_arg"
42+
;;
2943
--help|-h)
30-
echo "Usage: $0 [--json] [--short-name <name>] <feature_description>"
44+
echo "Usage: $0 [--json] [--short-name <name>] [--number N] <feature_description>"
3145
echo ""
3246
echo "Options:"
3347
echo " --json Output in JSON format"
3448
echo " --short-name <name> Provide a custom short name (2-4 words) for the branch"
49+
echo " --number N Specify branch number manually (overrides auto-detection)"
3550
echo " --help, -h Show this help message"
3651
echo ""
3752
echo "Examples:"
3853
echo " $0 'Add user authentication system' --short-name 'user-auth'"
39-
echo " $0 'Implement OAuth2 integration for API'"
54+
echo " $0 'Implement OAuth2 integration for API' --number 5"
4055
exit 0
4156
;;
4257
*)
@@ -48,7 +63,7 @@ done
4863

4964
FEATURE_DESCRIPTION="${ARGS[*]}"
5065
if [ -z "$FEATURE_DESCRIPTION" ]; then
51-
echo "Usage: $0 [--json] [--short-name <name>] <feature_description>" >&2
66+
echo "Usage: $0 [--json] [--short-name <name>] [--number N] <feature_description>" >&2
5267
exit 1
5368
fi
5469

@@ -65,6 +80,28 @@ find_repo_root() {
6580
return 1
6681
}
6782

83+
# Function to check existing branches (local and remote) and return next available number
84+
check_existing_branches() {
85+
local short_name="$1"
86+
87+
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
88+
git fetch --all --prune 2>/dev/null || true
89+
90+
# Find all branches matching the pattern (local and remote)
91+
local branches=$(git branch -a 2>/dev/null | grep -E "feature/[0-9]+-${short_name}$" | sed 's/.*feature\///' | sed "s/-${short_name}$//" | sort -n)
92+
93+
# Get the highest number
94+
local max_num=0
95+
for num in $branches; do
96+
if [ "$num" -gt "$max_num" ]; then
97+
max_num=$num
98+
fi
99+
done
100+
101+
# Return next number
102+
echo $((max_num + 1))
103+
}
104+
68105
# Resolve repository root. Prefer git information when available, but fall back
69106
# to searching for repository markers so the workflow still functions in repositories that
70107
# were initialised with --no-git.
@@ -87,20 +124,6 @@ cd "$REPO_ROOT"
87124
SPECS_DIR="$REPO_ROOT/specs"
88125
mkdir -p "$SPECS_DIR"
89126

90-
HIGHEST=0
91-
if [ -d "$SPECS_DIR" ]; then
92-
for dir in "$SPECS_DIR"/*; do
93-
[ -d "$dir" ] || continue
94-
dirname=$(basename "$dir")
95-
number=$(echo "$dirname" | grep -o '^[0-9]\+' || echo "0")
96-
number=$((10#$number))
97-
if [ "$number" -gt "$HIGHEST" ]; then HIGHEST=$number; fi
98-
done
99-
fi
100-
101-
NEXT=$((HIGHEST + 1))
102-
FEATURE_NUM=$(printf "%03d" "$NEXT")
103-
104127
# Function to generate branch name with stop word filtering and length filtering
105128
generate_branch_name() {
106129
local description="$1"
@@ -157,6 +180,28 @@ else
157180
BRANCH_SUFFIX=$(generate_branch_name "$FEATURE_DESCRIPTION")
158181
fi
159182

183+
# Determine branch number
184+
if [ -z "$BRANCH_NUMBER" ]; then
185+
if [ "$HAS_GIT" = true ]; then
186+
# Check existing branches on remotes
187+
BRANCH_NUMBER=$(check_existing_branches "$BRANCH_SUFFIX")
188+
else
189+
# Fall back to local directory check
190+
HIGHEST=0
191+
if [ -d "$SPECS_DIR" ]; then
192+
for dir in "$SPECS_DIR"/*; do
193+
[ -d "$dir" ] || continue
194+
dirname=$(basename "$dir")
195+
number=$(echo "$dirname" | grep -o '^[0-9]\+' || echo "0")
196+
number=$((10#$number))
197+
if [ "$number" -gt "$HIGHEST" ]; then HIGHEST=$number; fi
198+
done
199+
fi
200+
BRANCH_NUMBER=$((HIGHEST + 1))
201+
fi
202+
fi
203+
204+
FEATURE_NUM=$(printf "%03d" "$BRANCH_NUMBER")
160205
BRANCH_NAME="${FEATURE_NUM}-${BRANCH_SUFFIX}"
161206

162207
# GitHub enforces a 244-byte limit on branch names

scripts/powershell/create-new-feature.ps1

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
param(
55
[switch]$Json,
66
[string]$ShortName,
7+
[int]$Number = 0,
78
[switch]$Help,
89
[Parameter(ValueFromRemainingArguments = $true)]
910
[string[]]$FeatureDescription
@@ -12,11 +13,12 @@ $ErrorActionPreference = 'Stop'
1213

1314
# Show help if requested
1415
if ($Help) {
15-
Write-Host "Usage: ./create-new-feature.ps1 [-Json] [-ShortName <name>] <feature description>"
16+
Write-Host "Usage: ./create-new-feature.ps1 [-Json] [-ShortName <name>] [-Number N] <feature description>"
1617
Write-Host ""
1718
Write-Host "Options:"
1819
Write-Host " -Json Output in JSON format"
1920
Write-Host " -ShortName <name> Provide a custom short name (2-4 words) for the branch"
21+
Write-Host " -Number N Specify branch number manually (overrides auto-detection)"
2022
Write-Host " -Help Show this help message"
2123
Write-Host ""
2224
Write-Host "Examples:"
@@ -56,6 +58,45 @@ function Find-RepositoryRoot {
5658
$current = $parent
5759
}
5860
}
61+
62+
function Get-NextBranchNumber {
63+
param(
64+
[string]$ShortName
65+
)
66+
67+
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
68+
try {
69+
git fetch --all --prune 2>$null | Out-Null
70+
} catch {
71+
# Ignore fetch errors
72+
}
73+
74+
# Find all branches matching the pattern (local and remote)
75+
$branches = @()
76+
try {
77+
$allBranches = git branch -a 2>$null
78+
if ($allBranches) {
79+
$branches = $allBranches | Where-Object { $_ -match "feature/(\d+)-$([regex]::Escape($ShortName))$" } | ForEach-Object {
80+
if ($_ -match "feature/(\d+)-") {
81+
[int]$matches[1]
82+
}
83+
}
84+
}
85+
} catch {
86+
# Ignore errors
87+
}
88+
89+
# Get the highest number
90+
$maxNum = 0
91+
foreach ($num in $branches) {
92+
if ($num -gt $maxNum) {
93+
$maxNum = $num
94+
}
95+
}
96+
97+
# Return next number
98+
return $maxNum + 1
99+
}
59100
$fallbackRoot = (Find-RepositoryRoot -StartDir $PSScriptRoot)
60101
if (-not $fallbackRoot) {
61102
Write-Error "Error: Could not determine repository root. Please run this script from within the repository."
@@ -79,18 +120,6 @@ Set-Location $repoRoot
79120
$specsDir = Join-Path $repoRoot 'specs'
80121
New-Item -ItemType Directory -Path $specsDir -Force | Out-Null
81122

82-
$highest = 0
83-
if (Test-Path $specsDir) {
84-
Get-ChildItem -Path $specsDir -Directory | ForEach-Object {
85-
if ($_.Name -match '^(\d{3})') {
86-
$num = [int]$matches[1]
87-
if ($num -gt $highest) { $highest = $num }
88-
}
89-
}
90-
}
91-
$next = $highest + 1
92-
$featureNum = ('{0:000}' -f $next)
93-
94123
# Function to generate branch name with stop word filtering and length filtering
95124
function Get-BranchName {
96125
param([string]$Description)
@@ -145,6 +174,27 @@ if ($ShortName) {
145174
$branchSuffix = Get-BranchName -Description $featureDesc
146175
}
147176

177+
# Determine branch number
178+
if ($Number -eq 0) {
179+
if ($hasGit) {
180+
# Check existing branches on remotes
181+
$Number = Get-NextBranchNumber -ShortName $branchSuffix
182+
} else {
183+
# Fall back to local directory check
184+
$highest = 0
185+
if (Test-Path $specsDir) {
186+
Get-ChildItem -Path $specsDir -Directory | ForEach-Object {
187+
if ($_.Name -match '^(\d{3})') {
188+
$num = [int]$matches[1]
189+
if ($num -gt $highest) { $highest = $num }
190+
}
191+
}
192+
}
193+
$Number = $highest + 1
194+
}
195+
}
196+
197+
$featureNum = ('{0:000}' -f $Number)
148198
$branchName = "$featureNum-$branchSuffix"
149199

150200
# GitHub enforces a 244-byte limit on branch names

templates/commands/specify.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,34 @@ Given that feature description, do this:
3131
- "Create a dashboard for analytics" → "analytics-dashboard"
3232
- "Fix payment processing timeout bug" → "fix-payment-timeout"
3333

34-
2. Run the script `{SCRIPT}` from repo root **with the short-name argument** and parse its JSON output for BRANCH_NAME and SPEC_FILE. All file paths must be absolute.
35-
34+
2. **Check for existing branches before creating new one**:
35+
36+
a. First, fetch all remote branches to ensure we have the latest information:
37+
```bash
38+
git fetch --all --prune
39+
```
40+
41+
b. List all branches (local and remote) that match the short-name pattern:
42+
```bash
43+
git branch -a | grep -E "feature/[0-9]+-<short-name>$"
44+
```
45+
46+
c. Determine the next available number:
47+
- Extract all numbers from existing branches (both local and remote)
48+
- Find the highest number N from branches that exist
49+
- Use N+1 for the new branch number
50+
51+
d. Run the script `{SCRIPT}` with the calculated number and short-name:
52+
- Pass `--number N+1` and `--short-name "your-short-name"` along with the feature description
53+
- Bash example: `{SCRIPT} --json --number 5 --short-name "user-auth" "Add user authentication"`
54+
- PowerShell example: `{SCRIPT} -Json -Number 5 -ShortName "user-auth" "Add user authentication"`
55+
3656
**IMPORTANT**:
37-
38-
- Append the short-name argument to the `{SCRIPT}` command with the 2-4 word short name you created in step 1. Keep the feature description as the final argument.
39-
- Bash example: `--short-name "your-generated-short-name" "Feature description here"`
40-
- PowerShell example: `-ShortName "your-generated-short-name" "Feature description here"`
57+
- Only consider branches that still exist (local or remote)
58+
- If no existing branches found with this short-name, start with number 1
59+
- The JSON output will contain BRANCH_NAME and SPEC_FILE paths
60+
- You must only ever run this script once per feature
4161
- For single quotes in args like "I'm Groot", use escape syntax: e.g 'I'\''m Groot' (or double-quote if possible: "I'm Groot")
42-
- You must only ever run this script once
43-
- The JSON is provided in the terminal as output - always refer to it to get the actual content you're looking for
4462

4563
3. Load `templates/spec-template.md` to understand required sections.
4664

0 commit comments

Comments
 (0)