Files
git-workshop/01_essentials/04-merging/setup.ps1
2026-01-07 23:02:55 +01:00

247 lines
6.5 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 04 challenge environment for learning about merging.
.DESCRIPTION
This script creates a challenge directory with a Git repository that
contains two divergent branches ready to merge (three-way merge scenario).
#>
Write-Host "`n=== Setting up Module 04 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"
# Commit 1: Initial project structure on main
Write-Host "Creating initial project structure..." -ForegroundColor Green
$readmeContent = @"
# My Project
A simple web application project.
## Setup
Coming soon...
"@
Set-Content -Path "README.md" -Value $readmeContent
$appContent = @"
# app.py - Main application file
def main():
print("Welcome to My App!")
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Initial project structure" | Out-Null
# Commit 2: Add configuration file on main
Write-Host "Adding configuration..." -ForegroundColor Green
$configContent = @"
# config.py - Configuration settings
APP_NAME = "My Project"
VERSION = "1.0.0"
DEBUG = False
"@
Set-Content -Path "config.py" -Value $configContent
git add .
git commit -m "Add basic configuration" | Out-Null
# Commit 3: Add database utilities on main
Write-Host "Adding database utilities..." -ForegroundColor Green
$dbContent = @"
# database.py - Database utilities
def connect():
"""Connect to database."""
print("Connecting to database...")
return True
def disconnect():
"""Disconnect from database."""
print("Disconnecting...")
return True
"@
Set-Content -Path "database.py" -Value $dbContent
git add .
git commit -m "Add database utilities" | Out-Null
# Create feature-login branch
Write-Host "Creating feature-login branch..." -ForegroundColor Green
git switch -c feature-login | Out-Null
# Commit 4: Add login module on feature-login
Write-Host "Adding login functionality on feature-login..." -ForegroundColor Green
$loginContent = @"
# login.py - User login module
def login(username, password):
"""Authenticate a user."""
print(f"Authenticating user: {username}")
# TODO: Add actual authentication logic
return True
def logout(username):
"""Log out a user."""
print(f"Logging out user: {username}")
return True
"@
Set-Content -Path "login.py" -Value $loginContent
git add .
git commit -m "Add login module" | Out-Null
# Commit 5: Add password validation on feature-login
$loginContent = @"
# login.py - User login module
def validate_password(password):
"""Validate password strength."""
if len(password) < 8:
return False
return True
def login(username, password):
"""Authenticate a user."""
if not validate_password(password):
print("Password too weak!")
return False
print(f"Authenticating user: {username}")
# TODO: Add actual authentication logic
return True
def logout(username):
"""Log out a user."""
print(f"Logging out user: {username}")
return True
"@
Set-Content -Path "login.py" -Value $loginContent
git add .
git commit -m "Add password validation" | Out-Null
# Commit 6: Integrate login with app on feature-login
$appContent = @"
# app.py - Main application file
from login import login, logout
def main():
print("Welcome to My App!")
# Add login integration
if login("testuser", "password123"):
print("Login successful!")
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Integrate login with main app" | Out-Null
# Commit 7: Add session management on feature-login
$sessionContent = @"
# session.py - Session management
class Session:
def __init__(self, username):
self.username = username
self.active = True
def end(self):
"""End the session."""
self.active = False
print(f"Session ended for {self.username}")
"@
Set-Content -Path "session.py" -Value $sessionContent
git add .
git commit -m "Add session management" | Out-Null
# Switch back to main branch
Write-Host "Switching back to main branch..." -ForegroundColor Green
git switch main | Out-Null
# Commit 8: Update README on main (creates divergence)
Write-Host "Adding documentation on main (creates divergence)..." -ForegroundColor Green
$readmeContent = @"
# My Project
A simple web application project.
## Setup
1. Install Python 3.8 or higher
2. Run: python app.py
## Features
- User authentication (coming soon)
- Data management (coming soon)
## Contributing
Please follow our coding standards when contributing.
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Update README with setup instructions" | Out-Null
# Commit 9: Update configuration on main
$configContent = @"
# config.py - Configuration settings
APP_NAME = "My Project"
VERSION = "1.0.0"
DEBUG = False
DATABASE_PATH = "./data/app.db"
LOG_LEVEL = "INFO"
"@
Set-Content -Path "config.py" -Value $configContent
git add .
git commit -m "Add database path to config" | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
Write-Host "`nScenario: You have two divergent branches!" -ForegroundColor Yellow
Write-Host " - main: Has 5 commits (config, database utils, README updates)" -ForegroundColor White
Write-Host " - feature-login: Has 4 commits (login, validation, session)" -ForegroundColor White
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. cd challenge" -ForegroundColor White
Write-Host " 2. View the branch structure: git log --oneline --graph --all" -ForegroundColor White
Write-Host " 3. Merge feature-login into main: git merge feature-login" -ForegroundColor White
Write-Host " 4. View the merge result: git log --oneline --graph --all" -ForegroundColor White
Write-Host " 5. Run '..\verify.ps1' to check your solution" -ForegroundColor White
Write-Host ""