#!/usr/bin/env pwsh <# .SYNOPSIS Verifies the Module 03 challenge solution. .DESCRIPTION This script checks that you've practiced branching and merging by: - Creating at least one new branch (beyond the example branches) - Making commits on your branch - Merging your branch into main #> $script:allChecksPassed = $true # ============================================================================ # Helper Functions # ============================================================================ function Write-Pass { param([string]$Message) Write-Host "[PASS] $Message" -ForegroundColor Green } function Write-Fail { param([string]$Message) Write-Host "[FAIL] $Message" -ForegroundColor Red $script:allChecksPassed = $false } function Write-Hint { param([string]$Message) Write-Host "[HINT] $Message" -ForegroundColor Yellow } function Write-Info { param([string]$Message) Write-Host "[INFO] $Message" -ForegroundColor Cyan } # ============================================================================ # Check challenge directory exists # ============================================================================ if (-not (Test-Path "challenge")) { Write-Host "[ERROR] Challenge directory not found." -ForegroundColor Red Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow exit 1 } Push-Location "challenge" if (-not (Test-Path ".git")) { Write-Host "[ERROR] Not a git repository." -ForegroundColor Red Write-Host "Run ..\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow Pop-Location exit 1 } Write-Host "`n=== Verifying Module 03: Branching and Merging ===" -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 # ============================================================================ # Count initial setup commits (should be 15 commits from setup) # ============================================================================ $initialCommitCount = 15 # ============================================================================ # Check for new commits beyond setup # ============================================================================ Write-Host "`nChecking your work..." -ForegroundColor Cyan $totalCommits = [int](git rev-list --count HEAD 2>$null) if ($totalCommits -gt $initialCommitCount) { $newCommits = $totalCommits - $initialCommitCount Write-Pass "Found $newCommits new commit(s) beyond the initial setup" } else { Write-Fail "No new commits found" Write-Hint "Create a branch, make some commits, and merge it into main" Write-Hint "Example: git switch -c my-feature" } # ============================================================================ # Check for branches (excluding the example branches) # ============================================================================ $allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') } $exampleBranches = @($mainBranch, 'feature-login', 'feature-api', 'feature-database') $studentBranches = $allBranches | Where-Object { $_ -notin $exampleBranches } if ($studentBranches.Count -gt 0) { Write-Pass "Created $($studentBranches.Count) new branch(es): $($studentBranches -join ', ')" } else { Write-Info "No new branches found (it's OK if you deleted them after merging)" Write-Hint "To practice: git switch -c your-branch-name" } # ============================================================================ # Check for merge commits by the student # ============================================================================ $setupUser = "Workshop Student" $mergeCommits = git log --merges --format="%s" 2>$null # Count how many merge commits exist beyond the initial 3 $totalMerges = ($mergeCommits | Measure-Object).Count $setupMerges = 3 # feature-login, feature-api, feature-database if ($totalMerges -gt $setupMerges) { $studentMerges = $totalMerges - $setupMerges Write-Pass "Performed $studentMerges merge(s) of your own work" } else { Write-Fail "No merge commits found beyond the example merges" Write-Hint "Create a branch, add commits, then merge it: git merge your-branch-name" } # ============================================================================ # Check current branch # ============================================================================ $currentBranch = git branch --show-current 2>$null 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 $mainBranch" } Pop-Location # ============================================================================ # Final summary # ============================================================================ Write-Host "" if ($script:allChecksPassed) { Write-Host "=========================================" -ForegroundColor Green Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green Write-Host "=========================================" -ForegroundColor Green Write-Host "`nYou've successfully practiced:" -ForegroundColor Cyan Write-Host " ✓ Creating branches" -ForegroundColor White Write-Host " ✓ Making commits on branches" -ForegroundColor White Write-Host " ✓ Merging branches together" -ForegroundColor White Write-Host "`nReady for the next module!" -ForegroundColor Green Write-Host "" exit 0 } else { Write-Host "[SUMMARY] Some checks failed. Review the hints above." -ForegroundColor Red Write-Host "" 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 $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 "" exit 1 }