AgentSkillsCN

fetch-github-file

从 GitHub URL 中获取源代码。每当用户分享任意 GitHub URL 时(即使只是简单粘贴链接),尤其是像 github.com/owner/repo/blob/... 或 raw.githubusercontent.com/... 这样的文件 URL,或者当用户提出从 GitHub 读取或获取代码时,此技能都会自动生效。支持文件内容、行范围(#L10-L25)、目录列表,以及完整仓库的克隆操作。

SKILL.md
--- frontmatter
name: fetch-github-file
description: Fetch source code from GitHub URLs. Automatically use this skill whenever the user shares any GitHub URL (even if they only paste the link), especially file URLs like github.com/owner/repo/blob/... or raw.githubusercontent.com/..., or when they ask to read/fetch code from GitHub. Supports file contents, line ranges (#L10-L25), directory listings, and full repository clones.

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 PatternTypeAction
github.com/owner/repo/blob/ref/pathFileFetch file contents
github.com/owner/repo/blob/ref/path#L10File + lineFetch file, show from line 10
github.com/owner/repo/blob/ref/path#L10-L25File + rangeFetch file, show lines 10-25
github.com/owner/repo/tree/ref/pathDirectoryList directory contents
github.com/owner/repo/tree/refRootList repository root
github.com/owner/repoRepositoryOffer to clone or list root

Fetching Files

Use the GitHub API via gh to fetch file contents:

bash
# 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:

bash
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:

  1. Fetch the full file
  2. Extract and display only the specified lines
  3. Show the line numbers for context

Example output format:

code
# 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:

bash
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):

bash
# 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:

code
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 status is valid.
  • Large files: If content is null and download_url is present, file is too large for API. Use download_url directly.

Workflow

  1. Parse the URL - Extract owner, repo, ref, path, and line range
  2. Determine type - File (blob) or directory (tree)
  3. Fetch via gh API - Use authenticated API access
  4. Format output - Show file contents with line numbers, or directory listing
  5. 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:

  1. Parse: owner=anthropics, repo=claude-code, ref=main, path=src/index.ts, lines=1-50
  2. Fetch: gh api repos/anthropics/claude-code/contents/src/index.ts?ref=main
  3. Decode base64 content
  4. Display lines 1-50 with line numbers

User pastes: https://github.com/anthropics/claude-code/tree/main/src

Response:

  1. Parse: owner=anthropics, repo=claude-code, ref=main, path=src
  2. Fetch: gh api repos/anthropics/claude-code/contents/src?ref=main
  3. Display directory listing
  4. Offer to fetch specific files