Files
git-workshop/01_essentials/08-stash/setup.ps1
2026-01-07 17:59:02 +01:00

157 lines
5.0 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the stash challenge environment.
.DESCRIPTION
Creates a Git repository with work in progress that needs to be stashed
while handling an urgent bug fix.
#>
# 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 application on main
$app = @"
class Application:
def __init__(self):
self.name = 'MyApp'
self.version = '1.0.0'
def start(self):
print('Application started')
self.authenticate()
def authenticate(self):
print('Authentication check')
return True
"@
Set-Content -Path "app.py" -Value $app
git add app.py
git commit -m "Initial application" | Out-Null
$readme = @"
# MyApp
A sample application for learning Git stash.
"@
Set-Content -Path "README.md" -Value $readme
git add README.md
git commit -m "Add README" | Out-Null
# Create feature branch for login feature
git checkout -b feature-login | Out-Null
# Start working on login feature
$loginInitial = @"
from datetime import datetime
class LoginService:
def __init__(self):
self.users = {}
def register(self, username, password):
if username in self.users:
raise ValueError('User already exists')
self.users[username] = {'password': password, 'created_at': datetime.now()}
return True
"@
Set-Content -Path "login.py" -Value $loginInitial
git add login.py
git commit -m "Start login service implementation" | Out-Null
# Add a critical bug to main branch (simulating a bug that was introduced)
git checkout main | Out-Null
$appWithBug = @"
class Application:
def __init__(self):
self.name = 'MyApp'
self.version = '1.0.0'
def start(self):
print('Application started')
self.authenticate()
def authenticate(self):
print('Authentication check')
# BUG: This allows unauthenticated access!
return True # Should check actual credentials
"@
Set-Content -Path "app.py" -Value $appWithBug
git add app.py
git commit -m "Update authentication (contains bug)" | Out-Null
# Go back to feature branch
git checkout feature-login | Out-Null
# Create work in progress (uncommitted changes)
$loginWIP = @"
from datetime import datetime
class LoginService:
def __init__(self):
self.users = {}
def register(self, username, password):
if username in self.users:
raise ValueError('User already exists')
self.users[username] = {'password': password, 'created_at': datetime.now()}
return True
# TODO: Complete this method
def login(self, username, password):
if username not in self.users:
raise ValueError('User not found')
# TODO: Verify password
# TODO: Return user session
# TODO: Add logout method
"@
Set-Content -Path "login.py" -Value $loginWIP
# Don't commit - leave as uncommitted changes
# Return to module 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 login feature (feature-login branch)" -ForegroundColor White
Write-Host "You have uncommitted changes - the feature is NOT complete yet" -ForegroundColor Yellow
Write-Host "`nUrgent: A critical security bug was found in production (main branch)!" -ForegroundColor Red
Write-Host "You need to fix it immediately, but your current work isn't ready to commit." -ForegroundColor Red
Write-Host "`nYour task:" -ForegroundColor Yellow
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
Write-Host "2. Check your status: git status (see uncommitted changes)" -ForegroundColor White
Write-Host "3. Stash your work: git stash save 'WIP: login feature'" -ForegroundColor White
Write-Host "4. Switch to main: git checkout main" -ForegroundColor White
Write-Host "5. Fix the security bug in app.py (remove the comment and fix the auth)" -ForegroundColor White
Write-Host "6. Commit the fix: git add app.py && git commit -m 'Fix critical security bug'" -ForegroundColor White
Write-Host "7. Switch back: git checkout feature-login" -ForegroundColor White
Write-Host "8. Restore your work: git stash pop" -ForegroundColor White
Write-Host "9. Complete the TODOs in login.py" -ForegroundColor White
Write-Host "10. Commit your completed feature" -ForegroundColor White
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan