| 40 | } |
| 41 | |
| 42 | func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobra.Command { |
| 43 | opts := &CheckoutOptions{ |
| 44 | IO: f.IOStreams, |
| 45 | HttpClient: f.HttpClient, |
| 46 | GitClient: f.GitClient, |
| 47 | Config: f.Config, |
| 48 | Remotes: f.Remotes, |
| 49 | Branch: f.Branch, |
| 50 | } |
| 51 | |
| 52 | cmd := &cobra.Command{ |
| 53 | Use: "checkout [<number> | <url> | <branch>]", |
| 54 | Short: "Check out a pull request in git", |
| 55 | Example: heredoc.Doc(` |
| 56 | # Interactively select a PR from the 10 most recent to check out |
| 57 | $ gh pr checkout |
| 58 | |
| 59 | # Checkout a specific PR |
| 60 | $ gh pr checkout 32 |
| 61 | $ gh pr checkout https://github.com/OWNER/REPO/pull/32 |
| 62 | $ gh pr checkout feature |
| 63 | $ gh pr checkout 32 --branch feature --worktree /path/to/wt-feature |
| 64 | `), |
| 65 | Args: cobra.MaximumNArgs(1), |
| 66 | Aliases: []string{"co"}, |
| 67 | RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | if cmd.Flags().Changed("worktree") && opts.Worktree == "" { |
| 69 | return cmdutil.FlagErrorf("--worktree cannot be blank") |
| 70 | } |
| 71 | |
| 72 | if len(args) > 0 { |
| 73 | opts.PRResolver = &specificPRResolver{ |
| 74 | prFinder: shared.NewFinder(f), |
| 75 | selector: args[0], |
| 76 | } |
| 77 | } else if opts.IO.CanPrompt() { |
| 78 | baseRepo, err := f.BaseRepo() |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | |
| 83 | httpClient, err := f.HttpClient() |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | opts.PRResolver = &promptingPRResolver{ |
| 89 | io: opts.IO, |
| 90 | prompter: f.Prompter, |
| 91 | prLister: shared.NewLister(httpClient), |
| 92 | baseRepo: baseRepo, |
| 93 | } |
| 94 | } else { |
| 95 | return cmdutil.FlagErrorf("pull request number, URL, or branch required when not running interactively") |
| 96 | } |
| 97 | |
| 98 | if runF != nil { |
| 99 | return runF(opts) |