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

Function cmdsForMissingRemote

pkg/cmd/pr/checkout/checkout.go:251–319  ·  view source on GitHub ↗
(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions, reuseWorktree bool)

Source from the content-addressed store, hash-verified

249}
250
251func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions, reuseWorktree bool) [][]string {
252 var cmds [][]string
253 ref := fmt.Sprintf("refs/pull/%d/head", pr.Number)
254
255 if opts.Detach {
256 fetchCmd := []string{"fetch", baseURLOrName, ref, "--no-tags"}
257 return detachCmds(fetchCmd, opts.Worktree, reuseWorktree)
258 }
259
260 localBranch := pr.HeadRefName
261 if opts.BranchName != "" {
262 localBranch = opts.BranchName
263 } else if pr.HeadRefName == defaultBranch {
264 // avoid naming the new branch the same as the default branch
265 localBranch = fmt.Sprintf("%s/%s", pr.HeadRepositoryOwner.Login, localBranch)
266 }
267
268 currentBranch, _ := opts.Branch()
269 if opts.Worktree != "" {
270 if reuseWorktree {
271 // FETCH_HEAD is per-worktree, and git refuses to update a branch via
272 // refspec while it is checked out, so fetch to FETCH_HEAD inside the worktree.
273 cmds = append(cmds, []string{"-C", opts.Worktree, "fetch", baseURLOrName, ref, "--no-tags"})
274 if localBranchExists(opts.GitClient, localBranch) {
275 cmds = append(cmds, []string{"-C", opts.Worktree, "checkout", localBranch})
276 cmds = append(cmds, syncBranchCmds(opts.Worktree, "FETCH_HEAD", opts.Force)...)
277 } else {
278 cmds = append(cmds, []string{"-C", opts.Worktree, "checkout", "-b", localBranch, "FETCH_HEAD"})
279 }
280 } else {
281 fetchCmd := []string{"fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, localBranch), "--no-tags"}
282 if opts.Force {
283 fetchCmd = append(fetchCmd, "--force")
284 }
285 cmds = append(cmds, fetchCmd)
286 cmds = append(cmds, []string{"worktree", "add", "--", opts.Worktree, localBranch})
287 }
288 } else if localBranch == currentBranch {
289 // PR head matches currently checked out branch
290 cmds = append(cmds, []string{"fetch", baseURLOrName, ref, "--no-tags"})
291 cmds = append(cmds, syncBranchCmds("", "FETCH_HEAD", opts.Force)...)
292 } else {
293 // TODO: check if non-fast-forward and suggest to use `--force`
294 fetchCmd := []string{"fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, localBranch), "--no-tags"}
295 if opts.Force {
296 fetchCmd = append(fetchCmd, "--force")
297 }
298 cmds = append(cmds, fetchCmd)
299 cmds = append(cmds, []string{"checkout", localBranch})
300 }
301
302 remote := baseURLOrName
303 mergeRef := ref
304 if pr.MaintainerCanModify && pr.HeadRepository != nil {
305 headRepo := ghrepo.NewWithHost(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name, repoHost)
306 remote = ghrepo.FormatRemoteURL(headRepo, protocol)
307 mergeRef = fmt.Sprintf("refs/heads/%s", pr.HeadRefName)
308 }

Callers 1

checkoutRunFunction · 0.85

Calls 6

NewWithHostFunction · 0.92
FormatRemoteURLFunction · 0.92
detachCmdsFunction · 0.85
localBranchExistsFunction · 0.85
syncBranchCmdsFunction · 0.85

Tested by

no test coverage detected