Skip to content

Commit 78ac771

Browse files
author
Nate Smith
authored
Merge pull request cli#3950 from bchadwic/browse-commit
Add feature open latest commit in gh browse
2 parents b0905a4 + 5bdaab8 commit 78ac771

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/cmd/browse/browse.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010
"github.com/cli/cli/v2/api"
11+
"github.com/cli/cli/v2/git"
1112
"github.com/cli/cli/v2/internal/ghrepo"
1213
"github.com/cli/cli/v2/pkg/cmdutil"
1314
"github.com/cli/cli/v2/pkg/iostreams"
@@ -28,6 +29,7 @@ type BrowseOptions struct {
2829
SelectorArg string
2930

3031
Branch string
32+
CommitFlag bool
3133
ProjectsFlag bool
3234
SettingsFlag bool
3335
WikiFlag bool
@@ -81,8 +83,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
8183
}
8284

8385
if err := cmdutil.MutuallyExclusive(
84-
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
86+
"specify only one of `--branch`, `--commit`, `--projects`, `--wiki`, or `--settings`",
8587
opts.Branch != "",
88+
opts.CommitFlag,
8689
opts.WikiFlag,
8790
opts.SettingsFlag,
8891
opts.ProjectsFlag,
@@ -102,6 +105,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
102105
cmd.Flags().BoolVarP(&opts.WikiFlag, "wiki", "w", false, "Open repository wiki")
103106
cmd.Flags().BoolVarP(&opts.SettingsFlag, "settings", "s", false, "Open repository settings")
104107
cmd.Flags().BoolVarP(&opts.NoBrowserFlag, "no-browser", "n", false, "Print destination URL instead of opening the browser")
108+
cmd.Flags().BoolVarP(&opts.CommitFlag, "commit", "c", false, "Open the last commit")
105109
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Select another branch by passing in the branch name")
106110

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

120+
if opts.CommitFlag {
121+
commit, err := git.LastCommit()
122+
if err == nil {
123+
opts.Branch = commit.Sha
124+
}
125+
}
126+
116127
section, err := parseSection(baseRepo, opts)
117128
if err != nil {
118129
return err

pkg/cmd/browse/browse_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package browse
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67
"testing"
78

89
"github.com/cli/cli/v2/internal/ghrepo"
@@ -102,6 +103,14 @@ func TestNewCmdBrowse(t *testing.T) {
102103
cli: "main.go main.go",
103104
wantsErr: true,
104105
},
106+
{
107+
name: "last commit flag",
108+
cli: "-c",
109+
wants: BrowseOptions{
110+
CommitFlag: true,
111+
},
112+
wantsErr: false,
113+
},
105114
}
106115
for _, tt := range tests {
107116
t.Run(tt.name, func(t *testing.T) {
@@ -133,7 +142,17 @@ func TestNewCmdBrowse(t *testing.T) {
133142
}
134143
}
135144

145+
func setGitDir(t *testing.T, dir string) {
146+
// taken from git_test.go
147+
old_GIT_DIR := os.Getenv("GIT_DIR")
148+
os.Setenv("GIT_DIR", dir)
149+
t.Cleanup(func() {
150+
os.Setenv("GIT_DIR", old_GIT_DIR)
151+
})
152+
}
153+
136154
func Test_runBrowse(t *testing.T) {
155+
setGitDir(t, "../../../git/fixtures/simple.git")
137156
tests := []struct {
138157
name string
139158
opts BrowseOptions
@@ -296,6 +315,25 @@ func Test_runBrowse(t *testing.T) {
296315
wantsErr: false,
297316
expectedURL: "https://github.com/mislav/will_paginate/blob/3-0-stable/init.rb?plain=1#L6",
298317
},
318+
{
319+
name: "open last commit",
320+
opts: BrowseOptions{
321+
CommitFlag: true,
322+
},
323+
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
324+
wantsErr: false,
325+
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/",
326+
},
327+
{
328+
name: "open last commit with a file",
329+
opts: BrowseOptions{
330+
CommitFlag: true,
331+
SelectorArg: "main.go",
332+
},
333+
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
334+
wantsErr: false,
335+
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/main.go",
336+
},
299337
}
300338

301339
for _, tt := range tests {

0 commit comments

Comments
 (0)