|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Standardize this repository to a single public main branch. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + This helper prepares a clean public GitHub repository layout: |
| 7 | + - ensures a local main branch exists, |
| 8 | + - pushes main to origin, |
| 9 | + - sets GitHub default branch to main, |
| 10 | + - optionally deletes old remote branches after you confirm they are no longer needed. |
| 11 | +
|
| 12 | + The script is intentionally conservative. By default it runs in preview mode. |
| 13 | +
|
| 14 | +.EXAMPLE |
| 15 | + .\scripts\standardize-main-branch.ps1 -Owner masarray -Repo DigSubAnalyzer -WhatIf |
| 16 | +
|
| 17 | +.EXAMPLE |
| 18 | + .\scripts\standardize-main-branch.ps1 -Owner masarray -Repo DigSubAnalyzer -Apply |
| 19 | +
|
| 20 | +.EXAMPLE |
| 21 | + .\scripts\standardize-main-branch.ps1 -Owner masarray -Repo DigSubAnalyzer -Apply -DeleteRemoteBranches master,public-repo-hardening |
| 22 | +#> |
| 23 | + |
| 24 | +[CmdletBinding()] |
| 25 | +param( |
| 26 | + [Parameter(Mandatory = $true)] |
| 27 | + [string]$Owner, |
| 28 | + |
| 29 | + [Parameter(Mandatory = $true)] |
| 30 | + [string]$Repo, |
| 31 | + |
| 32 | + [string]$TargetBranch = "main", |
| 33 | + |
| 34 | + [string[]]$DeleteRemoteBranches = @(), |
| 35 | + |
| 36 | + [switch]$Apply, |
| 37 | + |
| 38 | + [switch]$WhatIf |
| 39 | +) |
| 40 | + |
| 41 | +$ErrorActionPreference = "Stop" |
| 42 | + |
| 43 | +function Invoke-Step { |
| 44 | + param( |
| 45 | + [Parameter(Mandatory = $true)] [string]$Description, |
| 46 | + [Parameter(Mandatory = $true)] [scriptblock]$Command |
| 47 | + ) |
| 48 | + |
| 49 | + Write-Host "`n==> $Description" -ForegroundColor Cyan |
| 50 | + if (-not $Apply -or $WhatIf) { |
| 51 | + Write-Host "Preview only. Use -Apply to execute." -ForegroundColor Yellow |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + & $Command |
| 56 | +} |
| 57 | + |
| 58 | +if (-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 59 | + throw "Git CLI was not found in PATH. Install Git for Windows first." |
| 60 | +} |
| 61 | + |
| 62 | +if (-not (Get-Command gh -ErrorAction SilentlyContinue)) { |
| 63 | + throw "GitHub CLI was not found in PATH. Install GitHub CLI first." |
| 64 | +} |
| 65 | + |
| 66 | +$authStatus = gh auth status 2>&1 |
| 67 | +if ($LASTEXITCODE -ne 0) { |
| 68 | + throw "GitHub CLI is not authenticated. Run: gh auth login" |
| 69 | +} |
| 70 | + |
| 71 | +$repoFullName = "$Owner/$Repo" |
| 72 | +Write-Host "Repository : $repoFullName" |
| 73 | +Write-Host "Target : $TargetBranch" |
| 74 | +Write-Host "Apply : $Apply" |
| 75 | +Write-Host "Delete : $($DeleteRemoteBranches -join ', ')" |
| 76 | + |
| 77 | +$currentBranch = (git branch --show-current).Trim() |
| 78 | +if ([string]::IsNullOrWhiteSpace($currentBranch)) { |
| 79 | + throw "No current Git branch detected. Run this script from a normal local checkout." |
| 80 | +} |
| 81 | + |
| 82 | +Invoke-Step "Fetch origin" { |
| 83 | + git fetch origin --prune |
| 84 | +} |
| 85 | + |
| 86 | +Invoke-Step "Create or update local $TargetBranch from current branch '$currentBranch'" { |
| 87 | + $existing = git branch --list $TargetBranch |
| 88 | + if ($existing) { |
| 89 | + git checkout $TargetBranch |
| 90 | + git merge $currentBranch --ff-only |
| 91 | + } else { |
| 92 | + git branch -m $TargetBranch |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +Invoke-Step "Push $TargetBranch to origin" { |
| 97 | + git push -u origin $TargetBranch |
| 98 | +} |
| 99 | + |
| 100 | +Invoke-Step "Set GitHub default branch to $TargetBranch" { |
| 101 | + gh repo edit $repoFullName --default-branch $TargetBranch --delete-branch-on-merge=true |
| 102 | +} |
| 103 | + |
| 104 | +foreach ($branch in $DeleteRemoteBranches) { |
| 105 | + if ($branch -eq $TargetBranch) { |
| 106 | + Write-Host "Skipping delete for target branch '$TargetBranch'." -ForegroundColor Yellow |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + Invoke-Step "Delete remote branch origin/$branch" { |
| 111 | + git push origin --delete $branch |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +Write-Host "`nDone. Expected public branch layout: only '$TargetBranch' remains as the public default branch." -ForegroundColor Green |
0 commit comments