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

Function cmdsForExistingRemote

pkg/cmd/pr/checkout/checkout.go:203–249  ·  view source on GitHub ↗
(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions, reuseWorktree bool)

Source from the content-addressed store, hash-verified

201}
202
203func cmdsForExistingRemote(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions, reuseWorktree bool) [][]string {
204 var cmds [][]string
205 remoteBranch := fmt.Sprintf("%s/%s", remote.Name, pr.HeadRefName)
206
207 refSpec := fmt.Sprintf("+refs/heads/%s", pr.HeadRefName)
208 if !opts.Detach {
209 refSpec += fmt.Sprintf(":refs/remotes/%s", remoteBranch)
210 }
211
212 localBranch := pr.HeadRefName
213 if opts.BranchName != "" {
214 localBranch = opts.BranchName
215 }
216
217 remoteBranchRef := fmt.Sprintf("refs/remotes/%s", remoteBranch)
218 fetchCmd := []string{"fetch", remote.Name, refSpec, "--no-tags"}
219
220 if opts.Detach {
221 return append(cmds, detachCmds(fetchCmd, opts.Worktree, reuseWorktree)...)
222 }
223
224 cmds = append(cmds, fetchCmd)
225
226 switch {
227 case opts.Worktree != "":
228 if reuseWorktree {
229 if localBranchExists(opts.GitClient, localBranch) {
230 cmds = append(cmds, worktreeCheckoutCmds(opts.Worktree, localBranch, remoteBranchRef, opts.Force)...)
231 } else {
232 // New --branch name while reusing a worktree: create it tracking the remote.
233 cmds = append(cmds, []string{"-C", opts.Worktree, "checkout", "-b", localBranch, "--track", remoteBranch})
234 }
235 } else if localBranchExists(opts.GitClient, localBranch) {
236 cmds = append(cmds, []string{"worktree", "add", "--", opts.Worktree, localBranch})
237 cmds = append(cmds, syncBranchCmds(opts.Worktree, remoteBranchRef, opts.Force)...)
238 } else {
239 cmds = append(cmds, []string{"worktree", "add", "--track", "-b", localBranch, "--", opts.Worktree, remoteBranch})
240 }
241 case localBranchExists(opts.GitClient, localBranch):
242 cmds = append(cmds, []string{"checkout", localBranch})
243 cmds = append(cmds, syncBranchCmds("", remoteBranchRef, opts.Force)...)
244 default:
245 cmds = append(cmds, []string{"checkout", "-b", localBranch, "--track", remoteBranch})
246 }
247
248 return cmds
249}
250
251func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions, reuseWorktree bool) [][]string {
252 var cmds [][]string

Callers 1

checkoutRunFunction · 0.85

Calls 4

detachCmdsFunction · 0.85
localBranchExistsFunction · 0.85
worktreeCheckoutCmdsFunction · 0.85
syncBranchCmdsFunction · 0.85

Tested by

no test coverage detected