432 lines
12 KiB
PowerShell
432 lines
12 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
<#
|
|
.SYNOPSIS
|
|
Sets up the Module 02 challenge environment with commit history.
|
|
|
|
.DESCRIPTION
|
|
This script creates a challenge directory with a Git repository that
|
|
contains multiple commits for students to explore using git log and git diff.
|
|
#>
|
|
|
|
Write-Host "`n=== Setting up Module 02 Challenge ===" -ForegroundColor Cyan
|
|
|
|
# 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"
|
|
|
|
# Commit 1: Initial project structure
|
|
Write-Host "Creating initial project structure..." -ForegroundColor Green
|
|
|
|
$appContent = @"
|
|
# app.py - Main application file
|
|
|
|
def main():
|
|
print("Welcome to My App!")
|
|
# Application initialization code here
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
|
|
git add .
|
|
git commit -m "Initial project structure" | Out-Null
|
|
|
|
# Commit 2: Add user authentication
|
|
Write-Host "Adding user authentication..." -ForegroundColor Green
|
|
$authContent = @"
|
|
# auth.py - Authentication module
|
|
|
|
def login(username, password):
|
|
# Authenticate user
|
|
print(f"Logging in user: {username}")
|
|
return True
|
|
|
|
def logout(username):
|
|
# Log out user
|
|
print(f"Logging out user: {username}")
|
|
return True
|
|
"@
|
|
Set-Content -Path "auth.py" -Value $authContent
|
|
|
|
$appContent = @"
|
|
# app.py - Main application file
|
|
from auth import login, logout
|
|
|
|
def main():
|
|
print("Welcome to My App!")
|
|
# Application initialization code here
|
|
login("user", "password")
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
|
|
git add .
|
|
git commit -m "Add user authentication" | Out-Null
|
|
|
|
# Commit 3: Add database connection
|
|
Write-Host "Adding database connection..." -ForegroundColor Green
|
|
$databaseContent = @"
|
|
# database.py - Database connection module
|
|
# SECRET_CODE: UNICORN
|
|
|
|
def connect():
|
|
# Connect to database
|
|
print("Connecting to database...")
|
|
return True
|
|
|
|
def disconnect():
|
|
# Disconnect from database
|
|
print("Disconnecting from database...")
|
|
return True
|
|
"@
|
|
Set-Content -Path "database.py" -Value $databaseContent
|
|
|
|
$appContent = @"
|
|
# app.py - Main application file
|
|
from auth import login, logout
|
|
from database import connect, disconnect
|
|
|
|
def main():
|
|
print("Welcome to My App!")
|
|
connect()
|
|
# Application initialization code here
|
|
login("user", "password")
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
|
|
git add .
|
|
git commit -m "Add database connection" | Out-Null
|
|
|
|
# Commit 4: Fix authentication bug
|
|
Write-Host "Fixing authentication bug..." -ForegroundColor Green
|
|
$authContent = @"
|
|
# auth.py - Authentication module
|
|
|
|
def login(username, password):
|
|
# Authenticate user
|
|
if not username or not password:
|
|
print("Error: Username and password required")
|
|
return False
|
|
print(f"Logging in user: {username}")
|
|
return True
|
|
|
|
def logout(username):
|
|
# Log out user
|
|
print(f"Logging out user: {username}")
|
|
return True
|
|
"@
|
|
Set-Content -Path "auth.py" -Value $authContent
|
|
|
|
# Remove the secret code from database.py
|
|
$databaseContent = @"
|
|
# database.py - Database connection module
|
|
|
|
def connect():
|
|
# Connect to database
|
|
print("Connecting to database...")
|
|
return True
|
|
|
|
def disconnect():
|
|
# Disconnect from database
|
|
print("Disconnecting from database...")
|
|
return True
|
|
"@
|
|
Set-Content -Path "database.py" -Value $databaseContent
|
|
|
|
git add .
|
|
git commit -m "Fix authentication bug" | Out-Null
|
|
|
|
# Commit 5: Add user profile feature
|
|
Write-Host "Adding user profile feature..." -ForegroundColor Green
|
|
$profileContent = @"
|
|
# profile.py - User profile module
|
|
|
|
def get_profile(username):
|
|
# Get user profile
|
|
print(f"Fetching profile for: {username}")
|
|
return {"username": username, "email": f"{username}@example.com"}
|
|
|
|
def update_profile(username, data):
|
|
# Update user profile
|
|
print(f"Updating profile for: {username}")
|
|
return True
|
|
"@
|
|
Set-Content -Path "profile.py" -Value $profileContent
|
|
|
|
$appContent = @"
|
|
# app.py - Main application file
|
|
from auth import login, logout
|
|
from database import connect, disconnect
|
|
from profile import get_profile
|
|
|
|
def main():
|
|
print("Welcome to My App!")
|
|
connect()
|
|
# Application initialization code here
|
|
if login("user", "password"):
|
|
profile = get_profile("user")
|
|
print(f"User profile: {profile}")
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
|
|
git add .
|
|
git commit -m "Add user profile feature" | Out-Null
|
|
|
|
# Commit 6: Add a suspicious change (for git blame challenge)
|
|
Write-Host "Adding configuration change..." -ForegroundColor Green
|
|
# Temporarily change git user for this commit
|
|
git config user.name "Suspicious Developer"
|
|
git config user.email "guilty@email.com"
|
|
|
|
$appContent = @"
|
|
# app.py - Main application file
|
|
from auth import login, logout
|
|
from database import connect, disconnect
|
|
from profile import get_profile
|
|
|
|
def main():
|
|
print("Welcome to My App!")
|
|
connect()
|
|
# Application initialization code here
|
|
if login("admin", "admin123"): # TODO: Change default credentials!
|
|
profile = get_profile("admin")
|
|
print(f"User profile: {profile}")
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
"@
|
|
Set-Content -Path "app.py" -Value $appContent
|
|
|
|
git add .
|
|
git commit -m "Update default login credentials" | Out-Null
|
|
|
|
# Reset git config back to original
|
|
git config user.name "Workshop Student"
|
|
git config user.email "student@example.com"
|
|
|
|
# Create a staged change scenario
|
|
Write-Host "Creating staged changes for exploration..." -ForegroundColor Green
|
|
$configContent = @"
|
|
# config.py - Configuration settings
|
|
|
|
DEBUG_MODE = False
|
|
DATABASE_URL = "sqlite:///app.db"
|
|
"@
|
|
Set-Content -Path "config.py" -Value $configContent
|
|
git add config.py
|
|
|
|
# Create answers.md template
|
|
Write-Host "Creating answers.md template..." -ForegroundColor Green
|
|
$answersTemplate = @"
|
|
# Git History Exploration - Answers
|
|
|
|
Welcome! Answer the following questions by exploring the Git repository history using Git commands.
|
|
|
|
**Instructions:**
|
|
- Replace the "Write your answer here" comments with your actual answers
|
|
- You can use any Git commands to explore the repository
|
|
- The hints section at the bottom provides helpful commands
|
|
|
|
---
|
|
|
|
## Question 1: How many commits are in the repository?
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
git log --oneline
|
|
# Or count them with:
|
|
git rev-list --count HEAD
|
|
``````
|
|
|
|
**Your Answer:**
|
|
|
|
<!-- Write your answer here -->
|
|
|
|
---
|
|
|
|
## Question 2: What was the commit message for the third commit?
|
|
|
|
**Note:** Count from the first/oldest commit (the one at the bottom of `git log`)
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
git log --oneline --reverse # Shows oldest first
|
|
git log # Shows newest first
|
|
``````
|
|
|
|
**Your Answer:**
|
|
|
|
<!-- Write your answer here -->
|
|
|
|
---
|
|
|
|
## Question 3: Which files were modified in the "Fix authentication bug" commit?
|
|
|
|
**Note:** There are multiple files - list all of them!
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
git log --stat # Shows files changed in each commit
|
|
git show <commit-hash> --name-only # Shows only filenames for a commit
|
|
``````
|
|
|
|
**Your Answer:**
|
|
|
|
<!-- Write your answer here (list all files) -->
|
|
|
|
---
|
|
|
|
## Question 4: What new file is currently staged (ready to be committed)?
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
git status # Shows staged, unstaged, and untracked files
|
|
git diff --staged # Shows content of staged changes
|
|
``````
|
|
|
|
**Your Answer:**
|
|
|
|
<!-- Write your answer here -->
|
|
|
|
---
|
|
|
|
## Question 5: Find the secret code hidden in the commit history! 🔍
|
|
|
|
**The Challenge:**
|
|
A secret code was added in one commit and then removed in a later commit. It doesn't exist in the current files - you can ONLY find it by comparing commits with `git diff`.
|
|
|
|
**Hint:** The secret code is in `database.py` and exists in the third commit but was removed in the fourth commit.
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
# First, get the commit hashes
|
|
git log --oneline
|
|
|
|
# Then compare the third and fourth commits
|
|
git diff <commit3-hash> <commit4-hash> database.py
|
|
# Look for lines that were removed (marked with - in red)
|
|
``````
|
|
|
|
**Your Answer (what is the secret code?):**
|
|
|
|
<!-- Write your answer here -->
|
|
|
|
---
|
|
|
|
## Question 6: Who changed the login credentials to hardcoded values? 🔍
|
|
|
|
**The Challenge:**
|
|
Someone on the team added hardcoded login credentials to `app.py` (username: "admin", password: "admin123"). This is a security issue! Use `git blame` to find out who made this change.
|
|
|
|
**Your task:** Find the email address of the person who wrote the line containing `login("admin", "admin123")`.
|
|
|
|
**Suggested commands:**
|
|
``````bash
|
|
# View who changed each line in app.py
|
|
git blame app.py
|
|
|
|
# For a more readable format
|
|
git blame -e app.py # Shows email addresses
|
|
|
|
# Look for the line with login("admin", "admin123")
|
|
# The format is: commit_hash (Author Name <email> date time line_number) line_content
|
|
``````
|
|
|
|
**Your Answer (provide the email address):**
|
|
|
|
<!-- Write your answer here -->
|
|
|
|
---
|
|
|
|
## Quick Reference - Useful Commands
|
|
|
|
**Viewing History:**
|
|
``````bash
|
|
git log # View commit history (newest first)
|
|
git log --oneline # Compact one-line format
|
|
git log --reverse # Show oldest commits first
|
|
git log --stat # Show files changed in each commit
|
|
git log --graph --all # Visual branch graph
|
|
``````
|
|
|
|
**Viewing Specific Commits:**
|
|
``````bash
|
|
git show <commit-hash> # View commit details
|
|
git show <commit-hash> --stat # Show files changed
|
|
git show <commit-hash>:file.txt # View file from specific commit
|
|
``````
|
|
|
|
**Comparing Changes:**
|
|
``````bash
|
|
git diff <commit1> <commit2> # Compare two commits
|
|
git diff <commit1> <commit2> file # Compare specific file
|
|
git diff --staged # Show staged changes
|
|
git diff # Show unstaged changes
|
|
git diff HEAD # Show all changes since last commit
|
|
``````
|
|
|
|
**Finding Who Changed What:**
|
|
``````bash
|
|
git blame <file> # Show who last modified each line
|
|
git blame -e <file> # Show with email addresses
|
|
git blame -L 10,20 <file> # Blame specific line range
|
|
git blame <file> | grep "pattern" # Find who changed lines matching pattern
|
|
``````
|
|
|
|
**Repository Status:**
|
|
``````bash
|
|
git status # Show working tree status
|
|
git ls-files # List all tracked files
|
|
``````
|
|
|
|
**Pro Tip:** You can use just the first 7 characters of a commit hash (e.g., `a1b2c3d` instead of the full hash)
|
|
|
|
---
|
|
|
|
When you're done, run ``..\verify.ps1`` to check your answers!
|
|
"@
|
|
|
|
Set-Content -Path "answers.md" -Value $answersTemplate
|
|
|
|
# Return to module directory
|
|
Set-Location ..
|
|
|
|
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
|
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
|
|
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
|
Write-Host " 1. cd challenge" -ForegroundColor White
|
|
Write-Host " 2. Open 'answers.md' to see the questions" -ForegroundColor White
|
|
Write-Host " 3. Explore the commit history using 'git log'" -ForegroundColor White
|
|
Write-Host " 4. Fill in your answers in 'answers.md'" -ForegroundColor White
|
|
Write-Host " 5. Run '..\verify.ps1' to check your answers" -ForegroundColor White
|
|
Write-Host ""
|