Files
git-workshop/01-essentials/07-reset/verify.ps1
2026-01-15 12:51:43 +01:00

232 lines
9.8 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 06 challenge solutions.
.DESCRIPTION
Checks that all three reset scenarios have been completed correctly:
- soft-reset: Commit reset but changes remain staged
- mixed-reset: Commits reset and changes unstaged
- hard-reset: Everything reset and discarded
#>
Write-Host "`n=== Verifying Module 06: Git Reset Solutions ===" -ForegroundColor Cyan
Write-Host "⚠️ Remember: NEVER reset pushed commits! ⚠️" -ForegroundColor Red
$allChecksPassed = $true
$originalDir = Get-Location
# Check if challenge directory exists
if (-not (Test-Path "challenge")) {
Write-Host "[FAIL] Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
exit 1
}
Set-Location "challenge"
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] Not a git repository. Run setup.ps1 first." -ForegroundColor Red
Set-Location $originalDir
exit 1
}
# ============================================================================
# SCENARIO 1: Soft Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 1: Soft Reset ===`n" -ForegroundColor Cyan
git switch soft-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] soft-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 4: Initial + project setup + feature A + feature B)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 4) {
Write-Host "[PASS] Commit count is 4 (feature C commit was reset)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Expected 4 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --soft HEAD~1" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check if changes are staged
$stagedChanges = git diff --cached --name-only 2>$null
if ($stagedChanges) {
Write-Host "[PASS] Changes are staged (feature C code in staging area)" -ForegroundColor Green
# Verify the staged changes contain feature C code
$stagedContent = git diff --cached 2>$null
if ($stagedContent -match "feature_c") {
Write-Host "[PASS] Staged changes contain feature C code" -ForegroundColor Green
} else {
Write-Host "[INFO] Staged changes don't seem to contain feature C" -ForegroundColor Yellow
}
} else {
Write-Host "[FAIL] No staged changes found" -ForegroundColor Red
Write-Host "[HINT] After --soft reset, changes should remain staged" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check working directory has no unstaged changes to tracked files
$unstagedChanges = git diff --name-only 2>$null
if (-not $unstagedChanges) {
Write-Host "[PASS] No unstaged changes (all changes are staged)" -ForegroundColor Green
} else {
Write-Host "[INFO] Found unstaged changes (expected only staged changes)" -ForegroundColor Yellow
}
}
# ============================================================================
# SCENARIO 2: Mixed Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 2: Mixed Reset ===`n" -ForegroundColor Cyan
git switch mixed-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] mixed-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 3: Initial + lifecycle + logging, bad commits removed)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 3) {
Write-Host "[PASS] Commit count is 3 (both bad commits were reset)" -ForegroundColor Green
} elseif ($commitCount -eq 4) {
Write-Host "[INFO] Commit count is 4 (one commit reset, need to reset one more)" -ForegroundColor Yellow
Write-Host "[HINT] Use: git reset HEAD~1 (or git reset --mixed HEAD~1)" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Host "[FAIL] Expected 3 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --mixed HEAD~2 to remove both bad commits" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there are NO staged changes
$stagedChanges = git diff --cached --name-only 2>$null
if (-not $stagedChanges) {
Write-Host "[PASS] No staged changes (--mixed unstages everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found staged changes (--mixed should unstage)" -ForegroundColor Red
Write-Host "[HINT] After --mixed reset, changes should be unstaged" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there ARE unstaged changes in working directory
$unstagedChanges = git diff --name-only 2>$null
if ($unstagedChanges) {
Write-Host "[PASS] Unstaged changes present in working directory" -ForegroundColor Green
# Verify unstaged changes contain the experimental/debug code
$workingContent = git diff 2>$null
if ($workingContent -match "experimental|DEBUG") {
Write-Host "[PASS] Unstaged changes contain the reset code" -ForegroundColor Green
} else {
Write-Host "[INFO] Unstaged changes don't contain expected code" -ForegroundColor Yellow
}
} else {
Write-Host "[FAIL] No unstaged changes found" -ForegroundColor Red
Write-Host "[HINT] After --mixed reset, changes should be in working directory (unstaged)" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
# ============================================================================
# SCENARIO 3: Hard Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 3: Hard Reset ===`n" -ForegroundColor Cyan
git switch hard-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] hard-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 3: Initial + utilities + helper C, bad commit removed)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 3) {
Write-Host "[PASS] Commit count is 3 (broken commit was reset)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Expected 3 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --hard HEAD~1" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there are NO staged changes
$stagedChanges = git diff --cached --name-only 2>$null
if (-not $stagedChanges) {
Write-Host "[PASS] No staged changes (--hard discards everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found staged changes (--hard should discard all)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that there are NO unstaged changes
$unstagedChanges = git diff --name-only 2>$null
if (-not $unstagedChanges) {
Write-Host "[PASS] No unstaged changes (--hard discards everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found unstaged changes (--hard should discard all)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check working directory is clean
$statusOutput = git status --porcelain 2>$null
if (-not $statusOutput) {
Write-Host "[PASS] Working directory is completely clean" -ForegroundColor Green
} else {
Write-Host "[INFO] Working directory has some changes" -ForegroundColor Yellow
}
# Verify the file doesn't have the broken code
if (Test-Path "utils.py") {
$utilsContent = Get-Content "utils.py" -Raw
if ($utilsContent -notmatch "helper_d") {
Write-Host "[PASS] Broken helper_d function is gone" -ForegroundColor Green
} else {
Write-Host "[FAIL] Broken helper_d still exists (wasn't reset)" -ForegroundColor Red
$allChecksPassed = $false
}
if ($utilsContent -match "helper_c") {
Write-Host "[PASS] Good helper_c function is preserved" -ForegroundColor Green
} else {
Write-Host "[FAIL] Good helper_c function missing" -ForegroundColor Red
$allChecksPassed = $false
}
}
}
Set-Location $originalDir
# Final summary
Write-Host ""
if ($allChecksPassed) {
Write-Host "==========================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! ALL SCENARIOS PASSED!" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green
Write-Host "`nYou've mastered git reset!" -ForegroundColor Cyan
Write-Host "You now understand:" -ForegroundColor Cyan
Write-Host " ✓ Resetting commits with --soft (keep staged)" -ForegroundColor White
Write-Host " ✓ Resetting commits with --mixed (unstage)" -ForegroundColor White
Write-Host " ✓ Resetting commits with --hard (discard all)" -ForegroundColor White
Write-Host " ✓ The DANGER of reset on shared history" -ForegroundColor White
Write-Host "`n⚠️ CRITICAL REMINDER ⚠️" -ForegroundColor Red
Write-Host "NEVER use 'git reset' on commits you've already PUSHED!" -ForegroundColor Red
Write-Host "Always use 'git revert' (Module 05) for shared commits!" -ForegroundColor Yellow
Write-Host "`nReady for Module 07: Git Stash!" -ForegroundColor Green
Write-Host ""
exit 0
} else {
Write-Host "[SUMMARY] Some checks failed. Review the hints above and try again." -ForegroundColor Red
Write-Host "[INFO] You can run this verification script as many times as needed." -ForegroundColor Yellow
Write-Host "[REMINDER] Reset is ONLY for local, un-pushed commits!" -ForegroundColor Yellow
Write-Host ""
exit 1
}