X Tutup
Skip to content

Commit 381d1a1

Browse files
committed
Added option to specify branch in gh repo view
1 parent 9a8deb0 commit 381d1a1

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

pkg/cmd/repo/view/http.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ type RepoReadme struct {
1717
Content string
1818
}
1919

20-
func RepositoryReadme(client *http.Client, repo ghrepo.Interface) (*RepoReadme, error) {
20+
func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string) (*RepoReadme, error) {
2121
apiClient := api.NewClientFromHTTP(client)
2222
var response struct {
2323
Name string
2424
Content string
2525
}
2626

27-
err := apiClient.REST(repo.RepoHost(), "GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response)
27+
err := apiClient.REST(repo.RepoHost(), "GET", getReadmePath(repo, branch), nil, &response)
2828
if err != nil {
2929
var httpError api.HTTPError
3030
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
@@ -43,3 +43,11 @@ func RepositoryReadme(client *http.Client, repo ghrepo.Interface) (*RepoReadme,
4343
Content: string(decoded),
4444
}, nil
4545
}
46+
47+
func getReadmePath(repo ghrepo.Interface, branch string) string {
48+
path := fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo))
49+
if branch != "" {
50+
path = fmt.Sprintf("%s?ref=%s", path, branch)
51+
}
52+
return path
53+
}

pkg/cmd/repo/view/view.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ViewOptions struct {
2525

2626
RepoArg string
2727
Web bool
28+
Branch string
2829
}
2930

3031
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
4142
4243
With no argument, the repository for the current directory is displayed.
4344
44-
With '--web', open the repository in a web browser instead.`,
45+
With '--web', open the repository in a web browser instead.
46+
47+
With '--branch', view a specific branch of the repository.`,
4548
Args: cobra.MaximumNArgs(1),
4649
RunE: func(c *cobra.Command, args []string) error {
4750
if len(args) > 0 {
@@ -55,6 +58,7 @@ With '--web', open the repository in a web browser instead.`,
5558
}
5659

5760
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open a repository in the browser")
61+
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "View a specific branch of the repository")
5862

5963
return cmd
6064
}
@@ -104,7 +108,7 @@ func viewRun(opts *ViewOptions) error {
104108

105109
fullName := ghrepo.FullName(toView)
106110

107-
readme, err := RepositoryReadme(httpClient, toView)
111+
readme, err := RepositoryReadme(httpClient, toView, opts.Branch)
108112
if err != nil && err != NotFoundError {
109113
return err
110114
}

pkg/cmd/repo/view/view_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ func TestNewCmdView(t *testing.T) {
4747
Web: true,
4848
},
4949
},
50+
{
51+
name: "sets branch",
52+
cli: "-b feat/awesome",
53+
wants: ViewOptions{
54+
RepoArg: "",
55+
Branch: "feat/awesome",
56+
},
57+
},
5058
}
5159

5260
for _, tt := range tests {
@@ -80,6 +88,7 @@ func TestNewCmdView(t *testing.T) {
8088
assert.NoError(t, err)
8189

8290
assert.Equal(t, tt.wants.Web, gotOpts.Web)
91+
assert.Equal(t, tt.wants.Branch, gotOpts.Branch)
8392
assert.Equal(t, tt.wants.RepoArg, gotOpts.RepoArg)
8493
})
8594
}
@@ -198,6 +207,24 @@ func Test_ViewRun(t *testing.T) {
198207
View this repository on GitHub: https://github.com/jill/valentine
199208
`),
200209
},
210+
{
211+
name: "branch arg",
212+
opts: &ViewOptions{
213+
Branch: "feat/awesome",
214+
},
215+
stdoutTTY: true,
216+
wantOut: heredoc.Doc(`
217+
OWNER/REPO
218+
social distancing
219+
220+
221+
# truly cool readme check it out
222+
223+
224+
225+
View this repository on GitHub: https://github.com/OWNER/REPO
226+
`),
227+
},
201228
{
202229
name: "no args",
203230
stdoutTTY: true,

0 commit comments

Comments
 (0)
X Tutup