Merge pull request #2404 from alissonbrunosa/fix-repo-view-command-with-branch

Generate correct URL when branch option is passed in
This commit is contained in:
Mislav Marohnić 2020-11-13 13:05:56 +01:00 committed by GitHub
commit 6a0d38df44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 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,11 @@ 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
`),
},
{