Skip to content

Commit 0a49631

Browse files
committed
added in the ability to view repository by commit hashes
1 parent 49ff0c6 commit 0a49631

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

pkg/cmd/browse/browse.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type BrowseOptions struct {
2727
SelectorArg string
2828

2929
Branch string
30+
SHA string
3031
ProjectsFlag bool
3132
SettingsFlag bool
3233
WikiFlag bool
@@ -102,6 +103,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
102103
cmd.Flags().BoolVarP(&opts.SettingsFlag, "settings", "s", false, "Open repository settings")
103104
cmd.Flags().BoolVarP(&opts.NoBrowserFlag, "no-browser", "n", false, "Print destination URL instead of opening the browser")
104105
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Select another branch by passing in the branch name")
106+
cmd.Flags().StringVarP(&opts.SHA, "sha", "a", "", "Select a commit by passing in the SHA hash")
105107

106108
return cmd
107109
}
@@ -121,15 +123,14 @@ func runBrowse(opts *BrowseOptions) error {
121123
if opts.SelectorArg == "" {
122124
if opts.ProjectsFlag {
123125
url += "/projects"
124-
}
125-
if opts.SettingsFlag {
126+
} else if opts.SettingsFlag {
126127
url += "/settings"
127-
}
128-
if opts.WikiFlag {
128+
} else if opts.WikiFlag {
129129
url += "/wiki"
130-
}
131-
if opts.Branch != "" {
130+
} else if opts.Branch != "" {
132131
url += "/tree/" + opts.Branch + "/"
132+
} else if opts.SHA != "" {
133+
url += "/tree/" + opts.SHA + "/"
133134
}
134135
} else {
135136
if isNumber(opts.SelectorArg) {
@@ -141,6 +142,8 @@ func runBrowse(opts *BrowseOptions) error {
141142
}
142143
if opts.Branch != "" {
143144
url += "/tree/" + opts.Branch + "/"
145+
} else if opts.SHA != "" {
146+
url += "/tree/" + opts.SHA + "/"
144147
} else {
145148
apiClient := api.NewClientFromHTTP(httpClient)
146149
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)

pkg/cmd/browse/browse_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ func TestNewCmdBrowse(t *testing.T) {
5858
},
5959
wantsErr: false,
6060
},
61+
{
62+
name: "SHA flag",
63+
cli: "--sha e32e640",
64+
wants: BrowseOptions{
65+
SHA: "e32e640",
66+
},
67+
wantsErr: false,
68+
},
69+
{
70+
name: "SHA flag no arg",
71+
cli: "-a",
72+
wantsErr: true,
73+
},
6174
{
6275
name: "branch flag",
6376
cli: "--branch main",
@@ -254,6 +267,25 @@ func Test_runBrowse(t *testing.T) {
254267
wantsErr: false,
255268
expectedURL: "https://github.com/mislav/will_paginate/tree/3-0-stable/init.rb#L6",
256269
},
270+
{
271+
name: "opening browser with SHA hash no args",
272+
opts: BrowseOptions{
273+
SHA: "162a1b2",
274+
},
275+
baseRepo: ghrepo.New("torvalds", "linux"),
276+
wantsErr: false,
277+
expectedURL: "https://github.com/torvalds/linux/tree/162a1b2/",
278+
},
279+
{
280+
name: "opening browser with SHA hash file arg",
281+
opts: BrowseOptions{
282+
SHA: "162a1b2",
283+
SelectorArg: "api/cache.go:32",
284+
},
285+
baseRepo: ghrepo.New("cli", "cli"),
286+
wantsErr: false,
287+
expectedURL: "https://github.com/cli/cli/tree/162a1b2/api/cache.go#L32",
288+
},
257289
}
258290

259291
for _, tt := range tests {

0 commit comments

Comments
 (0)