1- package command
1+ package checkout
22
33import (
44 "errors"
55 "fmt"
6+ "net/http"
67 "os"
78 "os/exec"
89 "strings"
910
10- "github.com/spf13/cobra"
11-
1211 "github.com/cli/cli/api"
12+ "github.com/cli/cli/context"
1313 "github.com/cli/cli/git"
14+ "github.com/cli/cli/internal/config"
1415 "github.com/cli/cli/internal/ghrepo"
1516 "github.com/cli/cli/internal/run"
17+ "github.com/cli/cli/pkg/cmd/pr/shared"
18+ "github.com/cli/cli/pkg/cmdutil"
19+ "github.com/cli/cli/pkg/iostreams"
20+ "github.com/spf13/cobra"
1621)
1722
18- func prCheckout (cmd * cobra.Command , args []string ) error {
19- ctx := contextForCommand (cmd )
20- currentBranch , _ := ctx .Branch ()
21- remotes , err := ctx .Remotes ()
23+ type CheckoutOptions struct {
24+ HttpClient func () (* http.Client , error )
25+ Config func () (config.Config , error )
26+ IO * iostreams.IOStreams
27+ BaseRepo func () (ghrepo.Interface , error )
28+ Remotes func () (context.Remotes , error )
29+ Branch func () (string , error )
30+
31+ SelectorArg string
32+ }
33+
34+ func NewCmdCheckout (f * cmdutil.Factory , runF func (* CheckoutOptions ) error ) * cobra.Command {
35+ opts := & CheckoutOptions {
36+ IO : f .IOStreams ,
37+ HttpClient : f .HttpClient ,
38+ Config : f .Config ,
39+ BaseRepo : f .BaseRepo ,
40+ Remotes : f .Remotes ,
41+ Branch : f .Branch ,
42+ }
43+
44+ cmd := & cobra.Command {
45+ Use : "checkout {<number> | <url> | <branch>}" ,
46+ Short : "Check out a pull request in git" ,
47+ Args : func (cmd * cobra.Command , args []string ) error {
48+ if len (args ) == 0 {
49+ return & cmdutil.FlagError {Err : errors .New ("argument required" )}
50+ }
51+ return nil
52+ },
53+ RunE : func (cmd * cobra.Command , args []string ) error {
54+ if len (args ) > 0 {
55+ opts .SelectorArg = args [0 ]
56+ }
57+
58+ if runF != nil {
59+ return runF (opts )
60+ }
61+ return checkoutRun (opts )
62+ },
63+ }
64+
65+ return cmd
66+ }
67+
68+ func checkoutRun (opts * CheckoutOptions ) error {
69+ currentBranch , err := opts .Branch ()
2270 if err != nil {
2371 return err
2472 }
2573
26- apiClient , err := apiClientForContext ( ctx )
74+ remotes , err := opts . Remotes ( )
2775 if err != nil {
2876 return err
2977 }
3078
31- pr , baseRepo , err := prFromArgs ( ctx , apiClient , cmd , args )
79+ httpClient , err := opts . HttpClient ( )
3280 if err != nil {
3381 return err
3482 }
83+ apiClient := api .NewClientFromHTTP (httpClient )
84+
85+ pr , baseRepo , err := shared .PRFromArgs (apiClient , opts .BaseRepo , opts .Branch , opts .Remotes , opts .SelectorArg )
86+ if err != nil {
87+ return err
88+ }
89+
90+ cfg , err := opts .Config ()
91+ if err != nil {
92+ return err
93+ }
94+ protocol , _ := cfg .Get (baseRepo .RepoHost (), "git_protocol" )
3595
3696 baseRemote , _ := remotes .FindByRepo (baseRepo .RepoOwner (), baseRepo .RepoName ())
3797 // baseRemoteSpec is a repository URL or a remote name to be used in git fetch
38- baseURLOrName := formatRemoteURL ( cmd , baseRepo )
98+ baseURLOrName := ghrepo . FormatRemoteURL ( baseRepo , protocol )
3999 if baseRemote != nil {
40100 baseURLOrName = baseRemote .Name
41101 }
@@ -95,7 +155,7 @@ func prCheckout(cmd *cobra.Command, args []string) error {
95155 mergeRef := ref
96156 if pr .MaintainerCanModify {
97157 headRepo := ghrepo .NewWithHost (pr .HeadRepositoryOwner .Login , pr .HeadRepository .Name , baseRepo .RepoHost ())
98- remote = formatRemoteURL ( cmd , headRepo )
158+ remote = ghrepo . FormatRemoteURL ( headRepo , protocol )
99159 mergeRef = fmt .Sprintf ("refs/heads/%s" , pr .HeadRefName )
100160 }
101161 if mc , err := git .Config (fmt .Sprintf ("branch.%s.merge" , newBranchName )); err != nil || mc == "" {
@@ -115,15 +175,3 @@ func prCheckout(cmd *cobra.Command, args []string) error {
115175
116176 return nil
117177}
118-
119- var prCheckoutCmd = & cobra.Command {
120- Use : "checkout {<number> | <url> | <branch>}" ,
121- Short : "Check out a pull request in Git" ,
122- Args : func (cmd * cobra.Command , args []string ) error {
123- if len (args ) < 1 {
124- return errors .New ("requires a pull request number as an argument" )
125- }
126- return nil
127- },
128- RunE : prCheckout ,
129- }
0 commit comments