Files
2026-01-21 10:51:59 +01:00

134 lines
5.7 KiB
PowerShell
Executable File

#!/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
$challengeRoot = "$PSScriptRoot\challenge"
# ============================================================================
# Check challenge directory exists
# ============================================================================
if (-not (Test-Path $challengeRoot)) {
Write-Host "[ERROR] Challenge directory not found." -ForegroundColor Red
Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
exit 1
}
if (-not (Test-Path "$challengeRoot\.git")) {
Write-Host "[ERROR] Not a git repository." -ForegroundColor Red
Write-Host "Run ..\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
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 = Get-MainBranch
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
}