refactor: check for main branch

This commit is contained in:
Bjarke Sporring
2026-01-15 13:14:38 +01:00
parent 7b638d27de
commit bf07cb1868
2 changed files with 65 additions and 14 deletions

View File

@@ -59,9 +59,33 @@ if (-not (Test-Path ".git")) {
Write-Host "`n=== Verifying Module 03: Branching and Merging ===" -ForegroundColor Cyan
# ============================================================================
# Count initial setup commits (should be 13 commits from setup)
# Detect the main branch name (could be main, master, etc.)
# ============================================================================
$initialCommitCount = 13
# Try to get the default branch from remote origin first
$mainBranch = git symbolic-ref refs/remotes/origin/HEAD 2>$null | Split-Path -Leaf
if (-not $mainBranch) {
# Fallback: try to detect from local branches
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
if ($allBranches -contains "main") {
$mainBranch = "main"
} elseif ($allBranches -contains "master") {
$mainBranch = "master"
} else {
# Get the default branch from git config
$mainBranch = git config --get init.defaultBranch
if (-not $mainBranch) {
# Ultimate fallback: use the first branch
$mainBranch = $allBranches | Select-Object -First 1
if (-not $mainBranch) { $mainBranch = "main" }
}
}
}
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
# ============================================================================
# Count initial setup commits (should be 15 commits from setup)
# ============================================================================
$initialCommitCount = 15
# ============================================================================
# Check for new commits beyond setup
@@ -83,7 +107,7 @@ if ($totalCommits -gt $initialCommitCount) {
# Check for branches (excluding the example branches)
# ============================================================================
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
$exampleBranches = @('main', 'feature-login', 'feature-api', 'feature-database')
$exampleBranches = @($mainBranch, 'feature-login', 'feature-api', 'feature-database')
$studentBranches = $allBranches | Where-Object { $_ -notin $exampleBranches }
if ($studentBranches.Count -gt 0) {
@@ -115,11 +139,11 @@ if ($totalMerges -gt $setupMerges) {
# Check current branch
# ============================================================================
$currentBranch = git branch --show-current 2>$null
if ($currentBranch -eq "main") {
Write-Pass "Currently on main branch"
if ($currentBranch -eq $mainBranch) {
Write-Pass "Currently on $mainBranch branch"
} else {
Write-Info "Currently on '$currentBranch' branch"
Write-Hint "Typically you merge feature branches INTO main"
Write-Hint "Typically you merge feature branches INTO $mainBranch"
}
Pop-Location
@@ -145,7 +169,7 @@ if ($script:allChecksPassed) {
Write-Host "Quick guide:" -ForegroundColor Cyan
Write-Host " 1. Create a branch: git switch -c my-feature" -ForegroundColor White
Write-Host " 2. Make changes and commit them" -ForegroundColor White
Write-Host " 3. Switch to main: git switch main" -ForegroundColor White
Write-Host " 3. Switch to $mainBranch : git switch $mainBranch" -ForegroundColor White
Write-Host " 4. Merge your branch: git merge my-feature" -ForegroundColor White
Write-Host " 5. Run this verify script again" -ForegroundColor White
Write-Host ""