Files
git-workshop/module-09-cherry-pick/setup.ps1
2026-01-04 17:29:52 +01:00

209 lines
5.3 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the cherry-pick challenge environment.
.DESCRIPTION
Creates a Git repository with a development branch containing both
bug fixes and experimental features. Students must cherry-pick only
the bug fixes to the main branch.
#>
# 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 commits on main branch
$app = @"
class App {
constructor() {
this.version = '1.0.0';
}
start() {
console.log('App started');
}
}
module.exports = App;
"@
Set-Content -Path "app.js" -Value $app
git add app.js
git commit -m "Initial app implementation" | Out-Null
$readme = @"
# Application
Version 1.0.0
"@
Set-Content -Path "README.md" -Value $readme
git add README.md
git commit -m "Add README" | Out-Null
# Create development branch
git checkout -b development | Out-Null
# Commit 1: Experimental feature (should NOT be cherry-picked)
$appWithExperimental = @"
class App {
constructor() {
this.version = '1.0.0';
this.experimentalMode = false;
}
start() {
console.log('App started');
if (this.experimentalMode) {
this.enableExperimentalFeatures();
}
}
enableExperimentalFeatures() {
console.log('Experimental features enabled');
}
}
module.exports = App;
"@
Set-Content -Path "app.js" -Value $appWithExperimental
git add app.js
git commit -m "Add experimental AI features" | Out-Null
# Commit 2: Security bug fix (SHOULD be cherry-picked)
$security = @"
class Security {
static sanitizeInput(input) {
// Remove potential XSS attacks
return input.replace(/[<>]/g, '');
}
static validateToken(token) {
if (!token || token.length < 32) {
throw new Error('Invalid security token');
}
return true;
}
}
module.exports = Security;
"@
Set-Content -Path "security.js" -Value $security
git add security.js
git commit -m "Fix security vulnerability in input validation" | Out-Null
# Commit 3: Another experimental feature (should NOT be cherry-picked)
$appWithMoreExperimental = @"
class App {
constructor() {
this.version = '1.0.0';
this.experimentalMode = false;
this.betaFeatures = [];
}
start() {
console.log('App started');
if (this.experimentalMode) {
this.enableExperimentalFeatures();
}
}
enableExperimentalFeatures() {
console.log('Experimental features enabled');
}
addBetaFeature(feature) {
this.betaFeatures.push(feature);
}
}
module.exports = App;
"@
Set-Content -Path "app.js" -Value $appWithMoreExperimental
git add app.js
git commit -m "Add beta features framework" | Out-Null
# Commit 4: Performance bug fix (SHOULD be cherry-picked)
$appWithPerformance = @"
class App {
constructor() {
this.version = '1.0.0';
this.experimentalMode = false;
this.betaFeatures = [];
this.cache = new Map();
}
start() {
console.log('App started');
if (this.experimentalMode) {
this.enableExperimentalFeatures();
}
}
enableExperimentalFeatures() {
console.log('Experimental features enabled');
}
addBetaFeature(feature) {
this.betaFeatures.push(feature);
}
getData(key) {
// Use cache to improve performance
if (this.cache.has(key)) {
return this.cache.get(key);
}
const data = this.fetchData(key);
this.cache.set(key, data);
return data;
}
fetchData(key) {
// Simulate data fetching
return { key: key, value: 'data' };
}
}
module.exports = App;
"@
Set-Content -Path "app.js" -Value $appWithPerformance
git add app.js
git commit -m "Fix performance issue with data caching" | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "Challenge environment created!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou are on the 'development' branch with multiple commits:" -ForegroundColor Cyan
Write-Host "- Experimental features (not ready for production)" -ForegroundColor Yellow
Write-Host "- Critical bug fixes (needed in production NOW)" -ForegroundColor Green
Write-Host "`nYour task:" -ForegroundColor Yellow
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
Write-Host "2. View the development branch commits: git log --oneline" -ForegroundColor White
Write-Host "3. Identify which commits are bug fixes (look for 'Fix' in messages)" -ForegroundColor White
Write-Host "4. Switch to main branch: git checkout main" -ForegroundColor White
Write-Host "5. Cherry-pick ONLY the bug fix commits to main" -ForegroundColor White
Write-Host "6. Do NOT bring the experimental features to main" -ForegroundColor White
Write-Host "`nHint: Look for commits mentioning 'security' and 'performance' fixes" -ForegroundColor Cyan
Write-Host "Run '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan