refactor: rewrite the merge-revert section

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.
This commit is contained in:
Bjarke Sporring
2026-01-15 16:11:24 +01:00
parent aa24c50b45
commit 575e083f33
4 changed files with 186 additions and 282 deletions

View File

@@ -1,17 +1,17 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 05 challenge environment for learning git revert.
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
- merge-revert: Reverting a merge commit with -m flag
- 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 05: Git Revert Challenge ===" -ForegroundColor Cyan
Write-Host "`n=== Setting up Module 06: Git Revert Challenge ===" -ForegroundColor Cyan
# Remove existing challenge directory if it exists
if (Test-Path "challenge") {
@@ -104,90 +104,63 @@ git commit -m "Add modulo function" | Out-Null
Write-Host "[CREATED] regular-revert branch with bad divide commit" -ForegroundColor Green
# ============================================================================
# SCENARIO 2: Merge Revert (Merge Commit with -m flag)
# SCENARIO 2: Revert in Middle of History
# ============================================================================
Write-Host "`nScenario 2: Creating merge-revert scenario..." -ForegroundColor Cyan
Write-Host "`nScenario 2: Creating middle-revert scenario..." -ForegroundColor Cyan
# Switch back to main
git switch $mainBranch | Out-Null
# Create merge-revert branch
git switch -c merge-revert | Out-Null
# Create middle-revert branch
git switch -c middle-revert | Out-Null
# Create a feature branch to merge
git switch -c feature-auth | Out-Null
# Good commit: Add validation module
$validationContent = @"
# validation.py - Input validation
# Add auth functionality
$authContent = @"
# auth.py - Authentication module
def login(username, password):
\"\"\"Login user.\"\"\"
print(f"Logging in {username}...")
return True
def logout(username):
\"\"\"Logout user.\"\"\"
print(f"Logging out {username}...")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
git add .
git commit -m "Add authentication module" | Out-Null
# Add password validation
$authContent = @"
# auth.py - Authentication module
def validate_password(password):
\"\"\"Validate password strength.\"\"\"
return len(password) >= 8
def login(username, password):
\"\"\"Login user.\"\"\"
if not validate_password(password):
print("Password too weak!")
def validate_number(value):
"""Check if value is a valid number."""
try:
float(value)
return True
except ValueError:
return False
print(f"Logging in {username}...")
return True
def logout(username):
\"\"\"Logout user.\"\"\"
print(f"Logging out {username}...")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
Set-Content -Path "validation.py" -Value $validationContent
git add .
git commit -m "Add password validation" | Out-Null
git commit -m "Add input validation module" | Out-Null
# Integrate auth into calculator (part of the feature branch)
$calcContent = @"
# calculator.py - Simple calculator
from auth import login
# BAD commit: Add broken formatter
$formatterContent = @"
# formatter.py - Output formatting
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def secure_divide(a, b, username):
"""Secure divide - requires authentication."""
if login(username, "password123"):
return a / b
return None
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 "calculator.py" -Value $calcContent
Set-Content -Path "formatter.py" -Value $formatterContent
git add .
git commit -m "Integrate auth into calculator" | Out-Null
git commit -m "Add broken formatter - needs to be reverted!" | Out-Null
# Switch back to merge-revert and merge feature-auth
git switch merge-revert | Out-Null
git merge feature-auth --no-ff -m "Merge feature-auth branch" | Out-Null
# Good commit: Add configuration (depends on validation, not formatter)
$configContent = @"
# config.py - Application configuration
Write-Host "[CREATED] merge-revert branch with merge commit to revert" -ForegroundColor Green
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)
@@ -275,8 +248,8 @@ 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 single bad commit (basic)" -ForegroundColor White
Write-Host " 2. merge-revert - Revert a merge commit with -m flag" -ForegroundColor White
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