here is my ps: List all local branches Write-Host "Local branches:" -ForegroundColor…
- User
here is my ps:
List all local branches
Write-Host "Local branches:" -ForegroundColor Green localBranches = git branch | ForEach-Object { _ -replace '^\s**?\s*', '' } $localBranches
List all remote branches
Write-Host "`nRemote branches:" -ForegroundColor Green remoteBranches
Ask the user for a prefix
$prefix = Read-Host -Prompt 'Enter the branch prefix to filter'
Ensure the prefix starts and ends with '/'
if (-not prefix.StartsWith('/')) { prefix = '/' + prefix } if (-not prefix.EndsWith('/')) { prefix + '/' }
Filter local branches based on the prefix
localBranches | Where-Object { _ -imatch "^prefix" }
Filter remote branches based on the prefix
remoteBranches | Where-Object { prefix" }
Display branches that match the prefix
Write-Host "`nBranches that match the prefix 'matchingBranches = matchingRemoteBranches $matchingBranches
Ask for confirmation to delete
$confirmation = Read-Host -Prompt "Do you want to delete these branches? (yes/no)"
if (confirmation -ieq 'yes') { foreach (branch in matchingBranches) { # Remove leading spaces, "*" from the branch name, and any trailing whitespace branchName = $branch.Trim()
# For remote branches if ($branchName -imatch "^origin/") { $cleanBranchName = $branchName -replace '^origin/', '' try { git push origin --delete "$cleanBranchName" } catch { Write-Host "Error deleting remote branch ${cleanBranchName}: $_" -ForegroundColor Red } } else { try { git branch -D "$branchName" # Force delete the local branch } catch { Write-Host "Error deleting local branch ${branchName}: $_" -ForegroundColor Red } } }} else { Write-Host "No branches were deleted." -ForegroundColor Yellow }
it doesn't correctly list branches based on prefix. it should list both local and remote branches
Assistant
You're right, the code is not correctly filtering the branches based on the prefix. Here is one way to fix it:
# List all local and remote branches Write-Host "All branches:" -ForegroundColor Green $allBranches = git branch -a # Ask for prefix $prefix = Read-Host -Prompt 'Enter the branch prefix to filter' # Filter branches based on prefix $matchingBranches = $allBranches | Where-Object { $_ -imatch "^$prefix" } # Display matching branches Write-Host "`nBranches matching '$prefix':" -ForegroundColor Green $matchingBranches # Rest of deletion logic...The key changes:
- Use
git branch -ato get all local and remote branches in one list - Filter that full list based on the prefix regex match
- No need to separate into local/remote filtering
This will correctly show any branches (local or remote) that match the given prefix in one list to the user for deletion.
- Use
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.