Files
git-workshop/module-10-stash/setup.ps1
2026-01-04 17:53:41 +01:00

179 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 {
constructor() {
this.name = 'MyApp';
this.version = '1.0.0';
}
start() {
console.log('Application started');
this.authenticate();
}
authenticate() {
console.log('Authentication check');
return true;
}
}
module.exports = Application;
"@
Set-Content -Path "app.js" -Value $app
git add app.js
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 = @"
class LoginService {
constructor() {
this.users = new Map();
}
register(username, password) {
if (this.users.has(username)) {
throw new Error('User already exists');
}
this.users.set(username, { password, createdAt: new Date() });
return true;
}
}
module.exports = LoginService;
"@
Set-Content -Path "login.js" -Value $loginInitial
git add login.js
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 {
constructor() {
this.name = 'MyApp';
this.version = '1.0.0';
}
start() {
console.log('Application started');
this.authenticate();
}
authenticate() {
console.log('Authentication check');
// BUG: This allows unauthenticated access!
return true; // Should check actual credentials
}
}
module.exports = Application;
"@
Set-Content -Path "app.js" -Value $appWithBug
git add app.js
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 = @"
class LoginService {
constructor() {
this.users = new Map();
}
register(username, password) {
if (this.users.has(username)) {
throw new Error('User already exists');
}
this.users.set(username, { password, createdAt: new Date() });
return true;
}
// TODO: Complete this method
login(username, password) {
if (!this.users.has(username)) {
throw new Error('User not found');
}
// TODO: Verify password
// TODO: Return user session
}
// TODO: Add logout method
}
module.exports = LoginService;
"@
Set-Content -Path "login.js" -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.js (remove the comment and fix the auth)" -ForegroundColor White
Write-Host "6. Commit the fix: git add app.js && 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.js" -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