Skip to content

Commit 4178b61

Browse files
authored
fix(scripts): improve git branch creation error handling (#2089)
* 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. * 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. * fix(scripts): use quiet mode for git checkout -b when capturing errors Ensures branch_create_error is empty on success, matching variable semantics.
1 parent d9e63a5 commit 4178b61

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

scripts/bash/create-new-feature.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,17 @@ SPEC_FILE="$FEATURE_DIR/spec.md"
327327

328328
if [ "$DRY_RUN" != true ]; then
329329
if [ "$HAS_GIT" = true ]; then
330-
if ! git checkout -b "$BRANCH_NAME" 2>/dev/null; then
330+
branch_create_error=""
331+
if ! branch_create_error=$(git checkout -q -b "$BRANCH_NAME" 2>&1); then
332+
current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
331333
# Check if branch already exists
332334
if git branch --list "$BRANCH_NAME" | grep -q .; then
333335
if [ "$ALLOW_EXISTING" = true ]; then
334-
# Switch to the existing branch instead of failing
335-
if ! git checkout "$BRANCH_NAME" 2>/dev/null; then
336+
# If we're already on the branch, continue without another checkout.
337+
if [ "$current_branch" = "$BRANCH_NAME" ]; then
338+
:
339+
# Otherwise switch to the existing branch instead of failing.
340+
elif ! git checkout "$BRANCH_NAME" 2>/dev/null; then
336341
>&2 echo "Error: Failed to switch to existing branch '$BRANCH_NAME'. Please resolve any local changes or conflicts and try again."
337342
exit 1
338343
fi
@@ -344,7 +349,12 @@ if [ "$DRY_RUN" != true ]; then
344349
exit 1
345350
fi
346351
else
347-
>&2 echo "Error: Failed to create git branch '$BRANCH_NAME'. Please check your git configuration and try again."
352+
>&2 echo "Error: Failed to create git branch '$BRANCH_NAME'."
353+
if [ -n "$branch_create_error" ]; then
354+
>&2 printf '%s\n' "$branch_create_error"
355+
else
356+
>&2 echo "Please check your git configuration and try again."
357+
fi
348358
exit 1
349359
fi
350360
fi

scripts/powershell/create-new-feature.ps1

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,33 @@ $specFile = Join-Path $featureDir 'spec.md'
293293
if (-not $DryRun) {
294294
if ($hasGit) {
295295
$branchCreated = $false
296+
$branchCreateError = ''
296297
try {
297-
git checkout -q -b $branchName 2>$null | Out-Null
298+
$branchCreateError = git checkout -q -b $branchName 2>&1 | Out-String
298299
if ($LASTEXITCODE -eq 0) {
299300
$branchCreated = $true
300301
}
301302
} catch {
302-
# Exception during git command
303+
$branchCreateError = $_.Exception.Message
303304
}
304305

305306
if (-not $branchCreated) {
307+
$currentBranch = ''
308+
try { $currentBranch = (git rev-parse --abbrev-ref HEAD 2>$null).Trim() } catch {}
306309
# Check if branch already exists
307310
$existingBranch = git branch --list $branchName 2>$null
308311
if ($existingBranch) {
309312
if ($AllowExistingBranch) {
310-
# Switch to the existing branch instead of failing
311-
git checkout -q $branchName 2>$null | Out-Null
312-
if ($LASTEXITCODE -ne 0) {
313-
Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again."
314-
exit 1
313+
# If we're already on the branch, continue without another checkout.
314+
if ($currentBranch -eq $branchName) {
315+
# Already on the target branch — nothing to do
316+
} else {
317+
# Otherwise switch to the existing branch instead of failing.
318+
git checkout -q $branchName 2>$null | Out-Null
319+
if ($LASTEXITCODE -ne 0) {
320+
Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again."
321+
exit 1
322+
}
315323
}
316324
} elseif ($Timestamp) {
317325
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) {
321329
exit 1
322330
}
323331
} else {
324-
Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again."
332+
if ($branchCreateError) {
333+
Write-Error "Error: Failed to create git branch '$branchName'.`n$($branchCreateError.Trim())"
334+
} else {
335+
Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again."
336+
}
325337
exit 1
326338
}
327339
}

0 commit comments

Comments
 (0)