Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pkg/cmd/browse/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
Expand All @@ -28,6 +29,7 @@ type BrowseOptions struct {
SelectorArg string

Branch string
CommitFlag bool
ProjectsFlag bool
SettingsFlag bool
WikiFlag bool
Expand Down Expand Up @@ -81,8 +83,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
}

if err := cmdutil.MutuallyExclusive(
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
"specify only one of `--branch`, `--commit`, `--projects`, `--wiki`, or `--settings`",
opts.Branch != "",
opts.CommitFlag,
opts.WikiFlag,
opts.SettingsFlag,
opts.ProjectsFlag,
Expand All @@ -102,6 +105,7 @@ 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")

return cmd
Expand All @@ -113,6 +117,13 @@ func runBrowse(opts *BrowseOptions) error {
return fmt.Errorf("unable to determine base repository: %w", err)
}

if opts.CommitFlag {
commit, err := git.LastCommit()
if err == nil {
opts.Branch = commit.Sha
}
}

section, err := parseSection(baseRepo, opts)
if err != nil {
return err
Expand Down
38 changes: 38 additions & 0 deletions pkg/cmd/browse/browse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package browse
import (
"fmt"
"net/http"
"os"
"testing"

"github.com/cli/cli/v2/internal/ghrepo"
Expand Down Expand Up @@ -102,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) {
Expand Down Expand Up @@ -133,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
Expand Down Expand Up @@ -296,6 +315,25 @@ func Test_runBrowse(t *testing.T) {
wantsErr: false,
expectedURL: "https://github.com/mislav/will_paginate/blob/3-0-stable/init.rb?plain=1#L6",
},
{
name: "open last commit",
opts: BrowseOptions{
CommitFlag: true,
},
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
wantsErr: false,
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/",
},
{
name: "open last commit with a file",
opts: BrowseOptions{
CommitFlag: true,
SelectorArg: "main.go",
},
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
wantsErr: false,
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/main.go",
},
}

for _, tt := range tests {
Expand Down