diff --git a/01-essentials/04-merge-conflict/setup.ps1 b/01-essentials/04-merge-conflict/setup.ps1 index 86fa42d..5775cbd 100644 --- a/01-essentials/04-merge-conflict/setup.ps1 +++ b/01-essentials/04-merge-conflict/setup.ps1 @@ -30,6 +30,19 @@ git init | Out-Null git config user.name "Workshop Student" git config user.email "student@example.com" +# Detect the default branch name (could be main, master, etc.) +# First commit creates the branch, so we detect it after that +$mainBranch = git branch --show-current +if (-not $mainBranch) { + # Fallback: Get default branch name from git config + $mainBranch = git config --get init.defaultBranch + if (-not $mainBranch) { + # Ultimate fallback: use "main" + $mainBranch = "main" + } +} +Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow + # ============================================================================ # Create base project # ============================================================================ @@ -87,7 +100,7 @@ git commit -m "Add timeout configuration" | Out-Null # Branch 2: add-debug (adds debug setting - CONFLICTS with timeout!) # ============================================================================ Write-Host "Creating add-debug branch..." -ForegroundColor Cyan -git switch main | Out-Null +git switch $mainBranch | Out-Null git switch -c add-debug | Out-Null $debugConfig = @" @@ -104,8 +117,8 @@ Set-Content -Path "config.json" -Value $debugConfig git add . git commit -m "Add debug mode configuration" | Out-Null -# Switch back to main -git switch main | Out-Null +# Switch back to main branch +git switch $mainBranch | Out-Null # Return to module directory Set-Location .. @@ -113,7 +126,7 @@ Set-Location .. Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan Write-Host "`nThe repository contains:" -ForegroundColor Yellow -Write-Host " - main branch: base configuration" -ForegroundColor White +Write-Host " - $mainBranch branch: base configuration" -ForegroundColor White Write-Host " - add-timeout branch: adds timeout setting" -ForegroundColor White Write-Host " - add-debug branch: adds debug setting" -ForegroundColor White Write-Host "`nBoth branches modify the same part of config.json!" -ForegroundColor Red diff --git a/01-essentials/04-merge-conflict/verify.ps1 b/01-essentials/04-merge-conflict/verify.ps1 index 8a04114..739442e 100644 --- a/01-essentials/04-merge-conflict/verify.ps1 +++ b/01-essentials/04-merge-conflict/verify.ps1 @@ -59,15 +59,39 @@ if (-not (Test-Path ".git")) { 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 "main") { - Write-Pass "Currently on main branch" +if ($currentBranch -eq $mainBranch) { + Write-Pass "Currently on $mainBranch branch" } else { - Write-Fail "Should be on main branch (currently on: $currentBranch)" - Write-Hint "Switch to main with: git switch main" + Write-Fail "Should be on $mainBranch branch (currently on: $currentBranch)" + Write-Hint "Switch to $mainBranch with: git switch $mainBranch" } # ============================================================================ @@ -196,7 +220,7 @@ if ($script:allChecksPassed) { 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 main: git switch main" -ForegroundColor White + 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 diff --git a/01-essentials/05-cherry-pick/setup.ps1 b/01-essentials/05-cherry-pick/setup.ps1 index 879936f..5dfa6df 100644 --- a/01-essentials/05-cherry-pick/setup.ps1 +++ b/01-essentials/05-cherry-pick/setup.ps1 @@ -26,7 +26,8 @@ git init | Out-Null git config user.name "Workshop User" | Out-Null git config user.email "user@workshop.local" | Out-Null -# Create initial commits on main branch +# Detect the default branch name (could be main, master, etc.) +# First commit creates the branch, so we detect it after the first commit below $app = @" class App: def __init__(self): @@ -40,6 +41,14 @@ Set-Content -Path "app.py" -Value $app git add app.py git commit -m "Initial app implementation" | Out-Null +# Detect the main branch name after first commit +$mainBranch = git branch --show-current +if (-not $mainBranch) { + $mainBranch = git config --get init.defaultBranch + if (-not $mainBranch) { $mainBranch = "main" } +} +Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow + $readme = @" # Application @@ -164,12 +173,13 @@ Write-Host "========================================" -ForegroundColor Green Write-Host "`nYou are on the 'development' branch with multiple commits:" -ForegroundColor Cyan Write-Host "- Experimental features (not ready for production)" -ForegroundColor Yellow Write-Host "- Critical bug fixes (needed in production NOW)" -ForegroundColor Green +Write-Host "`nDetected main branch: $mainBranch" -ForegroundColor Cyan Write-Host "`nYour task:" -ForegroundColor Yellow Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White Write-Host "2. View the development branch commits: git log --oneline" -ForegroundColor White Write-Host "3. Identify which commits are bug fixes (look for 'Fix' in messages)" -ForegroundColor White -Write-Host "4. Switch to main branch: git checkout main" -ForegroundColor White -Write-Host "5. Cherry-pick ONLY the bug fix commits to main" -ForegroundColor White -Write-Host "6. Do NOT bring the experimental features to main" -ForegroundColor White +Write-Host "4. Switch to $mainBranch branch: git checkout $mainBranch" -ForegroundColor White +Write-Host "5. Cherry-pick ONLY the bug fix commits to $mainBranch" -ForegroundColor White +Write-Host "6. Do NOT bring the experimental features to $mainBranch" -ForegroundColor White Write-Host "`nHint: Look for commits mentioning 'security' and 'performance' fixes" -ForegroundColor Cyan Write-Host "Run '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan diff --git a/01-essentials/05-cherry-pick/verify.ps1 b/01-essentials/05-cherry-pick/verify.ps1 index 2512222..7449dbc 100644 --- a/01-essentials/05-cherry-pick/verify.ps1 +++ b/01-essentials/05-cherry-pick/verify.ps1 @@ -32,12 +32,27 @@ if (-not (Test-Path ".git")) { 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" } + } +} +Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan + # Check current branch $currentBranch = git branch --show-current 2>$null -if ($currentBranch -ne "main") { - Write-Host "[FAIL] You should be on the 'main' branch." -ForegroundColor Red +if ($currentBranch -ne $mainBranch) { + Write-Host "[FAIL] You should be on the '$mainBranch' branch." -ForegroundColor Red Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow - Write-Host "Hint: Use 'git checkout main' to switch to main branch" -ForegroundColor Yellow + Write-Host "Hint: Use 'git checkout $mainBranch' to switch to $mainBranch branch" -ForegroundColor Yellow Set-Location .. exit 1 } @@ -54,15 +69,15 @@ if (Test-Path ".git/CHERRY_PICK_HEAD") { } # Check commit count on main (should be 4: 2 initial + 2 cherry-picked) -$mainCommitCount = (git rev-list --count main 2>$null) +$mainCommitCount = (git rev-list --count $mainBranch 2>$null) if ($mainCommitCount -ne 4) { - Write-Host "[FAIL] Expected 4 commits on main branch, found $mainCommitCount" -ForegroundColor Red + Write-Host "[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 main" -ForegroundColor Yellow + Write-Host "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-Host "`nExpected commits on main:" -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 @@ -72,9 +87,9 @@ if ($mainCommitCount -ne 4) { } # Check for merge commits (should be none - cherry-pick doesn't create merge commits) -$mergeCommits = git log --merges --oneline main 2>$null +$mergeCommits = git log --merges --oneline $mainBranch 2>$null if ($mergeCommits) { - Write-Host "[FAIL] Found merge commits on main. You should use cherry-pick, not merge." -ForegroundColor Red + Write-Host "[FAIL] Found merge commits on $mainBranch. You should use cherry-pick, not merge." -ForegroundColor Red Write-Host "Hint: Use 'git cherry-pick ' instead of 'git merge'" -ForegroundColor Yellow Set-Location .. exit 1 @@ -82,7 +97,7 @@ if ($mergeCommits) { # Check that security.py exists (from the security fix commit) if (-not (Test-Path "security.py")) { - Write-Host "[FAIL] security.py not found on main branch." -ForegroundColor Red + 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 .. exit 1 @@ -151,7 +166,7 @@ if ($appContent -match "enable_experimental_features") { } # Check commit messages to verify cherry-picks -$commits = git log --pretty=format:"%s" main 2>$null +$commits = git log --pretty=format:"%s" $mainBranch 2>$null $commitArray = $commits -split "`n" $hasSecurityFix = $false @@ -167,14 +182,14 @@ foreach ($commit in $commitArray) { } if (-not $hasSecurityFix) { - Write-Host "[FAIL] Security fix commit not found on main branch." -ForegroundColor Red + 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 .. exit 1 } if (-not $hasPerformanceFix) { - Write-Host "[FAIL] Performance fix commit not found on main branch." -ForegroundColor Red + 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 .. exit 1 @@ -194,8 +209,8 @@ 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 "- Cherry-picked the security vulnerability fix to main" -ForegroundColor White -Write-Host "- Cherry-picked the performance issue fix to main" -ForegroundColor White +Write-Host "- Cherry-picked the security vulnerability fix to $mainBranch" -ForegroundColor White +Write-Host "- Cherry-picked the performance issue fix to $mainBranch" -ForegroundColor White Write-Host "- Left experimental features on development branch only" -ForegroundColor White Write-Host "- Kept development branch intact with all commits" -ForegroundColor White Write-Host "`nPerfect use of cherry-pick!" -ForegroundColor Green diff --git a/01-essentials/06-revert/setup.ps1 b/01-essentials/06-revert/setup.ps1 index f3e3911..b481baa 100644 --- a/01-essentials/06-revert/setup.ps1 +++ b/01-essentials/06-revert/setup.ps1 @@ -32,6 +32,9 @@ git init | Out-Null git config user.name "Workshop Student" git config user.email "student@example.com" +# Detect the default branch name after first commit (created below) +# Will be detected after the initial commit in SCENARIO 1 + # ============================================================================ # SCENARIO 1: Regular Revert (Basic) # ============================================================================ @@ -53,6 +56,14 @@ Set-Content -Path "calculator.py" -Value $calcContent git add . git commit -m "Initial calculator implementation" | Out-Null +# Detect the main branch name after first commit +$mainBranch = git branch --show-current +if (-not $mainBranch) { + $mainBranch = git config --get init.defaultBranch + if (-not $mainBranch) { $mainBranch = "main" } +} +Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow + # Create regular-revert branch git switch -c regular-revert | Out-Null @@ -136,7 +147,7 @@ Write-Host "[CREATED] regular-revert branch with bad divide commit" -ForegroundC Write-Host "`nScenario 2: Creating merge-revert scenario..." -ForegroundColor Cyan # Switch back to main -git switch main | Out-Null +git switch $mainBranch | Out-Null # Create merge-revert branch git switch -c merge-revert | Out-Null @@ -222,7 +233,7 @@ Write-Host "[CREATED] merge-revert branch with merge commit to revert" -Foregrou Write-Host "`nScenario 3: Creating multi-revert branch..." -ForegroundColor Cyan # Switch back to main -git switch main | Out-Null +git switch $mainBranch | Out-Null # Create multi-revert branch git switch -c multi-revert | Out-Null diff --git a/01-essentials/07-reset/setup.ps1 b/01-essentials/07-reset/setup.ps1 index 52ebf47..ccdaf38 100644 --- a/01-essentials/07-reset/setup.ps1 +++ b/01-essentials/07-reset/setup.ps1 @@ -33,6 +33,9 @@ git init | Out-Null git config user.name "Workshop Student" git config user.email "student@example.com" +# Detect the default branch name after first commit (created below) +# Will be detected after the initial commit + # ============================================================================ # Create initial commit (shared by all scenarios) # ============================================================================ @@ -45,6 +48,14 @@ Set-Content -Path "README.md" -Value $readmeContent git add . git commit -m "Initial commit" | Out-Null +# Detect the main branch name after first commit +$mainBranch = git branch --show-current +if (-not $mainBranch) { + $mainBranch = git config --get init.defaultBranch + if (-not $mainBranch) { $mainBranch = "main" } +} +Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow + # ============================================================================ # SCENARIO 1: Soft Reset (--soft) # ============================================================================ @@ -155,7 +166,7 @@ Write-Host "[CREATED] soft-reset branch with commit to reset --soft" -Foreground Write-Host "`nScenario 2: Creating mixed-reset branch..." -ForegroundColor Cyan # Switch back to initial commit and create mixed-reset branch -git switch main | Out-Null +git switch $mainBranch | Out-Null git switch -c mixed-reset | Out-Null # Build up scenario 2 commits @@ -256,7 +267,7 @@ Write-Host "[CREATED] mixed-reset branch with commits to reset --mixed" -Foregro Write-Host "`nScenario 3: Creating hard-reset branch..." -ForegroundColor Cyan # Switch back to main and create hard-reset branch -git switch main | Out-Null +git switch $mainBranch | Out-Null git switch -c hard-reset | Out-Null # Reset to basic state diff --git a/01-essentials/08-stash/setup.ps1 b/01-essentials/08-stash/setup.ps1 index 80481b3..c4f09d1 100644 --- a/01-essentials/08-stash/setup.ps1 +++ b/01-essentials/08-stash/setup.ps1 @@ -25,6 +25,9 @@ git init | Out-Null git config user.name "Workshop User" | Out-Null git config user.email "user@workshop.local" | Out-Null +# Detect the default branch name after first commit (created below) +# Will be detected after the initial commit + # Create initial application on main $app = @" class Application: @@ -45,6 +48,14 @@ Set-Content -Path "app.py" -Value $app git add app.py git commit -m "Initial application" | Out-Null +# Detect the main branch name after first commit +$mainBranch = git branch --show-current +if (-not $mainBranch) { + $mainBranch = git config --get init.defaultBranch + if (-not $mainBranch) { $mainBranch = "main" } +} +Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow + $readme = @" # MyApp @@ -78,7 +89,7 @@ git add login.py git commit -m "Start login service implementation" | Out-Null # Add a critical bug to main branch (simulating a bug that was introduced) -git checkout main | Out-Null +git checkout $mainBranch | Out-Null $appWithBug = @" class Application: @@ -140,13 +151,14 @@ Write-Host "========================================" -ForegroundColor Green Write-Host "`nSituation:" -ForegroundColor Cyan Write-Host "You're working on the login feature (feature-login branch)" -ForegroundColor White Write-Host "You have uncommitted changes - the feature is NOT complete yet" -ForegroundColor Yellow -Write-Host "`nUrgent: A critical security bug was found in production (main branch)!" -ForegroundColor Red +Write-Host "`nUrgent: A critical security bug was found in production ($mainBranch branch)!" -ForegroundColor Red Write-Host "You need to fix it immediately, but your current work isn't ready to commit." -ForegroundColor Red +Write-Host "`nDetected main branch: $mainBranch" -ForegroundColor Cyan Write-Host "`nYour task:" -ForegroundColor Yellow Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White Write-Host "2. Check your status: git status (see uncommitted changes)" -ForegroundColor White Write-Host "3. Stash your work: git stash save 'WIP: login feature'" -ForegroundColor White -Write-Host "4. Switch to main: git checkout main" -ForegroundColor White +Write-Host "4. Switch to $mainBranch: git checkout $mainBranch" -ForegroundColor White Write-Host "5. Fix the security bug in app.py (remove the comment and fix the auth)" -ForegroundColor White Write-Host "6. Commit the fix: git add app.py && git commit -m 'Fix critical security bug'" -ForegroundColor White Write-Host "7. Switch back: git checkout feature-login" -ForegroundColor White diff --git a/01-essentials/08-stash/verify.ps1 b/01-essentials/08-stash/verify.ps1 index 8c517d9..312ab2e 100644 --- a/01-essentials/08-stash/verify.ps1 +++ b/01-essentials/08-stash/verify.ps1 @@ -32,6 +32,21 @@ if (-not (Test-Path ".git")) { 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" } + } +} +Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan + # Check current branch $currentBranch = git branch --show-current 2>$null if ($currentBranch -ne "feature-login") { @@ -53,14 +68,14 @@ if ($status) { } # Verify main branch has the security fix -Write-Host "`nChecking main branch for bug fix..." -ForegroundColor Cyan -git checkout main 2>$null | Out-Null +Write-Host "`nChecking $mainBranch branch for bug fix..." -ForegroundColor Cyan +git checkout $mainBranch 2>$null | Out-Null # Check for bug fix commit -$mainCommits = git log --pretty=format:"%s" main 2>$null +$mainCommits = git log --pretty=format:"%s" $mainBranch 2>$null if ($mainCommits -notmatch "security bug|Fix.*bug|security fix") { - Write-Host "[FAIL] No security bug fix commit found on main branch." -ForegroundColor Red - Write-Host "Hint: After stashing, switch to main and commit a bug fix" -ForegroundColor Yellow + Write-Host "[FAIL] No security bug fix commit found on $mainBranch branch." -ForegroundColor Red + Write-Host "Hint: After stashing, switch to $mainBranch and commit a bug fix" -ForegroundColor Yellow git checkout feature-login 2>$null | Out-Null Set-Location .. exit 1 @@ -68,7 +83,7 @@ if ($mainCommits -notmatch "security bug|Fix.*bug|security fix") { # Check that app.py has been fixed if (-not (Test-Path "app.py")) { - Write-Host "[FAIL] app.py not found on main branch." -ForegroundColor Red + Write-Host "[FAIL] app.py not found on $mainBranch branch." -ForegroundColor Red git checkout feature-login 2>$null | Out-Null Set-Location .. exit 1