changed functionality to open up last commit with -c / --commit for gh browse
This commit is contained in:
parent
158a15160d
commit
8962aeebf9
2 changed files with 40 additions and 36 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/api"
|
||||
"github.com/cli/cli/git"
|
||||
"github.com/cli/cli/internal/ghrepo"
|
||||
"github.com/cli/cli/pkg/cmdutil"
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
|
|
@ -27,7 +28,7 @@ type BrowseOptions struct {
|
|||
SelectorArg string
|
||||
|
||||
Branch string
|
||||
Commit string
|
||||
CommitFlag bool
|
||||
ProjectsFlag bool
|
||||
SettingsFlag bool
|
||||
WikiFlag bool
|
||||
|
|
@ -46,7 +47,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
|
|||
Short: "Open the repository in the browser",
|
||||
Use: "browse [<number> | <path>]",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Example: heredoc.Doc(`
|
||||
Example: heredoc.Doc(`
|
||||
$ gh browse
|
||||
#=> Open the home page of the current repository
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
|
|||
if err := cmdutil.MutuallyExclusive(
|
||||
"specify only one of `--branch`, `--commit`, `--projects`, `--wiki`, or `--settings`",
|
||||
opts.Branch != "",
|
||||
opts.Commit != "",
|
||||
opts.CommitFlag,
|
||||
opts.WikiFlag,
|
||||
opts.SettingsFlag,
|
||||
opts.ProjectsFlag,
|
||||
|
|
@ -103,8 +104,8 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
|
|||
cmd.Flags().BoolVarP(&opts.WikiFlag, "wiki", "w", false, "Open repository wiki")
|
||||
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().BoolVarP(&opts.CommitFlag, "commit", "c", false, "Open the last commit")
|
||||
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Select another branch by passing in the branch name")
|
||||
cmd.Flags().StringVarP(&opts.Commit, "commit", "c", "", "Select a commit by passing in the SHA hash")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -130,8 +131,9 @@ func runBrowse(opts *BrowseOptions) error {
|
|||
url += "/wiki"
|
||||
} else if opts.Branch != "" {
|
||||
url += "/tree/" + opts.Branch + "/"
|
||||
} else if opts.Commit != "" {
|
||||
url += "/tree/" + opts.Commit + "/"
|
||||
} else if opts.CommitFlag {
|
||||
commit, _ := git.LastCommit()
|
||||
url += "/tree/" + commit.Sha + "/"
|
||||
}
|
||||
} else {
|
||||
if isNumber(opts.SelectorArg) {
|
||||
|
|
@ -141,10 +143,11 @@ func runBrowse(opts *BrowseOptions) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.Branch != "" {
|
||||
if opts.CommitFlag {
|
||||
commit, _ := git.LastCommit()
|
||||
url += "/tree/" + commit.Sha + "/"
|
||||
} else if opts.Branch != "" {
|
||||
url += "/tree/" + opts.Branch + "/"
|
||||
} else if opts.Commit != "" {
|
||||
url += "/tree/" + opts.Commit + "/"
|
||||
} else {
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package browse
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/cli/cli/internal/ghrepo"
|
||||
|
|
@ -58,24 +59,6 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "Commit flag",
|
||||
cli: "--commit e32e640",
|
||||
wants: BrowseOptions{
|
||||
Commit: "e32e640",
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "Commit flag no arg",
|
||||
cli: "-c",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "Multi flags",
|
||||
cli: "-c 1a2b3c -b trunk",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "branch flag",
|
||||
cli: "--branch main",
|
||||
|
|
@ -120,6 +103,14 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
cli: "main.go main.go",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "last commit flag",
|
||||
cli: "-c",
|
||||
wants: BrowseOptions{
|
||||
CommitFlag: true,
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
@ -151,7 +142,17 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func setGitDir(t *testing.T, dir string) {
|
||||
// taken from git_test.go
|
||||
old_GIT_DIR := os.Getenv("GIT_DIR")
|
||||
os.Setenv("GIT_DIR", dir)
|
||||
t.Cleanup(func() {
|
||||
os.Setenv("GIT_DIR", old_GIT_DIR)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_runBrowse(t *testing.T) {
|
||||
setGitDir(t, "../../../git/fixtures/simple.git")
|
||||
tests := []struct {
|
||||
name string
|
||||
opts BrowseOptions
|
||||
|
|
@ -273,23 +274,23 @@ func Test_runBrowse(t *testing.T) {
|
|||
expectedURL: "https://github.com/mislav/will_paginate/tree/3-0-stable/init.rb#L6",
|
||||
},
|
||||
{
|
||||
name: "opening browser with Commit hash no args",
|
||||
name: "open last commit",
|
||||
opts: BrowseOptions{
|
||||
Commit: "162a1b2",
|
||||
CommitFlag: true,
|
||||
},
|
||||
baseRepo: ghrepo.New("torvalds", "linux"),
|
||||
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
|
||||
wantsErr: false,
|
||||
expectedURL: "https://github.com/torvalds/linux/tree/162a1b2/",
|
||||
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/",
|
||||
},
|
||||
{
|
||||
name: "opening browser with commit hash file arg",
|
||||
name: "open last commit with a file",
|
||||
opts: BrowseOptions{
|
||||
Commit: "162a1b2",
|
||||
SelectorArg: "api/cache.go:32",
|
||||
CommitFlag: true,
|
||||
SelectorArg: "main.go",
|
||||
},
|
||||
baseRepo: ghrepo.New("cli", "cli"),
|
||||
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
|
||||
wantsErr: false,
|
||||
expectedURL: "https://github.com/cli/cli/tree/162a1b2/api/cache.go#L32",
|
||||
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/main.go",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue