Files
git-workshop/01-essentials/04-merge-conflict/verify.ps1
2026-01-15 14:31:00 +01:00

233 lines
9.1 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 04 challenge solution.
.DESCRIPTION
This script checks that you've successfully resolved the merge conflict by:
- Merging both branches into main
- Resolving the conflict in config.json
- Keeping both timeout and debug settings
- Ensuring valid JSON syntax
#>
$script:allChecksPassed = $true
# ============================================================================
# Helper Functions
# ============================================================================
function Write-Pass {
param([string]$Message)
Write-Host "[PASS] $Message" -ForegroundColor Green
}
function Write-Fail {
param([string]$Message)
Write-Host "[FAIL] $Message" -ForegroundColor Red
$script:allChecksPassed = $false
}
function Write-Hint {
param([string]$Message)
Write-Host "[HINT] $Message" -ForegroundColor Yellow
}
function Write-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Cyan
}
# ============================================================================
# Check 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
}
Push-Location "challenge"
if (-not (Test-Path ".git")) {
Write-Host "[ERROR] Not a git repository." -ForegroundColor Red
Write-Host "Run ..\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
Pop-Location
exit 1
}
Write-Host "`n=== Verifying Module 04: Merge Conflicts ===" -ForegroundColor Cyan
# ============================================================================
# Detect the main branch name (could be main, master, etc.)
# ============================================================================
# Try to get the default branch from remote origin first
$mainBranch = git symbolic-ref refs/remotes/origin/HEAD 2>$null | Split-Path -Leaf
if (-not $mainBranch) {
# Fallback: try to detect from local branches
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
if ($allBranches -contains "main") {
$mainBranch = "main"
} elseif ($allBranches -contains "master") {
$mainBranch = "master"
} else {
# Get the default branch from git config
$mainBranch = git config --get init.defaultBranch
if (-not $mainBranch) {
# Ultimate fallback: use the first branch
$mainBranch = $allBranches | Select-Object -First 1
if (-not $mainBranch) { $mainBranch = "main" }
}
}
}
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
# ============================================================================
# Check current branch
# ============================================================================
$currentBranch = git branch --show-current 2>$null
if ($currentBranch -eq $mainBranch) {
Write-Pass "Currently on $mainBranch branch"
} else {
Write-Fail "Should be on $mainBranch branch (currently on: $currentBranch)"
Write-Hint "Switch to $mainBranch with: git switch $mainBranch"
}
# ============================================================================
# Check that merge is not in progress
# ============================================================================
if (Test-Path ".git/MERGE_HEAD") {
Write-Fail "Merge is still in progress (conflicts not resolved)"
Write-Hint "Resolve conflicts in config.json, then: git add config.json && git commit"
Pop-Location
exit 1
} else {
Write-Pass "No merge in progress (conflicts resolved)"
}
# ============================================================================
# Check if config.json exists
# ============================================================================
if (-not (Test-Path "config.json")) {
Write-Fail "File 'config.json' not found"
Write-Hint "The config.json file should exist"
Pop-Location
exit 1
}
# ============================================================================
# Verify config.json is valid JSON
# ============================================================================
try {
$configContent = Get-Content "config.json" -Raw
$config = $configContent | ConvertFrom-Json -ErrorAction Stop
Write-Pass "File 'config.json' is valid JSON"
} catch {
Write-Fail "File 'config.json' is not valid JSON"
Write-Hint "Make sure you removed all conflict markers (<<<<<<<, =======, >>>>>>>)"
Write-Hint "Check for missing commas or brackets"
Pop-Location
exit 1
}
# ============================================================================
# Check for conflict markers
# ============================================================================
if ($configContent -match '<<<<<<<|=======|>>>>>>>') {
Write-Fail "Conflict markers still present in config.json"
Write-Hint "Remove all conflict markers (<<<<<<<, =======, >>>>>>>)"
Pop-Location
exit 1
} else {
Write-Pass "No conflict markers in config.json"
}
# ============================================================================
# Verify both settings are present (timeout and debug)
# ============================================================================
if ($config.app.timeout -eq 5000) {
Write-Pass "Timeout setting preserved (5000)"
} else {
Write-Fail "Timeout setting missing or incorrect"
Write-Hint "Keep the timeout: 5000 setting from add-timeout branch"
}
if ($config.app.debug -eq $true) {
Write-Pass "Debug setting preserved (true)"
} else {
Write-Fail "Debug setting missing or incorrect"
Write-Hint "Keep the debug: true setting from add-debug branch"
}
# ============================================================================
# Verify both branches were merged
# ============================================================================
$addTimeoutMerged = git log --oneline --grep="add-timeout" 2>$null | Select-String "Merge"
$addDebugMerged = git log --oneline --grep="add-debug" 2>$null | Select-String "Merge"
if ($addTimeoutMerged) {
Write-Pass "add-timeout branch has been merged"
} else {
# Check if it was a fast-forward merge (commits exist but no merge commit)
$timeoutCommit = git log --oneline --grep="Add timeout configuration" 2>$null
if ($timeoutCommit) {
Write-Pass "add-timeout branch changes are in main"
} else {
Write-Fail "add-timeout branch not merged"
Write-Hint "Merge with: git merge add-timeout"
}
}
if ($addDebugMerged) {
Write-Pass "add-debug branch has been merged"
} else {
Write-Fail "add-debug branch not merged"
Write-Hint "Merge with: git merge add-debug"
}
# ============================================================================
# Check commit count (should have both merges)
# ============================================================================
$totalCommits = [int](git rev-list --count HEAD 2>$null)
if ($totalCommits -ge 5) {
Write-Pass "Repository has $totalCommits commits (all merges complete)"
} else {
Write-Info "Repository has $totalCommits commits"
Write-Hint "Make sure both branches are merged"
}
Pop-Location
# ============================================================================
# Final summary
# ============================================================================
Write-Host ""
if ($script:allChecksPassed) {
Write-Host "=========================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Green
Write-Host "`nYou've successfully:" -ForegroundColor Cyan
Write-Host " ✓ Discovered changes using git diff" -ForegroundColor White
Write-Host " ✓ Merged the first branch" -ForegroundColor White
Write-Host " ✓ Encountered a merge conflict" -ForegroundColor White
Write-Host " ✓ Resolved the conflict by keeping both changes" -ForegroundColor White
Write-Host " ✓ Completed the merge" -ForegroundColor White
Write-Host "`nYou're now ready to handle merge conflicts in real projects!" -ForegroundColor Green
Write-Host ""
exit 0
} else {
Write-Host "[SUMMARY] Some checks failed. Review the hints above." -ForegroundColor Red
Write-Host ""
Write-Host "Quick guide:" -ForegroundColor Cyan
Write-Host " 1. Make sure you're on $mainBranch : git switch $mainBranch" -ForegroundColor White
Write-Host " 2. Merge first branch: git merge add-timeout" -ForegroundColor White
Write-Host " 3. Merge second branch: git merge add-debug" -ForegroundColor White
Write-Host " 4. Resolve conflict: edit config.json, remove markers, keep both settings" -ForegroundColor White
Write-Host " 5. Stage resolved file: git add config.json" -ForegroundColor White
Write-Host " 6. Complete merge: git commit" -ForegroundColor White
Write-Host " 7. Run this verify script again" -ForegroundColor White
Write-Host ""
exit 1
}