MCPcopy Create free account
hub / github.com/cli/cli / NewCmdCheckout

Function NewCmdCheckout

pkg/cmd/pr/checkout/checkout.go:40–110  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*CheckoutOptions) error)

Source from the content-addressed store, hash-verified

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

Callers 2

TestNewCmdCheckoutFunction · 0.85
runCommandFunction · 0.85

Calls 7

FlagErrorfFunction · 0.92
NewFinderFunction · 0.92
NewListerFunction · 0.92
checkoutRunFunction · 0.85
ChangedMethod · 0.80
CanPromptMethod · 0.80
BaseRepoMethod · 0.65

Tested by 2

TestNewCmdCheckoutFunction · 0.68
runCommandFunction · 0.68