Enable browsing to commit page (#5729)

This commit is contained in:
Yuta Iwama 2022-08-15 20:43:11 +09:00 committed by GitHub
parent d21d388b8d
commit a447c078cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"net/url"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
@ -62,6 +63,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
$ gh browse 217
#=> Open issue or pull request 217
$ gh browse 77507cd94ccafcf568f8560cfecde965fcfa63
#=> Open commit page
$ gh browse --settings
#=> Open repository settings
@ -169,6 +173,10 @@ func parseSection(baseRepo ghrepo.Interface, opts *BrowseOptions) (string, error
return fmt.Sprintf("issues/%s", opts.SelectorArg), nil
}
if isCommit(opts.SelectorArg) {
return fmt.Sprintf("commit/%s", opts.SelectorArg), nil
}
filePath, rangeStart, rangeEnd, err := parseFile(*opts, opts.SelectorArg)
if err != nil {
return "", err
@ -252,6 +260,13 @@ func isNumber(arg string) bool {
return err == nil
}
// sha1 and sha256 are supported
var commitHash = regexp.MustCompile(`\A[a-f0-9]{7,64}\z`)
func isCommit(arg string) bool {
return commitHash.MatchString(arg)
}
// gitClient is used to implement functions that can be performed on both local and remote git repositories
type gitClient interface {
LastCommit() (*git.Commit, error)

View file

@ -414,6 +414,35 @@ func Test_runBrowse(t *testing.T) {
expectedURL: "https://github.com/bchadwic/test/blob/branch/with%20spaces%3F/%3F=hello%20world/%20%2A?plain=1#L23-L44",
wantsErr: false,
},
{
name: "commit hash in selector arg",
opts: BrowseOptions{
SelectorArg: "77507cd94ccafcf568f8560cfecde965fcfa63e7",
},
baseRepo: ghrepo.New("bchadwic", "test"),
expectedURL: "https://github.com/bchadwic/test/commit/77507cd94ccafcf568f8560cfecde965fcfa63e7",
wantsErr: false,
},
{
name: "short commit hash in selector arg",
opts: BrowseOptions{
SelectorArg: "6e3689d5",
},
baseRepo: ghrepo.New("bchadwic", "test"),
expectedURL: "https://github.com/bchadwic/test/commit/6e3689d5",
wantsErr: false,
},
{
name: "commit hash with extension",
opts: BrowseOptions{
SelectorArg: "77507cd94ccafcf568f8560cfecde965fcfa63e7.txt",
Branch: "trunk",
},
baseRepo: ghrepo.New("bchadwic", "test"),
expectedURL: "https://github.com/bchadwic/test/tree/trunk/77507cd94ccafcf568f8560cfecde965fcfa63e7.txt",
wantsErr: false,
},
}
for _, tt := range tests {