Fetch GitHub File
Fetch source code from GitHub URLs using the gh CLI for reliable, authenticated access.
URL Patterns
Parse the GitHub URL to determine what to fetch:
| URL Pattern | Type | Action |
|---|---|---|
github.com/owner/repo/blob/ref/path | File | Fetch file contents |
github.com/owner/repo/blob/ref/path#L10 | File + line | Fetch file, show from line 10 |
github.com/owner/repo/blob/ref/path#L10-L25 | File + range | Fetch file, show lines 10-25 |
github.com/owner/repo/tree/ref/path | Directory | List directory contents |
github.com/owner/repo/tree/ref | Root | List repository root |
github.com/owner/repo | Repository | Offer to clone or list root |
Fetching Files
Use the GitHub API via gh to fetch file contents:
# Fetch file content (returns JSON with base64-encoded content)
# IMPORTANT: Quote the URL to prevent shell interpretation of ?
gh api 'repos/{owner}/{repo}/contents/{path}?ref={ref}'
To decode and display:
gh api 'repos/{owner}/{repo}/contents/{path}?ref={ref}' --jq '.content' | base64 -d
Line Ranges
If the URL includes a line range like #L10-L25:
- •Fetch the full file
- •Extract and display only the specified lines
- •Show the line numbers for context
Example output format:
# src/utils/parser.ts (lines 10-25)
10: export function parseUrl(url: string): ParsedUrl {
11: const match = url.match(GITHUB_PATTERN);
...
Fetching Directories
For directory URLs, list the contents:
gh api 'repos/{owner}/{repo}/contents/{path}?ref={ref}' --jq '.[] | "\(.type)\t\(.name)"'
Display as a tree structure showing files and subdirectories. Offer to fetch specific files if the user wants to explore further.
Cloning Repositories
For full repository access (exploring multiple files, understanding structure):
# Clone to a temporary directory
TEMP_DIR=$(mktemp -d)
gh repo clone {owner}/{repo} "$TEMP_DIR" -- --depth 1 --branch {ref}
echo "Cloned to: $TEMP_DIR"
Use shallow clone (--depth 1) by default for speed. After cloning:
- •List the directory structure
- •Read files as requested
- •Remember the temp directory path for subsequent requests
URL Parsing
Extract components from GitHub URLs:
https://github.com/anthropics/claude-code/blob/main/src/index.ts#L10-L25
^^^^^^^^^ ^^^^^^^^^^^ ^^^^ ^^^^ ^^^^^^^^^^^^^ ^^^^^^^^
| | | | | |
| | | | path line range
| | | ref (branch/tag/commit)
| | "blob" = file, "tree" = directory
| repo
owner
Error Handling
- •404: File/path doesn't exist. Check spelling and ref.
- •403: Rate limited or private repo. Ensure
gh auth statusis valid. - •Large files: If content is null and download_url is present, file is too large for API. Use download_url directly.
Workflow
- •Parse the URL - Extract owner, repo, ref, path, and line range
- •Determine type - File (blob) or directory (tree)
- •Fetch via gh API - Use authenticated API access
- •Format output - Show file contents with line numbers, or directory listing
- •Offer next steps - "Want me to fetch another file?" or "Should I clone the repo for deeper exploration?"
Examples
User pastes: https://github.com/anthropics/claude-code/blob/main/src/index.ts#L1-L50
Response:
- •Parse: owner=anthropics, repo=claude-code, ref=main, path=src/index.ts, lines=1-50
- •Fetch:
gh api repos/anthropics/claude-code/contents/src/index.ts?ref=main - •Decode base64 content
- •Display lines 1-50 with line numbers
User pastes: https://github.com/anthropics/claude-code/tree/main/src
Response:
- •Parse: owner=anthropics, repo=claude-code, ref=main, path=src
- •Fetch:
gh api repos/anthropics/claude-code/contents/src?ref=main - •Display directory listing
- •Offer to fetch specific files