Files
2026-01-15 14:31:00 +01:00

139 lines
4.4 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 04 challenge environment for merge conflicts.
.DESCRIPTION
This script creates a challenge directory with a Git repository containing
two feature branches that have conflicting changes to the same file.
Students will learn to identify, understand, and resolve merge conflicts.
#>
Write-Host "`n=== Setting up Module 04: Merge Conflicts ===" -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"
# Detect the default branch name (could be main, master, etc.)
# First commit creates the branch, so we detect it after that
$mainBranch = git branch --show-current
if (-not $mainBranch) {
# Fallback: Get default branch name from git config
$mainBranch = git config --get init.defaultBranch
if (-not $mainBranch) {
# Ultimate fallback: use "main"
$mainBranch = "main"
}
}
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
# ============================================================================
# Create base project
# ============================================================================
Write-Host "Creating base project..." -ForegroundColor Cyan
# Initial commit with config file
$configContent = @"
{
"app": {
"name": "MyApp",
"version": "1.0.0",
"port": 3000
}
}
"@
Set-Content -Path "config.json" -Value $configContent
git add .
git commit -m "Initial commit with config" | Out-Null
# Add README
$readmeContent = @"
# My Application
A simple application for learning merge conflicts.
## Configuration
Edit `config.json` to configure the application.
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Add README" | Out-Null
# ============================================================================
# Branch 1: add-timeout (adds timeout setting)
# ============================================================================
Write-Host "Creating add-timeout branch..." -ForegroundColor Cyan
git switch -c add-timeout | Out-Null
$timeoutConfig = @"
{
"app": {
"name": "MyApp",
"version": "1.0.0",
"port": 3000,
"timeout": 5000
}
}
"@
Set-Content -Path "config.json" -Value $timeoutConfig
git add .
git commit -m "Add timeout configuration" | Out-Null
# ============================================================================
# Branch 2: add-debug (adds debug setting - CONFLICTS with timeout!)
# ============================================================================
Write-Host "Creating add-debug branch..." -ForegroundColor Cyan
git switch $mainBranch | Out-Null
git switch -c add-debug | Out-Null
$debugConfig = @"
{
"app": {
"name": "MyApp",
"version": "1.0.0",
"port": 3000,
"debug": true
}
}
"@
Set-Content -Path "config.json" -Value $debugConfig
git add .
git commit -m "Add debug mode configuration" | Out-Null
# Switch back to main branch
git switch $mainBranch | 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:" -ForegroundColor Yellow
Write-Host " - $mainBranch branch: base configuration" -ForegroundColor White
Write-Host " - add-timeout branch: adds timeout setting" -ForegroundColor White
Write-Host " - add-debug branch: adds debug setting" -ForegroundColor White
Write-Host "`nBoth branches modify the same part of config.json!" -ForegroundColor Red
Write-Host "This will cause a merge conflict when you try to merge both." -ForegroundColor Red
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. Follow the guide to discover and resolve the conflict" -ForegroundColor White
Write-Host ""