Explain Code History
File: $1
Lines: $2 (or full file if not specified)
- Run git blame on the specified range:
git blame -L ${2:-1,50} $1 2>/dev/null || git blame $1
- For each distinct commit found, get the full message:
git show <hash> --stat --format="%H%n%an%n%ad%n%s%n%b"
- Correlate code lines with commit messages.
- Produce a narrative explanation:
- Who wrote each section and when
- What the commit message says about the intent
- Any related issue/PR references in the commit
- Notable patterns (e.g., "this was a hotfix", "this was a refactor from X")
This helps understand why code exists, not just what it does.