feat: add repo read-file and repo read-dir#13580
Open
babakks wants to merge 12 commits into
Open
Conversation
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>
Contributor
There was a problem hiding this comment.
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-filefor fetching a single file’s metadata/content and optionally writing it to disk. - Add
gh repo read-dirfor listing a repository directory (TTY table / non-TTY TSV / JSON). - Add a shared
internal/text.FormatSizehelper 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 |
| // 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 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"}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds two new (preview) commands for reading repository contents without cloning.
gh repo read-fileReads a single file from a repository and writes it to stdout (or to a file with
--output).gh repo read-dirLists 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/--templatefor 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-filegh repo read-dir