@@ -29,6 +29,7 @@ type BrowseOptions struct {
2929 HttpClient func () (* http.Client , error )
3030 IO * iostreams.IOStreams
3131 PathFromRepoRoot func () string
32+ GitClient gitClient
3233
3334 SelectorArg string
3435
@@ -46,6 +47,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
4647 HttpClient : f .HttpClient ,
4748 IO : f .IOStreams ,
4849 PathFromRepoRoot : git .PathFromRepoRoot ,
50+ GitClient : & localGitClient {},
4951 }
5052
5153 cmd := & cobra.Command {
@@ -97,6 +99,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
9799 ); err != nil {
98100 return err
99101 }
102+ if cmd .Flags ().Changed ("repo" ) {
103+ opts .GitClient = & remoteGitClient {opts .BaseRepo , opts .HttpClient }
104+ }
100105
101106 if runF != nil {
102107 return runF (opts )
@@ -123,10 +128,11 @@ func runBrowse(opts *BrowseOptions) error {
123128 }
124129
125130 if opts .CommitFlag {
126- commit , err := git .LastCommit ()
127- if err = = nil {
128- opts . Branch = commit . Sha
131+ commit , err := opts . GitClient .LastCommit ()
132+ if err ! = nil {
133+ return err
129134 }
135+ opts .Branch = commit .Sha
130136 }
131137
132138 section , err := parseSection (baseRepo , opts )
@@ -245,3 +251,33 @@ func isNumber(arg string) bool {
245251 _ , err := strconv .Atoi (arg )
246252 return err == nil
247253}
254+
255+ // gitClient is used to implement functions that can be performed on both local and remote git repositories
256+ type gitClient interface {
257+ LastCommit () (* git.Commit , error )
258+ }
259+
260+ type localGitClient struct {}
261+
262+ type remoteGitClient struct {
263+ repo func () (ghrepo.Interface , error )
264+ httpClient func () (* http.Client , error )
265+ }
266+
267+ func (gc * localGitClient ) LastCommit () (* git.Commit , error ) { return git .LastCommit () }
268+
269+ func (gc * remoteGitClient ) LastCommit () (* git.Commit , error ) {
270+ httpClient , err := gc .httpClient ()
271+ if err != nil {
272+ return nil , err
273+ }
274+ repo , err := gc .repo ()
275+ if err != nil {
276+ return nil , err
277+ }
278+ commit , err := api .LastCommit (api .NewClientFromHTTP (httpClient ), repo )
279+ if err != nil {
280+ return nil , err
281+ }
282+ return & git.Commit {Sha : commit .OID }, nil
283+ }
0 commit comments