Skip to content

Commit 8962aee

Browse files
committed
changed functionality to open up last commit with -c / --commit for gh browse
1 parent 158a151 commit 8962aee

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

pkg/cmd/browse/browse.go

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

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

2930
Branch string
30-
Commit string
31+
CommitFlag bool
3132
ProjectsFlag bool
3233
SettingsFlag bool
3334
WikiFlag bool
@@ -46,7 +47,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
4647
Short: "Open the repository in the browser",
4748
Use: "browse [<number> | <path>]",
4849
Args: cobra.MaximumNArgs(1),
49-
Example: heredoc.Doc(`
50+
Example: heredoc.Doc(`
5051
$ gh browse
5152
#=> Open the home page of the current repository
5253
@@ -83,7 +84,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
8384
if err := cmdutil.MutuallyExclusive(
8485
"specify only one of `--branch`, `--commit`, `--projects`, `--wiki`, or `--settings`",
8586
opts.Branch != "",
86-
opts.Commit != "",
87+
opts.CommitFlag,
8788
opts.WikiFlag,
8889
opts.SettingsFlag,
8990
opts.ProjectsFlag,
@@ -103,8 +104,8 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
103104
cmd.Flags().BoolVarP(&opts.WikiFlag, "wiki", "w", false, "Open repository wiki")
104105
cmd.Flags().BoolVarP(&opts.SettingsFlag, "settings", "s", false, "Open repository settings")
105106
cmd.Flags().BoolVarP(&opts.NoBrowserFlag, "no-browser", "n", false, "Print destination URL instead of opening the browser")
107+
cmd.Flags().BoolVarP(&opts.CommitFlag, "commit", "c", false, "Open the last commit")
106108
cmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Select another branch by passing in the branch name")
107-
cmd.Flags().StringVarP(&opts.Commit, "commit", "c", "", "Select a commit by passing in the SHA hash")
108109

109110
return cmd
110111
}
@@ -130,8 +131,9 @@ func runBrowse(opts *BrowseOptions) error {
130131
url += "/wiki"
131132
} else if opts.Branch != "" {
132133
url += "/tree/" + opts.Branch + "/"
133-
} else if opts.Commit != "" {
134-
url += "/tree/" + opts.Commit + "/"
134+
} else if opts.CommitFlag {
135+
commit, _ := git.LastCommit()
136+
url += "/tree/" + commit.Sha + "/"
135137
}
136138
} else {
137139
if isNumber(opts.SelectorArg) {
@@ -141,10 +143,11 @@ func runBrowse(opts *BrowseOptions) error {
141143
if err != nil {
142144
return err
143145
}
144-
if opts.Branch != "" {
146+
if opts.CommitFlag {
147+
commit, _ := git.LastCommit()
148+
url += "/tree/" + commit.Sha + "/"
149+
} else if opts.Branch != "" {
145150
url += "/tree/" + opts.Branch + "/"
146-
} else if opts.Commit != "" {
147-
url += "/tree/" + opts.Commit + "/"
148151
} else {
149152
apiClient := api.NewClientFromHTTP(httpClient)
150153
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)

pkg/cmd/browse/browse_test.go

Lines changed: 28 additions & 27 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/internal/ghrepo"
@@ -58,24 +59,6 @@ func TestNewCmdBrowse(t *testing.T) {
5859
},
5960
wantsErr: false,
6061
},
61-
{
62-
name: "Commit flag",
63-
cli: "--commit e32e640",
64-
wants: BrowseOptions{
65-
Commit: "e32e640",
66-
},
67-
wantsErr: false,
68-
},
69-
{
70-
name: "Commit flag no arg",
71-
cli: "-c",
72-
wantsErr: true,
73-
},
74-
{
75-
name: "Multi flags",
76-
cli: "-c 1a2b3c -b trunk",
77-
wantsErr: true,
78-
},
7962
{
8063
name: "branch flag",
8164
cli: "--branch main",
@@ -120,6 +103,14 @@ func TestNewCmdBrowse(t *testing.T) {
120103
cli: "main.go main.go",
121104
wantsErr: true,
122105
},
106+
{
107+
name: "last commit flag",
108+
cli: "-c",
109+
wants: BrowseOptions{
110+
CommitFlag: true,
111+
},
112+
wantsErr: false,
113+
},
123114
}
124115
for _, tt := range tests {
125116
t.Run(tt.name, func(t *testing.T) {
@@ -151,7 +142,17 @@ func TestNewCmdBrowse(t *testing.T) {
151142
}
152143
}
153144

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+
154154
func Test_runBrowse(t *testing.T) {
155+
setGitDir(t, "../../../git/fixtures/simple.git")
155156
tests := []struct {
156157
name string
157158
opts BrowseOptions
@@ -273,23 +274,23 @@ func Test_runBrowse(t *testing.T) {
273274
expectedURL: "https://github.com/mislav/will_paginate/tree/3-0-stable/init.rb#L6",
274275
},
275276
{
276-
name: "opening browser with Commit hash no args",
277+
name: "open last commit",
277278
opts: BrowseOptions{
278-
Commit: "162a1b2",
279+
CommitFlag: true,
279280
},
280-
baseRepo: ghrepo.New("torvalds", "linux"),
281+
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
281282
wantsErr: false,
282-
expectedURL: "https://github.com/torvalds/linux/tree/162a1b2/",
283+
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/",
283284
},
284285
{
285-
name: "opening browser with commit hash file arg",
286+
name: "open last commit with a file",
286287
opts: BrowseOptions{
287-
Commit: "162a1b2",
288-
SelectorArg: "api/cache.go:32",
288+
CommitFlag: true,
289+
SelectorArg: "main.go",
289290
},
290-
baseRepo: ghrepo.New("cli", "cli"),
291+
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
291292
wantsErr: false,
292-
expectedURL: "https://github.com/cli/cli/tree/162a1b2/api/cache.go#L32",
293+
expectedURL: "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/main.go",
293294
},
294295
}
295296

0 commit comments

Comments
 (0)