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

Function checkoutRun

pkg/cmd/pr/checkout/checkout.go:112–197  ·  view source on GitHub ↗
(opts *CheckoutOptions)

Source from the content-addressed store, hash-verified

110}
111
112func checkoutRun(opts *CheckoutOptions) error {
113 pr, baseRepo, err := opts.PRResolver.Resolve()
114 if err != nil {
115 return err
116 }
117
118 var reuseWorktree bool
119 if opts.Worktree != "" {
120 target, err := shared.ResolveWorktreeTarget(opts.GitClient, opts.Worktree)
121 if err != nil {
122 return err
123 }
124 reuseWorktree = target.Reuse
125 }
126
127 cfg, err := opts.Config()
128 if err != nil {
129 return err
130 }
131 protocol := cfg.GitProtocol(baseRepo.RepoHost()).Value
132
133 remotes, err := opts.Remotes()
134 if err != nil {
135 return err
136 }
137 baseRemote, _ := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName())
138 baseURLOrName := ghrepo.FormatRemoteURL(baseRepo, protocol)
139 if baseRemote != nil {
140 baseURLOrName = baseRemote.Name
141 }
142
143 headRemote := baseRemote
144 if pr.HeadRepository == nil {
145 headRemote = nil
146 } else if pr.IsCrossRepository {
147 headRemote, _ = remotes.FindByRepo(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name)
148 }
149
150 if strings.HasPrefix(pr.HeadRefName, "-") {
151 return fmt.Errorf("invalid branch name: %q", pr.HeadRefName)
152 }
153
154 var cmdQueue [][]string
155
156 if headRemote != nil {
157 cmdQueue = append(cmdQueue, cmdsForExistingRemote(headRemote, pr, opts, reuseWorktree)...)
158 } else {
159 httpClient, err := opts.HttpClient()
160 if err != nil {
161 return err
162 }
163 apiClient := api.NewClientFromHTTP(httpClient)
164
165 defaultBranch, err := api.RepoDefaultBranch(apiClient, baseRepo)
166 if err != nil {
167 return err
168 }
169 cmdQueue = append(cmdQueue, cmdsForMissingRemote(pr, baseURLOrName, baseRepo.RepoHost(), defaultBranch, protocol, opts, reuseWorktree)...)

Callers 2

NewCmdCheckoutFunction · 0.85
Test_checkoutRunFunction · 0.85

Calls 15

ResolveWorktreeTargetFunction · 0.92
FormatRemoteURLFunction · 0.92
NewClientFromHTTPFunction · 0.92
RepoDefaultBranchFunction · 0.92
cmdsForExistingRemoteFunction · 0.85
cmdsForMissingRemoteFunction · 0.85
executeCmdsFunction · 0.85
FindByRepoMethod · 0.80
IsStdoutTTYMethod · 0.80
ColorSchemeMethod · 0.80
SuccessIconMethod · 0.80

Tested by 1

Test_checkoutRunFunction · 0.68