diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 71c75267f..16d793979 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -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" @@ -28,6 +29,7 @@ type BrowseOptions struct { SelectorArg string Branch string + CommitFlag bool ProjectsFlag bool SettingsFlag bool WikiFlag bool @@ -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, @@ -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 @@ -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 diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 0cb4df786..e342cde68 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -3,6 +3,7 @@ package browse import ( "fmt" "net/http" + "os" "testing" "github.com/cli/cli/v2/internal/ghrepo" @@ -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) { @@ -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 @@ -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 {