385 lines
10 KiB
PowerShell
385 lines
10 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
<#
|
|
.SYNOPSIS
|
|
Sets up the Module 05 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
|
|
- multi-revert: Reverting multiple commits at once
|
|
#>
|
|
|
|
Write-Host "`n=== Setting up Module 05: 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
|
|
$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
|
|
|
|
def multiply(a, b):
|
|
"""Multiply two numbers."""
|
|
return a * b
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Add multiply function" | Out-Null
|
|
|
|
# BAD commit: Add broken divide function
|
|
$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
|
|
|
|
def multiply(a, b):
|
|
"""Multiply two numbers."""
|
|
return a * b
|
|
|
|
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 "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Add broken divide function - needs to be reverted!" | Out-Null
|
|
|
|
# Good commit: Add modulo (after bad 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
|
|
|
|
def multiply(a, b):
|
|
"""Multiply two numbers."""
|
|
return a * b
|
|
|
|
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!
|
|
|
|
def modulo(a, b):
|
|
"""Return remainder of a divided by b."""
|
|
return a % b
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
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)
|
|
# ============================================================================
|
|
Write-Host "`nScenario 2: Creating merge-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 a feature branch to merge
|
|
git switch -c feature-auth | Out-Null
|
|
|
|
# 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!")
|
|
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
|
|
git add .
|
|
git commit -m "Add password validation" | Out-Null
|
|
|
|
# Integrate auth into calculator (part of the feature branch)
|
|
$calcContent = @"
|
|
# calculator.py - Simple calculator
|
|
from auth import login
|
|
|
|
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
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Integrate auth into calculator" | 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
|
|
|
|
Write-Host "[CREATED] merge-revert branch with merge commit to revert" -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
|
|
$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
|
|
|
|
def power(a, b):
|
|
"""Raise a to the power of b."""
|
|
return a ** b
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Add power function" | Out-Null
|
|
|
|
# BAD commit 1: Add broken square_root
|
|
$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
|
|
|
|
def power(a, b):
|
|
"""Raise a to the power of b."""
|
|
return a ** b
|
|
|
|
def square_root(a):
|
|
"""BROKEN: Returns wrong result for negative numbers!"""
|
|
return a ** 0.5 # This returns NaN for negative numbers!
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Add broken square_root - REVERT THIS!" | Out-Null
|
|
|
|
# BAD commit 2: Add broken logarithm
|
|
$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
|
|
|
|
def power(a, b):
|
|
"""Raise a to the power of b."""
|
|
return a ** b
|
|
|
|
def square_root(a):
|
|
"""BROKEN: Returns wrong result for negative numbers!"""
|
|
return a ** 0.5 # This returns NaN for negative numbers!
|
|
|
|
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 "calculator.py" -Value $calcContent
|
|
git add .
|
|
git commit -m "Add broken logarithm - REVERT THIS TOO!" | Out-Null
|
|
|
|
# Good commit: Add absolute value (after bad commits)
|
|
$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
|
|
|
|
def power(a, b):
|
|
"""Raise a to the power of b."""
|
|
return a ** b
|
|
|
|
def square_root(a):
|
|
"""BROKEN: Returns wrong result for negative numbers!"""
|
|
return a ** 0.5 # This returns NaN for negative numbers!
|
|
|
|
def logarithm(a):
|
|
"""BROKEN: Doesn't handle zero or negative numbers!"""
|
|
import math
|
|
return math.log(a) # This crashes for a <= 0!
|
|
|
|
def absolute(a):
|
|
"""Return absolute value of a."""
|
|
return abs(a)
|
|
"@
|
|
Set-Content -Path "calculator.py" -Value $calcContent
|
|
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 single bad commit (basic)" -ForegroundColor White
|
|
Write-Host " 2. merge-revert - Revert a merge commit with -m flag" -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 ""
|