It is an advanced and difficult revert to accomplish and should probably be done through a reset instead, which means that we're modifying history which is dangerous and so should be handled by someone who understands these dangers.
261 lines
8.1 KiB
PowerShell
261 lines
8.1 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
<#
|
|
.SYNOPSIS
|
|
Sets up the Module 06 challenge environment for learning git revert.
|
|
|
|
.DESCRIPTION
|
|
This script creates a challenge directory with three branches demonstrating
|
|
different revert scenarios:
|
|
- regular-revert: Basic revert of a single bad commit at the end
|
|
- middle-revert: Reverting a bad commit in the middle of history
|
|
- multi-revert: Reverting multiple commits at once
|
|
#>
|
|
|
|
Write-Host "`n=== Setting up Module 06: Git Revert Challenge ===" -ForegroundColor Cyan
|
|
|
|
# Remove existing challenge directory if it exists
|
|
if (Test-Path "challenge") {
|
|
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
|
|
Remove-Item -Recurse -Force "challenge"
|
|
}
|
|
|
|
# Create fresh challenge directory
|
|
Write-Host "Creating challenge directory..." -ForegroundColor Green
|
|
New-Item -ItemType Directory -Path "challenge" | Out-Null
|
|
Set-Location "challenge"
|
|
|
|
# Initialize Git repository
|
|
Write-Host "Initializing Git repository..." -ForegroundColor Green
|
|
git init | Out-Null
|
|
|
|
# Configure git for this repository
|
|
git config user.name "Workshop Student"
|
|
git config user.email "student@example.com"
|
|
|
|
# Detect the default branch name after first commit (created below)
|
|
# Will be detected after the initial commit in SCENARIO 1
|
|
|
|
# ============================================================================
|
|
# SCENARIO 1: Regular Revert (Basic)
|
|
# ============================================================================
|
|
Write-Host "`nScenario 1: Creating regular-revert branch..." -ForegroundColor Cyan
|
|
|
|
# Initial commit
|
|
$calcContent = @"
|
|
# calculator.py - Simple calculator
|
|
|
|
def add(a, b):
|
|
"""Add two numbers."""
|
|
return a + b
|
|
|
|
def subtract(a, b):
|
|
"""Subtract b from a."""
|
|
return a - b
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Initial calculator implementation" | Out-Null
|
|
|
|
# Detect the main branch name after first commit
|
|
$mainBranch = git branch --show-current
|
|
if (-not $mainBranch) {
|
|
$mainBranch = git config --get init.defaultBranch
|
|
if (-not $mainBranch) { $mainBranch = "main" }
|
|
}
|
|
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
|
|
|
# Create regular-revert branch
|
|
git switch -c regular-revert | Out-Null
|
|
|
|
# Good commit: Add multiply using append
|
|
$multiplyFunc = @"
|
|
|
|
def multiply(a, b):
|
|
"""Multiply two numbers."""
|
|
return a * b
|
|
"@
|
|
Add-Content -Path "calculator.py" -Value $multiplyFunc
|
|
git add .
|
|
git commit -m "Add multiply function" | Out-Null
|
|
|
|
# BAD commit: Add broken divide function using separate file
|
|
$divideContent = @"
|
|
# divide.py - Division functionality
|
|
|
|
def divide(a, b):
|
|
"""Divide a by b - BROKEN: doesn't handle division by zero!"""
|
|
return a / b # This will crash if b is 0!
|
|
"@
|
|
Set-Content -Path "divide.py" -Value $divideContent
|
|
git add .
|
|
git commit -m "Add broken divide function - needs to be reverted!" | Out-Null
|
|
|
|
# Good commit: Add modulo (after bad commit) using append
|
|
$moduloFunc = @"
|
|
|
|
def modulo(a, b):
|
|
"""Return remainder of a divided by b."""
|
|
return a % b
|
|
"@
|
|
Add-Content -Path "calculator.py" -Value $moduloFunc
|
|
git add .
|
|
git commit -m "Add modulo function" | Out-Null
|
|
|
|
Write-Host "[CREATED] regular-revert branch with bad divide commit" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# SCENARIO 2: Revert in Middle of History
|
|
# ============================================================================
|
|
Write-Host "`nScenario 2: Creating middle-revert scenario..." -ForegroundColor Cyan
|
|
|
|
# Switch back to main
|
|
git switch $mainBranch | Out-Null
|
|
|
|
# Create middle-revert branch
|
|
git switch -c middle-revert | Out-Null
|
|
|
|
# Good commit: Add validation module
|
|
$validationContent = @"
|
|
# validation.py - Input validation
|
|
|
|
def validate_number(value):
|
|
"""Check if value is a valid number."""
|
|
try:
|
|
float(value)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
"@
|
|
Set-Content -Path "validation.py" -Value $validationContent
|
|
git add .
|
|
git commit -m "Add input validation module" | Out-Null
|
|
|
|
# BAD commit: Add broken formatter
|
|
$formatterContent = @"
|
|
# formatter.py - Output formatting
|
|
|
|
def format_result(result):
|
|
"""BROKEN: Doesn't handle None or errors properly!"""
|
|
return f"Result: {result.upper()}" # This crashes if result is not a string!
|
|
"@
|
|
Set-Content -Path "formatter.py" -Value $formatterContent
|
|
git add .
|
|
git commit -m "Add broken formatter - needs to be reverted!" | Out-Null
|
|
|
|
# Good commit: Add configuration (depends on validation, not formatter)
|
|
$configContent = @"
|
|
# config.py - Application configuration
|
|
|
|
from validation import validate_number
|
|
|
|
DEFAULT_PRECISION = 2
|
|
|
|
def set_precision(value):
|
|
"""Set calculation precision."""
|
|
if validate_number(value):
|
|
return int(value)
|
|
return DEFAULT_PRECISION
|
|
"@
|
|
Set-Content -Path "config.py" -Value $configContent
|
|
git add .
|
|
git commit -m "Add configuration module" | Out-Null
|
|
|
|
Write-Host "[CREATED] middle-revert branch with bad commit in the middle" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# SCENARIO 3: Multi Revert (Multiple Bad Commits)
|
|
# ============================================================================
|
|
Write-Host "`nScenario 3: Creating multi-revert branch..." -ForegroundColor Cyan
|
|
|
|
# Switch back to main
|
|
git switch $mainBranch | Out-Null
|
|
|
|
# Create multi-revert branch
|
|
git switch -c multi-revert | Out-Null
|
|
|
|
# Reset calculator to simple version
|
|
$calcContent = @"
|
|
# calculator.py - Simple calculator
|
|
|
|
def add(a, b):
|
|
"""Add two numbers."""
|
|
return a + b
|
|
|
|
def subtract(a, b):
|
|
"""Subtract b from a."""
|
|
return a - b
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Reset to basic calculator" | Out-Null
|
|
|
|
# Good commit: Add power function using append
|
|
$powerFunc = @"
|
|
|
|
def power(a, b):
|
|
"""Raise a to the power of b."""
|
|
return a ** b
|
|
"@
|
|
Add-Content -Path "calculator.py" -Value $powerFunc
|
|
git add .
|
|
git commit -m "Add power function" | Out-Null
|
|
|
|
# BAD commit 1: Add broken square_root in separate file
|
|
$sqrtContent = @"
|
|
# sqrt.py - Square root functionality
|
|
|
|
def square_root(a):
|
|
"""BROKEN: Returns wrong result for negative numbers!"""
|
|
return a ** 0.5 # This returns NaN for negative numbers!
|
|
"@
|
|
Set-Content -Path "sqrt.py" -Value $sqrtContent
|
|
git add .
|
|
git commit -m "Add broken square_root - REVERT THIS!" | Out-Null
|
|
|
|
# BAD commit 2: Add broken logarithm in separate file
|
|
$logContent = @"
|
|
# logarithm.py - Logarithm functionality
|
|
|
|
def logarithm(a):
|
|
"""BROKEN: Doesn't handle zero or negative numbers!"""
|
|
import math
|
|
return math.log(a) # This crashes for a <= 0!
|
|
"@
|
|
Set-Content -Path "logarithm.py" -Value $logContent
|
|
git add .
|
|
git commit -m "Add broken logarithm - REVERT THIS TOO!" | Out-Null
|
|
|
|
# Good commit: Add absolute value (after bad commits) using append
|
|
$absoluteFunc = @"
|
|
|
|
def absolute(a):
|
|
"""Return absolute value of a."""
|
|
return abs(a)
|
|
"@
|
|
Add-Content -Path "calculator.py" -Value $absoluteFunc
|
|
git add .
|
|
git commit -m "Add absolute value function" | Out-Null
|
|
|
|
Write-Host "[CREATED] multi-revert branch with two bad commits to revert" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# Return to regular-revert to start
|
|
# ============================================================================
|
|
git switch regular-revert | Out-Null
|
|
|
|
# Return to module directory
|
|
Set-Location ..
|
|
|
|
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
|
Write-Host "`nThree revert scenarios have been created:" -ForegroundColor Cyan
|
|
Write-Host " 1. regular-revert - Revert a bad commit at the end" -ForegroundColor White
|
|
Write-Host " 2. middle-revert - Revert a bad commit in the middle of history" -ForegroundColor White
|
|
Write-Host " 3. multi-revert - Revert multiple bad commits" -ForegroundColor White
|
|
Write-Host "`nYou are currently on the 'regular-revert' branch." -ForegroundColor Cyan
|
|
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
|
Write-Host " 1. cd challenge" -ForegroundColor White
|
|
Write-Host " 2. Read the README.md for detailed instructions" -ForegroundColor White
|
|
Write-Host " 3. Complete each revert challenge" -ForegroundColor White
|
|
Write-Host " 4. Run '..\verify.ps1' to check your solutions" -ForegroundColor White
|
|
Write-Host ""
|