| name | code-archaeology |
| description | Understand code history and context through git archaeology to answer "why", "who", and "when" questions about code. |
Code Archaeology
Purpose
Understand code history and context through git archaeology to answer "why", "who", and "when" questions about code.
Capabilities
- git blame analysis (who wrote this, when, why)
- git log --follow (track file renames and moves)
- Find related PRs/issues via commit messages
- Identify original author for questions
- Find test coverage for legacy code
- Understand evolution of APIs and patterns
- Detect dead code and deprecated patterns
Triggers
- "why was this written"
- "who owns this code"
- "when was this added"
- "history of this function"
- "find related PRs"
- "track this file's history"
Implementation
1. Git Blame Analysis
git blame <file>
git blame -s --date=short <file>
git blame -L <start>,<end> <file>
git blame -w <file>
git blame -e <file>
git blame -L $(grep -n "function myFunction" file.ts | cut -d: -f1),+20 file.ts
Output format:
a1b2c3d4 (John Doe 2024-03-15) export function myFunction() {
e5f6g7h8 (Jane Smith 2024-06-20) // Updated to support new API
a1b2c3d4 (John Doe 2024-03-15) return doSomething();
i9j0k1l2 (John Doe 2024-08-10) }
Interpretation:
- Original author: John Doe (2024-03-15)
- Modified by: Jane Smith (2024-06-20) - comment update
- Last touch: John Doe (2024-08-10) - closing brace (probably refactor)
2. Commit History Deep Dive
git show <commit-hash>
git log -p <commit-hash> -1
git log -S "specific code string" --source --all
git log -L :<function-name>:<file>
git log -p -L :<function-name>:<file>
3. Track File Renames and Moves
git log --follow --name-status -- <file>
git log --follow --stat -- <file>
git log --follow --diff-filter=R -- <file>
git log --follow --oneline \
x-pack/platform/packages/shared/kbn-agent-builder/src/core.ts
Output:
abc1234 refactor: move agent-builder to shared packages
def5678 feat: add agent-builder core functionality
ghi9012 initial commit
4. Find Related PRs and Issues
git log --grep="PR" --grep="#[0-9]" --oneline
git log --format="%H %s" | grep -E "#[0-9]+"
commit_hash="abc1234"
pr_number=$(git log --format="%s" $commit_hash -1 | grep -oE "#[0-9]+" | head -1 | tr -d '#')
gh pr view $pr_number
gh pr list --author john.doe --state all --limit 100
gh pr list --search "agent builder" --state all
5. Identify Code Ownership
git log --format="%an" -- <file> | sort | uniq -c | sort -rn | head -5
git log --since="6 months ago" --format="%an" -- <file> | sort | uniq -c | sort -rn
grep -r "path/to/file" .github/CODEOWNERS
grep "x-pack/platform/packages/shared/kbn-agent-builder" .github/CODEOWNERS
Example output:
45 John Doe
23 Jane Smith
8 Bob Johnson
Result: John Doe is primary owner (45 commits), Jane Smith is secondary (23 commits)
6. Find Test Coverage for Legacy Code
source_file="src/core/server/http_server.ts"
test_file="${source_file%.ts}.test.ts"
if [ -f "$test_file" ]; then
echo "Unit test found: $test_file"
else
echo "No unit test found"
fi
grep -r "$(basename $source_file .ts)" --include="*.integration.test.ts" .
grep -r "http server" x-pack/test/api_integration/
find . -name "*.scout.ts" -exec grep -l "http server" {} \;
cat target/kibana-coverage/jest/coverage-summary.json | \
jq ".\"$(pwd)/$source_file\""
7. Detect Dead Code and Deprecations
grep -r "@deprecated" --include="*.ts" <path>
grep -r "TODO\|FIXME" --include="*.ts" <path> | grep -E "[0-9]{4}"
exports=$(grep -E "^export (const|function|class|interface|type)" <file> | \
sed -E 's/export (const|function|class|interface|type) ([a-zA-Z0-9_]+).*/\2/')
for export in $exports; do
count=$(rg -l "import.*$export.*from" | wc -l)
if [ $count -eq 0 ]; then
echo "Unused export: $export"
fi
done
git log --all -S "@deprecated" -- <file>
8. Understand API Evolution
function_name="myFunction"
file="src/core.ts"
git log -L :$function_name:$file --oneline
git log -p -L :$function_name:$file
git log -p -S "function $function_name" -- $file
Example Workflow
User: "why was this function written this way?"
Context: User is looking at a complex function with unusual implementation
Step 1: Get function location
file="x-pack/platform/packages/shared/kbn-agent-builder/src/core.ts"
func="executeAgentWorkflow"
line_num=$(grep -n "executeAgentWorkflow" $file | head -1 | cut -d: -f1)
Step 2: Blame the function
git blame -L $line_num,+50 $file
Step 3: Get commit details
git show abc123
Step 4: Find related PR
pr_num=$(git log --format="%s" abc123 -1 | grep -oE "#[0-9]+" | tr -d '#')
gh pr view $pr_num
Step 5: Show evolution
git log -p -L :executeAgentWorkflow:$file
Step 6: Find ownership
git log --format="%an" -- $file | sort | uniq -c | sort -rn | head -3
grep "kbn-agent-builder" .github/CODEOWNERS
Final Answer:
Code Archaeology Report for executeAgentWorkflow()
====================================================
Original Author: John Doe (john@elastic.co)
Date: 2024-03-15
Commit: abc123
Why Written This Way:
The function uses sequential execution (not Promise.all) to avoid race
conditions when multiple workflows modify shared state. This was changed
from a simpler parallel implementation after discovering a bug in
production (issue #12345).
Evolution:
- v1 (2024-01-10): Simple Promise.all for parallelism
- v2 (2024-02-15): Added error handling
- v3 (2024-03-15): Serialized execution to fix race condition
Related PR: #12346
Discussion highlights:
- Trade-off: Serialization reduces throughput but ensures correctness
- Alternative approaches were considered (locking, immutable state)
- Current approach chosen for simplicity and safety
Code Ownership:
- Primary: @elastic/platform-core
- Contact: John Doe (15 commits), Jane Smith (5 commits)
Recommendation:
If questioning this approach, read PR #12346 first. Consider consulting
John Doe or @elastic/platform-core before refactoring.
Advanced Techniques
Find When a Bug Was Introduced
git bisect start
git bisect bad HEAD
git bisect good v7.0.0
git bisect good
git bisect bad
git bisect reset
Find All Places a Pattern Was Changed
git log -p -G "pattern|regex" -- <path>
git log -p -G "try.*catch" -- src/
git log -p -G "client\.search\(" -- x-pack/
Visualize File History
gitk --follow <file>
tig --follow <file>
git log --follow --format="%h|%ad|%an|%s" --date=short -- <file> | \
awk -F'|' '{print "- **" $2 "** " $3 ": " $4 " (commit: " $1 ")"}'
Integration with Other Skills
- spike-builder: Research existing patterns before implementing new features
- buildkite-ci-debugger: Find when CI config was last changed
- pr-optimizer: Identify reviewers based on code ownership
Quality Principles
- Always provide context, not just facts (why, not just who/when)
- Link to PRs/issues for deeper discussion
- Identify current code owners for follow-up questions
- Respect git history as documentation of intent
References