Skip to content

Commit 6c3d698

Browse files
authored
Merge pull request #1237 from Mearman/fix/branch-number-collision-bug
fix: use global maximum for branch numbering to prevent collisions
2 parents 4d80667 + dadda12 commit 6c3d698

File tree

2 files changed

+29
-81
lines changed

2 files changed

+29
-81
lines changed

scripts/bash/create-new-feature.sh

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,23 @@ get_highest_from_branches() {
128128

129129
# Function to check existing branches (local and remote) and return next available number
130130
check_existing_branches() {
131-
local short_name="$1"
132-
local specs_dir="$2"
133-
131+
local specs_dir="$1"
132+
134133
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
135134
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)
135+
136+
# Get highest number from ALL branches (not just matching short name)
137+
local highest_branch=$(get_highest_from_branches)
138+
139+
# Get highest number from ALL specs (not just matching short name)
140+
local highest_spec=$(get_highest_from_specs "$specs_dir")
141+
142+
# Take the maximum of both
143+
local max_num=$highest_branch
144+
if [ "$highest_spec" -gt "$max_num" ]; then
145+
max_num=$highest_spec
147146
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-
147+
157148
# Return next number
158149
echo $((max_num + 1))
159150
}
@@ -247,15 +238,16 @@ fi
247238
if [ -z "$BRANCH_NUMBER" ]; then
248239
if [ "$HAS_GIT" = true ]; then
249240
# Check existing branches on remotes
250-
BRANCH_NUMBER=$(check_existing_branches "$BRANCH_SUFFIX" "$SPECS_DIR")
241+
BRANCH_NUMBER=$(check_existing_branches "$SPECS_DIR")
251242
else
252243
# Fall back to local directory check
253244
HIGHEST=$(get_highest_from_specs "$SPECS_DIR")
254245
BRANCH_NUMBER=$((HIGHEST + 1))
255246
fi
256247
fi
257248

258-
FEATURE_NUM=$(printf "%03d" "$BRANCH_NUMBER")
249+
# Force base-10 interpretation to prevent octal conversion (e.g., 010 → 8 in octal, but should be 10 in decimal)
250+
FEATURE_NUM=$(printf "%03d" "$((10#$BRANCH_NUMBER))")
259251
BRANCH_NAME="${FEATURE_NUM}-${BRANCH_SUFFIX}"
260252

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

scripts/powershell/create-new-feature.ps1

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -101,69 +101,25 @@ function Get-HighestNumberFromBranches {
101101

102102
function Get-NextBranchNumber {
103103
param(
104-
[string]$ShortName,
105104
[string]$SpecsDir
106105
)
107-
106+
108107
# Fetch all remotes to get latest branch info (suppress errors if no remotes)
109108
try {
110109
git fetch --all --prune 2>$null | Out-Null
111110
} catch {
112111
# Ignore fetch errors
113112
}
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-
113+
114+
# Get highest number from ALL branches (not just matching short name)
115+
$highestBranch = Get-HighestNumberFromBranches
116+
117+
# Get highest number from ALL specs (not just matching short name)
118+
$highestSpec = Get-HighestNumberFromSpecs -SpecsDir $SpecsDir
119+
120+
# Take the maximum of both
121+
$maxNum = [Math]::Max($highestBranch, $highestSpec)
122+
167123
# Return next number
168124
return $maxNum + 1
169125
}
@@ -254,7 +210,7 @@ if ($ShortName) {
254210
if ($Number -eq 0) {
255211
if ($hasGit) {
256212
# Check existing branches on remotes
257-
$Number = Get-NextBranchNumber -ShortName $branchSuffix -SpecsDir $specsDir
213+
$Number = Get-NextBranchNumber -SpecsDir $specsDir
258214
} else {
259215
# Fall back to local directory check
260216
$Number = (Get-HighestNumberFromSpecs -SpecsDir $specsDir) + 1

0 commit comments

Comments
 (0)