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 )
0 commit comments