Generate correct URL when branch option is passed in

This commit is contained in:
Alisson Santos 2020-11-13 10:20:30 +01:00
parent 6e1e62f496
commit 9ecc902959
2 changed files with 10 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"syscall"
"text/template"
@ -100,7 +101,7 @@ func viewRun(opts *ViewOptions) error {
return err
}
openURL := ghrepo.GenerateRepoURL(toView, "")
openURL := generateBranchURL(toView, opts.Branch)
if opts.Web {
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
@ -201,3 +202,10 @@ func isMarkdownFile(filename string) bool {
strings.HasSuffix(filename, ".mdown") ||
strings.HasSuffix(filename, ".mkdown")
}
func generateBranchURL(r ghrepo.Interface, branch string) string {
if branch == "" {
return ghrepo.GenerateRepoURL(r, "")
}
return ghrepo.GenerateRepoURL(r, "tree/%s", url.QueryEscape(branch))
}

View file

@ -222,7 +222,7 @@ func Test_ViewRun(t *testing.T) {
View this repository on GitHub: https://github.com/OWNER/REPO
View this repository on GitHub: https://github.com/OWNER/REPO/tree/feat%2Fawesome
`),
},
{