Files
git-workshop/02_advanced/03-worktrees/setup.ps1
2026-01-07 23:46:32 +01:00

124 lines
4.1 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the worktrees challenge environment.
.DESCRIPTION
Creates a Git repository with a feature in progress, ready for
demonstrating the use of worktrees for parallel work.
#>
# 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"
# Create main repository
New-Item -ItemType Directory -Path "main-repo" | Out-Null
Set-Location "main-repo"
# 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 calculator with a bug
$calculator = @"
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: No division by zero check!
def divide(self, a, b):
return a / b
"@
Set-Content -Path "calculator.py" -Value $calculator
git add calculator.py
git commit -m "Initial calculator implementation" | Out-Null
$readme = @"
# Calculator Project
A simple calculator with basic operations.
## Features
- Addition
- Subtraction
- Multiplication
- Division (has a bug!)
"@
Set-Content -Path "README.md" -Value $readme
git add README.md
git commit -m "Add README" | Out-Null
# Create feature branch and start working on it
git checkout -b feature-advanced-math | Out-Null
# Add work in progress on feature branch
$calculatorWithFeature = @"
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: No division by zero check!
def divide(self, a, b):
return a / b
# New feature: power function (work in progress)
def power(self, a, b):
return a ** b
# TODO: Add square root function
# TODO: Add logarithm function
"@
Set-Content -Path "calculator.py" -Value $calculatorWithFeature
git add calculator.py
git commit -m "Add power function (WIP: more math functions coming)" | Out-Null
# Return to challenge directory
Set-Location ..
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "Challenge environment created!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nSituation:" -ForegroundColor Cyan
Write-Host "You're working on the 'feature-advanced-math' branch" -ForegroundColor White
Write-Host "You have plans to add more math functions (see TODOs)" -ForegroundColor White
Write-Host "`nUrgent: A critical bug was discovered in the divide function!" -ForegroundColor Red
Write-Host "It doesn't check for division by zero." -ForegroundColor Red
Write-Host "`nInstead of stashing your feature work, use a worktree:" -ForegroundColor Yellow
Write-Host "`nYour task:" -ForegroundColor Yellow
Write-Host "1. Navigate to main-repo: cd challenge/main-repo" -ForegroundColor White
Write-Host "2. Create a worktree: git worktree add ../bugfix-worktree -b bugfix" -ForegroundColor White
Write-Host "3. Go to worktree: cd ../bugfix-worktree" -ForegroundColor White
Write-Host "4. Fix the bug in calculator.py:" -ForegroundColor White
Write-Host " Add a check: if (b === 0) throw new Error('Division by zero');" -ForegroundColor White
Write-Host "5. Commit the fix: git add . && git commit -m 'Fix divide by zero bug'" -ForegroundColor White
Write-Host "6. Return to main-repo: cd ../main-repo" -ForegroundColor White
Write-Host "7. Complete your feature: Add square root method to calculator.py" -ForegroundColor White
Write-Host "8. Commit: git add . && git commit -m 'Add square root function'" -ForegroundColor White
Write-Host "9. Clean up worktree: git worktree remove ../bugfix-worktree" -ForegroundColor White
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan