#!/usr/bin/env pwsh <# .SYNOPSIS Verifies the bisect challenge solution. .DESCRIPTION Checks that the user successfully used git bisect to find the commit that introduced the bug. #> 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 } # Make sure we're not in a bisect session $bisectHead = Test-Path ".git/BISECT_HEAD" if ($bisectHead) { Write-Host "[FAIL] You're still in a bisect session." -ForegroundColor Red Write-Host "Hint: End the bisect with: git bisect reset" -ForegroundColor Yellow Set-Location .. exit 1 } # Check if bug-commit.txt exists if (-not (Test-Path "bug-commit.txt")) { Write-Host "[FAIL] bug-commit.txt not found." -ForegroundColor Red Write-Host "Hint: After finding the bad commit, create a file with its hash:" -ForegroundColor Yellow Write-Host " echo 'commit-hash' > bug-commit.txt" -ForegroundColor Yellow Set-Location .. exit 1 } # Read the commit hash from the file $userCommit = (Get-Content "bug-commit.txt" -Raw).Trim() if (-not $userCommit) { Write-Host "[FAIL] bug-commit.txt is empty." -ForegroundColor Red Set-Location .. exit 1 } # Get all commit hashes $allCommits = git log --pretty=format:"%H" --reverse 2>$null $commitArray = $allCommits -split "`n" # The bug was introduced in commit 6 (index 5 in 0-based array) # This is the "Refactor add function for clarity" commit $badCommitMessage = git log --pretty=format:"%s" --grep="Refactor add function" 2>$null $actualBadCommit = git log --pretty=format:"%H" --grep="Refactor add function" 2>$null if (-not $actualBadCommit) { Write-Host "[FAIL] Could not find the expected bad commit." -ForegroundColor Red Write-Host "Something may be wrong with the repository setup." -ForegroundColor Yellow Set-Location .. exit 1 } # Check if user found the correct commit if ($userCommit -ne $actualBadCommit) { # Maybe they provided a short hash if ($actualBadCommit -like "$userCommit*") { Write-Host "[PASS] Correct commit identified (using short hash)!" -ForegroundColor Green } else { Write-Host "[FAIL] Incorrect commit identified." -ForegroundColor Red Write-Host "You identified: $userCommit" -ForegroundColor Yellow Write-Host "Expected: $actualBadCommit" -ForegroundColor Yellow Write-Host "Expected commit message: 'Refactor add function for clarity'" -ForegroundColor Yellow Write-Host "`nHint: Use git bisect to find where the add function broke:" -ForegroundColor Yellow Write-Host " git bisect start" -ForegroundColor White Write-Host " git bisect bad" -ForegroundColor White Write-Host " git bisect good HEAD~10" -ForegroundColor White Write-Host " # Then test with: node test.py" -ForegroundColor White Write-Host " git bisect good # or bad" -ForegroundColor White Set-Location .. exit 1 } } else { Write-Host "[PASS] Correct commit identified!" -ForegroundColor Green } # Verify the commit actually has the bug git checkout $actualBadCommit 2>$null | Out-Null $calcContent = Get-Content "calculator.py" -Raw if ($calcContent -notmatch "add\(a, b\)[\s\S]*?return a - b") { Write-Host "[WARNING] The identified commit doesn't seem to have the expected bug." -ForegroundColor Yellow } # Return to latest commit git checkout $(git branch --show-current 2>$null) 2>$null | Out-Null if (-not $?) { git checkout main 2>$null | Out-Null } # 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 "- Used git bisect to perform a binary search" -ForegroundColor White Write-Host "- Tested commits systematically" -ForegroundColor White Write-Host "- Identified the exact commit that introduced the bug" -ForegroundColor White Write-Host "- Found: '$badCommitMessage'" -ForegroundColor White Write-Host "`nThe bug was in commit 6 out of 10 commits." -ForegroundColor Yellow Write-Host "Manual checking: up to 10 tests" -ForegroundColor Yellow Write-Host "With bisect: only ~4 tests needed!" -ForegroundColor Green Write-Host "`nYou now understand git bisect!" -ForegroundColor Green Write-Host "`nKey takeaway:" -ForegroundColor Yellow Write-Host "Bisect uses binary search to efficiently find bugs," -ForegroundColor White Write-Host "saving massive amounts of time in large codebases.`n" -ForegroundColor White Set-Location .. exit 0