refactor: use the new utils.ps1 script for operations
This commit is contained in:
@@ -9,168 +9,136 @@
|
||||
to the main branch without merging the experimental features.
|
||||
#>
|
||||
|
||||
Set-Location "challenge" -ErrorAction SilentlyContinue
|
||||
. "$PSScriptRoot\..\..\util.ps1"
|
||||
|
||||
# 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
|
||||
}
|
||||
$challengeRoot = "$PSScriptRoot\challenge"
|
||||
|
||||
if (-not (Test-Path ".")) {
|
||||
if (-not (Test-Path $challengeRoot)) {
|
||||
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 ..
|
||||
if (-not (Test-Path "$challengeRoot\.git")) {
|
||||
Write-Fail "No git repository found." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Detect the main branch name
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
if ($allBranches -contains "main") {
|
||||
$mainBranch = "main"
|
||||
} elseif ($allBranches -contains "master") {
|
||||
$mainBranch = "master"
|
||||
} else {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = $allBranches | Select-Object -First 1
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
}
|
||||
$mainBranch = Get-MainBranch
|
||||
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
|
||||
# Check current branch
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -ne $mainBranch) {
|
||||
Write-Host "[FAIL] You should be on the '$mainBranch' branch." -ForegroundColor Red
|
||||
Write-Fail "You should be on the '$mainBranch' branch." -ForegroundColor Red
|
||||
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git checkout $mainBranch' to switch to $mainBranch branch" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Hint "Use 'git checkout $mainBranch' to switch to $mainBranch branch" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if there's an ongoing cherry-pick
|
||||
if (Test-Path ".git/CHERRY_PICK_HEAD") {
|
||||
Write-Host "[FAIL] Cherry-pick is not complete. There may be unresolved conflicts." -ForegroundColor Red
|
||||
Write-Host "Hint: Resolve any conflicts, then use:" -ForegroundColor Yellow
|
||||
if (Test-Path "$challengeRoot\.git\CHERRY_PICK_HEAD") {
|
||||
Write-Fail "Cherry-pick is not complete. There may be unresolved conflicts." -ForegroundColor Red
|
||||
Write-Hint "Resolve any conflicts, then use:" -ForegroundColor Yellow
|
||||
Write-Host " git add <file>" -ForegroundColor White
|
||||
Write-Host " git cherry-pick --continue" -ForegroundColor White
|
||||
Write-Host "Or abort with: git cherry-pick --abort" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check commit count on main (should be 4: 2 initial + 2 cherry-picked)
|
||||
$mainCommitCount = (git rev-list --count $mainBranch 2>$null)
|
||||
if ($mainCommitCount -ne 4) {
|
||||
Write-Host "[FAIL] Expected 4 commits on $mainBranch branch, found $mainCommitCount" -ForegroundColor Red
|
||||
Write-Fail "Expected 4 commits on $mainBranch branch, found $mainCommitCount" -ForegroundColor Red
|
||||
if ($mainCommitCount -lt 4) {
|
||||
Write-Host "Hint: You should cherry-pick 2 bug fix commits to $mainBranch" -ForegroundColor Yellow
|
||||
Write-Hint "You should cherry-pick 2 bug fix commits to $mainBranch" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "Hint: You should cherry-pick ONLY the 2 bug fix commits, not all commits" -ForegroundColor Yellow
|
||||
Write-Hint "You should cherry-pick ONLY the 2 bug fix commits, not all commits" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host "`nExpected commits on ${mainBranch}:" -ForegroundColor Yellow
|
||||
Write-Host " 1. Initial app implementation" -ForegroundColor White
|
||||
Write-Host " 2. Add README" -ForegroundColor White
|
||||
Write-Host " 3. Fix security vulnerability in input validation (cherry-picked)" -ForegroundColor White
|
||||
Write-Host " 4. Fix performance issue with data caching (cherry-picked)" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check for merge commits (should be none - cherry-pick doesn't create merge commits)
|
||||
$mergeCommits = git log --merges --oneline $mainBranch 2>$null
|
||||
if ($mergeCommits) {
|
||||
Write-Host "[FAIL] Found merge commits on $mainBranch. You should use cherry-pick, not merge." -ForegroundColor Red
|
||||
Write-Host "Hint: Use 'git cherry-pick <commit-hash>' instead of 'git merge'" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Fail "Found merge commits on $mainBranch. You should use cherry-pick, not merge." -ForegroundColor Red
|
||||
Write-Hint "Use 'git cherry-pick <commit-hash>' instead of 'git merge'" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that security.py exists (from the security fix commit)
|
||||
if (-not (Test-Path "security.py")) {
|
||||
Write-Host "[FAIL] security.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: You need to cherry-pick the 'Fix security vulnerability' commit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
if (-not (Test-Path "$challengeRoot\security.py")) {
|
||||
Write-Fail "security.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Hint "You need to cherry-pick the 'Fix security vulnerability' commit" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that security.py has the security fix
|
||||
$securityContent = Get-Content "security.py" -Raw
|
||||
$securityContent = Get-Content "$challengeRoot\security.py" -Raw
|
||||
|
||||
if ($securityContent -notmatch "sanitize_input") {
|
||||
Write-Host "[FAIL] security.py is missing the sanitize_input function." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
Write-Fail "security.py is missing the sanitize_input function." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($securityContent -notmatch "validate_token") {
|
||||
Write-Host "[FAIL] security.py is missing the validate_token function." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
Write-Fail "security.py is missing the validate_token function." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that app.py exists
|
||||
if (-not (Test-Path "app.py")) {
|
||||
Write-Host "[FAIL] app.py not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
if (-not (Test-Path "$challengeRoot\app.py")) {
|
||||
Write-Fail "app.py not found." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that cache.py exists (from performance fix)
|
||||
if (-not (Test-Path "cache.py")) {
|
||||
Write-Host "[FAIL] cache.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: You need to cherry-pick the 'Fix performance issue' commit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
if (-not (Test-Path "$challengeRoot\cache.py")) {
|
||||
Write-Fail "cache.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Hint "You need to cherry-pick the 'Fix performance issue' commit" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that cache.py has the DataCache class
|
||||
$cacheContent = Get-Content "cache.py" -Raw
|
||||
$cacheContent = Get-Content "$challengeRoot\cache.py" -Raw
|
||||
|
||||
if ($cacheContent -notmatch "DataCache") {
|
||||
Write-Host "[FAIL] cache.py is missing the DataCache class." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
Write-Fail "cache.py is missing the DataCache class." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($cacheContent -notmatch "def get\(") {
|
||||
Write-Host "[FAIL] cache.py is missing the get method." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
Write-Fail "cache.py is missing the get method." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that app.py does NOT have experimental features
|
||||
$appContent = Get-Content "app.py" -Raw
|
||||
$appContent = Get-Content "$challengeRoot\app.py" -Raw
|
||||
|
||||
# Should NOT have experimental features
|
||||
if ($appContent -match "experimental_mode") {
|
||||
Write-Host "[FAIL] app.py contains experimental features (experimental_mode)." -ForegroundColor Red
|
||||
Write-Host "Hint: You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
Write-Fail "app.py contains experimental features (experimental_mode)." -ForegroundColor Red
|
||||
Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
Write-Host " The experimental feature commits should stay on development branch only" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($appContent -match "beta_features") {
|
||||
Write-Host "[FAIL] app.py contains experimental features (beta_features)." -ForegroundColor Red
|
||||
Write-Host "Hint: You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Fail "app.py contains experimental features (beta_features)." -ForegroundColor Red
|
||||
Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($appContent -match "enable_experimental_features") {
|
||||
Write-Host "[FAIL] app.py contains experimental features (enable_experimental_features)." -ForegroundColor Red
|
||||
Write-Host "Hint: You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Fail "app.py contains experimental features (enable_experimental_features)." -ForegroundColor Red
|
||||
Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -191,25 +159,22 @@ foreach ($commit in $commitArray) {
|
||||
}
|
||||
|
||||
if (-not $hasSecurityFix) {
|
||||
Write-Host "[FAIL] Security fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Cherry-pick the 'Fix security vulnerability' commit from development" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Fail "Security fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Hint "Cherry-pick the 'Fix security vulnerability' commit from development" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not $hasPerformanceFix) {
|
||||
Write-Host "[FAIL] Performance fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Cherry-pick the 'Fix performance issue' commit from development" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
Write-Fail "Performance fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Hint "Cherry-pick the 'Fix performance issue' commit from development" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verify development branch still has all commits
|
||||
$devCommitCount = (git rev-list --count development 2>$null)
|
||||
if ($devCommitCount -ne 6) {
|
||||
Write-Host "[FAIL] Development branch should still have 6 commits." -ForegroundColor Red
|
||||
Write-Fail "Development branch should still have 6 commits." -ForegroundColor Red
|
||||
Write-Host "Found: $devCommitCount commits" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -226,5 +191,4 @@ Write-Host "`nPerfect use of cherry-pick!" -ForegroundColor Green
|
||||
Write-Host "You selectively applied critical fixes without merging unfinished features.`n" -ForegroundColor Green
|
||||
Write-Host "Try 'git log --oneline --graph --all' to see both branches." -ForegroundColor Cyan
|
||||
|
||||
Set-Location ..
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user