Private/Get-DefaultBranch.ps1
|
<#
.SYNOPSIS Returns the preferred default branch name for a repository. .DESCRIPTION Resolution order: 1. git config init.defaultBranch (repo-local or global) 2. 'master' if that branch exists 3. 'main' if that branch exists 4. 'master' (hard fallback) #> function Get-DefaultBranch { [CmdletBinding()] [OutputType([string])] param( [string] $RepoPath = (Get-Location).Path ) $configured = git -C $RepoPath config init.defaultBranch 2>$null if ($LASTEXITCODE -eq 0 -and $configured) { return $configured } foreach ($candidate in @('master', 'main')) { $exists = git -C $RepoPath branch --list $candidate 2>$null if ($exists) { return $candidate } } return 'master' } |