Skip to content

Commit 33df897

Browse files
committed
fix: use global maximum for branch numbering to prevent collisions
The check_existing_branches (bash) and Get-NextBranchNumber (PowerShell) functions were only looking for branches/specs matching the SAME short name when determining the next feature number. This caused collisions where multiple features could be assigned the same number if they had different short names. For example, if feature 023-ci-optimization existed, creating a new feature with a different short name would incorrectly use 001 instead of 024. This fix changes both functions to: 1. Use get_highest_from_branches() / Get-HighestNumberFromBranches to find the highest number across ALL branches globally 2. Use get_highest_from_specs() / Get-HighestNumberFromSpecs to find the highest number across ALL spec directories globally 3. Return the maximum of both + 1 The helper functions already existed but were not being used. This fix properly utilizes them to ensure features are numbered sequentially regardless of their short names. Issue: Branch number collisions when creating features with different names Impact: Prevents multiple features from sharing the same number prefix
1 parent f205fa3 commit 33df897

File tree

2 files changed

+24
-75
lines changed

2 files changed

+24
-75
lines changed

scripts/bash/create-new-feature.sh

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,22 @@ get_highest_from_branches() {
130130
check_existing_branches() {
131131
local short_name="$1"
132132
local specs_dir="$2"
133-
133+
134134
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
135135
git fetch --all --prune 2>/dev/null || true
136-
137-
# Find all branches matching the pattern using git ls-remote (more reliable)
138-
local remote_branches=$(git ls-remote --heads origin 2>/dev/null | grep -E "refs/heads/[0-9]+-${short_name}$" | sed 's/.*\/\([0-9]*\)-.*/\1/' | sort -n)
139-
140-
# Also check local branches
141-
local local_branches=$(git branch 2>/dev/null | grep -E "^[* ]*[0-9]+-${short_name}$" | sed 's/^[* ]*//' | sed 's/-.*//' | sort -n)
142-
143-
# Check specs directory as well
144-
local spec_dirs=""
145-
if [ -d "$specs_dir" ]; then
146-
spec_dirs=$(find "$specs_dir" -maxdepth 1 -type d -name "[0-9]*-${short_name}" 2>/dev/null | xargs -n1 basename 2>/dev/null | sed 's/-.*//' | sort -n)
136+
137+
# Get highest number from ALL branches (not just matching short name)
138+
local highest_branch=$(get_highest_from_branches)
139+
140+
# Get highest number from ALL specs (not just matching short name)
141+
local highest_spec=$(get_highest_from_specs "$specs_dir")
142+
143+
# Take the maximum of both
144+
local max_num=$highest_branch
145+
if [ "$highest_spec" -gt "$max_num" ]; then
146+
max_num=$highest_spec
147147
fi
148-
149-
# Combine all sources and get the highest number
150-
local max_num=0
151-
for num in $remote_branches $local_branches $spec_dirs; do
152-
if [ "$num" -gt "$max_num" ]; then
153-
max_num=$num
154-
fi
155-
done
156-
148+
157149
# Return next number
158150
echo $((max_num + 1))
159151
}

scripts/powershell/create-new-feature.ps1

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -104,66 +104,23 @@ function Get-NextBranchNumber {
104104
[string]$ShortName,
105105
[string]$SpecsDir
106106
)
107-
107+
108108
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
109109
try {
110110
git fetch --all --prune 2>$null | Out-Null
111111
} catch {
112112
# Ignore fetch errors
113113
}
114-
115-
# Find remote branches matching the pattern using git ls-remote
116-
$remoteBranches = @()
117-
try {
118-
$remoteRefs = git ls-remote --heads origin 2>$null
119-
if ($remoteRefs) {
120-
$remoteBranches = $remoteRefs | Where-Object { $_ -match "refs/heads/(\d+)-$([regex]::Escape($ShortName))$" } | ForEach-Object {
121-
if ($_ -match "refs/heads/(\d+)-") {
122-
[int]$matches[1]
123-
}
124-
}
125-
}
126-
} catch {
127-
# Ignore errors
128-
}
129-
130-
# Check local branches
131-
$localBranches = @()
132-
try {
133-
$allBranches = git branch 2>$null
134-
if ($allBranches) {
135-
$localBranches = $allBranches | Where-Object { $_ -match "^\*?\s*(\d+)-$([regex]::Escape($ShortName))$" } | ForEach-Object {
136-
if ($_ -match "(\d+)-") {
137-
[int]$matches[1]
138-
}
139-
}
140-
}
141-
} catch {
142-
# Ignore errors
143-
}
144-
145-
# Check specs directory
146-
$specDirs = @()
147-
if (Test-Path $SpecsDir) {
148-
try {
149-
$specDirs = Get-ChildItem -Path $SpecsDir -Directory | Where-Object { $_.Name -match "^(\d+)-$([regex]::Escape($ShortName))$" } | ForEach-Object {
150-
if ($_.Name -match "^(\d+)-") {
151-
[int]$matches[1]
152-
}
153-
}
154-
} catch {
155-
# Ignore errors
156-
}
157-
}
158-
159-
# Combine all sources and get the highest number
160-
$maxNum = 0
161-
foreach ($num in ($remoteBranches + $localBranches + $specDirs)) {
162-
if ($num -gt $maxNum) {
163-
$maxNum = $num
164-
}
165-
}
166-
114+
115+
# Get highest number from ALL branches (not just matching short name)
116+
$highestBranch = Get-HighestNumberFromBranches
117+
118+
# Get highest number from ALL specs (not just matching short name)
119+
$highestSpec = Get-HighestNumberFromSpecs -SpecsDir $SpecsDir
120+
121+
# Take the maximum of both
122+
$maxNum = [Math]::Max($highestBranch, $highestSpec)
123+
167124
# Return next number
168125
return $maxNum + 1
169126
}

0 commit comments

Comments
 (0)