#!/usr/bin/env pwsh <# .SYNOPSIS Sets up the reset vs revert challenge environment. .DESCRIPTION Creates a Git repository with two branches: - local-feature: A private branch where reset should be used - shared-feature: A pushed branch where revert should be used #> # Remove existing challenge directory if present if (Test-Path "challenge") { Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow Remove-Item -Path "challenge" -Recurse -Force } # Create challenge directory Write-Host "Creating challenge environment..." -ForegroundColor Cyan New-Item -ItemType Directory -Path "challenge" | Out-Null Set-Location "challenge" # Initialize git repository git init | Out-Null git config user.name "Workshop User" | Out-Null git config user.email "user@workshop.local" | Out-Null # Create initial commits on main $app = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $app git add calculator.js git commit -m "Initial calculator implementation" | Out-Null $readme = @" # Calculator App A simple calculator application. "@ Set-Content -Path "README.md" -Value $readme git add README.md git commit -m "Add README" | Out-Null # Create local-feature branch (private, not shared) git checkout -b local-feature | Out-Null $appWithMultiply = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } multiply(a, b) { return a * b; } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $appWithMultiply git add calculator.js git commit -m "Add multiply function" | Out-Null # Add a bad commit that should be removed with reset $appWithBadCode = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } multiply(a, b) { return a * b; } // BUG: This is broken and should never have been committed! divide(a, b) { // Forgot to check for division by zero return a / b; // This will return Infinity or NaN for zero! } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $appWithBadCode git add calculator.js git commit -m "Add broken divide function - DO NOT KEEP" | Out-Null # Switch back to main for shared-feature branch git checkout main | Out-Null # Create shared-feature branch (simulating a pushed/shared branch) git checkout -b shared-feature | Out-Null $appWithPower = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } power(a, b) { return Math.pow(a, b); } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $appWithPower git add calculator.js git commit -m "Add power function" | Out-Null # Add a bad commit that should be reverted (not reset) $appWithBrokenFeature = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } power(a, b) { return Math.pow(a, b); } // BUG: This breaks the calculator! squareRoot(a) { // This implementation is wrong for negative numbers return Math.sqrt(a); // Returns NaN for negative numbers without warning! } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $appWithBrokenFeature git add calculator.js git commit -m "Add broken feature" | Out-Null # Add another good commit after the bad one (to show that revert preserves subsequent commits) $appWithMoreFeatures = @" class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } power(a, b) { return Math.pow(a, b); } // BUG: This breaks the calculator! squareRoot(a) { // This implementation is wrong for negative numbers return Math.sqrt(a); // Returns NaN for negative numbers without warning! } modulo(a, b) { return a % b; } } module.exports = Calculator; "@ Set-Content -Path "calculator.js" -Value $appWithMoreFeatures git add calculator.js git commit -m "Add modulo function" | Out-Null # Switch to local-feature for the challenge start git checkout local-feature | Out-Null # Return to module directory Set-Location .. Write-Host "`n========================================" -ForegroundColor Green Write-Host "Challenge environment created!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green Write-Host "`nYou have two branches with bad commits:" -ForegroundColor Cyan Write-Host "`n1. local-feature (PRIVATE - not shared):" -ForegroundColor Yellow Write-Host " - Has a broken divide function commit" -ForegroundColor White Write-Host " - Safe to use 'git reset' to remove it" -ForegroundColor Green Write-Host "`n2. shared-feature (PUBLIC - shared with team):" -ForegroundColor Yellow Write-Host " - Has a broken feature commit" -ForegroundColor White Write-Host " - Must use 'git revert' to undo it safely" -ForegroundColor Green Write-Host "`nYour task:" -ForegroundColor Yellow Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White Write-Host "2. You're on local-feature - view commits: git log --oneline" -ForegroundColor White Write-Host "3. Remove the bad commit with: git reset --hard HEAD~1" -ForegroundColor White Write-Host "4. Switch to shared-feature: git checkout shared-feature" -ForegroundColor White Write-Host "5. Find the 'Add broken feature' commit hash: git log --oneline" -ForegroundColor White Write-Host "6. Revert it with: git revert " -ForegroundColor White Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan