Files
git-workshop/02_advanced/01-rebasing/verify.ps1
2026-01-07 23:46:32 +01:00

155 lines
5.7 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the rebasing challenge solution.
.DESCRIPTION
Checks that the user successfully rebased the feature branch onto main,
resulting in a clean, linear history.
#>
Set-Location "challenge" -ErrorAction SilentlyContinue
# Check if challenge directory exists
if (-not (Test-Path "../verify.ps1")) {
Write-Host "Error: Please run this script from the module directory" -ForegroundColor Red
exit 1
}
if (-not (Test-Path ".")) {
Write-Host "Error: Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
Set-Location ..
exit 1
}
Write-Host "Verifying your solution..." -ForegroundColor Cyan
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] No git repository found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check current branch
$currentBranch = git branch --show-current 2>$null
if ($currentBranch -ne "feature") {
Write-Host "[FAIL] You should be on the 'feature' branch." -ForegroundColor Red
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
Write-Host "Hint: Use 'git checkout feature' to switch to feature branch" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check if there's an ongoing rebase
if (Test-Path ".git/rebase-merge") {
Write-Host "[FAIL] Rebase is not complete. There may be unresolved conflicts." -ForegroundColor Red
Write-Host "Hint: Resolve any conflicts, then use:" -ForegroundColor Yellow
Write-Host " git add <file>" -ForegroundColor White
Write-Host " git rebase --continue" -ForegroundColor White
Write-Host "Or abort with: git rebase --abort" -ForegroundColor White
Set-Location ..
exit 1
}
# Get all commits on feature branch
$featureCommits = git log --oneline feature 2>$null
if (-not $featureCommits) {
Write-Host "[FAIL] No commits found on feature branch." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check commit count (should be 5: 1 initial + 2 main + 2 feature)
$commitCount = (git rev-list --count feature 2>$null)
if ($commitCount -ne 5) {
Write-Host "[FAIL] Expected 5 commits on feature branch, found $commitCount" -ForegroundColor Red
Write-Host "Hint: The rebased feature branch should contain:" -ForegroundColor Yellow
Write-Host " - 1 initial commit" -ForegroundColor White
Write-Host " - 2 commits from main" -ForegroundColor White
Write-Host " - 2 commits from feature" -ForegroundColor White
Set-Location ..
exit 1
}
# Get commit messages in reverse chronological order (newest first)
$commits = git log --pretty=format:"%s" feature 2>$null
# Convert to array and reverse to get chronological order (oldest first)
$commitArray = $commits -split "`n"
[array]::Reverse($commitArray)
# Check that commits are in the expected order after rebase
$expectedOrder = @(
"Initial commit",
"Add installation instructions",
"Add configuration instructions",
"Add feature A",
"Add feature B"
)
$orderCorrect = $true
for ($i = 0; $i -lt $expectedOrder.Length; $i++) {
if ($commitArray[$i] -ne $expectedOrder[$i]) {
$orderCorrect = $false
break
}
}
if (-not $orderCorrect) {
Write-Host "[FAIL] Commit order is incorrect after rebase." -ForegroundColor Red
Write-Host "Expected order (oldest to newest):" -ForegroundColor Yellow
$expectedOrder | ForEach-Object { Write-Host " $_" -ForegroundColor White }
Write-Host "`nActual order:" -ForegroundColor Yellow
$commitArray | ForEach-Object { Write-Host " $_" -ForegroundColor White }
Write-Host "`nHint: The feature commits should come AFTER the main commits" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check that main branch still has only 3 commits (initial + 2 main commits)
$mainCommitCount = (git rev-list --count main 2>$null)
if ($mainCommitCount -ne 3) {
Write-Host "[FAIL] Main branch should have 3 commits, found $mainCommitCount" -ForegroundColor Red
Write-Host "Hint: You should rebase feature onto main, not the other way around" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check that there are no merge commits (parent count should be 1 for all commits)
$mergeCommits = git log --merges --oneline feature 2>$null
if ($mergeCommits) {
Write-Host "[FAIL] Found merge commits in history. Rebasing should create a linear history." -ForegroundColor Red
Write-Host "Hint: Use 'git rebase main' instead of 'git merge main'" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Verify that feature branch contains all commits from main
$mainCommits = git log --pretty=format:"%s" main 2>$null
$featureCommitMessages = git log --pretty=format:"%s" feature 2>$null
foreach ($mainCommit in ($mainCommits -split "`n")) {
if ($featureCommitMessages -notcontains $mainCommit) {
Write-Host "[FAIL] Feature branch is missing commits from main." -ForegroundColor Red
Write-Host "Missing: $mainCommit" -ForegroundColor Yellow
Set-Location ..
exit 1
}
}
# Success!
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "SUCCESS! Challenge completed!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou have successfully:" -ForegroundColor Cyan
Write-Host "- Rebased the feature branch onto main" -ForegroundColor White
Write-Host "- Created a clean, linear commit history" -ForegroundColor White
Write-Host "- Preserved all commits from both branches" -ForegroundColor White
Write-Host "`nYour feature branch now has a linear history!" -ForegroundColor Green
Write-Host "Run 'git log --oneline --graph --all' to see the result.`n" -ForegroundColor Cyan
Set-Location ..
exit 0