diff --git a/pkg/cmd/repo/view/http.go b/pkg/cmd/repo/view/http.go index 9567dc1de..b7e3096ce 100644 --- a/pkg/cmd/repo/view/http.go +++ b/pkg/cmd/repo/view/http.go @@ -17,14 +17,14 @@ type RepoReadme struct { Content string } -func RepositoryReadme(client *http.Client, repo ghrepo.Interface) (*RepoReadme, error) { +func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string) (*RepoReadme, error) { apiClient := api.NewClientFromHTTP(client) var response struct { Name string Content string } - err := apiClient.REST(repo.RepoHost(), "GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response) + err := apiClient.REST(repo.RepoHost(), "GET", getReadmePath(repo, branch), nil, &response) if err != nil { var httpError api.HTTPError if errors.As(err, &httpError) && httpError.StatusCode == 404 { @@ -43,3 +43,11 @@ func RepositoryReadme(client *http.Client, repo ghrepo.Interface) (*RepoReadme, Content: string(decoded), }, nil } + +func getReadmePath(repo ghrepo.Interface, branch string) string { + path := fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)) + if branch != "" { + path = fmt.Sprintf("%s?ref=%s", path, branch) + } + return path +} diff --git a/pkg/cmd/repo/view/view.go b/pkg/cmd/repo/view/view.go index 95da1ad0c..73051501a 100644 --- a/pkg/cmd/repo/view/view.go +++ b/pkg/cmd/repo/view/view.go @@ -25,6 +25,7 @@ type ViewOptions struct { RepoArg string Web bool + Branch string } func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { @@ -41,7 +42,9 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman With no argument, the repository for the current directory is displayed. -With '--web', open the repository in a web browser instead.`, +With '--web', open the repository in a web browser instead. + +With '--branch', view a specific branch of the repository.`, Args: cobra.MaximumNArgs(1), RunE: func(c *cobra.Command, args []string) error { if len(args) > 0 { @@ -55,6 +58,7 @@ With '--web', open the repository in a web browser instead.`, } cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open a repository in the browser") + cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "View a specific branch of the repository") return cmd } @@ -104,7 +108,7 @@ func viewRun(opts *ViewOptions) error { fullName := ghrepo.FullName(toView) - readme, err := RepositoryReadme(httpClient, toView) + readme, err := RepositoryReadme(httpClient, toView, opts.Branch) if err != nil && err != NotFoundError { return err } diff --git a/pkg/cmd/repo/view/view_test.go b/pkg/cmd/repo/view/view_test.go index 55e8ace6b..aeb8ae9b7 100644 --- a/pkg/cmd/repo/view/view_test.go +++ b/pkg/cmd/repo/view/view_test.go @@ -47,6 +47,14 @@ func TestNewCmdView(t *testing.T) { Web: true, }, }, + { + name: "sets branch", + cli: "-b feat/awesome", + wants: ViewOptions{ + RepoArg: "", + Branch: "feat/awesome", + }, + }, } for _, tt := range tests { @@ -80,6 +88,7 @@ func TestNewCmdView(t *testing.T) { assert.NoError(t, err) assert.Equal(t, tt.wants.Web, gotOpts.Web) + assert.Equal(t, tt.wants.Branch, gotOpts.Branch) assert.Equal(t, tt.wants.RepoArg, gotOpts.RepoArg) }) } @@ -198,6 +207,24 @@ func Test_ViewRun(t *testing.T) { View this repository on GitHub: https://github.com/jill/valentine `), }, + { + name: "branch arg", + opts: &ViewOptions{ + Branch: "feat/awesome", + }, + stdoutTTY: true, + wantOut: heredoc.Doc(` + OWNER/REPO + social distancing + + + # truly cool readme check it out + + + + View this repository on GitHub: https://github.com/OWNER/REPO + `), + }, { name: "no args", stdoutTTY: true,