added in the ability to view repository by commit hashes

This commit is contained in:
bchadwic 2021-07-05 14:57:44 -07:00
parent 49ff0c6530
commit 0a496317a5
2 changed files with 41 additions and 6 deletions

View file

@ -27,6 +27,7 @@ type BrowseOptions struct {
SelectorArg string
Branch string
SHA string
ProjectsFlag bool
SettingsFlag bool
WikiFlag bool
@ -102,6 +103,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
cmd.Flags().BoolVarP(&opts.SettingsFlag, "settings", "s", false, "Open repository settings")
cmd.Flags().BoolVarP(&opts.NoBrowserFlag, "no-browser", "n", false, "Print destination URL instead of opening the browser")
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Select another branch by passing in the branch name")
cmd.Flags().StringVarP(&opts.SHA, "sha", "a", "", "Select a commit by passing in the SHA hash")
return cmd
}
@ -121,15 +123,14 @@ func runBrowse(opts *BrowseOptions) error {
if opts.SelectorArg == "" {
if opts.ProjectsFlag {
url += "/projects"
}
if opts.SettingsFlag {
} else if opts.SettingsFlag {
url += "/settings"
}
if opts.WikiFlag {
} else if opts.WikiFlag {
url += "/wiki"
}
if opts.Branch != "" {
} else if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
} else if opts.SHA != "" {
url += "/tree/" + opts.SHA + "/"
}
} else {
if isNumber(opts.SelectorArg) {
@ -141,6 +142,8 @@ func runBrowse(opts *BrowseOptions) error {
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
} else if opts.SHA != "" {
url += "/tree/" + opts.SHA + "/"
} else {
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)

View file

@ -58,6 +58,19 @@ func TestNewCmdBrowse(t *testing.T) {
},
wantsErr: false,
},
{
name: "SHA flag",
cli: "--sha e32e640",
wants: BrowseOptions{
SHA: "e32e640",
},
wantsErr: false,
},
{
name: "SHA flag no arg",
cli: "-a",
wantsErr: true,
},
{
name: "branch flag",
cli: "--branch main",
@ -254,6 +267,25 @@ func Test_runBrowse(t *testing.T) {
wantsErr: false,
expectedURL: "https://github.com/mislav/will_paginate/tree/3-0-stable/init.rb#L6",
},
{
name: "opening browser with SHA hash no args",
opts: BrowseOptions{
SHA: "162a1b2",
},
baseRepo: ghrepo.New("torvalds", "linux"),
wantsErr: false,
expectedURL: "https://github.com/torvalds/linux/tree/162a1b2/",
},
{
name: "opening browser with SHA hash file arg",
opts: BrowseOptions{
SHA: "162a1b2",
SelectorArg: "api/cache.go:32",
},
baseRepo: ghrepo.New("cli", "cli"),
wantsErr: false,
expectedURL: "https://github.com/cli/cli/tree/162a1b2/api/cache.go#L32",
},
}
for _, tt := range tests {