From b064ce2204650ff1f3ea3fca327c630b659af17f Mon Sep 17 00:00:00 2001 From: Adam Boczek Date: Sat, 4 Apr 2026 08:59:13 +0200 Subject: [PATCH 1/3] fix(scripts): improve git branch creation error handling - Capture git checkout -b stderr for meaningful error reporting - Skip redundant checkout when already on target branch - Surface actual git error messages instead of generic fallback Applies to both bash and PowerShell create-new-feature scripts. --- scripts/bash/create-new-feature.sh | 18 +++++++++++---- scripts/powershell/create-new-feature.ps1 | 28 ++++++++++++++++------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index 36ea537991..f4366a17aa 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -327,12 +327,17 @@ SPEC_FILE="$FEATURE_DIR/spec.md" if [ "$DRY_RUN" != true ]; then if [ "$HAS_GIT" = true ]; then - if ! git checkout -b "$BRANCH_NAME" 2>/dev/null; then + branch_create_error="" + if ! branch_create_error=$(git checkout -b "$BRANCH_NAME" 2>&1); then + current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" # Check if branch already exists if git branch --list "$BRANCH_NAME" | grep -q .; then if [ "$ALLOW_EXISTING" = true ]; then - # Switch to the existing branch instead of failing - if ! git checkout "$BRANCH_NAME" 2>/dev/null; then + # If we're already on the branch, continue without another checkout. + if [ "$current_branch" = "$BRANCH_NAME" ]; then + : + # Otherwise switch to the existing branch instead of failing. + elif ! git checkout "$BRANCH_NAME" 2>/dev/null; then >&2 echo "Error: Failed to switch to existing branch '$BRANCH_NAME'. Please resolve any local changes or conflicts and try again." exit 1 fi @@ -344,7 +349,12 @@ if [ "$DRY_RUN" != true ]; then exit 1 fi else - >&2 echo "Error: Failed to create git branch '$BRANCH_NAME'. Please check your git configuration and try again." + >&2 echo "Error: Failed to create git branch '$BRANCH_NAME'." + if [ -n "$branch_create_error" ]; then + >&2 printf '%s\n' "$branch_create_error" + else + >&2 echo "Please check your git configuration and try again." + fi exit 1 fi fi diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1 index 2cfa351399..a4282a6063 100644 --- a/scripts/powershell/create-new-feature.ps1 +++ b/scripts/powershell/create-new-feature.ps1 @@ -293,25 +293,33 @@ $specFile = Join-Path $featureDir 'spec.md' if (-not $DryRun) { if ($hasGit) { $branchCreated = $false + $branchCreateError = '' try { - git checkout -q -b $branchName 2>$null | Out-Null + $branchCreateError = git checkout -b $branchName 2>&1 | Out-String if ($LASTEXITCODE -eq 0) { $branchCreated = $true } } catch { - # Exception during git command + $branchCreateError = $_.Exception.Message } if (-not $branchCreated) { + $currentBranch = '' + try { $currentBranch = (git rev-parse --abbrev-ref HEAD 2>$null).Trim() } catch {} # Check if branch already exists $existingBranch = git branch --list $branchName 2>$null if ($existingBranch) { if ($AllowExistingBranch) { - # Switch to the existing branch instead of failing - git checkout -q $branchName 2>$null | Out-Null - if ($LASTEXITCODE -ne 0) { - Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again." - exit 1 + # If we're already on the branch, continue without another checkout. + if ($currentBranch -eq $branchName) { + # Already on the target branch — nothing to do + } else { + # Otherwise switch to the existing branch instead of failing. + git checkout -q $branchName 2>$null | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again." + exit 1 + } } } elseif ($Timestamp) { Write-Error "Error: Branch '$branchName' already exists. Rerun to get a new timestamp or use a different -ShortName." @@ -321,7 +329,11 @@ if (-not $DryRun) { exit 1 } } else { - Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again." + if ($branchCreateError) { + Write-Error "Error: Failed to create git branch '$branchName'.`n$($branchCreateError.Trim())" + } else { + Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again." + } exit 1 } } From d30f54b483a02d308764fc63f40cb2264586a54e Mon Sep 17 00:00:00 2001 From: Adam Boczek Date: Sat, 4 Apr 2026 08:59:13 +0200 Subject: [PATCH 2/3] fix(scripts): improve git branch creation error handling - Capture git checkout -b stderr for meaningful error reporting - Skip redundant checkout when already on target branch - Surface actual git error messages instead of generic fallback Applies to both bash and PowerShell create-new-feature scripts. --- scripts/bash/create-new-feature.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index f4366a17aa..de9d47284d 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -357,6 +357,9 @@ if [ "$DRY_RUN" != true ]; then fi exit 1 fi + else + # Show normal git output (e.g., "Switched to a new branch '...'") + [ -n "$branch_create_error" ] && >&2 printf '%s\n' "$branch_create_error" fi else >&2 echo "[specify] Warning: Git repository not detected; skipped branch creation for $BRANCH_NAME" From 127a3e0e193cacd92095883f2d713bffd82a120e Mon Sep 17 00:00:00 2001 From: Adam Boczek Date: Sat, 4 Apr 2026 09:27:07 +0200 Subject: [PATCH 3/3] fix(scripts): use quiet mode for git checkout -b when capturing errors Ensures branch_create_error is empty on success, matching variable semantics. --- scripts/bash/create-new-feature.sh | 5 +---- scripts/powershell/create-new-feature.ps1 | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index de9d47284d..f9ba9545df 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -328,7 +328,7 @@ SPEC_FILE="$FEATURE_DIR/spec.md" if [ "$DRY_RUN" != true ]; then if [ "$HAS_GIT" = true ]; then branch_create_error="" - if ! branch_create_error=$(git checkout -b "$BRANCH_NAME" 2>&1); then + if ! branch_create_error=$(git checkout -q -b "$BRANCH_NAME" 2>&1); then current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" # Check if branch already exists if git branch --list "$BRANCH_NAME" | grep -q .; then @@ -357,9 +357,6 @@ if [ "$DRY_RUN" != true ]; then fi exit 1 fi - else - # Show normal git output (e.g., "Switched to a new branch '...'") - [ -n "$branch_create_error" ] && >&2 printf '%s\n' "$branch_create_error" fi else >&2 echo "[specify] Warning: Git repository not detected; skipped branch creation for $BRANCH_NAME" diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1 index a4282a6063..3e7e525b86 100644 --- a/scripts/powershell/create-new-feature.ps1 +++ b/scripts/powershell/create-new-feature.ps1 @@ -295,7 +295,7 @@ if (-not $DryRun) { $branchCreated = $false $branchCreateError = '' try { - $branchCreateError = git checkout -b $branchName 2>&1 | Out-String + $branchCreateError = git checkout -q -b $branchName 2>&1 | Out-String if ($LASTEXITCODE -eq 0) { $branchCreated = $true }