2.0 KiB
2.0 KiB
Remote Repository Analysis
Fetch files and analyze repositories without cloning them locally.
Fetch Files Without Cloning
Get directory listing
gh api repos/OWNER/REPO/contents/PATH
Returns JSON array with file/directory metadata.
Fetch file content
gh api repos/OWNER/REPO/contents/path/file.ts | jq -r '.content' | base64 -d
The API returns base64-encoded content, so decode it with base64 -d.
Get entire file tree recursively
gh api repos/OWNER/REPO/git/trees/main?recursive=1
Returns complete tree structure in one request.
Useful Remote Analysis Patterns
Check if file exists
gh api repos/OWNER/REPO/contents/path/file.ts 2>/dev/null && echo "exists" || echo "not found"
Get latest commit for specific file
gh api repos/OWNER/REPO/commits?path=src/index.ts | jq -r '.[0].sha'
Compare file across branches
gh api repos/OWNER/REPO/contents/file.ts?ref=main | jq -r '.content' | base64 -d > main.ts
gh api repos/OWNER/REPO/contents/file.ts?ref=dev | jq -r '.content' | base64 -d > dev.ts
diff main.ts dev.ts
Get file from specific commit
gh api repos/OWNER/REPO/contents/file.ts?ref=abc123 | jq -r '.content' | base64 -d
Use any commit SHA, branch name, or tag as the ref parameter.
Working with Large Repositories
For large repos, use the Git Trees API instead of Contents API:
# Get full tree
gh api repos/OWNER/REPO/git/trees/main?recursive=1 | jq '.tree[] | select(.type == "blob") | .path'
This is more efficient for listing many files.
Common Use Cases
Inspect configuration files
gh api repos/vercel/next.js/contents/package.json | jq -r '.content' | base64 -d | jq '.dependencies'
Check documentation
gh api repos/anthropics/anthropic-sdk-python/contents/README.md | jq -r '.content' | base64 -d
Analyze project structure
gh api repos/OWNER/REPO/git/trees/main?recursive=1 | jq -r '.tree[] | select(.type == "tree") | .path'
Shows all directories in the repository.