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

Function cmdsForMissingRemote

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

Source from the content-addressed store, hash-verified

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

Callers 1

checkoutRunFunction · 0.85

Calls 6

NewWithHostFunction · 0.92
FormatRemoteURLFunction · 0.92
detachCmdsFunction · 0.85
syncBranchCmdsFunction · 0.85
HasLocalBranchMethod · 0.65

Tested by

no test coverage detected