feat: check for mainbranch

This commit is contained in:
Bjarke Sporring
2026-01-15 14:31:00 +01:00
parent 40341d21a7
commit 9fbdd941fa
8 changed files with 152 additions and 41 deletions

View File

@@ -30,6 +30,19 @@ git init | Out-Null
git config user.name "Workshop Student"
git config user.email "student@example.com"
# Detect the default branch name (could be main, master, etc.)
# First commit creates the branch, so we detect it after that
$mainBranch = git branch --show-current
if (-not $mainBranch) {
# Fallback: Get default branch name from git config
$mainBranch = git config --get init.defaultBranch
if (-not $mainBranch) {
# Ultimate fallback: use "main"
$mainBranch = "main"
}
}
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
# ============================================================================
# Create base project
# ============================================================================
@@ -87,7 +100,7 @@ git commit -m "Add timeout configuration" | Out-Null
# Branch 2: add-debug (adds debug setting - CONFLICTS with timeout!)
# ============================================================================
Write-Host "Creating add-debug branch..." -ForegroundColor Cyan
git switch main | Out-Null
git switch $mainBranch | Out-Null
git switch -c add-debug | Out-Null
$debugConfig = @"
@@ -104,8 +117,8 @@ Set-Content -Path "config.json" -Value $debugConfig
git add .
git commit -m "Add debug mode configuration" | Out-Null
# Switch back to main
git switch main | Out-Null
# Switch back to main branch
git switch $mainBranch | Out-Null
# Return to module directory
Set-Location ..
@@ -113,7 +126,7 @@ Set-Location ..
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
Write-Host "`nThe repository contains:" -ForegroundColor Yellow
Write-Host " - main branch: base configuration" -ForegroundColor White
Write-Host " - $mainBranch branch: base configuration" -ForegroundColor White
Write-Host " - add-timeout branch: adds timeout setting" -ForegroundColor White
Write-Host " - add-debug branch: adds debug setting" -ForegroundColor White
Write-Host "`nBoth branches modify the same part of config.json!" -ForegroundColor Red

View File

@@ -59,15 +59,39 @@ if (-not (Test-Path ".git")) {
Write-Host "`n=== Verifying Module 04: Merge Conflicts ===" -ForegroundColor Cyan
# ============================================================================
# Detect the main branch name (could be main, master, etc.)
# ============================================================================
# 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
# ============================================================================
# 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-Fail "Should be on main branch (currently on: $currentBranch)"
Write-Hint "Switch to main with: git switch main"
Write-Fail "Should be on $mainBranch branch (currently on: $currentBranch)"
Write-Hint "Switch to $mainBranch with: git switch $mainBranch"
}
# ============================================================================
@@ -196,7 +220,7 @@ if ($script:allChecksPassed) {
Write-Host "[SUMMARY] Some checks failed. Review the hints above." -ForegroundColor Red
Write-Host ""
Write-Host "Quick guide:" -ForegroundColor Cyan
Write-Host " 1. Make sure you're on main: git switch main" -ForegroundColor White
Write-Host " 1. Make sure you're on $mainBranch : git switch $mainBranch" -ForegroundColor White
Write-Host " 2. Merge first branch: git merge add-timeout" -ForegroundColor White
Write-Host " 3. Merge second branch: git merge add-debug" -ForegroundColor White
Write-Host " 4. Resolve conflict: edit config.json, remove markers, keep both settings" -ForegroundColor White