Files
git-workshop/module-10-reset-vs-revert/verify.ps1
2026-01-04 17:35:25 +01:00

173 lines
6.9 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the reset vs revert challenge solution.
.DESCRIPTION
Checks that the user correctly used reset on the local branch
and revert on the shared branch.
#>
Set-Location "challenge" -ErrorAction SilentlyContinue
# Check if challenge directory exists
if (-not (Test-Path "../verify.ps1")) {
Write-Host "Error: Please run this script from the module directory" -ForegroundColor Red
exit 1
}
if (-not (Test-Path ".")) {
Write-Host "Error: Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
Set-Location ..
exit 1
}
Write-Host "Verifying your solution..." -ForegroundColor Cyan
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] No git repository found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Verify local-feature branch
Write-Host "`nChecking local-feature branch..." -ForegroundColor Cyan
git checkout local-feature 2>$null | Out-Null
# Check commit count on local-feature (should be 3: initial + README + multiply)
$localCommitCount = (git rev-list --count local-feature 2>$null)
if ($localCommitCount -ne 3) {
Write-Host "[FAIL] local-feature should have 3 commits, found $localCommitCount" -ForegroundColor Red
if ($localCommitCount -gt 3) {
Write-Host "Hint: The bad commit should be removed. Use 'git reset --hard HEAD~1'" -ForegroundColor Yellow
} else {
Write-Host "Hint: You may have reset too far. Run ../reset.ps1 to start over." -ForegroundColor Yellow
}
Set-Location ..
exit 1
}
# Check that calculator.js exists
if (-not (Test-Path "calculator.js")) {
Write-Host "[FAIL] calculator.js not found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check calculator.js on local-feature
$localCalcContent = Get-Content "calculator.js" -Raw
# Should have multiply function
if ($localCalcContent -notmatch "multiply") {
Write-Host "[FAIL] calculator.js should have the multiply function." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should NOT have divide function (it was in the bad commit that should be reset)
if ($localCalcContent -match "divide") {
Write-Host "[FAIL] calculator.js should NOT have the divide function." -ForegroundColor Red
Write-Host "Hint: Use 'git reset --hard HEAD~1' to remove the bad commit" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check commit messages on local-feature
$localCommits = git log --pretty=format:"%s" local-feature 2>$null
if ($localCommits -match "broken divide") {
Write-Host "[FAIL] The 'broken divide' commit should be removed from local-feature." -ForegroundColor Red
Write-Host "Hint: Use 'git reset --hard HEAD~1' to remove it" -ForegroundColor Yellow
Set-Location ..
exit 1
}
Write-Host "[PASS] local-feature branch correctly reset!" -ForegroundColor Green
# Verify shared-feature branch
Write-Host "`nChecking shared-feature branch..." -ForegroundColor Cyan
git checkout shared-feature 2>$null | Out-Null
# Check commit count on shared-feature
# Should be 6: initial + README + power + broken feature + modulo + revert
$sharedCommitCount = (git rev-list --count shared-feature 2>$null)
if ($sharedCommitCount -ne 6) {
Write-Host "[FAIL] shared-feature should have 6 commits, found $sharedCommitCount" -ForegroundColor Red
if ($sharedCommitCount -lt 6) {
Write-Host "Hint: You should REVERT the bad commit, not reset it." -ForegroundColor Yellow
Write-Host " Revert creates a new commit that undoes the bad one." -ForegroundColor Yellow
Write-Host " Use: git revert <commit-hash>" -ForegroundColor Yellow
} else {
Write-Host "Hint: You should have exactly 6 commits after reverting." -ForegroundColor Yellow
}
Set-Location ..
exit 1
}
# Check that there's a revert commit
$sharedCommits = git log --pretty=format:"%s" shared-feature 2>$null
if ($sharedCommits -notmatch "Revert") {
Write-Host "[FAIL] No revert commit found on shared-feature." -ForegroundColor Red
Write-Host "Hint: Use 'git revert <commit-hash>' to undo the bad commit" -ForegroundColor Yellow
Write-Host " Find the hash with: git log --oneline" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check calculator.js on shared-feature
$sharedCalcContent = Get-Content "calculator.js" -Raw
# Should have power function
if ($sharedCalcContent -notmatch "power") {
Write-Host "[FAIL] calculator.js should have the power function." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should have modulo function (commits after the reverted one should be preserved)
if ($sharedCalcContent -notmatch "modulo") {
Write-Host "[FAIL] calculator.js should have the modulo function." -ForegroundColor Red
Write-Host "Hint: Reverting should preserve commits made after the bad one" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Should NOT have squareRoot function (it was in the bad commit that should be reverted)
if ($sharedCalcContent -match "squareRoot") {
Write-Host "[FAIL] calculator.js should NOT have the squareRoot function." -ForegroundColor Red
Write-Host "Hint: The 'Add broken feature' commit should be reverted" -ForegroundColor Yellow
Write-Host " Use: git revert <commit-hash-of-broken-feature>" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Verify the revert commit specifically reverted the "Add broken feature" commit
$revertCommitMessage = git log --grep="Revert" --pretty=format:"%s" -n 1 2>$null
if ($revertCommitMessage -notmatch "broken feature") {
Write-Host "[FAIL] The revert commit should mention 'broken feature'." -ForegroundColor Red
Write-Host "Hint: Make sure you reverted the correct commit (the one that added squareRoot)" -ForegroundColor Yellow
Set-Location ..
exit 1
}
Write-Host "[PASS] shared-feature branch correctly reverted!" -ForegroundColor Green
# Success!
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "SUCCESS! Challenge completed!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou have successfully:" -ForegroundColor Cyan
Write-Host "- Used 'git reset' on local-feature (private branch)" -ForegroundColor White
Write-Host " Removed the bad commit completely from history" -ForegroundColor White
Write-Host "- Used 'git revert' on shared-feature (public branch)" -ForegroundColor White
Write-Host " Created a new commit that undoes the bad one" -ForegroundColor White
Write-Host " Preserved all history and subsequent commits" -ForegroundColor White
Write-Host "`nYou now understand when to use reset vs revert!" -ForegroundColor Green
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
Write-Host "- Reset rewrites history (use only on private commits)" -ForegroundColor White
Write-Host "- Revert preserves history (safe for shared commits)`n" -ForegroundColor White
Set-Location ..
exit 0