Skip to content

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

Open
babakks wants to merge 12 commits into
trunkfrom
babakks/feature-repo-read
Open

feat: add repo read-file and repo read-dir#13580
babakks wants to merge 12 commits into
trunkfrom
babakks/feature-repo-read

Conversation

@babakks
Copy link
Copy Markdown
Member

@babakks babakks commented Jun 3, 2026

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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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

Long: heredoc.Docf(`
Read the contents of a file in a GitHub repository without cloning it.

This command is in preview and and subject to change without notice.
Comment on lines +253 to +260
if lr, err := lstatF(dest); err == nil {
if lr.isSymlink {
return "", fmt.Errorf("output path is a symlink")
}
if lr.isDir {
asDir = true
}
}
Comment on lines +266 to +273
if lr, err := lstatF(dest); err == nil {
if lr.isSymlink {
return "", fmt.Errorf("output path is a symlink")
}
if !clobber {
return "", fmt.Errorf("output path already exists: %q (use --clobber to overwrite)", dest)
}
}
Comment on lines +160 to +166
if content.Encoding == "base64" && content.Content != "" {
decoded, err := base64.StdEncoding.DecodeString(content.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode base64 file content: %w", err)
}
file.Content = decoded
}
Comment on lines +182 to +183
// Refuse terminal escape sequences unless --unsafe, in both TTY and non-TTY modes,
// so a malicious file cannot manipulate a downstream terminal.
Comment on lines +148 to +149
NameRaw string
PathRaw string
Type string
Mode int
OID string `graphql:"oid"`
Size int
Comment thread internal/text/text.go
// FormatSize formats a byte count using binary units (B, KB, MB, GB, TB, PB).
// Values below a kilobyte are shown as whole bytes; larger values are shown with
// one decimal place of precision.
func FormatSize(n int) string {
Comment thread internal/text/text.go
Comment on lines +168 to +170
units := []string{"KB", "MB", "GB", "TB", "PB"}
value := float64(n) / float64(div)
return fmt.Sprintf("%.1f %s", value, units[exp])
Comment on lines +186 to +205
tests := []struct {
n int
want string
}{
{0, "0 B"},
{1, "1 B"},
{512, "512 B"},
{1023, "1023 B"},
{1024, "1.0 KB"},
{1536, "1.5 KB"},
{2048, "2.0 KB"},
{10240, "10.0 KB"},
{524288, "512.0 KB"},
{1048576, "1.0 MB"},
{1572864, "1.5 MB"},
{5242880, "5.0 MB"},
{1073741824, "1.0 GB"},
{1610612736, "1.5 GB"},
{1099511627776, "1.0 TB"},
{1125899906842624, "1.0 PB"},
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.

2 participants