360 lines
10 KiB
PowerShell
360 lines
10 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
<#
|
|
.SYNOPSIS
|
|
Sets up the Module 06 challenge environment for learning git reset.
|
|
|
|
.DESCRIPTION
|
|
This script creates a challenge directory with three branches demonstrating
|
|
different reset scenarios:
|
|
- soft-reset: Reset with --soft (keeps changes staged)
|
|
- mixed-reset: Reset with --mixed (unstages changes)
|
|
- hard-reset: Reset with --hard (discards everything) + reflog recovery
|
|
#>
|
|
|
|
Write-Host "`n=== Setting up Module 06: Git Reset Challenge ===" -ForegroundColor Cyan
|
|
Write-Host "⚠️ WARNING: Git reset is DANGEROUS - use with extreme caution! ⚠️" -ForegroundColor Red
|
|
|
|
# Remove existing challenge directory if it exists
|
|
if (Test-Path "challenge") {
|
|
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
|
|
Remove-Item -Recurse -Force "challenge"
|
|
}
|
|
|
|
# Create fresh challenge directory
|
|
Write-Host "Creating challenge directory..." -ForegroundColor Green
|
|
New-Item -ItemType Directory -Path "challenge" | Out-Null
|
|
Set-Location "challenge"
|
|
|
|
# Initialize Git repository
|
|
Write-Host "Initializing Git repository..." -ForegroundColor Green
|
|
git init | Out-Null
|
|
|
|
# Configure git for this repository
|
|
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)
|
|
# ============================================================================
|
|
$readmeContent = @"
|
|
# Git Reset Practice
|
|
|
|
This repository contains practice scenarios for learning git reset.
|
|
"@
|
|
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)
|
|
# ============================================================================
|
|
Write-Host "`nScenario 1: Creating soft-reset branch..." -ForegroundColor Cyan
|
|
|
|
# Create soft-reset branch from initial commit
|
|
git switch -c soft-reset | Out-Null
|
|
|
|
# Build up scenario 1 commits
|
|
$projectContent = @"
|
|
# project.py - Main project file
|
|
|
|
def initialize():
|
|
"""Initialize the project."""
|
|
print("Project initialized")
|
|
|
|
def main():
|
|
initialize()
|
|
print("Running application...")
|
|
"@
|
|
Set-Content -Path "project.py" -Value $projectContent
|
|
git add .
|
|
git commit -m "Initial project setup" | Out-Null
|
|
|
|
# Good commit: Add feature A
|
|
$projectContent = @"
|
|
# project.py - Main project file
|
|
|
|
def initialize():
|
|
"""Initialize the project."""
|
|
print("Project initialized")
|
|
|
|
def feature_a():
|
|
"""Feature A implementation."""
|
|
print("Feature A is working")
|
|
|
|
def main():
|
|
initialize()
|
|
feature_a()
|
|
print("Running application...")
|
|
"@
|
|
Set-Content -Path "project.py" -Value $projectContent
|
|
git add .
|
|
git commit -m "Add feature A" | Out-Null
|
|
|
|
# Good commit: Add feature B
|
|
$projectContent = @"
|
|
# project.py - Main project file
|
|
|
|
def initialize():
|
|
"""Initialize the project."""
|
|
print("Project initialized")
|
|
|
|
def feature_a():
|
|
"""Feature A implementation."""
|
|
print("Feature A is working")
|
|
|
|
def feature_b():
|
|
"""Feature B implementation."""
|
|
print("Feature B is working")
|
|
|
|
def main():
|
|
initialize()
|
|
feature_a()
|
|
feature_b()
|
|
print("Running application...")
|
|
"@
|
|
Set-Content -Path "project.py" -Value $projectContent
|
|
git add .
|
|
git commit -m "Add feature B" | Out-Null
|
|
|
|
# BAD commit: Add feature C (wrong implementation)
|
|
$projectContent = @"
|
|
# project.py - Main project file
|
|
|
|
def initialize():
|
|
"""Initialize the project."""
|
|
print("Project initialized")
|
|
|
|
def feature_a():
|
|
"""Feature A implementation."""
|
|
print("Feature A is working")
|
|
|
|
def feature_b():
|
|
"""Feature B implementation."""
|
|
print("Feature B is working")
|
|
|
|
def feature_c():
|
|
"""Feature C implementation - WRONG!"""
|
|
print("Feature C has bugs!") # This needs to be re-implemented
|
|
|
|
def main():
|
|
initialize()
|
|
feature_a()
|
|
feature_b()
|
|
feature_c()
|
|
print("Running application...")
|
|
"@
|
|
Set-Content -Path "project.py" -Value $projectContent
|
|
git add .
|
|
git commit -m "Add feature C - needs better implementation!" | Out-Null
|
|
|
|
Write-Host "[CREATED] soft-reset branch with commit to reset --soft" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# SCENARIO 2: Mixed Reset (--mixed, default)
|
|
# ============================================================================
|
|
Write-Host "`nScenario 2: Creating mixed-reset branch..." -ForegroundColor Cyan
|
|
|
|
# Switch back to initial commit and create mixed-reset branch
|
|
git switch $mainBranch | Out-Null
|
|
git switch -c mixed-reset | Out-Null
|
|
|
|
# Build up scenario 2 commits
|
|
$appContent = @"
|
|
# app.py - Application entry point
|
|
|
|
def start():
|
|
"""Start the application."""
|
|
print("Application started")
|
|
|
|
def stop():
|
|
"""Stop the application."""
|
|
print("Application stopped")
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
git add .
|
|
git commit -m "Add application lifecycle" | Out-Null
|
|
|
|
# Good commit: Add logging
|
|
$appContent = @"
|
|
# app.py - Application entry point
|
|
|
|
def log(message):
|
|
"""Log a message."""
|
|
print(f"[LOG] {message}")
|
|
|
|
def start():
|
|
"""Start the application."""
|
|
log("Application started")
|
|
|
|
def stop():
|
|
"""Stop the application."""
|
|
log("Application stopped")
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
git add .
|
|
git commit -m "Add logging system" | Out-Null
|
|
|
|
# BAD commit 1: Add experimental feature X
|
|
$appContent = @"
|
|
# app.py - Application entry point
|
|
|
|
def log(message):
|
|
"""Log a message."""
|
|
print(f"[LOG] {message}")
|
|
|
|
def experimental_feature_x():
|
|
"""Experimental feature - NOT READY!"""
|
|
log("Feature X is experimental and buggy")
|
|
|
|
def start():
|
|
"""Start the application."""
|
|
log("Application started")
|
|
experimental_feature_x()
|
|
|
|
def stop():
|
|
"""Stop the application."""
|
|
log("Application stopped")
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
git add .
|
|
git commit -m "Add experimental feature X - REMOVE THIS!" | Out-Null
|
|
|
|
# BAD commit 2: Add debug mode (also not ready)
|
|
$appContent = @"
|
|
# app.py - Application entry point
|
|
|
|
DEBUG_MODE = True # Should not be committed!
|
|
|
|
def log(message):
|
|
"""Log a message."""
|
|
print(f"[LOG] {message}")
|
|
|
|
def experimental_feature_x():
|
|
"""Experimental feature - NOT READY!"""
|
|
log("Feature X is experimental and buggy")
|
|
|
|
def start():
|
|
"""Start the application."""
|
|
if DEBUG_MODE:
|
|
log("DEBUG MODE ACTIVE!")
|
|
log("Application started")
|
|
experimental_feature_x()
|
|
|
|
def stop():
|
|
"""Stop the application."""
|
|
log("Application stopped")
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
git add .
|
|
git commit -m "Add debug mode - REMOVE THIS TOO!" | Out-Null
|
|
|
|
Write-Host "[CREATED] mixed-reset branch with commits to reset --mixed" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# SCENARIO 3: Hard Reset (--hard) + Reflog Recovery
|
|
# ============================================================================
|
|
Write-Host "`nScenario 3: Creating hard-reset branch..." -ForegroundColor Cyan
|
|
|
|
# Switch back to main and create hard-reset branch
|
|
git switch $mainBranch | Out-Null
|
|
git switch -c hard-reset | Out-Null
|
|
|
|
# Reset to basic state
|
|
$utilsContent = @"
|
|
# utils.py - Utility functions
|
|
|
|
def helper_a():
|
|
"""Helper function A."""
|
|
return "Helper A"
|
|
|
|
def helper_b():
|
|
"""Helper function B."""
|
|
return "Helper B"
|
|
"@
|
|
Set-Content -Path "utils.py" -Value $utilsContent
|
|
git add .
|
|
git commit -m "Add utility helpers" | Out-Null
|
|
|
|
# Good commit: Add helper C
|
|
$utilsContent = @"
|
|
# utils.py - Utility functions
|
|
|
|
def helper_a():
|
|
"""Helper function A."""
|
|
return "Helper A"
|
|
|
|
def helper_b():
|
|
"""Helper function B."""
|
|
return "Helper B"
|
|
|
|
def helper_c():
|
|
"""Helper function C."""
|
|
return "Helper C"
|
|
"@
|
|
Set-Content -Path "utils.py" -Value $utilsContent
|
|
git add .
|
|
git commit -m "Add helper C" | Out-Null
|
|
|
|
# BAD commit: Add broken helper D (completely wrong)
|
|
$utilsContent = @"
|
|
# utils.py - Utility functions
|
|
|
|
def helper_a():
|
|
"""Helper function A."""
|
|
return "Helper A"
|
|
|
|
def helper_b():
|
|
"""Helper function B."""
|
|
return "Helper B"
|
|
|
|
def helper_c():
|
|
"""Helper function C."""
|
|
return "Helper C"
|
|
|
|
def helper_d():
|
|
"""COMPLETELY BROKEN - throw away!"""
|
|
# This is all wrong and needs to be discarded
|
|
broken_code = "This doesn't even make sense"
|
|
return broken_code.nonexistent_method() # Will crash!
|
|
"@
|
|
Set-Content -Path "utils.py" -Value $utilsContent
|
|
git add .
|
|
git commit -m "Add broken helper D - DISCARD COMPLETELY!" | Out-Null
|
|
|
|
Write-Host "[CREATED] hard-reset branch with commit to reset --hard" -ForegroundColor Green
|
|
|
|
# ============================================================================
|
|
# Return to soft-reset to start
|
|
# ============================================================================
|
|
git switch soft-reset | Out-Null
|
|
|
|
# Return to module directory
|
|
Set-Location ..
|
|
|
|
Write-Host "`n=== Setup Complete! ===`n" -ForegroundColor Green
|
|
Write-Host "Three reset scenarios have been created:" -ForegroundColor Cyan
|
|
Write-Host " 1. soft-reset - Reset --soft (keep changes staged)" -ForegroundColor White
|
|
Write-Host " 2. mixed-reset - Reset --mixed (unstage changes)" -ForegroundColor White
|
|
Write-Host " 3. hard-reset - Reset --hard (discard everything) + reflog recovery" -ForegroundColor White
|
|
Write-Host "`n⚠️ CRITICAL SAFETY REMINDER ⚠️" -ForegroundColor Red
|
|
Write-Host "NEVER use git reset on commits that have been PUSHED!" -ForegroundColor Red
|
|
Write-Host "These scenarios are LOCAL ONLY for practice." -ForegroundColor Yellow
|
|
Write-Host "`nYou are currently on the 'soft-reset' branch." -ForegroundColor Cyan
|
|
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
|
Write-Host " 1. cd challenge" -ForegroundColor White
|
|
Write-Host " 2. Read the README.md for detailed instructions" -ForegroundColor White
|
|
Write-Host " 3. Complete each reset challenge" -ForegroundColor White
|
|
Write-Host " 4. Run '..\\verify.ps1' to check your solutions" -ForegroundColor White
|
|
Write-Host ""
|