feat: initial branching+merge+conflicts module merge

This commit is contained in:
Bjarke Sporring
2026-01-11 14:50:10 +01:00
parent 0a9f7c1842
commit a0a9a5a0d9
32 changed files with 1696 additions and 1727 deletions

View File

@@ -0,0 +1,216 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Resets the challenge environment to a specific checkpoint.
.DESCRIPTION
This script allows you to jump to any checkpoint in the module,
resetting your repository to that state. Useful for skipping ahead,
starting over, or practicing specific sections.
.PARAMETER Checkpoint
The checkpoint to reset to: start, merge, or merge-conflict.
If not specified, displays help information.
.EXAMPLE
.\reset.ps1
Shows available checkpoints and current status.
.EXAMPLE
.\reset.ps1 start
Resets to the beginning (branching basics section).
.EXAMPLE
.\reset.ps1 merge
Jumps to the merging section (feature-login branch already exists).
.EXAMPLE
.\reset.ps1 merge-conflict
Jumps to the conflict resolution section (merge already complete).
#>
param(
[ValidateSet('start', 'merge', 'merge-conflict', '')]
[string]$Checkpoint = ''
)
# Checkpoint to tag mapping
$checkpointTags = @{
'start' = 'checkpoint-start'
'merge' = 'checkpoint-merge'
'merge-conflict' = 'checkpoint-merge-conflict'
}
# Checkpoint descriptions
$checkpointDescriptions = @{
'start' = 'Branching Basics - Create and work with feature branches'
'merge' = 'Merging Branches - Merge feature-login into main'
'merge-conflict' = 'Resolving Conflicts - Fix merge conflicts in config.json'
}
# ============================================================================
# Display help if no checkpoint specified
# ============================================================================
if ($Checkpoint -eq '') {
Write-Host "`n=== Module 03: Branching and Merging - Checkpoints ===" -ForegroundColor Cyan
Write-Host "`nAvailable checkpoints:" -ForegroundColor White
Write-Host ""
foreach ($key in @('start', 'merge', 'merge-conflict')) {
$desc = $checkpointDescriptions[$key]
Write-Host " $key" -ForegroundColor Green -NoNewline
Write-Host " - $desc" -ForegroundColor White
}
Write-Host "`nUsage:" -ForegroundColor Cyan
Write-Host " .\reset.ps1 <checkpoint>" -ForegroundColor White
Write-Host ""
Write-Host "Examples:" -ForegroundColor Cyan
Write-Host " .\reset.ps1 start # Start from the beginning" -ForegroundColor White
Write-Host " .\reset.ps1 merge # Jump to merging section" -ForegroundColor White
Write-Host " .\reset.ps1 merge-conflict # Jump to conflict resolution" -ForegroundColor White
Write-Host ""
# Try to detect current checkpoint
if (Test-Path "challenge/.git") {
Push-Location "challenge"
$currentBranch = git branch --show-current 2>$null
$currentCommit = git rev-parse HEAD 2>$null
# Check which checkpoint we're at
$currentCheckpoint = $null
foreach ($cp in @('start', 'merge', 'merge-conflict')) {
$tagCommit = git rev-parse $checkpointTags[$cp] 2>$null
if ($currentCommit -eq $tagCommit) {
$currentCheckpoint = $cp
break
}
}
if ($currentCheckpoint) {
Write-Host "Current checkpoint: " -ForegroundColor Yellow -NoNewline
Write-Host "$currentCheckpoint" -ForegroundColor Green -NoNewline
Write-Host " (on branch $currentBranch)" -ForegroundColor Yellow
} else {
Write-Host "Current status: " -ForegroundColor Yellow -NoNewline
Write-Host "In progress (on branch $currentBranch)" -ForegroundColor White
}
Pop-Location
}
Write-Host ""
exit 0
}
# ============================================================================
# Validate challenge directory exists
# ============================================================================
if (-not (Test-Path "challenge")) {
Write-Host "[ERROR] Challenge directory not found." -ForegroundColor Red
Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
exit 1
}
if (-not (Test-Path "challenge/.git")) {
Write-Host "[ERROR] No git repository found in challenge directory." -ForegroundColor Red
Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
exit 1
}
# Navigate to challenge directory
Push-Location "challenge"
# ============================================================================
# Verify the checkpoint tag exists
# ============================================================================
$targetTag = $checkpointTags[$Checkpoint]
$tagExists = git tag -l $targetTag
if (-not $tagExists) {
Write-Host "[ERROR] Checkpoint tag '$targetTag' not found." -ForegroundColor Red
Write-Host "Run ..\setup.ps1 to recreate the challenge environment." -ForegroundColor Yellow
Pop-Location
exit 1
}
# ============================================================================
# Check for uncommitted changes
# ============================================================================
$statusOutput = git status --porcelain 2>$null
if ($statusOutput) {
Write-Host "`n[WARNING] You have uncommitted changes!" -ForegroundColor Yellow
Write-Host "The following changes will be lost:" -ForegroundColor Yellow
Write-Host ""
git status --short
Write-Host ""
$response = Read-Host "Continue and discard all changes? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
Write-Host "`nReset cancelled." -ForegroundColor Cyan
Pop-Location
exit 0
}
}
# ============================================================================
# Reset to checkpoint
# ============================================================================
Write-Host "`nResetting to checkpoint: $Checkpoint" -ForegroundColor Cyan
Write-Host "Description: $($checkpointDescriptions[$Checkpoint])" -ForegroundColor White
Write-Host ""
try {
# Reset to the checkpoint tag
git reset --hard $targetTag 2>&1 | Out-Null
# Clean untracked files
git clean -fd 2>&1 | Out-Null
# Ensure we're on main branch
$currentBranch = git branch --show-current
if ($currentBranch -ne 'main') {
git switch main 2>&1 | Out-Null
git reset --hard $targetTag 2>&1 | Out-Null
}
Write-Host "[SUCCESS] Reset to checkpoint '$Checkpoint' complete!" -ForegroundColor Green
Write-Host ""
# Show what to do next
switch ($Checkpoint) {
'start' {
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Create a new branch: git switch -c feature-login" -ForegroundColor White
Write-Host " 2. Create login.py and make 2+ commits" -ForegroundColor White
Write-Host " 3. Verify: ..\verify.ps1 start" -ForegroundColor White
}
'merge' {
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. View branch structure: git log --oneline --graph --all" -ForegroundColor White
Write-Host " 2. Merge feature-login: git merge feature-login" -ForegroundColor White
Write-Host " 3. Verify: ..\verify.ps1 merge" -ForegroundColor White
}
'merge-conflict' {
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Attempt merge: git merge update-config" -ForegroundColor White
Write-Host " 2. Resolve conflicts in config.json" -ForegroundColor White
Write-Host " 3. Complete merge: git add config.json && git commit" -ForegroundColor White
Write-Host " 4. Verify: ..\verify.ps1 merge-conflict" -ForegroundColor White
}
}
Write-Host ""
Write-Host "View current state: git log --oneline --graph --all" -ForegroundColor Cyan
Write-Host ""
} catch {
Write-Host "[ERROR] Failed to reset to checkpoint." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Pop-Location
exit 1
}
Pop-Location
exit 0