feat: add `repo read-file` and `repo read-dir` by babakks · Pull Request #13580 · cli/cli · GitHub
Skip to content

feat: add repo read-file and repo read-dir#13580

Merged
BagToad merged 22 commits into
trunkfrom
babakks/feature-repo-read
Jun 17, 2026
Merged

feat: add repo read-file and repo read-dir#13580
BagToad merged 22 commits into
trunkfrom
babakks/feature-repo-read

Conversation

@babakks

@babakks babakks commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

This PR adds two new (preview) commands for reading repository contents without cloning.

gh repo read-file

gh repo read-file <path> [--ref <ref>] [--output <path>] [--clobber] [--unsafe] [--json <fields>] [--jq <expression>] [--template <string>] [--repo <owner/repo>]

Reads a single file from a repository and writes it to stdout (or to a file with --output).

gh repo read-dir

gh repo read-dir [<path>] [--ref <ref>] [--json <fields>] [--jq <expression>] [--template <string>] [--repo <owner/repo>]

Lists the entries of a directory in a repository, showing each entry's type, name, and size.

Both commands are marked preview, default to the current repository's default branch (overridable with --ref), and support --json/--jq/--template for scripting.

Test plan

You can verify the command behaviour via acceptance tests and the following examples.

The commands below target public repositories so they can be run as-is.

gh repo read-file

# Read a file to stdout
gh repo read-file README.md --repo cli/cli

# Read from a specific tag/branch/commit
gh repo read-file README.md --ref v2.0.0 --repo cli/cli

# Write to a file, then confirm --clobber is required to overwrite
gh repo read-file README.md --output /tmp/readme.md --repo cli/cli
gh repo read-file README.md --output /tmp/readme.md --repo cli/cli            # should error: already exists
gh repo read-file README.md --output /tmp/readme.md --clobber --repo cli/cli  # should succeed

# JSON output, with jq and template
gh repo read-file README.md --json path,size,type --repo cli/cli
gh repo read-file README.md --json path,size --jq '.size' --repo cli/cli
gh repo read-file README.md --template '{{.path}} ({{.size}} bytes){{"\n"}}' --repo cli/cli

# Error: path does not exist
gh repo read-file does/not/exist.md --repo cli/cli

gh repo read-dir

# List the repository root
gh repo read-dir --repo cli/cli

# List a subdirectory (note executables render as `file*`)
gh repo read-dir script --repo cli/cli

# Mixed entry types in one listing: regular files, a symlink (RelNotes),
# and a submodule (sha1collisiondetection)
gh repo read-dir --repo git/git

# List from a specific tag/branch/commit
gh repo read-dir docs --ref v2.0.0 --repo cli/cli

# Non-TTY / scriptable output (tab separated)
gh repo read-dir script --repo cli/cli | cat

# JSON output, with jq and template
gh repo read-dir script --json name,type,mode,modeOctal --repo cli/cli
gh repo read-dir script --json name,type --jq '.entries[] | select(.type=="file") | .name' --repo cli/cli
gh repo read-dir --template '{{range .entries}}{{.type}}: {{.name}}{{"\n"}}{{end}}' --repo cli/cli

# Error: path points to a file, not a directory
gh repo read-dir README.md --repo cli/cli

# Error: path or ref does not exist
gh repo read-dir nope/nope --repo cli/cli

babakks and others added 12 commits June 2, 2026 23:17
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
…ands

Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
@babakks babakks marked this pull request as ready for review June 3, 2026 22:29
@babakks babakks requested a review from a team as a code owner June 3, 2026 22:29
@babakks babakks requested review from BagToad and Copilot June 3, 2026 22:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces two new preview gh repo subcommands—read-file and read-dir—to fetch repository file and directory contents over the GitHub API without cloning, with support for scriptable output (--json/--jq/--template) and acceptance coverage.

Changes:

  • Add gh repo read-file for fetching a single file’s metadata/content and optionally writing it to disk.
  • Add gh repo read-dir for listing a repository directory (TTY table / non-TTY TSV / JSON).
  • Add a shared internal/text.FormatSize helper and acceptance fixtures for both commands.
Show a summary per file
File Description
pkg/cmd/repo/repo.go Registers the new read-file/read-dir commands under gh repo.
pkg/cmd/repo/read-file/read_file.go Implements command parsing and output modes for reading a single file.
pkg/cmd/repo/read-file/http.go Adds REST Contents API fetching and decoding logic for read-file.
pkg/cmd/repo/read-file/read_file_test.go Unit tests for read-file behaviors (TTY/non-TTY, JSON, output writing, etc.).
pkg/cmd/repo/read-dir/read_dir.go Implements command parsing and output modes for listing a directory.
pkg/cmd/repo/read-dir/http.go Adds GraphQL-backed directory listing and entry modeling for read-dir.
pkg/cmd/repo/read-dir/read_dir_test.go Unit tests for read-dir behaviors (TTY/non-TTY, JSON, errors, etc.).
internal/text/text.go Adds FormatSize helper for human-friendly byte formatting.
internal/text/text_test.go Adds tests for FormatSize.
acceptance/testdata/repo/repo-read-file.txtar Adds acceptance coverage for repo read-file.
acceptance/testdata/repo/repo-read-dir.txtar Adds acceptance coverage for repo read-dir.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 11/11 changed files
  • Comments generated: 10

Comment thread pkg/cmd/repo/read-file/read_file.go Outdated
Comment thread pkg/cmd/repo/read-file/read_file.go
Comment thread pkg/cmd/repo/read-file/read_file.go
Comment thread pkg/cmd/repo/read-file/http.go
Comment thread pkg/cmd/repo/read-file/read_file.go Outdated
Comment thread pkg/cmd/repo/read-dir/http.go
Comment thread pkg/cmd/repo/read-dir/http.go
Comment thread internal/text/text.go Outdated
Comment thread internal/text/text.go Outdated
Comment thread internal/text/text_test.go
babakks and others added 2 commits June 16, 2026 15:02
@babakks

babakks commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

I just pushed a commit to add the new commands in gh's skill file.

babakks and others added 5 commits June 16, 2026 15:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>

This comment was marked as spam.

Avoids an out-of-range panic for byte counts at or above one exabyte, which
fit within int64, and lets the function accept the full int64 range.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@BagToad BagToad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! 🔥 🚀

Comment thread pkg/cmd/repo/read-file/read_file.go
Comment thread skills/gh/SKILL.md Outdated
Comment thread pkg/cmd/repo/read-file/read_file.go Outdated
Comment thread pkg/cmd/repo/read-file/read_file.go
Comment thread pkg/cmd/repo/read-file/read_file.go
Signed-off-by: Babak K. Shandiz <babakks@github.com>
Signed-off-by: Babak K. Shandiz <babakks@github.com>
@BagToad BagToad merged commit 733e35c into trunk Jun 17, 2026
11 checks passed
@BagToad BagToad deleted the babakks/feature-repo-read branch June 17, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants