Files
git-workshop/01-essentials/03-branching-and-merging/setup.ps1
2026-01-15 12:51:43 +01:00

289 lines
8.0 KiB
PowerShell
Executable File

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 03 challenge environment for branching and merging.
.DESCRIPTION
This script creates a challenge directory with a Git repository containing
a realistic project history with multiple merged branches. Students will see
what branching and merging looks like in practice, then create their own
branches to experiment.
#>
Write-Host "`n=== Setting up Module 03: Branching and Merging ===" -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"
# ============================================================================
# Create a realistic project history with multiple merged branches
# ============================================================================
Write-Host "Creating project history with multiple branches..." -ForegroundColor Cyan
# Initial commits on main
$readmeContent = @"
# My Application
A sample application for learning Git branching and merging.
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Initial commit" | Out-Null
$mainContent = @"
# main.py - Main application file
def main():
print("Welcome to the Application!")
if __name__ == "__main__":
main()
"@
Set-Content -Path "main.py" -Value $mainContent
git add .
git commit -m "Add main application file" | Out-Null
# ============================================================================
# Branch 1: feature-login (will be merged)
# ============================================================================
Write-Host "Creating feature-login branch..." -ForegroundColor Cyan
git switch -c feature-login | Out-Null
$loginContent = @"
# login.py - User authentication
def login(username, password):
"""Authenticate a user."""
print(f"Logging in user: {username}")
return True
"@
Set-Content -Path "login.py" -Value $loginContent
git add .
git commit -m "Add login module" | Out-Null
$loginContent = @"
# login.py - User authentication
def validate_password(password):
"""Validate password strength."""
return len(password) >= 8
def login(username, password):
"""Authenticate a user."""
if not validate_password(password):
print("Password too weak!")
return False
print(f"Logging in user: {username}")
return True
"@
Set-Content -Path "login.py" -Value $loginContent
git add .
git commit -m "Add password validation" | Out-Null
# Switch back to main and make more commits
git switch main | Out-Null
$appContent = @"
# app.py - Application entry point
from main import main
def run():
"""Run the application."""
print("Starting application...")
main()
if __name__ == "__main__":
run()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add app.py entry point" | Out-Null
# Merge feature-login into main
Write-Host "Merging feature-login into main..." -ForegroundColor Green
git merge feature-login --no-edit | Out-Null
# ============================================================================
# Branch 2: feature-api (will be merged)
# ============================================================================
Write-Host "Creating feature-api branch..." -ForegroundColor Cyan
git switch -c feature-api | Out-Null
$apiContent = @"
# api.py - API endpoints
def get_data():
"""Retrieve data from API."""
return {"status": "ok", "data": []}
def post_data(data):
"""Send data to API."""
print(f"Posting data: {data}")
return {"status": "ok"}
"@
Set-Content -Path "api.py" -Value $apiContent
git add .
git commit -m "Add API module" | Out-Null
$apiContent = @"
# api.py - API endpoints
def get_data():
"""Retrieve data from API."""
return {"status": "ok", "data": []}
def post_data(data):
"""Send data to API."""
print(f"Posting data: {data}")
return {"status": "ok"}
def delete_data(id):
"""Delete data by ID."""
print(f"Deleting data: {id}")
return {"status": "ok"}
"@
Set-Content -Path "api.py" -Value $apiContent
git add .
git commit -m "Add delete endpoint to API" | Out-Null
# Switch back to main and add documentation
git switch main | Out-Null
$readmeContent = @"
# My Application
A sample application for learning Git branching and merging.
## Features
- User authentication
- Main application logic
## Setup
Run: python app.py
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Update README with setup instructions" | Out-Null
# Merge feature-api into main
Write-Host "Merging feature-api into main..." -ForegroundColor Green
git merge feature-api --no-edit | Out-Null
# ============================================================================
# Branch 3: feature-database (will be merged)
# ============================================================================
Write-Host "Creating feature-database branch..." -ForegroundColor Cyan
git switch -c feature-database | Out-Null
$dbContent = @"
# database.py - Database operations
class Database:
def __init__(self):
self.connection = None
def connect(self):
"""Connect to database."""
print("Connecting to database...")
self.connection = True
def query(self, sql):
"""Execute SQL query."""
print(f"Executing: {sql}")
return []
"@
Set-Content -Path "database.py" -Value $dbContent
git add .
git commit -m "Add database module" | Out-Null
$dbContent = @"
# database.py - Database operations
class Database:
def __init__(self):
self.connection = None
def connect(self):
"""Connect to database."""
print("Connecting to database...")
self.connection = True
def disconnect(self):
"""Disconnect from database."""
print("Disconnecting from database...")
self.connection = None
def query(self, sql):
"""Execute SQL query."""
print(f"Executing: {sql}")
return []
"@
Set-Content -Path "database.py" -Value $dbContent
git add .
git commit -m "Add disconnect method" | Out-Null
# Switch to main and merge
git switch main | Out-Null
Write-Host "Merging feature-database into main..." -ForegroundColor Green
git merge feature-database --no-edit | Out-Null
# Final update on main
$readmeContent = @"
# My Application
A sample application for learning Git branching and merging.
## Features
- User authentication
- API endpoints
- Database operations
- Main application logic
## Setup
Run: python app.py
## Requirements
- Python 3.6+
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Update README with all features" | 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 "`nThe repository contains a realistic project history:" -ForegroundColor Yellow
Write-Host " - Multiple feature branches (login, api, database)" -ForegroundColor White
Write-Host " - All branches have been merged into main" -ForegroundColor White
Write-Host " - View the history: git log --oneline --graph --all" -ForegroundColor White
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. Read the README.md for detailed instructions" -ForegroundColor White
Write-Host " 2. cd challenge" -ForegroundColor White
Write-Host " 3. Explore the repository history: git log --oneline --graph --all" -ForegroundColor White
Write-Host " 4. Create your own branches and practice merging!" -ForegroundColor White
Write-Host ""