Files
git-workshop/01_essentials/07-reset-vs-revert/setup.ps1
2026-01-07 17:59:02 +01:00

191 lines
5.6 KiB
PowerShell

#!/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:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
"@
Set-Content -Path "calculator.py" -Value $app
git add calculator.py
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:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
"@
Set-Content -Path "calculator.py" -Value $appWithMultiply
git add calculator.py
git commit -m "Add multiply function" | Out-Null
# Add a bad commit that should be removed with reset
$appWithBadCode = @"
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
# BUG: This is broken and should never have been committed!
def divide(self, a, b):
# Forgot to check for division by zero
return a / b # This will raise ZeroDivisionError for zero!
"@
Set-Content -Path "calculator.py" -Value $appWithBadCode
git add calculator.py
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:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
"@
Set-Content -Path "calculator.py" -Value $appWithPower
git add calculator.py
git commit -m "Add power function" | Out-Null
# Add a bad commit that should be reverted (not reset)
$appWithBrokenFeature = @"
import math
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
# BUG: This breaks the calculator!
def square_root(self, a):
# This implementation is wrong for negative numbers
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
"@
Set-Content -Path "calculator.py" -Value $appWithBrokenFeature
git add calculator.py
git commit -m "Add broken feature" | Out-Null
# Add another good commit after the bad one (to show that revert preserves subsequent commits)
$appWithMoreFeatures = @"
import math
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
# BUG: This breaks the calculator!
def square_root(self, a):
# This implementation is wrong for negative numbers
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
def modulo(self, a, b):
return a % b
"@
Set-Content -Path "calculator.py" -Value $appWithMoreFeatures
git add calculator.py
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 <commit-hash>" -ForegroundColor White
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan