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

@@ -31,6 +31,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 a realistic project history with multiple merged branches
# ============================================================================
@@ -96,8 +109,8 @@ Set-Content -Path "login.py" -Value $loginContent
git add .
git commit -m "Add password validation" | Out-Null
# Switch back to main and make more commits
git switch main | Out-Null
# Switch back to main branch
git switch $mainBranch | Out-Null
$appContent = @"
# app.py - Application entry point
@@ -163,8 +176,8 @@ Set-Content -Path "api.py" -Value $apiContent
git add .
git commit -m "Add delete endpoint to API" | Out-Null
# Switch back to main and add documentation
git switch main | Out-Null
# Switch back to main branch and add documentation
git switch $mainBranch | Out-Null
$readmeContent = @"
# My Application
@@ -241,8 +254,22 @@ Set-Content -Path "database.py" -Value $dbContent
git add .
git commit -m "Add disconnect method" | Out-Null
# Switch to main and merge
git switch main | Out-Null
# Switch to main branch and add another commit (to create divergent history)
git switch $mainBranch | Out-Null
$configContent = @"
{
"app": {
"port": 3000,
"debug": false
}
}
"@
Set-Content -Path "config.json" -Value $configContent
git add .
git commit -m "Add configuration file" | Out-Null
# Merge feature-database (will be three-way merge since main diverged)
Write-Host "Merging feature-database into main..." -ForegroundColor Green
git merge feature-database --no-edit | Out-Null
@@ -278,7 +305,7 @@ 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 a realistic project history:" -ForegroundColor Yellow
Write-Host " - Multiple feature branches (login, api, database)" -ForegroundColor White
Write-Host " - All branches have been merged into main" -ForegroundColor White
Write-Host " - All branches have been merged into $mainBranch" -ForegroundColor White
Write-Host " - View the history: git log --oneline --graph --all" -ForegroundColor White
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. Read the README.md for detailed instructions" -ForegroundColor White

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 ""