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